Oppaitime's version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

requests.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. $SphQL = new SphinxqlQuery();
  3. $SphQL->select('id, votes, bounty')->from('requests, requests_delta');
  4. $SortOrders = array(
  5. 'votes' => 'votes',
  6. 'bounty' => 'bounty',
  7. 'lastvote' => 'lastvote',
  8. 'filled' => 'timefilled',
  9. 'year' => 'year',
  10. 'created' => 'timeadded',
  11. 'random' => false);
  12. if (empty($_GET['order']) || !isset($SortOrders[$_GET['order']])) {
  13. $_GET['order'] = 'created';
  14. }
  15. $OrderBy = $_GET['order'];
  16. if (!empty($_GET['sort']) && $_GET['sort'] === 'asc') {
  17. $OrderWay = 'asc';
  18. } else {
  19. $_GET['sort'] = 'desc';
  20. $OrderWay = 'desc';
  21. }
  22. $NewSort = $_GET['sort'] === 'asc' ? 'desc' : 'asc';
  23. if ($OrderBy === 'random') {
  24. $SphQL->order_by('RAND()', '');
  25. unset($_GET['page']);
  26. } else {
  27. $SphQL->order_by($SortOrders[$OrderBy], $OrderWay);
  28. }
  29. $Submitted = !empty($_GET['submit']);
  30. //Paranoia
  31. if (!empty($_GET['userid'])) {
  32. if (!is_number($_GET['userid'])) {
  33. json_die("failure");
  34. }
  35. $UserInfo = Users::user_info($_GET['userid']);
  36. if (empty($UserInfo)) {
  37. json_die("failure");
  38. }
  39. $Perms = Permissions::get_permissions($UserInfo['PermissionID']);
  40. $UserClass = $Perms['Class'];
  41. }
  42. $BookmarkView = false;
  43. if (empty($_GET['type'])) {
  44. $Title = 'Requests';
  45. if (empty($_GET['showall'])) {
  46. $SphQL->where('visible', 1);
  47. }
  48. } else {
  49. switch ($_GET['type']) {
  50. case 'created':
  51. if (!empty($UserInfo)) {
  52. if (!check_paranoia('requestsvoted_list', $UserInfo['Paranoia'], $Perms['Class'], $UserInfo['ID'])) {
  53. json_die("failure");
  54. }
  55. $Title = "Requests created by $UserInfo[Username]";
  56. $SphQL->where('userid', $UserInfo['ID']);
  57. } else {
  58. $Title = 'My requests';
  59. $SphQL->where('userid', $LoggedUser['ID']);
  60. }
  61. break;
  62. case 'voted':
  63. if (!empty($UserInfo)) {
  64. if (!check_paranoia('requestsvoted_list', $UserInfo['Paranoia'], $Perms['Class'], $UserInfo['ID'])) {
  65. json_die("failure");
  66. }
  67. $Title = "Requests voted for by $UserInfo[Username]";
  68. $SphQL->where('voter', $UserInfo['ID']);
  69. } else {
  70. $Title = 'Requests I have voted on';
  71. $SphQL->where('voter', $LoggedUser['ID']);
  72. }
  73. break;
  74. case 'filled':
  75. if (!empty($UserInfo)) {
  76. if (!check_paranoia('requestsfilled_list', $UserInfo['Paranoia'], $Perms['Class'], $UserInfo['ID'])) {
  77. json_die("failure");
  78. }
  79. $Title = "Requests filled by $UserInfo[Username]";
  80. $SphQL->where('fillerid', $UserInfo['ID']);
  81. } else {
  82. $Title = 'Requests I have filled';
  83. $SphQL->where('fillerid', $LoggedUser['ID']);
  84. }
  85. break;
  86. case 'bookmarks':
  87. $Title = 'Your bookmarked requests';
  88. $BookmarkView = true;
  89. $SphQL->where('bookmarker', $LoggedUser['ID']);
  90. break;
  91. default:
  92. json_die("failure");
  93. }
  94. }
  95. if ($Submitted && empty($_GET['show_filled'])) {
  96. $SphQL->where('torrentid', 0);
  97. }
  98. if (!empty($_GET['formats'])) {
  99. $FormatArray = $_GET['formats'];
  100. if (count($FormatArray) !== count($Formats)) {
  101. $FormatNameArray = array();
  102. foreach ($FormatArray as $Index => $MasterIndex) {
  103. if (isset($Formats[$MasterIndex])) {
  104. $FormatNameArray[$Index] = '"' . strtr(Sphinxql::sph_escape_string($Formats[$MasterIndex]), '-.', ' ') . '"';
  105. }
  106. }
  107. if (count($FormatNameArray) >= 1) {
  108. if (!empty($_GET['formats_strict'])) {
  109. $SearchString = '(' . implode(' | ', $FormatNameArray) . ')';
  110. } else {
  111. $SearchString = '(any | ' . implode(' | ', $FormatNameArray) . ')';
  112. }
  113. $SphQL->where_match($SearchString, 'formatlist', false);
  114. }
  115. }
  116. }
  117. if (!empty($_GET['media'])) {
  118. $MediaArray = $_GET['media'];
  119. if (count($MediaArray) !== count($Media)) {
  120. $MediaNameArray = array();
  121. foreach ($MediaArray as $Index => $MasterIndex) {
  122. if (isset($Media[$MasterIndex])) {
  123. $MediaNameArray[$Index] = '"' . strtr(Sphinxql::sph_escape_string($Media[$MasterIndex]), '-.', ' ') . '"';
  124. }
  125. }
  126. if (count($MediaNameArray) >= 1) {
  127. if (!empty($_GET['media_strict'])) {
  128. $SearchString = '(' . implode(' | ', $MediaNameArray) . ')';
  129. } else {
  130. $SearchString = '(any | ' . implode(' | ', $MediaNameArray) . ')';
  131. }
  132. $SphQL->where_match($SearchString, 'medialist', false);
  133. }
  134. }
  135. }
  136. if (!empty($_GET['bitrates'])) {
  137. $BitrateArray = $_GET['bitrates'];
  138. if (count($BitrateArray) !== count($Bitrates)) {
  139. $BitrateNameArray = array();
  140. foreach ($BitrateArray as $Index => $MasterIndex) {
  141. if (isset($Bitrates[$MasterIndex])) {
  142. $BitrateNameArray[$Index] = '"' . strtr(Sphinxql::sph_escape_string($Bitrates[$MasterIndex]), '-.', ' ') . '"';
  143. }
  144. }
  145. if (count($BitrateNameArray) >= 1) {
  146. if (!empty($_GET['bitrate_strict'])) {
  147. $SearchString = '(' . implode(' | ', $BitrateNameArray) . ')';
  148. } else {
  149. $SearchString = '(any | ' . implode(' | ', $BitrateNameArray) . ')';
  150. }
  151. $SphQL->where_match($SearchString, 'bitratelist', false);
  152. }
  153. }
  154. }
  155. if (!empty($_GET['search'])) {
  156. $SearchString = trim($_GET['search']);
  157. if ($SearchString !== '') {
  158. $SearchWords = array('include' => array(), 'exclude' => array());
  159. $Words = explode(' ', $SearchString);
  160. foreach ($Words as $Word) {
  161. $Word = trim($Word);
  162. // Skip isolated hyphens to enable "Artist - Title" searches
  163. if ($Word === '-') {
  164. continue;
  165. }
  166. if ($Word[0] === '!' && strlen($Word) >= 2) {
  167. if (strpos($Word, '!', 1) === false) {
  168. $SearchWords['exclude'][] = $Word;
  169. } else {
  170. $SearchWords['include'][] = $Word;
  171. }
  172. } elseif ($Word !== '') {
  173. $SearchWords['include'][] = $Word;
  174. }
  175. }
  176. }
  177. }
  178. if (!isset($_GET['tags_type']) || $_GET['tags_type'] === '1') {
  179. $TagType = 1;
  180. $_GET['tags_type'] = '1';
  181. } else {
  182. $TagType = 0;
  183. $_GET['tags_type'] = '0';
  184. }
  185. if (!empty($_GET['tags'])) {
  186. $SearchTags = array('include' => array(), 'exclude' => array());
  187. $Tags = explode(',', $_GET['tags']);
  188. foreach ($Tags as $Tag) {
  189. $Tag = trim($Tag);
  190. if ($Tag[0] === '!' && strlen($Tag) >= 2) {
  191. if (strpos($Tag, '!', 1) === false) {
  192. $SearchTags['exclude'][] = $Tag;
  193. } else {
  194. $SearchTags['include'][] = $Tag;
  195. }
  196. } elseif ($Tag !== '') {
  197. $SearchTags['include'][] = $Tag;
  198. }
  199. }
  200. $TagFilter = Tags::tag_filter_sph($SearchTags, $TagType);
  201. if (!empty($TagFilter['predicate'])) {
  202. $SphQL->where_match($TagFilter['predicate'], 'taglist', false);
  203. }
  204. } elseif (!isset($_GET['tags_type']) || $_GET['tags_type'] !== '0') {
  205. $_GET['tags_type'] = 1;
  206. } else {
  207. $_GET['tags_type'] = 0;
  208. }
  209. if (isset($SearchWords)) {
  210. $QueryParts = array();
  211. foreach ($SearchWords['include'] as $Word) {
  212. $QueryParts[] = Sphinxql::sph_escape_string($Word);
  213. }
  214. if (!empty($SearchWords['exclude'])) {
  215. foreach ($SearchWords['exclude'] as $Word) {
  216. $QueryParts[] = '!' . Sphinxql::sph_escape_string(substr($Word, 1));
  217. }
  218. }
  219. if (!empty($QueryParts)) {
  220. $SearchString = implode(' ', $QueryParts);
  221. $SphQL->where_match($SearchString, '*', false);
  222. }
  223. }
  224. if (!empty($_GET['filter_cat'])) {
  225. $CategoryArray = array_keys($_GET['filter_cat']);
  226. if (count($CategoryArray) !== count($Categories)) {
  227. foreach ($CategoryArray as $Key => $Index) {
  228. if (!isset($Categories[$Index - 1])) {
  229. unset($CategoryArray[$Key]);
  230. }
  231. }
  232. if (count($CategoryArray) >= 1) {
  233. $SphQL->where('categoryid', $CategoryArray);
  234. }
  235. }
  236. }
  237. if (!empty($_GET['releases'])) {
  238. $ReleaseArray = $_GET['releases'];
  239. if (count($ReleaseArray) !== count($ReleaseTypes)) {
  240. foreach ($ReleaseArray as $Index => $Value) {
  241. if (!isset($ReleaseTypes[$Value])) {
  242. unset($ReleaseArray[$Index]);
  243. }
  244. }
  245. if (count($ReleaseArray) >= 1) {
  246. $SphQL->where('releasetype', $ReleaseArray);
  247. }
  248. }
  249. }
  250. if (!empty($_GET['requestor'])) {
  251. if (is_number($_GET['requestor'])) {
  252. $SphQL->where('userid', $_GET['requestor']);
  253. } else {
  254. error(404);
  255. }
  256. }
  257. if (isset($_GET['year'])) {
  258. if (is_number($_GET['year']) || $_GET['year'] === '0') {
  259. $SphQL->where('year', $_GET['year']);
  260. } else {
  261. error(404);
  262. }
  263. }
  264. if (!empty($_GET['page']) && is_number($_GET['page']) && $_GET['page'] > 0) {
  265. $Page = $_GET['page'];
  266. $Offset = ($Page - 1) * REQUESTS_PER_PAGE;
  267. $SphQL->limit($Offset, REQUESTS_PER_PAGE, $Offset + REQUESTS_PER_PAGE);
  268. } else {
  269. $Page = 1;
  270. $SphQL->limit(0, REQUESTS_PER_PAGE, REQUESTS_PER_PAGE);
  271. }
  272. $SphQLResult = $SphQL->query();
  273. $NumResults = (int)$SphQLResult->get_meta('total_found');
  274. if ($NumResults > 0) {
  275. $SphRequests = $SphQLResult->to_array('id');
  276. if ($OrderBy === 'random') {
  277. $NumResults = count($RequestIDs);
  278. }
  279. if ($NumResults > REQUESTS_PER_PAGE) {
  280. if (($Page - 1) * REQUESTS_PER_PAGE > $NumResults) {
  281. $Page = 0;
  282. }
  283. }
  284. }
  285. if ($NumResults == 0) {
  286. json_die("success", array(
  287. 'currentPage' => 1,
  288. 'pages' => 1,
  289. 'results' => array()
  290. ));
  291. } else {
  292. $JsonResults = array();
  293. $Requests = Requests::get_requests(array_keys($SphRequests));
  294. foreach ($SphRequests as $RequestID => $SphRequest) {
  295. $Request = $Requests[$RequestID];
  296. $VoteCount = $SphRequest['votes'];
  297. $Bounty = $SphRequest['bounty'] * 1024; // Sphinx stores bounty in kB
  298. $Requestor = Users::user_info($Request['UserID']);
  299. $Filler = $Request['FillerID'] ? Users::user_info($Request['FillerID']) : null;
  300. if ($Request['CategoryID'] == 0) {
  301. $CategoryName = 'Unknown';
  302. } else {
  303. $CategoryName = $Categories[$Request['CategoryID'] - 1];
  304. }
  305. $JsonArtists = pullmediainfo(Requests::get_artists($RequestID));
  306. $Tags = $Request['Tags'];
  307. $JsonResults[] = array(
  308. 'requestId' => (int)$RequestID,
  309. 'requestorId' => (int)$Requestor['ID'],
  310. 'requestorName' => $Requestor['Username'],
  311. 'timeAdded' => $Request['TimeAdded'],
  312. 'lastVote' => $Request['LastVote'],
  313. 'voteCount' => (int)$VoteCount,
  314. 'bounty' => (int)$Bounty,
  315. 'categoryId' => (int)$Request['CategoryID'],
  316. 'categoryName' => $CategoryName,
  317. 'artists' => $JsonArtists,
  318. 'title' => $Request['Title'],
  319. 'year' => (int)$Request['Year'],
  320. 'image' => $Request['Image'],
  321. 'description' => $Request['Description'],
  322. 'isFilled' => ($Request['TorrentID'] > 0),
  323. 'fillerId' => (int)$Request['FillerID'],
  324. 'fillerName' => $Filler ? $Filler['Username'] : '',
  325. 'torrentId' => (int)$Request['TorrentID'],
  326. 'timeFilled' => $Request['TimeFilled'] == 0 ? '' : $Request['TimeFilled']
  327. );
  328. }
  329. json_die("success", array(
  330. 'currentPage' => intval($Page),
  331. 'pages' => ceil($NumResults / REQUESTS_PER_PAGE),
  332. 'results' => $JsonResults
  333. ));
  334. }
  335. ?>