<?php
/**
 * @package iCMS
 * @copyright 2007-2010, iDreamSoft
 * @license http://www.idreamsoft.cn iDreamSoft
 * @author coolmoo <idreamsoft@qq.com>
 */
class User{
	var $uId;
	var $user;
	var $nickname;
	var $group;
	var $db;
	var $CT;
	var $loginSuccess=False;
	var $cpower;
	//初始化 操作 兼容PHP4
	function User(){
		$this->__construct();
	}
	//初始化 操作 PHP5
	function __construct(){
		global $iCMS;
		$this->db	= $iCMS->db;
	}
	//检验用户
	function checkuser($a,$p,$ajax=false){
		//验证用户 账号/密码
		$this->user = $this->db->getRow("SELECT * FROM `#iCMS@__members` WHERE `username`='{$a}' AND `password`='{$p}'");
		if(empty($this->user)){
			//记录
			$a && runlog('user.login', 'username='.$a.'&password='.$_POST['password']);
			if($ajax) return false;
			$this->LoginPage();
		}else{
			$this->uId=$this->user->uid;
			$this->user->info && $this->user->info=unserialize($this->user->info);
			$this->group = $this->db->getRow("SELECT * FROM `#iCMS@__group` WHERE `gid`='{$this->user->groupid}'");//用户组
			$this->power = explode(',',$this->merge($this->group->power,$this->user->power));
			$cpower		 = $this->merge($this->group->cpower,$this->user->cpower);
			$this->cpower= empty($cpower)?array(0):explode(',',$cpower);
			$this->nickname= empty($this->user->nickname)?$this->user->username:$this->user->nickname;
			if($ajax) return true;
		}
	}
	//检查栏目权限
	function CP($p=NULL,$T="F",$url=__REF__){
		if($this->group->gid=="1") return TRUE;
		if(is_array($p)?array_intersect($p,$this->cpower):in_array($p,$this->cpower)){
			return TRUE;
		}else{
			if($T=='F'){
				return FALSE;
			}else{
//				$this->cleancookie();
				redirect(lang($T),$url);
				exit();
			}
		};
	}
	//检查后台权限
	function MP($p=NULL,$T="Permission_Denied",$url=__REF__){
//		var_dump($p);
//		$R=is_array($p)?array_intersect($p,$this->power):!in_array($p,$this->power);
//		var_dump($R);
//		exit;
		if(is_array($p)?array_intersect($p,$this->power):in_array($p,$this->power)){
			return TRUE;
		}else{
			if($T=='F'){
				return FALSE;
			}else{
//				$this->cleancookie();
				redirect(lang($T),$url);
				exit();
			}
		}
	}
	//登陆验证
	function checklogin($a,$p,$ajax=false){
		global $_iGLOBAL;
		if(empty($a) && empty($p)){
			$auth		= get_cookie('user');
			list($a,$p)	= explode('#=iCMS!=#',authcode($auth,'DECODE'));
			return $this->checkuser($a,$p,$ajax);
		}else{
			$crs = $this->checkuser($a,$p,$ajax);
			if($ajax && !$crs) return false;
			set_cookie('user',authcode($a.'#=iCMS!=#'.$p,'ENCODE'));
			set_cookie('username',$this->nickname);
	        $this->db->query("UPDATE `#iCMS@__members` SET `lastip`='".getip()."',`lastlogintime`='".$_iGLOBAL['timestamp']."',`logintimes`=logintimes+1 WHERE `uid`='$this->uId'");
			if($ajax) return true;
			redirect('登陆成功, 请稍候......', __SELF__);
		}
	}
	//登陆页
	function LoginPage(){
		include iCMS_usercp_tpl('login');
	}
	//注销
	function logout($url){
		$this->cleancookie();
		redirect('注销成功, 请稍后......',$url);
	}
	function cleancookie(){
		set_cookie("user", '',-31536000);
		set_cookie("seccode",'',-31536000);
		set_cookie("username",'',-31536000);
	}
	function merge($G,$A){
		$G && $tmp[]=$G;
		$A && $tmp[]=$A;
		return @implode(',',$tmp);
	}
}
?>