<?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 \admin\model\setting AS model;
use \temmoku\admin;
use \temmoku\db;
use \admin\public_class\cache;
class setting extends admin
{
    public function index()
    {
    	if('post'==$_POST['step']){
    		$this->post();
    	}
		$_style=['home','admin','user'];
		$style=[];
		foreach($_style as $value){
			$style[$value]=style($value);
		}
		$modular = db::select('*')->from(jab.'modular')->where('state != 0')->query();
    	$this->assign('modular', $modular);
		$this->assign('style', $style);
		$setting=(new model)->setting();
		$this->assign('setting', $setting);
       	$this->display(tpl('setting/index'));
    }

    public function article()
    {
    	if('post'===$_POST['step']){
    		$this->post();
    	}
		$setting=(new model)->setting();
		$this->assign('setting', $setting);
		$this->display(tpl('setting/article'));
    }
    
 	public function admin()
    {
    	if('post'===$_POST['step']){
    		$this->post();
    	}
    	$setting=(new model)->setting();
		$this->assign('setting', $setting);
		$this->display(tpl('setting/admin'));
    }
    
    public function stmp()
    {
    	if('post'===$_POST['step']){
    		$this->post();
    	}
		$setting=(new model)->setting();
		$this->assign('setting', $setting);
		$this->display(tpl('setting/stmp'));
    }

    public function caching(){
    	if('post'===$_POST['step']){
			if(!$_POST['web']['caching']['memcache']['host']){
				$_POST['web']['caching']['memcache']['host']='127.0.0.1';
			}
			if(!$_POST['web']['caching']['memcache']['port']){
				$_POST['web']['caching']['memcache']['port']='11211';
			}
			if(!$_POST['web']['caching']['redis']['host']){
				$_POST['web']['caching']['redis']['host']='127.0.0.1';
			}
			if(!$_POST['web']['caching']['redis']['port']){
				$_POST['web']['caching']['redis']['port']='6379';
			}
			$_POST['web']['caching']['redis']['timeout']=intval($_POST['web']['caching']['redis']['timeout']);
			$_POST['web']['caching']['memcache']['timeout']=intval($_POST['web']['caching']['memcache']['timeout']);
			if(CACHING==true){
				define('CACHING_ALLCACHE',true);
				\temmoku\vendor\think\cache::rm('conf_file');
				\temmoku\vendor\think\cache::rm('_config');
			}
			$caching=serialize($_POST['web']['caching']);
			unset($_POST);
			$_POST['web']['caching']=$caching;
    		$this->post();
    	}
    	$setting=(new model)->setting();
    	$this->assign('setting', $setting);
    	$this->display(tpl('setting/caching'));
    }
    public function Shielding_words()
    {
    	if('post'===$_POST['step']){
    		$this->post();
    	}
    	$setting=(new model)->setting();
		$this->assign('setting', $setting);
		$this->display(tpl('setting/shielding_words'));
    }
	
	public function trigger_state_words()
	{
		if('post'===$_POST['step']){
			$this->post();
		}
		
		$setting=(new model)->setting();
		$this->assign('setting', $setting);
		$this->display(tpl('setting/trigger_state_words'));
	}
	
	public function Gd(){
		if('post'===$_POST['step']){
    		$this->post();
    	}
    	$setting=(new model)->setting();
		$this->assign('setting', $setting);
		$this->display(tpl('setting/gd'));
	}
    public function mysqlslave(){//mysql主从
		if($_POST['step']=='post'){
			$mysqlslave=[];
			$_POST['web']['mysqlslave_state']=intval($_POST['web']['mysqlslave']['state']);
			foreach($_POST['web']['mysqlslave']['host'] as $key=>$dbhost){
				if(!$dbhost){
					continue;
				}
				$dbport 	=	intval($_POST['web']['mysqlslave']['port'][$key]) ? intval($_POST['web']['mysqlslave']['port'][$key]) : 3306;
				$user 		=	$_POST['web']['mysqlslave']['user'][$key] 		? $_POST['web']['mysqlslave']['user'][$key] : C('dbuser');
				$password 	=	$_POST['web']['mysqlslave']['password'][$key] 	? $_POST['web']['mysqlslave']['password'][$key] : C('dbpassword');
				$mysqlslave[]=[
					'host'=>$dbhost,
					'port'=>$dbport,
					'user'=>$user,
					'password'=>$password,
					'dbname'=> C('dbname')
				];
			}
			if($mysqlslave){
				unset($_POST['web']['mysqlslave']);
				$_POST['web']['mysqlslave']=serialize($mysqlslave);
				$this->post();
			}
		}
		$setting=(new model)->setting();
		$this->assign('setting', $setting);
		$this->display(tpl('setting/mysqlslave'));
	}
	
	public function del_bad_slave(){
		$file=APP_PATH.'conf/bad_slave_'.md5(c('MD5')).".conf";
		\unlink($file);
		cache::caching();
	}
	
	private function post()
    {
    	if(!$_POST['web']['function_m']){
    		$dir='tmp/M';
    		delete_dir($dir);
    	}
    	if($_POST['web']['weburl']){
    		$parse_url=parse_url ( $_POST['web']['weburl']);
			$parse_url['host'] ? $_POST['web']['weburl']=$parse_url['host'] : $_POST['web']['weburl']=$parse_url['path'];
			$_POST['web']['weburl']=trim(trim($_POST['web']['weburl'],'/'));
    	}
    	(new model)->post();
    	//生成一些必要的缓存
    	cache::setting_cache();
    	$this->Success('修改成功');
    }
     public function allcache(){
		cache::caching();
    	//先更新核心缓存
    	cache::all();
    	//更新模块缓存
    	$modular=db::select('*')->from(jab.'modular')->query();
    	if($modular){
    		foreach($modular AS $key=>$row){
				delete_dir(APP_PATH.$row['dir'].DS.'conf'.DS);
	    		$modular_file= APP_PATH.$row['dir']."/public_class/cache.php";
	    		if(is_file($modular_file)){
	    			//加载此文件。
	    			include_once $modular_file;
	    			//执行相关操作
	    			$controller=$row['dir']."\public_class\cache";
	    			$controller::run($row);
	    		}
	    	}
    	}
    	//更新插件缓存
    	$plugin=db::select('*')->from(jab.'plugin')->query();
    	if($plugin){
    		foreach($plugin AS $key=>$row){
				delete_dir(APP_PATH.'plugin'.DS.$row['dir'].DS.'conf'.DS);
	    		$file= APP_PATH.'plugin/'.$row['dir']."/public_class/cache.php";
	    		if(is_file($file)){
	    			//加载此文件。
	    			include_once $file;
	    			//执行相关操作
	    			$controller='plugin\\'.$row['dir']."\public_class\cache";
	    			$controller::run($row);
	    		}
	    	}
    	}
    	$this->success('缓存更新成功','/admin/index/map');
    }
    
    
    public function home(){
    	if('post'===$_POST['step']){
    		$this->post();
    	}
    	$setting=(new model)->setting();
		$this->assign('setting', $setting);
		$this->display(tpl('setting/home'));
    }
}
?>