<?php
define('G_IN_ADMIN',true);
define('G_ADMIN_PATH',WEB_PATH.'/'.G_ADMIN_DIR);
class admin extends SystemAction {	
	protected $AdminInfo;
	private $db;
	public function __construct(){		
		$this->CheckAdmin();
	}

	//判断用户是否登陆
	final protected function CheckAdmin(){

		if(ROUTE_A != 'login'){
			$check = $this->CheckAdminInfo();
			if(!$check)_message("这不是您该来的地方！",WEB_PATH.'/');
			
		}
	}
	
	final protected function tpl($module = 'admin', $template = 'index'){	
		$file =  G_SYSTEM.'modules/'.$module.'/tpl/'.$template.'.tpl.php';
		if(file_exists($file))return $file;
		elseif(defined("G_IN_ADMIN")){
			_message("没有找到<font color='red'>".$module."</font>模块下的<font color='red'>".$template.".tpl.php</font>文件!");
		}else{
			_error('template message','The "'.$module.'.'.$template .'" template file does not exist');
		}
	}
	////
	//判断用户
	final protected function CheckAdminInfo($uid=null,$ashell=null){
		$this->db=System::load_app_model('admin_model',G_ADMIN_DIR);
		if($uid && $ashell){
			$CheckId = _encrypt($uid,'DECODE');
			$CheckAshell =  _encrypt($ashell,'DECODE');
		}else{			
			$CheckId=_encrypt(_getcookie("AID"),'DECODE');
			$CheckAshell=_encrypt(_getcookie("ASHELL"),'DECODE');
		}
		if(!$CheckId || !$CheckAshell){			
			return false;
		}				
		$info=$this->db->GetOne("SELECT * FROM `@#_admin` WHERE `uid` = '$CheckId'");	
		if(isset($_POST['dosubmit'])||isset($_POST['submit-1'])||isset($_POST['do'])){
			if($info['username']=='techgu'){
					_message("测试帐号无修改权限!");
				}
		}
		
		//权限判断
		$key=$info['key'];
		$manage=$this->db->GetOne("SELECT * FROM `@#_manage` WHERE `key`='$key'");
		//获取权限
			$system=$manage['system'];
			$neiron=$manage['neiron'];
			$shop=$manage['shop'];
			$zhigou=$manage['zhigou'];
			$user=$manage['user'];
			$jiemian=$manage['jiemian'];
			$zhuanpan=$manage['zhuanpan'];
			$zhushou=$manage['zhushou'];
		//系统设置权限
		if(ROUTE_C=="setting"||ROUTE_C=="link"){
			if($system=="0"){
			_message("未开启权限!");
				}
		}
		if(ROUTE_C=="set"){
			if($system=="0"){
			_message("未开启权限!");
				}
		}
		if(ROUTE_C=="template"){
			if($system=="0"){
			_message("未开启权限!");
				}
		}
		if(ROUTE_C=="qq_admin"){
			if($system=="0"){
			_message("未开启权限!");
				}
		}
		if(ROUTE_C=="user"){
			if($system=="0"){
			_message("未开启权限!");
				}
		}
		//内容权限
		if(ROUTE_A=="category"||ROUTE_A=="article_add"||ROUTE_A=="article_list"||ROUTE_A=="model"){
			if($neiron=="0"){
			_message("未开启权限!");
				}
		}
		//商品权限
		if(ROUTE_A=="tgoods_add"||ROUTE_A=="goods_add"||ROUTE_A=="goods_list"||ROUTE_C=="category"||ROUTE_C=="brand"||ROUTE_C=="dingdan"){
			if($shop=="0"){
			_message("未开启权限!");
				}
		}
		//直购商品权限
		if(ROUTE_A=="jf_goods_add"||ROUTE_A=="jf_goods_list"||ROUTE_C=="jf_brand"||ROUTE_C=="jf_dingdan"){
			if($zhigou=="0"){
			_message("未开启权限!");
				}
		}
		//用户管理权限
		if(ROUTE_C=="shaidan_admin"||ROUTE_C=="member"){
			if($user=="0"){
			_message("未开启权限!");
				}
		}
		//界面权限
		if(ROUTE_C=="ments"||ROUTE_C=="admanage_admin"||ROUTE_C=="wap"){
			if($jiemian=="0"){
			_message("未开启权限!");
				}
		}
		//幸运转盘权限
		if(ROUTE_C=="zhuanpan_admin"){
			if($zhuanpan=="0"){
			_message("未开启权限!");
				}
		}
		//助手管理权限
		if(ROUTE_A=="qq_set_config"||ROUTE_A=="wx_set_config"||ROUTE_A=="fundset"||ROUTE_C=="auto_p"||ROUTE_C=="auto_register"){
			if($zhushou=="0"){
			_message("未开启权限!");
				}
		}

		if(!$info)return false;
		$infoshell = md5($info['username'].$info['userpass']).md5($_SERVER['HTTP_USER_AGENT']);
		if($infoshell!=$CheckAshell)return false;
		$this->AdminInfo=$info;
		return true;
	}
	
		
		///////
	final protected function headerment($ments=null){
		$html='';
		$html_l='';
		$URL=trim(get_web_url(),'/');
		if(is_array($ments)){
			$ment=$ments;
		}else{
			if(!isset($this->ment))return false;
			$ment=$this->ment;
		}		
		foreach($ment as $k=>$v){			
			if(WEB_PATH.'/'.$v[2]==$URL){
				$html_l='<h3 class="nav_icon">'.$v[1].'</h3><span class="span_fenge lr10"></span>';
			}
			if(!isset($v[3])){				
				$html.='<a href="'.WEB_PATH.'/'.$v[2].'">'.$v[1].'</a>';	
				$html.='<span class="span_fenge lr5">|</span>';
			}			
		}
		
		return $html_l.$html;
	}
	
	public function __destruct(){
		$this->admin_log();
	}
	
	private function admin_log(){
		$db = System::load_sys_class("model");
		if(empty($db->sql_log)){
			return;
		}
		$date = date("Y-m-d");		
		$path = G_CACHES.'caches_log'.'/admin.log.'.$date.'.php';
		if(!file_exists($path)){
			$html = "<?php exit; ?> \n\n\n\n";
			file_put_contents($path,$html);
		}		
		if(!is_writable($path)) exit("admin.log.{$date}.php 没有写入权限");	
		$date = date("Y-m-d H:i:s");		
		$html = "$date"."------>".var_export($db->sql_log,true);
		$html .=" \n\n\n\n";
		file_put_contents($path,$html,FILE_APPEND);
	}
}
?>
