<?php 
class cls_ctl_user_base
{
	protected $perms;
    function __construct()
    {
		$this->perms=array("sid"=>"","id"=>0,"uname"=>"","admin_id"=>0,"admin_group_id"=>0,"is_admin"=>0,"type"=>"","company_id"=>0,"person_id"=>0,"version"=>"xxgo.net");
		$this->perms["cookie_time"]=1;
		self::_init_login_info();
    }
	function _init_login_info()
	{
		global $obj_db;
		$str_sid=get_cookie("login_sid");
		if($str_sid!=""){
			global $get_isajax;
			$str_sid=self::get_safecode($str_sid,"decode");//解码\
			$str_sql="select * from ".DB_PRE."sys_session where session_sid='".$str_sid."'";
			$obj_result  = $obj_db->query($str_sql);
			if($obj_rs=$obj_db->fetch_array($obj_result)){
				$lng_key=intval(get_cookie("login_key"));
				//echo format_date($lng_key,"Y-m-d H:i:s")."<br>".format_date($obj_rs["session_addtime"],"Y-m-d H:i:s")."<br>";
				if($obj_rs["session_user_id"]>0&&$lng_key==$obj_rs["session_addtime"]){
					$this->perms["id"]=$obj_rs["session_user_id"];
					$this->perms["uname"]=$obj_rs["session_user_name"];
					$this->perms["admin_id"]=$obj_rs["session_admin_id"];
					$this->perms["admin_group_id"]=$obj_rs["session_admin_group_id"];
					$this->perms["type"]=$obj_rs["session_type"];
					$this->perms["is_admin"]=$obj_rs["session_isadmin"];
					//15分钟更新一次
					set_cookie("login_key",$obj_rs["session_addtime"],$this->perms["cookie_time"]);
				}
				$this->perms["sid"]=$str_sid;
			}
		}
		if($this->perms["sid"]==""){
			$obj_tab_session=new cls_tab_sys_session;
			$arr_fields=array();
			$arr_fields["session_user_id"]=0;
			$arr_fields["session_user_name"]="";
			$arr_fields["session_type"]="";
			$arr_msg=$obj_tab_session->on_save($arr_fields);
			if($arr_msg["err"]==""&&$arr_msg["sid"]!=""){
				$this->perms["sid"]=$arr_msg["sid"];
				$str_sid=self::get_safecode($arr_msg["sid"]);
				set_cookie("login_sid",$str_sid,$this->perms["cookie_time"]);
			}
		}
		//同步uc 登录
		$str_uc_issynlogin=get_cookie("uc_issynlogin");
		if($str_uc_issynlogin=="1"&&$str_synlogin_cookies!=""){
			global $obj_msg;
			$obj_msg->html($str_synlogin_cookies);
			set_cookie("uc_issynlogin","");
		}
	}
	function is_login()
	{
		if($this->perms["id"]>0) return true;
		return false;
	}
	function is_admin_login()
	{
		if($this->perms["admin_id"]>0) return true;
		return false;
	}
	function is_admin(){
		if($this->perms["is_admin"]==1){
			return true;
		}else{
			return false;
		}
	}
	function on_admin_login($arr_fields=array())
	{
		global $obj_db;
		$arr_return=array("err"=>"");
		if(!isset($arr_fields["pwd"]) || $arr_fields["pwd"]==""){
			$arr_return["err"]="输入密码不能为空！";
			return $arr_return;
		}
		//登陆
		$str_sql="select * from ".DB_PRE."sys_admin where admin_user_id='".$this->perms["id"]."'";
		$obj_result=$obj_db->query($str_sql);
		if($obj_rs=$obj_db->fetch_array($obj_result))
		{
			$str_pwd=get_pwd($arr_fields["pwd"]);
			if($str_pwd==$obj_rs["admin_pwd"]){
				$this->perms["admin_id"]=$obj_rs["admin_user_id"];
				$this->perms["admin_group_id"]=$obj_rs["admin_group_id"];
				$obj_tab_session=new cls_tab_sys_session;
				$arr_fields=array();
				$arr_fields["session_sid"]=$this->perms["sid"];
				$arr_fields["session_admin_id"]=$obj_rs["admin_user_id"];
				$arr_fields["session_admin_group_id"]=$obj_rs["admin_group_id"];
				$arr_msg=$obj_tab_session->on_save($arr_fields);
			}else{
				$arr_return["err"]="输入密码错误！";
			}
		}else{
			$arr_return["err"]="您没有管理权限！";
		}
		return $arr_return;
	}
	//登录, arr_fields=登录数组,no_login=表示是否需要登录验证
	function on_login($arr_fields,$no_login=0)
	{
		global $obj_db,$obj_tab_sys_config;
		$arr_return=array("err"=>"");
		if($no_login==0){
			if(!isset($arr_fields["uname"]) || $arr_fields["uname"]==""){
				$arr_return["err"]="用户名不能为空！";
				return $arr_return;
			}
			if(!isset($arr_fields["pwd"]) || $arr_fields["pwd"]==""){
				$arr_return["err"]="输入密码不能为空！";
				return $arr_return;
			}
			$str_where=" where user_name='".$arr_fields["uname"]."'";
		}else{
			if(isset($arr_fields["user_id"])){
				$str_where=" where user_id=".intval($arr_fields["user_id"]);
			}else if(isset($arr_fields["user_name"])){
				$str_where=" where user_name='".$arr_fields["user_id"]."'";
			}else{
				$arr_return["err"]="输入登录参数不正确";
				return $arr_return;
			}
		}

		//检查用户是否已经存在
		$str_sql="select * from ".DB_PRE."sys_user".$str_where;
		$obj_rs=$obj_db->get_one($str_sql);
		if(empty($obj_rs)){
			$arr_return["err"]="用户不存在";
			return $arr_return;
		}
		//验证登录
		if($no_login==0){
			$lng_errtime=$lng_errnum=0;
			$arr_login_verify=$obj_tab_sys_config->get_val('sys','login_verify');//登录配置
			(isset($arr_login_verify["stop_time"]))? $lng_stop_time=intval($arr_login_verify["stop_time"]) : $lng_stop_time=15;
			$arr_unit=array("d"=>60*60*24,"h"=>60*60,"i"=>60,"s"=>1);
			$arr_unit_name=array("d"=>"天","h"=>"小时","i"=>"分钟","s"=>"秒");
			(isset($arr_unit_name[$arr_login_verify["stop_unit"]]))? $str_unit_name=$lng_stop_time.$arr_unit_name[$arr_login_verify["stop_unit"]] : $stt_unit_name=$lng_stop_time."分钟";
			(isset($arr_login_verify["stop_unit"]) && isset($arr_unit[$arr_login_verify["stop_unit"]]))? $lng_stop_time=$lng_stop_time*$arr_unit[$arr_login_verify["stop_unit"]] : $lng_stop_time=60*$lng_stop_time;
			if(!empty($obj_rs["user_loginerr"])){
				$arr_loginerr=explode(",",$obj_rs["user_loginerr"]);//验证当天登录错误记录
				$lng_errtime=intval($arr_loginerr[0]);
				if(TIME-$lng_errtime<$lng_stop_time){
					$lng_errnum=intval($arr_loginerr[1]);
				}else{
					$lng_errtime=0;
				}
			}
			if(isset($arr_login_verify["show_code"]) && intval($arr_login_verify["show_code"])<=$lng_errnum){
				if(!isset($arr_fields["verify_code"]) || empty($arr_fields["verify_code"])){
					$arr_return["err"]="验证码不能为空！";
					return $arr_return;
				}
				$str_code=get_safecode($arr_fields["verify_code"]);
				if($str_code!=get_cookie("user_code_login")){
					$arr_return["err"]="验证码不正确！";
					return $arr_return;
				}
				set_cookie("user_code_login","");//清空原验证码
			}
			if(isset($arr_login_verify["stop_num"]) && $lng_errnum>=intval($arr_login_verify["stop_num"])){
					$arr_return["err"]="登录错误次数过多，请".$str_unit_name."后再来登录";
					return $arr_return;
			}
			$str_pwd=get_pwd($arr_fields["pwd"],$obj_rs["user_pwd_key"]);
			if($obj_rs["user_pwd"]!=$str_pwd){
				$arr_return["err"]="输入密码不正确！";
				//设置登录错误信息
				$lng_errnum++;
				$obj_db->execute("update ".DB_PRE."sys_user set user_loginerr='".TIME.",".$lng_errnum."' where user_id=".$obj_rs["user_id"]);
				return $arr_return;
			}
		}
		$str_is_web_admin=false;
		if(defined('WEB_ADMIN_ID')){
			//网站创始人，拥有最高权限
			$str_webadmin=WEB_ADMIN_ID;
			$arr_webadmin=explode(",",$str_webadmin);
			if(in_array($obj_rs["user_id"],$arr_webadmin)){
				$str_is_web_admin=true;
			}
		}
		if($obj_rs["user_state"]<1&&!$str_is_web_admin){
			$obj_tab_user=new cls_tab_sys_user();
			$str_state=array_search($obj_rs["user_state"],$obj_tab_user->state);
			$arr_return["err"]="您的账号处于：".$str_state."状态，暂时不能用，请联系管理员！";
			unset($obj_tab_user);
			return $arr_return;
		}
		$lng_isadmin=0;
		//判断用户是否为管理员
		$arr_admin=array();
		$str_sql2="select admin_user_id,admin_group_id,admin_id from ".DB_PRE."sys_admin where admin_user_id='".$obj_rs["user_id"]."'";
		$obj_result2=$obj_db->query($str_sql2);
		if($obj_rs2=$obj_db->fetch_array($obj_result2))
		{
			$arr_admin=$obj_rs2;
			$lng_isadmin=1;
		}
		//设置登陆信息
		$this->perms["id"]=$obj_rs["user_id"];
		$this->perms["uname"]=$obj_rs["user_name"];
		$this->perms["type"]=$obj_rs["user_type"];
		$this->perms["is_admin"]=$lng_isadmin;
		$obj_tab_session=new cls_tab_sys_session;
		$arr_fields=array();
		$arr_fields["session_sid"]=$this->perms["sid"];
		$arr_fields["session_user_id"]=$obj_rs["user_id"];
		$arr_fields["session_user_name"]=$obj_rs["user_name"];
		$arr_fields["session_type"]=$obj_rs["user_type"];
		$arr_fields["session_isadmin"]=$lng_isadmin;
		$arr_fields["session_addtime"]=TIME;//重设登记时间
		if($lng_isadmin==1&&WEB_ADMIN_DOUBLE!="1"){
			//系统非限制两次登陆
			$arr_fields["session_admin_id"]=$arr_admin["admin_user_id"];
			$arr_fields["session_admin_group_id"]=$arr_admin["admin_group_id"];
			$this->perms["admin_id"]=$arr_admin["admin_user_id"];
			$this->perms["admin_group_id"]=$arr_admin["admin_group_id"];
		}
		$arr_msg=$obj_tab_session->on_save($arr_fields);
		if($arr_msg["err"]==""&&$arr_msg["sid"]!=""){
			$lng_continuenum=$obj_rs["user_continuenum"];
			if($obj_rs["user_logintime"]<strtotime(date("Y-m-d",TIME))){
				//每日登录奖励
				cls_ctl_score::on_login($obj_rs["user_id"]);
				if($obj_rs["user_logintime"]>strtotime("-1 day",strtotime(date("Y-m-d",TIME)))){
					//连续登录奖励
					$lng_continuenum=$obj_rs["user_continuenum"]+1;
					cls_ctl_score::on_login_continue($obj_rs["user_id"],$lng_continuenum);
				}else{
					$lng_continuenum=0;
				}
			}
			//更新登陆信息
			$str_ip=get_user_ip();
			$str_sql="update ".DB_PRE."sys_user set user_loginnum=user_loginnum+1,user_loginip='".$str_ip."',user_logintime=".TIME.",user_continuenum=".$lng_continuenum." where user_id=".$this->perms["id"];
			$obj_db->execute($str_sql);
			$str_sid=self::get_safecode($arr_msg["sid"]);
			set_cookie("login_key",TIME,$this->perms["cookie_time"]);
		}
		return $arr_return;
	}
	function get_safecode($msg_val,$msg_type="encode"){
		$str_key=SYS_MD5_KEY;
		//if(isset($_SERVER["HTTP_USER_AGENT"])) $str_key.=$_SERVER["HTTP_USER_AGENT"];
		$str_en_key=base64_encode($str_key);
		$str_md5_key=md5($str_key);
		$str_md5_key_1=substr($str_md5_key,0,1);
		$str_md5_key_2=substr($str_md5_key,-1,1);
		$lng_key_1=ord($str_md5_key_1);
		$lng_key_2=ord($str_md5_key_2);
		$lng_x_key1=substr($lng_key_1,-1,1);
		if($lng_key_1>9){
			$lng_x_key2=intval(substr($lng_key_1,-2,1))+$lng_x_key1;
		}else{
			$lng_x_key2=$lng_x_key1*2;
		}
		$str_left=base64_encode(substr($str_md5_key,$lng_x_key1,$lng_x_key2));
		$lng_2_key1=substr($lng_key_2,-1,1);
		if($lng_2_key1>9){
			$lng_2_key2=intval(substr($lng_key_2,-2,1))+$lng_2_key1;
		}else{
			$lng_2_key2=$lng_2_key1*2;
		}
		$str_right=base64_encode(substr($str_md5_key,-$lng_2_key2));
		if($msg_type=="encode"){
			$str_en_id=base64_encode($msg_val);
			$str_en_code=$str_left.$str_en_id.$str_right;
			$str_return=str_replace("=","",$str_en_code);
		}else{
			$str_left=str_replace("=","",$str_left);
			$str_right=str_replace("=","",$str_right);
			$str_llen=strlen($str_left);
			$str_rlen=strlen($str_right);
			$str_len=strlen($msg_val);
			if($str_len<($str_llen+$str_rlen)){
				$str_return="";
			}else{
				$str_return=base64_decode(substr($msg_val,$str_llen,-$str_rlen));
			}
		}
		return $str_return;
	}
	function on_update_uname($new_uname)
	{
		$obj_tab_user=new cls_tab_sys_user();
		$arr_return=$obj_tab_user->on_update_uname($this->perms["id"],$new_uname);
		if($arr_return["err"]==""){
			$str_sql="update ".DB_PRE."sys_session set session_user_name='".$new_uname."' where session_user_id='".$this->perms["id"]."'";
			$obj_db->execute($str_sql);
			$this->perms["uname"]=$new_uname;
		}
		return $arr_return;
	}
	function on_update_pwd($arr_msg=array())
	{
		global $obj_db;
		$arr_return=array("err"=>"");
		if(!isset($arr_msg["oldpwd"]) || $arr_msg["oldpwd"]==""){
			$arr_return["err"]="旧密码不能为空！";
			return $arr_return;
		}
		if(!isset($arr_msg["pwd1"]) || $arr_msg["pwd1"]==""){
			$arr_return["err"]="新密码不能为空！";
			return $arr_return;
		}
		if(!isset($arr_msg["pwd2"]) || $arr_msg["pwd2"]==""){
			$arr_return["err"]="确认密码不能为空！";
			return $arr_return;
		}
		if($arr_msg["pwd1"]!=$arr_msg["pwd2"]){
			$arr_return["err"]="两次输入密码不一至！";
			return $arr_return;
		}
		$msg_info=chk_pwd($arr_msg["pwd1"]);
		if($msg_info["err"]!="") return $msg_info;
		if($arr_msg["oldpwd"]==$arr_msg["pwd1"]){
			$arr_return["err"]="旧密码与新密码相同！";
			return $arr_return;
		}
		//检查
		$str_sql="select * from ".DB_PRE."sys_user where user_id='".$this->perms["id"]."'";
		$obj_result=$obj_db->query($str_sql);
		if($obj_rs=$obj_db->fetch_array($obj_result))
		{
			$str_pwd=get_pwd($arr_msg["oldpwd"],$obj_rs["user_pwd_key"]);
			$str_new_pwd=get_pwd($arr_msg["pwd1"],$obj_rs["user_pwd_key"]);
			if($obj_rs["user_pwd"]!=$str_pwd){
				$arr_return["err"]="输入旧密码不正确！";
				return $arr_return;
			}
		}else{
			$arr_return["err"]="意外错误！当前用户不存在！";
			return $arr_return;
		}
		$str_sql="update ".DB_PRE."sys_user set user_pwd='".$str_new_pwd."' where user_id='".$this->perms["id"]."'";
		$arr_return["err"]=$obj_db->execute($str_sql);
		return $arr_return;
	}
	function on_exit()
	{
		global $obj_db;
		$str_sql="update ".DB_PRE."sys_session set session_user_id=0,session_admin_id=0,session_admin_group_id=0 where session_sid='".$this->perms["sid"]."'";
		$obj_db->execute($str_sql);
		set_cookie("login_key","0");

	}
	function on_admin_exit()
	{
		if(WEB_ADMIN_DOUBLE!="1"){
			//非系统两次登录要求，则一次全退出
			self::on_exit();
			return;
		}
		$obj_tab_session=new cls_tab_sys_session;
		$arr_fields=array();
		$arr_fields["session_sid"]=$this->perms["sid"];
		$arr_fields["session_admin_id"]=0;
		$arr_fields["session_admin_group_id"]=0;
		$this->perms["admin_id"]=0;
		$this->perms["admin_group_id"]=0;
		$arr_msg=$obj_tab_session->on_save($arr_fields);
	}
	function get_rs($arr_id){
		global $obj_db;
		$arr_return=array();
		$str_id=get_arr_id($arr_id);
		$str_sql="select user_id,user_name,user_email from ".DB_PRE."sys_user where user_id in(".$str_id.")";
		$obj_result  = $obj_db->query($str_sql);
		while($obj_rs=$obj_db->fetch_array($obj_result)){
			$arr_return[]=$obj_rs;
		}
		return $arr_return;
	}
	function get_user_name($arr_id){
		global $obj_db;
		$arr_return=array();
		$str_id=get_arr_id($arr_id);
		if($str_id=="") return $arr_return;
		$str_sql="select user_id,user_name from ".DB_PRE."sys_user where user_id in(".$str_id.")";
		$obj_result  = $obj_db->query($str_sql);
		while($obj_rs=$obj_db->fetch_array($obj_result)){
			$arr_return["id_".$obj_rs["user_id"]]=$obj_rs["user_name"];
		}
		return $arr_return;
	}
	function get_user_info($arr_id){
		global $obj_db;
		$arr_return=array();
		$str_id=get_arr_id($arr_id);
		if($str_id=="") return $arr_return;
		$str_sql="select * from ".DB_PRE."sys_user where user_id in(".$str_id.")";
		$obj_result  = $obj_db->query($str_sql);
		while($obj_rs=$obj_db->fetch_array($obj_result)){
			$obj_rs["regtime"]=format_date($obj_rs["user_regtime"],"Y-m-d");
			$obj_rs["logintime"]=format_date($obj_rs["user_logintime"],"Y-m-d");
			$arr_return["id_".$obj_rs["user_id"]]=$obj_rs;
		}
		return $arr_return;
	}

