<?php
if(!empty($_GET["action"]))
	$post=$_GET;
else
	$post=$_POST;

if(!empty($_GET['forward'])&&strpos($_GET['forward'],'script')>0)
	header("Location:login.php");//安全检查

if(!empty($post["action"])&&$post["action"]=="submit")
{
	include_once("includes/global.php");
	include_once("includes/smarty_config.php");
	include_once("config/reg_config.php");
	if(strtolower($_SESSION["auth"])!=strtolower($post["randcode"])&&empty($post['first_index'])&&empty($post['connect_id']))
	{
		header("Location: login.php?erry=-3");//验证码错误
		exit();
	} 
	$config = array_merge($config,$reg_config);
	if($config['openbbs']==2)
	{	//ucenter1.5 login
		$sql="select userid,user,password,email from ".ALLUSER." a where user='$post[user]' or email='$post[user]'";
		$db->query($sql);
		$re=$db->fetchRow();//bb用户是否存在
		if(!empty($re['password']))
		{
			if(substr($re['password'],0,4)=='lock')
				msg("login.php?erry=-4&connect_id=$post[connect_id]");//之前使用了找回密码功能，账户被锁定
			if($re['password']!=md5($post['password']))
				msg("login.php?erry=-2&connect_id=$post[connect_id]");//密码错误
		}
		include_once('uc_client/client.php');
		list($uid, $username, $password, $email) = uc_user_login($post['user'], $post['password']);//uc是否存在
		if($uid>0||$re["userid"])
		{	//如果uc或者BB之中有一个账户是正确的执行如下操作
		
			if($uid<=0&&$re["userid"]>0)//UC不存在ＢＢ存在
			{
				$uid = uc_user_register($re['user'], $post['password'], $re['email']);
				login($re['userid'],$re['user']);
			}
			elseif($uid>0&&$re["userid"]<=0)//UC存在BB不存在
			{
				$dbc=new dba($config['dbhost'],$config['dbuser'],$config['dbpass'],$config['dbname'],$config['dbport']);
				
				$ip=getip();
				$dbc->query("insert into ".ALLUSER." (user,email,password,ip) values 
				('$post[user]','$email','".md5($post['password'])."','$ip')");
				$re['userid']=$dbc->lastid();
				$re['user']=$_POST['user'];
				
				if(empty($config['user_reg']))
					$user_reg=1;
				elseif($config['user_reg']==3)
					$user_reg=1;
				else
					$user_reg=$config['user_reg'];
				login($re['userid'],$re['user']);
			}
			else
			{
				login($re['userid'],$re['user']);
			}
			echo uc_user_synlogin($uid);//ＵＣ同步登录
			$forward = $post['forward']?$post['forward']:$config["weburl"]."/main.php";
			msg($forward);
		}
		else
		{
			header("Location: login.php?erry=-1&connect_id=".$post['connect_id']);//用户不存在
			exit();
		}
	}
	else
	{	
		// no ucenter login
		$sql="select * from ".ALLUSER." where user='$post[user]' or email='$post[user]'";
		$db->query($sql);
		$re=$db->fetchRow();
		if($re["userid"])
		{
			if(substr($re['password'],0,4)=='lock')
				msg("login.php?erry=-4&connect_id=$post[connect_id]");//之前使用了找回密码功能，账户被锁定
			if($re['password']!=md5($post['password']))
				msg("login.php?erry=-2&connect_id=$post[connect_id]");//密码错误
			
			if($re["password"]==md5($post['password']))
			{
				login($re['userid'],$re['user']);
				$forward = $post['forward']?$post['forward']:$config["weburl"]."/main.php";
				msg($forward);
			}
		}
		else
			msg('login.php?erry=-1&connect_id='.$post['connect_id']);//用户不存在
	}
}
//========================================================
function login($uid,$username)
{
	global $post,$config;
	$db=new dba($config['dbhost'],$config['dbuser'],$config['dbpass'],$config['dbname'],$config['dbport']);
	if($uid)
		$sql="select lastLoginTime,userid,user,regtime,statu,company_id from ".ALLUSER." a where a.userid='$uid'";
	else
		$sql="select lastLoginTime,userid,user,regtime,statu,company_id from ".ALLUSER." where user='$username'";
	$db->query($sql);
	$re=$db->fetchRow();
	bsetcookie("USERID","$uid\t$re[user]\t$re[company_id]",NULL,"/",$config['baseurl']);
	setcookie("USER",$re['user'],NULL,"/",$config['baseurl']);
	$_SESSION["STATU"]=$re['statu'];
	
	if(time()-$re["lastLoginTime"]>=3600)
	{
		include("includes/point_inc.php");
		if($point_config['point']=='1'&&$point_config['every_logo']!='0')
			renew_point('',$point_config['every_logo']);
	}	
	$sql="update ".ALLUSER." set lastLoginTime='".time()."' WHERE userid='$uid'";
	$db->query($sql);
}
//==================================================================================
include_once("includes/global.php");
include_once("includes/smarty_config.php");
include_once("config/reg_config.php");
$config = array_merge($config,$reg_config);
//===========================================
if($buid&&empty($_GET['style']))
{
	header("Location:main.php");
	exit();
}
include_once("footer.php");
$tpl -> assign("lang",$lang);
$tpl -> assign("current","office");
$tpl->display("login.htm");
?>