CommonAction.class.php 1.93 KB
<?php
class CommonAction extends BaseAction {
	public function __construct() {
		parent::__construct();
		header('Content-Type:text/html; charset=utf-8');
		$this->checkLog();
		//去除反斜梗
		if (get_magic_quotes_gpc()){
			$_POST = array_map('stripslashes_deep', $_POST);
			$_GET = array_map('stripslashes_deep', $_GET);
			$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
		}
	}

	public function _initialize()
	{
		parent::_initialize();
		if((MODULE_NAME !="Index" || (MODULE_NAME =="Index" && !in_array(ACTION_NAME,array('index','main','menu','delcache','outlogin')))) && (MODULE_NAME !="Public" || (MODULE_NAME == 'Public' && !in_array(ACTION_NAME,array('login','index','verify')))))
		{
			import('ORG.Util.Auth');//加载类库
			$auth=new Auth();
			// var_dump($auth->check(strtolower(MODULE_NAME).'-'.strtolower(ACTION_NAME),cookie('uid')));
			// exit;
			if(!$auth->check(strtolower(MODULE_NAME).'-'.strtolower(ACTION_NAME),cookie('uid'))){
				$this->error('你没有权限');
			}
		}
	}
/**
 * 判断用户是否登录
 * @return [bool]
 */
	public function checkLog() {
		if(!cookie('uid') || !cookie('account')){
			$this->clearlog();
			exit('<script>alert("当前用户未登录或登录超时,请重新登录");top.location.href="'.U('Public/login').'";</script>');
		}
	}
/**
 * 清除登录所生成的cookie
 */
	protected function clearLog(){
		cookie('uid',null);
		cookie('account',null);
		cookie('username',null);
		cookie('logtime',null);
	}
/**
 * 用户退出登录
 */
	public function outlogin(){
		$this->clearLog();
		$this->success('退出成功!',U('Public/index'));
	}
/**
 * 获取用户权限
 * 防止 authlist 走出cookie限制
 * @return array
 */
	protected function getPower(){
		//获取用户权限
        import('ORG.Util.Auth');//加载类库
        $auth=new Auth();
        $authlist=$auth->getAuthList(cookie('uid'));
		return $authlist;
	}
}