<?php
define('CURSCRIPT', 'verify.php');
require_once './engine/common.inc.php';
if($action=='loginin'){
	if($gid!=4){
		showmsg('不能重复登录');
	}
	if($allowedauthcode{5}){
		if($authcode!=$loginauthcode){
			showmsg('验证码输入不正确');
		}
	}
	$login_userid=strtolower($login_userid);
	$memberformat=preg_match("/(^[a-z0-9_]+$)/", $login_userid);
	if(strlen($login_userid)>16 || strlen($login_userid)<4){
			$memberformat=false;
		}
	if(!$memberformat){
		showmsg("用户账号只能是字母、数字或下划线,且字符数大于3位小于16位");
	}
	$md5pwd=md5(trim($login_userpwd));
	if(strlen($login_userid)<3 || empty($login_userpwd)){
		showmsg('用户名,密码输入不正确');
	}
	$uptime=$timestamp-43200;
	$q=$db->query("SELECT * FROM g_members a , g_permission b , g_member_info c  WHERE a.userid='$login_userid' and a.userpwd='$md5pwd' and a.gid=b.gid and a.uid=c.uid and a.state = 0 LIMIT 1");


	if($userinfo=$db->fetch_array($q)){
		$db->query("DELETE FROM g_sessions WHERE uptime < $uptime");//删除一些所有的cookies
		$db->query("DELETE FROM g_sessions WHERE sid = '$sid' LIMIT 1");//删除登录网站时时自动生成的来宾cookies
		@SetCookie('sid','',$timestamp-3153600);//清除客户本机电脑上cookies
		@extract($userinfo);
		$logintimes++;
		$q=$db->query("select * from  g_msgs where recgid in ($gid,0) and sendtime < $timestamp and sendtime>$lastlogintime ");
		while($recmsg=$db->fetch_array($q)){
			$db->query("INSERT INTO g_member_msg ( mmid , uid , userid , title , content , sendtime , readed , senderid , sender , sendergid) VALUES (NULL, $uid, '$userid' , '{$recmsg['title']}' , '{$recmsg['content']}' , '{$recmsg['sendtime']}' , 0 , {$recmsg['senderid']} , {$recmsg['sender']} , {$recmsg['sendergid']})");
		}
		$db->query("update g_member_info set msgboxcount=(select count(*) from g_member_msg where uid = $uid) , lastlogintime =$timestamp , logintimes = $logintimes where uid = $uid");
		$str_pervalue=sprintf('%019b', $pervalue);
		$sid = random(6,0);
		@SetCookie('sid',$sid,$timestamp+43200);
		$db->query("INSERT INTO g_sessions ( sid , uid , userid , gid , pervalue , lastloginip , lastlogintime , uptime , newmsgcount ,adminlogin , authcode) VALUES ('$sid', $uid, '$userid', $gid , $pervalue , '$currentip' , $timestamp , $timestamp , (select count(*) from g_member_msg where uid=$uid and readed=0) , 0 , '')");
		$q=$db->query("SELECT * FROM g_sessions LIMIT 1");
		$_GSESSION=$db->fetch_array($q);
		$_GSESSION['pervalue']=$str_pervalue;
		showmsg("用户登陆成功","index.php");
	}else{
		$login_userpwd=substr($login_userpwd,0,3).'***';
		writelog("登录失败,".$login_userid." 登陆密码:".$login_userpwd);
		showmsg("登录失败，用户名或密码有误");
	}
}

if($action=='loginout'){
	@SetCookie('sid','',$timestamp-3153600);
	$db->query("DELETE FROM g_sessions WHERE sid = '$sid' LIMIT 1");
	unset($_GSESSION);
	$_GSESSION['sid']='';
	$_GSESSION['userid']='访客';
	$_GSESSION['gid']=4;
	showmsg("用户退出成功","index.php");
}

if($action=='adminloginout'){
	$db->query("update g_sessions set adminlogin=0 WHERE sid = '$sid' LIMIT 1");
	writelog($userid." 成功退出管理后台");
	showmsg("管理后台退出成功","index.php");
}

if($action=='panel'){
	$ipart=isset($ipart)?$ipart:'msglist';
	gheader('Location:panel.php?ipart='.$ipart);
}

if($action=='siteadmin'){
	if(!$uid||!$pervalue{18}){
		writelog($userid." 试图进入管理后台由于权限不足被阻止");
		showmsg("没有管理权限","index.php");
	}
	if(!$adminlogin){
		include($templatedir."adminlogin.tpl.php");
	}else{
	    header("location:siteadmin.php");
	}
}

if($action=="adminloginchecking"){
	$admin_userid=strtolower($admin_userid);
	$log_pwd=substr($admin_userpwd,0,3).'***';
	$md5pwd=md5($admin_userpwd);
	$q=$db->query("SELECT * FROM g_members WHERE userid='$admin_userid' and userpwd='$md5pwd' LIMIT 1");
	if($userinfo=$db->fetch_array($q)){
		$db->query("update g_sessions set adminlogin=1 WHERE sid = '$sid' LIMIT 1");
		writelog($userid." 成功登陆到管理后台");
		header("location:siteadmin.php");
	}else{
		writelog("登录失败 ".$admin_userid." 登陆密码:".$log_pwd);
		showmsg("登陆验证失败，请重新登录","verify.php?action=siteadmin");
	}
}