	function get_user_id($arr_uname){
		global $obj_db;
		$arr_return=array();
		if(is_array($arr_uname)){
			$str_name=implode("','",$arr_uname);
		}else{
			$str_name=$arr_uname;
		}
		$str_sql="select user_id,user_name from ".DB_PRE."sys_user where user_name in('".$str_name."')";
		$obj_result  = $obj_db->query($str_sql);
		while($obj_rs=$obj_db->fetch_array($obj_result)){
			$arr_return[$obj_rs["user_name"]]=$obj_rs["user_id"];
		}
		return $arr_return;
	}
	function get_company_id(){
		global $obj_db;
		if(isset($this->perms["company_id"])&&$this->perms["company_id"]>0){
			return $this->perms["company_id"];
		}
		$str_sql="select company_id from ".DB_PRE."hr_company where company_user_id='".$this->perms["id"]."'";
		$obj_result  = $obj_db->query($str_sql);
		if($obj_rs=$obj_db->fetch_array($obj_result)){
			$this->perms["company_id"]=$obj_rs["company_id"];
		}
		return $this->perms["company_id"];
	}
	function get_resume_id(){
		global $obj_db;
		if(isset($this->perms["resume_id"])&&$this->perms["resume_id"]>0){
			return $this->perms["resume_id"];
		}
		$str_sql="select resume_id from ".DB_PRE."hr_resume where resume_user_id='".$this->perms["id"]."'";
		$obj_result  = $obj_db->query($str_sql);
		if($obj_rs=$obj_db->fetch_array($obj_result)){
			$this->perms["resume_id"]=$obj_rs["resume_id"];
		}
		return $this->perms["resume_id"];
	}
	function get_fz_id(){
		global $obj_db;
		if(isset($this->perms["fz_id"])&&$this->perms["fz_id"]>0){
			return $this->perms["fz_id"];
		}
		if($this->perms["type"]=="company"){
			$str_sql="select company_fz_id as fz_id from ".DB_PRE."hr_company where company_user_id='".$this->perms["id"]."'";
		}else{
			$str_sql="select resume_fz_id as fz_id from ".DB_PRE."hr_resume where resume_user_id='".$this->perms["id"]."'";
		}
		$obj_result  = $obj_db->query($str_sql);
		if($obj_rs=$obj_db->fetch_array($obj_result)){
			$this->perms["fz_id"]=$obj_rs["fz_id"];
		}
		return $this->perms["fz_id"];
	}
	function get_gold($msg_uid=0){
		if(empty($msg_uid)){
			$msg_uid=$this->perms["id"];
			if(isset($this->perms["gold"])) return $this->perms["gold"];
		}
		$arr_return=self::_set_gold_score($msg_uid);
		return $arr_return["gold"];
	}
	function get_score($msg_uid=0){
		if(empty($msg_uid)){
			$msg_uid=$this->perms["id"];
			if(isset($this->perms["score"])) return $this->perms["score"];
		}
		$arr_return=self::_set_gold_score($msg_uid);
		return $arr_return["score"];
	}
	private function _set_gold_score($msg_uid){
		global $obj_db;
		$arr_return=array("score"=>0,"gold"=>0);
		$obj_result=$obj_db->query("select account_score+account_score_use as 'score',account_gold+account_gold_use as 'gold' from ".DB_PRE."sys_account where account_user_id=".$msg_uid);
		if($obj_rs=$obj_db->fetch_array($obj_result)){
			$arr_return["score"]=$obj_rs["score"];
			$arr_return["gold"]=$obj_rs["gold"];
		}
		if($msg_uid==$this->perms["id"]){
			$this->perms["score"]=$arr_return["score"];
			$this->perms["gold"]=$arr_return["gold"];
		}
		return $arr_return;
	}
	function on_refresh_act($arr_fields){
		$str_x=serialize($arr_fields);
		$str_sql="update ".DB_PRE."sys_session set session_actinfo='".$str_x."',session_addtime=".TIME." where session_sid='".$this->perms["sid"]."'";
		$obj_db->execute($str_sql);
	}
	//认证状态
	function is_verify($msg_type,$msg_val=-1){
		if($msg_val<0){
			global $obj_db;
			$obj_rs=$obj_db->get_one("select user_verify from ".DB_PRE."sys_user where user_id=".$this->perms["id"]);
			if(isset($obj_rs["user_verify"])) $msg_val=$obj_rs["user_verify"];
		}
		$arr_verify=array("type_1"=>array(1,3,5,7,9,11,13,15),"type_2"=>array(2,3,6,7,10,11,14,15),"type_4"=>array(4,5,6,7,12,13,14,15),"type_8"=>array(8,9,10,12,11,14,15));
		if(!isset($arr_verify["type_".$msg_type])) return false;
		if(in_array($msg_val,$arr_verify["type_".$msg_type])) return true;
		return false;
	}
}
?>