<?php
error_reporting(0);
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");
include "../../include/cctvai.conn.php";
include "../../include/cctvai.inc.php";
include "../../plug/qqhl/conf.inc.php";
if(!eregi($_SERVER['SERVER_NAME'], $_SERVER['HTTP_REFERER'])) {
	exit();
}
$ac=SafeRequest("action", "get");
if($ac=="login") {
	if($userlogined==true) {
		$showstr='
<li class=lei_index><a href="' . linkweburl($CcTvAi_com_userid, $CcTvAi_com_username) . '" target="_blank">' . getlen(1, 7, $CcTvAi_com_nicheng) . '</a></li> 
<li><a href="' . cd_webpath . 'user/space.php?do=upmusic&view=list" target="_blank">我的音乐</a></li>
<li><a href="' . cd_webpath . 'user/space.php?do=upvideo&view=list" target="_blank">我的视频</a></li> 
<li><a href="' . cd_webpath . 'user/space.php?do=profile" target="_blank">设置</a></li> 
<li><a href="javascript:void(0);" onclick="cctvai_logout();">注销</a></li>
';
	} else {
		if(qqapp_open=="yes") {$qq='<li><a href="'.cd_webpath.'plug/qqhl/" target="_blank" class=lei_qq></a></li>';}
		$showstr='
<input type="text" id="cd_name" name="cd_name">
<input class="pas" type="password" id="cd_pass" name="cd_pass">
<input type="submit" class="dlbtn" value="" onclick="cctvai_logadd();"/>
<li><a href="'.cd_webpath.'user/reg.php" target="_blank">注册</a></li>
<li><a href="'.cd_webpath.'user/lostpasswd.php" target="_blank">忘记密码</a></li>' . $qq . '
';
	}
	echo $showstr;
} elseif($ac=="dologin") {
	$callback=SafeRequest("callback", "get");
	if(cd_userlogin=="no") {
		exit($callback . '({"error":10009})');
	}
	$cd_username=unescape(SafeRequest("name", "get"));
	$cd_password=SafeRequest("pass", "get");
	$cd_logintime=date('Y-m-d H:i:s');
	$cd_loginip=getonlineip();
	$cookietime=cd_userlogintime;
	$cd_password=substr(md5($cd_password), 8, 16);
	if($cd_username=="") {
		exit($callback . '({"error":10001})');
	}
	if($cd_password=="") {
		exit($callback . '({"error":10002})');
	}
	global $db;
	$sql="select cd_id from " . tname('user') . " where cd_name='" . $cd_username . "' and cd_password='" . $cd_password . "'";
	$cd_id=$db->Getone($sql);
	if(!IsNumID($cd_id)) {
		exit();
	}
	if($cd_id) {
		$row=$db->Getrow("select cd_id,cd_lock,cd_name,cd_password,cd_vipgrade from " . tname('user') . " where cd_id='" . $cd_id . "'");
		if($row["cd_lock"]==1) {
			exit($callback . '({"error":10005})');
		} elseif($row["cd_lock"]==2) {
			exit($callback . '({"error":10007})');
		} else {
			$cd_ids=$db->Getone("select cd_id from " . tname('session') . " where cd_uid='" . $cd_id . "'");
			if($cd_ids) {
				updatetable('session', array(
					'cd_logintime'=>time()
				), array(
					'cd_id'=>$cd_ids
				));
			} else {
				$setarr=array(
					'cd_uid'=>$cd_id,
					'cd_uname'=>$cd_username,
					'cd_uip'=>$cd_loginip,
					'cd_logintime'=>time()
				);
				inserttable('session', $setarr, 1);
			}
			if($db->getone("select cd_id from " . tname('user') . " where DateDiff(DATE(cd_logintime),'" . date('Y-m-d') . "')=0 and cd_id='" . $cd_id . "'")) {
				$db->query("update " . tname('user') . " set cd_loginnum=cd_loginnum+'1',cd_loginip='" . $cd_loginip . "',cd_logintime='" . $cd_logintime . "' where cd_id=cd_id='" . $cd_id . "'");
			} else {
				if(cd_userloginpoints>=1) {
					$setarr=array(
						'cd_type'=>1,
						'cd_uid'=>$cd_id,
						'cd_uname'=>$cd_username,
						'cd_icon'=>'app',
						'cd_title'=>'每日登录',
						'cd_points'=>cd_userloginpoints,
						'cd_state'=>0,
						'cd_addtime'=>date('Y-m-d H:i:s'),
						'cd_endtime'=>getendtime()
					);
					inserttable('bill', $setarr, 1);
				}
				if($row["cd_vipgrade"]==1) {
					$cd_speed=5;
				} elseif($row["cd_vipgrade"]==2) {
					$cd_speed=10;
				} else {
					$cd_speed=0;
				}
				$db->query("update " . tname('user') . " set cd_points=cd_points+'" . cd_userloginpoints . "',cd_rank=cd_rank+'" . cd_userloginrank . "',cd_viprank=cd_viprank+'" . $cd_speed . "',cd_loginnum=cd_loginnum+'1',cd_loginip='" . $cd_loginip . "',cd_logintime='" . $cd_logintime . "' where cd_id='" . $cd_id . "'");
			}
			setcookie("cd_id", $row["cd_id"], time()+$cookietime, cd_cookiepath);
			setcookie("cd_name", $row["cd_name"], time()+$cookietime, cd_cookiepath);
			setcookie("cd_password", $row["cd_password"], time()+$cookietime, cd_cookiepath);
			$_SESSION["codes"]=substr(md5(date('' . rand(1, 9) . 'msds' . rand(1, 9) . 'Hs' . rand(1, 9) . 's')), 8, 16);
			exit($callback . '({"error":10008})');
		}
	} else {
		exit($callback . '({"error":10003})');
	}
} elseif($ac=="logout") {
	$callback=SafeRequest("callback", "get");
	global $db;
	if(!IsNumID($CcTvAi_com_userid)) {
		exit();
	}
	if(!$userlogined) {
		exit($callback . '({"error":10001})');
	}
	$db->query("delete from " . tname('session') . " where cd_uid=$CcTvAi_com_userid");
	setcookie("cd_id", "", time()-1, cd_cookiepath);
	setcookie("cd_name", "", time()-1, cd_cookiepath);
	setcookie("cd_password", "", time()-1, cd_cookiepath);
	exit($callback . '({"error":10001})');
}
//获取在线IP
function getonlineip($format = 0) {
	global $_SGLOBAL;

	if(empty($_SGLOBAL['onlineip'])) {
		if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
			$onlineip=getenv('HTTP_CLIENT_IP');
		} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
			$onlineip=getenv('HTTP_X_FORWARDED_FOR');
		} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
			$onlineip=getenv('REMOTE_ADDR');
		} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
			$onlineip=$_SERVER['REMOTE_ADDR'];
		}
		preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches);
		$_SGLOBAL['onlineip']=$onlineipmatches[0] ? $onlineipmatches[0] : 'unknown';
	}
	if($format) {
		$ips=explode('.', $_SGLOBAL['onlineip']);
		for($i=0; $i<3; $i++) {
			$ips[$i]=intval($ips[$i]);
		}
		return sprintf('%03d%03d%03d', $ips[0], $ips[1], $ips[2]);
	} else {
		return $_SGLOBAL['onlineip'];
	}
}
?>