<?php

class settingAction extends BackendAction {

    public function _initialize() {
        parent::_initialize();
        $this->_mod = D('setting');		
    }

    public function index() {
        $type = $this->_get('type', 'trim', 'index');		
        $this->display($type);
    }
		
	
	
	public function cache() {
		$config_file = CONF_PATH . 'config.php';
        $config = require $config_file;
		if(!get_extension_funcs("memcache")){
		$info = '<span class="red">系统检测到您的服务器不支持memcache，请选择使用file方式。点击查看具体教程 -> <a href="https://cms.douhao.com/bangzhu/1.html" target="_blank">点击查看教程</a></span>';
		}else{
		$info = '<span class="red">系统检测到您的服务器支持memcache，可以选择memcache方式。点击查看具体教程 -> <a href="https://cms.douhao.com/bangzhu/1.html" target="_blank">点击查看教程</a></span>';
		}
		$this->assign('config', $config);		
		$this->assign('info', $info);
        $this->display();
    }		
	
    public function edit() {
	    $config_file = CONF_PATH . 'config.php';
        $config = require $config_file;	
		$setting = $this->_post('setting', ',');
		$c_name = $setting['c_name'];
		$url_set = $setting['url_set'];
		$single_name = $setting['single_name'];		
		if(!$c_name){
		$c_name = C('ftx_c_name');	
		}
		if(!$c_name){
		$c_name = 'wen';	
		}		
		if(!$single_name){
		$single_name = C('ftx_single_name');	
		}
		if(!$single_name){
		$single_name = 'single';	
		}
		$tpl_file = CONF_PATH . 'index/config.php';
        $tpl = include $tpl_file;
		if($tpl['DEFAULT_THEME'] == 'qiyecms'){
		$upry = 'news/view?id=:1';	
		}else{
		$upry = 'index/wen?id=:1';	
		}
		if($url_set==1){
		$c_name = '(\w+)';	
		if($tpl['DEFAULT_THEME'] == 'qiyecms'){
		$upry = 'news/view?cat=:1&id=:2';
		}else{
		$upry = 'index/wen?cat=:1&id=:2';	
		}
		}		
		if($url_set){	
		$wriurl = array(
			'/^index\/$/' => 'index/index',    
			'/^to$/' => 'to/index',
			'/^'.$single_name.'\/(\w+)$/' => 'single/index?alias=:1',	
			'/^weixinpay\/index$/' => 'weixinpay/index',
			'/^weixinpay\/index\/info\/(\w+)$/' => 'weixinpay/index?info=:1',
			'/^weixinpay\/jump$/' => 'weixinpay/jump',
			'/^weixinpay\/jump\/info\/(\w+)$/' => 'weixinpay/jump?info=:1',
			'/^notify\/index$/' => 'notify/index',
			'/^notify\/paycopy$/' => 'notify/paycopy',
			'/^notify\/paydown$/' => 'notify/paydown',			
			'/^ipay\/index$/' => 'ipay/index',
			'/^ipay\/returnurl$/' => 'ipay/returnurl',
			'/^ipay\/notifyurl$/' => 'ipay/notifyurl',						
			'/^(\d+)$/' => 'index/index?p=:1', 				
			'/^rss$/' => 'tag/index', 
			'/^rss\/(\d+)$/' => 'tag/index?p=:1',		
			'/^rs\/(.*?)\/(\d+)$/' => 'rs/index?k=:1&p=:2',		
			'/^rs\/(.*?)$/' => 'rs/index?k=:1',		
			'/^(\w+)_(\d+)$/' => 'index/cate?ename=:1&p=:2',
			'/^(\w+)$/' => 'index/cate?ename=:1',	
			'/^'.$c_name.'\/(\w+)$/' => $upry,	
		);
		if($tpl['DEFAULT_THEME'] == 'qm'){
		$wriurl = array(
			'/^index\/$/' => 'index/index',    
			'/^to$/' => 'to/index',
			'/^'.$single_name.'\/(\w+)$/' => 'single/index?alias=:1',	
			'/^weixinpay\/index$/' => 'weixinpay/index',
			'/^weixinpay\/index\/info\/(\w+)$/' => 'weixinpay/index?info=:1',
			'/^weixinpay\/jump$/' => 'weixinpay/jump',
			'/^weixinpay\/jump\/info\/(\w+)$/' => 'weixinpay/jump?info=:1',
			'/^notify\/index$/' => 'notify/index',
			'/^notify\/paycopy$/' => 'notify/paycopy',
			'/^notify\/paydown$/' => 'notify/paydown',
			'/^notify\/payqm$/' => 'notify/payqm',
			'/^ipay\/index$/' => 'ipay/index',
			'/^ipay\/returnurl$/' => 'ipay/returnurl',
			'/^ipay\/notifyurl$/' => 'ipay/notifyurl',
			'/^bjx$/' => 'bjx/index',
			'/^mz$/' => 'mz/dafen',
			'/^mz\/([+%A-Za-z0-9]+)$/' => 'mz/index?k=:1',
			'/^qm\/bbqm$/' => 'qm/bbqm',
			'/^qm\/([+%A-Za-z0-9]+)$/' => 'qm/index?xing=:1',
			'/^qm\/([+%A-Za-z0-9]+)\/p(\d+)$/' => 'qm/index?xing=:1&p=:2',
			'/^qm\/([+%A-Za-z0-9]+)\/(\d+)$/' => 'qm/index?xing=:1&sex=:2',
			'/^qm\/([+%A-Za-z0-9]+)\/(\d+)\/p(\d+)$/' => 'qm/index?xing=:1&sex=:2&p=:3',
			'/^qm\/([+%A-Za-z0-9]+)\/(\d+)\/(\d+)$/' => 'qm/index?xing=:1&sex=:2&num=:3',
			'/^qm\/([+%A-Za-z0-9]+)\/(\d+)\/(\d+)\/p(\d+)$/' => 'qm/index?xing=:1&sex=:2&num=:3&p=:4',
			'/^qm\/([+%A-Za-z0-9]+)\/(\d+)\/(\d+)\/([+%A-Za-z0-9]+)$/' => 'qm/index?xing=:1&sex=:2&num=:3&zi=:4',
			'/^qm\/([+%A-Za-z0-9]+)\/(\d+)\/(\d+)\/([+%A-Za-z0-9]+)\/p(\d+)$/' => 'qm/index?xing=:1&sex=:2&num=:3&zi=:4&p=:5',
			'/^order\/([+%A-Za-z0-9]+)$/' => 'qiming/index?order=:1',
			'/^order\/([+%A-Za-z0-9]+)\/(\d+)$/' => 'qiming/index?order=:1&p=:2',
			'/^qiming\/([+%A-Za-z0-9]+)$/' => 'qiming/index?xing=:1',
			'/^qiming\/([+%A-Za-z0-9]+)\/p(\d+)$/' => 'qiming/index?xing=:1&p=:2',
			'/^qiming\/([+%A-Za-z0-9]+)\/(\d+)$/' => 'qiming/index?xing=:1&sex=:2',
			'/^qiming\/([+%A-Za-z0-9]+)\/(\d+)\/p(\d+)$/' => 'qiming/index?xing=:1&sex=:2&p=:3',
			'/^qiming\/([+%A-Za-z0-9]+)\/(\d+)\/(\d+)$/' => 'qiming/index?xing=:1&sex=:2&num=:3',
			'/^qiming\/([+%A-Za-z0-9]+)\/(\d+)\/(\d+)\/p(\d+)$/' => 'qiming/index?xing=:1&sex=:2&num=:3&p=:4',
			'/^qiming\/([+%A-Za-z0-9]+)\/(\d+)\/(\d+)\/([+%A-Za-z0-9]+)$/' => 'qiming/index?xing=:1&sex=:2&num=:3&zi=:4',
			'/^qiming\/([+%A-Za-z0-9]+)\/(\d+)\/(\d+)\/([+%A-Za-z0-9]+)\/p(\d+)$/' => 'qiming/index?xing=:1&sex=:2&num=:3&zi=:4&p=:5',			
			'/^(\d+)$/' => 'index/index?p=:1', 				
			'/^rss$/' => 'tag/index', 
			'/^rss\/(\d+)$/' => 'tag/index?p=:1',		
			'/^rs\/(.*?)\/(\d+)$/' => 'rs/index?k=:1&p=:2',		
			'/^rs\/(.*?)$/' => 'rs/index?k=:1',		
			'/^(\w+)_(\d+)$/' => 'index/cate?ename=:1&p=:2',
			'/^(\w+)$/' => 'index/cate?ename=:1',	
			'/^'.$c_name.'\/(\w+)$/' => $upry,	
		);	
		}
		if($tpl['DEFAULT_THEME'] == 'qiyecms'){			
		$wriurl = array(
			'/^index\/$/' => 'index/index',    			
			'/^liuyan$/' => 'liuyan/index',
			'/^news$/' => 'news/index',
			'/^news\/p(\d+)$/' => 'news/index?p=:1',			
			'/^news\/(\w+)\/p(\d+)$/' => 'news/cate?ename=:1&p=:2',
			'/^news\/(\w+)$/' => 'news/cate?ename=:1',		
			'/^news\/search\/(.*?)\/(\d+)$/' => 'news/search?k=:1&p=:2',
			'/^news\/search\/(.*?)$/' => 'news/search?k=:1',					
			'/^items$/' => 'items/index',
			'/^items\/p(\d+)$/' => 'items/index?p=:1',
			'/^items\/(\d+)$/' => 'items/view?id=:1',
			'/^items\/(\w+)\/p(\d+)$/' => 'items/cate?ename=:1&p=:2',
			'/^items\/(\w+)$/' => 'items/cate?ename=:1',		
			'/^items\/search\/(.*?)\/(\d+)$/' => 'items/search?k=:1&p=:2',
			'/^items\/search\/(.*?)$/' => 'items/search?k=:1',	
			'/^case$/' => 'case/index',
			'/^case\/p(\d+)$/' => 'case/index?p=:1',
			'/^case\/(\d+)$/' => 'case/view?id=:1',
			'/^case\/(\w+)\/p(\d+)$/' => 'case/cate?ename=:1&p=:2',
			'/^case\/(\w+)$/' => 'case/cate?ename=:1',		
			'/^case\/search\/(.*?)\/(\d+)$/' => 'case/search?k=:1&p=:2',
			'/^case\/search\/(.*?)$/' => 'case/search?k=:1',	
			'/^'.$single_name.'\/(\w+)$/' => 'single/index?alias=:1',				
			'/^(\w+)\/p(\d+)$/' => 'news/cate?ename=:1&p=:2',
            '/^(\w+)$/' => 'news/cate?ename=:1',
			'/^'.$c_name.'\/(\w+)$/' => $upry,
		);	
		}	
		}
		
		if($wriurl){
		$url_config_file = CONF_PATH . 'url.php';
        $url_config = require $url_config_file;		
		$new_url_config = array(
                'URL_MODEL' => 2,
                'URL_HTML_SUFFIX' => $url_config['URL_HTML_SUFFIX'],
                'URL_PATHINFO_DEPR' => $url_config['URL_PATHINFO_DEPR'],
				'URL_ROUTE_RULES' => $wriurl
		);		
		$this->update_config($new_url_config, $url_config_file);
		}
        $cache_model = $this->_post('cache_mode', 'trim');            
        $cache_time = $this->_post('cache_time', 'trim');
        $new_config = array(
                'DATA_CACHE_TYPE' => $cache_model,
                'DATA_CACHE_TIME' => $cache_time,                
        );
        if($cache_model && $cache_time){
        $this->update_config($new_config, $config_file);
		}
        		
        foreach ($setting as $key => $val) {
            $val = is_array($val) ? serialize($val) : $val;           
		  if($this->_mod->where(array('name' => $key))->count()){
              $this->_mod->where(array('name' => $key))->save(array('data' => $val));  
          }else{
              $this->_mod->add(array('name' => $key,'data' => $val));
          }
		  if($key == 'site_url'){
			  $this->_api = new douhaocms();
			  $it = parse_url($val);
			  $this->_api->sendurl($it['host']);
		  }
        }
        $type = $this->_post('type', 'trim', 'index');
        $this->success(L('operation_success'));
		
    }

    

	public function ajax_upload( ){		
        if(!empty( $_FILES['img']['name'])){
            $result = $this->_upload( $_FILES['img'], "site/" );
            if ( $result['error']){
                $this->error( $result['info'] );
            }else{
                $data['img'] = $result['info'][0]['savename'];
                $this->ajaxReturn( 1, L( "operation_success" ), C( "ftx_attach_path" )."site/".$data['img'] );
            }
        }else{
            $this->ajaxReturn( 0, L( "illegal_parameters" ) );
        }
    }

}