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 11KB

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