<?php
if(!defined('R_P')) exit('Access Denied');
$id = intval( $_GET['id'] );
$pid = intval( $_GET['pid'] );
$sid = intval( $_GET['sid'] );
$hash =  base64_decode(urldecode(trim($_GET['hash']))) ;
$md5 =  trim($_GET['md5']) ;
if ($pid < 1 || empty($hash) || empty($md5)) qbase::purviewCheckDie( 'error request' );

if(false === ($server_config = cache::get('sys_server'))){
	$server_config = qbase::get_object('server')->cache();
}

if($sid > 0 && (empty($server_config[$sid]) || $server_config[$sid]['server_islock'] == 1)) qbase::purviewCheckDie("没有找到相关数据,你可以向管理员报告错误");
$_output = qbase::get_object('content', $channelid, $id)->get();
if(empty($_output)) qbase::purviewCheckDie("没有找到相关数据,你可以向管理员报告错误");

$cid = $_output['cid'];
$_category_info = getCidInfo( $channelid, $cid);
if(empty($_category_info)) qbase::purviewCheckDie("栏目数据出现错误，请联系管理员");

$user_info = qbase::get_object('login')->checkSession();
parse_str($hash, $hashData );

if(strtoupper(substr(md5($hash . $_SET['passport_key']), 0, 15)) != $md5 || $hashData['p'] != qbase::get_ip() || $user_info['uid'] != $hashData['u'] || $_SERVER['REQUEST_TIME'] - $hashData['t'] >  1800){
	$wait = 3;
	$iif = !( $_category_info['pur_browse'] || $_category_info['html'] || $_output['pay']);
	$_output['url']  = qbase::GetHtmlDirType($_output['channelid'], $_output['id'], $_category_info['ciddir'], $_output['dateline'],$_output['filename'],$_output['ver'],$iif);
	qbase::showmsg("盗链是不好的行为，我们要制止", "default", $_output['url']);
}

$urlss = qdb::QueryFirst("SELECT urls,serverid FROM ".TABLEPRE."downloads_{$channelid} WHERE pid = '{$pid}'" );
if(empty($urlss)) qbase::purviewCheckDie( "此$channeltitle数据已经不存在，请联系管理员");

qbase::get_object('view', $channelid, $id)->update();

if($_output['type']) $urls = $server_config[$sid]['server_serverpath'] . qbase::trimn($urlss['urls'] );
else $urls = qbase::trimn($urlss['urls']);

$filename = substr( strrchr( $urls, '/' ), 1 );
ob_clean();
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=text.rar");
header("Accept-Ranges: bytes");
header("Content-transfer-encoding: binary");
header("Location:".$urls);
?>