<?php
// +----------------------------------------------------------------------
// | YZCMS <XueHen CMS>
// +----------------------------------------------------------------------
// | Copyright (c) http://www.xuehen.org
// +----------------------------------------------------------------------
// | Author XueHen <490860758@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

class SetController extends BaseController{

	//修改成功
	function edit(){
		unset($_POST['button']);
		$webset = $_POST;
		
		foreach($webset as $k => $v){	
			if(isset($_POST[$k])){
				if($v){
					F($k,$v);
				}else{
					F($k,' ');
				}
			}
		}
		$this->success('修改成功');
	}
	
	function url(){
		$url = F('url');
		if(!$url){
			$url = array('curl'=>'',$purl=>'','aurl'=>'','url'=>1);
		}
		$this->url = $url;
		$this->display();
	}
	
	function set(){
		$url = I('url');
		//获取URL内容
		$curl = I('curl') ? I('curl') : '/{url}/';
		$purl = I('purl') ? I('purl') : '/{url}/{page}/';
		$aurl = I('aurl') ? I('aurl') : '/read/{url}/';
		//写入URL
		$url_arr = array('curl'=>$curl,'purl'=>$purl,'aurl'=>$aurl,'url'=>$url);
		F('url',$url_arr);
		$aurl = trim($aurl,'/');
		$curl = trim($curl,'/');
		$purl = trim($purl,'/');
		//如果是伪静态模式
		if($url == 2){
			
			$str = "
			<?php
				return array(
					'URL_MODEL'          => '2', //URL模式
					'URL_ROUTER_ON'   => true,
					'URL_ROUTE_RULES' => array(
					   'search'        => 'Search/index',
					   '/^{aurl}$/'        => 'Content/index?id=:1',
					   '/^{purl}$/'        => 'Category/index?id=:1&p=:2',
					   '/^{curl}$/'        => 'Category/index?id=:1',
					)
				);
			";
			//替换URL
			$curl = str_replace('{url}','([a-zA-Z]+)',$curl);
			$curl = str_replace('/','\/',$curl);
			$purl = str_replace('/','\/',$purl);
			$purl = str_replace('{url}','([a-zA-Z]+)',$purl);
			$purl = str_replace('{page}','(\d+)',$purl);
			$aurl = str_replace('/','\/',$aurl);
			$aurl = str_replace('{url}','([a-zA-Z0-9]+)',$aurl);
			//替换规则
			$str = str_replace('{curl}',$curl,$str);
			$str = str_replace('{purl}',$purl,$str);
			$str = str_replace('{aurl}',$aurl,$str);
			
		}else{
			$str = "
			<?php
				return array(
					'URL_MODEL'          => '".$url."', //URL模式
				);
			";
		}
		
		$path = APP_PATH.'Home/Conf/config.php';
		$fp=fopen($path,'w');
		fwrite($fp,$str);
		fclose($fp);
		$this->success('设置成功，清除缓存即可生效','url');
		
	}
}