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

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