CommonAction.class.php 4.26 KB
<?php
class CommonAction extends BaseAction {
	public $ADMIN_KEY='';
	public $LOGIN_TIME='';
	public $ADMIN_SCHOOL='';
	public function __construct() {
		parent::__construct();
		$this->ADMIN_KEY=cookie('ADMIN_KEY');
		$this->ADMIN_SCHOOL=cookie('ADMIN_SCHOOL');
		$this->LOGIN_TIME=cookie('LOGIN_TIME');
		header('Content-Type:text/html; charset=utf-8');
		$this->ppFirewall();
		$this->checkAdminSession();
		//去除反斜梗
		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),session('ADMIN_KEY')));
			// exit;
			if(!$auth->check(strtolower(MODULE_NAME).'-'.strtolower(ACTION_NAME),cookie('ADMIN_KEY'))){
				$this->error('你没有权限');
			}
		}
	}
	/**
	 * 判断用户并返回查询条件 超级管理员与学院管理员
	 */
	protected function getWhere(){
		if($this->ADMIN_KEY == 1)
			return NULL;
		else
			return array('eq',$this->ADMIN_SCHOOL);
	}
	public function checkAdminSession() {
		$ADMIN_KEY=$this->ADMIN_KEY;
		if (!isset($ADMIN_KEY) || !isset($this->LOGIN_TIME)) {
			$this->outclear();
			exit('<script>alert("当前用户未登录或登录超时,请重新登录");top.location.href="'.U('Public/login').'";</script>');
		}
	}
	//防火墙验证
	protected function ppFirewall(){
		if(!$_COOKIE['pro_ppfirewall']){
			header('Content-Type:text/html; charset=utf-8');
			exit('您无权限操作');
		}
	}
	//清除//防火墙验证
	protected function dFirewall(){
		setcookie('pro_ppfirewall',null,time()-1000);
	}

	public function outlogin(){
		$this->outclear();
		$this->success('退出成功!',U('Public/index'));
	}
	protected function outclear(){
		cookie('ADMIN_KEY',null);
		cookie('ADMIN_NAME',null);
		cookie('LOGIN_TIME',null);
		cookie('pp_authlist',null);
		// $this->dFirewall();
	}
	public function IsAuth($action) {
		import('ORG.Util.Auth');//加载类库
		$auth=new Auth();
		/*if(!$auth->check(strtolower($action),$_SESSION['ADMIN_KEY']['id'])){
			$this->error('你没有权限');
		}*/		
	}

	public function AllCatid($id,$type='1',$tab='Category',$upid='upid'){
		$table=M($tab);
		
		if($type==1){
			$return[]=$id;
		}else{
			$return =$id;
		}
		$map[$upid]=array('eq',$id);
		$lists=$table->where($map)->select();
		foreach($lists as $k=>$v){
			$listarr=$this->AllCatid($v['id'],$type,$tab);
			if($type==1){
				$return=array_merge($return,$listarr);
			}else{
				$return .=','.$listarr;
			}
		}
		return $return;
	}
	public function topId($id){
		$table=M('Category');
		$da=$table->where('id='.$id)->find();
		if(!$da){
			return false;
		}
		if($da['topid']==0){
			return $da['id'];
		}else{
			return $da['topid'];
		}
	}
/**
 * 导入excel
**/
	function impExcel()
	{
		if(isset($_FILES["import"]) && ($_FILES["import"]["error"] == 0))
		{
			$result = $this->importExecl($_FILES["import"]["tmp_name"]);
			if($result["error"] == 1)
			{          
				$execl_data = $result["data"][0]["Content"];
				foreach($execl_data as $k=>$v)
				{

				// 这里写你的业务代码

				}
			}
		}
	}
/**
 * 导出Excel
**/	
	function expExcel()
	{
		$xlsName  = "Authrule";
		$xlsCell  = array(
			array('id','序列'),
			array('name','规则'),
			array('title','规则说明')
		);
		$xlsModel = M('Authrule');
		$xlsData  = $xlsModel->Field('id,name,title')->select();
		$this->exportExcel($xlsName,$xlsCell,$xlsData);
	}
/**
 * 获取用户权限
 * 防止 authlist 走出cookie限制
 * @return array
 */
	protected function getPower(){
		//获取用户权限
        import('ORG.Util.Auth');//加载类库
        $auth=new Auth();
        $authlist=$auth->getAuthList(cookie('ADMIN_KEY'));
		return $authlist;
	}
}