function BuildPages(&$pages, $callback = null, $all = FALSE) { global $DB, $C, $L; // One at a time please $wouldblock = FALSE; $fd = fopen("{$GLOBALS['BASE_DIR']}/data/_build_lock", 'w'); flock($fd, LOCK_EX | LOCK_NB, $wouldblock); if ($wouldblock) { return; } // Clear old build history $DB->Update('DELETE FROM `tx_build_history` WHERE `date_start` < DATE_ADD(?, INTERVAL -14 DAY)', array(MYSQL_NOW)); $DB->Update('INSERT INTO `tx_build_history` VALUES (?,?,?,?,?,?,?)', array(null, MYSQL_NOW, null, null, count($pages), 0, null)); $GLOBALS['build_history_id'] = $DB->InsertID(); if (!preg_match('~^\\d\\d\\d$~', $C['page_permissions'])) { $C['page_permissions'] = $GLOBALS['FILE_PERMISSIONS']; } else { $C['page_permissions'] = octdec('0' . $C['page_permissions']); } // Clear records of currently used galleries on these pages ClearUsedGalleries($pages, $all); // Process the clicklog ProcessClickLog(); // Cache icons if (!isset($GLOBALS['ICON_CACHE'])) { $GLOBALS['ICON_CACHE'] =& $DB->FetchAll('SELECT * FROM `tx_icons`', null, 'icon_id'); } // Cache categories by tag if (!isset($GLOBALS['CATEGORY_CACHE_TAG'])) { $GLOBALS['CATEGORY_CACHE_TAG'] =& $DB->FetchAll('SELECT * FROM `tx_categories` ORDER BY `name`', null, 'tag'); } // Cache categories by id if (!isset($GLOBALS['CATEGORY_CACHE_ID'])) { $GLOBALS['CATEGORY_CACHE_ID'] =& $DB->FetchAll('SELECT * FROM `tx_categories`', null, 'category_id'); $GLOBALS['CATEGORY_CACHE_ID'][''] = array('name' => $L['MIXED'], 'category_id' => 0); } // Cache sponsors by id if (!isset($GLOBALS['SPONSOR_CACHE'])) { $GLOBALS['SPONSOR_CACHE'] =& $DB->FetchAll('SELECT * FROM `tx_sponsors`', null, 'sponsor_id'); } // Clear data on galleries used during the previous build $DB->Update('UPDATE `tx_gallery_used` SET `this_build`=0,`new`=0'); // Remove galleries scheduled for deletion $result = $DB->Query('SELECT * FROM `tx_galleries` WHERE `date_deletion` <= ?', array(MYSQL_NOW)); while ($gallery = $DB->NextRow($result)) { DeleteGallery($gallery['gallery_id'], $gallery); } $DB->Free($result); // Delete old submitted galleries that are currently holding $result = $DB->Query('SELECT * FROM `tx_galleries` WHERE `status`=? AND `type`=? AND `date_displayed` <= SUBDATE(?, INTERVAL ? DAY)', array('holding', 'submitted', MYSQL_NOW, $C['submitted_hold'])); while ($gallery = $DB->NextRow($result)) { DeleteGallery($gallery['gallery_id'], $gallery); } $DB->Free($result); // Rotate permanent galleries from holding back to approved queue $DB->Update('UPDATE `tx_galleries` SET ' . "`status`='approved', " . "`date_displayed`=NULL, " . "`build_counter`=IF(?, 0, `build_counter`), " . "`used_counter`=IF(?, 0, `used_counter`), " . "`clicks`=IF(?, 0, `clicks`) " . "WHERE `status`='holding' AND `type`='permanent' AND `date_displayed` <= SUBDATE(?, INTERVAL ? DAY)", array(intval($C['reset_on_rotate']), intval($C['reset_on_rotate']), intval($C['reset_on_rotate']), MYSQL_NOW, $C['permanent_hold'])); // Count total thumbs and galleries $GLOBALS['_totals'] = $DB->Row("SELECT COUNT(*) AS `galleries`,SUM(`thumbnails`) AS `thumbnails` FROM `tx_galleries` WHERE `status` IN ('approved','used')"); $GLOBALS['_totals']['categories'] = count($GLOBALS['CATEGORY_CACHE_ID']) - 1; // Build each page foreach ($pages as $page) { $page_all = $DB->Row('SELECT * FROM `tx_pages` WHERE `page_id`=?', array($page['page_id'])); if ($page_all['locked'] && !$GLOBALS['override_page_lock']) { continue; } if ($callback) { call_user_func($callback, $page_all); } $DB->Update('DELETE FROM `tx_gallery_used_page`'); $DB->Update('DELETE FROM `tx_ads_used_page`'); $DB->Update('UPDATE `tx_build_history` SET `current_page_url`=? WHERE `history_id`=?', array($page_all['page_url'], $GLOBALS['build_history_id'])); BuildPage($page_all); $DB->Update('UPDATE `tx_build_history` SET `pages_built`=`pages_built`+1 WHERE `history_id`=?', array($GLOBALS['build_history_id'])); unset($page_all); unset($page); } // Mark newly selected galleries as used // Update counters for galleries used this build $DB->Update('UPDATE `tx_galleries` JOIN `tx_gallery_used` USING (`gallery_id`) SET ' . '`build_counter`=`build_counter`+1, ' . '`used_counter`=`used_counter`+1, ' . '`times_selected`=IF(`new`=1, `times_selected`+1, `times_selected`), ' . '`status`=?, ' . '`date_displayed`=IF(`new`=1, ?, `date_displayed`) ' . 'WHERE `this_build`=1', array('used', MYSQL_NOW)); // Move no longer used galleries to holding queue (or rotate back to approved if permanent holding period is 0) if ($C['permanent_hold'] == 0) { $DB->Update('UPDATE `tx_galleries` LEFT JOIN `tx_gallery_used` USING (`gallery_id`) SET ' . "`status`=IF(`type`='permanent', 'approved', 'holding'), " . "`date_displayed`=IF(`type`='permanent', NULL, `date_displayed`), " . "`date_approved`=IF(`type`='permanent', NULL, `date_approved`), " . "`date_scheduled`=IF(`type`='permanent', NULL, `date_scheduled`), " . "`build_counter`=IF(`type`='permanent', IF(?, 0, `build_counter`+1), `build_counter`+1), " . "`used_counter`=IF(`type`='permanent', IF(?, 0, `used_counter`), `used_counter`), " . "`clicks`=IF(`type`='permanent', IF(?, 0, `clicks`), `clicks`) " . "WHERE `status`='used' AND `page_id` IS NULL", array(intval($C['reset_on_rotate']), intval($C['reset_on_rotate']), intval($C['reset_on_rotate']), 'used')); } else { $DB->Update('UPDATE `tx_galleries` LEFT JOIN `tx_gallery_used` USING (`gallery_id`) SET `status`=?,`build_counter`=`build_counter`+1 WHERE `status`=? AND `page_id` IS NULL', array('holding', 'used')); } $DB->Update('UPDATE `tx_build_history` SET `date_end`=?,`current_page_url`=NULL WHERE `history_id`=?', array(gmdate(DF_DATETIME, TimeWithTz()), $GLOBALS['build_history_id'])); flock($fd, LOCK_UN); fclose($fd); @chmod("{$GLOBALS['BASE_DIR']}/data/_build_lock", 0666); }
function txCategoryDelete() { global $DB, $json, $C; VerifyPrivileges(P_CATEGORY_REMOVE, TRUE); if (!is_array($_REQUEST['category_id'])) { $_REQUEST['category_id'] = array($_REQUEST['category_id']); } foreach ($_REQUEST['category_id'] as $category_id) { $category = $DB->Row('SELECT * FROM `tx_categories` WHERE `category_id`=?', array($category_id)); // Find all of the galleries in this category $result = $DB->Query('SELECT * FROM `tx_galleries` WHERE MATCH(`categories`) AGAINST (? IN BOOLEAN MODE)', array($category['tag'])); while ($gallery = $DB->NextRow($result)) { $gallery['categories'] = RemoveCategoryTag($category['tag'], $gallery['categories']); // Delete the gallery if it is not also located in another category if ($gallery['categories'] == MIXED_CATEGORY) { DeleteGallery($gallery['gallery_id'], $gallery); } else { $DB->Update('UPDATE `tx_galleries` SET `categories`=? WHERE `gallery_id`=?', array($gallery['categories'], $gallery['gallery_id'])); } } $DB->Free($result); // Remove the category $DB->Update('DELETE FROM `tx_categories` WHERE `category_id`=?', array($category_id)); // Remove pages associated with this category $DB->Update('DELETE FROM `tx_pages` WHERE `category_id`=?', array($category['category_id'])); } echo $json->encode(array('status' => JSON_SUCCESS, 'message' => 'The selected categories have been deleted')); }
function ProcessGallery(&$gallery, &$scan, &$exception) { global $configuration, $exceptions, $penalties, $DB, $config_id, $history_id; $removed = FALSE; $message = ''; $penalty = 0x0; $reasons = array('connect' => "Connection error: {$scan['errstr']}", 'forward' => "Redirecting URL: {$scan['status']}", 'broken' => "Broken URL: {$scan['status']}", 'blacklist' => "Blacklisted data: " . htmlspecialchars($scan['blacklist_item']), 'norecip' => "No reciprocal link found", 'no2257' => "No 2257 code found", 'excessivelinks' => "Too many links found on the gallery: {$scan['links']}", 'thumbchange' => "Thumbnail count has changed from {$gallery['thumbnails_old']} to {$scan['thumbnails']}", 'pagechange' => "Page content has changed", 'content_server' => 'The gallery content is not hosted on the same server as the gallery', 'badformat' => 'The gallery format is not allowed in this category'); // Determine the most strict penalty based on the infractions that were found foreach ($exceptions as $key => $value) { if ($exception & $value && $configuration['action_' . $key] >= $penalty) { $message = $reasons[$key]; $penalty = intval($configuration['action_' . $key], 16); } } // Blacklist if ($penalty & $penalties['blacklist']) { $action = 'Blacklisted'; $removed = TRUE; AutoBlacklist($gallery); DeleteGallery($gallery['gallery_id'], $gallery); // Update history $DB->Update('UPDATE `tx_scanner_history` SET `exceptions`=`exceptions`+1,`blacklisted`=`blacklisted`+1 WHERE `history_id`=?', array($history_id)); } else { if ($penalty & $penalties['delete']) { $action = 'Deleted'; $removed = TRUE; DeleteGallery($gallery['gallery_id'], $gallery); // Update history $DB->Update('UPDATE `tx_scanner_history` SET `exceptions`=`exceptions`+1,`deleted`=`deleted`+1 WHERE `history_id`=?', array($history_id)); } else { if ($penalty & $penalties['disable']) { $action = 'Disabled'; // Don't re-disable a gallery if ($gallery['status'] != 'disabled') { //$DB->Update('UPDATE `tx_galleries` SET `status`=?,`admin_comments`=? WHERE `gallery_id`=?', array('disabled', $message, $gallery['gallery_id'])); $gallery['previous_status'] = $gallery['status']; $gallery['status'] = 'disabled'; $gallery['admin_comments'] = $message; } // Update history $DB->Update('UPDATE `tx_scanner_history` SET `exceptions`=`exceptions`+1,`disabled`=`disabled`+1 WHERE `history_id`=?', array($history_id)); } else { if ($penalty & $penalties['report']) { $action = 'Unchanged'; // Update history $DB->Update('UPDATE `tx_scanner_history` SET `exceptions`=`exceptions`+1 WHERE `history_id`=?', array($history_id)); } else { // Do nothing $exception = 0x0; return $removed; } } } } $DB->Update('INSERT INTO `tx_scanner_results` VALUES (?,?,?,?,?,?,?)', array($config_id, $gallery['gallery_id'], $gallery['gallery_url'], $scan['status'], gmdate(DF_DATETIME, TimeWithTz()), $action, $message)); return $removed; }
function &DeletePartner($username, $partner = null) { global $DB; if ($partner == null) { $partner = $DB->Row('SELECT * FROM `tx_partners` WHERE `username`=?', array($username)); } if ($partner) { // Remove this partner's galleries $result = $DB->Query('SELECT * FROM `tx_galleries` WHERE `partner`=?', array($partner['username'])); while ($gallery = $DB->NextRow($result)) { DeleteGallery($gallery['gallery_id'], $gallery); } $DB->Free($result); $DB->Update('DELETE FROM `tx_partners` WHERE `username`=?', array($partner['username'])); $DB->Update('DELETE FROM `tx_partner_icons` WHERE `username`=?', array($partner['username'])); $DB->Update('DELETE FROM `tx_partner_fields` WHERE `username`=?', array($partner['username'])); } return $partner; }
function txGalleryImport() { global $DB, $C; VerifyPrivileges(P_GALLERY_ADD); $defaults = array('gallery_url' => null, 'description' => null, 'keywords' => null, 'thumbnails' => 0, 'email' => $C['from_email'], 'nickname' => null, 'weight' => $C['gallery_weight'], 'clicks' => 0, 'submit_ip' => $_SERVER['REMOTE_ADDR'], 'gallery_ip' => '', 'sponsor_id' => !empty($_REQUEST['sponsor']) ? $_REQUEST['sponsor'] : null, 'type' => $_REQUEST['type'], 'format' => $_REQUEST['format'], 'status' => $_REQUEST['status'], 'previous_status' => null, 'date_scanned' => null, 'date_added' => MYSQL_NOW, 'date_approved' => null, 'date_scheduled' => null, 'date_displayed' => null, 'date_deletion' => null, 'partner' => null, 'administrator' => $_SERVER['REMOTE_USER'], 'admin_comments' => null, 'page_hash' => null, 'has_recip' => 0, 'has_preview' => 0, 'allow_scan' => 1, 'allow_preview' => 1, 'times_selected' => 0, 'used_counter' => 0, 'build_counter' => 0, 'tags' => null, 'categories' => null, 'preview_url' => null, 'dimensions' => null); $v = new Validator(); if (empty($_REQUEST['type']) && !in_array('type', $_REQUEST['fields'])) { $v->SetError('You indicated that the gallery type should come from the import data, but that field has not been defined'); } if (empty($_REQUEST['format']) && !in_array('format', $_REQUEST['fields'])) { $v->SetError('You indicated that the gallery format should come from the import data, but that field has not been defined'); } // Make sure only one of each field is submitted $field_counts = array_count_values($_REQUEST['fields']); foreach ($field_counts as $field_name => $field_count) { if ($field_name != 'IGNORE' && $field_count > 1) { $v->SetError("The {$field_name} field has been specified more than once"); } } if (!$v->Validate()) { return $v->ValidationError('txShGalleryImportAnalyze'); } // Create/empty log files for skipped galleries FileWrite("{$GLOBALS['BASE_DIR']}/data/skipped-cat.txt", ''); FileWrite("{$GLOBALS['BASE_DIR']}/data/skipped-dupe.txt", ''); // Initialize variables $imported = 0; $duplicates = 0; $no_matching_cat = 0; $lines = file(SafeFilename("{$GLOBALS['BASE_DIR']}/data/{$_REQUEST['filename']}")); $sponsors =& $DB->FetchAll('SELECT * FROM `tx_sponsors`', null, 'name'); $partners = array(); $columns = $DB->GetColumns('tx_gallery_fields'); foreach ($lines as $line_number => $line) { $line_number++; $line = trim($line); if (IsEmptyString($line)) { continue; } $data = explode('|', $line); $gallery = array(); foreach ($_REQUEST['fields'] as $index => $field) { $gallery[$field] = trim($data[$index]); } $gallery = array_merge($defaults, $gallery); // Check for and handle duplicates $dupes =& $DB->FetchAll('SELECT `gallery_id` FROM `tx_galleries` WHERE `gallery_url`=?', array($gallery['gallery_url'])); if (count($dupes) > 0) { switch ($_REQUEST['duplicates']) { case 'replace': // Remove existing so it can be replaced with new foreach ($dupes as $dupe) { DeleteGallery($dupe['gallery_id']); } break; case 'allow': // Allow duplicate galleries, so do nothing here break; default: // Go to next line if this is a duplicate FileAppend("{$GLOBALS['BASE_DIR']}/data/skipped-dupe.txt", sprintf("%-6d %s", $line_number, $line)); $duplicates++; continue 2; break; } } // Check for valid categories $skipped = array(); $category_tags = CategoryTagsFromList($gallery['categories'], $skipped); if ($category_tags == MIXED_CATEGORY) { switch ($_REQUEST['bad_category']) { case 'create': if (IsEmptyString($gallery['categories'])) { FileAppend("{$GLOBALS['BASE_DIR']}/data/skipped-cat.txt", sprintf("%-6d %s", $line_number, $line)); $no_matching_cat++; continue 2; } $category_tags = CreateCategories($gallery['categories']); break; case 'force': $category_tags = MIXED_CATEGORY . " " . $_REQUEST['forced_category']; break; default: FileAppend("{$GLOBALS['BASE_DIR']}/data/skipped-cat.txt", sprintf("%-6d %s", $line_number, $line)); $no_matching_cat++; continue 2; break; } } if (count($skipped) && $_REQUEST['bad_category'] == 'create') { $category_tags = join(' ', array_unique(array($category_tags, CreateCategories($gallery['categories'])))); } // Setup the sponsor if (empty($_REQUEST['sponsor']) && $_REQUEST['add_sponsor'] && $gallery['sponsor_id'] != null && !isset($sponsors[$gallery['sponsor_id']])) { $DB->Update('INSERT INTO `tx_sponsors` VALUES (?,?,?)', array(null, $gallery['sponsor_id'], null)); $sponsors[$gallery['sponsor_id']]['sponsor_id'] = $DB->InsertID(); $gallery['sponsor_id'] = $sponsors[$gallery['sponsor_id']]['sponsor_id']; } else { if (empty($_REQUEST['sponsor']) && $gallery['sponsor_id'] != null && isset($sponsors[$gallery['sponsor_id']])) { $gallery['sponsor_id'] = $sponsors[$gallery['sponsor_id']]['sponsor_id']; } } // Check for valid format $gallery['format'] = strtolower($gallery['format']); if (!in_array($gallery['format'], array('pictures', 'movies'))) { $gallery['format'] = 'pictures'; } // Check for valid type $gallery['type'] = strtolower($gallery['type']); if (!in_array($gallery['type'], array('submitted', 'permanent'))) { $gallery['type'] = 'submitted'; } // Check date scheduled for errors if ($gallery['date_scheduled'] != null && !preg_match(RE_DATETIME, $gallery['date_scheduled'])) { $gallery['date_scheduled'] = null; } // Check date of deletion for errors if ($gallery['date_deletion'] != null && !preg_match(RE_DATETIME, $gallery['date_deletion'])) { $gallery['date_deletion'] = null; } // Has a preview thumbnail if (!empty($gallery['preview_url'])) { $gallery['has_preview'] = 1; } // Add regular fields $DB->Update('INSERT INTO `tx_galleries` VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', array(null, $gallery['gallery_url'], $gallery['description'], FormatSpaceSeparated($gallery['keywords']), $gallery['thumbnails'], $gallery['email'], $gallery['nickname'], $gallery['weight'], $gallery['clicks'], $gallery['submit_ip'], $gallery['gallery_ip'], $gallery['sponsor_id'], $gallery['type'], $gallery['format'], $gallery['status'], $gallery['previous_status'], $gallery['date_scanned'], $gallery['date_added'], $gallery['date_approved'], $gallery['date_scheduled'], $gallery['date_displayed'], $gallery['date_deletion'], $gallery['partner'], $gallery['administrator'], $gallery['admin_comments'], $gallery['page_hash'], $gallery['has_recip'], $gallery['has_preview'], $gallery['allow_scan'], $gallery['allow_preview'], $gallery['times_selected'], $gallery['used_counter'], $gallery['build_counter'], FormatSpaceSeparated($gallery['tags']), $category_tags)); $gallery['gallery_id'] = $DB->InsertID(); // Add user defined fields $query_data = CreateUserInsert('tx_gallery_fields', $gallery, $columns); $DB->Update('INSERT INTO `tx_gallery_fields` VALUES (' . $query_data['bind_list'] . ')', $query_data['binds']); // Has a preview thumbnail if (!empty($gallery['preview_url'])) { $DB->Update('INSERT INTO `tx_gallery_previews` VALUES (?,?,?,?)', array(null, $gallery['gallery_id'], $gallery['preview_url'], $gallery['dimensions'])); } // Add icons if (!IsEmptyString($gallery['icons'])) { foreach (explode(',', $gallery['icons']) as $icon) { $icon = trim($icon); if (!empty($icon)) { $icon_id = $DB->Count('SELECT `icon_id` FROM `tx_icons` WHERE `identifier`=?', array($icon)); if ($icon_id) { $DB->Update('INSERT INTO `tx_gallery_icons` VALUES (?,?)', array($gallery['gallery_id'], $icon_id)); } } } } if (!empty($gallery['partner'])) { $partners[$gallery['partner']]++; // Add partner icons $partner_icons =& $DB->FetchAll('SELECT * FROM `tx_partner_icons` WHERE `username`=?', array($gallery['partner'])); foreach ($partner_icons as $icon) { $DB->Update('REPLACE INTO `tx_gallery_icons` VALUES (?,?)', array($gallery['gallery_id'], $icon['icon_id'])); } } $imported++; } StoreValue('last_import', serialize($_REQUEST['fields'])); // Update partner submit counts foreach ($partners as $username => $amount) { $DB->Update('UPDATE `tx_partners` SET `submitted`=`submitted`+? WHERE `username`=?', array($amount, $username)); } $GLOBALS['message'] = "A total of {$imported} galleries have been imported<br />" . "<a href=\"index.php?r=txShSkippedImport&type=dupe\" class=\"window {title: 'Skipped Galleries'}\">{$duplicates} galleries</a> were skipped because they were duplicates<br />" . "<a href=\"index.php?r=txShSkippedImport&type=cat\" class=\"window {title: 'Skipped Galleries'}\">{$no_matching_cat} galleries</a> were skipped because they did not fit into an existing category"; txShGalleryImport(); }