<?php
define('THIS_SCRIPT', 'passport_client');
require_once 'global.php';
if(ISDVBBS!==true){
	header("http/1.0 404 Not Found");
	exit;
}
head();
$lang = load_lang($lang, 'passport_client' );
if($passport['enabledpassportclient']!=1){
	showmsg($lang['passport.notclientpassport']);
	exit;
}
parse_str($_SERVER['QUERY_STRING'],$remotepass);
$pass_verify="";
empty($remotepass['comeurl']) AND ($remotepass['comeurl']=$remotepass['forward']);
$pass_verify= md5("{$remotepass['action']}{$remotepass['userinfo']}{$remotepass['comeurl']}{$passport['passport_pkey']}");
if($pass_verify==""||$pass_verify!==$remotepass['verify']){
	header("http/1.0 404 Not Found");
	showmsg($lang['passport.lackdata']);
	exit;
}
$key	= md5($passport['passport_pkey']);
parse_str(encodexor(base64_decode($remotepass['userinfo']),$key),$rpc_userinfo);
(empty($rpc_userinfo['userpost'])) AND ($rpc_userinfo['userpost']=0);
(empty($rpc_userinfo['usertopic'])) AND ($rpc_userinfo['usertopic']=0);
(empty($rpc_userinfo['lockuser'])) AND ($rpc_userinfo['lockuser']=0);
(empty($rpc_userinfo['userpower'])) AND ($rpc_userinfo['userpower']=0);
(empty($rpc_userinfo['userdel'])) AND ($rpc_userinfo['userdel']=0);
(empty($rpc_userinfo['userisbest'])) AND ($rpc_userinfo['userisbest']=0);
(empty($rpc_userinfo['usermoney'])) AND ($rpc_userinfo['usermoney']=0);
(empty($rpc_userinfo['userticket'])) AND ($rpc_userinfo['userticket']=0);
(empty($rpc_userinfo['ischallenge'])) AND ($rpc_userinfo['ischallenge']=0);
(empty($rpc_userinfo['userwealth'])) AND ($rpc_userinfo['userwealth']=0);
(empty($rpc_userinfo['userface'])) AND ($rpc_userinfo['userface']='images/userface/image1.gif');
(empty($rpc_userinfo['userfacewidth'])) AND ($rpc_userinfo['userfacewidth']='32');
(empty($rpc_userinfo['userfaceheight'])) AND ($rpc_userinfo['userfaceheight']='32');
$cookiedate=intval($rpc_userinfo['cookiedate']);
$userhidden=intval($rpc_userinfo['userhidden'])===1?1:0;
if($remotepass['action']=="login"){
	check_login($rpc_userinfo['username'],$rpc_userinfo['password'],true);
}elseif($remotepass['action']=="logout"){
	login_out();
}

function encodexor($string,$key)
{
	$len	= strlen($key);
	$code	= '';
	for($i=0; $i<strlen($string); $i++)
	{
		$k		= $i % $len;
		$code  .= $string[$i] ^ $key[$k];
	}
	return $code;
}

