<?php
# Name: PHP版程氏音乐CMS管理系统 v3.0
# Author: 程氏<web@chshcms.com> [QQ:848769359]
# Homepage:http://www.chshcms.cn/
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
header("Cache-Control: no-cache, must-revalidate"); 
header("Pragma: no-cache");
if(!eregi($_SERVER['SERVER_NAME'],$_SERVER['HTTP_REFERER'])){exit('QQ:848769359');}
include_once("../lib/cs_conn.php");
include_once("../lib/cs_common.php");
$id=CS_Request('id');
$ac=CS_Request('ac');
	global $db;
	$row=$db->getRow("select * from ".Getdbname('dance')." where CS_ID=".$id." and cs_yid=0");
	if($row){
             $server=$db->getrow("select * from ".Getdbname('fwqip')." where CS_ID=".$row['CS_FID']."");
             $name=$row['CS_Name'];
             $cion=$row['CS_Coin'];
             $cid=$row['CS_CID'];
             $vip=$row['CS_Vip'];
             $downurl=$row['CS_DownUrl1'];
             if(substr($downurl,0,5)=="csdj_"){
                   $str=explode("_",$downurl);
                   $downurl="http://up.chshcms.com/d/?".substr($str[2],0,-4);
             }elseif(substr($downurl,0,3)=="wk_"){
                   $str=explode("_",$downurl);
                   $downurl=getvdisk(substr($str[1],0,-4));
             }elseif(substr($downurl,0,22)=="http://www.rayfile.com"){
                   $downurl=$downurl;
             }elseif(substr($downurl,0,7)=="http://"){
                   $downurl=$downurl;
             }else{
                   $downurl=$server['CS_Downip'].$downurl;
             }
             $downurl1=$downurl;
             if(substr($row['CS_DownUrl2'],0,7)=="http://"){
                        $downurl2=$row['CS_DownUrl2'];
             }else{
                        $downurl2=$server['CS_Downip'].$row['CS_DownUrl2'];
             }
             $dancehz=substr($downurl,-3);
        }else{
            exit('抱歉，该歌曲不存在或者没有审核!');
        }
