<?php
/**
 * 链接规则管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 
namespace Admin\Controller;
class UrlruleController extends BaseController{
	public function _initialize(){
		parent::_initialize ();
        $configfile=APP_PATH.'Home/Conf/config.php';
        $this->config=require($configfile);
        $this->basetpl=APP_PATH.'Home/'.C('DEFAULT_V_LAYER').'/'.get_default_theme().'/';
    }
    public function _before_index(){        
        $big_menu = array('添加规则' => U('add'));
        $this->assign('big_menu', $big_menu);
    }
    public function config(){
        $config=$this->config;
        if(IS_POST){
            $postdata=I('post.');
            $config['VENDOR_URL']=$postdata['vendorurl'];
            $config['URL_MODEL']=$postdata['model_url'];
            $config['VENDOR_PATH']=$postdata['vendorpath'];
            $config['URL_ROUTER_ON']=$postdata['urlrouter'];
            if(!empty($postdata['urlrouter'])){                
                if(!empty($postdata['urlrouterrules'])){
                    $config['URL_ROUTE_RULES']=array();
                    $routerarr=array_filter(explode(PHP_EOL,$postdata['urlrouterrules']));
                    foreach($routerarr as $k =>$v){
                        $dataarr=explode('=>',htmlspecialchars_decode($v));
                        if(!empty($dataarr[0]) && !empty($dataarr[1]))$config['URL_ROUTE_RULES'][$dataarr[0]]=str_exists($dataarr[1],'array')?array_map('trim',explode(',',strtr($dataarr[1],array('array'=>'','('=>'',')'=>'','"'=>'',"'"=>'')))):$dataarr[1];
                    }
                }
                if(!empty($postdata['urlmaprules'])){
                    $config['URL_MAP_RULES']=array();
                    $routerarr=array_filter(explode(PHP_EOL,$postdata['urlmaprules']));
                    foreach($routerarr as $k =>$v){
                        $dataarr=explode('=>',htmlspecialchars_decode($v));
                        if(!empty($dataarr[0]) && !empty($dataarr[1]))$config['URL_MAP_RULES'][$dataarr[0]]=str_exists($dataarr[1],'array')?array_map('trim',explode(',',strtr($dataarr[1],array('array'=>'','('=>'',')'=>'','"'=>'',"'"=>'')))):$dataarr[1];
                    }
                }
            }
            $config['HTML_CACHE_ON']=$postdata['htmlcache'];
            if(!empty($postdata['htmlcache'])){
                $config['HTML_CACHE_TIME']=$postdata['htmlcachetime'];
                if(!empty($postdata['htmlcacherules'])){
                    $config['HTML_CACHE_RULES']=array();
                    $urlrarr=array_filter(explode(PHP_EOL,$postdata['htmlcacherules']));
                    foreach($urlrarr as $k =>$v){
                        $cachearr=explode('=>',htmlspecialchars_decode($v));
                        if(!empty($cachearr[0]) && !empty($cachearr[1]))$config['HTML_CACHE_RULES'][$cachearr[0]]=str_exists($cachearr[1],'array')?array_map('trim',explode(',',strtr($cachearr[1],array('array'=>'','('=>'',')'=>'','"'=>'',"'"=>'')))):$cachearr[1];
                    }
                }
            }
        extentionconfig($config,'Home','config.php');
        $this->success('更新完成', U('config'));
        exit;
        }
        $this->assign('model_url', $config['URL_MODEL']);//URL模式
        $this->assign('vendorurl', $config['VENDOR_URL']);//是否启用URL静态规则
        $this->assign('vendorpath', $config['VENDOR_PATH']);//是否启用URL静态目录
        $this->assign('urlrouter', $config['URL_ROUTER_ON']);//是否开启URL路由
        $this->assign('htmlcache', $config['HTML_CACHE_ON']);//是否开启静态缓存
        $this->assign('htmlcachetime', $config['HTML_CACHE_TIME']);//静态缓存有效期
        if(!empty($config['URL_ROUTE_RULES'])){
            $urlrouterrules='';
            foreach($config['URL_ROUTE_RULES'] as $k =>$v){
                $urlrouterrules.=$k."=>".(is_array($v)?"array('".implode("','",$v)."')":$v).PHP_EOL;
                $this->assign('urlrouterrules', $urlrouterrules);
            }
        }
        if(!empty($config['URL_MAP_RULES'])){
            $urlmaprules='';
            foreach($config['URL_MAP_RULES'] as $k =>$v){
                $urlmaprules.=$k."=>".(is_array($v)?"array('".implode("','",$v)."')":$v).PHP_EOL;
                $this->assign('urlmaprules', $urlmaprules);
            }
        }
        if(!empty($config['HTML_CACHE_RULES'])){            
            $htmlcacherules='';
            foreach($config['HTML_CACHE_RULES'] as $k =>$v){                
                $htmlcacherules.=$k."=>".(is_array($v)?"array('".implode("','",$v)."')":$v).PHP_EOL;
                $this->assign('htmlcacherules', $htmlcacherules);
            }
        }        
        $this->display();
    }
}
?>