<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST, ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace admin\controller;
use \temmoku\db;
use \temmoku\admin;
use \temmoku\lib\cookie;
use admin\model\index AS model;
class index extends admin
{
    public function index()
    {
    	$modular = db::select('*')->from(jab.'modular')->where("state != 0")->query();
    	if($modular){
    		foreach($modular AS $row){
	    		$row['rute']=@include_once APP_PATH.$row['dir']."/admin/power.php";
	    		$Modular_DB[]=$row;
	    	}
    	}
    	$plugin = db::from(jab.'plugin')->select('*')->where("state != 0")->query();
    	if($plugin){
    		foreach($plugin AS $row){
	    		$row['rute']=@include_once APP_PATH.'plugin/'.$row['dir']."/admin/power.php";
	    		$pluginDB[]=$row;
	    	}
    	}
    	C('MYDB.thumb') ? $thumb=  get_img_url(C('MYDB.thumb')) : $thumb= '/public/global/images/default.png';
		//判断一下是否存在后台定义的模块maps
		 $file=APP_PATH.c('index')."/admin/controller/maps.php";
		$maps=is_file($file) ?  "/admin/index/modular/".c('index')."/maps" :  '/admin/index/map/';
		$this->assign('maps', $maps);
    	$this->assign('plugin', $plugin);
    	$this->assign('thumb', $thumb);
    	$this->assign('pluginDB', $pluginDB);
    	$this->assign('Modular_DB', $Modular_DB);
    	$this->assign('modular', $modular);
    	$this->display(tpl('index'));
    }
    public function login()
    {
		if($_POST['username'] && $_POST['password']){
			if(C('admin_login_is_yzm')=='1'){
				$yzm=\temmoku\lib\yzm::get_yzm_code($_POST['yzm']);
				if($yzm['code'] =='9'){
					$this->err('验证码错误');
				}
			}
			$data = (new model)->Login($_POST['username'],$_POST['password']);
			if('0'===$data['code']){
				C('MYDB',$data['UserDB']);
				$MYGROUP=C('USER_GROUP.'.$data['UserDB']['groupid']);
				$MYGROUP['setting']=unserialize($MYGROUP['setting']);
				C('MYGROUP',$MYGROUP);
				
				if(3===$data['UserDB']['groupid']){
					$ORDINARY_ADMIN=encode($data['UserDB']['uid'].' '.$data['UserDB']['password'].' '.C('ONLINEIP'));
					//写入浏览器缓存
					cookie::set('ORDINARY_ADMIN',$ORDINARY_ADMIN);
					$this->Success('登陆成功，正在为您跳转',ADMIN_WEBURL.'/admin');
				}else{
					//权限判断，如果不是超管就需要判断权限
					$this->Admin_Permission_Check(true);
				}
			}else{
	    		$this->err($data['text']);
			}
		}
		$this->display(tpl('login'));
    }
    public function out(){
    	cookie::del('ORDINARY_ADMIN');
    	$this->Success('退出成功',ADMIN_WEBURL.'/admin/index/login');
    }
    public function map()
    {
		$count = (new model)->map();
		if(function_exists('gd_info')) {
		    $gd_info = @gd_info();
			$gd= $gd_info["GD Version"];
		}
		$AUTHORIZATION=http_request(Upgrade_URL.'upgrade/query_authorization/',['AUTHORIZATION'=>C('cms_AUTHORIZATION'),'weburl'=>WEBURL,'part'=>'1']);
        $this->assign('AUTHORIZATION', $AUTHORIZATION);
		$this->assign('Mysql_VERSION', $count['Mysql_VERSION']);
		$this->assign('PHP_VERSION', PHP_VERSION);
		$this->assign('gd', $gd);
		$this->assign('win', @php_uname());
		$this->assign('max_up_size', is_php("upload_max_filesize"));
		$this->assign('memory_limit', is_php("memory_limit"));
		$this->assign('max_execution_time', is_php("max_execution_time"));
		$this->assign('safe_mode', is_php("safe_mode"));
		$this->assign('curl_init', isfun("curl_init"));
        $this->assign('zlib_get_coding_type', isfun("zlib_get_coding_type"));
        
        $this->display(tpl('map'));
    }
    
