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.

download.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. #declare(strict_types=1);
  3. if (!isset($_REQUEST['authkey']) || !isset($_REQUEST['torrent_pass'])) {
  4. enforce_login();
  5. $TorrentPass = $LoggedUser['torrent_pass'];
  6. $UserID = $LoggedUser['ID'];
  7. $AuthKey = $LoggedUser['AuthKey'];
  8. } else {
  9. if (strpos($_REQUEST['torrent_pass'], '_') !== false) {
  10. error(404);
  11. }
  12. $UserInfo = $Cache->get_value('user_'.$_REQUEST['torrent_pass']);
  13. if (!is_array($UserInfo)) {
  14. $DB->query("
  15. SELECT ID, la.UserID
  16. FROM users_main AS m
  17. INNER JOIN users_info AS i ON i.UserID = m.ID
  18. LEFT JOIN locked_accounts AS la ON la.UserID = m.ID
  19. WHERE m.torrent_pass = '".db_string($_REQUEST['torrent_pass'])."'
  20. AND m.Enabled = '1'");
  21. $UserInfo = $DB->next_record();
  22. $Cache->cache_value('user_'.$_REQUEST['torrent_pass'], $UserInfo, 3600);
  23. }
  24. $UserInfo = array($UserInfo);
  25. list($UserID, $Locked) = array_shift($UserInfo);
  26. if (!$UserID) {
  27. error(0);
  28. }
  29. $TorrentPass = $_REQUEST['torrent_pass'];
  30. $AuthKey = $_REQUEST['authkey'];
  31. if ($Locked === $UserID) {
  32. header('HTTP/1.1 403 Forbidden');
  33. error();
  34. }
  35. }
  36. $TorrentID = $_REQUEST['id'];
  37. if (!is_number($TorrentID)) {
  38. error(0);
  39. }
  40. /*
  41. uTorrent Remote and various scripts redownload .torrent files periodically.
  42. To prevent this retardation from blowing bandwidth etc., let's block it
  43. if the .torrent file has been downloaded four times before
  44. */
  45. $ScriptUAs = array('BTWebClient*', 'Python-urllib*', 'python-requests*');
  46. if (Misc::in_array_partial($_SERVER['HTTP_USER_AGENT'], $ScriptUAs)) {
  47. $DB->query("
  48. SELECT 1
  49. FROM users_downloads
  50. WHERE UserID = $UserID
  51. AND TorrentID = $TorrentID
  52. LIMIT 4");
  53. if ($DB->record_count() === 4) {
  54. error('You have already downloaded this torrent file four times. If you need to download it again, please do so from your browser.', true);
  55. error();
  56. }
  57. }
  58. $Info = $Cache->get_value('torrent_download_'.$TorrentID);
  59. if (!is_array($Info) || !array_key_exists('PlainArtists', $Info) || empty($Info[10])) {
  60. $DB->query("
  61. SELECT
  62. t.Media,
  63. t.Version,
  64. t.Codec,
  65. tg.Year,
  66. tg.ID AS GroupID,
  67. COALESCE(NULLIF(tg.Name,''), NULLIF(tg.Title2,''), tg.NameJP) AS Name,
  68. tg.WikiImage,
  69. tg.CategoryID,
  70. t.Size,
  71. t.FreeTorrent,
  72. HEX(t.info_hash)
  73. FROM torrents AS t
  74. INNER JOIN torrents_group AS tg ON tg.ID = t.GroupID
  75. WHERE t.ID = '".db_string($TorrentID)."'");
  76. if (!$DB->has_results()) {
  77. error(404);
  78. }
  79. $Info = array($DB->next_record(MYSQLI_NUM, array(4, 5, 6, 10)));
  80. $Artists = Artists::get_artist($Info[0][4], false);
  81. $Info['Artists'] = Artists::display_artists($Artists, false, true);
  82. $Info['PlainArtists'] = Artists::display_artists($Artists, false, true, false);
  83. $Cache->cache_value("torrent_download_$TorrentID", $Info, 0);
  84. }
  85. if (!is_array($Info[0])) {
  86. error(404);
  87. }
  88. list($Media, $Format, $Encoding, $Year, $GroupID, $Name, $Image, $CategoryID, $Size, $FreeTorrent, $InfoHash) = array_shift($Info); // used for generating the filename
  89. $Artists = $Info['Artists'];
  90. // If he's trying use a token on this, we need to make sure he has one,
  91. // deduct it, add this to the FLs table, and update his cache key.
  92. if ($_REQUEST['usetoken'] && $FreeTorrent === '0') {
  93. if (isset($LoggedUser)) {
  94. $FLTokens = $LoggedUser['FLTokens'];
  95. if ($LoggedUser['CanLeech'] !== '1') {
  96. error('You cannot use tokens while leech disabled.');
  97. }
  98. } else {
  99. $UInfo = Users::user_heavy_info($UserID);
  100. if ($UInfo['CanLeech'] !== '1') {
  101. error('You may not use tokens while leech disabled.');
  102. }
  103. $FLTokens = $UInfo['FLTokens'];
  104. }
  105. // First make sure this isn't already FL, and if it is, do nothing
  106. if (!Torrents::has_token($TorrentID)) {
  107. if ($FLTokens <= 0) {
  108. error('You do not have any freeleech tokens left. Please use the regular DL link.');
  109. }
  110. if ($Size >= 10737418240) {
  111. error('This torrent is too large. Please use the regular DL link.');
  112. }
  113. // Let the tracker know about this
  114. if (!Tracker::update_tracker('add_token', ['info_hash' => substr('%'.chunk_split($InfoHash, 2, '%'), 0, -1), 'userid' => $UserID])) {
  115. error('Sorry! An error occurred while trying to register your token. Most often, this is due to the tracker being down or under heavy load. Please try again later.');
  116. }
  117. if (!Torrents::has_token($TorrentID)) {
  118. $DB->query("
  119. INSERT INTO users_freeleeches (UserID, TorrentID, Time)
  120. VALUES ($UserID, $TorrentID, NOW())
  121. ON DUPLICATE KEY UPDATE
  122. Time = VALUES(Time),
  123. Expired = FALSE,
  124. Uses = Uses + 1");
  125. $DB->query("
  126. UPDATE users_main
  127. SET FLTokens = FLTokens - 1
  128. WHERE ID = $UserID");
  129. // Fix for downloadthemall messing with the cached token count
  130. $UInfo = Users::user_heavy_info($UserID);
  131. $FLTokens = $UInfo['FLTokens'];
  132. $Cache->begin_transaction("user_info_heavy_$UserID");
  133. $Cache->update_row(false, array('FLTokens' => ($FLTokens - 1)));
  134. $Cache->commit_transaction(0);
  135. $Cache->delete_value("users_tokens_$UserID");
  136. }
  137. }
  138. }
  139. // Stupid Recent Snatches On User Page
  140. if ($Image !== '') {
  141. $RecentSnatches = $Cache->get_value("recent_snatches_$UserID");
  142. if (!empty($RecentSnatches)) {
  143. $Snatch = array(
  144. 'ID' => $GroupID,
  145. 'Name' => $Name,
  146. 'Artist' => $Artists,
  147. 'WikiImage' => $Image);
  148. if (!in_array($Snatch, $RecentSnatches)) {
  149. if (count($RecentSnatches) === 5) {
  150. array_pop($RecentSnatches);
  151. }
  152. array_unshift($RecentSnatches, $Snatch);
  153. } elseif (!is_array($RecentSnatches)) {
  154. $RecentSnatches = array($Snatch);
  155. }
  156. $Cache->cache_value("recent_snatches_$UserID", $RecentSnatches, 0);
  157. }
  158. }
  159. $DB->query("
  160. INSERT IGNORE INTO users_downloads (UserID, TorrentID, Time)
  161. VALUES ('$UserID', '$TorrentID', NOW())");
  162. Torrents::set_snatch_update_time($UserID, Torrents::SNATCHED_UPDATE_AFTERDL);
  163. $Contents = file_get_contents(TORRENT_STORE.$TorrentID.'.torrent');
  164. $FileName = TorrentsDL::construct_file_name($TorrentID);
  165. header('Content-Type: application/x-bittorrent; charset=utf-8');
  166. header('Content-disposition: attachment; filename="'.$FileName.'"');
  167. function add_passkey($ann)
  168. {
  169. global $TorrentPass;
  170. return (is_array($ann)) ? array_map("add_passkey", $ann) : $ann."/".$TorrentPass."/announce";
  171. }
  172. $UserAnnounceURL = ANNOUNCE_URLS[0][0]."/".$TorrentPass."/announce";
  173. # todo: Probably not working, but no need yet
  174. $UserAnnounceList = (sizeof(ANNOUNCE_URLS[0]) === 1 && sizeof(ANNOUNCE_URLS[0][0]) === 1) ? [] : array_map('add_passkey', ANNOUNCE_URLS[0]);
  175. # Tracker tiers (pending)
  176. #$UserAnnounceList = (sizeof(ANNOUNCE_URLS) === 1 && sizeof(ANNOUNCE_URLS[0]) === 1) ? [] : array(array_map('add_passkey', ANNOUNCE_URLS[0]), ANNOUNCE_URLS[1]);
  177. # Original Oppaitime
  178. #$UserAnnounceList = (sizeof(ANNOUNCE_URLS) == 1 && sizeof(ANNOUNCE_URLS[0]) == 1) ? [] : array_map("add_passkey", ANNOUNCE_URLS);
  179. echo TorrentsDL::get_file($Contents, $UserAnnounceURL, $UserAnnounceList);
  180. define('SKIP_NO_CACHE_HEADERS', 1);