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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 ($CategoryName != 'Other') {
  90. if (empty($_POST['artists'])) {
  91. $Err = 'You did not enter any artists.';
  92. } else {
  93. $Artists = $_POST['artists'];
  94. }
  95. //Not required
  96. if (!empty($_POST['cataloguenumber']) && $CategoryName == 'Movies') {
  97. $CatalogueNumber = trim($_POST['cataloguenumber']);
  98. } else {
  99. $CatalogueNumber = '';
  100. }
  101. if (!empty($_POST['dlsiteid']) && $CategoryName == 'Games') {
  102. $DLSiteID = trim($_POST['dlsiteid']);
  103. } else {
  104. $DLSiteID = '';
  105. }
  106. }
  107. // GroupID
  108. if (!empty($_POST['groupid'])) {
  109. $GroupID = $_POST['groupid'];
  110. if (is_number($GroupID)) {
  111. $DB->query("
  112. SELECT CategoryID
  113. FROM torrents_group
  114. WHERE ID = '$GroupID'");
  115. if (!$DB->has_results()) {
  116. $Err = 'The torrent group, if entered, must correspond to a torrent group on the site.';
  117. } else {
  118. if ($CategoryID != $DB->to_array()[0]['CategoryID']) {
  119. $Err = 'The category of the specified torrent group does not match the category of your request.';
  120. }
  121. }
  122. } else {
  123. $Err = 'The torrent group, if entered, must correspond to a torrent group on the site.';
  124. }
  125. } elseif (isset($_POST['groupid']) && $_POST['groupid'] === '0') {
  126. $GroupID = 0;
  127. }
  128. //For refilling on error
  129. if ($CategoryName != 'Other') {
  130. $ArtistNames = array();
  131. $ArtistForm = array();
  132. for ($i = 0; $i < count($Artists); $i++) {
  133. if (trim($Artists[$i]) !== '') {
  134. if (!in_array($Artists[$i], $ArtistNames)) {
  135. $ArtistForm[] = array('name' => trim($Artists[$i]));
  136. $ArtistNames[] = trim($Artists[$i]);
  137. }
  138. }
  139. }
  140. if (!isset($ArtistNames[0])) {
  141. unset($ArtistForm);
  142. }
  143. }
  144. if (!empty($Err)) {
  145. error($Err);
  146. $Div = $_POST['unit'] === 'mb' ? 1024 * 1024 : 1024 * 1024 * 1024;
  147. $Bounty /= $Div;
  148. include(SERVER_ROOT.'/sections/requests/new_edit.php');
  149. die();
  150. }
  151. //Databasify the input
  152. /*if ($CategoryName === 'Music') {
  153. if (empty($AllBitrates)) {
  154. foreach ($BitrateArray as $Index => $MasterIndex) {
  155. if (array_key_exists($Index, $Bitrates)) {
  156. $BitrateArray[$Index] = $Bitrates[$MasterIndex];
  157. } else {
  158. //Hax
  159. error(0);
  160. }
  161. }
  162. $BitrateList = implode('|', $BitrateArray);
  163. } else {
  164. $BitrateList = 'Any';
  165. }
  166. if (empty($AllFormats)) {
  167. foreach ($FormatArray as $Index => $MasterIndex) {
  168. if (array_key_exists($Index, $Formats)) {
  169. $FormatArray[$Index] = $Formats[$MasterIndex];
  170. } else {
  171. //Hax
  172. error(0);
  173. }
  174. }
  175. $FormatList = implode('|', $FormatArray);
  176. } else {
  177. $FormatList = 'Any';
  178. }
  179. if (empty($AllMedia)) {
  180. foreach ($MediaArray as $Index => $MasterIndex) {
  181. if (array_key_exists($Index, $Media)) {
  182. $MediaArray[$Index] = $Media[$MasterIndex];
  183. } else {
  184. //Hax
  185. error(0);
  186. }
  187. }
  188. $MediaList = implode('|', $MediaArray);
  189. } else {
  190. $MediaList = 'Any';
  191. }
  192. $LogCue = '';
  193. if ($NeedLog) {
  194. $LogCue .= 'Log';
  195. if ($MinLogScore > 0) {
  196. if ($MinLogScore >= 100) {
  197. $LogCue .= ' (100%)';
  198. } else {
  199. $LogCue .= ' (>= '.$MinLogScore.'%)';
  200. }
  201. }
  202. }
  203. if ($NeedCue) {
  204. if ($LogCue !== '') {
  205. $LogCue .= ' + Cue';
  206. } else {
  207. $LogCue = 'Cue';
  208. }
  209. }
  210. }*/
  211. if (!isset($GroupID)) $GroupID = '';
  212. //Query time!
  213. if ($NewRequest) {
  214. $DB->query('
  215. INSERT INTO requests (
  216. UserID, TimeAdded, LastVote, CategoryID, Title, TitleRJ, TitleJP, Image, Description,
  217. CatalogueNumber, DLSiteID, Visible, GroupID)
  218. VALUES
  219. ('.$LoggedUser['ID'].", '".sqltime()."', '".sqltime()."', $CategoryID, '".db_string($Title)."', '".db_string($TitleRJ)."', '".db_string($TitleJP)."', '".db_string($Image)."', '".db_string($Description)."',
  220. '".db_string($CatalogueNumber)."', '".db_string($DLSiteID)."', '1', '$GroupID')");
  221. $RequestID = $DB->inserted_id();
  222. } else {
  223. $DB->query("
  224. UPDATE requests
  225. SET CategoryID = $CategoryID,
  226. Title = '".db_string($Title)."',
  227. TitleRJ = '".db_string($TitleRJ)."',
  228. TitleJP = '".db_string($TitleJP)."',
  229. Image = '".db_string($Image)."',
  230. Description = '".db_string($Description)."',
  231. CatalogueNumber = '".db_string($CatalogueNumber)."',
  232. DLSiteID = '".db_string($DLSiteID)."'
  233. WHERE ID = $RequestID");
  234. // We need to be able to delete artists / tags
  235. $DB->query("
  236. SELECT ArtistID
  237. FROM requests_artists
  238. WHERE RequestID = $RequestID");
  239. $RequestArtists = $DB->to_array();
  240. foreach ($RequestArtists as $RequestArtist) {
  241. $Cache->delete_value("artists_requests_".$RequestArtist['ArtistID']);
  242. }
  243. $DB->query("
  244. DELETE FROM requests_artists
  245. WHERE RequestID = $RequestID");
  246. $Cache->delete_value("request_artists_$RequestID");
  247. }
  248. if ($GroupID) {
  249. $Cache->delete_value("requests_group_$GroupID");
  250. }
  251. /*
  252. * Multiple Artists!
  253. * For the multiple artists system, we have 3 steps:
  254. * 1. See if each artist given already exists and if it does, grab the ID.
  255. * 2. For each artist that didn't exist, create an artist.
  256. * 3. Create a row in the requests_artists table for each artist, based on the ID.
  257. */
  258. if (isset($CategoryName) && $CategoryName != "Other" && isset($ArtistForm)) {
  259. foreach ($ArtistForm as $Num => $Artist) {
  260. //1. See if each artist given already exists and if it does, grab the ID.
  261. $DB->query("
  262. SELECT
  263. ArtistID,
  264. Name
  265. FROM artists_group
  266. WHERE Name = '".db_string($Artist['name'])."'");
  267. list($ArtistID, $ArtistName) = $DB->next_record(MYSQLI_NUM, false);
  268. $ArtistForm[$Num] = array('name' => $ArtistName, 'id' => $ArtistID);
  269. if (!$ArtistID) {
  270. //2. For each artist that didn't exist, create an artist.
  271. $DB->query("
  272. INSERT INTO artists_group (Name)
  273. VALUES ('".db_string($Artist['name'])."')");
  274. $ArtistID = $DB->inserted_id();
  275. $Cache->increment('stats_artist_count');
  276. $ArtistForm[$Num] = array('id' => $ArtistID, 'name' => $Artist['name']);
  277. }
  278. }
  279. //3. Create a row in the requests_artists table for each artist, based on the ID.
  280. foreach ($ArtistForm as $Num => $Artist) {
  281. $DB->query("
  282. INSERT IGNORE INTO requests_artists
  283. (RequestID, ArtistID)
  284. VALUES
  285. ($RequestID, ".$Artist['id'].")");
  286. $Cache->delete_value('artists_requests_'.$Artist['id']);
  287. }
  288. //End Music only
  289. } else {
  290. //Not a music request anymore, delete music only fields.
  291. if (!$NewRequest) {
  292. $DB->query("
  293. SELECT ArtistID
  294. FROM requests_artists
  295. WHERE RequestID = $RequestID");
  296. $OldArtists = $DB->collect('ArtistID');
  297. foreach ($OldArtists as $ArtistID) {
  298. if (empty($ArtistID)) {
  299. continue;
  300. }
  301. //Get a count of how many groups or requests use the artist ID
  302. $DB->query("
  303. SELECT COUNT(ag.ArtistID)
  304. FROM artists_group AS ag
  305. LEFT JOIN requests_artists AS ra ON ag.ArtistID = ra.ArtistID
  306. WHERE ra.ArtistID IS NOT NULL
  307. AND ag.ArtistID = '$ArtistID'");
  308. list($ReqCount) = $DB->next_record();
  309. $DB->query("
  310. SELECT COUNT(ag.ArtistID)
  311. FROM artists_group AS ag
  312. LEFT JOIN torrents_artists AS ta ON ag.ArtistID = ta.ArtistID
  313. WHERE ta.ArtistID IS NOT NULL
  314. AND ag.ArtistID = '$ArtistID'");
  315. list($GroupCount) = $DB->next_record();
  316. if (($ReqCount + $GroupCount) == 0) {
  317. //The only group to use this artist
  318. Artists::delete_artist($ArtistID);
  319. } else {
  320. //Not the only group, still need to clear cache
  321. $Cache->delete_value("artists_requests_$ArtistID");
  322. }
  323. }
  324. $DB->query("
  325. DELETE FROM requests_artists
  326. WHERE RequestID = $RequestID");
  327. $Cache->delete_value("request_artists_$RequestID");
  328. }
  329. }
  330. //Tags
  331. if (!$NewRequest) {
  332. $DB->query("
  333. DELETE FROM requests_tags
  334. WHERE RequestID = $RequestID");
  335. }
  336. $Tags = array_unique(explode(',', $Tags));
  337. foreach ($Tags as $Index => $Tag) {
  338. $Tag = Misc::sanitize_tag($Tag);
  339. $Tag = Misc::get_alias_tag($Tag);
  340. $Tags[$Index] = $Tag; //For announce
  341. $DB->query("
  342. INSERT INTO tags
  343. (Name, UserID)
  344. VALUES
  345. ('$Tag', ".$LoggedUser['ID'].")
  346. ON DUPLICATE KEY UPDATE
  347. Uses = Uses + 1");
  348. $TagID = $DB->inserted_id();
  349. $DB->query("
  350. INSERT IGNORE INTO requests_tags
  351. (TagID, RequestID)
  352. VALUES
  353. ($TagID, $RequestID)");
  354. }
  355. if ($NewRequest) {
  356. //Remove the bounty and create the vote
  357. $DB->query("
  358. INSERT INTO requests_votes
  359. (RequestID, UserID, Bounty)
  360. VALUES
  361. ($RequestID, ".$LoggedUser['ID'].', '.($Bytes * (1 - $RequestTax)).')');
  362. $DB->query("
  363. UPDATE users_main
  364. SET Uploaded = (Uploaded - $Bytes)
  365. WHERE ID = ".$LoggedUser['ID']);
  366. $Cache->delete_value('user_stats_'.$LoggedUser['ID']);
  367. $AnnounceTitle = empty($Title) ? (empty($TitleRJ) ? $TitleJP : $TitleRJ) : $Title;
  368. if ($CategoryName != 'Other') {
  369. $Announce = "\"$AnnounceTitle\"".(isset($ArtistForm)?(' - '.Artists::display_artists($ArtistForm, false, false)):'').' '.site_url()."requests.php?action=view&id=$RequestID - ".implode(' ', $Tags);
  370. } else {
  371. $Announce = "\"$AnnounceTitle\" - ".site_url()."requests.php?action=view&id=$RequestID - ".implode(' ', $Tags);
  372. }
  373. send_irc('PRIVMSG '.BOT_REQUEST_CHAN.' '.$Announce);
  374. } else {
  375. $Cache->delete_value("request_$RequestID");
  376. $Cache->delete_value("request_artists_$RequestID");
  377. }
  378. Requests::update_sphinx_requests($RequestID);
  379. header("Location: requests.php?action=view&id=$RequestID");
  380. ?>