Gazelle/sections/torrents/download.php

189 lines
6.0 KiB
PHP
Raw Normal View History

2011-03-28 14:21:28 +00:00
<?
if (!isset($_REQUEST['authkey']) || !isset($_REQUEST['torrent_pass'])) {
enforce_login();
$TorrentPass = $LoggedUser['torrent_pass'];
$DownloadAlt = $LoggedUser['DownloadAlt'];
2013-02-07 08:00:47 +00:00
$UserID = $LoggedUser['ID'];
$AuthKey = $LoggedUser['AuthKey'];
2011-03-28 14:21:28 +00:00
} else {
$UserInfo = $Cache->get_value('user_'.$_REQUEST['torrent_pass']);
2013-04-13 08:00:19 +00:00
if (!is_array($UserInfo)) {
2013-07-04 08:00:56 +00:00
$DB->query("
SELECT ID, DownloadAlt
2011-10-08 08:00:14 +00:00
FROM users_main AS m
2013-07-04 08:00:56 +00:00
INNER JOIN users_info AS i ON i.UserID = m.ID
WHERE m.torrent_pass = '".db_string($_REQUEST['torrent_pass'])."'
AND m.Enabled = '1'");
2011-03-28 14:21:28 +00:00
$UserInfo = $DB->next_record();
$Cache->cache_value('user_'.$_REQUEST['torrent_pass'], $UserInfo, 3600);
}
$UserInfo = array($UserInfo);
2013-07-04 08:00:56 +00:00
list($UserID, $DownloadAlt) = array_shift($UserInfo);
2013-03-10 08:00:41 +00:00
if (!$UserID) {
error(0);
}
2011-03-28 14:21:28 +00:00
$TorrentPass = $_REQUEST['torrent_pass'];
2012-06-20 08:00:14 +00:00
$AuthKey = $_REQUEST['authkey'];
2011-03-28 14:21:28 +00:00
}
$TorrentID = $_REQUEST['id'];
2012-06-19 08:00:14 +00:00
2013-03-10 08:00:41 +00:00
if (!is_number($TorrentID)) {
error(0);
}
2011-03-28 14:21:28 +00:00
2013-10-07 08:01:03 +00:00
/* uTorrent Remote and various scripts redownload .torrent files periodically.
2013-07-04 08:00:56 +00:00
To prevent this retardation from blowing bandwidth etc., let's block it
2013-10-07 08:01:03 +00:00
if the .torrent file has been downloaded four times before */
2015-01-28 08:00:26 +00:00
$ScriptUAs = array('BTWebClient*', 'Python-urllib*', 'python-requests*', 'uTorrent*');
2013-10-18 08:00:55 +00:00
if (Misc::in_array_partial($_SERVER['HTTP_USER_AGENT'], $ScriptUAs)) {
2013-07-04 08:00:56 +00:00
$DB->query("
SELECT 1
FROM users_downloads
WHERE UserID = $UserID
AND TorrentID = $TorrentID
2013-10-07 08:01:03 +00:00
LIMIT 4");
if ($DB->record_count() === 4) {
error('You have already downloaded this torrent file four times. If you need to download it again, please do so from your browser.', true);
2012-10-24 08:00:16 +00:00
die();
}
}
2011-03-28 14:21:28 +00:00
$Info = $Cache->get_value('torrent_download_'.$TorrentID);
2013-04-13 08:00:19 +00:00
if (!is_array($Info) || !array_key_exists('PlainArtists', $Info) || empty($Info[10])) {
2013-07-04 08:00:56 +00:00
$DB->query("
SELECT
t.Media,
t.Format,
t.Encoding,
IF(t.RemasterYear = 0, tg.Year, t.RemasterYear),
tg.ID AS GroupID,
tg.Name,
tg.WikiImage,
tg.CategoryID,
t.Size,
t.FreeTorrent,
t.info_hash
2011-03-28 14:21:28 +00:00
FROM torrents AS t
2013-07-04 08:00:56 +00:00
INNER JOIN torrents_group AS tg ON tg.ID = t.GroupID
WHERE t.ID = '".db_string($TorrentID)."'");
2013-07-10 00:08:53 +00:00
if (!$DB->has_results()) {
2012-05-18 13:35:17 +00:00
error(404);
2011-03-28 14:21:28 +00:00
}
2013-07-04 08:00:56 +00:00
$Info = array($DB->next_record(MYSQLI_NUM, array(4, 5, 6, 10)));
$Artists = Artists::get_artist($Info[0][4], false);
2012-10-11 08:00:15 +00:00
$Info['Artists'] = Artists::display_artists($Artists, false, true);
$Info['PlainArtists'] = Artists::display_artists($Artists, false, true, false);
2013-07-04 08:00:56 +00:00
$Cache->cache_value("torrent_download_$TorrentID", $Info, 0);
2011-03-28 14:21:28 +00:00
}
2013-04-13 08:00:19 +00:00
if (!is_array($Info[0])) {
2011-03-28 14:21:28 +00:00
error(404);
}
2013-07-04 08:00:56 +00:00
list($Media, $Format, $Encoding, $Year, $GroupID, $Name, $Image, $CategoryID, $Size, $FreeTorrent, $InfoHash) = array_shift($Info); // used for generating the filename
2011-03-28 14:21:28 +00:00
$Artists = $Info['Artists'];
2011-10-08 08:00:14 +00:00
// If he's trying use a token on this, we need to make sure he has one,
// deduct it, add this to the FLs table, and update his cache key.
if ($_REQUEST['usetoken'] && $FreeTorrent == '0') {
if (isset($LoggedUser)) {
$FLTokens = $LoggedUser['FLTokens'];
if ($LoggedUser['CanLeech'] != '1') {
error('You cannot use tokens while leech disabled.');
}
}
else {
2012-10-11 08:00:15 +00:00
$UInfo = Users::user_heavy_info($UserID);
if ($UInfo['CanLeech'] != '1') {
error('You may not use tokens while leech disabled.');
}
$FLTokens = $UInfo['FLTokens'];
}
2013-02-22 08:00:24 +00:00
2011-10-08 08:00:14 +00:00
// First make sure this isn't already FL, and if it is, do nothing
2013-02-22 08:00:24 +00:00
2012-10-16 08:00:18 +00:00
if (!Torrents::has_token($TorrentID)) {
if ($FLTokens <= 0) {
2013-07-04 08:00:56 +00:00
error('You do not have any freeleech tokens left. Please use the regular DL link.');
2011-10-08 08:00:14 +00:00
}
if ($Size >= 1073741824) {
2013-07-04 08:00:56 +00:00
error('This torrent is too large. Please use the regular DL link.');
2011-10-08 08:00:14 +00:00
}
2013-02-22 08:00:24 +00:00
// Let the tracker know about this
2012-10-11 08:00:15 +00:00
if (!Tracker::update_tracker('add_token', array('info_hash' => rawurlencode($InfoHash), 'userid' => $UserID))) {
2013-07-04 08:00:56 +00:00
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.');
}
2013-02-22 08:00:24 +00:00
2012-10-16 08:00:18 +00:00
if (!Torrents::has_token($TorrentID)) {
2013-07-04 08:00:56 +00:00
$DB->query("
INSERT INTO users_freeleeches (UserID, TorrentID, Time)
VALUES ($UserID, $TorrentID, NOW())
ON DUPLICATE KEY UPDATE
Time = VALUES(Time),
Expired = FALSE,
Uses = Uses + 1");
$DB->query("
UPDATE users_main
SET FLTokens = FLTokens - 1
WHERE ID = $UserID");
2013-02-22 08:00:24 +00:00
2011-11-02 08:00:14 +00:00
// Fix for downloadthemall messing with the cached token count
2012-10-11 08:00:15 +00:00
$UInfo = Users::user_heavy_info($UserID);
2011-11-02 08:00:14 +00:00
$FLTokens = $UInfo['FLTokens'];
2013-02-22 08:00:24 +00:00
2013-07-04 08:00:56 +00:00
$Cache->begin_transaction("user_info_heavy_$UserID");
$Cache->update_row(false, array('FLTokens' => ($FLTokens - 1)));
2011-10-29 08:00:15 +00:00
$Cache->commit_transaction(0);
2013-02-22 08:00:24 +00:00
2013-07-04 08:00:56 +00:00
$Cache->delete_value("users_tokens_$UserID");
2011-10-29 08:00:15 +00:00
}
2011-10-08 08:00:14 +00:00
}
}
2011-03-28 14:21:28 +00:00
//Stupid Recent Snatches On User Page
2013-05-08 08:00:26 +00:00
if ($CategoryID == '1' && $Image != '') {
2013-07-04 08:00:56 +00:00
$RecentSnatches = $Cache->get_value("recent_snatches_$UserID");
2013-04-13 08:00:19 +00:00
if (!empty($RecentSnatches)) {
2013-07-04 08:00:56 +00:00
$Snatch = array(
'ID' => $GroupID,
'Name' => $Name,
'Artist' => $Artists,
'WikiImage' => $Image);
2013-04-13 08:00:19 +00:00
if (!in_array($Snatch, $RecentSnatches)) {
2013-07-17 08:00:52 +00:00
if (count($RecentSnatches) === 5) {
2011-03-28 14:21:28 +00:00
array_pop($RecentSnatches);
}
array_unshift($RecentSnatches, $Snatch);
2013-04-13 08:00:19 +00:00
} elseif (!is_array($RecentSnatches)) {
2011-03-28 14:21:28 +00:00
$RecentSnatches = array($Snatch);
}
2013-07-04 08:00:56 +00:00
$Cache->cache_value("recent_snatches_$UserID", $RecentSnatches, 0);
2011-03-28 14:21:28 +00:00
}
}
2013-05-08 08:00:26 +00:00
$DB->query("
INSERT IGNORE INTO users_downloads (UserID, TorrentID, Time)
VALUES ('$UserID', '$TorrentID', '".sqltime()."')");
2011-03-28 14:21:28 +00:00
2013-07-04 08:00:56 +00:00
$DB->query("
SELECT File
FROM torrents_files
WHERE TorrentID = '$TorrentID'");
2011-03-28 14:21:28 +00:00
2014-05-30 08:02:33 +00:00
Torrents::set_snatch_update_time($UserID, Torrents::SNATCHED_UPDATE_AFTERDL);
2013-02-21 08:00:30 +00:00
list($Contents) = $DB->next_record(MYSQLI_NUM, false);
$FileName = TorrentsDL::construct_file_name($Info['PlainArtists'], $Name, $Year, $Media, $Format, $Encoding, false, $DownloadAlt);
2011-03-28 14:21:28 +00:00
2013-02-25 21:16:55 +00:00
if ($DownloadAlt) {
2011-03-28 14:21:28 +00:00
header('Content-Type: text/plain; charset=utf-8');
2013-04-13 08:00:19 +00:00
} elseif (!$DownloadAlt || $Failed) {
2011-03-28 14:21:28 +00:00
header('Content-Type: application/x-bittorrent; charset=utf-8');
}
header('Content-disposition: attachment; filename="'.$FileName.'"');
2013-02-25 21:16:55 +00:00
echo TorrentsDL::get_file($Contents, ANNOUNCE_URL."/$TorrentPass/announce");
2012-08-25 08:00:15 +00:00
2013-08-22 08:00:54 +00:00
define('SKIP_NO_CACHE_HEADERS', 1);