<?php
/***********************************************************
    [大米CMS] (C)2011 - 2011 damicms.com
	
    @function 认证模块

    @Filename PublicAction.class.php $

    @Author 追影 QQ:279197963 $

    @Date 2011-11-17 15:14:43 $
*************************************************************/
class PublicAction extends Action 
{	
	public function index()
	{
		//dump($_SESSION);
	//如果通过认证跳转到首页
		redirect(__APP__.'/Index/index');
	}
	
    public function login()
	{
		if($this->check_lock()){
		$this->display('lock');
		}else{
		$this->display('login');
		}
	}

	public function check_lock(){
		$ip = get_client_ip();
		$lock_info = M('admin_lock')->where("ip='{$ip}' and expire_time>".time())->find();
		if($lock_info){
			return  true;
		}
		return false;
	}
	
	//返回所有类型
	function type_tree(){
		$type = M('type');
		$where ='';
		//echo 'ff'.$_SESSION[C('USER_CONTENT_KEY')];
		if(!$_SESSION[C('ADMIN_AUTH_KEY')] && $_SESSION[C('USER_CONTENT_KEY')]!=''){
		$where = ('typeid in('.$_SESSION[C('USER_CONTENT_KEY')].') and ');	
		}
		$where .= 'islink=0';
		$oplist = $type->where($where)->field("typeid,typename,fid,concat(path,'-',typeid) as bpath")->group('bpath')->select();
		//echo $type->getLastSql();
		$this->assign('type_tree',$oplist);
		}
	
	function checkLogin()
	{
		if($this->check_lock()){
			$this->display('lock');
			return false;
		}

		if(!isset($_SESSION['admin_err_number'])){
			$_SESSION['admin_err_number'] = 0;
		}

		if(empty($_POST['username']))
		{
			$this->error("帐号错误");
		}
		elseif (empty($_POST['password']))
		{
			$this->error("密码必须!");
		}
		elseif (empty($_POST['verify']))
		{
			$this->error('验证码必须!');
		}
		if(md5($_POST['verify']) != $_SESSION['verify'])
		{
			$this->error('验证码错误!');
		}
		
        //生成认证条件
        $map            =   array();
		// 支持使用绑定帐号登录
		$map['username']	= inject_check($_POST['username']);
		$map["status"]	= array('gt',0);
		import ('ORG.Util.RBAC' );
        $authInfo = RBAC::authenticate($map);
        //使用用户名、密码和状态的方式进行认证
        if(false === $authInfo)
		{
			$_SESSION['admin_err_number'] += 1;
			$this->error('帐号不存在!');
        }
		if(empty($authInfo))
		{
			$_SESSION['admin_err_number'] += 1;
			$this->error('帐号不存在或已禁用!');
		}
		$pwdinfo = strcmp($authInfo['password'],md5('wk'.trim($_POST['password']).'cms'));
		$ip = get_client_ip();
		if($pwdinfo <> 0)
		{
			$_SESSION['admin_err_number'] += 1;
			$this->_log_operation('账号'.$map['username'].'尝试登录,密码错误','失败');
			if($_SESSION['admin_err_number'] >=5){
				//锁定该IP
				$lock = array();
				$lock['ip'] = $ip;
				$lock['expire_time'] = time() + 15*60;
				M('admin_lock')->add($lock);
				$this->_log_operation('IP:'.$lock['ip'].'已被锁定，限制登录','限制');
			}
			$this->error('密码错误!');
        }
		M('admin_lock')->where("ip='{$ip}'")->delete();
		$_SESSION['admin_err_number'] = 0;
		$this->_log_operation($map['username'].'登录成功');
        $_SESSION[C('USER_AUTH_KEY')]	=	$authInfo['id'];
		$_SESSION['username']		=	$_POST['username'];
		$_SESSION['cookietime'] = time();
		$role = M('role_admin');
		$authInfo['role_id'] = $role->where('user_id='.$authInfo['id'])->getField('role_id');
        if($authInfo['role_id'] == '1')
		{
            $_SESSION[C('ADMIN_AUTH_KEY')]		=	true;
			
        }
		//保存登录信息
		$admin	=	M('admin');
		$ip		=	get_client_ip();
		$time	=	time();
        $data = array();
		$data['id']	=	$authInfo['id'];
		$data['lastlogintime']	=	$time;
		$data['lastloginip']	=	$ip;
		$admin->save($data);
		// 缓存访问权限
        RBAC::saveAccessList();
		//保存cookie信息
		import ('ORG.Util.Cookie');
		Cookie::set($_SESSION['cookietime'],'1',60*60*3);
		//dump($_SESSION);
		$this->index();
	}
	
	function loginout()
	{
		if(isset($_SESSION[C('USER_AUTH_KEY')]))
		{
			$this->_log_operation($_SESSION['username'].'退出登录');
			unset($_SESSION[C('USER_AUTH_KEY')]);
			unset($_SESSION);
			session_destroy();
			$this->assign("jumpUrl",U('Public/login'));
			$this->success('登出成功!');
        }
		$this->assign("jumpUrl",U('Public/login'));
		$this->error('已经登出!');
	}
	
	public function verify()
	{
		import('ORG.Util.Image');
		ob_end_clean();
		Image::buildImageVerify(5,1,'png',100,32,'verify');
	}

	function _log_operation($log, $result = '成功')
	{
		if (!isset($_SESSION[C('USER_AUTH_KEY')])) {
			$username = '游客';
			$usergroup = '';
		} else {
			if(isset($_SESSION['admin_username']) && isset($_SESSION['admin_group'])){
				$username = $_SESSION['admin_username'] ;
				$usergroup =  $_SESSION['admin_group'];
			}else{
				$sql = 'SELECT role_admin.role_id,role.`name` as groupname,admin.username FROM ' . C('RBAC_USER_TABLE') . ' as role_admin LEFT JOIN ' . C('DB_PREFIX') . 'admin  as admin ON role_admin.user_id = admin.id left JOIN ' . C('RBAC_ROLE_TABLE') . ' as role ON  role.id = role_admin.role_id WHERE admin.id = ' . $_SESSION[C('USER_AUTH_KEY')] . ' and role.`status` = 1';
				$rs =   M()->query($sql);
				//echo $sql;
				if($rs) {
					$usergroup  = $rs[0]['groupname'];
					$_SESSION['admin_group'] = $rs[0]['groupname'];
					$username = $rs[0]['username'];
					$_SESSION['admin_username'] = $rs[0]['username'];
				}
			}

		}
		$ip = get_client_ip();
		$data = array();
		$data['username'] = $username;
		$data['usergroup'] = $usergroup;
		$data['operate'] = $log;
		$data['result'] = $result;
		$data['ip'] = $ip;
		$data['addtime'] = time();
		M('log')->add($data);
		unset($data);
	}
}
?>