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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. die();
  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. die();
  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.AudioFormat,
  63. t.Codec,
  64. tg.Year,
  65. tg.ID AS GroupID,
  66. COALESCE(NULLIF(tg.Name,''), NULLIF(tg.NameRJ,''), tg.NameJP) AS Name,
  67. tg.WikiImage,
  68. tg.CategoryID,
  69. tm.Resource,
  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. LEFT JOIN torrents_mirrors AS tm ON tm.ID = t.GroupID
  76. WHERE t.ID = '".db_string($TorrentID)."'");
  77. if (!$DB->has_results()) {
  78. error(404);
  79. }
  80. $Info = array($DB->next_record(MYSQLI_NUM, array(4, 5, 6, 10)));
  81. $Artists = Artists::get_artist($Info[0][4], false);
  82. $Info['Artists'] = Artists::display_artists($Artists, false, true);
  83. $Info['PlainArtists'] = Artists::display_artists($Artists, false, true, false);
  84. $Cache->cache_value("torrent_download_$TorrentID", $Info, 0);
  85. }
  86. if (!is_array($Info[0])) {
  87. error(404);
  88. }
  89. list($Media, $Format, $Encoding, $Year, $GroupID, $Name, $Image, $CategoryID, $Resources, $Size, $FreeTorrent, $InfoHash) = array_shift($Info); // Used for generating the filename
  90. $Artists = $Info['Artists'];
  91. // If he's trying use a token on this, we need to make sure he has one,
  92. // deduct it, add this to the FLs table, and update his cache key.
  93. // todo: Make sure strict equality works
  94. if ($_REQUEST['usetoken'] && $FreeTorrent == '0') {
  95. if (isset($LoggedUser)) {
  96. $FLTokens = $LoggedUser['FLTokens'];
  97. if ($LoggedUser['CanLeech'] != '1') {
  98. error('You cannot use tokens while leech disabled.');
  99. }
  100. } else {
  101. $UInfo = Users::user_heavy_info($UserID);
  102. if ($UInfo['CanLeech'] != '1') {
  103. error('You may not use tokens while leech disabled.');
  104. }
  105. $FLTokens = $UInfo['FLTokens'];
  106. }
  107. // First make sure this isn't already FL, and if it is, do nothing
  108. if (!Torrents::has_token($TorrentID)) {
  109. if ($FLTokens <= 0) {
  110. error('You do not have any freeleech tokens left. Please use the regular DL link.');
  111. }
  112. if ($Size >= 10737418240) {
  113. error('This torrent is too large. Please use the regular DL link.');
  114. }
  115. // Let the tracker know about this
  116. if (!Tracker::update_tracker('add_token', ['info_hash' => substr('%'.chunk_split($InfoHash, 2, '%'), 0, -1), 'userid' => $UserID])) {
  117. 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.');
  118. }
  119. if (!Torrents::has_token($TorrentID)) {
  120. $DB->query("
  121. INSERT INTO users_freeleeches (UserID, TorrentID, Time)
  122. VALUES ($UserID, $TorrentID, NOW())
  123. ON DUPLICATE KEY UPDATE
  124. Time = VALUES(Time),
  125. Expired = FALSE,
  126. Uses = Uses + 1");
  127. $DB->query("
  128. UPDATE users_main
  129. SET FLTokens = FLTokens - 1
  130. WHERE ID = $UserID");
  131. // Fix for downloadthemall messing with the cached token count
  132. $UInfo = Users::user_heavy_info($UserID);
  133. $FLTokens = $UInfo['FLTokens'];
  134. $Cache->begin_transaction("user_info_heavy_$UserID");
  135. $Cache->update_row(false, array('FLTokens' => ($FLTokens - 1)));
  136. $Cache->commit_transaction(0);
  137. $Cache->delete_value("users_tokens_$UserID");
  138. }
  139. }
  140. }
  141. // Stupid Recent Snatches on User Page
  142. if ($Image != '') {
  143. $RecentSnatches = $Cache->get_value("recent_snatches_$UserID");
  144. if (!empty($RecentSnatches)) {
  145. $Snatch = array(
  146. 'ID' => $GroupID,
  147. 'Name' => $Name,
  148. 'Artist' => $Artists,
  149. 'WikiImage' => $Image);
  150. if (!in_array($Snatch, $RecentSnatches)) {
  151. if (count($RecentSnatches) === 5) {
  152. array_pop($RecentSnatches);
  153. }
  154. array_unshift($RecentSnatches, $Snatch);
  155. } elseif (!is_array($RecentSnatches)) {
  156. $RecentSnatches = array($Snatch);
  157. }
  158. $Cache->cache_value("recent_snatches_$UserID", $RecentSnatches, 0);
  159. }
  160. }
  161. $DB->query("
  162. INSERT IGNORE INTO users_downloads (UserID, TorrentID, Time)
  163. VALUES ('$UserID', '$TorrentID', NOW())");
  164. Torrents::set_snatch_update_time($UserID, Torrents::SNATCHED_UPDATE_AFTERDL);
  165. $Contents = file_get_contents(TORRENT_STORE.$TorrentID.'.torrent');
  166. $FileName = TorrentsDL::construct_file_name($Info['PlainArtists'], $Name, $Year, $Media, $Format, $Encoding, $TorrentID);
  167. header('Content-Type: application/x-bittorrent; charset=utf-8');
  168. header('Content-disposition: attachment; filename="'.$FileName.'"');
  169. function add_passkey($Ann)
  170. {
  171. global $TorrentPass;
  172. return (is_array($Ann)) ? array_map('add_passkey', $Ann) : $Ann.'/'.$TorrentPass.'/announce';
  173. }
  174. $UserAnnounceURL = ANNOUNCE_URLS[0][0].'/'.$TorrentPass.'/announce';
  175. $UserAnnounceList = (sizeof(ANNOUNCE_URLS) === 1 && sizeof(ANNOUNCE_URLS[0]) === 1) ? [] : array(array_map('add_passkey', ANNOUNCE_URLS[0]), ANNOUNCE_URLS[1]);
  176. #$UserAnnounceList = (sizeof(ANNOUNCE_URLS) === 1 && sizeof(ANNOUNCE_URLS[0]) === 1) ? [] : array_map('add_passkey', ANNOUNCE_URLS);
  177. echo TorrentsDL::get_file($Contents, $UserAnnounceURL, $UserAnnounceList, $Resources);
  178. define('SKIP_NO_CACHE_HEADERS', 1);