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.

add_torrent.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. #declare(strict_types=1);
  3. authorize();
  4. include(SERVER_ROOT.'/classes/validate.class.php');
  5. $Val = new Validate;
  6. function add_torrent($CollageID, $GroupID) {
  7. global $Cache, $LoggedUser, $DB;
  8. $DB->query("
  9. SELECT MAX(Sort)
  10. FROM collages_torrents
  11. WHERE CollageID = '$CollageID'");
  12. list($Sort) = $DB->next_record();
  13. $Sort += 10;
  14. $DB->query("
  15. SELECT GroupID
  16. FROM collages_torrents
  17. WHERE CollageID = '$CollageID'
  18. AND GroupID = '$GroupID'");
  19. if (!$DB->has_results()) {
  20. $DB->query("
  21. INSERT IGNORE INTO collages_torrents
  22. (CollageID, GroupID, UserID, Sort, AddedOn)
  23. VALUES
  24. ('$CollageID', '$GroupID', '$LoggedUser[ID]', '$Sort', '" . sqltime() . "')");
  25. $DB->query("
  26. UPDATE collages
  27. SET NumTorrents = NumTorrents + 1, Updated = '" . sqltime() . "'
  28. WHERE ID = '$CollageID'");
  29. $Cache->delete_value("collage_$CollageID");
  30. $Cache->delete_value("torrents_details_$GroupID");
  31. $Cache->delete_value("torrent_collages_$GroupID");
  32. $Cache->delete_value("torrent_collages_personal_$GroupID");
  33. $DB->query("
  34. SELECT UserID
  35. FROM users_collage_subs
  36. WHERE CollageID = $CollageID");
  37. while (list($CacheUserID) = $DB->next_record()) {
  38. $Cache->delete_value("collage_subs_user_new_$CacheUserID");
  39. }
  40. }
  41. }
  42. $CollageID = $_POST['collageid'];
  43. if (!is_number($CollageID)) {
  44. error(404);
  45. }
  46. $DB->query("
  47. SELECT UserID, CategoryID, Locked, NumTorrents, MaxGroups, MaxGroupsPerUser
  48. FROM collages
  49. WHERE ID = '$CollageID'");
  50. list($UserID, $CategoryID, $Locked, $NumTorrents, $MaxGroups, $MaxGroupsPerUser) = $DB->next_record();
  51. if (!check_perms('site_collages_delete')) {
  52. if ($Locked) {
  53. $Err = 'This collage is locked';
  54. }
  55. if ($CategoryID == 0 && $UserID != $LoggedUser['ID']) {
  56. $Err = 'You cannot edit someone else\'s personal collage.';
  57. }
  58. if ($MaxGroups > 0 && $NumTorrents >= $MaxGroups) {
  59. $Err = 'This collage already holds its maximum allowed number of torrents.';
  60. }
  61. if (isset($Err)) {
  62. error($Err);
  63. }
  64. }
  65. if ($MaxGroupsPerUser > 0) {
  66. $DB->query("
  67. SELECT COUNT(*)
  68. FROM collages_torrents
  69. WHERE CollageID = '$CollageID'
  70. AND UserID = '$LoggedUser[ID]'");
  71. list($GroupsForUser) = $DB->next_record();
  72. if (!check_perms('site_collages_delete') && $GroupsForUser >= $MaxGroupsPerUser) {
  73. error(403);
  74. }
  75. }
  76. if ($_REQUEST['action'] == 'add_torrent') {
  77. $Val->SetFields('url', '1', 'regex', 'The URL must be a link to a torrent on the site.', array('regex' => '/^'.TORRENT_GROUP_REGEX.'/i'));
  78. $Err = $Val->ValidateForm($_POST);
  79. if ($Err) {
  80. error($Err);
  81. }
  82. $URL = $_POST['url'];
  83. // Get torrent ID
  84. preg_match('/^'.TORRENT_GROUP_REGEX.'/i', $URL, $Matches);
  85. $TorrentID = $Matches[4];
  86. if (!$TorrentID || (int)$TorrentID == 0) {
  87. error(404);
  88. }
  89. $DB->query("
  90. SELECT ID
  91. FROM torrents_group
  92. WHERE ID = '$TorrentID'");
  93. list($GroupID) = $DB->next_record();
  94. if (!$GroupID) {
  95. error('The torrent was not found in the database.');
  96. }
  97. add_torrent($CollageID, $GroupID);
  98. } else {
  99. $URLs = explode("\n", $_REQUEST['urls']);
  100. $GroupIDs = [];
  101. $Err = '';
  102. foreach ($URLs as $Key => &$URL) {
  103. $URL = trim($URL);
  104. if ($URL == '') {
  105. unset($URLs[$Key]);
  106. }
  107. }
  108. unset($URL);
  109. if (!check_perms('site_collages_delete')) {
  110. if ($MaxGroups > 0 && ($NumTorrents + count($URLs) > $MaxGroups)) {
  111. $Err = "This collage can only hold $MaxGroups torrents.";
  112. }
  113. if ($MaxGroupsPerUser > 0 && ($GroupsForUser + count($URLs) > $MaxGroupsPerUser)) {
  114. $Err = "You may only have $MaxGroupsPerUser torrents in this collage.";
  115. }
  116. }
  117. foreach ($URLs as $URL) {
  118. $Matches = [];
  119. if (preg_match('/^'.TORRENT_GROUP_REGEX.'/i', $URL, $Matches)) {
  120. $GroupIDs[] = $Matches[4];
  121. $GroupID = $Matches[4];
  122. } else {
  123. $Err = "One of the entered URLs ($URL) does not correspond to a torrent group on the site.";
  124. break;
  125. }
  126. $DB->query("
  127. SELECT ID
  128. FROM torrents_group
  129. WHERE ID = '$GroupID'");
  130. if (!$DB->has_results()) {
  131. $Err = "One of the entered URLs ($URL) does not correspond to a torrent group on the site.";
  132. break;
  133. }
  134. }
  135. if ($Err) {
  136. error($Err);
  137. }
  138. foreach ($GroupIDs as $GroupID) {
  139. add_torrent($CollageID, $GroupID);
  140. }
  141. }
  142. header('Location: collages.php?id='.$CollageID);