if($ac=='down'){ //下载
 define('CSDJ_PATH',$_SERVER['DOCUMENT_ROOT']);

 if($vip!=0){
     if(!isset($_COOKIE["cs_name"])){
	  exit(Msg_Error('抱歉，您还没有登入，请先登入!',CS_WebPath.'user/login.php'));
     }
         $us=$db->getRow("select * from ".Getdbname('user')." where CS_Name='".$_COOKIE["cs_name"]."'");
         if(!$us) exit(Msg_Error('抱歉，我在数据库中没有找到您!',CS_WebPath.'user/login.php'));  //没有该会员

	 $cookies="cscms_down_$id"; 
	 if(isset($_COOKIE[$cookies])){
		$CS_DownCook=$_COOKIE[$cookies];
	 }else{
		$CS_DownCook="";
	 }

         //判断下载权限
	 if($vip==2){ 
            if($us['CS_Vip']==0){
            	exit(Msg_Error('抱歉，您的级别不能下载改歌曲，请先升级VIP!',CS_WebPath.'user/space.php?ac=pay&op=vip'));
            }
         }

	 if($CS_DownCook!='yes' && $us['CS_Vip']==0){ 
            //判断金币是否够
            if($us['CS_Cion']<$cion) exit(Msg_Error('抱歉，您的金币不够，请先充值!',CS_WebPath.'user/space.php?ac=pay'));

            //减去金币
            $nescion=$us['CS_Cion']-$cion;
            $db->query("update ".Getdbname('user')." set CS_Cion=".$nescion." where CS_name='".$_COOKIE["cs_name"]."'");
	    setcookie($cookies,'yes',time()+86400);
         }

           //*********分享到腾讯微薄********//
             require_once("../user/open/qq/common.php");
             $linkurl="http://".CS_WebUrl.LinkUrl('play','dance','id',$row['CS_CID'],$row['CS_ID']);
             $title="我刚刚在“".CS_WebName."”下载了音乐[ ".$row['CS_Name']." ]非常好听哦！ 感谢支持！";
             $ret = add_share($title,$linkurl,CS_WebName,CS_WebName);
 }
  if(isset($_COOKIE["cs_name"])){
            //写入下载记录
		//php获取ip的算法
		$CS_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
		$CS_IP = ($CS_IP) ? $CS_IP : $_SERVER["REMOTE_ADDR"]; 
                  //判断是否已下载过
		  $sqld="select * from ".Getdbname('down')." where CS_User='".$_COOKIE["cs_name"]."' and CS_DID=$id";
		  if($rowd=$db->Getrow($sqld)){//有则更新下载时间
			$db->query("update ".Getdbname('down')." set CS_AddTime='".date('Y-m-d H:i:s')."' where CS_User='".$_COOKIE["cs_name"]."' and CS_DID=$id");
			$db->query("update ".Getdbname('dt')." set CS_Addtime='".date('Y-m-d H:i:s')."' where CS_User='".$_COOKIE["cs_name"]."' and CS_DID=$id");
		  }else{//没有则入库
			$sqlhya="insert into ".Getdbname('down')." (CS_Name,CS_Cion,CS_DID,CS_CID,CS_Ip,CS_User,CS_AddTime) values ('".$name."','".$cion."',".$id.",".$cid.",'".$CS_IP."','".$_COOKIE["cs_name"]."','".date('Y-m-d H:i:s')."')";
			if($db->query($sqlhya)){
				$sqldt="insert into ".Getdbname('dt')." (CS_Title,CS_DID,CS_CID,CS_User,CS_AddTime) values ('".$name."',".$id.",6,'".$_COOKIE["cs_name"]."','".date('Y-m-d H:i:s')."')";
				$db->query($sqldt);
			}
		  }
  }
         //增加下载次数
         $db->query("update ".Getdbname('dance')." set CS_Xhits=CS_Xhits+1 where CS_ID=".$id."");

         if (!empty($downurl)){ //判断默认下载地址是否为空
              if (!file_exists(CSDJ_PATH.$downurl1)){
                   header("Location: ".$downurl1); //连接下载
                   exit();
              }else{
	           $file = fopen(CSDJ_PATH.$downurl1,"r"); 
	           Header("Content-type: application/octet-stream");
	           Header("Accept-Ranges: bytes");
	           Header("Accept-Length: ".filesize(CSDJ_PATH.$downurl1));
	           Header("Content-Disposition: attachment; filename=".$name.".".$dancehz."");  //下载时文件改名
	           Header('Content-Type:application/force-download');
                   $Newsname=fread($file, filesize(CSDJ_PATH.$downurl1));
                    exit($Newsname);  //下载
	           fclose($file);
               }
         }else{
                   header("Location: ".$downurl2); //备用下载
                   exit();
         }
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="gbk" />
<title>歌曲下载_<?php echo $name?></title>
<style type="text/css">
.popDownload-form{padding:0px 20px;margin:0 auto;}
.popDownload-form dl dt{font-weight:400;font-size:14px;border-bottom:1px dashed #ccc;padding-bottom:10px;}
.popDownload-form dl strong{font-weight:800;}
.popDownload-form dl dt .gray11{display:inline;float:left;width:360px;word-break:keep-all;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}
.popDownload-form dl dt .gray{display:inline;width:360px;}.popDownload-form dl dd{margin-top:10px;}
.popDownload-form dl .btn-download{width:60px;height:24px;margin-top:5px;float:right;background:url(images/down.png) 0 0 no-repeat;}
.popDownload-form dl .download-help{font-size:12px;color:#999;float:left;width:220px;}
.form-box{color:#666;font-size:14px;overflow:hidden;zoom:1;}
</style>
<script language="javascript"> 
function stop(){return false;} 
document.oncontextmenu=stop; 
</script>
</head>
<body>
<div class='popDownload-form form-box'>
<dl>
<dt>
<span class='gray'>歌曲名称：<strong title="<?php echo $name?>"><?php echo $name?></strong></span>
</dt>
<dd>
<span class='download-help'>该歌曲需要金币 <font color=red><?php echo $cion?></font> 个。<br>如果下载有问题，请联系本站管理员QQ:<?php echo CS_AdminQQ?><br>24小时重复下载不扣金币.</span>
<a href='?ac=down&id=<?php echo $id?>' class='btn-download' target='_blank'></a>
</dd>
</dl>
</div>
</body>
</html>