function check_login($username,$password,$regifnoexists=false){
	global $fsetting,$lang,$db,$cookiedate,$forum_user,$remotepass,$comeurl,$rpc_userinfo,$dv,$userinfo,$usertrueip,$statuserid;
	$comeurl=$remotepass['comeurl']?$remotepass['comeurl']:$comeurl;
	$member = array();
	$group = array();
	$FoundGrade = false;
	$ChkUserLogin = false;
	if ( $username == ''|| $password == '' ){
		showmsg($lang['passport.lackdata']);
	}
	$query=$db->query("Select userid,username,userpassword,userpost,userclass,lockuser,userhidden,joindate,usergroupid,titlepic,truepassword,lastlogin,usertitle From {$dv}user Where username='$username'");
	if($db->affected_rows()==0&&$regifnoexists===true){
		$truepassword = Createpass(16);
		$userim = checkreal($rpc_userinfo['homepage']). '|||' .$rpc_userinfo['oicq']. '|||' .checkreal($rpc_userinfo['icq']). '|||' .checkreal($rpc_userinfo['msn']). '|||' .checkreal($rpc_userinfo['yahoo']). '|||' .checkreal($rpc_userinfo['aim']). '|||' .checkreal($rpc_userinfo['uc']);
		$sex=$rpc_userinfo['sex']==0?0:1;
		$UserLastLogin=time();
		$userjointime=$rpc_userinfo['userjointime']?$rpc_userinfo['userjointime']:$UserLastLogin;
		$userinfo = checkreal($rpc_userinfo['realname']). '|||' .$rpc_userinfo['character']. '|||' .checkreal($rpc_userinfo['personal']). '|||' .checkreal($rpc_userinfo['country']). '|||' .checkreal($rpc_userinfo['province']). '|||' .checkreal($rpc_userinfo['city']). '|||' .checkreal($rpc_userinfo['shengxiao']). '|||' .checkreal($rpc_userinfo['blood']). '|||' .checkreal($rpc_userinfo['belief']). '|||' .checkreal($rpc_userinfo['occupation']). '|||' .checkreal($rpc_userinfo['marital']). '|||' .checkreal($rpc_userinfo['education']). '|||' .checkreal($rpc_userinfo['college']). '|||' .checkreal($rpc_userinfo['userphone']). '|||' .checkreal($rpc_userinfo['address']);
		$showre = $rpc_userinfo['showre'] == 0 ?0:1;
		$usersetting = checkreal($rpc_userinfo['setuserinfo']). '|||' .checkreal($rpc_userinfo['setusertrue']). '|||' .$showre;

		$db->query("Insert Into {$dv}user (username,userpassword,useremail,userclass,titlepic,userquesion,useranswer,truepassword,userim,usersign,userpost,usertopic,lockuser,usersex,userbirthday,joindate,userface,userwidth,userheight,usertoday,userlogins,lastlogin,userwealth,userep,usercp,userinfo,usersetting,userpower,userdel,userisbest,usermoney,userticket,userfav,ischallenge,userhidden,userlastip) Values ('$username','$password','{$rpc_userinfo['email']}','{$rpc_userinfo['userclass']}','{$rpc_userinfo['titlepic']}','{$rpc_userinfo['question']}','{$rpc_userinfo['answer']}','$truepassword','$userim','$rpc_userinfo[usersign]',$rpc_userinfo[userpost],$rpc_userinfo[usertopic],{$rpc_userinfo['lockuser']},'$sex','{$rpc_userinfo['birthday']}','$userjointime','{$rpc_userinfo['userface']}','{$rpc_userinfo['userfacewidth']}','{$rpc_userinfo['userfaceheight']}','0|0|0|0|0',1,'$UserLastLogin',{$rpc_userinfo['userwealth']},'$forum_user[5]','$forum_user[10]','$userinfo','$usersetting',{$rpc_userinfo['userpower']},{$rpc_userinfo['userdel']},{$rpc_userinfo['userisbest']},{$rpc_userinfo['usermoney']},{$rpc_userinfo['userticket']},'$lang[strings24]',{$rpc_userinfo['ischallenge']},2,'$GLOBALS[usertrueip]')");

		$query=$db->query("Select userid,username,userpassword,userpost,userclass,lockuser,userhidden,joindate,usergroupid,titlepic,truepassword,lastlogin,usertitle From {$dv}user Where username='$username'");
		
	}
	if ($member = $db->fetch_array($query)){
		if ( $member['lockuser'] == 1 || $member['usergroupid'] == 5 ){
			showmsg($lang['login_msg_lock']);
			exit;
		}else{
			if ( $member['userpassword'] == $password || $member['userpassword'] == substr($password,8,16) ){
				$ChkUserLogin = true;
				$userid = $member['userid'];
				$RegName = $member['username'];
				$Article = $member['userpost'];
				$UserLastLogin = $member['lastlogin'];
				$UserClass = $member['userclass'];
				$GroupID = $member['usergroupid'];
				$TitlePic = $member['usertitle'];
				$joindate = $member['joindate'];
				$truepassword = Createpass(16);
				if ( $Article < 0 ){$Article = 0;}
			}else{
				showmsg($lang['login_msg_none']);
				exit;
			}
		}
	}else{
		showmsg($lang['login_msg_none']);
		exit;
	}
	if ( $ChkUserLogin ){
		$query=$db->query("Select minarticle,issetting,parentgid,usertitle,grouppic From {$dv}usergroups Where usergroupid=$GroupID");
		if ( $group = $db->fetch_array($query) ){
			if ( $group['parentgid'] == 1 || $group['parentgid'] == 2 || $group['parentgid'] == 4 || $group['parentgid'] == 5 ){
				$UserClass = $group['usertitle'];
				$TitlePic = $group['grouppic'];
				$FoundGrade = true;
			}
		}
		if ( !$FoundGrade ){
			$query=$db->query("Select usertitle,grouppic,usergroupid From {$dv}usergroups Where parentgid=3 And minarticle<=$Article Order By minarticle Desc,usergroupid",array('pageSize'=>1));
			if ( $group = $db->fetch_array($query) ){
				$UserClass = $group['usertitle'];
				$TitlePic = $group['grouppic'];
				$GroupID = $group['usergroupid'];
				$FoundGrade = true;
			}
		}
		if ( !$FoundGrade ){
			showmsg($lang['login_msg_group']);
			exit;
		}
		if ( dateDiff('d',TIME_NOW,$UserLastLogin) == 0 ){
			$db->query("update {$dv}user set lastlogin='".TIME_NOW."',userlogins=userlogins+1,userlastip='$usertrueip',userclass='$UserClass',titlepic='$TitlePic',usergroupid='$GroupID',truepassword='$truepassword',userhidden='2' where userid=$userid");
		}else{
			$forum_user = explode(',',$fsetting['userpoint']);
			$db->query("update {$dv}user set lastlogin='".TIME_NOW."',userlogins=userlogins+1,userlastip='$usertrueip',truepassword='$truepassword',userhidden='2' where userid=$userid");
		}
		if ( $userinfo['username'] == '' ){
			$db->query("Delete From {$dv}online Where id='$statuserid'");
		}else{
			if ( $userinfo['username'] != $username ){
				$db->query("Delete From {$dv}online Where username='$userinfo[username]'");
			}
		}

		$comeurl = strtolower($comeurl);
		if ( $comeurl == '' || strpos($comeurl,'login.php') === true || strpos($comeurl,'reg.php') === true ){
			$comeurl = $fsetting['homepage'];
		}
		cookie_destory();	//	清除所有的 COOKIES
		$tmpdatenow = gmdate('Y-m-d H:i:s');
		set_cookie('cometime',$tmpdatenow,$GLOBALS['cookiedate']);
		set_cookie('activetime',dateAdd('s',-3600,$tmpdatenow));
		set_cookie('cboardid',0,$GLOBALS['cookiedate']);
		$GLOBALS['_COOKIE_USERID'] = $userid;
		set_cookie("userid",$userid,$GLOBALS['cookiedate']);
		$GLOBALS['_COOKIE_USERNAME'] = $RegName;
		set_cookie("username",$RegName,$GLOBALS['cookiedate']);
		$GLOBALS['_COOKIE_USERPASSWD'] = $truepassword;
		set_cookie("password",$truepassword,$GLOBALS['cookiedate']);
		set_cookie("userhidden",$userhidden,$GLOBALS['cookiedate']);
		set_cookie("joindate", gmdate('Y-m-d H:i:s',$joindate),$GLOBALS['cookiedate']);
		set_cookie('onlinecachetime', '',$GLOBALS['cookiedate']);
		set_cookie('selected_cookiedate', '');
		set_cookie('logout_id', dv_md5(uniqid(rand(), true)), $GLOBALS['cookiedate']);
		set_cookie('selected_cookiedate', intval($GLOBALS['cookiedate']), $GLOBALS['cookiedate']);
		$leftbar = isset($_COOKIE[$cookieprename.'setleftbar']) ? $_COOKIE[$cookieprename.'setleftbar'] : $fsetting['leftbar'];
		header("Location:{$comeurl}");
		exit;
	}
}
function login_out(){
	global $fsetting,$f,$dv,$db,$userid, $_GET;
	set_cookie('userid', false);
	set_cookie('userid', false, -86400 * 365);
	set_cookie('username', false);
	set_cookie('username', false, -86400 * 365);
	set_cookie('password', false);
	set_cookie('password', false, -86400 * 365);
	set_cookie('userhidden', false);
	set_cookie('userhidden', false);
	set_cookie('joindate', false);
	set_cookie('joindate', false);
	set_cookie('onlinecachetime', '');
	if ($userid > 0) {
		$db->query("DELETE FROM {$dv}online WHERE id='{$userid}'");
		$db->query("UPDATE {$dv}user SET userhidden=1 WHERE userid='{$userid}'");
	}

	if ( $f != '' ){
		$comeurl = $f;
	}elseif( $_SERVER['HTTP_REFERER'] != '' ){
		$comeurl = $_SERVER['HTTP_REFERER'];
	}else{
		$comeurl = $fsetting['homepage'];
	}
	empty($comeurl) AND ($comeurl = $fsetting['homepage']);
	$leftbar = isset($_COOKIE[$cookieprename.'setleftbar']) ? $_COOKIE[$cookieprename.'setleftbar'] : $fsetting['leftbar'];
	cookie_destory();	//清除所有的 COOKIES
	if($leftbar) {
		echo '<script type="text/JavaScript">top.location.href="'.$fsetting['homepage'].'?targeturl='.urlencode($comeurl).'";</script>';
		exit;
	} else {
		ob_end_clean();
		header("Location:".$comeurl);
		exit;
	}
}
function checkreal($str){
	if (is_null($str)){return;}
	$str = str_replace('|','',$str);
	return $str;
}
?>