<?php 
require_once(SYS_ROOT. '/cls_tab/tab.sys.admin.group.php');
class cls_ctl_limit
{
   private $perms,$obj_db;
   private $arr_pageact,$arr_model,$arr_page,$arr_article;
   function __construct()
    {
		global $obj_db,$obj_ctl_user;
		$this->obj_db=$obj_db;
		$this->perms=array("id"=>0,"uname"=>"","admin_group_id"=>0,"issuper"=>false);
		$this->perms["id"]=$obj_ctl_user->id;
		$this->perms["uname"]=$obj_ctl_user->uname;
		$this->perms["admin_group_id"]=$obj_ctl_user->admin_group_id;
		if(defined('WEB_ADMIN_ID')){
			//网站创始人，拥有最高权限
			$str_webadmin=WEB_ADMIN_ID;
			$arr_webadmin=explode(",",$str_webadmin);
			if(in_array($this->perms["id"],$arr_webadmin)){
				$this->perms["issuper"]=true;
			}
		}
		//取控制权限
		$str_sql="select * from ".DB_PRE."sys_admin_group where group_id='".$this->perms["admin_group_id"]."'";
		$obj_result=$this->obj_db->query($str_sql);
		$arr_model=array();
		$arr_page=array();
		$arr_model_page=array();
		if($obj_rs=$this->obj_db->fetch_array($obj_result))
		{
			$arr_x1=explode("||",$obj_rs["group_inherit_pageact"]);
			foreach($arr_x1 as $item1){
				$arr_x2=explode("=>",$item1);
				$arr_model[]=$arr_x2[0];
				if(count($arr_x2)>1){
					$arr_x3=explode("|",$arr_x2[1]);
					$arr_page_x["m_".$arr_x2[0]]=array();
					foreach($arr_x3 as $item2){
						$arr_x4=explode("=",$item2);
						$arr_x5=explode(",",$arr_x4[1]);
						$arr_page["m_".$arr_x2[0]]["p_".$arr_x4[0]]=$arr_x5;
						$arr_model_page["m_".$arr_x2[0]][]=$arr_x4[0];
					}
				}
			}
			$this->arr_article=unserialize($obj_rs["group_article"]);
		}
		$this->arr_model=$arr_model;
		$this->arr_page=$arr_page;
		$this->arr_model_page=$arr_model_page;
    }
	function __get($name){
		if(isset($this->perms["$name"])){
			return $this->perms["$name"];
		}
		if(isset($this->config["$name"])){
			return $this->config["$name"];
		}
	}

	function chk_limit($msg_act="")
	{
		$arr_return=array("err"=>"");
		if($this->perms["issuper"]) return $arr_return;
		$str_act="";
		if($msg_act==""&&isset($_REQUEST["act"])) $str_act=$_REQUEST["act"];
		$arr_act=explode("_",$str_act);
		$str_act=$arr_act[0];
		//取当前而面所属模块
		$str_url=$_SERVER["SCRIPT_FILENAME"];
		$str_pagename=basename($_SERVER["SCRIPT_FILENAME"]);
		$str_model=get_dir_name($str_url,1);
		if(strlen($str_model)>6){
			$str_model=substr($str_model,6);
		}
		$arr_page=array("index.php","menu.php","main.php","top.php");
		if(in_array($str_pagename,$arr_page)){
			return $arr_return;
		}
		$int_admin_group_id=$this->perms["admin_group_id"];
		if($int_admin_group_id<1){
			$arr_return["err"]="系统还没有为你分配管理权限！";
			return $arr_return;
		}
		//检查模块

		if(!in_array($str_model,$this->arr_model)){
			$arr_return["err"]="您没有管理该模块的权限!";
			return $arr_return;
		}
		$arr_adminact=$this->arr_page["m_".$str_model];
		if(isset($arr_adminact["p_".$str_pagename])){
			$this->arr_pageact=$arr_adminact["p_".$str_pagename];
			if($str_act==""){
				return $arr_return;
			}
			if(!is_array($arr_adminact["p_".$str_pagename])){
				$arr_return="你没有操作权限！";
				return $arr_return;
			}
			if(in_array($str_act,$arr_adminact["p_".$str_pagename])){
				return $arr_return;
			}else{
				$arr_return["err"]="你没有操作权限！";
				return $arr_return;
			}
		}else{
			$arr_return["err"]="你没有操作权限！";
			return $arr_return;
		}
		return $arr_return;
	}
	function chk_model($msgmodel){
		if($msgmodel=="") return true;
		if($this->perms["issuper"]) return true;
		if(is_array($this->arr_model)){
			if(in_array($msgmodel,$this->arr_model)){
				return true;
			}else{
				return false;
			}
		}else{
			if($this->arr_model=="*"){
				return true;
			}else{
				return false;
			}
		}
	}
	function chk_page($msg_model,$msgpage){
		if($msgpage=="") return true;
		if($this->perms["issuper"]) return true;
		if(is_array($this->arr_model_page)){
			if(!isset($this->arr_model_page["m_".$msg_model])) return false;
			$arr_page=$this->arr_model_page["m_".$msg_model];
			if(in_array($msgpage,$arr_page)){
				return true;
			}else{
				return false;
			}
		}else{
			if($this->arr_model_page=="*"){
				return true;
			}else{
				return false;
			}
		}
	}
	function chk_act($msgact)
	{
		if($msgact=="") return true;
		if($this->perms["issuper"]) return true;
		if(is_array($this->arr_pageact)){
			if(in_array($msgact,$this->arr_pageact)){
				return true;
			}else{
				return false;
			}
		}else{
			if($this->arr_pageact=="*"){
				return true;
			}else{
				return false;
			}
		}
	}
	function get_folder_id(){
		if(isset($this->arr_article["folder"])){
			return $this->arr_article["folder"];
		}else{
			return "";
		}
	}
	function chk_article_folder($msg_id){
		if($this->perms["issuper"]) return true;
		if(isset($this->arr_article["folder"]) && is_array($this->arr_article["folder"])){
			if(in_array($msg_id,$this->arr_article["folder"])){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
}
?>