2020-12-26 01:42:23 +08:00
<? php
2021-01-13 19:32:26 +08:00
require_once ( "../include/bittorrent.php" );
2020-12-26 01:42:23 +08:00
dbconn ();
2022-04-18 23:46:00 +08:00
require_once ROOT_PATH . get_langfile_path ( "functions.php" );
require_once ( get_langfile_path ());
2022-04-18 19:07:35 +08:00
function denyDownload ()
{
permissiondenied ();
}
2021-06-04 02:18:34 +08:00
$torrentRep = new \App\Repositories\TorrentRepository ();
2021-06-10 12:00:51 +08:00
if ( ! empty ( $_REQUEST [ 'downhash' ])) {
2021-06-02 19:01:28 +08:00
$params = explode ( '|' , $_REQUEST [ 'downhash' ]);
if ( empty ( $params [ 0 ]) || empty ( $params [ 1 ])) {
die ( "invalid downhash, format error" );
2021-06-02 08:44:22 +08:00
}
2021-06-02 19:01:28 +08:00
$uid = $params [ 0 ];
$hash = $params [ 1 ];
$res = sql_query ( "SELECT * FROM users WHERE id=" . sqlesc ( $uid ) . " LIMIT 1" );
$user = mysql_fetch_array ( $res );
if ( ! $user )
die ( "invalid uid" );
elseif ( $user [ 'enabled' ] == 'no' || $user [ 'parked' ] == 'yes' )
die ( "account disabed or parked" );
$oldip = $user [ 'ip' ];
$user [ 'ip' ] = getip ();
$CURUSER = $user ;
$decrypted = $torrentRep -> decryptDownHash ( $hash , $user );
if ( empty ( $decrypted )) {
do_log ( "downhash invalid: " . nexus_json_encode ( $_REQUEST ));
die ( "invalid downhash, decrpyt fail" );
2021-06-02 08:44:22 +08:00
}
2021-06-02 19:01:28 +08:00
$id = $decrypted [ 0 ];
2021-06-10 12:00:51 +08:00
} elseif ( get_setting ( 'torrent.download_support_passkey' ) == 'yes' && ! empty ( $_REQUEST [ 'passkey' ]) && ! empty ( $_REQUEST [ 'id' ])) {
$res = sql_query ( "SELECT * FROM users WHERE passkey=" . sqlesc ( $_REQUEST [ 'passkey' ]) . " LIMIT 1" );
$user = mysql_fetch_array ( $res );
if ( ! $user )
die ( "invalid passkey" );
elseif ( $user [ 'enabled' ] == 'no' || $user [ 'parked' ] == 'yes' )
die ( "account disabed or parked" );
$oldip = $user [ 'ip' ];
$user [ 'ip' ] = getip ();
$CURUSER = $user ;
$id = $_REQUEST [ 'id' ];
} else {
2021-06-02 08:44:22 +08:00
$id = ( int ) $_GET [ "id" ];
if ( ! $id )
httperr ();
2020-12-26 01:42:23 +08:00
loggedinorreturn ();
parked ();
2021-01-06 00:56:13 +08:00
$letdown = intval ( $_GET [ 'letdown' ] ?? 0 );
2020-12-26 01:42:23 +08:00
if ( ! $letdown && $CURUSER [ 'showdlnotice' ] == 1 )
{
2022-04-06 21:32:57 +08:00
nexus_redirect ( getSchemeAndHttpHost () . "/downloadnotice.php?torrentid=" . $id . "&type=firsttime" );
2020-12-26 01:42:23 +08:00
}
elseif ( ! $letdown && $CURUSER [ 'showclienterror' ] == 'yes' )
{
2022-04-06 21:32:57 +08:00
nexus_redirect ( getSchemeAndHttpHost () . "/downloadnotice.php?torrentid=" . $id . "&type=client" );
2020-12-26 01:42:23 +08:00
}
elseif ( ! $letdown && $CURUSER [ 'leechwarn' ] == 'yes' )
{
2022-04-06 21:32:57 +08:00
nexus_redirect ( getSchemeAndHttpHost () . "/downloadnotice.php?torrentid=" . $id . "&type=ratio" );
2020-12-26 01:42:23 +08:00
}
}
//User may choose to download torrent from RSS. So log ip changes when downloading torrents.
if ( $iplog1 == "yes" ) {
if (( $oldip != $CURUSER [ "ip" ]) && $CURUSER [ "ip" ])
sql_query ( "INSERT INTO iplog (ip, userid, access) VALUES (" . sqlesc ( $CURUSER [ 'ip' ]) . ", " . $CURUSER [ 'id' ] . ", '" . $CURUSER [ 'last_access' ] . "')" );
}
//User may choose to download torrent from RSS. So update his last_access and ip when downloading torrents.
sql_query ( "UPDATE users SET last_access = " . sqlesc ( date ( "Y-m-d H:i:s" )) . ", ip = " . sqlesc ( $CURUSER [ 'ip' ]) . " WHERE id = " . sqlesc ( $CURUSER [ 'id' ]));
/*
@ini_set('zlib.output_compression', 'Off');
@set_time_limit(0);
if (@ini_get('output_handler') == 'ob_gzhandler' AND @ob_get_length() !== false)
{ // if output_handler = ob_gzhandler, turn it off and remove the header sent by PHP
@ob_end_clean();
header('Content-Encoding:');
}
*/
2022-03-14 15:43:10 +08:00
if ( $CURUSER [ 'downloadpos' ] == "no" ) {
2022-04-18 19:07:35 +08:00
denyDownload ();
2022-03-14 15:43:10 +08:00
}
2021-06-10 00:50:17 +08:00
$trackerSchemaAndHost = get_tracker_schema_and_host ();
$ssl_torrent = $trackerSchemaAndHost [ 'ssl_torrent' ];
$base_announce_url = $trackerSchemaAndHost [ 'base_announce_url' ];
2020-12-26 01:42:23 +08:00
2022-06-24 14:55:10 +08:00
$res = sql_query ( "SELECT torrents.name, torrents.filename, torrents.save_as, torrents.size, torrents.owner, torrents.banned, torrents.approval_status, categories.mode as search_box_id FROM torrents left join categories on torrents.category = categories.id WHERE torrents.id = " . sqlesc ( $id )) or sqlerr ( __FILE__ , __LINE__ );
2020-12-26 01:42:23 +08:00
$row = mysql_fetch_assoc ( $res );
2022-03-10 18:16:10 +08:00
if ( ! $row ) {
2022-03-14 15:43:10 +08:00
do_log ( "[TORRENT_NOT_EXISTS_IN_DATABASE] $id " , 'error' );
2022-03-10 18:16:10 +08:00
httperr ();
}
$fn = getFullDirectory ( " $torrent_dir / $id .torrent" );
2022-03-14 15:43:10 +08:00
if ( ! is_file ( $fn )) {
do_log ( "[TORRENT_NOT_EXISTS_IN_PATH] $fn " , 'error' );
httperr ();
2021-05-29 18:26:04 +08:00
}
2022-03-14 15:43:10 +08:00
if ( ! is_readable ( $fn )) {
2022-03-10 18:16:10 +08:00
do_log ( "[TORRENT_NOT_READABLE] $fn " , 'error' );
2021-05-29 18:26:04 +08:00
httperr ();
}
2022-03-14 15:43:10 +08:00
if ( filesize ( $fn ) == 0 ) {
do_log ( "[TORRENT_NOT_VALID_SIZE_ZERO] $fn " , 'error' );
httperr ();
}
2022-06-24 14:55:10 +08:00
$approvalNotAllowed = $row [ 'approval_status' ] != \App\Models\Torrent :: APPROVAL_STATUS_ALLOW && get_setting ( 'torrent.approval_status_none_visible' ) == 'no' ;
if ((( $row [ 'banned' ] == 'yes' || $approvalNotAllowed ) && get_user_class () < $seebanned_class ) || ! can_access_torrent ( $row )) {
2022-04-18 19:07:35 +08:00
denyDownload ();
2021-05-29 18:26:04 +08:00
}
2020-12-26 01:42:23 +08:00
sql_query ( "UPDATE torrents SET hits = hits + 1 WHERE id = " . sqlesc ( $id )) or sqlerr ( __FILE__ , __LINE__ );
2021-06-09 02:23:09 +08:00
//require_once "include/benc.php";
2020-12-26 01:42:23 +08:00
if ( strlen ( $CURUSER [ 'passkey' ]) != 32 ) {
$CURUSER [ 'passkey' ] = md5 ( $CURUSER [ 'username' ] . date ( "Y-m-d H:i:s" ) . $CURUSER [ 'passhash' ]);
2021-01-06 00:56:13 +08:00
sql_query ( "UPDATE users SET passkey=" . sqlesc ( $CURUSER [ 'passkey' ]) . " WHERE id=" . sqlesc ( $CURUSER [ 'id' ]));
2020-12-26 01:42:23 +08:00
}
2021-06-03 21:13:59 +08:00
$trackerReportAuthKey = $torrentRep -> getTrackerReportAuthKey ( $id , $CURUSER [ 'id' ], true );
2021-06-08 20:43:47 +08:00
$dict = \Rhilip\Bencode\Bencode :: load ( $fn );
$dict [ 'announce' ] = $ssl_torrent . $base_announce_url . "?authkey= $trackerReportAuthKey " ;
2021-06-03 21:13:59 +08:00
2022-01-28 02:50:30 +08:00
/**
* does not support multi-tracker
*
* @see https://github.com/xiaomlove/nexusphp/issues/26
*/
//if (count($announce_urls) > 1) {
// foreach ($announce_urls as $announce_url) {
// /** d['announce-list'] = [[ tracker1, tracker2, tracker3 ]] */
// $dict['announce-list'][0][] = $ssl_torrent . $announce_url . "?authkey=$trackerReportAuthKey";
// /** d['announce-list'] = [ [tracker1], [backup1], [backup2] ] */
// //$dict['announce-list'][] = [$ssl_torrent . $announce_url . "?passkey=" . $CURUSER['passkey']];
// }
//}
2021-06-09 02:23:09 +08:00
2021-06-08 20:43:47 +08:00
//$dict = bdec_file($fn, $max_torrent_size);
2021-02-28 02:47:13 +08:00
//$dict['value']['announce']['value'] = $ssl_torrent . $base_announce_url . "?passkey=$CURUSER[passkey]";
2021-06-08 20:43:47 +08:00
//$dict['value']['announce']['value'] = $ssl_torrent . $base_announce_url . "?authkey=$trackerReportAuthKey";
2021-06-05 15:50:23 +08:00
//$dict['value']['announce']['value'] = getSchemeAndHttpHost() . "/announce.php?authkey=$trackerReportAuthKey";
2021-06-08 20:43:47 +08:00
//$dict['value']['announce']['string'] = strlen($dict['value']['announce']['value']).":".$dict['value']['announce']['value'];
//$dict['value']['announce']['strlen'] = strlen($dict['value']['announce']['string']);
2020-12-26 01:42:23 +08:00
/*if ($announce_urls[1] != "") // add multi-tracker
{
$dict['value']['announce-list']['type'] = "list";
$dict['value']['announce-list']['value'][0]['type'] = "list";
$dict['value']['announce-list']['value'][0]['value'][0]["type"] = "string";
$dict['value']['announce-list']['value'][0]['value'][0]["value"] = $ssl_torrent . $announce_urls[0] . "?passkey=$CURUSER[passkey]";
$dict['value']['announce-list']['value'][0]['value'][0]["string"] = strlen($dict['value']['announce-list']['value'][0]['value'][0]["value"]).":".$dict['value']['announce-list']['value'][0]['value'][0]["value"];
$dict['value']['announce-list']['value'][0]['value'][0]["strlen"] = strlen($dict['value']['announce-list']['value'][0]['value'][0]["string"]);
$dict['value']['announce-list']['value'][0]['string'] = "l".$dict['value']['announce-list']['value'][0]['value'][0]["string"]."e";
$dict['value']['announce-list']['value'][0]['strlen'] = strlen($dict['value']['announce-list']['value'][0]['string']);
$dict['value']['announce-list']['value'][1]['type'] = "list";
$dict['value']['announce-list']['value'][1]['value'][0]["type"] = "string";
$dict['value']['announce-list']['value'][1]['value'][0]["value"] = $ssl_torrent . $announce_urls[1] . "?passkey=$CURUSER[passkey]";
$dict['value']['announce-list']['value'][1]['value'][0]["string"] = strlen($dict['value']['announce-list']['value'][0]['value'][0]["value"]).":".$dict['value']['announce-list']['value'][0]['value'][0]["value"];
$dict['value']['announce-list']['value'][1]['value'][0]["strlen"] = strlen($dict['value']['announce-list']['value'][0]['value'][0]["string"]);
$dict['value']['announce-list']['value'][1]['string'] = "l".$dict['value']['announce-list']['value'][0]['value'][0]["string"]."e";
$dict['value']['announce-list']['value'][1]['strlen'] = strlen($dict['value']['announce-list']['value'][0]['string']);
$dict['value']['announce-list']['string'] = "l".$dict['value']['announce-list']['value'][0]['string'].$dict['value']['announce-list']['value'][1]['string']."e";
$dict['value']['announce-list']['strlen'] = strlen($dict['value']['announce-list']['string']);
}*/
/*
header ("Expires: Tue, 1 Jan 1980 00:00:00 GMT");
header ("Last-Modified: ".date("D, d M Y H:i:s"));
header ("Cache-Control: no-store, no-cache, must-revalidate");
header ("Cache-Control: post-check=0, pre-check=0", false);
header ("Pragma: no-cache");
header ("X-Powered-By: ".VERSION." (c) ".date("Y")." ".$SITENAME."");
header ("Accept-Ranges: bytes");
header ("Connection: close");
header ("Content-Transfer-Encoding: binary");
*/
header ( "Content-Type: application/x-bittorrent" );
if ( str_replace ( "Gecko" , "" , $_SERVER [ 'HTTP_USER_AGENT' ]) != $_SERVER [ 'HTTP_USER_AGENT' ])
{
header ( "Content-Disposition: attachment; filename= \" $torrentnameprefix ." . $row [ "save_as" ] . ".torrent \" ; charset=utf-8" );
}
else if ( str_replace ( "Firefox" , "" , $_SERVER [ 'HTTP_USER_AGENT' ]) != $_SERVER [ 'HTTP_USER_AGENT' ] )
{
header ( "Content-Disposition: attachment; filename= \" $torrentnameprefix ." . $row [ "save_as" ] . ".torrent \" ; charset=utf-8" );
}
else if ( str_replace ( "Opera" , "" , $_SERVER [ 'HTTP_USER_AGENT' ]) != $_SERVER [ 'HTTP_USER_AGENT' ] )
{
header ( "Content-Disposition: attachment; filename= \" $torrentnameprefix ." . $row [ "save_as" ] . ".torrent \" ; charset=utf-8" );
}
else if ( str_replace ( "IE" , "" , $_SERVER [ 'HTTP_USER_AGENT' ]) != $_SERVER [ 'HTTP_USER_AGENT' ] )
{
header ( "Content-Disposition: attachment; filename=" . str_replace ( "+" , "%20" , rawurlencode ( " $torrentnameprefix ." . $row [ "save_as" ] . ".torrent" )));
}
else
{
header ( "Content-Disposition: attachment; filename=" . str_replace ( "+" , "%20" , rawurlencode ( " $torrentnameprefix ." . $row [ "save_as" ] . ".torrent" )));
}
//header ("Content-Disposition: attachment; filename=".$row["filename"]."");
//ob_implicit_flush(true);
2021-06-08 20:43:47 +08:00
//print(benc($dict));
echo \Rhilip\Bencode\Bencode :: encode ( $dict );
2020-12-26 01:42:23 +08:00
?>