<?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=="friendAdd") { //提交关注
	global $db;
	$CD_ID=SafeRequest("id", "get");
	if(!IsNumID($CD_ID)) {exit('10004');}
	if(!$userlogined==true) {exit('10001');}
	if ($CcTvAi_com_userid==$CD_ID) {exit('10002');}
	$sql="select cd_id,cd_name from " . tname('user') . " where cd_lock=0 and cd_id='$CD_ID'";
	if ($user=$db->getrow($sql)) {
		$cd_cfriend=$db->getone("select cd_id from " . tname('friend') . " where cd_uid='$CcTvAi_com_userid' and cd_uids='".$user["cd_id"]."'");//查询是否已关注
		if ($cd_cfriend) {exit('10003');}//已关注
		$cd_idb=$db->getone("select cd_id from " . tname('friend') . " where cd_uid='".$user["cd_id"]."' and cd_uids='$CcTvAi_com_userid'");
			if ($cd_idb) {
				$db->query("update " . tname('friend') . " set cd_lock=1 where cd_uid='".$user["cd_id"]."' and cd_uids='$CcTvAi_com_userid'");
				$cd_lock=1;
			} else {
				$cd_lock=0;
			}
			
			//入库
			$setarr=array(
				'cd_uid' => $CcTvAi_com_userid,
				'cd_uname' => $CcTvAi_com_username,
				'cd_uids' => $user["cd_id"],
				'cd_unames' => $user["cd_name"],
				'cd_lock' => $cd_lock,
				'cd_note' => '',
				'cd_group' => 0,
				'cd_hidden' => 0,
				'cd_addtime' => date('Y-m-d H:i:s')
			);
			inserttable('friend', $setarr, 1);
			$db->query("update " . tname('user') . " set cd_ufans=cd_ufans+1 where cd_id='".$user["cd_id"]."'");
			$db->query("update " . tname('user') . " set cd_ufav=cd_ufav+1 where cd_id='$CcTvAi_com_userid'");
			//检测是否已提交通知
			$query="select cd_id from " . tname('notice') . " where cd_icon='fans' and cd_uid='$CcTvAi_com_userid' and cd_uids='".$user["cd_id"]."'";
			if ($rows=$db->getrow($query)) {
				$db->query("update " . tname('notice') . " set cd_addtime='" . date('Y-m-d H:i:s') . "' where cd_id='" . $rows['cd_id'] . "'");
			} else {
				//入库
				$setarr=array(
					'cd_uid' => $CcTvAi_com_userid,
					'cd_uname' => $CcTvAi_com_username,
					'cd_uids' => $user["cd_id"],
					'cd_unames' => $user["cd_name"],
					'cd_icon' => 'fans',
					'cd_data' => '关注了您&nbsp;<a href="' . linkweburl($CcTvAi_com_userid, $CcTvAi_com_username) . '" target="_blank">立即查看</a>',
					'cd_dataid' => 0,
					'cd_state' => 1,
					'cd_addtime' => date('Y-m-d H:i:s')
				);
				inserttable('notice', $setarr, 1);
			}
			exit('10005');

	}else{
		exit('10004');
	}
}elseif($ac=="friendDel") { //取消关注
	global $db;
	$CD_ID=SafeRequest("id", "get");
	if(!IsNumID($CD_ID)) {exit('10004');}
	if(!$userlogined==true) {exit('10001');}
	if ($CcTvAi_com_userid==$CD_ID) {exit('10002');}
	$sql="select cd_id,cd_uids,cd_unames from " . tname('friend') . " where cd_uid='$CcTvAi_com_userid' and cd_uids='$CD_ID'";
	if ($row=$db->getrow($sql)) {
		$db->query("delete from " . tname('friend') . " where cd_id='" . $row['cd_id'] . "'");
		$db->query("update " . tname('friend') . " set cd_lock=0 where cd_uid='$cd_uid' and cd_uids='$CcTvAi_com_userid'");
		$db->query("update " . tname('user') . " set cd_ufans=cd_ufans-1 where cd_ufans>=1 and cd_id='$CD_ID'");
		$db->query("update " . tname('user') . " set cd_ufav=cd_ufav-1 where cd_ufav>=1 and cd_id='$CcTvAi_com_userid'");
		//提交通知
		$setarr=array(
			'cd_uid' => $CcTvAi_com_userid,
			'cd_uname' => $CcTvAi_com_username,
			'cd_uids' => $row['cd_uids'],
			'cd_unames' => $row['cd_unames'],
			'cd_icon' => 'fans',
			'cd_data' => '已取消对您的关注&nbsp;<a href="' . linkweburl($CcTvAi_com_userid, $CcTvAi_com_username) . '" target="_blank">立即查看</a>',
			'cd_dataid' => 0,
			'cd_state' => 1,
			'cd_addtime' => date('Y-m-d H:i:s')
		);
		inserttable('notice', $setarr, 1);
		exit('10005');
	}else{
		exit('10003');
	}
	
	

}
?>