<?php 
class cls_ctl_user
{
	private $tab_user;
	private $obj_db;
	private $config=array();
	private $perms;
    function __construct()
    {
		global $obj_db;
		$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->obj_db=$obj_db;
		$this->tab_user=DB_PRE."sys_user";
		$this->perms["cookie_time"]=1;
		self::_init_login_info();
    }
	function _init_login_info()
	{
		$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  = $this->obj_db->query($str_sql);
			if($obj_rs=$this->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"]);
			}
		}

	}
	function __destruct()
	{
	}
	function __get($name){
		if(isset($this->perms["$name"])){
			return $this->perms["$name"];
		}
	}
	function __set($name,$value){
		if($this->perms["$name"]){
			$this->perms["$name"]=$value;
		}else{
		}
	}
	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_regin($arr_msg=array())
	{
		if(!isset($arr_msg["pwd1"])||$arr_msg["pwd1"]=="") return "输入密码不能为空！";
		if(!isset($arr_msg["pwd2"])||$arr_msg["pwd2"]=="") return "确认密码不能为空！";
		if($arr_msg["pwd1"]!=$arr_msg["pwd2"]) return "两次输入密码不同！";
		$arr_fields=array();
		$arr_fields["user_name"]=$arr_msg["uname"];
		$arr_fields["user_pwd"]=$arr_msg["pwd1"];
		$arr_fields["user_email"]=$arr_msg["email"];
		$obj_tab_user=new cls_tab_sys_user();
		$msg_info=$obj_tab_user->on_insert($arr_fields);
		return $msg_info;
	}
	function on_admin_login($arr_fields=array())
	{
		$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=$this->obj_db->query($str_sql);
		if($obj_rs=$this->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;
	}
	function on_login($arr_fields=array())
	{
		$arr_return=array("err"=>"");
		if(!isset($arr_fields["uname"]) || $arr_fields["uname"]==""){
			$arr_return["err"]="用户名不能为空！";
			return $arr_return;
		}
		$arr_msg=chk_uname($arr_fields["uname"]);
		if($arr_msg["err"]!=""){
			$arr_return["err"]=$arr_msg["err"];
			return $msg_info;
		}
		if(!isset($arr_fields["pwd"]) || $arr_fields["pwd"]==""){
			$arr_return["err"]="输入密码不能为空！";
			return $arr_return;
		}
		$arr_msg=chk_pwd($arr_fields["pwd"]);
		if($arr_msg["err"]!=""){
			$arr_return["err"]=$arr_msg["err"];
			return $arr_return;
		}
		//检查用户是否已经存在
		$str_sql="select user_id,user_state,user_name,user_type,user_logintime,user_continuenum,user_pwd,user_pwd_key from ".$this->tab_user." where user_name='".$arr_fields["uname"]."'";
		$obj_result=$this->obj_db->query($str_sql);
		if($obj_rs=$this->obj_db->fetch_array($obj_result))
		{
			$str_pwd=get_pwd($arr_fields["pwd"],$obj_rs["user_pwd_key"]);
			if($obj_rs["user_pwd"]!=$str_pwd){
				$arr_return["err"]="输入密码不正确！";
				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=$this->obj_db->query($str_sql2);
			if($obj_rs2=$this->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"];
				$this->obj_db->execute($str_sql);
				$str_sid=self::get_safecode($arr_msg["sid"]);
				set_cookie("login_key",TIME,$this->perms["cookie_time"]);
			}
		}else{
			$arr_return["err"]="输入用户名不正确！";
		}
		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"]."'";
			$this->obj_db->execute($str_sql);
			$this->perms["uname"]=$new_uname;
		}
		return $arr_return;
	}
	function on_update_pwd($arr_msg=array())
	{
		$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 ".$this->tab_user." where user_id='".$this->perms["id"]."'";
		$obj_result=$this->obj_db->query($str_sql);
		if($obj_rs=$this->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 ".$this->tab_user." set user_pwd='".$str_new_pwd."' where user_id='".$this->perms["id"]."'";
		$arr_return["err"]=$this->obj_db->execute($str_sql);
		return $arr_return;
	}
	function on_exit()
	{
		$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"]."'";
		$this->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(){
		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  = $this->obj_db->query($str_sql);
		if($obj_rs=$this->obj_db->fetch_array($obj_result)){
			$this->perms["company_id"]=$obj_rs["company_id"];
		}
		return $this->perms["company_id"];
	}
	function get_resume_id(){
		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  = $this->obj_db->query($str_sql);
		if($obj_rs=$this->obj_db->fetch_array($obj_result)){
			$this->perms["resume_id"]=$obj_rs["resume_id"];
		}
		return $this->perms["resume_id"];
	}
	function get_fz_id(){
		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  = $this->obj_db->query($str_sql);
		if($obj_rs=$this->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"]."'";
		$this->obj_db->execute($str_sql);
	}

}
?>