    public function plugin(){
	    $data = db::select('*')->from(jab.'plugin')->where("dir= '".P_MODULE."' ")->row();
	    isset($data['table']) && define('P_jab',jab.'plugin_'.$data['table']);
	    if(C('MYDB.groupid') != 3){
	    	$this->plugin_power_Test($data['table']);
	    }
	    $CONF=APP_PATH.'plugin/'.P_MODULE.'/conf';
	    define('is_plugin',true);
	    define('is_home','admin');
	    if(file_exists($CONF)){
		   	Load_conf($CONF);
		}
		//定义一个插件试图所在目录的常量
		define('PLUGIN_VIEW', APP_PATH.'plugin/'.P_MODULE."/admin/view/");
		//定义一个插件所在目录的常量
		define('PLUGIN', 'plugin/'.P_MODULE."/");
		//定义一个插件操作方法变量
		define('PLUGIN_ACTION', '/admin/index/plugin/'.P_MODULE."/");
		define('admin_plugin_url', PLUGIN_ACTION);
	    //文件所在位置
	    $file= APP_PATH.'plugin/'.P_MODULE.'/admin/controller/'.P_CONTROLLER.'.php';
	    if(!is_file($file)){
	    	$this->err('不存在的控制器文件<br>'.$file);
	    }
		$function= APP_PATH.'plugin/'.P_MODULE."/function.php";
		if(is_file($function)){
			include_once $function;
		}
		
	    @include_once $file;
		$Modular_Class= plugin.'\\'.P_MODULE.'\admin\controller\\'.P_CONTROLLER;
	    //加载类
	    $Class=new $Modular_Class();
	    //加载方法
	    Load_Method($Class,P_METHOD,$Modular_Class);
    }
    private function plugin_power_Test($table){
    	$plugin=$table.'power';
    	$setting=C('MYGROUP.setting')['power'];
    	!isset($setting[$plugin][P_CONTROLLER]) && $setting[$plugin][P_CONTROLLER]=[];
    	if(!in_array(P_METHOD ,$setting[$plugin][P_CONTROLLER])){
    		$this->err('您没有权限操作');
    	}
    }
    public function modular(){
		$data = db::select('*')->from(jab.'modular')->where("dir= '".M_MODULE."' ")->row();
    	isset($data['table']) && define('M_jab',jab.$data['table']);
    	if(C('MYDB.groupid') != 3){
    		$this->Modular_power_Test($data['table']);
    	}
    	$dir=APP_PATH.M_MODULE.'/conf';
		Load_conf($dir);
		$function= APP_PATH.M_MODULE."/function.php";
		if(is_file($function)){
			include_once $function;
		}
    	define('is_home','admin');
		$data['domain'] ? $url=HTTP.$data['domain'] : $url=WEBURL.'/'.M_MODULE;
	    define('M_WEBURL',$url); 
		//定义一个插件操作方法变量
		define('admin_module_url', '/admin/index/modular/'.M_MODULE."/");
    	//文件所在位置
    	$file= APP_PATH.M_MODULE.'/admin/controller/'.M_CONTROLLER.'.php';
    	@include_once $file;
		$Modular_Class= M_MODULE.'\admin\controller\\'.M_CONTROLLER;
    	//加载类
    	$Class=new $Modular_Class();
    	//加载方法
    	Load_Method($Class,M_METHOD,$Modular_Class);
    }
    private function Modular_power_Test($table){
    	$Modular=$table.'power';
    	$setting=C('MYGROUP.setting')['power'];
    	!isset($setting[$Modular][M_CONTROLLER]) && $setting[$Modular][M_CONTROLLER]=[];
    	if(!in_array(M_METHOD ,$setting[$Modular][M_CONTROLLER])){
    		$this->err('您没有权限操作');
    	}
    }
}
?>
