Advertisement
Guest User

Untitled

a guest
Jul 28th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.15 KB | None | 0 0
  1. if ($action == 'get_deals') {
  2.     if (!is_writable('./tmp')) {
  3.         acp_error($lang['TMP_not_writable']);
  4.     }
  5.  
  6.     $sql = "SELECT SQL_CACHE * FROM xml_feeds
  7.             WHERE feed_active = 'yes'";
  8.  
  9.     if ( ! ($result = mysql_query($sql) ) ) {
  10.         die('Could not execute "fetch xml feeds" MySQL query.');
  11.     }
  12.  
  13.     $rows = mysql_num_rows($result);
  14.     if ($rows == 0) {
  15.         acp_error($lang['No_active_XML_feeds']);
  16.     }
  17.  
  18.     $required_city = get_option('required_city');
  19.     $required_initial_price = get_option('required_initial_price');
  20.     $required_picture = get_option('required_picture');
  21.     $required_purchases = get_option('required_purchases'); // new
  22.     $required_category = get_option('required_category');
  23.     $required_start = get_option('required_start');
  24.     $required_end = get_option('required_end');
  25.     $required_description = get_option('required_description'); // new
  26.     $required_lat = get_option('required_lat'); // new
  27.     $required_lon = get_option('required_lon'); // new
  28.  
  29.     acp_header($lang['Auto_deals_get']);
  30.  
  31.     while($row = mysql_fetch_array($result)) {
  32.         $has_expired = false;
  33.         $offers_updated_num = $offers_added_num = 0;
  34.         $feed_url = $row['feed_url'];
  35.         $feed_type = $row['feed_type'];
  36.         $url_structure = $row['url_structure'];
  37.         $compression = $row['compression'];
  38.         $service_xml = readfile_chunked($feed_url, false, $compression);
  39.         $feed_ID = (int) $row['feed_ID'];
  40.         $feed_hash = $row['hash'];
  41.         $new_hash = md5($service_xml);
  42.  
  43.         if ($feed_hash == $new_hash) {
  44.             echo '<p><em>'.$lang['No_changes_XML_feed'].': '.e($feed_url).'</em></p>';
  45.         }
  46.         else {
  47.             mysql_query("UPDATE xml_feeds SET last_checked = '".now()."', hash = '".mysql_real_escape_string((string) $new_hash)."'
  48.                         WHERE feed_ID = '".(int) $row['feed_ID']."' LIMIT 1;")
  49.             or die('Could not execute MySQL query.');
  50.  
  51.             if (empty($service_xml)) { echo '<p><em>'.$lang['Blank_XML_feed'].': '.e($feed_url).'</em></p>'; }
  52.             else { echo '<h2>'.$feed_url.':</h2><textarea cols="75" rows="15">'; }
  53.  
  54.             if (!empty($service_xml)) {
  55.                 $doc = new DOMDocument();
  56.                 $doc->loadXML($service_xml, LIBXML_PARSEHUGE|LIBXML_DTDATTR|LIBXML_COMPACT|LIBXML_NOBLANKS|LIBXML_NOCDATA|LIBXML_NSCLEAN);
  57.  
  58.                 $xpath = new DOMXpath($doc);
  59.  
  60.                 $deal_title_tag = (!empty($row['title'])) ? $xpath->query($row['title']) : '';
  61.                 $deal_url_tag = (!empty($row['url'])) ? $xpath->query($row['url']) : '';
  62.                 $deal_location_tag = $deal_city_tag = (!empty($row['city'])) ? $xpath->query($row['city']) : '';
  63.                 $deal_price_tag = (!empty($row['price'])) ? $xpath->query($row['price']) : '';
  64.                 $deal_previous_price_tag = (!empty($row['previous_price'])) ? $xpath->query($row['previous_price']) : '';
  65.                 $deal_image_tag = (!empty($row['image'])) ? $xpath->query($row['image']) : '';
  66.                 $deal_description_tag = (!empty($row['description'])) ? $xpath->query($row['description']) : '';
  67.                 $deal_store_tag = $deal_shop_tag = (!empty($row['shop'])) ? $xpath->query($row['shop']) : '';
  68.                 $deal_category_tag = (!empty($row['category'])) ? $xpath->query($row['category']) : '';
  69.                 $deal_active_tag = (!empty($row['active'])) ? $xpath->query($row['active']) : '';
  70.                 $deal_purchases_tag = (!empty($row['purchases'])) ? $xpath->query($row['purchases']) : '';
  71.                 $deal_start_tag = (!empty($row['start'])) ? $xpath->query($row['start']) : '';
  72.                 $deal_end_tag = (!empty($row['end'])) ? $xpath->query($row['end']) : '';
  73.                 $deal_latitude_tag = (!empty($row['latitude'])) ? $xpath->query($row['latitude']) : '';
  74.                 $deal_longitude_tag = (!empty($row['longitude'])) ? $xpath->query($row['longitude']) : '';
  75.  
  76.                 $total_deals = $deal_title_tag->length;
  77.                 for($i=0; $i<$total_deals; $i++) {
  78.                     $insert_deal = true;
  79.  
  80.                     $deal_image = $deal_image_tag->item($i)->nodeValue;
  81.  
  82.                 $deal_title = (is_object($deal_title_tag) && !empty($deal_title_tag->item($i)->nodeValue)) ? htmlspecialchars(strip_tags(ltrim(trim($deal_title_tag->item($i)->nodeValue)))) : '';
  83.                 $deal_url = $deal_url_orig = (is_object($deal_url_tag) && !empty($deal_url_tag->item($i)->nodeValue)) ? trim($deal_url_tag->item($i)->nodeValue) : '';
  84.                 $deal_location = $deal_city = (is_object($deal_location_tag) && !empty($deal_location_tag->item($i)->nodeValue)) ? trim($deal_location_tag->item($i)->nodeValue) : '';
  85.                 $deal_price = (is_object($deal_price_tag) && !empty($deal_price_tag->item($i)->nodeValue)) ? trim($deal_price_tag->item($i)->nodeValue) : '';
  86.                 $deal_previous_price = (is_object($deal_previous_price_tag) && !empty($deal_previous_price_tag->item($i)->nodeValue)) ? trim($deal_previous_price_tag->item($i)->nodeValue) : '';
  87.                 $deal_discount = calculate_discount($deal_price, $deal_previous_price);
  88.                 $deal_description = (is_object($deal_description_tag) && !empty($deal_description_tag->item($i)->nodeValue)) ? trim($deal_description_tag->item($i)->nodeValue) : '';
  89.                 $deal_store = (is_object($deal_store_tag) && !empty($deal_store_tag->item($i)->nodeValue)) ? trim($deal_store_tag->item($i)->nodeValue) : '';
  90.                 $deal_category = (is_object($deal_category_tag) && !empty($deal_category_tag->item($i)->nodeValue)) ? trim($deal_category_tag->item($i)->nodeValue) : '';
  91.                 $deal_active = (is_object($deal_active_tag) && !empty($deal_active_tag->item($i)->nodeValue)) ? trim($deal_active_tag->item($i)->nodeValue) : '';
  92.                 $deal_purchases = (is_object($deal_purchases_tag) && !empty($deal_purchases_tag->item($i)->nodeValue)) ? trim($deal_purchases_tag->item($i)->nodeValue) : '';
  93.                 $deal_start = (is_object($deal_start_tag) && !empty($deal_start_tag->item($i)->nodeValue)) ? trim($deal_start_tag->item($i)->nodeValue) : '';
  94.                 $deal_end = (is_object($deal_end_tag) && !empty($deal_end_tag->item($i)->nodeValue)) ? trim($deal_end_tag->item($i)->nodeValue) : '';
  95.                 $deal_latitude = (is_object($deal_latitude_tag) && !empty($deal_latitude_tag->item($i)->nodeValue)) ? trim($deal_latitude_tag->item($i)->nodeValue) : '';
  96.                 $deal_longitude = (is_object($deal_longitude_tag) && !empty($deal_longitude_tag->item($i)->nodeValue)) ? trim($deal_longitude_tag->item($i)->nodeValue) : '';
  97.  
  98.                     if ( (!empty($deal_title) && !empty($deal_price) && !empty($deal_url)) &&
  99.                         (empty($deal_active) || strtolower($deal_active) == 'active' || strtolower($deal_active) == 'true')) { // insert only active deals
  100.                         if ($required_city == 'true' && empty($deal_city)) {
  101.                             $insert_deal = false;
  102.                         }
  103.  
  104.                         if ($required_initial_price == 'true' && empty($deal_previous_price)) {
  105.                             $insert_deal = false;
  106.                         }
  107.  
  108.                         if ($required_picture == 'true' && empty($deal_image)) {
  109.                             $insert_deal = false;
  110.                         }
  111.  
  112.                         if ($required_category == 'true' && empty($deal_category)) {
  113.                             $insert_deal = false;
  114.                         }
  115.  
  116.                         if ($required_start == 'true' && empty($deal_start)) {
  117.                             $insert_deal = false;
  118.                         }
  119.  
  120.                         if ($required_end == 'true' && empty($deal_end)) {
  121.                             $insert_deal = false;
  122.                         }
  123.  
  124.                         if ($required_purchases == 'true' && empty($deal_purchases)) {
  125.                             $insert_deal = false;
  126.                         }
  127.  
  128.                         if ($required_description == 'true' && empty($deal_description)) {
  129.                             $insert_deal = false;
  130.                         }
  131.  
  132.                         if ($required_lat == 'true' && empty($deal_latitude)) {
  133.                             $insert_deal = false;
  134.                         }
  135.  
  136.                         if ($required_lon == 'true' && empty($deal_longitude)) {
  137.                             $insert_deal = false;
  138.                         }
  139.  
  140.                         if (!empty($deal_end) && countdown($deal_end) == $lang['Expired']) {
  141.                             $has_expired = true;
  142.                             $insert_deal = false;
  143.                         }
  144.  
  145.                         // check if deal site is disabled
  146.                         if ($feed_type == 'simple') {
  147.                             $deal_store = $store_ID = (int) $row['shop'];
  148.                         }
  149.                         else {
  150.                             $store_ID = get_shop_ID($deal_store);
  151.                         }
  152.  
  153.                         if (ShopStatus($store_ID) === false) {
  154.                             $insert_deal = false;
  155.                         }
  156.  
  157.                         if ($insert_deal === false && $has_expired === false) {
  158.                             echo "\n".sprintf($lang['Offer_ignored_by_rules'], substr($deal_title,0,50))."\n";
  159.                         }
  160.  
  161.                         if ($insert_deal === false && $has_expired === true) {
  162.                             echo "\n".sprintf($lang['Found_expired_offer'], substr($deal_title,0,50))."\n";
  163.                         }
  164.  
  165.                         if ($insert_deal === true) {
  166.                             // check if deal already exists
  167.                              $sql0 = "SELECT SQL_CACHE deal_ID, purchases, feed_ID
  168.                                         FROM deals
  169.                                         WHERE slug = '".mysql_real_escape_string((string) clean_slug($deal_title))."'
  170.                                         LIMIT 1;";
  171.  
  172.                              if ( ! ( $result0 = mysql_query($sql0) ) ) {
  173.                                 die('Could not execute "deal already exists" MySQL query.');
  174.                              }
  175.  
  176.                              $rows0 = mysql_num_rows($result0);
  177.                              $row0 = mysql_fetch_array($result0);
  178.  
  179.                              if ($rows0 == 0) {
  180.                                 if ($feed_type == 'simple') {
  181.                                     $deal_url = str_replace('?', '%3F', $deal_url);
  182.                                     $url_structure = str_replace('{url}', $deal_url, $url_structure);
  183.                                     $deal_url = $url_structure;
  184.        
  185.                                     if (empty($deal_url)) {
  186.                                         $deal_url = $deal_url_orig;
  187.                                     }
  188.                                 }
  189.    
  190.                                 if (empty($deal_end)) { $deal_end = '0000-00-00 00:00:00'; }
  191.  
  192.                                 $deal_price = NoRound($deal_price,0);
  193.                                 $deal_previous_price = (!empty($deal_previous_price)) ? NoRound($deal_previous_price,0) : 0;
  194.                                 $deal_slug = clean_slug($deal_title);
  195.  
  196.                                 mysql_query("INSERT INTO deals (deal_ID, title, slug, premium_deal, image, discount, description,
  197.                                         previous_price, price, purchases, end, start, status, url, date_added, hits, latitude, longitude, feed_ID)
  198.                                         VALUES ('0',
  199.                                         '".mysql_real_escape_string((string) $deal_title)."',
  200.                                         '".mysql_real_escape_string((string) $deal_slug)."',
  201.                                         'no',
  202.                                         '".mysql_real_escape_string((string) $deal_image)."',
  203.                                         '".mysql_real_escape_string((string) $deal_discount)."',
  204.                                         '".mysql_real_escape_string((string) $deal_description)."',
  205.                                         '".mysql_real_escape_string((string) $deal_previous_price)."',
  206.                                         '".mysql_real_escape_string((string) $deal_price)."',
  207.                                         '".mysql_real_escape_string((string) $deal_purchases)."',
  208.                                         '".mysql_real_escape_string((string) $deal_end)."',
  209.                                         '".mysql_real_escape_string((string) $deal_start)."',
  210.                                         'active',
  211.                                         '".mysql_real_escape_string((string) $deal_url)."',
  212.                                         '".now()."',
  213.                                         '0',
  214.                                         '".mysql_real_escape_string((string) $deal_latitude)."',
  215.                                         '".mysql_real_escape_string((string) $deal_longitude)."',
  216.                                         '".(int) $feed_ID."');");
  217.  
  218.                                 $deal_insert_id = mysql_insert_id();
  219.                                 mysql_query("INSERT INTO deals_shops (deal_ID, shop_ID)
  220.                                             VALUES ('".(int) $deal_insert_id."', '".(int) $store_ID."');");
  221.  
  222.                                 $get_category_ID = get_category_ID($deal_category);
  223.                                 if (!empty($get_category_ID) && $get_category_ID <> 0) {
  224.                                     mysql_query("INSERT INTO deals_categories (deal_ID, category_ID)
  225.                                                 VALUES ('".(int) $deal_insert_id."', '".(int) $get_category_ID."');");
  226.                                 }
  227.  
  228.                                 $get_location_ID = get_city_ID($deal_location);
  229.                                 if (!empty($get_location_ID) && $get_location_ID <> 0) {
  230.                                     mysql_query("INSERT INTO deals_locations (deal_ID, location_ID)
  231.                                                 VALUES ('".(int) $deal_insert_id."', '".(int) $get_location_ID."');");
  232.                                 }
  233.  
  234.                                 echo $lang['New_offer_added'].': '.$deal_title."\n\n";
  235.                                 $offers_added_num++;
  236.                              } // end rows0
  237.                              else {
  238.                                  if (($row0['purchases'] <> $deal_purchases) && !empty($deal_purchases) && ($row0['feed_ID'] == $feed_ID)) {
  239.                                     mysql_query("UPDATE deals SET purchases = '".(int) $deal_purchases."' WHERE deal_ID = '".(int) $row0['deal_ID']."' LIMIT 1;");
  240.                                     echo $lang['Offer_updated'].': '.$deal_title."\n\n";
  241.                                     $offers_updated_num++;
  242.                                  }
  243.                              }
  244.                         } // end insert deal
  245.                     } // end valid feed (deals array check)
  246.                 } // end for loop
  247.                 unset($doc);
  248.             } // empty service xml
  249.  
  250.             echo '</textarea>';
  251.             echo '<p>'.sprintf($lang['Offers_added_updated'], $offers_added_num, $offers_updated_num).'</p>';
  252.         } // end hash check
  253.  
  254.         $has_expired = false;
  255.         $offers_updated_num = $offers_added_num = 0;
  256.         unset($service_xml);
  257.     } // end while loop
  258.  
  259.     $time_end = microtime_float();
  260.     $time = $time_end - $time_start;
  261.  
  262.     echo '<h2>'.sprintf($lang['Deals_get_finished'], round($time,5)).'</h2>';
  263.     update_option('last-auto-deals-run', now());
  264.  
  265.     acp_footer();
  266. } // end get_deals
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement