Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if ($action == 'get_deals') {
- if (!is_writable('./tmp')) {
- acp_error($lang['TMP_not_writable']);
- }
- $sql = "SELECT SQL_CACHE * FROM xml_feeds
- WHERE feed_active = 'yes'";
- if ( ! ($result = mysql_query($sql) ) ) {
- die('Could not execute "fetch xml feeds" MySQL query.');
- }
- $rows = mysql_num_rows($result);
- if ($rows == 0) {
- acp_error($lang['No_active_XML_feeds']);
- }
- $required_city = get_option('required_city');
- $required_initial_price = get_option('required_initial_price');
- $required_picture = get_option('required_picture');
- $required_purchases = get_option('required_purchases'); // new
- $required_category = get_option('required_category');
- $required_start = get_option('required_start');
- $required_end = get_option('required_end');
- $required_description = get_option('required_description'); // new
- $required_lat = get_option('required_lat'); // new
- $required_lon = get_option('required_lon'); // new
- acp_header($lang['Auto_deals_get']);
- while($row = mysql_fetch_array($result)) {
- $has_expired = false;
- $offers_updated_num = $offers_added_num = 0;
- $feed_url = $row['feed_url'];
- $feed_type = $row['feed_type'];
- $url_structure = $row['url_structure'];
- $compression = $row['compression'];
- $service_xml = readfile_chunked($feed_url, false, $compression);
- $feed_ID = (int) $row['feed_ID'];
- $feed_hash = $row['hash'];
- $new_hash = md5($service_xml);
- if ($feed_hash == $new_hash) {
- echo '<p><em>'.$lang['No_changes_XML_feed'].': '.e($feed_url).'</em></p>';
- }
- else {
- mysql_query("UPDATE xml_feeds SET last_checked = '".now()."', hash = '".mysql_real_escape_string((string) $new_hash)."'
- WHERE feed_ID = '".(int) $row['feed_ID']."' LIMIT 1;")
- or die('Could not execute MySQL query.');
- if (empty($service_xml)) { echo '<p><em>'.$lang['Blank_XML_feed'].': '.e($feed_url).'</em></p>'; }
- else { echo '<h2>'.$feed_url.':</h2><textarea cols="75" rows="15">'; }
- if (!empty($service_xml)) {
- $doc = new DOMDocument();
- $doc->loadXML($service_xml, LIBXML_PARSEHUGE|LIBXML_DTDATTR|LIBXML_COMPACT|LIBXML_NOBLANKS|LIBXML_NOCDATA|LIBXML_NSCLEAN);
- $xpath = new DOMXpath($doc);
- $deal_title_tag = (!empty($row['title'])) ? $xpath->query($row['title']) : '';
- $deal_url_tag = (!empty($row['url'])) ? $xpath->query($row['url']) : '';
- $deal_location_tag = $deal_city_tag = (!empty($row['city'])) ? $xpath->query($row['city']) : '';
- $deal_price_tag = (!empty($row['price'])) ? $xpath->query($row['price']) : '';
- $deal_previous_price_tag = (!empty($row['previous_price'])) ? $xpath->query($row['previous_price']) : '';
- $deal_image_tag = (!empty($row['image'])) ? $xpath->query($row['image']) : '';
- $deal_description_tag = (!empty($row['description'])) ? $xpath->query($row['description']) : '';
- $deal_store_tag = $deal_shop_tag = (!empty($row['shop'])) ? $xpath->query($row['shop']) : '';
- $deal_category_tag = (!empty($row['category'])) ? $xpath->query($row['category']) : '';
- $deal_active_tag = (!empty($row['active'])) ? $xpath->query($row['active']) : '';
- $deal_purchases_tag = (!empty($row['purchases'])) ? $xpath->query($row['purchases']) : '';
- $deal_start_tag = (!empty($row['start'])) ? $xpath->query($row['start']) : '';
- $deal_end_tag = (!empty($row['end'])) ? $xpath->query($row['end']) : '';
- $deal_latitude_tag = (!empty($row['latitude'])) ? $xpath->query($row['latitude']) : '';
- $deal_longitude_tag = (!empty($row['longitude'])) ? $xpath->query($row['longitude']) : '';
- $total_deals = $deal_title_tag->length;
- for($i=0; $i<$total_deals; $i++) {
- $insert_deal = true;
- $deal_image = $deal_image_tag->item($i)->nodeValue;
- $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)))) : '';
- $deal_url = $deal_url_orig = (is_object($deal_url_tag) && !empty($deal_url_tag->item($i)->nodeValue)) ? trim($deal_url_tag->item($i)->nodeValue) : '';
- $deal_location = $deal_city = (is_object($deal_location_tag) && !empty($deal_location_tag->item($i)->nodeValue)) ? trim($deal_location_tag->item($i)->nodeValue) : '';
- $deal_price = (is_object($deal_price_tag) && !empty($deal_price_tag->item($i)->nodeValue)) ? trim($deal_price_tag->item($i)->nodeValue) : '';
- $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) : '';
- $deal_discount = calculate_discount($deal_price, $deal_previous_price);
- $deal_description = (is_object($deal_description_tag) && !empty($deal_description_tag->item($i)->nodeValue)) ? trim($deal_description_tag->item($i)->nodeValue) : '';
- $deal_store = (is_object($deal_store_tag) && !empty($deal_store_tag->item($i)->nodeValue)) ? trim($deal_store_tag->item($i)->nodeValue) : '';
- $deal_category = (is_object($deal_category_tag) && !empty($deal_category_tag->item($i)->nodeValue)) ? trim($deal_category_tag->item($i)->nodeValue) : '';
- $deal_active = (is_object($deal_active_tag) && !empty($deal_active_tag->item($i)->nodeValue)) ? trim($deal_active_tag->item($i)->nodeValue) : '';
- $deal_purchases = (is_object($deal_purchases_tag) && !empty($deal_purchases_tag->item($i)->nodeValue)) ? trim($deal_purchases_tag->item($i)->nodeValue) : '';
- $deal_start = (is_object($deal_start_tag) && !empty($deal_start_tag->item($i)->nodeValue)) ? trim($deal_start_tag->item($i)->nodeValue) : '';
- $deal_end = (is_object($deal_end_tag) && !empty($deal_end_tag->item($i)->nodeValue)) ? trim($deal_end_tag->item($i)->nodeValue) : '';
- $deal_latitude = (is_object($deal_latitude_tag) && !empty($deal_latitude_tag->item($i)->nodeValue)) ? trim($deal_latitude_tag->item($i)->nodeValue) : '';
- $deal_longitude = (is_object($deal_longitude_tag) && !empty($deal_longitude_tag->item($i)->nodeValue)) ? trim($deal_longitude_tag->item($i)->nodeValue) : '';
- if ( (!empty($deal_title) && !empty($deal_price) && !empty($deal_url)) &&
- (empty($deal_active) || strtolower($deal_active) == 'active' || strtolower($deal_active) == 'true')) { // insert only active deals
- if ($required_city == 'true' && empty($deal_city)) {
- $insert_deal = false;
- }
- if ($required_initial_price == 'true' && empty($deal_previous_price)) {
- $insert_deal = false;
- }
- if ($required_picture == 'true' && empty($deal_image)) {
- $insert_deal = false;
- }
- if ($required_category == 'true' && empty($deal_category)) {
- $insert_deal = false;
- }
- if ($required_start == 'true' && empty($deal_start)) {
- $insert_deal = false;
- }
- if ($required_end == 'true' && empty($deal_end)) {
- $insert_deal = false;
- }
- if ($required_purchases == 'true' && empty($deal_purchases)) {
- $insert_deal = false;
- }
- if ($required_description == 'true' && empty($deal_description)) {
- $insert_deal = false;
- }
- if ($required_lat == 'true' && empty($deal_latitude)) {
- $insert_deal = false;
- }
- if ($required_lon == 'true' && empty($deal_longitude)) {
- $insert_deal = false;
- }
- if (!empty($deal_end) && countdown($deal_end) == $lang['Expired']) {
- $has_expired = true;
- $insert_deal = false;
- }
- // check if deal site is disabled
- if ($feed_type == 'simple') {
- $deal_store = $store_ID = (int) $row['shop'];
- }
- else {
- $store_ID = get_shop_ID($deal_store);
- }
- if (ShopStatus($store_ID) === false) {
- $insert_deal = false;
- }
- if ($insert_deal === false && $has_expired === false) {
- echo "\n".sprintf($lang['Offer_ignored_by_rules'], substr($deal_title,0,50))."\n";
- }
- if ($insert_deal === false && $has_expired === true) {
- echo "\n".sprintf($lang['Found_expired_offer'], substr($deal_title,0,50))."\n";
- }
- if ($insert_deal === true) {
- // check if deal already exists
- $sql0 = "SELECT SQL_CACHE deal_ID, purchases, feed_ID
- FROM deals
- WHERE slug = '".mysql_real_escape_string((string) clean_slug($deal_title))."'
- LIMIT 1;";
- if ( ! ( $result0 = mysql_query($sql0) ) ) {
- die('Could not execute "deal already exists" MySQL query.');
- }
- $rows0 = mysql_num_rows($result0);
- $row0 = mysql_fetch_array($result0);
- if ($rows0 == 0) {
- if ($feed_type == 'simple') {
- $deal_url = str_replace('?', '%3F', $deal_url);
- $url_structure = str_replace('{url}', $deal_url, $url_structure);
- $deal_url = $url_structure;
- if (empty($deal_url)) {
- $deal_url = $deal_url_orig;
- }
- }
- if (empty($deal_end)) { $deal_end = '0000-00-00 00:00:00'; }
- $deal_price = NoRound($deal_price,0);
- $deal_previous_price = (!empty($deal_previous_price)) ? NoRound($deal_previous_price,0) : 0;
- $deal_slug = clean_slug($deal_title);
- mysql_query("INSERT INTO deals (deal_ID, title, slug, premium_deal, image, discount, description,
- previous_price, price, purchases, end, start, status, url, date_added, hits, latitude, longitude, feed_ID)
- VALUES ('0',
- '".mysql_real_escape_string((string) $deal_title)."',
- '".mysql_real_escape_string((string) $deal_slug)."',
- 'no',
- '".mysql_real_escape_string((string) $deal_image)."',
- '".mysql_real_escape_string((string) $deal_discount)."',
- '".mysql_real_escape_string((string) $deal_description)."',
- '".mysql_real_escape_string((string) $deal_previous_price)."',
- '".mysql_real_escape_string((string) $deal_price)."',
- '".mysql_real_escape_string((string) $deal_purchases)."',
- '".mysql_real_escape_string((string) $deal_end)."',
- '".mysql_real_escape_string((string) $deal_start)."',
- 'active',
- '".mysql_real_escape_string((string) $deal_url)."',
- '".now()."',
- '0',
- '".mysql_real_escape_string((string) $deal_latitude)."',
- '".mysql_real_escape_string((string) $deal_longitude)."',
- '".(int) $feed_ID."');");
- $deal_insert_id = mysql_insert_id();
- mysql_query("INSERT INTO deals_shops (deal_ID, shop_ID)
- VALUES ('".(int) $deal_insert_id."', '".(int) $store_ID."');");
- $get_category_ID = get_category_ID($deal_category);
- if (!empty($get_category_ID) && $get_category_ID <> 0) {
- mysql_query("INSERT INTO deals_categories (deal_ID, category_ID)
- VALUES ('".(int) $deal_insert_id."', '".(int) $get_category_ID."');");
- }
- $get_location_ID = get_city_ID($deal_location);
- if (!empty($get_location_ID) && $get_location_ID <> 0) {
- mysql_query("INSERT INTO deals_locations (deal_ID, location_ID)
- VALUES ('".(int) $deal_insert_id."', '".(int) $get_location_ID."');");
- }
- echo $lang['New_offer_added'].': '.$deal_title."\n\n";
- $offers_added_num++;
- } // end rows0
- else {
- if (($row0['purchases'] <> $deal_purchases) && !empty($deal_purchases) && ($row0['feed_ID'] == $feed_ID)) {
- mysql_query("UPDATE deals SET purchases = '".(int) $deal_purchases."' WHERE deal_ID = '".(int) $row0['deal_ID']."' LIMIT 1;");
- echo $lang['Offer_updated'].': '.$deal_title."\n\n";
- $offers_updated_num++;
- }
- }
- } // end insert deal
- } // end valid feed (deals array check)
- } // end for loop
- unset($doc);
- } // empty service xml
- echo '</textarea>';
- echo '<p>'.sprintf($lang['Offers_added_updated'], $offers_added_num, $offers_updated_num).'</p>';
- } // end hash check
- $has_expired = false;
- $offers_updated_num = $offers_added_num = 0;
- unset($service_xml);
- } // end while loop
- $time_end = microtime_float();
- $time = $time_end - $time_start;
- echo '<h2>'.sprintf($lang['Deals_get_finished'], round($time,5)).'</h2>';
- update_option('last-auto-deals-run', now());
- acp_footer();
- } // end get_deals
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement