Gazelle/sections/ajax/browse.php

693 lines
22 KiB
PHP
Raw Normal View History

2012-01-27 08:00:19 +00:00
<?
include(SERVER_ROOT.'/sections/torrents/functions.php');
2012-11-02 08:00:18 +00:00
/** Start default parameters and validation **/
2012-01-27 08:00:19 +00:00
// Setting default search options
2012-11-02 08:00:18 +00:00
if (!empty($_GET['setdefault'])) {
2013-07-10 00:08:53 +00:00
$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, false);
2012-11-02 08:00:18 +00:00
if (!empty($SiteOptions)) {
2012-01-27 08:00:19 +00:00
$SiteOptions = unserialize($SiteOptions);
} else {
$SiteOptions = array();
}
2013-07-10 00:08:53 +00:00
$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' => $SiteOptions['DefaultSearch']));
2012-01-27 08:00:19 +00:00
$Cache->commit_transaction(0);
// Clearing default search options
2012-11-02 08:00:18 +00:00
} elseif (!empty($_GET['cleardefault'])) {
2013-07-10 00:08:53 +00:00
$DB->query("
SELECT SiteOptions
FROM users_info
WHERE UserID = '".db_string($LoggedUser['ID'])."'");
list($SiteOptions) = $DB->next_record(MYSQLI_NUM, false);
$SiteOptions = unserialize($SiteOptions);
2013-09-16 08:00:51 +00:00
$SiteOptions['DefaultSearch'] = '';
2013-07-10 00:08:53 +00:00
$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' => ''));
2012-01-27 08:00:19 +00:00
$Cache->commit_transaction(0);
// Use default search options
2013-07-10 00:08:53 +00:00
} elseif (empty($_SERVER['QUERY_STRING']) || (count($_GET) === 1 && isset($_GET['page']))) {
2012-11-02 08:00:18 +00:00
if (!empty($LoggedUser['DefaultSearch'])) {
if (!empty($_GET['page'])) {
$Page = $_GET['page'];
2013-07-10 00:08:53 +00:00
parse_str($LoggedUser['DefaultSearch'], $_GET);
2012-11-02 08:00:18 +00:00
$_GET['page'] = $Page;
} else {
2013-07-10 00:08:53 +00:00
parse_str($LoggedUser['DefaultSearch'], $_GET);
2012-11-02 08:00:18 +00:00
}
2012-01-27 08:00:19 +00:00
}
}
2012-11-02 08:00:18 +00:00
// Terms were not submitted via the search form
if (!isset($_GET['searchsubmit'])) {
$_GET['group_results'] = !$LoggedUser['DisableGrouping2'];
}
if (isset($_GET['group_results']) && $_GET['group_results']) {
$_GET['group_results'] = 1;
$GroupResults = true;
$SortOrders = array(
// 'url attr' => [global order, order within group]
'year' => array('year', 'year'),
'time' => array('id', 'id'),
'size' => array('maxsize', 'size'),
'seeders' => array('sumseeders', 'seeders'),
'leechers' => array('sumleechers', 'leechers'),
'snatched' => array('sumsnatched', 'snatched'),
'random' => false);
2013-09-16 08:00:51 +00:00
2012-11-02 08:00:18 +00:00
$AggregateExp = array(
'maxsize' => 'MAX(size) AS maxsize',
'sumseeders' => 'SUM(seeders) AS sumseeders',
'sumleechers' => 'SUM(leechers) AS sumleechers',
'sumsnatched' => 'SUM(snatched) AS sumsnatched');
} else {
2013-09-16 08:00:51 +00:00
$GroupResults = false;
2012-11-02 08:00:18 +00:00
$SortOrders = array(
'year' => 'year',
'time' => 'id',
'size' => 'size',
'seeders' => 'seeders',
'leechers' => 'leechers',
'snatched' => 'snatched',
'random' => false);
}
if (empty($_GET['order_by']) || !isset($SortOrders[$_GET['order_by']])) {
$_GET['order_by'] = 'time';
$OrderBy = 'time'; // For header links
} else {
$OrderBy = $_GET['order_by'];
}
2013-04-15 08:00:54 +00:00
if (!empty($_GET['order_way']) && $_GET['order_way'] == 'asc') {
2012-11-02 08:00:18 +00:00
$OrderWay = 'asc';
} else {
$_GET['order_way'] = 'desc';
$OrderWay = 'desc';
}
/** End default parameters and validation **/
2012-01-27 08:00:19 +00:00
2012-11-02 08:00:18 +00:00
/** Start preparation of property arrays **/
2012-01-27 08:00:19 +00:00
array_pop($Bitrates); // remove 'other'
2013-07-10 00:08:53 +00:00
$SearchBitrates = array_merge($Bitrates, array('v0', 'v1', 'v2', '24bit'));
2012-01-27 08:00:19 +00:00
2013-07-10 00:08:53 +00:00
foreach ($SearchBitrates as $ID => $Val) {
2012-11-02 08:00:18 +00:00
$SearchBitrates[$ID] = strtolower($Val);
2012-01-27 08:00:19 +00:00
}
2012-11-02 08:00:18 +00:00
foreach ($Formats as $ID => $Val) {
$SearchFormats[$ID] = strtolower($Val);
}
/** End preparation of property arrays **/
/** Start query preparation **/
2013-03-17 08:00:17 +00:00
$SphQL = new SphinxqlQuery();
$SphQLTor = new SphinxqlQuery();
2012-11-02 08:00:18 +00:00
if ($OrderBy == 'random') {
$SphQL->select('id, groupid, categoryid')
->order_by('RAND()', '');
$Random = true;
2013-04-15 08:00:54 +00:00
} elseif ($GroupResults) {
2012-11-02 08:00:18 +00:00
$OrderProperties = $SortOrders[$OrderBy];
$SphQL->select('groupid, categoryid' . (isset($AggregateExp[$OrderProperties[0]]) ? ', '.$AggregateExp[$OrderProperties[0]] : ''))
->group_by('groupid')
->order_by($OrderProperties[0], $OrderWay)
->order_group_by($OrderProperties[1], $OrderWay);
2012-01-27 08:00:19 +00:00
2012-11-02 08:00:18 +00:00
} else {
$SphQL->select('id, groupid, categoryid')
->order_by($SortOrders[$OrderBy], $OrderWay);
}
$SphQL->from('torrents, delta');
$SphQLTor->select('id, groupid')->from('torrents, delta');
/** End query preparation **/
/** Start building search query **/
$Filtered = false;
$EnableNegation = false; // Sphinx needs at least one positive search condition to support the NOT operator
2013-09-16 08:00:51 +00:00
// File list searches make use of the proximity operator to ensure that all keywords match the same file
2012-11-02 08:00:18 +00:00
if (!empty($_GET['filelist'])) {
$SearchString = trim($_GET['filelist']);
2013-07-17 08:00:52 +00:00
if ($SearchString !== '') {
2013-09-08 08:00:57 +00:00
$SearchString = '"'.Sphinxql::sph_escape_string($_GET['filelist']).'"~20';
2012-11-02 08:00:18 +00:00
$SphQL->where_match($SearchString, 'filelist', false);
$SphQLTor->where_match($SearchString, 'filelist', false);
$EnableNegation = true;
2012-01-27 08:00:19 +00:00
}
2012-11-02 08:00:18 +00:00
}
// Collect all entered search terms to find out whether to enable the NOT operator
$SearchWords = array();
foreach (array('artistname', 'groupname', 'recordlabel', 'cataloguenumber',
'taglist', 'remastertitle', 'remasteryear', 'remasterrecordlabel',
2013-09-16 08:00:51 +00:00
'remastercataloguenumber', 'encoding', 'format', 'media', 'description') as $Search) {
2012-11-02 08:00:18 +00:00
if (!empty($_GET[$Search])) {
$SearchString = trim($_GET[$Search]);
2013-07-17 08:00:52 +00:00
if ($SearchString !== '') {
2012-11-02 08:00:18 +00:00
$SearchWords[$Search] = array('include' => array(), 'exclude' => array());
if ($Search == 'taglist') {
$SearchString = strtr($SearchString, '.', '_');
$Words = explode(',', $SearchString);
} else {
$Words = explode(' ', $SearchString);
}
foreach ($Words as $Word) {
$Word = trim($Word);
2013-09-16 08:00:51 +00:00
// Skip isolated hyphens to enable "Artist - Title" searches
if ($Word === '-') {
continue;
}
if ($Word[0] === '!' && strlen($Word) >= 2) {
2013-07-10 00:08:53 +00:00
if (strpos($Word, '!', 1) === false) {
2012-11-02 08:00:18 +00:00
$SearchWords[$Search]['exclude'][] = $Word;
} else {
$SearchWords[$Search]['include'][] = $Word;
$EnableNegation = true;
}
2013-07-17 08:00:52 +00:00
} elseif ($Word !== '') {
2012-11-02 08:00:18 +00:00
$SearchWords[$Search]['include'][] = $Word;
$EnableNegation = true;
}
}
}
2012-01-27 08:00:19 +00:00
}
2012-11-02 08:00:18 +00:00
}
//Simple search
if (!empty($_GET['searchstr'])) {
$SearchString = trim($_GET['searchstr']);
2013-07-10 00:08:53 +00:00
$Words = explode(' ', strtolower($SearchString));
2012-11-02 08:00:18 +00:00
if (!empty($Words)) {
$FilterBitrates = $FilterFormats = array();
$BasicSearch = array('include' => array(), 'exclude' => array());
foreach ($Words as $Word) {
$Word = trim($Word);
2013-09-16 08:00:51 +00:00
// Skip isolated hyphens to enable "Artist - Title" searches
if ($Word === '-') {
continue;
}
if ($Word[0] === '!' && strlen($Word) >= 2) {
if ($Word === '!100%') {
2012-11-02 08:00:18 +00:00
$_GET['haslog'] = '-1';
2013-07-10 00:08:53 +00:00
} elseif (strpos($Word, '!', 1) === false) {
2012-11-02 08:00:18 +00:00
$BasicSearch['exclude'][] = $Word;
2012-01-27 08:00:19 +00:00
} else {
2012-11-02 08:00:18 +00:00
$BasicSearch['include'][] = $Word;
$EnableNegation = true;
2012-01-27 08:00:19 +00:00
}
2012-11-02 08:00:18 +00:00
} elseif (in_array($Word, $SearchBitrates)) {
$FilterBitrates[] = $Word;
$EnableNegation = true;
} elseif (in_array($Word, $SearchFormats)) {
$FilterFormats[] = $Word;
$EnableNegation = true;
2013-07-17 08:00:52 +00:00
} elseif ($Word === '100%') {
2012-11-02 08:00:18 +00:00
$_GET['haslog'] = '100';
2013-07-17 08:00:52 +00:00
} elseif ($Word !== '') {
2012-11-02 08:00:18 +00:00
$BasicSearch['include'][] = $Word;
$EnableNegation = true;
}
}
if (!$EnableNegation && !empty($BasicSearch['exclude'])) {
$BasicSearch['include'] = array_merge($BasicSearch['include'], $BasicSearch['exclude']);
unset($BasicSearch['exclude']);
}
$QueryParts = array();
foreach ($BasicSearch['include'] as $Word) {
2013-09-08 08:00:57 +00:00
$QueryParts[] = Sphinxql::sph_escape_string($Word);
2012-11-02 08:00:18 +00:00
}
if (!empty($BasicSearch['exclude'])) {
foreach ($BasicSearch['exclude'] as $Word) {
2013-09-08 08:00:57 +00:00
$QueryParts[] = '!'.Sphinxql::sph_escape_string(substr($Word, 1));
2012-01-27 08:00:19 +00:00
}
}
2012-11-02 08:00:18 +00:00
if (!empty($FilterBitrates)) {
$SearchString = implode(' ', $FilterBitrates);
$SphQL->where_match($SearchString, 'encoding', false);
$SphQLTor->where_match($SearchString, 'encoding', false);
}
if (!empty($FilterFormats)) {
$SearchString = implode(' ', $FilterFormats);
$SphQL->where_match($SearchString, 'format', false);
$SphQLTor->where_match($SearchString, 'format', false);
}
if (!empty($QueryParts)) {
$SearchString = implode(' ', $QueryParts);
$SphQL->where_match($SearchString, '(groupname,artistname,yearfulltext)', false);
$SphQLTor->where_match($SearchString, '(groupname,artistname,yearfulltext)', false);
2012-01-27 08:00:19 +00:00
}
}
}
2012-11-02 08:00:18 +00:00
// Tag list
if (!empty($SearchWords['taglist'])) {
2014-04-06 08:00:49 +00:00
$_GET['tags_type'] = (!isset($_GET['tags_type']) || $_GET['tags_type'] == 1) ? '1' : '0';
$TagType = (int)$_GET['tags_type'];
2013-09-16 08:00:51 +00:00
//Get tags
2012-11-02 08:00:18 +00:00
$Tags = $SearchWords['taglist'];
2012-01-27 08:00:19 +00:00
2014-04-06 08:00:49 +00:00
$TagFilter = Tags::tag_filter_sph($Tags, $EnableNegation, $TagType);
if (!empty($TagFilter['predicate'])) {
$SphQL->where_match($TagFilter['predicate'], 'taglist', false);
$SphQLTor->where_match($TagFilter['predicate'], 'taglist', false);
2012-11-02 08:00:18 +00:00
}
unset($SearchWords['taglist']);
2013-09-16 08:00:51 +00:00
}
elseif (!isset($_GET['tags_type'])) {
2012-01-27 08:00:19 +00:00
$_GET['tags_type'] = '1';
}
2012-11-02 08:00:18 +00:00
foreach ($SearchWords as $Search => $Words) {
$QueryParts = array();
if (!$EnableNegation && !empty($Words['exclude'])) {
$Words['include'] = array_merge($Words['include'], $Words['exclude']);
unset($Words['exclude']);
}
foreach ($Words['include'] as $Word) {
2013-09-08 08:00:57 +00:00
$QueryParts[] = Sphinxql::sph_escape_string($Word);
2012-11-02 08:00:18 +00:00
}
if (!empty($Words['exclude'])) {
foreach ($Words['exclude'] as $Word) {
2013-09-08 08:00:57 +00:00
$QueryParts[] = '!'.Sphinxql::sph_escape_string(substr($Word, 1));
2012-01-27 08:00:19 +00:00
}
}
2012-11-02 08:00:18 +00:00
if (!empty($QueryParts)) {
$SearchString = implode(' ', $QueryParts);
$SphQL->where_match($SearchString, $Search, false);
$SphQLTor->where_match($SearchString, $Search, false);
}
2012-01-27 08:00:19 +00:00
}
2012-11-02 08:00:18 +00:00
if (!empty($_GET['year'])) {
2012-01-27 08:00:19 +00:00
$Years = explode('-', $_GET['year']);
2012-11-02 08:00:18 +00:00
if (is_number($Years[0]) || (empty($Years[0]) && !empty($Years[1]) && is_number($Years[1]))) {
2013-07-10 00:08:53 +00:00
if (count($Years) === 1) {
2012-11-02 08:00:18 +00:00
$SphQL->where('year', (int)$Years[0]);
$SphQLTor->where('year', (int)$Years[0]);
2012-01-27 08:00:19 +00:00
} else {
2012-11-02 08:00:18 +00:00
if (empty($Years[1]) || !is_number($Years[1])) {
2012-01-27 08:00:19 +00:00
$Years[1] = PHP_INT_MAX;
2012-11-02 08:00:18 +00:00
} elseif ($Years[0] > $Years[1]) {
2012-01-27 08:00:19 +00:00
$Years = array_reverse($Years);
}
2012-11-02 08:00:18 +00:00
$SphQL->where_between('year', array((int)$Years[0], (int)$Years[1]));
$SphQLTor->where_between('year', array((int)$Years[0], (int)$Years[1]));
2012-01-27 08:00:19 +00:00
}
}
}
2013-07-10 00:08:53 +00:00
if (isset($_GET['haslog']) && $_GET['haslog'] !== '') {
2013-07-17 08:00:52 +00:00
if ($_GET['haslog'] === '100') {
2012-11-02 08:00:18 +00:00
$SphQL->where('logscore', 100);
$SphQLTor->where('logscore', 100);
2013-04-19 08:00:55 +00:00
} elseif ($_GET['haslog'] < 0) {
2012-11-02 08:00:18 +00:00
// Exclude torrents with log score equal to 100
$SphQL->where('logscore', 100, true);
$SphQL->where('haslog', 1);
$SphQLTor->where('logscore', 100, true);
$SphQLTor->where('haslog', 1);
} elseif ($_GET['haslog'] == 0) {
$SphQL->where('haslog', 0);
$SphQLTor->where('haslog', 0);
2012-01-27 08:00:19 +00:00
} else {
2012-11-02 08:00:18 +00:00
$SphQL->where('haslog', 1);
$SphQLTor->where('haslog', 1);
}
2012-01-27 08:00:19 +00:00
}
2013-10-11 08:01:04 +00:00
2013-07-10 00:08:53 +00:00
foreach (array('hascue', 'scene', 'vanityhouse', 'releasetype') as $Search) {
2012-11-02 08:00:18 +00:00
if (isset($_GET[$Search]) && $_GET[$Search] !== '') {
$SphQL->where($Search, $_GET[$Search]);
// Release type is group specific
if ($Search != 'releasetype') {
$SphQLTor->where($Search, $_GET[$Search]);
}
2012-01-27 08:00:19 +00:00
}
}
2013-09-14 08:00:52 +00:00
if (isset($_GET['freetorrent']) && $_GET['freetorrent'] !== '') {
2012-11-02 08:00:18 +00:00
switch ($_GET['freetorrent']) {
case 0: // Only normal freeleech
$SphQL->where('freetorrent', 0);
$SphQLTor->where('freetorrent', 0);
break;
case 1: // Only free leech
$SphQL->where('freetorrent', 1);
$SphQLTor->where('freetorrent', 1);
break;
case 2: // Only neutral leech
$SphQL->where('freetorrent', 2);
$SphQLTor->where('freetorrent', 2);
break;
case 3: // Free or neutral leech
$SphQL->where('freetorrent', 0, true);
$SphQLTor->where('freetorrent', 0, true);
break;
2012-01-27 08:00:19 +00:00
}
}
2012-11-02 08:00:18 +00:00
if (!empty($_GET['filter_cat'])) {
$SphQL->where('categoryid', array_keys($_GET['filter_cat']));
2012-01-27 08:00:19 +00:00
}
2012-11-02 08:00:18 +00:00
/** End building search query **/
2012-01-27 08:00:19 +00:00
2012-11-02 08:00:18 +00:00
/** Run search query and collect results **/
if (isset($Random) && $GroupResults) {
// ORDER BY RAND() can't be used together with GROUP BY, so we need some special tactics
$Page = 1;
2013-07-10 00:08:53 +00:00
$SphQL->limit(0, 5 * TORRENTS_PER_PAGE, 5 * TORRENTS_PER_PAGE);
2012-11-02 08:00:18 +00:00
$SphQLResult = $SphQL->query();
$TotalCount = $SphQLResult->get_meta('total_found');
$Results = $SphQLResult->to_array('groupid');
$GroupIDs = array_keys($Results);
$GroupCount = count($GroupIDs);
while ($SphQLResult->get_meta('total') < $TotalCount && $GroupCount < TORRENTS_PER_PAGE) {
// Make sure we get TORRENTS_PER_PAGE results, or all of them if there are less than TORRENTS_PER_PAGE hits
$SphQL->where('groupid', $GroupIDs, true);
$SphQLResult = $SphQL->query();
2013-09-13 08:00:53 +00:00
if (!$SphQLResult->has_results()) {
2012-11-02 08:00:18 +00:00
break;
}
$Results += $SphQLResult->to_array('groupid');
$GroupIDs = array_keys($Results);
$GroupCount = count($GroupIDs);
2012-01-27 08:00:19 +00:00
}
2012-11-02 08:00:18 +00:00
if ($GroupCount > TORRENTS_PER_PAGE) {
$Results = array_slice($Results, 0, TORRENTS_PER_PAGE, true);
}
$GroupIDs = array_keys($Results);
2013-10-11 08:01:04 +00:00
$NumResults = count($Results);
2012-08-08 08:00:12 +00:00
} else {
2012-11-02 08:00:18 +00:00
if (!empty($_GET['page']) && is_number($_GET['page']) && $_GET['page'] > 0) {
if (check_perms('site_search_many')) {
$Page = $_GET['page'];
} else {
2013-09-16 08:00:51 +00:00
$Page = min(SPHINX_MAX_MATCHES / TORRENTS_PER_PAGE, $_GET['page']);
2012-01-27 08:00:19 +00:00
}
2012-11-02 08:00:18 +00:00
$Offset = ($Page - 1) * TORRENTS_PER_PAGE;
$SphQL->limit($Offset, TORRENTS_PER_PAGE, $Offset + TORRENTS_PER_PAGE);
} else {
$Page = 1;
$SphQL->limit(0, TORRENTS_PER_PAGE, TORRENTS_PER_PAGE);
2012-01-27 08:00:19 +00:00
}
2012-11-02 08:00:18 +00:00
$SphQLResult = $SphQL->query();
2013-10-11 08:01:04 +00:00
$NumResults = $SphQLResult->get_meta('total_found');
2012-11-02 08:00:18 +00:00
if ($GroupResults) {
$Results = $SphQLResult->to_array('groupid');
$GroupIDs = array_keys($Results);
} else {
$Results = $SphQLResult->to_array('id');
$GroupIDs = $SphQLResult->collect('groupid');
2012-01-27 08:00:19 +00:00
}
}
2013-10-11 08:01:04 +00:00
if (!check_perms('site_search_many') && $NumResults > SPHINX_MAX_MATCHES) {
$NumResults = SPHINX_MAX_MATCHES;
2012-01-27 08:00:19 +00:00
}
2013-10-11 08:01:04 +00:00
if ($NumResults) {
2012-11-02 08:00:18 +00:00
$Groups = Torrents::get_groups($GroupIDs);
2013-09-16 08:00:51 +00:00
2012-11-02 08:00:18 +00:00
if (!empty($Groups) && $GroupResults) {
$TorrentIDs = array();
foreach ($Groups as $Group) {
if (!empty($Group['Torrents'])) {
$TorrentIDs = array_merge($TorrentIDs, array_keys($Group['Torrents']));
}
}
2013-10-11 08:01:04 +00:00
$TorrentCount = count($TorrentIDs);
if ($TorrentCount > 0) {
2013-09-16 08:00:51 +00:00
// Get a list of all torrent ids that match the search query
2013-10-11 08:01:04 +00:00
$SphQLTor->where('id', $TorrentIDs)->limit(0, $TorrentCount, $TorrentCount);
2013-09-16 08:00:51 +00:00
$SphQLResultTor = $SphQLTor->query();
2013-10-11 08:01:04 +00:00
$TorrentIDs = $SphQLResultTor->to_pair('id', 'id'); // Because isset() is faster than in_array()
2013-09-16 08:00:51 +00:00
}
2012-11-02 08:00:18 +00:00
}
}
/** End run search query and collect results **/
2013-10-11 08:01:04 +00:00
if ($NumResults == 0) {
2012-08-08 08:00:12 +00:00
2013-07-10 00:08:53 +00:00
$DB->query("
SELECT
tags.Name,
((COUNT(tags.Name) - 2) * (SUM(tt.PositiveVotes) - SUM(tt.NegativeVotes))) / (tags.Uses * 0.8) AS Score
2013-02-16 08:00:57 +00:00
FROM xbt_snatched AS s
2013-07-10 00:08:53 +00:00
INNER JOIN torrents AS t ON t.ID = s.fid
INNER JOIN torrents_group AS g ON t.GroupID = g.ID
INNER JOIN torrents_tags AS tt ON tt.GroupID = g.ID
INNER JOIN tags ON tags.ID = tt.TagID
2013-05-27 08:00:58 +00:00
WHERE s.uid = '$LoggedUser[ID]'
AND tt.TagID != '13679'
AND tt.TagID != '4820'
AND tt.TagID != '2838'
AND g.CategoryID = '1'
2013-04-15 08:00:54 +00:00
AND tags.Uses > '10'
2012-02-09 08:00:20 +00:00
GROUP BY tt.TagID
ORDER BY Score DESC
LIMIT 8");
2012-01-27 08:00:19 +00:00
$JsonYouMightLike = array();
2013-04-15 08:00:54 +00:00
while (list($Tag) = $DB->next_record()) {
2012-01-27 08:00:19 +00:00
$JsonYouMightLike[] = $Tag;
}
2012-08-08 08:00:12 +00:00
2013-05-14 08:00:34 +00:00
json_die("success", array(
'results' => array(),
'youMightLike' => $JsonYouMightLike
));
2012-01-27 08:00:19 +00:00
}
2013-03-29 08:00:08 +00:00
$Bookmarks = Bookmarks::all_bookmarks('torrent');
2012-01-27 08:00:19 +00:00
$JsonGroups = array();
2012-11-02 08:00:18 +00:00
foreach ($Results as $Result) {
$GroupID = $Result['groupid'];
$GroupInfo = $Groups[$GroupID];
if (empty($GroupInfo['Torrents'])) {
continue;
}
$CategoryID = $Result['categoryid'];
$GroupYear = $GroupInfo['Year'];
$ExtendedArtists = $GroupInfo['ExtendedArtists'];
$GroupCatalogueNumber = $GroupInfo['CatalogueNumber'];
$GroupName = $GroupInfo['Name'];
$GroupRecordLabel = $GroupInfo['RecordLabel'];
$ReleaseType = $GroupInfo['ReleaseType'];
if ($GroupResults) {
$Torrents = $GroupInfo['Torrents'];
$GroupTime = $MaxSize = $TotalLeechers = $TotalSeeders = $TotalSnatched = 0;
foreach ($Torrents as $T) {
$GroupTime = max($GroupTime, strtotime($T['Time']));
if ($T['Size'] > $MaxSize) {
$MaxSize = $T['Size'];
}
$TotalLeechers += $T['Leechers'];
$TotalSeeders += $T['Seeders'];
$TotalSnatched += $T['Snatched'];
}
} else {
$Torrents = array($Result['id'] => $GroupInfo['Torrents'][$Result['id']]);
}
2013-02-16 08:00:57 +00:00
2013-07-10 00:08:53 +00:00
$TagList = explode(' ', str_replace('_', '.', $GroupInfo['TagList']));
2013-04-19 08:00:55 +00:00
$JsonArtists = array();
2012-11-02 08:00:18 +00:00
if (!empty($ExtendedArtists[1]) || !empty($ExtendedArtists[4]) || !empty($ExtendedArtists[5]) || !empty($ExtendedArtists[6])) {
unset($ExtendedArtists[2]);
unset($ExtendedArtists[3]);
$DisplayName = Artists::display_artists($ExtendedArtists, false, false, true);
2013-04-19 08:00:55 +00:00
foreach ($ExtendedArtists[1] as $Artist) {
$JsonArtists[] = array(
2013-10-30 08:01:19 +00:00
'id' => (int)$Artist['id'],
2013-04-19 08:00:55 +00:00
'name' => $Artist['name'],
2013-10-30 08:01:19 +00:00
'aliasid' => (int)$Artist['id']
2013-04-19 08:00:55 +00:00
);
}
2013-04-15 08:00:54 +00:00
} elseif (!empty($Artists)) {
2013-10-30 08:01:19 +00:00
$DisplayName = Artists::display_artists(array(1 => $Artists), false, false, true);
2013-04-19 08:00:55 +00:00
foreach ($Artists as $Artist) {
$JsonArtists[] = array(
2013-10-30 08:01:19 +00:00
'id' => (int)$Artist['id'],
2013-04-19 08:00:55 +00:00
'name' => $Artist['name'],
2013-10-30 08:01:19 +00:00
'aliasid' => (int)$Artist['id']
2013-04-19 08:00:55 +00:00
);
}
2012-11-02 08:00:18 +00:00
} else {
2013-04-19 08:00:55 +00:00
$DisplayName = '';
2012-11-02 08:00:18 +00:00
}
2013-07-10 00:08:53 +00:00
if ($GroupResults && (count($Torrents) > 1 || isset($GroupedCategories[$CategoryID - 1]))) {
2012-01-27 08:00:19 +00:00
// These torrents are in a group
$LastRemasterYear = '-';
$LastRemasterTitle = '';
$LastRemasterRecordLabel = '';
$LastRemasterCatalogueNumber = '';
$LastMedia = '';
2013-02-16 08:00:57 +00:00
2012-01-27 08:00:19 +00:00
$EditionID = 0;
unset($FirstUnknown);
$JsonTorrents = array();
2013-04-15 08:00:54 +00:00
foreach ($Torrents as $TorrentID => $Data) {
2012-02-09 08:00:20 +00:00
// All of the individual torrents in the group
2013-02-16 08:00:57 +00:00
2012-02-09 08:00:20 +00:00
// If they're using the advanced search and have chosen enabled grouping, we just skip the torrents that don't check out
2012-11-02 08:00:18 +00:00
if (!isset($TorrentIDs[$TorrentID])) {
2012-01-27 08:00:19 +00:00
continue;
}
if ($Data['Remastered'] && !$Data['RemasterYear']) {
$FirstUnknown = !isset($FirstUnknown);
}
2013-02-16 08:00:57 +00:00
2013-07-10 00:08:53 +00:00
if (isset($GroupedCategories[$CategoryID - 1])
2012-11-02 08:00:18 +00:00
&& ($Data['RemasterTitle'] != $LastRemasterTitle
|| $Data['RemasterYear'] != $LastRemasterYear
|| $Data['RemasterRecordLabel'] != $LastRemasterRecordLabel
|| $Data['RemasterCatalogueNumber'] != $LastRemasterCatalogueNumber)
|| $FirstUnknown
|| $Data['Media'] != $LastMedia) {
2012-01-27 08:00:19 +00:00
$EditionID++;
2013-04-24 08:00:23 +00:00
if ($Data['Remastered'] && $Data['RemasterYear'] != 0) {
$RemasterName = $Data['RemasterYear'];
2013-07-10 00:08:53 +00:00
$AddExtra = ' - ';
if ($Data['RemasterRecordLabel']) {
$RemasterName .= $AddExtra.display_str($Data['RemasterRecordLabel']);
$AddExtra = ' / ';
}
if ($Data['RemasterCatalogueNumber']) {
$RemasterName .= $AddExtra.display_str($Data['RemasterCatalogueNumber']);
$AddExtra = ' / ';
}
if ($Data['RemasterTitle']) {
$RemasterName .= $AddExtra.display_str($Data['RemasterTitle']);
$AddExtra = ' / ';
}
2013-04-24 08:00:23 +00:00
$RemasterName .= $AddExtra.display_str($Data['Media']);
} else {
2013-07-10 00:08:53 +00:00
$AddExtra = ' / ';
2013-04-24 08:00:23 +00:00
if (!$Data['Remastered']) {
2013-07-10 00:08:53 +00:00
$MasterName = 'Original Release';
if ($GroupRecordLabel) {
$MasterName .= $AddExtra.$GroupRecordLabel;
$AddExtra = ' / ';
}
if ($GroupCatalogueNumber) {
$MasterName .= $AddExtra.$GroupCatalogueNumber;
$AddExtra = ' / ';
}
2013-04-24 08:00:23 +00:00
} else {
2013-07-10 00:08:53 +00:00
$MasterName = 'Unknown Release(s)';
2013-04-24 08:00:23 +00:00
}
$MasterName .= $AddExtra.display_str($Data['Media']);
}
2012-01-27 08:00:19 +00:00
}
2012-02-09 08:00:20 +00:00
$LastRemasterTitle = $Data['RemasterTitle'];
$LastRemasterYear = $Data['RemasterYear'];
$LastRemasterRecordLabel = $Data['RemasterRecordLabel'];
$LastRemasterCatalogueNumber = $Data['RemasterCatalogueNumber'];
$LastMedia = $Data['Media'];
2013-02-16 08:00:57 +00:00
2012-02-09 08:00:20 +00:00
$JsonTorrents[] = array(
2013-10-30 08:01:19 +00:00
'torrentId' => (int)$TorrentID,
'editionId' => (int)$EditionID,
2012-11-02 08:00:18 +00:00
'artists' => $JsonArtists,
2012-02-10 08:00:19 +00:00
'remastered' => $Data['Remastered'] == '1',
2013-10-30 08:01:19 +00:00
'remasterYear' => (int)$Data['RemasterYear'],
2012-02-09 08:00:20 +00:00
'remasterCatalogueNumber' => $Data['RemasterCatalogueNumber'],
'remasterTitle' => $Data['RemasterTitle'],
'media' => $Data['Media'],
'encoding' => $Data['Encoding'],
'format' => $Data['Format'],
'hasLog' => $Data['HasLog'] == '1',
2013-10-30 08:01:19 +00:00
'logScore' => (int)$Data['LogScore'],
2012-02-09 08:00:20 +00:00
'hasCue' => $Data['HasCue'] == '1',
'scene' => $Data['Scene'] == '1',
2013-09-08 08:00:57 +00:00
'vanityHouse' => $GroupInfo['VanityHouse'] == '1',
2013-10-30 08:01:19 +00:00
'fileCount' => (int)$Data['FileCount'],
2012-02-09 08:00:20 +00:00
'time' => $Data['Time'],
2013-10-30 08:01:19 +00:00
'size' => (int)$Data['Size'],
'snatches' => (int)$Data['Snatched'],
'seeders' => (int)$Data['Seeders'],
'leechers' => (int)$Data['Leechers'],
2012-02-09 08:00:20 +00:00
'isFreeleech' => $Data['FreeTorrent'] == '1',
'isNeutralLeech' => $Data['FreeTorrent'] == '2',
2012-10-16 08:00:18 +00:00
'isPersonalFreeleech' => $Data['PersonalFL'],
2012-10-31 08:00:17 +00:00
'canUseToken' => Torrents::can_use_token($Data),
'hasSnatched' => $Data['IsSnatched']
2012-02-09 08:00:20 +00:00
);
2012-01-27 08:00:19 +00:00
}
2013-02-16 08:00:57 +00:00
2012-02-09 08:00:20 +00:00
$JsonGroups[] = array(
2013-10-30 08:01:19 +00:00
'groupId' => (int)$GroupID,
2012-02-09 08:00:20 +00:00
'groupName' => $GroupName,
2012-02-11 08:00:22 +00:00
'artist' => $DisplayName,
2013-06-12 08:00:46 +00:00
'cover' => $GroupInfo['WikiImage'],
2012-02-09 08:00:20 +00:00
'tags' => $TagList,
'bookmarked' => in_array($GroupID, $Bookmarks),
2013-09-08 08:00:57 +00:00
'vanityHouse' => $GroupInfo['VanityHouse'] == '1',
2013-10-30 08:01:19 +00:00
'groupYear' => (int)$GroupYear,
2012-02-09 08:00:20 +00:00
'releaseType' => $ReleaseTypes[$ReleaseType],
2013-10-30 08:01:19 +00:00
'groupTime' => (string)$GroupTime,
'maxSize' => (int)$MaxSize,
'totalSnatched' => (int)$TotalSnatched,
'totalSeeders' => (int)$TotalSeeders,
'totalLeechers' => (int)$TotalLeechers,
2012-02-09 08:00:20 +00:00
'torrents' => $JsonTorrents
);
2013-04-15 08:00:54 +00:00
} else {
2012-02-09 08:00:20 +00:00
// Viewing a type that does not require grouping
2013-02-16 08:00:57 +00:00
2012-01-27 08:00:19 +00:00
list($TorrentID, $Data) = each($Torrents);
2013-02-16 08:00:57 +00:00
2012-02-09 08:00:20 +00:00
$JsonGroups[] = array(
2013-10-30 08:01:19 +00:00
'groupId' => (int)$GroupID,
2012-02-09 08:00:20 +00:00
'groupName' => $GroupName,
2013-10-30 08:01:19 +00:00
'torrentId' => (int)$TorrentID,
2012-02-09 08:00:20 +00:00
'tags' => $TagList,
2013-07-10 00:08:53 +00:00
'category' => $Categories[$CategoryID - 1],
2013-10-30 08:01:19 +00:00
'fileCount' => (int)$Data['FileCount'],
'groupTime' => (string)strtotime($Data['Time']),
'size' => (int)$Data['Size'],
'snatches' => (int)$Data['Snatched'],
'seeders' => (int)$Data['Seeders'],
'leechers' => (int)$Data['Leechers'],
2012-02-09 08:00:20 +00:00
'isFreeleech' => $Data['FreeTorrent'] == '1',
'isNeutralLeech' => $Data['FreeTorrent'] == '2',
2012-10-16 08:00:18 +00:00
'isPersonalFreeleech' => $Data['PersonalFL'],
2012-10-31 08:00:17 +00:00
'canUseToken' => Torrents::can_use_token($Data),
'hasSnatched' => $Data['IsSnatched']
2012-02-09 08:00:20 +00:00
);
2012-01-27 08:00:19 +00:00
}
}
2013-10-29 08:01:29 +00:00
echo json_encode(
array(
'status' => 'success',
'response' => array(
'currentPage' => intval($Page),
'pages' => ceil($NumResults / TORRENTS_PER_PAGE),
'results' => $JsonGroups)));