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.5KB

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