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

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