<?php
define('THIS_SCRIPT', 'login');
require_once 'global.php';

$lang = load_lang($lang, 'login' );
$stats = $lang['logintitle'];
if(($passport['enabledpassportclient']=='1')&&($_GET['is_rpc']!=1)){
(substr($passport['serveraddress'],-1,1)!="/") &&($passport['serveraddress'].="/");
if($action=='logout'){
($passport['serverlogout']{0}=="/") &&($passport['serverlogout']=substr($passport['serverlogout'],1));
$jumpurl=$passport['serveraddress'].$passport['serverlogout'];
}else{
($passport['serverlogin']{0}=="/") &&($passport['serverlogin']=substr($passport['serverlogin'],1));
$jumpurl=$passport['serveraddress'].$passport['serverlogin'];
}
$jumpurl.=(false!==strpos($jumpurl,'?')?'&':'?')."comeurl=".rawurldecode($comeurl);
header("location:{$jumpurl}");
exit;
}

if ($action=='chk'){
	$arrNavMenu = array(
	0=>array('linktitle'=>$lang['logintitle'],'linkurl'=>'login.php')
	,1=>array('linktitle'=>$lang['loginsuccess'])
	);
	head(1,0,0,$arrNavMenu);
	check_login();
}elseif($action=='logout'){
	login_out();
}else{
	$arrNavMenu = array(
	0=>array('linktitle'=>$lang['logintitle'],'linkurl'=>'login.php')
	,1=>array('linktitle'=>$lang['logininput'])
	);
	head(1,0,0,$arrNavMenu);
	login_main();
}
footer();
function login_main(){
	global $fsetting,$lang,$f,$comeurl;
	require_once printout('login');
}
function check_login(){
	global $fsetting,$lang,$db,$username,$password,$getcode,$cookiedate,$userhidden,$comeurl,$dv,$userinfo,$passport,$action,$usertrueip,$statuserid;
	$member = array();
	$group = array();
	$FoundGrade = false;
	$ChkUserLogin = false;
	if ( $username == '' ){
		showmsg($lang['login_msg_name']);
	}
	if ( $password == '' ){
		showmsg($lang['login_msg_pass']);
	}
	if ( $fsetting['logingetcode'] == 1 && ($userinfo['getcode'] == '' || $userinfo['getcode'] != $getcode) ){
		showmsg($lang['login_msg_getcode']);
	}
	$password=md5($password);

	$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 ){
		$psysusername=strval($db->scalar("select adduser from {$dv}admin where adduser='$RegName'"));
		$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='$userhidden' where userid=$userid");
		}else{
			$forum_user = explode(',',$fsetting['userpoint']);
			$db->query("update {$dv}user set userwealth=userwealth+".intval($forum_user[4]).",userep=userep+".intval($forum_user[9]).",usercp=usercp+".intval($forum_user[14]).",lastlogin='".time()."',userlogins=userlogins+1,userlastip='$usertrueip',userclass='$UserClass',titlepic='$TitlePic',usergroupid='$GroupID',truepassword='$truepassword',userhidden='$userhidden' 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);
		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']);
		set_cookie("psysusername",$psysusername,$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']);
		set_cookie('lastuserposttime', false);

		if($passport['enabledpassportserver']=='1'){
			global $action;
			include(ROOT_PATH."passport/{$passport['colientsystem']}.php");
			header("Location:{$url}");
			exit;
		}
        
		$leftbar = isset($_COOKIE[$cookieprename.'setleftbar']) ? $_COOKIE[$cookieprename.'setleftbar'] : $fsetting['leftbar'];
		if($leftbar) {
			echo "<script language=\"JavaScript\">top.location.href=\"".$fsetting['homepage']."?targeturl=".urlencode($comeurl)."\"</script>";
			exit;
		}

		showmsg($lang['login_msg_suc'],1,$comeurl);
	}
}
function login_out(){
	global $fsetting,$f,$dv,$db,$userid, $_GET, $action,$passport;
	set_cookie('userid', false);
	set_cookie('userid', false, -86400 * 365);
	set_cookie('username', false);
	set_cookie('username', false, -86400 * 365);
	set_cookie('psysusername', false);
	set_cookie('psysusername', false, -86400 * 365);//add by hj
	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', '');
	set_cookie('lastuserposttime', false);
	set_cookie('msghide', '');
	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($passport['enabledpassportserver']=='1'){
		include(ROOT_PATH."passport/{$passport['colientsystem']}.php");
		header("Location:{$url}");
		exit;
	}
	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;
	}
}
?>