<?php
/*
'**************************************************************************************************
' 程序名称: CCTVAI开源音乐管理系统
' 官方网站: http://www.cctvai.com
' 联系 Q Q: 1542367792
' QQ交流群：136687984
' 版本：(终结版)
' 备注：未经书面授权，不得向任何第三方提供本软件系统！
' 功能，模板，插件，扩展，定制请联系QQ：1542367792
'**************************************************************************************************
*/
error_reporting(0);
include "../include/cctvai.conn.php";
if(cd_mobile=="no"){exit();}
if(cd_mobilepc=="no"){if (!isMobile()){exit();}}
include "../include/cctvai.inc.php";
$action=SafeRequest("action", "get");
$cd_id=SafeRequest("id", "get");
if(!eregi($_SERVER['SERVER_NAME'], $_SERVER['HTTP_REFERER'])) {
	$error="来路不正确，请从本站访问！";
	exit(include _cctvai_root_ . "/include/error/content_error.php");
}
if(!IsNumID($cd_id)) {
	$error="ID错误，请检查Url地址是否正确！";
	exit(include _cctvai_root_ . "/include/error/content_error.php");
}
if(!IsNum($cd_id)) {
	$error="缺少ID，请检查Url地址是否正确！";
	exit(include _cctvai_root_ . "/include/error/content_error.php");
}
if($action=="dj") { //单曲
	$sql="select * from " . tname('dj') . " where CD_ID='$cd_id'";
	if($row=$db->getrow($sql)) {
		$server=$db->getrow("select * from " . tname('server') . " where CD_ID=" . $row['CD_Server'] . "");
		$dowmurl=$server['CD_DownUrl'].urlencode(mb_convert_encoding($row['CD_DownUrl'],'utf-8','gb2312' ));
		$dowmurl=str_replace('%2F','/',$dowmurl);
		$dowmurl=str_replace('+',' ',$dowmurl);
		$dowmurl=str_replace('%3A',':',$dowmurl);
		if($dowmurl==""){
			$error="暂无下载地址！";
			exit(include _cctvai_root_ . "/include/error/content_error.php");
		}
		if($row['CD_Grade']=="0") {
			$row['CD_Points']=0;
			if($CcTvAi_com_userid) {
				$downsql="select cd_id from " . tname('down') . " where cd_uid='" . $CcTvAi_com_userid . "' and cd_type='dj' and cd_musicid='" . $row['CD_ID'] . "'";
				$down=$db->getrow($downsql);
				if($down) {
					if(comparetime(date('Y-m-d H:i:s', $down['cd_addtime']), date('Y-m-d H:i:s'))>24) {
						$db->query("update " . tname('down') . " set cd_addtime=" . time() . " where cd_id='" . $down['cd_id'] . "'"); //超过24小时更新时间
					}
				} else {
					$setarr=array(
						'cd_type'=>'dj',
						'cd_uid'=>$CcTvAi_com_userid,
						'cd_uname'=>$CcTvAi_com_username,
						'cd_musicid'=>$row['CD_ID'],
						'cd_musicname'=>$row['CD_Name'],
						'cd_classid'=>$row['CD_ClassID'],
						'cd_addtime'=>time()
					);
					inserttable('down', $setarr, 1);
				}
				$feedsql="select cd_id from " . tname('feed') . " where cd_icon='down' and cd_title='下载了音乐' and cd_uid=" . $CcTvAi_com_userid . " and cd_dataid='" . $row['CD_ID'] . "'";
				$feed=$db->getrow($feedsql);
				if($feed) {
					$db->query("update " . tname('feed') . " set cd_addtime=" . date('Y-m-d H:i:s') . " where cd_id='" . $feed['cd_id'] . "'");
				} else {
					$setarrs=array(
						'cd_uid'=>$CcTvAi_com_userid,
						'cd_uname'=>$CcTvAi_com_username,
						'cd_icon'=>'down',
						'cd_title'=>'下载了音乐',
						'cd_data'=>'下载了《' . $row['CD_Name'] . '》<a href="' . LinkUrl("dj", $row['CD_ClassID'], 1, $row['CD_ID']) . '" target="_blank">试听</a>',
						'cd_image'=>'',
						'cd_imagelink'=>'',
						'cd_dataid'=>$row['CD_ID'],
						'cd_addtime'=>date('Y-m-d H:i:s')
					);
					inserttable('feed', $setarrs, 1);
				}

			}
			$db->query("update " . tname('dj') . " set CD_DownHits=CD_DownHits+1 where CD_ID='" . $row['CD_ID'] . "'");
			//本地附件下载
			if($row['CD_Server']=="1") {
				if(cd_dowattachment=="yes") {
					$file_name=$row['CD_DownUrl'];
					$form=strtolower(trim(substr(strrchr($file_name, "."), 1)));
					$down_name=str_replace('[dj:name]', $row['CD_Name'], cd_dowattachmentname);
					$down_name=str_replace('[dj:singer]', GetSingerAlias('cctvai_singer', 'CD_Name', 'CD_ID', $row['CD_Singer']), $down_name);
					$down_name=$down_name . '.' . $form;
					header("Content-type:text/html;charset=gb2312");
					$file_path=_cctvai_root_ . $file_name;
					if(!file_exists($file_path)) {
						echo'<meta charset="gbk" />';
						exit("<script language=javascript>alert('出错了,下载资源不存在!');JavaScript:window.close();</script>");
					}
					$fp=@fopen($file_path, "r");
					$file_size=filesize($file_path);
					//下载文件需要用到的头 
					header("Content-type: application/octet-stream");
					header("Accept-Ranges: bytes");
					header("Accept-Length:" . $file_size);
					header("Content-Disposition: attachment; filename=" . $down_name);
					//向浏览器返回数据 
					while(!feof($fp)) {
						echo fread($fp, cd_dowattachmentsiz); //每秒传输字节
					}
					fclose($file);
					exit();
				}

			}
				header("Content-Type: application/force-download");
				header("Location:$dowmurl");
			

		} elseif($row['CD_Grade']=="1") { //普通会员下载
			if($CcTvAi_com_userid) {
				$user=$db->getrow("select cd_name,cd_id from " . tname('user') . " where cd_name='" . $row['CD_User'] . "'");
				if($CcTvAi_com_username==$user['cd_name']) {
					$row['CD_Points']=0;
				}
				$downsql="select cd_id,cd_addtime from " . tname('down') . " where cd_uid='" . $CcTvAi_com_userid . "' and cd_type='dj' and cd_musicid='" . $row['CD_ID'] . "'";
				$down=$db->getrow($downsql);
				if($down) {
					if(comparetime(date('Y-m-d H:i:s', $down['cd_addtime']), date('Y-m-d H:i:s'))>24) {
						$db->query("update " . tname('down') . " set cd_addtime=" . time() . " where cd_id='" . $down['cd_id'] . "'"); //超过24小时更新时间
					} else {
						$row['CD_Points']=0;
					}
				} else {
				if($CcTvAi_com_grade==1) {
					$row['CD_Points']=0;
				} else {
					if($CcTvAi_com_points<$row['CD_Points']) {
						echo'<meta charset="gbk" />';
						exit("<script language='javascript'>if( confirm('出错了,您的帐号金币不足无法下载!是否前往充值中心？') ){location.href='user/index.php?ac=assets&view=topup';}else{;JavaScript:window.close();}</script>");
					}
				}
					$setarr=array(
						'cd_type'=>'dj',
						'cd_uid'=>$CcTvAi_com_userid,
						'cd_uname'=>$CcTvAi_com_username,
						'cd_musicid'=>$row['CD_ID'],
						'cd_musicname'=>$row['CD_Name'],
						'cd_classid'=>$row['CD_ClassID'],
						'cd_addtime'=>time()
					);
					inserttable('down', $setarr, 1);

				}
				$feedsql="select cd_id from " . tname('feed') . " where cd_icon='down' and cd_title='下载了音乐' and cd_uid=" . $CcTvAi_com_userid . " and cd_dataid='" . $row['CD_ID'] . "'";
				$feed=$db->getrow($feedsql);
				if($feed) {
					$db->query("update " . tname('feed') . " set cd_addtime=" . date('Y-m-d H:i:s') . " where cd_id='" . $feed['cd_id'] . "'");
				} else {
					$setarrs=array(
						'cd_uid'=>$CcTvAi_com_userid,
						'cd_uname'=>$CcTvAi_com_username,
						'cd_icon'=>'down',
						'cd_title'=>'下载了音乐',
						'cd_data'=>'下载了《' . $row['CD_Name'] . '》<a href="' . LinkUrl("dj", $row['CD_ClassID'], 1, $row['CD_ID']) . '" target="_blank">试听</a>',
						'cd_image'=>'',
						'cd_imagelink'=>'',
						'cd_dataid'=>$row['CD_ID'],
						'cd_addtime'=>date('Y-m-d H:i:s')
					);
					inserttable('feed', $setarrs, 1);
				}

				$db->query("update " . tname('dj') . " set CD_DownHits=CD_DownHits+1 where CD_ID='" . $row['CD_ID'] . "'");
				if($row['CD_Points']>=1) {
					$setarr=array(
						'cd_type'=>0,
						'cd_uid'=>$CcTvAi_com_userid,
						'cd_uname'=>$CcTvAi_com_username,
						'cd_icon'=>'down',
						'cd_title'=>'您下载别人的音乐',
						'cd_points'=>$row['CD_Points'],
						'cd_state'=>1,
						'cd_addtime'=>date('Y-m-d H:i:s'),
						'cd_endtime'=>date('Y-m-d H:i:s')
					);
					inserttable('bill', $setarr, 1);
					$setarr=array(
						'cd_type'=>1,
						'cd_uid'=>$user['cd_id'],
						'cd_uname'=>$row['CD_User'],
						'cd_icon'=>'down',
						'cd_title'=>'别人下载您的音乐',
						'cd_points'=>$row['CD_Points'],
						'cd_state'=>1,
						'cd_addtime'=>date('Y-m-d H:i:s'),
						'cd_endtime'=>date('Y-m-d H:i:s')
					);
					inserttable('bill', $setarr, 1);
					$db->query("update " . tname('user') . " set cd_points=cd_points-'" . $row['CD_Points'] . "' where cd_id='" . $CcTvAi_com_userid . "'");
					$db->query("update " . tname('user') . " set cd_points=cd_points+'" . $row['CD_Points'] . "' where cd_name='" . $row['CD_User'] . "'");
				}

				//本地附件下载
				if($row['CD_Server']=="1") {
					if(cd_dowattachment=="yes") {
						$file_name=$row['CD_DownUrl'];
						$form=strtolower(trim(substr(strrchr($file_name, "."), 1)));
						$down_name=str_replace('[dj:name]', $row['CD_Name'], cd_dowattachmentname);
						$down_name=str_replace('[dj:singer]', GetSingerAlias('cctvai_singer', 'CD_Name', 'CD_ID', $row['CD_Singer']), $down_name);
						$down_name=$down_name . '.' . $form;
						header("Content-type:text/html;charset=gb2312");
						$file_path=_cctvai_root_ . $file_name;
						if(!file_exists($file_path)) {
							echo'<meta charset="gbk" />';
							exit("<script language=javascript>alert('出错了,下载资源不存在!');JavaScript:window.close();</script>");
						}
						$fp=@fopen($file_path, "r");
						$file_size=filesize($file_path);
						//下载文件需要用到的头 
						header("Content-type: application/octet-stream");
						header("Accept-Ranges: bytes");
						header("Accept-Length:" . $file_size);
						header("Content-Disposition: attachment; filename=" . $down_name);
						//向浏览器返回数据 
						while(!feof($fp)) {
							echo fread($fp, cd_dowattachmentsiz); //每秒传输字节
						}
						fclose($file);
						exit();
					}

				}
					header("Content-Type: application/force-download");
					header("Location:$dowmurl");
				
			} else {
				echo'<meta charset="gbk" />';
				exit("<script language=javascript>alert('您需要先登录才能进行此操作!');top.location='user/login.php';</script>");
			}

		} elseif($row['CD_Grade']=="2") { //VIP会员下载
			if($CcTvAi_com_userid) {
				if($row['CD_Points']>=1) {
					$user=$db->getrow("select cd_name,cd_id from " . tname('user') . " where cd_name='" . $row['CD_User'] . "'");
					if($CcTvAi_com_username==$user['cd_name']) {
						$row['CD_Points']=0;
					}

					$downsql="select cd_id,cd_addtime from " . tname('down') . " where cd_uid='" . $CcTvAi_com_userid . "' and cd_type='dj' and cd_musicid='" . $row['CD_ID'] . "'";
					$down=$db->getrow($downsql);
					if($down) {
						if(comparetime(date('Y-m-d H:i:s', $down['cd_addtime']), date('Y-m-d H:i:s'))>24) {
							$db->query("update " . tname('down') . " set cd_addtime=" . time() . " where cd_id='" . $down['cd_id'] . "'"); //超过24小时更新时间
						} else {
							$row['CD_Points']=0;
						}
					} else {
				if($CcTvAi_com_grade==1) {
					$row['CD_Points']=0;
				} else {
					if($CcTvAi_com_points<$row['CD_Points']) {
						echo'<meta charset="gbk" />';
						exit("<script language='javascript'>if( confirm('出错了,您的帐号金币不足无法下载!是否前往充值中心？') ){location.href='user/index.php?ac=assets&view=topup';}else{;JavaScript:window.close();}</script>");
					}
				}
						$setarr=array(
							'cd_type'=>'dj',
							'cd_uid'=>$CcTvAi_com_userid,
							'cd_uname'=>$CcTvAi_com_username,
							'cd_musicid'=>$row['CD_ID'],
							'cd_musicname'=>$row['CD_Name'],
							'cd_classid'=>$row['CD_ClassID'],
							'cd_addtime'=>time()
						);
						inserttable('down', $setarr, 1);

					}
					$feedsql="select cd_id from " . tname('feed') . " where cd_icon='down' and cd_title='下载了音乐' and cd_uid=" . $CcTvAi_com_userid . " and cd_dataid='" . $row['CD_ID'] . "'";
					$feed=$db->getrow($feedsql);
					if($feed) {
						$db->query("update " . tname('feed') . " set cd_addtime=" . date('Y-m-d H:i:s') . " where cd_id='" . $feed['cd_id'] . "'");
					} else {
						$setarrs=array(
							'cd_uid'=>$CcTvAi_com_userid,
							'cd_uname'=>$CcTvAi_com_username,
							'cd_icon'=>'down',
							'cd_title'=>'下载了音乐',
							'cd_data'=>'下载了《' . $row['CD_Name'] . '》<a href="' . LinkUrl("dj", $row['CD_ClassID'], 1, $row['CD_ID']) . '" target="_blank">试听</a>',
							'cd_image'=>'',
							'cd_imagelink'=>'',
							'cd_dataid'=>$row['CD_ID'],
							'cd_addtime'=>date('Y-m-d H:i:s')
						);
						inserttable('feed', $setarrs, 1);
					}

					$db->query("update " . tname('dj') . " set CD_DownHits=CD_DownHits+1 where CD_ID='" . $row['CD_ID'] . "'");
					if($row['CD_Points']>=1) {
						$setarr=array(
							'cd_type'=>0,
							'cd_uid'=>$CcTvAi_com_userid,
							'cd_uname'=>$CcTvAi_com_username,
							'cd_icon'=>'down',
							'cd_title'=>'您下载别人的音乐',
							'cd_points'=>$row['CD_Points'],
							'cd_state'=>1,
							'cd_addtime'=>date('Y-m-d H:i:s'),
							'cd_endtime'=>date('Y-m-d H:i:s')
						);
						inserttable('bill', $setarr, 1);
						$setarr=array(
							'cd_type'=>1,
							'cd_uid'=>$user['cd_id'],
							'cd_uname'=>$row['CD_User'],
							'cd_icon'=>'down',
							'cd_title'=>'别人下载您的音乐',
							'cd_points'=>$row['CD_Points'],
							'cd_state'=>1,
							'cd_addtime'=>date('Y-m-d H:i:s'),
							'cd_endtime'=>date('Y-m-d H:i:s')
						);
						inserttable('bill', $setarr, 1);
						$db->query("update " . tname('user') . " set cd_points=cd_points-'" . $row['CD_Points'] . "' where cd_id='" . $CcTvAi_com_userid . "'");
						$db->query("update " . tname('user') . " set cd_points=cd_points+'" . $row['CD_Points'] . "' where cd_name='" . $row['CD_User'] . "'");
					}

					//本地附件下载
					if($row['CD_Server']=="1") {
						if(cd_dowattachment=="yes") {
							$file_name=$row['CD_DownUrl'];
							$form=strtolower(trim(substr(strrchr($file_name, "."), 1)));
							$down_name=str_replace('[dj:name]', $row['CD_Name'], cd_dowattachmentname);
							$down_name=str_replace('[dj:singer]', GetSingerAlias('cctvai_singer', 'CD_Name', 'CD_ID', $row['CD_Singer']), $down_name);
							$down_name=$down_name . '.' . $form;
							header("Content-type:text/html;charset=gb2312");
							$file_path=_cctvai_root_ . $file_name;
							if(!file_exists($file_path)) {
								echo'<meta charset="gbk" />';
								exit("<script language=javascript>alert('出错了,下载资源不存在!');JavaScript:window.close();</script>");
							}
							$fp=@fopen($file_path, "r");
							$file_size=filesize($file_path);
							//下载文件需要用到的头 
							header("Content-type: application/octet-stream");
							header("Accept-Ranges: bytes");
							header("Accept-Length:" . $file_size);
							header("Content-Disposition: attachment; filename=" . $down_name);
							//向浏览器返回数据 
							while(!feof($fp)) {
								echo fread($fp, cd_dowattachmentsiz); //每秒传输字节
							}
							fclose($file);
							exit();
						}

					}
						header("Content-Type: application/force-download");
						header("Location:$dowmurl");
					
				} else {
					echo'<meta charset="gbk" />';
					exit("<script language=javascript>alert('出错了,您没有权限下载VIP资源!');JavaScript:window.close();</script>");
				}
			} else {
				echo'<meta charset="gbk" />';
				exit("<script language=javascript>alert('您需要先登录才能进行此操作!');top.location='user/login.php';</script>");
			}
		}
	} else {
		echo'<meta charset="gbk" />';
		exit("<script language=javascript>alert('出错了,下载资源不存在!');JavaScript:window.close();</script>");
	}
} elseif($action=="video") { //视频
	$sql="select * from " . tname('video') . " where CD_ID='$cd_id'";
	if($row=$db->getrow($sql)) {
		$server=$db->getrow("select * from " . tname('server') . " where CD_ID=" . $row['CD_Server'] . "");
		$dowmurl=$server['CD_DownUrl'] . $row['CD_DownUrl'];
		if($dowmurl==""){
			$error="暂无下载地址！";
			exit(include _cctvai_root_ . "/include/error/content_error.php");
		}
		if($row['CD_Grade']=="0") {
			$row['CD_Points']=0;
			if($CcTvAi_com_userid) {
				$downsql="select cd_id from " . tname('down') . " where cd_uid='" . $CcTvAi_com_userid . "' and cd_type='video' and cd_musicid='" . $row['CD_ID'] . "'";
				$down=$db->getrow($downsql);
				if($down) {
					if(comparetime(date('Y-m-d H:i:s', $down['cd_addtime']), date('Y-m-d H:i:s'))>24) {
						$db->query("update " . tname('down') . " set cd_addtime=" . time() . " where cd_id='" . $down['cd_id'] . "'"); 
					}
				} else {
					$setarr=array(
						'cd_type'=>'video',
						'cd_uid'=>$CcTvAi_com_userid,
						'cd_uname'=>$CcTvAi_com_username,
						'cd_musicid'=>$row['CD_ID'],
						'cd_musicname'=>$row['CD_Name'],
						'cd_classid'=>$row['CD_ClassID'],
						'cd_addtime'=>time()
					);
					inserttable('down', $setarr, 1);
				}
				$feedsql="select cd_id from " . tname('feed') . " where cd_icon='down' and cd_title='下载了视频' and cd_uid=" . $CcTvAi_com_userid . " and cd_dataid='" . $row['CD_ID'] . "'";
				$feed=$db->getrow($feedsql);
				if($feed) {
					$db->query("update " . tname('feed') . " set cd_addtime=" . date('Y-m-d H:i:s') . " where cd_id='" . $feed['cd_id'] . "'");
				} else {
					$setarrs=array(
						'cd_uid'=>$CcTvAi_com_userid,
						'cd_uname'=>$CcTvAi_com_username,
						'cd_icon'=>'down',
						'cd_title'=>'下载了视频',
						'cd_data'=>'下载了《' . $row['CD_Name'] . '》<a href="' . LinkUrl("video", $row['CD_ClassID'], 1, $row['CD_ID']) . '" target="_blank">观看</a>',
						'cd_image'=>'',
						'cd_imagelink'=>'',
						'cd_dataid'=>$row['CD_ID'],
						'cd_addtime'=>date('Y-m-d H:i:s')
					);
					inserttable('feed', $setarrs, 1);
				}

			}
			$db->query("update " . tname('video') . " set CD_DownHits=CD_DownHits+1 where CD_ID='" . $row['CD_ID'] . "'");
			//本地附件下载
			if($row['CD_Server']=="1") {
				if(cd_dowattachment=="yes") {
					$file_name=$row['CD_DownUrl'];
					$form=strtolower(trim(substr(strrchr($file_name, "."), 1)));
					$down_name=str_replace('[dj:name]', $row['CD_Name'], cd_dowattachmentname);
					$down_name=str_replace('[dj:singer]', GetSingerAlias('cctvai_singer', 'CD_Name', 'CD_ID', $row['CD_Singer']), $down_name);
					$down_name=$down_name . '.' . $form;
					header("Content-type:text/html;charset=gb2312");
					$file_path=_cctvai_root_ . $file_name;
					if(!file_exists($file_path)) {
						echo'<meta charset="gbk" />';
						exit("<script language=javascript>alert('出错了,下载资源不存在!');JavaScript:window.close();</script>");
					}
					$fp=@fopen($file_path, "r");
					$file_size=filesize($file_path);
					//下载文件需要用到的头 
					header("Content-type: application/octet-stream");
					header("Accept-Ranges: bytes");
					header("Accept-Length:" . $file_size);
					header("Content-Disposition: attachment; filename=" . $down_name);
					//向浏览器返回数据 
					while(!feof($fp)) {
						echo fread($fp, cd_dowattachmentsiz); //每秒传输字节
					}
					fclose($file);
					exit();
				}

			}
				header("Content-Type: application/force-download");
				header("Location:$dowmurl");
			

		} elseif($row['CD_Grade']=="1") { //普通会员下载
			if($CcTvAi_com_userid) {
				$user=$db->getrow("select cd_name,cd_id from " . tname('user') . " where cd_name='" . $row['CD_User'] . "'");
				if($CcTvAi_com_username==$user['cd_name']) {
					$row['CD_Points']=0;
				}

				$downsql="select cd_id,cd_addtime from " . tname('down') . " where cd_uid='" . $CcTvAi_com_userid . "' and cd_type='video' and cd_musicid='" . $row['CD_ID'] . "'";
				$down=$db->getrow($downsql);
				if($down) {
					if(comparetime(date('Y-m-d H:i:s', $down['cd_addtime']), date('Y-m-d H:i:s'))>24) {
						$db->query("update " . tname('down') . " set cd_addtime=" . time() . " where cd_id='" . $down['cd_id'] . "'"); 
					} else {
						$row['CD_Points']=0;
					}
				} else {
				if($CcTvAi_com_grade>=1) {
					$row['CD_Points']=0;
				} else {
					if($CcTvAi_com_points<$row['CD_Points']) {
						echo'<meta charset="gbk" />';
						exit("<script language='javascript'>if( confirm('出错了,您的帐号金币不足无法下载!是否前往充值中心？') ){location.href='user/index.php?ac=assets&view=topup';}else{;JavaScript:window.close();}</script>");
					}
				}
					$setarr=array(
						'cd_type'=>'video',
						'cd_uid'=>$CcTvAi_com_userid,
						'cd_uname'=>$CcTvAi_com_username,
						'cd_musicid'=>$row['CD_ID'],
						'cd_musicname'=>$row['CD_Name'],
						'cd_classid'=>$row['CD_ClassID'],
						'cd_addtime'=>time()
					);
					inserttable('down', $setarr, 1);

				}
				$feedsql="select cd_id from " . tname('feed') . " where cd_icon='down' and cd_title='下载了视频' and cd_uid=" . $CcTvAi_com_userid . " and cd_dataid='" . $row['CD_ID'] . "'";
				$feed=$db->getrow($feedsql);
				if($feed) {
					$db->query("update " . tname('feed') . " set cd_addtime=" . date('Y-m-d H:i:s') . " where cd_id='" . $feed['cd_id'] . "'");
				} else {
					$setarrs=array(
						'cd_uid'=>$CcTvAi_com_userid,
						'cd_uname'=>$CcTvAi_com_username,
						'cd_icon'=>'down',
						'cd_title'=>'下载了视频',
						'cd_data'=>'下载了《' . $row['CD_Name'] . '》<a href="' . LinkUrl("video", $row['CD_ClassID'], 1, $row['CD_ID']) . '" target="_blank">观看</a>',
						'cd_image'=>'',
						'cd_imagelink'=>'',
						'cd_dataid'=>$row['CD_ID'],
						'cd_addtime'=>date('Y-m-d H:i:s')
					);
					inserttable('feed', $setarrs, 1);
				}

				$db->query("update " . tname('video') . " set CD_DownHits=CD_DownHits+1 where CD_ID='" . $row['CD_ID'] . "'");
				if($row['CD_Points']>=1) {
					$setarr=array(
						'cd_type'=>0,
						'cd_uid'=>$CcTvAi_com_userid,
						'cd_uname'=>$CcTvAi_com_username,
						'cd_icon'=>'down',
						'cd_title'=>'您下载别人的视频',
						'cd_points'=>$row['CD_Points'],
						'cd_state'=>1,
						'cd_addtime'=>date('Y-m-d H:i:s'),
						'cd_endtime'=>date('Y-m-d H:i:s')
					);
					inserttable('bill', $setarr, 1);
					$setarr=array(
						'cd_type'=>1,
						'cd_uid'=>$user['cd_id'],
						'cd_uname'=>$row['CD_User'],
						'cd_icon'=>'down',
						'cd_title'=>'别人下载您的视频',
						'cd_points'=>$row['CD_Points'],
						'cd_state'=>1,
						'cd_addtime'=>date('Y-m-d H:i:s'),
						'cd_endtime'=>date('Y-m-d H:i:s')
					);
					inserttable('bill', $setarr, 1);
					$db->query("update " . tname('user') . " set cd_points=cd_points-'" . $row['CD_Points'] . "' where cd_id='" . $CcTvAi_com_userid . "'");
					$db->query("update " . tname('user') . " set cd_points=cd_points+'" . $row['CD_Points'] . "' where cd_name='" . $row['CD_User'] . "'");
				}

				//本地附件下载
				if($row['CD_Server']=="1") {
					if(cd_dowattachment=="yes") {
						$file_name=$row['CD_DownUrl'];
						$form=strtolower(trim(substr(strrchr($file_name, "."), 1)));
						$down_name=str_replace('[dj:name]', $row['CD_Name'], cd_dowattachmentname);
						$down_name=str_replace('[dj:singer]', GetSingerAlias('cctvai_singer', 'CD_Name', 'CD_ID', $row['CD_Singer']), $down_name);
						$down_name=$down_name . '.' . $form;
						header("Content-type:text/html;charset=gb2312");
						$file_path=_cctvai_root_ . $file_name;
						if(!file_exists($file_path)) {
							echo'<meta charset="gbk" />';
							exit("<script language=javascript>alert('出错了,下载资源不存在!');JavaScript:window.close();</script>");
						}
						$fp=@fopen($file_path, "r");
						$file_size=filesize($file_path);
						//下载文件需要用到的头 
						header("Content-type: application/octet-stream");
						header("Accept-Ranges: bytes");
						header("Accept-Length:" . $file_size);
						header("Content-Disposition: attachment; filename=" . $down_name);
						//向浏览器返回数据 
						while(!feof($fp)) {
							echo fread($fp, cd_dowattachmentsiz); //每秒传输字节
						}
						fclose($file);
						exit();
					}

				}
					header("Content-Type: application/force-download");
					header("Location:$dowmurl");
				
			} else {
				echo'<meta charset="gbk" />';
				exit("<script language=javascript>alert('您需要先登录才能进行此操作!');top.location='user/login.php';</script>");
			}

		} elseif($row['CD_Grade']=="2") { //VIP会员下载
			if($CcTvAi_com_userid) {
				if($row['CD_Points']>=1) {
					$user=$db->getrow("select cd_name,cd_id from " . tname('user') . " where cd_name='" . $row['CD_User'] . "'");
					if($CcTvAi_com_username==$user['cd_name']) {
						$row['CD_Points']=0;
					}

					$downsql="select cd_id,cd_addtime from " . tname('down') . " where cd_uid='" . $CcTvAi_com_userid . "' and cd_type='video' and cd_musicid='" . $row['CD_ID'] . "'";
					$down=$db->getrow($downsql);
					if($down) {
						if(comparetime(date('Y-m-d H:i:s', $down['cd_addtime']), date('Y-m-d H:i:s'))>24) {
							$db->query("update " . tname('down') . " set cd_addtime=" . time() . " where cd_id='" . $down['cd_id'] . "'"); 
						} else {
							$row['CD_Points']=0;
						}
					} else {
					if($CcTvAi_com_grade>=1) {
						$row['CD_Points']=0;
					} else {
						if($CcTvAi_com_points<$row['CD_Points']) {
							echo'<meta charset="gbk" />';
							exit("<script language='javascript'>if( confirm('出错了,您的帐号金币不足无法下载!是否前往充值中心？') ){location.href='user/index.php?ac=assets&view=topup';}else{;JavaScript:window.close();}</script>");
						}
					}
						$setarr=array(
							'cd_type'=>'video',
							'cd_uid'=>$CcTvAi_com_userid,
							'cd_uname'=>$CcTvAi_com_username,
							'cd_musicid'=>$row['CD_ID'],
							'cd_musicname'=>$row['CD_Name'],
							'cd_classid'=>$row['CD_ClassID'],
							'cd_addtime'=>time()
						);
						inserttable('down', $setarr, 1);

					}
					$feedsql="select cd_id from " . tname('feed') . " where cd_icon='down' and cd_title='下载了视频' and cd_uid=" . $CcTvAi_com_userid . " and cd_dataid='" . $row['CD_ID'] . "'";
					$feed=$db->getrow($feedsql);
					if($feed) {
						$db->query("update " . tname('feed') . " set cd_addtime=" . date('Y-m-d H:i:s') . " where cd_id='" . $feed['cd_id'] . "'");
					} else {
						$setarrs=array(
							'cd_uid'=>$CcTvAi_com_userid,
							'cd_uname'=>$CcTvAi_com_username,
							'cd_icon'=>'down',
							'cd_title'=>'下载了视频',
							'cd_data'=>'下载了《' . $row['CD_Name'] . '》<a href="' . LinkUrl("video", $row['CD_ClassID'], 1, $row['CD_ID']) . '" target="_blank">观看</a>',
							'cd_image'=>'',
							'cd_imagelink'=>'',
							'cd_dataid'=>$row['CD_ID'],
							'cd_addtime'=>date('Y-m-d H:i:s')
						);
						inserttable('feed', $setarrs, 1);
					}

					$db->query("update " . tname('video') . " set CD_DownHits=CD_DownHits+1 where CD_ID='" . $row['CD_ID'] . "'");
					if($row['CD_Points']>=1) {
						$setarr=array(
							'cd_type'=>0,
							'cd_uid'=>$CcTvAi_com_userid,
							'cd_uname'=>$CcTvAi_com_username,
							'cd_icon'=>'down',
							'cd_title'=>'您下载别人的视频',
							'cd_points'=>$row['CD_Points'],
							'cd_state'=>1,
							'cd_addtime'=>date('Y-m-d H:i:s'),
							'cd_endtime'=>date('Y-m-d H:i:s')
						);
						inserttable('bill', $setarr, 1);
						$setarr=array(
							'cd_type'=>1,
							'cd_uid'=>$user['cd_id'],
							'cd_uname'=>$row['CD_User'],
							'cd_icon'=>'down',
							'cd_title'=>'别人下载您的视频',
							'cd_points'=>$row['CD_Points'],
							'cd_state'=>1,
							'cd_addtime'=>date('Y-m-d H:i:s'),
							'cd_endtime'=>date('Y-m-d H:i:s')
						);
						inserttable('bill', $setarr, 1);
						$db->query("update " . tname('user') . " set cd_points=cd_points-'" . $row['CD_Points'] . "' where cd_id='" . $CcTvAi_com_userid . "'");
						$db->query("update " . tname('user') . " set cd_points=cd_points+'" . $row['CD_Points'] . "' where cd_name='" . $row['CD_User'] . "'");
					}

					//本地附件下载
					if($row['CD_Server']=="1") {
						if(cd_dowattachment=="yes") {
							$file_name=$row['CD_DownUrl'];
							$form=strtolower(trim(substr(strrchr($file_name, "."), 1)));
							$down_name=str_replace('[dj:name]', $row['CD_Name'], cd_dowattachmentname);
							$down_name=str_replace('[dj:singer]', GetSingerAlias('cctvai_singer', 'CD_Name', 'CD_ID', $row['CD_Singer']), $down_name);
							$down_name=$down_name . '.' . $form;
							header("Content-type:text/html;charset=gb2312");
							$file_path=_cctvai_root_ . $file_name;
							if(!file_exists($file_path)) {
								echo'<meta charset="gbk" />';
								exit("<script language=javascript>alert('出错了,下载资源不存在!');JavaScript:window.close();</script>");
							}
							$fp=@fopen($file_path, "r");
							$file_size=filesize($file_path);
							//下载文件需要用到的头 
							header("Content-type: application/octet-stream");
							header("Accept-Ranges: bytes");
							header("Accept-Length:" . $file_size);
							header("Content-Disposition: attachment; filename=" . $down_name);
							//向浏览器返回数据 
							while(!feof($fp)) {
								echo fread($fp, cd_dowattachmentsiz); //每秒传输字节
							}
							fclose($file);
							exit();
						}

					}
						header("Content-Type: application/force-download");
						header("Location:$dowmurl");
					
				} else {
					echo'<meta charset="gbk" />';
					exit("<script language=javascript>alert('出错了,您没有权限下载VIP资源!');JavaScript:window.close();</script>");
				}
			} else {
				echo'<meta charset="gbk" />';
				exit("<script language=javascript>alert('您需要先登录才能进行此操作!');top.location='user/login.php';</script>");
			}
		}
	} else {
		echo'<meta charset="gbk" />';
		exit("<script language=javascript>alert('出错了,下载资源不存在!');JavaScript:window.close();</script>");
	}
} else {
	$error="参数错误，请检查Url地址是否正确！";
	exit(include _cctvai_root_ . "/include/error/content_error.php");
}
?>