<?php
error_reporting(0);
include "../../include/cctvai.conn.php";
include "../../include/cctvai.inc.php";
header("Content-type: text/html; charset=gb2312");
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();
}
$ac=SafeRequest("action", "get");
//单曲 视频评论
if($ac=="comment") {
	global $db;
	$CD_ID=SafeRequest("cd_dataid", "get");
	$CD_Content=SafeRequest("content", "post");
	$cd_channel=SafeRequest("cd_channel", "get");
	$type=SafeRequest("type", "get");
	//$CD_Content=str_replace('`','',$CD_Content);
	if(!IsNumID($CD_ID)) {
		exit('30001');
	}
	if(!IsNumID($cd_channel)) {
		exit('30001');
	}
	if(preg_match("/".cd_messagereplace."/i",unescape($CD_Content))){
		exit('50001');
		}
	if($CcTvAi_com_userid) {
		$cctvai_userid=$CcTvAi_com_userid;
		$cctvai_username=$CcTvAi_com_username;
	} else {
		$cctvai_userid='0';
		$cctvai_username='游客';
		exit('20001');
	}
	if($CD_Content=="") {
		exit('10007');
	}
	$cookies="comment_$type_$CD_ID";
	if($_COOKIE[$cookies]=="yes") {
		exit('10002');
	} else {
		setcookie($cookies, "yes", time()+60);
		$sql="Insert " . tname('comment') . " (cd_channel,cd_dataid,cd_content,cd_uid,cd_uname,cd_uip,cd_addtime) values ('".$cd_channel."','" . $CD_ID . "','" . unescape($CD_Content) . "'," . $cctvai_userid . ",'" . $cctvai_username . "','" . $_SERVER['REMOTE_ADDR'] . "','" . date('Y-m-d H:i:s') . "')";
		if($db->query($sql)) {
			exit('10000');
		}
	}
} elseif($ac=="goodbad") { //顶踩,收藏下载部分 单曲和视频通用，type必须带上video或dj
	global $db;
	$CD_ID=SafeRequest("id", "get");
	$CD_type=SafeRequest("type", "get");
	if(!IsNumID($CD_ID)) {
		exit('参数错误！');
	}
	if($CD_type=="video") {
		$sql="Select CD_ID,CD_Hits,CD_uHits,CD_dHits,CD_DownHits,CD_FavHits from " . tname('video') . " where CD_ID=" . $CD_ID . "";
	} elseif($CD_type=="dj") {
		$sql="Select CD_ID,CD_Hits,CD_uHits,CD_dHits,CD_DownHits,CD_FavHits from " . tname('dj') . " where CD_ID=" . $CD_ID . "";
	}

	if($row=$db->Getrow($sql)) {
		$CD_ID=$row['CD_ID'];
		$CD_Hits=$row['CD_Hits'];
		$OuHits=$row['CD_uHits'];
		$OdHits=$row['CD_dHits'];
		$nCount=$row['CD_uHits']+$row['CD_dHits'];
		$uHits=round($OuHits/$nCount*100, 1);
		$dHits=round($OdHits/$nCount*100, 1);
			$haopingf=$row['CD_uHits']*5;
			$chapingf=$row['CD_dHits']*2;
			$zonga=$haopingf+$chapingf;
			$zfs=$haopingf/$zonga;
			$zfss=round($zfs, 2);
			$zongfenf=$zfss*10;
			
			$R=$zongfenf;
			$V=$row['CD_uHits']+$row['CD_dHits'];
			if($V>50){
			$M=50;
			$C=9;
			$VM=$V+$M;
			$VVM=$V/$VM;
			$MVM=$M/$VM;
			$zongfen=$VVM*$R+$MVM*$C;
			$zongfen=round($zongfen, 1);
			if($zongfen=="10"){$zongfen=9.9;}
			}else{
			$zongfen=5.5;
			}
			
			
			

	}
	if($CD_type=="video") { //视频菜单按钮
	$downurl=DownLinkUrl(cd_webmlvideo,$row['CD_ID']);
		$showstr="<div class=x5_video><a title=\"赞+1\" href=\"javascript:void(0);\" onfocus=\"this.blur();\" onClick=\"up_down($CD_ID, 'good', 'video')\" class=\"ding\" hidefocus=\"true\"><span>" . $OuHits . "</span></a></div>
      <div class=x5_video><a title=\"吐槽+1\" href=\"javascript:void(0);\" onfocus=\"this.blur();\" onClick=\"up_down($CD_ID, 'bad', 'video')\" class=\"cai\" hidefocus=\"true\"><span>" . $OdHits . "</span></a></div>
	  <div class=x5_video><a href=\"javascript:void(0);\" onfocus=\"this.blur();\" onClick=\"up_fav($CD_ID,'video')\" class=\"fav\" hidefocus=\"true\"><span>" . $row['CD_FavHits'] . "</span></a></div>
      <div class=x5_video><a href=\"".$downurl."\" target=\"_blank\" class=\"down\" hidefocus=\"true\"><span>" . $row['CD_DownHits'] . "</span></a></div>
	  ";
	} elseif($CD_type=="dj") { //单曲菜单按钮
	//$downurl=DownLinkUrl(cd_webmlplay,$row['CD_ID']);
		$showstr="
       <div class=\"album_assess\" style=\"display: block;\">
	   <strong class=\"score\">".$zongfen."</strong>
	   <p>".$nCount."评价</p>
        <div class=\"rate\">
         <p><i class=\"icon_good\"></i><span class=\"bar\"><span style=\"width:".$uHits."%;\"></span></span><span class=\"rate_count\">".$uHits."%</span></p>
         <p><i class=\"icon_bad\"></i><span class=\"bar\"><span style=\"width:".$dHits."%;\"></span></span><span class=\"rate_count\">".$dHits."%</span></p>
        </div>
       </div> 
        <a href=\"javascript:;\" onclick=\"up_down($CD_ID, 'good', 'dj')\" class=\"btn_good\"><i class=\"icon_good\"></i><span>顶&nbsp;".$row['CD_uHits']."</span></a> 
        <a href=\"javascript:;\" onclick=\"up_down($CD_ID, 'bad', 'dj')\" class=\"btn_bad\"><i class=\"icon_bad\"></i><span>踩&nbsp;".$row['CD_dHits']."</span></a> 
	  ";
	}

	echo $showstr;
} elseif($ac=="doHits") { //提交数据
	global $db;
	$CD_ID=SafeRequest("id", "get");
	$dowhat=SafeRequest("dowhat", "get");
	$CD_type=SafeRequest("type", "get");
	if(!IsNumID($CD_ID)) {
		exit('up_004');
	}
	if($CD_type=="video") {
		if($dowhat=="good") {
			$sql="update " . tname('video') . " set CD_uHits=CD_uHits+1,CD_LastHitTime='" . date('Y-m-d H:i:s') . "' where CD_ID=" . $CD_ID . "";
			$up_tips='up_001';
		} else {
			$sql="update " . tname('video') . " set CD_dHits=CD_dHits+1 where CD_ID=" . $CD_ID . "";
			$up_tips='up_002';
		}
		$cookies="doHits_video_$CD_ID";
	} elseif($CD_type=="dj") {
		if($dowhat=="good") {
			$sql="update " . tname('dj') . " set CD_uHits=CD_uHits+1,CD_LastHitTime='" . date('Y-m-d H:i:s') . "' where CD_ID=" . $CD_ID . "";
			$up_tips='up_001';
		} else {
			$sql="update " . tname('dj') . " set CD_dHits=CD_dHits+1 where CD_ID=" . $CD_ID . "";
			$up_tips='up_002';
		}
		$cookies="doHits_dj_$CD_ID";
	}
	if($_COOKIE[$cookies]=="yes") {
		exit('up_003');
	} else {
		setcookie($cookies, "yes", time()+86400);
		if($db->query($sql)) {
			exit($up_tips);
		}
	}
} elseif($ac=="dofav") { //提交收藏
	global $db;
	$CD_ID=SafeRequest("id", "get");
	$CD_type=SafeRequest("type", "get");
	if(!IsNumID($CD_ID)) {
		exit('upfav_005');
	}
	if(!$userlogined) {
		exit("upfav_002");
	}
	if($CD_type=="video") {
		$sql="select * from " . tname('video') . " where CD_ID='" . $CD_ID . "'";
		if($row=$db->getrow($sql)) {
			$cd_ids=$db->getone("select cd_id from " . tname('fav') . " where cd_type='video' and cd_uid=" . $CcTvAi_com_userid . " and cd_musicid='" . $CD_ID . "'");
			if($cd_ids) {
				exit('upfav_004');
			} else {
				$db->query("update " . tname('video') . " set CD_FavHits=CD_FavHits+1 where CD_ID='" . $CD_ID . "'");
				$setarr=array(
					'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_type'=>'video',
					'cd_addtime'=>time()
				);
				inserttable('fav', $setarr, 1);
				$setarrs=array(
					'cd_uid'=>$CcTvAi_com_userid,
					'cd_uname'=>$CcTvAi_com_username,
					'cd_icon'=>'favorites',
					'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);
				exit('upfav_001');
			}
		}else{
		exit('upfav_003');	
		}
	}
	if($CD_type=="dj") {
		$sql="select * from " . tname('dj') . " where CD_ID='" . $CD_ID . "'";
		if($row=$db->getrow($sql)) {
			$cd_ids=$db->getone("select cd_id from " . tname('fav') . " where cd_type='dj' and cd_uid=" . $CcTvAi_com_userid . " and cd_musicid='" . $CD_ID . "'");
			if($cd_ids) {
				exit('upfav_004');
			} else {
				$db->query("update " . tname('dj') . " set CD_FavHits=CD_FavHits+1 where CD_ID='" . $CD_ID . "'");
				$setarr=array(
					'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_type'=>'dj',
					'cd_addtime'=>time()
				);
				inserttable('fav', $setarr, 1);
				$setarrs=array(
					'cd_uid'=>$CcTvAi_com_userid,
					'cd_uname'=>$CcTvAi_com_username,
					'cd_icon'=>'favorites',
					'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);
				exit('upfav_001');
			}
		}else{
		exit('upfav_003');	
		}
	}
}
?>