diff --git a/classes/torrents.class.php b/classes/torrents.class.php index 56f6b888..83266cba 100644 --- a/classes/torrents.class.php +++ b/classes/torrents.class.php @@ -55,8 +55,8 @@ public static function get_groups($GroupIDs, $Return = true, $GetArtists = true, unset($GroupIDs[$i], $Found[$GroupID], $NotFound[$GroupID]); continue; } - $Data = G::$Cache->get_value($Key.$GroupID, true); - if (!empty($Data) && $Data['ver'] == CACHE::GROUP_VERSION) { + $Data = G::$Cache->get_value($Key . $GroupID, true); + if (!empty($Data) && is_array($Data) && $Data['ver'] == CACHE::GROUP_VERSION) { unset($NotFound[$GroupID]); $Found[$GroupID] = $Data['d']; } @@ -65,7 +65,6 @@ public static function get_groups($GroupIDs, $Return = true, $GetArtists = true, if (count($GroupIDs) === 0) { return array(); } - $IDs = implode(',', array_keys($NotFound)); /* Changing any of these attributes returned will cause very large, very dramatic site-wide chaos. @@ -76,6 +75,8 @@ public static function get_groups($GroupIDs, $Return = true, $GetArtists = true, */ if (count($NotFound) > 0) { + $IDs = implode(',', array_keys($NotFound)); + $NotFound = array(); $QueryID = G::$DB->get_query_id(); G::$DB->query(" SELECT @@ -84,20 +85,12 @@ public static function get_groups($GroupIDs, $Return = true, $GetArtists = true, WHERE g.ID IN ($IDs)"); while ($Group = G::$DB->next_record(MYSQLI_ASSOC, true)) { - unset($NotFound[$Group['ID']]); - $Found[$Group['ID']] = $Group; - $Found[$Group['ID']]['Torrents'] = array(); - $Found[$Group['ID']]['Artists'] = array(); + $NotFound[$Group['ID']] = $Group; + $NotFound[$Group['ID']]['Torrents'] = array(); + $NotFound[$Group['ID']]['Artists'] = array(); } G::$DB->set_query_id($QueryID); - // Orphan torrents. There shouldn't ever be any - if (count($NotFound) > 0) { - foreach (array_keys($NotFound) as $GroupID) { - unset($Found[$GroupID]); - } - } - if ($Torrents) { $QueryID = G::$DB->get_query_id(); G::$DB->query(" @@ -106,27 +99,25 @@ public static function get_groups($GroupIDs, $Return = true, $GetArtists = true, RemasterRecordLabel, RemasterCatalogueNumber, Scene, HasLog, HasCue, LogScore, FileCount, FreeTorrent, Size, Leechers, Seeders, Snatched, Time, ID AS HasFile FROM torrents AS t - WHERE GroupID IN($IDs) + WHERE GroupID IN ($IDs) ORDER BY GroupID, Remastered, (RemasterYear != 0) DESC, RemasterYear, RemasterTitle, RemasterRecordLabel, RemasterCatalogueNumber, Media, Format, Encoding, ID"); while ($Torrent = G::$DB->next_record(MYSQLI_ASSOC, true)) { - $Found[$Torrent['GroupID']]['Torrents'][$Torrent['ID']] = $Torrent; + $NotFound[$Torrent['GroupID']]['Torrents'][$Torrent['ID']] = $Torrent; } G::$DB->set_query_id($QueryID); - - // Cache it all - foreach ($Found as $GroupID => $GroupInfo) { - G::$Cache->cache_value("torrent_group_$GroupID", - array('ver' => CACHE::GROUP_VERSION, 'd' => $GroupInfo), 0); - G::$Cache->cache_value("torrent_group_light_$GroupID", - array('ver' => CACHE::GROUP_VERSION, 'd' => $GroupInfo), 0); - } - } else { - foreach ($Found as $Group) { - G::$Cache->cache_value('torrent_group_light_'.$Group['ID'], array('ver' => CACHE::GROUP_VERSION, 'd' => $Found[$Group['ID']]), 0); - } } + + foreach ($NotFound as $GroupID => $GroupInfo) { + G::$Cache->cache_value($Key . $GroupID, array('ver' => CACHE::GROUP_VERSION, 'd' => $GroupInfo), 0); + } + + $Found = $NotFound + $Found; } + + // Filter out orphans (elements that are == false) + $Found = array_filter($Found); + if ($GetArtists) { $Artists = Artists::get_artists($GroupIDs); } else { diff --git a/sections/tools/development/clear_cache.php b/sections/tools/development/clear_cache.php index 23398bfe..764fef10 100644 --- a/sections/tools/development/clear_cache.php +++ b/sections/tools/development/clear_cache.php @@ -29,46 +29,49 @@

Clear a cache key

-
- - - - -
Key + + + + - - - - + + + + - + + + - - - - + + + + -
Key + + -
Multi-key + +
Multi-key +
+ -
-
get_value($Key)); ?>
-
+
get_value($Key)); ?>
+
- +
query(" + SELECT Name + FROM torrents_group + WHERE ID = $GroupID"); +if (!$DB->has_results()) { + error(404); +} +list($GroupName) = $DB->next_record(MYSQLI_NUM, false); + $Changed = false; for ($i = 0; $i < count($AliasNames); $i++) { @@ -45,12 +54,6 @@ $AliasID = $DB->inserted_id(); } - $DB->query(" - SELECT Name - FROM torrents_group - WHERE ID = $GroupID"); - list($GroupName) = $DB->next_record(MYSQLI_NUM, false); - $DB->query(" SELECT Name FROM artists_group @@ -66,16 +69,6 @@ if ($DB->affected_rows()) { $Changed = true; - $DB->query(" - INSERT INTO torrents_group (ID, NumArtists) - SELECT ta.GroupID, COUNT(ta.ArtistID) - FROM torrents_artists AS ta - WHERE ta.GroupID = '$GroupID' - AND ta.Importance = '1' - GROUP BY ta.GroupID - ON DUPLICATE KEY UPDATE - NumArtists = VALUES (NumArtists);"); - Misc::write_log("Artist $ArtistID ($ArtistName) was added to the group $GroupID ($GroupName) as ".$ArtistTypes[$Importance].' by user '.$LoggedUser['ID'].' ('.$LoggedUser['Username'].')'); Torrents::write_group_log($GroupID, 0, $LoggedUser['ID'], "added artist $ArtistName as ".$ArtistTypes[$Importance], 0); } diff --git a/sections/torrents/browse.php b/sections/torrents/browse.php index eccfee40..bbdfb28e 100644 --- a/sections/torrents/browse.php +++ b/sections/torrents/browse.php @@ -4,142 +4,91 @@ * Welcome to one of the most complicated pages in all of gazelle - the * browse page. * -* This is the page that is displayed when someone visits torrents.php, if -* you're not using sphinx. -* -* It also handle snatch lists, and seeding/leeching/uploaded lists. +* This is the page that is displayed when someone visits torrents.php * * It offers normal and advanced search, as well as enabled/disabled * grouping. * -* For the Sphinx version, see sections/torrents/browse2.php -* -* This version is a little outdated. You should probably set up Sphinx. -* * Don't blink. +* Blink and you're dead. +* Don't turn your back. +* Don't look away. +* And don't blink. +* Good Luck. +* *************************************************************************/ -$ErrorPage = true; - -define('EXPLAIN_HACK', false); - -if (EXPLAIN_HACK) { - $SCFR = ''; -} else { - $SCFR = 'SQL_CALC_FOUND_ROWS'; -} - -// Function to build a SQL WHERE to search for a string -// Offers exact searching, fulltext searching, and negative searching -function build_search($SearchStr, $Field, $Exact = false, $SQLWhere = '', $FullText = 0, &$FilterString = '') { - if ($SQLWhere != '') { - $AddWhere = false; - } else { - $AddWhere = true; - } - - if (!$Exact) { - if ($FullText && preg_match('/[^a-zA-Z0-9 ]/i', $SearchStr)) { - $FullText = 0; - } - - $SearchLength = strlen(trim($SearchStr)); - $SearchStr = preg_replace('/\s\s+/', ' ', trim($SearchStr)); - $SearchStr = preg_replace_callback('/"(([^"])*)"/', 'quotes', $SearchStr); - $SearchStr = explode(' ', $SearchStr); - - $FilterString = '(.+?)'; - foreach ($SearchStr as $SearchVal) { - if (trim($SearchVal) != '') { - $SearchVal = trim($SearchVal); - $SearchVal = str_replace('{{SPACE}}', ' ', $SearchVal); - - // Choose between fulltext or LIKE based off length of the string - if ($FullText && strlen($SearchVal) > 2) { - if ($SQLWhere != '') { - $SQLWhere .= ' AND '; - } - if (substr($SearchVal, 0, 1) == '-') { - $SQLWhere .= "MATCH ($Field) AGAINST ('".db_string($SearchVal)."' IN BOOLEAN MODE)"; - } else { - $SQLWhere .= "MATCH ($Field) AGAINST ('".db_string($SearchVal)."')"; - } - } else { - if ($SQLWhere != '') { - $SQLWhere .= ' AND '; - } - if (substr($SearchVal,0,1) == '-') { - $SQLWhere .= "$Field NOT LIKE '%".db_string(substr($SearchVal,1))."%'"; - } else { - $SQLWhere .= "$Field LIKE '%".db_string($SearchVal)."%'"; - } - } - $FilterString .= "($SearchVal)(.+?)"; - } - } - } else { - if ($SQLWhere != '') { - $SQLWhere .= ' AND '; - } - $SQLWhere .= "$Field LIKE '".db_string($SearchStr)."'"; - $FilterString .= "(.+?)($SearchStr)(.+?)"; - } - $Search = 1; - $FilterString = "/$FilterString/si"; - if ($SQLWhere != '' && $AddWhere) { - $SQLWhere = "WHERE $SQLWhere"; - } - return $SQLWhere; -} - -function quotes($Str) { - $Str = str_replace(' ', '{{SPACE}}', trim($Str[1])); - return " $Str "; -} +include(SERVER_ROOT.'/sections/torrents/functions.php'); // The "order by x" links on columns headers -function header_link($SortKey, $DefaultWay = 'DESC') { +function header_link($SortKey, $DefaultWay = 'desc') { global $OrderBy, $OrderWay; if ($SortKey == $OrderBy) { - if ($OrderWay == 'DESC') { - $NewWay = 'ASC'; + if ($OrderWay == 'desc') { + $NewWay = 'asc'; } else { - $NewWay = 'DESC'; + $NewWay = 'desc'; } } else { $NewWay = $DefaultWay; } + return "torrents.php?order_way=$NewWay&order_by=$SortKey&".Format::get_url(array('order_way', 'order_by')); +} - return "torrents.php?order_way=$NewWay&order_by=$SortKey&" . Format::get_url(array('order_way', 'order_by')); +/** Start default parameters and validation **/ +if (!empty($_GET['searchstr']) || !empty($_GET['groupname'])) { + if (!empty($_GET['searchstr'])) { + $InfoHash = $_GET['searchstr']; + } else { + $InfoHash = $_GET['groupname']; + } + + // Search by infohash + if ($InfoHash = is_valid_torrenthash($InfoHash)) { + $InfoHash = db_string(pack('H*', $InfoHash)); + $DB->query(" + SELECT ID, GroupID + FROM torrents + WHERE info_hash = '$InfoHash'"); + if ($DB->has_results()) { + list($ID, $GroupID) = $DB->next_record(); + header("Location: torrents.php?id=$GroupID&torrentid=$ID"); + die(); + } + } } // Setting default search options -if ($_GET['setdefault']) { - $UnsetList[] = '/(&?page\=.+?&?)/i'; - $UnsetList[] = '/(&?setdefault\=.+?&?)/i'; +if (!empty($_GET['setdefault'])) { + $UnsetList = array('page', 'setdefault'); + $UnsetRegexp = '/(&|^)('.implode('|', $UnsetList).')=.*?(&|$)/i'; $DB->query(" SELECT SiteOptions FROM users_info WHERE UserID = '".db_string($LoggedUser['ID'])."'"); - list($SiteOptions) = $DB->next_record(MYSQLI_NUM, true); - $SiteOptions = unserialize($SiteOptions); - $SiteOptions['DefaultSearch'] = preg_replace($UnsetList, '', $_SERVER['QUERY_STRING']); + list($SiteOptions) = $DB->next_record(MYSQLI_NUM, false); + if (!empty($SiteOptions)) { + $SiteOptions = unserialize($SiteOptions); + } else { + $SiteOptions = array(); + } + $SiteOptions['DefaultSearch'] = preg_replace($UnsetRegexp, '', $_SERVER['QUERY_STRING']); $DB->query(" UPDATE users_info SET SiteOptions = '".db_string(serialize($SiteOptions))."' WHERE UserID = '".db_string($LoggedUser['ID'])."'"); $Cache->begin_transaction("user_info_heavy_$UserID"); - $Cache->update_row(false, array('DefaultSearch' => preg_replace($UnsetList, '', $_SERVER['QUERY_STRING']))); + $Cache->update_row(false, array('DefaultSearch' => $SiteOptions['DefaultSearch'])); $Cache->commit_transaction(0); // Clearing default search options -} elseif ($_GET['cleardefault']) { +} elseif (!empty($_GET['cleardefault'])) { $DB->query(" SELECT SiteOptions FROM users_info WHERE UserID = '".db_string($LoggedUser['ID'])."'"); - list($SiteOptions) = $DB->next_record(MYSQLI_NUM, true); + list($SiteOptions) = $DB->next_record(MYSQLI_NUM, false); $SiteOptions = unserialize($SiteOptions); $SiteOptions['DefaultSearch'] = ''; $DB->query(" @@ -151,739 +100,748 @@ function header_link($SortKey, $DefaultWay = 'DESC') { $Cache->commit_transaction(0); // Use default search options -} elseif (!$_SERVER['QUERY_STRING'] && $LoggedUser['DefaultSearch']) { - parse_str($LoggedUser['DefaultSearch'], $_GET); -} - -// If a user is hammering the search page (either via a