Gazelle/sections/ajax/requests.php

363 lines
9.8 KiB
PHP
Raw Normal View History

2013-05-28 08:01:02 +00:00
<?php
2012-01-27 08:00:19 +00:00
$Queries = array();
$OrderWays = array('year', 'votes', 'bounty', 'created', 'lastvote', 'filled');
2013-05-28 08:01:02 +00:00
list($Page, $Limit) = Format::page_limit(REQUESTS_PER_PAGE);
2012-01-27 08:00:19 +00:00
$Submitted = !empty($_GET['submit']);
2013-02-22 08:00:24 +00:00
//Paranoia
2012-10-11 08:00:15 +00:00
$UserInfo = Users::user_info((int)$_GET['userid']);
$Perms = Permissions::get_permissions($UserInfo['PermissionID']);
2012-01-27 08:00:19 +00:00
$UserClass = $Perms['Class'];
$BookmarkView = false;
2013-04-17 08:00:58 +00:00
if (empty($_GET['type'])) {
2012-01-27 08:00:19 +00:00
$Title = 'Requests';
2013-04-17 08:00:58 +00:00
if (!check_perms('site_see_old_requests') || empty($_GET['showall'])) {
2012-01-27 08:00:19 +00:00
$SS->set_filter('visible', array(1));
}
} else {
2013-05-01 08:00:16 +00:00
switch ($_GET['type']) {
2012-01-27 08:00:19 +00:00
case 'created':
$Title = 'My requests';
$SS->set_filter('userid', array($LoggedUser['ID']));
break;
case 'voted':
2013-04-17 08:00:58 +00:00
if (!empty($_GET['userid'])) {
if (is_number($_GET['userid'])) {
2012-01-27 08:00:19 +00:00
if (!check_paranoia('requestsvoted_list', $UserInfo['Paranoia'], $Perms['Class'], $_GET['userid'])) {
2013-05-14 08:00:34 +00:00
json_die("failure");
2012-01-27 08:00:19 +00:00
}
$Title = "Requests voted for by ".$UserInfo['Username'];
$SS->set_filter('voter', array($_GET['userid']));
} else {
2013-04-24 08:00:23 +00:00
json_die("failure");
2012-01-27 08:00:19 +00:00
}
} else {
$Title = "Requests I've voted on";
$SS->set_filter('voter', array($LoggedUser['ID']));
}
break;
case 'filled':
2013-05-05 08:00:31 +00:00
if (empty($_GET['userid']) || !is_number($_GET['userid'])) {
2013-04-24 08:00:23 +00:00
json_die("failure");
2012-01-27 08:00:19 +00:00
} else {
if (!check_paranoia('requestsfilled_list', $UserInfo['Paranoia'], $Perms['Class'], $_GET['userid'])) {
2013-04-24 08:00:23 +00:00
json_die("failure");
2012-01-27 08:00:19 +00:00
}
$Title = "Requests filled by ".$UserInfo['Username'];
$SS->set_filter('fillerid', array($_GET['userid']));
}
break;
case 'bookmarks':
$Title = 'Your bookmarked requests';
$BookmarkView = true;
$SS->set_filter('bookmarker', array($LoggedUser['ID']));
break;
default:
2013-04-24 08:00:23 +00:00
json_die("failure");
2012-01-27 08:00:19 +00:00
}
}
2013-04-17 08:00:58 +00:00
if ($Submitted && empty($_GET['show_filled'])) {
2012-01-27 08:00:19 +00:00
$SS->set_filter('torrentid', array(0));
}
2013-04-17 08:00:58 +00:00
if (!empty($_GET['search'])) {
2012-01-27 08:00:19 +00:00
$Words = explode(' ', $_GET['search']);
2013-04-17 08:00:58 +00:00
foreach ($Words as $Key => &$Word) {
if ($Word[0] == '!' && strlen($Word) > 2) {
if (strpos($Word,'!',1) === false) {
2012-01-27 08:00:19 +00:00
$Word = '!'.$SS->EscapeString(substr($Word,1));
} else {
$Word = $SS->EscapeString($Word);
}
2013-04-17 08:00:58 +00:00
} elseif (strlen($Word) >= 2) {
2012-01-27 08:00:19 +00:00
$Word = $SS->EscapeString($Word);
} else {
unset($Words[$Key]);
}
}
2013-04-17 08:00:58 +00:00
if (!empty($Words)) {
2012-01-27 08:00:19 +00:00
$Queries[] = "@* ".implode(' ', $Words);
}
}
2013-05-04 08:00:48 +00:00
if (!empty($_GET['tags'])) {
2012-01-27 08:00:19 +00:00
$Tags = explode(',', $_GET['tags']);
$TagNames = array();
foreach ($Tags as $Tag) {
2012-10-11 08:00:15 +00:00
$Tag = Misc::sanitize_tag($Tag);
2013-04-17 08:00:58 +00:00
if (!empty($Tag)) {
2012-01-27 08:00:19 +00:00
$TagNames[] = $Tag;
}
}
2012-10-11 08:00:15 +00:00
$Tags = Misc::get_tags($TagNames);
2012-01-27 08:00:19 +00:00
}
2013-04-17 08:00:58 +00:00
if (empty($_GET['tags_type']) && !empty($Tags)) {
2012-01-27 08:00:19 +00:00
$_GET['tags_type'] = '0';
$SS->set_filter('tagid', array_keys($Tags));
2013-04-17 08:00:58 +00:00
} elseif (!empty($Tags)) {
foreach (array_keys($Tags) as $Tag) {
2012-01-27 08:00:19 +00:00
$SS->set_filter('tagid', array($Tag));
}
} else {
$_GET['tags_type'] = '1';
}
2013-04-17 08:00:58 +00:00
if (!empty($_GET['filter_cat'])) {
2012-08-12 08:00:16 +00:00
$CategoryArray = array_keys($_GET['filter_cat']);
2013-07-10 00:08:53 +00:00
if (count($CategoryArray) !== count($Categories)) {
2013-04-17 08:00:58 +00:00
foreach ($CategoryArray as $Key => $Index) {
2013-05-04 08:00:48 +00:00
if (!isset($Categories[$Index - 1])) {
2012-08-12 08:00:16 +00:00
unset($CategoryArray[$Key]);
}
}
2013-04-17 08:00:58 +00:00
if (count($CategoryArray) >= 1) {
2012-08-12 08:00:16 +00:00
$SS->set_filter('categoryid', $CategoryArray);
}
}
2012-01-27 08:00:19 +00:00
}
2013-04-17 08:00:58 +00:00
if (!empty($_GET['releases'])) {
2012-01-27 08:00:19 +00:00
$ReleaseArray = $_GET['releases'];
2013-07-10 00:08:53 +00:00
if (count($ReleaseArray) !== count($ReleaseTypes)) {
2013-04-17 08:00:58 +00:00
foreach ($ReleaseArray as $Index => $Value) {
if (!isset($ReleaseTypes[$Value])) {
2012-08-12 08:00:16 +00:00
unset($ReleaseArray[$Index]);
2012-01-27 08:00:19 +00:00
}
}
2013-02-22 08:00:24 +00:00
2013-04-17 08:00:58 +00:00
if (count($ReleaseArray) >= 1) {
2012-08-12 08:00:16 +00:00
$SS->set_filter('releasetype', $ReleaseArray);
}
2012-01-27 08:00:19 +00:00
}
}
2013-04-17 08:00:58 +00:00
if (!empty($_GET['formats'])) {
2012-01-27 08:00:19 +00:00
$FormatArray = $_GET['formats'];
2013-07-10 00:08:53 +00:00
if (count($FormatArray) !== count($Formats)) {
2012-01-27 08:00:19 +00:00
$FormatNameArray = array();
2013-04-17 08:00:58 +00:00
foreach ($FormatArray as $Index => $MasterIndex) {
if (isset($Formats[$MasterIndex])) {
2012-08-12 08:00:16 +00:00
$FormatNameArray[$Index] = '"'.strtr($Formats[$MasterIndex], '-.', ' ').'"';
2012-01-27 08:00:19 +00:00
}
}
2013-02-22 08:00:24 +00:00
2013-04-17 08:00:58 +00:00
if (count($FormatNameArray) >= 1) {
2012-08-12 08:00:16 +00:00
$Queries[]='@formatlist (any | '.implode(' | ', $FormatNameArray).')';
}
2012-01-27 08:00:19 +00:00
}
}
2013-04-17 08:00:58 +00:00
if (!empty($_GET['media'])) {
2012-01-27 08:00:19 +00:00
$MediaArray = $_GET['media'];
2013-07-10 00:08:53 +00:00
if (count($MediaArray) !== count($Media)) {
2012-01-27 08:00:19 +00:00
$MediaNameArray = array();
2013-04-17 08:00:58 +00:00
foreach ($MediaArray as $Index => $MasterIndex) {
if (isset($Media[$MasterIndex])) {
2012-08-12 08:00:16 +00:00
$MediaNameArray[$Index] = '"'.strtr($Media[$MasterIndex], '-.', ' ').'"';
2012-01-27 08:00:19 +00:00
}
}
2013-04-17 08:00:58 +00:00
if (count($MediaNameArray) >= 1) {
2012-08-12 08:00:16 +00:00
$Queries[]='@medialist (any | '.implode(' | ', $MediaNameArray).')';
}
2012-01-27 08:00:19 +00:00
}
}
2013-04-17 08:00:58 +00:00
if (!empty($_GET['bitrates'])) {
2012-01-27 08:00:19 +00:00
$BitrateArray = $_GET['bitrates'];
2013-07-10 00:08:53 +00:00
if (count($BitrateArray) !== count($Bitrates)) {
2012-01-27 08:00:19 +00:00
$BitrateNameArray = array();
2013-04-17 08:00:58 +00:00
foreach ($BitrateArray as $Index => $MasterIndex) {
if (isset($Bitrates[$MasterIndex])) {
2012-08-12 08:00:16 +00:00
$BitrateNameArray[$Index] = '"'.strtr($SS->EscapeString($Bitrates[$MasterIndex]), '-.', ' ').'"';
2012-01-27 08:00:19 +00:00
}
}
2013-04-17 08:00:58 +00:00
if (count($BitrateNameArray) >= 1) {
2012-08-12 08:00:16 +00:00
$Queries[]='@bitratelist (any | '.implode(' | ', $BitrateNameArray).')';
}
2012-01-27 08:00:19 +00:00
}
}
2013-04-17 08:00:58 +00:00
if (!empty($_GET['requestor']) && check_perms('site_see_old_requests')) {
if (is_number($_GET['requestor'])) {
2012-01-27 08:00:19 +00:00
$SS->set_filter('userid', array($_GET['requestor']));
} else {
2013-04-24 08:00:23 +00:00
json_die("failure");
2012-01-27 08:00:19 +00:00
}
}
2013-04-17 08:00:58 +00:00
if (isset($_GET['year'])) {
if (is_number($_GET['year']) || $_GET['year'] == 0) {
2012-01-27 08:00:19 +00:00
$SS->set_filter('year', array($_GET['year']));
} else {
2013-04-24 08:00:23 +00:00
json_die("failure");
2012-01-27 08:00:19 +00:00
}
}
2013-04-17 08:00:58 +00:00
if (!empty($_GET['page']) && is_number($_GET['page']) && $_GET['page'] > 0) {
2012-08-12 08:00:16 +00:00
$Page = $_GET['page'];
2012-08-08 08:00:12 +00:00
$SS->limit(($Page - 1) * REQUESTS_PER_PAGE, REQUESTS_PER_PAGE);
2012-01-27 08:00:19 +00:00
} else {
$Page = 1;
2012-08-08 08:00:12 +00:00
$SS->limit(0, REQUESTS_PER_PAGE);
2012-01-27 08:00:19 +00:00
}
2013-04-17 08:00:58 +00:00
if (empty($_GET['order'])) {
2012-01-27 08:00:19 +00:00
$CurrentOrder = 'created';
$CurrentSort = 'desc';
$Way = SPH_SORT_ATTR_DESC;
$NewSort = 'asc';
} else {
2013-04-17 08:00:58 +00:00
if (in_array($_GET['order'], $OrderWays)) {
2012-01-27 08:00:19 +00:00
$CurrentOrder = $_GET['order'];
2013-04-17 08:00:58 +00:00
if ($_GET['sort'] == 'asc' || $_GET['sort'] == 'desc') {
2012-01-27 08:00:19 +00:00
$CurrentSort = $_GET['sort'];
$Way = ($CurrentSort == 'asc' ? SPH_SORT_ATTR_ASC : SPH_SORT_ATTR_DESC);
$NewSort = ($_GET['sort'] == 'asc' ? 'desc' : 'asc');
} else {
2013-04-24 08:00:23 +00:00
json_die("failure");
2012-01-27 08:00:19 +00:00
}
} else {
2013-04-24 08:00:23 +00:00
json_die("failure");
2012-01-27 08:00:19 +00:00
}
}
2013-05-01 08:00:16 +00:00
switch ($CurrentOrder) {
2013-07-10 00:08:53 +00:00
case 'votes':
2013-05-01 08:00:16 +00:00
$OrderBy = 'Votes';
2012-01-27 08:00:19 +00:00
break;
2013-07-10 00:08:53 +00:00
case 'bounty':
2013-05-01 08:00:16 +00:00
$OrderBy = 'Bounty';
2012-01-27 08:00:19 +00:00
break;
2013-07-10 00:08:53 +00:00
case 'created':
2013-05-01 08:00:16 +00:00
$OrderBy = 'TimeAdded';
2012-01-27 08:00:19 +00:00
break;
2013-07-10 00:08:53 +00:00
case 'lastvote':
2013-05-01 08:00:16 +00:00
$OrderBy = 'LastVote';
2012-01-27 08:00:19 +00:00
break;
2013-07-10 00:08:53 +00:00
case 'filled':
2013-05-01 08:00:16 +00:00
$OrderBy = 'TimeFilled';
2012-01-27 08:00:19 +00:00
break;
2013-07-10 00:08:53 +00:00
case 'year':
2013-05-01 08:00:16 +00:00
$OrderBy = 'Year';
2012-01-27 08:00:19 +00:00
break;
2013-07-10 00:08:53 +00:00
default:
2013-05-01 08:00:16 +00:00
$OrderBy = 'TimeAdded';
2012-01-27 08:00:19 +00:00
break;
}
//print($Way); print($OrderBy); die();
$SS->SetSortMode($Way, $OrderBy);
2013-04-17 08:00:58 +00:00
if (count($Queries) > 0) {
2013-07-10 00:08:53 +00:00
$Query = implode(' ', $Queries);
2012-01-27 08:00:19 +00:00
} else {
2013-04-17 08:00:58 +00:00
$Query = '';
2012-01-27 08:00:19 +00:00
}
$SS->set_index('requests requests_delta');
$SphinxResults = $SS->search($Query, '', 0, array(), '', '');
$NumResults = $SS->TotalResults;
//We don't use sphinxapi's default cache searcher, we use our own functions
2013-04-17 08:00:58 +00:00
if (!empty($SphinxResults['notfound'])) {
2012-10-11 08:00:15 +00:00
$SQLResults = Requests::get_requests($SphinxResults['notfound']);
2013-04-17 08:00:58 +00:00
if (is_array($SQLResults['notfound'])) {
2012-01-27 08:00:19 +00:00
//Something wasn't found in the db, remove it from results
reset($SQLResults['notfound']);
2013-04-17 08:00:58 +00:00
foreach ($SQLResults['notfound'] as $ID) {
2012-01-27 08:00:19 +00:00
unset($SQLResults['matches'][$ID]);
unset($SphinxResults['matches'][$ID]);
}
}
2013-02-22 08:00:24 +00:00
2012-01-27 08:00:19 +00:00
// Merge SQL results with memcached results
2013-04-17 08:00:58 +00:00
foreach ($SQLResults['matches'] as $ID => $SQLResult) {
2012-01-27 08:00:19 +00:00
$SphinxResults['matches'][$ID] = $SQLResult;
2013-02-22 08:00:24 +00:00
2012-01-27 08:00:19 +00:00
//$Requests['matches'][$ID] = array_merge($Requests['matches'][$ID], $SQLResult);
//We ksort because depending on the filter modes, we're given our data in an unpredictable order
//ksort($Requests['matches'][$ID]);
}
}
$Requests = $SphinxResults['matches'];
2012-02-11 08:00:22 +00:00
if ($NumResults == 0) {
2013-05-14 08:00:34 +00:00
json_die("success", array(
'currentPage' => 1,
'pages' => 1,
'results' => array()
));
2012-02-11 08:00:22 +00:00
} else {
$JsonResults = array();
2012-01-27 08:00:19 +00:00
$TimeCompare = 1267643718; // Requests v2 was implemented 2010-03-03 20:15:18
foreach ($Requests as $RequestID => $Request) {
2013-02-22 08:00:24 +00:00
//list($BitrateList, $CatalogueNumber, $CategoryID, $Description, $FillerID, $FormatList, $RequestID, $Image, $LogCue, $MediaList, $ReleaseType,
// $Tags, $TimeAdded, $TimeFilled, $Title, $TorrentID, $RequestorID, $RequestorName, $Year, $RequestID, $Categoryid, $FillerID, $LastVote,
2012-02-11 08:00:22 +00:00
// $ReleaseType, $TagIDs, $TimeAdded, $TimeFilled, $TorrentID, $RequestorID, $Voters) = array_values($Request);
2013-02-22 08:00:24 +00:00
list($RequestID, $RequestorID, $RequestorName, $TimeAdded, $LastVote, $CategoryID, $Title, $Year, $Image, $Description, $CatalogueNumber,
2012-01-27 08:00:19 +00:00
$ReleaseType, $BitrateList, $FormatList, $MediaList, $LogCue, $FillerID, $FillerName, $TorrentID, $TimeFilled) = $Request;
2013-02-22 08:00:24 +00:00
2013-05-13 08:00:33 +00:00
$RequestVotes = Requests::get_votes_array($RequestID);
2013-02-22 08:00:24 +00:00
2012-01-27 08:00:19 +00:00
$VoteCount = count($RequestVotes['Voters']);
2013-02-22 08:00:24 +00:00
2013-04-17 08:00:58 +00:00
if ($CategoryID == 0) {
2013-05-05 08:00:31 +00:00
$CategoryName = 'Unknown';
2012-01-27 08:00:19 +00:00
} else {
$CategoryName = $Categories[$CategoryID - 1];
}
2013-02-22 08:00:24 +00:00
2012-02-18 08:00:22 +00:00
$JsonArtists = array();
2013-05-05 08:00:31 +00:00
if ($CategoryName == 'Music') {
2013-05-13 08:00:33 +00:00
$ArtistForm = Requests::get_artists($RequestID);
2012-02-18 08:00:22 +00:00
$JsonArtists = array_values($ArtistForm);
2012-01-27 08:00:19 +00:00
}
2012-02-11 08:00:22 +00:00
$Tags = $Request['Tags'];
2013-02-22 08:00:24 +00:00
2012-01-27 08:00:19 +00:00
$JsonResults[] = array(
'requestId' => (int) $RequestID,
'requestorId' => (int) $RequestorID,
'requestorName' => $RequestorName,
'timeAdded' => $TimeAdded,
'lastVote' => $LastVote,
2012-02-11 08:00:22 +00:00
'voteCount' => $VoteCount,
'bounty' => $RequestVotes['TotalBounty'],
2012-01-27 08:00:19 +00:00
'categoryId' => (int) $CategoryID,
2012-02-11 08:00:22 +00:00
'categoryName' => $CategoryName,
2012-02-18 08:00:22 +00:00
'artists' => $JsonArtists,
2012-01-27 08:00:19 +00:00
'title' => $Title,
'year' => (int) $Year,
'image' => $Image,
'description' => $Description,
'catalogueNumber' => $CatalogueNumber,
'releaseType' => $ReleaseType,
'bitrateList' => $BitrateList,
'formatList' => $FormatList,
'mediaList' => $MediaList,
'logCue' => $LogCue,
'isFilled' => ($TorrentID > 0),
'fillerId' => (int) $FillerID,
2013-05-05 08:00:31 +00:00
'fillerName' => $FillerName == 0 ? '' : $FillerName,
2012-01-27 08:00:19 +00:00
'torrentId' => (int) $TorrentID,
2013-05-05 08:00:31 +00:00
'timeFilled' => $TimeFilled == 0 ? '' : $TimeFilled
2012-01-27 08:00:19 +00:00
);
}
2013-05-14 08:00:34 +00:00
json_die("success", array(
'currentPage' => intval($Page),
'pages' => ceil($NumResults / REQUESTS_PER_PAGE),
'results' => $JsonResults
));
2012-02-11 08:00:22 +00:00
}
2012-01-27 08:00:19 +00:00
?>