<?php
/*
乘风下载管理系统
作者QQ：178575
作者Email：yliangcf@163.com
作者网站：http://www.qqcf.com
详细简介：http://www.qqcf.com/cfdown.htm
上面有程序在线演示，安装演示，使用疑难解答，最新版本下载等内容
因为这些内容可能时常更新，就没有放在程序里，请自己上网站查看
*/
?>
<?php
require '../conn.php';
require '../config.php';
require '../include/myfunction.php';
require '../include/getcookie.php';

$idstr = isset($_GET["idstr"]) ? chkstr($_GET["idstr"],1) :  AlertUrl('缩短网址不能为空',"../");

$ip=empty($_SERVER["HTTP_X_FORWARDED_FOR"])?ChkStr($_SERVER["REMOTE_ADDR"],1):ChkStr($_SERVER["HTTP_X_FORWARDED_FOR"],1);
$ly=ChkStr($_SERVER["HTTP_REFERER"],1);

$Sql="Select * From cfdown_info where idstr='$idstr'";

$Result=mysql_query($Sql);
if(!$Rs=mysql_fetch_assoc($Result)) AlertUrl('记录不存在',"../");
$id=$Rs['id'];
$pwd=$Rs['pwd'];
$downurl=$Rs['downurl'];


//需要内部提取码时但没有用提取码登录了时
if($pwd!=""&&$_COOKIE["CFDownIDStr_".$idstr]!=md5($idstr.$syscode)) AlertUrl('需要提取码下载',"./$idstr");


//对搜索引擎统计
$agentstr=$_SERVER['HTTP_USER_AGENT'];
if(preg_match("/(Googlebot|Msnbot|YodaoBot|Sosospider|baiduspider|google|baidu|yahoo|sogou|bing|coodir|soso|youdao|zhongsou|slurp|ia_archiver|scooter|spider|webcrawler|OutfoxBot)/i", $agentstr)){
	$searchbot=1;
	
    $Sql="update cfdown_info set searchbotnum=searchbotnum+1,lasttime=NOW() where id=$id";
	mysql_query($Sql);
	
    $Sql="update cfdown_count_day set searchbotnum=searchbotnum+1 where infoid=$id and TO_DAYS(NOW())-TO_DAYS(adddate)=0";
	mysql_query($Sql);
	if(mysql_affected_rows()==0){
		$Sql="insert into cfdown_count_day (infoid,searchbotnum,adddate) values ($id,1,DATE_FORMAT(NOW(),'%Y-%m-%d'))";
		mysql_query($Sql);
	}
}else{
	$searchbot=0;
	//开启了下载链接的防盗链选项时，来源里不包含此站域名时,这里不能是否有cookie判断，因为下载工具时是获取不到cookie的
	if($RsSet["downlinkfdl"]==1){
	 if (strpos($ly,HttpPath(1))===false) AlertUrl('禁止外链',"../");
	}
	
	$Sql="update cfdown_info set downnum=downnum+1,lasttime=NOW() where id=$id";
	mysql_query($Sql);

	$Sql="update cfdown_count_day set downnum=downnum+1 where infoid=$id and TO_DAYS(NOW())-TO_DAYS(adddate)=0";
	mysql_query($Sql);
	if(mysql_affected_rows()==0){
		$Sql="insert into cfdown_count_day (infoid,downnum,adddate) values ($id,1,DATE_FORMAT(NOW(),'%Y-%m-%d'))";
		mysql_query($Sql);
	}
}


$Sql="Select ifnull(max(myid),0) myid From cfdown_visit_last";
$Result=mysql_query($Sql);
$Rs2=mysql_fetch_assoc($Result);
$myid=$Rs2['myid'];



$Sql="update cfdown_visit_last set myid=".($myid+ 1).",numtype=2,ip='$ip',ly='$ly',infoid=$id,searchbot=$searchbot,agentstr='$agentstr',addtime='".date("Y-m-d H:i:s")."' where myid = ".($myid - 10000 + 1);

mysql_query($Sql);
if(mysql_affected_rows()==0){
	$Sql="insert into cfdown_visit_last (myid,numtype,ip,ly,infoid,searchbot,agentstr,addtime) values (".($myid+1).",2,'$ip','$ly',$id,$searchbot,'$agentstr','".date("Y-m-d H:i:s")."')";
	mysql_query($Sql);
}
 
if(strpos($downurl,"http://")===false&&strpos($downurl,"https://")===false&&strpos($downurl,"ftp://")===false)
$downurl=$downurlprefix.urlencode($downurl);


header("Location: $downurl");
