Oppaitime'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 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?
  2. if (!isset($_REQUEST['authkey']) || !isset($_REQUEST['torrent_pass'])) {
  3. enforce_login();
  4. $TorrentPass = $LoggedUser['torrent_pass'];
  5. $DownloadAlt = $LoggedUser['DownloadAlt'];
  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, DownloadAlt, 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, $DownloadAlt, $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. die();
  34. }
  35. }
  36. $TorrentID = $_REQUEST['id'];
  37. if (!is_number($TorrentID)) {
  38. error(0);
  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. $ScriptUAs = array('BTWebClient*', 'Python-urllib*', 'python-requests*');
  44. if (Misc::in_array_partial($_SERVER['HTTP_USER_AGENT'], $ScriptUAs)) {
  45. $DB->query("
  46. SELECT 1
  47. FROM users_downloads
  48. WHERE UserID = $UserID
  49. AND TorrentID = $TorrentID
  50. LIMIT 4");
  51. if ($DB->record_count() === 4) {
  52. error('You have already downloaded this torrent file four times. If you need to download it again, please do so from your browser.', true);
  53. die();
  54. }
  55. }
  56. $Info = $Cache->get_value('torrent_download_'.$TorrentID);
  57. if (!is_array($Info) || !array_key_exists('PlainArtists', $Info) || empty($Info[10])) {
  58. $DB->query("
  59. SELECT
  60. t.Media,
  61. t.AudioFormat,
  62. t.Codec,
  63. tg.Year,
  64. tg.ID AS GroupID,
  65. tg.Name,
  66. tg.WikiImage,
  67. tg.CategoryID,
  68. t.Size,
  69. t.FreeTorrent,
  70. t.info_hash
  71. FROM torrents AS t
  72. INNER JOIN torrents_group AS tg ON tg.ID = t.GroupID
  73. WHERE t.ID = '".db_string($TorrentID)."'");
  74. if (!$DB->has_results()) {
  75. error(404);
  76. }
  77. $Info = array($DB->next_record(MYSQLI_NUM, array(4, 5, 6, 10)));
  78. $Artists = Artists::get_artist($Info[0][4], false);
  79. $Info['Artists'] = Artists::display_artists($Artists, false, true);
  80. $Info['PlainArtists'] = Artists::display_artists($Artists, false, true, false);
  81. $Cache->cache_value("torrent_download_$TorrentID", $Info, 0);
  82. }
  83. if (!is_array($Info[0])) {
  84. error(404);
  85. }
  86. list($Media, $Format, $Encoding, $Year, $GroupID, $Name, $Image, $CategoryID, $Size, $FreeTorrent, $InfoHash) = array_shift($Info); // used for generating the filename
  87. $Artists = $Info['Artists'];
  88. // If he's trying use a token on this, we need to make sure he has one,
  89. // deduct it, add this to the FLs table, and update his cache key.
  90. if ($_REQUEST['usetoken'] && $FreeTorrent == '0') {
  91. if (isset($LoggedUser)) {
  92. $FLTokens = $LoggedUser['FLTokens'];
  93. if ($LoggedUser['CanLeech'] != '1') {
  94. error('You cannot use tokens while leech disabled.');
  95. }
  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', array('info_hash' => rawurlencode($InfoHash), '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', '".sqltime()."')");
  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($Info['PlainArtists'], $Name, $Year, $Media, $Format, $Encoding, $TorrentID, $DownloadAlt);
  164. if ($DownloadAlt) {
  165. header('Content-Type: text/plain; charset=utf-8');
  166. } elseif (!$DownloadAlt || $Failed) {
  167. header('Content-Type: application/x-bittorrent; charset=utf-8');
  168. }
  169. header('Content-disposition: attachment; filename="'.$FileName.'"');
  170. function add_passkey($ann) {
  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);
  176. echo TorrentsDL::get_file($Contents, $UserAnnounceURL, $UserAnnounceList);
  177. define('SKIP_NO_CACHE_HEADERS', 1);