<?php
//--------------- 登錄函數 ---------------

//登錄
function qlogin($add){
	global $empire,$dbtbpre,$public_r,$ecms_config,$level_r;
	if($ecms_config['member']['loginurl'])
	{
		Header("Location:".$ecms_config['member']['loginurl']);
		exit();
	}
	$dopr=1;
	if($_POST['prtype'])
	{
		$dopr=9;
	}
	$username=trim($add['username']);
	$password=trim($add['password']);
	if(!$username||!$password)
	{
		printerror("EmptyLogin","history.go(-1)",$dopr);
	}
	$tobind=(int)$add['tobind'];
	//驗證碼
	$keyvname='checkloginkey';
	if($public_r['loginkey_ok'])
	{
		ecmsCheckShowKey($keyvname,$add['key'],$dopr);
	}
	$username=RepPostVar($username);
	$password=RepPostVar($password);
	$num=0;
	$r=$empire->fetch1("select ".eReturnSelectMemberF('*')." from ".eReturnMemberTable()." where ".egetmf('username')."='$username'".do_dblimit_one());
	if(!$r['userid'])
	{
		printerror("FailPassword","history.go(-1)",$dopr);
	}
	//默認會員組
	if(empty($r['groupid']))
	{
		$r['groupid']=eReturnMemberDefGroupid();
	}
	$r['groupid']=(int)$r['groupid'];
	if($level_r[$r['groupid']]['loginps'])
	{
		if('dg'.$level_r[$r['groupid']]['loginps']!='dg'.$add['mg_loginps'])
		{
			printerror('FailMgSetPs','',$dopr);
		}
	}
	if(!eDoCkMemberPw($password,$r['password'],$r['salt']))
	{
		printerror("FailPassword","history.go(-1)",$dopr);
	}
	if($r['checked']==0)
	{
		if($public_r['regacttype']==1)
		{
			printerror('NotCheckedUser','../member/register/regsend.php',$dopr);
		}
		else
		{
			printerror('NotCheckedUser','',$dopr);
		}
	}
	//綁定帳號
	if($tobind)
	{
		MemberConnect_BindUser($r['userid']);
	}
	$rnd=make_password(EcmsRandInt(20,36));//取得隨機密碼
	$rndt=make_password(EcmsRandInt(20,32));//取得隨機密碼2
	$lasttime=time();
	//IP
	$lastip=egetip();
	$eipf=egetipfrom();
	$lastipport=egetipport();
	//更新rnd
	$uprndstr=eReturnDoUpRndf('login',$rnd,$rndt);
	if(!$uprndstr)
	{
		$rnd=$r['rnd'];
		$rndt=$r['rndt'];
	}
	$usql=$empire->query("update ".eReturnMemberTable()." set ".$uprndstr.egetmf('groupid')."='".$r['groupid']."',".egetmf('isot')."=1 where ".egetmf('userid')."='".$r['userid']."'");
	$empire->query("update {$dbtbpre}enewsmemberadd set lasttime='$lasttime',lastip='$lastip',loginnum=loginnum+1,lastipport='$lastipport',eipf='$eipf' where userid='".$r['userid']."'");
	//設置cookie
	$lifetime=(int)$add['lifetime'];
	$logincookie=0;
	if($lifetime)
	{
		$logincookie=time()+$lifetime;
	}
	$set1=esetcookie("mlusername",$username,$logincookie);
	$set2=esetcookie("mluserid",$r['userid'],$logincookie);
	$set3=esetcookie("mlgroupid",$r['groupid'],$logincookie);
	$set4=esetcookie("mlrnd",$rnd,$logincookie);
	$set5=esetcookie("mlrndt",$rndt,$logincookie);
	//驗證符
	qGetLoginAuthstr($r['userid'],$username,$rnd,$r['groupid'],$logincookie);
	//登錄附加cookie
	AddLoginCookie($r);
	$location="../member/cp/";
	$returnurl=getcvar('returnurl');
	if($returnurl)
	{
		$location=CheckCanUseDm($returnurl);
	}
	if(strstr($_SERVER['HTTP_REFERER'],"e/member/iframe"))
	{
		$location="../member/iframe/";
	}
	if(strstr($location,"enews=exit")||strstr($location,"e/member/register")||strstr($_SERVER['HTTP_REFERER'],"e/member/register"))
	{
		$location="../member/cp/";
		$_POST['ecmsfrom']='';
	}
	ecmsEmptyShowKey($keyvname);//清空驗證碼
	$set6=esetcookie("returnurl","");
	if($set1&&$set2)
	{
		//易通行系統
		DoEpassport('login',$r['userid'],$username,$password,$r['salt'],$r['email'],$r['groupid'],$r['registertime']);
		$location=DoingReturnUrl($location,$_POST['ecmsfrom']);
		printerror("LoginSuccess",$location,$dopr);
    }
	else
	{
		printerror("NotCookie","history.go(-1)",$dopr);
	}
}

//退出登陸
function qloginout($userid,$username,$rnd){
	global $empire,$public_r,$ecms_config;
	//是否登陸
	$user_r=islogin();
	if($ecms_config['member']['quiturl'])
	{
		Header("Location:".$ecms_config['member']['quiturl']);
		exit();
	}
	$user_r['userid']=(int)$user_r['userid'];
	$rnd=make_password(EcmsRandInt(20,36));//產生隨機密碼
	$rndt=make_password(EcmsRandInt(20,32));//取得隨機密碼2
	//更新rnd
	$uprndstr=eReturnDoUpRndf('loginout',$rnd,$rndt);
	if($uprndstr)
	{
		$sql=$empire->query("update ".eReturnMemberTable()." set ".$uprndstr.egetmf('isot')."=0 where ".egetmf('userid')."='".$user_r['userid']."'");
	}
	EmptyEcmsCookie();
	$dopr=1;
	if($_GET['prtype'])
	{
		$dopr=9;
	}
	$gotourl="../../";
	if(strstr($_SERVER['HTTP_REFERER'],"e/member/iframe"))
	{
		$gotourl=$public_r['newsurl']."e/member/iframe/";
	}
	//易通行系統
	DoEpassport('logout',$user_r['userid'],$user_r['username'],'','','','','');
	$gotourl=DoingReturnUrl($gotourl,$_GET['ecmsfrom']);
	printerror("ExitSuccess",$gotourl,$dopr);
}
?>