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