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.

take_new_edit.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?
  2. //******************************************************************************//
  3. //----------------- Take request -----------------------------------------------//
  4. authorize();
  5. if ($_POST['action'] !== 'takenew' && $_POST['action'] !== 'takeedit') {
  6. error(0);
  7. }
  8. $NewRequest = ($_POST['action'] === 'takenew');
  9. if (!$NewRequest) {
  10. $ReturnEdit = true;
  11. }
  12. if ($NewRequest) {
  13. if (!check_perms('site_submit_requests') || $LoggedUser['BytesUploaded'] < 250 * 1024 * 1024) {
  14. error(403);
  15. }
  16. } else {
  17. $RequestID = $_POST['requestid'];
  18. if (!is_number($RequestID)) {
  19. error(0);
  20. }
  21. $Request = Requests::get_request($RequestID);
  22. if ($Request === false) {
  23. error(404);
  24. }
  25. $VoteArray = Requests::get_votes_array($RequestID);
  26. $VoteCount = count($VoteArray['Voters']);
  27. $IsFilled = !empty($Request['TorrentID']);
  28. $CategoryName = $Categories[$Request['CategoryID'] - 1];
  29. $ProjectCanEdit = (check_perms('project_team') && !$IsFilled && ($Request['CategoryID'] === '0' || ($CategoryName === 'Music' && $Year === '0')));
  30. $CanEdit = ((!$IsFilled && $LoggedUser['ID'] === $Request['UserID'] && $VoteCount < 2) || $ProjectCanEdit || check_perms('site_moderate_requests'));
  31. if (!$CanEdit) {
  32. error(403);
  33. }
  34. }
  35. // Validate
  36. if (empty($_POST['type'])) {
  37. error(0);
  38. }
  39. $CategoryName = $_POST['type'];
  40. $CategoryID = (array_search($CategoryName, $Categories) + 1);
  41. if (empty($CategoryID)) {
  42. error(0);
  43. }
  44. if (empty($_POST['title']) && empty($_POST['title_rj']) && empty($_POST['title_jp'])) {
  45. $Err = 'You must enter at least one title!';
  46. }
  47. if (!empty($_POST['title'])) {
  48. $Title = trim($_POST['title']);
  49. }
  50. if (!empty($_POST['title_rj'])) {
  51. $TitleRJ = trim($_POST['title_rj']);
  52. }
  53. if (!empty($_POST['title_jp'])) {
  54. $TitleJP = trim($_POST['title_jp']);
  55. }
  56. if (empty($_POST['tags'])) {
  57. $Err = 'You forgot to enter any tags!';
  58. } else {
  59. $Tags = trim($_POST['tags']);
  60. }
  61. if ($NewRequest) {
  62. if (empty($_POST['amount'])) {
  63. $Err = 'You forgot to enter any bounty!';
  64. } else {
  65. $Bounty = trim($_POST['amount']);
  66. if (!is_number($Bounty)) {
  67. $Err = 'Your entered bounty is not a number';
  68. } elseif ($Bounty < 100 * 1024 * 1024) {
  69. $Err = 'Minimum bounty is 100 MB.';
  70. }
  71. $Bytes = $Bounty; //From MB to B
  72. }
  73. }
  74. if (empty($_POST['image'])) {
  75. $Image = '';
  76. } else {
  77. ImageTools::blacklisted($_POST['image']);
  78. if (preg_match('/'.IMAGE_REGEX.'/', trim($_POST['image'])) > 0) {
  79. $Image = trim($_POST['image']);
  80. } else {
  81. $Err = display_str($_POST['image']).' does not appear to be a valid link to an image.';
  82. }
  83. }
  84. if (empty($_POST['description'])) {
  85. $Err = 'You forgot to enter a description.';
  86. } else {
  87. $Description = trim($_POST['description']);
  88. }
  89. if (empty($_POST['artists']) && $CategoryName != 'Other') {
  90. $Err = 'You did not enter any artists.';
  91. } else {
  92. $Artists = $_POST['artists'];
  93. }
  94. //Not required
  95. if (!empty($_POST['cataloguenumber']) && $CategoryName == 'Movies') {
  96. $CatalogueNumber = trim($_POST['cataloguenumber']);
  97. } else {
  98. $CatalogueNumber = '';
  99. }
  100. if (!empty($_POST['dlsiteid']) && $CategoryName == 'Games') {
  101. $DLSiteID = trim($_POST['dlsiteid']);
  102. } else {
  103. $DLSiteID = '';
  104. }
  105. // GroupID
  106. if (!empty($_POST['groupid'])) {
  107. $GroupID = $_POST['groupid'];
  108. if (is_number($GroupID)) {
  109. $DB->query("
  110. SELECT CategoryID
  111. FROM torrents_group
  112. WHERE ID = '$GroupID'");
  113. if (!$DB->has_results()) {
  114. $Err = 'The torrent group, if entered, must correspond to a torrent group on the site.';
  115. } else {
  116. if ($CategoryID != $DB->to_array()[0]['CategoryID']) {
  117. $Err = 'The category of the specified torrent group does not match the category of your request.';
  118. }
  119. }
  120. } else {
  121. $Err = 'The torrent group, if entered, must correspond to a torrent group on the site.';
  122. }
  123. } elseif (isset($_POST['groupid']) && $_POST['groupid'] === '0') {
  124. $GroupID = 0;
  125. }
  126. //For refilling on error
  127. $ArtistNames = [];
  128. $ArtistForm = [];
  129. for ($i = 0; $i < count($Artists); $i++) {
  130. if (trim($Artists[$i]) !== '') {
  131. if (!in_array($Artists[$i], $ArtistNames)) {
  132. $ArtistForm[] = array('name' => trim($Artists[$i]));
  133. $ArtistNames[] = trim($Artists[$i]);
  134. }
  135. }
  136. }
  137. if (!isset($ArtistNames[0])) {
  138. unset($ArtistForm);
  139. }
  140. if (!empty($Err)) {
  141. error($Err);
  142. $Div = $_POST['unit'] === 'mb' ? 1024 * 1024 : 1024 * 1024 * 1024;
  143. $Bounty /= $Div;
  144. include(SERVER_ROOT.'/sections/requests/new_edit.php');
  145. die();
  146. }
  147. //Databasify the input
  148. /*if ($CategoryName === 'Music') {
  149. if (empty($AllBitrates)) {
  150. foreach ($BitrateArray as $Index => $MasterIndex) {
  151. if (array_key_exists($Index, $Bitrates)) {
  152. $BitrateArray[$Index] = $Bitrates[$MasterIndex];
  153. } else {
  154. //Hax
  155. error(0);
  156. }
  157. }
  158. $BitrateList = implode('|', $BitrateArray);
  159. } else {
  160. $BitrateList = 'Any';
  161. }
  162. if (empty($AllFormats)) {
  163. foreach ($FormatArray as $Index => $MasterIndex) {
  164. if (array_key_exists($Index, $Formats)) {
  165. $FormatArray[$Index] = $Formats[$MasterIndex];
  166. } else {
  167. //Hax
  168. error(0);
  169. }
  170. }
  171. $FormatList = implode('|', $FormatArray);
  172. } else {
  173. $FormatList = 'Any';
  174. }
  175. if (empty($AllMedia)) {
  176. foreach ($MediaArray as $Index => $MasterIndex) {
  177. if (array_key_exists($Index, $Media)) {
  178. $MediaArray[$Index] = $Media[$MasterIndex];
  179. } else {
  180. //Hax
  181. error(0);
  182. }
  183. }
  184. $MediaList = implode('|', $MediaArray);
  185. } else {
  186. $MediaList = 'Any';
  187. }
  188. $LogCue = '';
  189. if ($NeedLog) {
  190. $LogCue .= 'Log';
  191. if ($MinLogScore > 0) {
  192. if ($MinLogScore >= 100) {
  193. $LogCue .= ' (100%)';
  194. } else {
  195. $LogCue .= ' (>= '.$MinLogScore.'%)';
  196. }
  197. }
  198. }
  199. if ($NeedCue) {
  200. if ($LogCue !== '') {
  201. $LogCue .= ' + Cue';
  202. } else {
  203. $LogCue = 'Cue';
  204. }
  205. }
  206. }*/
  207. if (!isset($GroupID)) $GroupID = '';
  208. //Query time!
  209. if ($NewRequest) {
  210. $DB->query('
  211. INSERT INTO requests (
  212. UserID, TimeAdded, LastVote, CategoryID, Title, TitleRJ, TitleJP, Image, Description,
  213. CatalogueNumber, DLSiteID, Visible, GroupID)
  214. VALUES
  215. ('.$LoggedUser['ID'].", NOW(), NOW(), $CategoryID, '".db_string($Title)."', '".db_string($TitleRJ)."', '".db_string($TitleJP)."', '".db_string($Image)."', '".db_string($Description)."',
  216. '".db_string($CatalogueNumber)."', '".db_string($DLSiteID)."', '1', '$GroupID')");
  217. $RequestID = $DB->inserted_id();
  218. } else {
  219. $DB->query("
  220. UPDATE requests
  221. SET CategoryID = $CategoryID,
  222. Title = '".db_string($Title)."',
  223. TitleRJ = '".db_string($TitleRJ??"")."',
  224. TitleJP = '".db_string($TitleJP??"")."',
  225. Image = '".db_string($Image)."',
  226. Description = '".db_string($Description)."',
  227. CatalogueNumber = '".db_string($CatalogueNumber)."',
  228. DLSiteID = '".db_string($DLSiteID)."'
  229. WHERE ID = $RequestID");
  230. // We need to be able to delete artists / tags
  231. $DB->query("
  232. SELECT ArtistID
  233. FROM requests_artists
  234. WHERE RequestID = $RequestID");
  235. $RequestArtists = $DB->to_array();
  236. foreach ($RequestArtists as $RequestArtist) {
  237. $Cache->delete_value("artists_requests_".$RequestArtist['ArtistID']);
  238. }
  239. $DB->query("
  240. DELETE FROM requests_artists
  241. WHERE RequestID = $RequestID");
  242. $Cache->delete_value("request_artists_$RequestID");
  243. }
  244. if ($GroupID) {
  245. $Cache->delete_value("requests_group_$GroupID");
  246. }
  247. /*
  248. * Multiple Artists!
  249. * For the multiple artists system, we have 3 steps:
  250. * 1. See if each artist given already exists and if it does, grab the ID.
  251. * 2. For each artist that didn't exist, create an artist.
  252. * 3. Create a row in the requests_artists table for each artist, based on the ID.
  253. */
  254. if (isset($ArtistForm)) {
  255. foreach ($ArtistForm as $Num => $Artist) {
  256. //1. See if each artist given already exists and if it does, grab the ID.
  257. $DB->query("
  258. SELECT
  259. ArtistID,
  260. Name
  261. FROM artists_group
  262. WHERE Name = '".db_string($Artist['name'])."'");
  263. list($ArtistID, $ArtistName) = $DB->next_record(MYSQLI_NUM, false);
  264. $ArtistForm[$Num] = array('name' => $ArtistName, 'id' => $ArtistID);
  265. if (!$ArtistID) {
  266. //2. For each artist that didn't exist, create an artist.
  267. $DB->query("
  268. INSERT INTO artists_group (Name)
  269. VALUES ('".db_string($Artist['name'])."')");
  270. $ArtistID = $DB->inserted_id();
  271. $Cache->increment('stats_artist_count');
  272. $ArtistForm[$Num] = array('id' => $ArtistID, 'name' => $Artist['name']);
  273. }
  274. }
  275. //3. Create a row in the requests_artists table for each artist, based on the ID.
  276. foreach ($ArtistForm as $Num => $Artist) {
  277. $DB->query("
  278. INSERT IGNORE INTO requests_artists
  279. (RequestID, ArtistID)
  280. VALUES
  281. ($RequestID, ".$Artist['id'].")");
  282. $Cache->delete_value('artists_requests_'.$Artist['id']);
  283. }
  284. //End Music only
  285. } else {
  286. //Not a music request anymore, delete music only fields.
  287. if (!$NewRequest) {
  288. $DB->query("
  289. SELECT ArtistID
  290. FROM requests_artists
  291. WHERE RequestID = $RequestID");
  292. $OldArtists = $DB->collect('ArtistID');
  293. foreach ($OldArtists as $ArtistID) {
  294. if (empty($ArtistID)) {
  295. continue;
  296. }
  297. //Get a count of how many groups or requests use the artist ID
  298. $DB->query("
  299. SELECT COUNT(ag.ArtistID)
  300. FROM artists_group AS ag
  301. LEFT JOIN requests_artists AS ra ON ag.ArtistID = ra.ArtistID
  302. WHERE ra.ArtistID IS NOT NULL
  303. AND ag.ArtistID = '$ArtistID'");
  304. list($ReqCount) = $DB->next_record();
  305. $DB->query("
  306. SELECT COUNT(ag.ArtistID)
  307. FROM artists_group AS ag
  308. LEFT JOIN torrents_artists AS ta ON ag.ArtistID = ta.ArtistID
  309. WHERE ta.ArtistID IS NOT NULL
  310. AND ag.ArtistID = '$ArtistID'");
  311. list($GroupCount) = $DB->next_record();
  312. if (($ReqCount + $GroupCount) == 0) {
  313. //The only group to use this artist
  314. Artists::delete_artist($ArtistID);
  315. } else {
  316. //Not the only group, still need to clear cache
  317. $Cache->delete_value("artists_requests_$ArtistID");
  318. }
  319. }
  320. $DB->query("
  321. DELETE FROM requests_artists
  322. WHERE RequestID = $RequestID");
  323. $Cache->delete_value("request_artists_$RequestID");
  324. }
  325. }
  326. //Tags
  327. if (!$NewRequest) {
  328. $DB->query("
  329. DELETE FROM requests_tags
  330. WHERE RequestID = $RequestID");
  331. }
  332. $Tags = array_unique(explode(',', $Tags));
  333. foreach ($Tags as $Index => $Tag) {
  334. $Tag = Misc::sanitize_tag($Tag);
  335. $Tag = Misc::get_alias_tag($Tag);
  336. $Tags[$Index] = $Tag; //For announce
  337. $DB->query("
  338. INSERT INTO tags
  339. (Name, UserID)
  340. VALUES
  341. ('$Tag', ".$LoggedUser['ID'].")
  342. ON DUPLICATE KEY UPDATE
  343. Uses = Uses + 1");
  344. $TagID = $DB->inserted_id();
  345. $DB->query("
  346. INSERT IGNORE INTO requests_tags
  347. (TagID, RequestID)
  348. VALUES
  349. ($TagID, $RequestID)");
  350. }
  351. if ($NewRequest) {
  352. //Remove the bounty and create the vote
  353. $DB->query("
  354. INSERT INTO requests_votes
  355. (RequestID, UserID, Bounty)
  356. VALUES
  357. ($RequestID, ".$LoggedUser['ID'].', '.($Bytes * (1 - $RequestTax)).')');
  358. $DB->query("
  359. UPDATE users_main
  360. SET Uploaded = (Uploaded - $Bytes)
  361. WHERE ID = ".$LoggedUser['ID']);
  362. $Cache->delete_value('user_stats_'.$LoggedUser['ID']);
  363. $AnnounceTitle = empty($Title) ? (empty($TitleRJ) ? $TitleJP : $TitleRJ) : $Title;
  364. $Announce = "\"$AnnounceTitle\"".(isset($ArtistForm)?(' - '.Artists::display_artists($ArtistForm, false, false)):'').' '.site_url()."requests.php?action=view&id=$RequestID - ".implode(' ', $Tags);
  365. send_irc('PRIVMSG '.BOT_REQUEST_CHAN.' '.$Announce);
  366. } else {
  367. $Cache->delete_value("request_$RequestID");
  368. $Cache->delete_value("request_artists_$RequestID");
  369. }
  370. Requests::update_sphinx_requests($RequestID);
  371. header("Location: requests.php?action=view&id=$RequestID");
  372. ?>