<?php
header( "Content-Type:text/html; charset=UTF-8 ");
if(!defined('APP_PATH')||!defined('I_PATH')){exit('Access Denied');}

class ld_sys extends controllerBase
{
	function __construct(){
		parent::__construct();
		$this->a=$this->sysParams('a',1);
	}
	function index(){
		$this->show_tab=$_REQUEST['d'];
		
		if($this->sysParams('run')==1){
			$configfile='config.php';
			$fp_tp=@fopen($configfile,"r");
			$fp_txt=@fread($fp_tp,filesize($configfile));
			@fclose($fp_tp);
			$config=array('imgwater_url','http_path','site_logo','login_logo','site_title','site_keywords','site_description','view_themes','site_html','site_html_rules','cache_auto','cache_time','filetype','filesize','imgwater','imgwater_t','imgcaling','img_w','img_h','comment_audit','comment_user','rewrite_open','qq_sync','weibo_sync');
			if($this->show_tab == "sys"){ syDB("sysconfig")->update(array('code'=>sysconfig_complate),array('value'=>1));}//1代表已经设置完系统信息
			if($this->show_tab == "mobile"){ syDB("sysconfig")->update(array('code'=>set_phone),array('value'=>1));}//1代表已经设置完手机相关内容
			foreach($config as $v){
				if (strpos(',site_html,cache_auto,weibo_sync,qq_sync,cache_time,filesize,imgwater,imgwater_t,imgcaling,img_w,img_h,comment_audit,comment_user,',$v)){
					$txt=$this->sysParams($v);if($v=='filesize'){$txt=$txt*1024;}
					$fp_txt=preg_replace("/'".$v."' => .*?,/","'".$v."' => ".$txt.",",$fp_txt);
				}else if($v=='filetype'){
					$fp_txt=preg_replace("/'".$v."' => '.*?',/","'".$v."' => '".strtolower($this->sysParams($v,1))."',",$fp_txt);
				}else if($v=='site_html_rules'){
					if(stripos($this->sysParams($v,1),'[id]')===false&&stripos($this->sysParams($v,1),'[file]')===false){
					message_a("静态规则中[id]与[file]必须至少包含一个");
					}
					if(stripos($this->sysParams($v,1),'.')===false){
					message_a("静态规则中必须包含“.”和后缀名");
					}
					$fp_txt=preg_replace("/'site_html_rules' => '.*?',/","'site_html_rules' => '".$this->sysParams($v,1)."',",$fp_txt);
				}		//TODO   site_html_dir
					/*
				else if($v=='site_html_dir'){
			
					if(preg_match("/^[a-zA-Z0-9_\/]*$/",$this->sysParams($v,1))==0)message_a("生成目录只能为英文、数字、下划线和/组成");
					if($this->sysParams($v,1)=='/'){
						$dir=$this->sysParams($v,1);
					}else if($this->sysParams($v,1)==''){
						$dir='html';
					}else{
						$dir=trim($this->sysParams($v,1),'/');
					}
					$fp_txt=preg_replace("/'".$v."' => '.*?',/","'".$v."' => '".strtolower($dir)."',",$fp_txt);
					
					
				}*/
				else if($v=='site_html_rules'){
					if(preg_match("/^[a-zA-Z0-9_\.\[\]\/]*$/",$this->sysParams($v,1))==0)message_a("生成规则只能为英文、数字、下划线、点和/组成");
					if($this->sysParams($v,1)==''){
						$dir='[y]/[m]/[id].html';
					}else{
						$dir=trim($this->sysParams($v,1),'/');
					}
					$fp_txt=preg_replace("/'".$v."' => '.*?',/","'".$v."' => '".strtolower(trim($this->sysParams($v,1),'/'))."',",$fp_txt);
				}else{
					
					$fp_txt=preg_replace("/'".$v."' => '.*?',/","'".$v."' => '".$this->sysParams($v,1)."',",$fp_txt);
				}
			}
			$fp_txt=preg_replace("/'textwater' => '.*?',/","'textwater' => '".$this->sysParams('textwater',1)."',",$fp_txt);
			$fp_txt=preg_replace("/'textwateryn' => .*?,/","'textwateryn' => ".$this->sysParams('textwateryn',1).",",$fp_txt);
			$fp_txt=preg_replace("/'img_text_r' => .*?,/","'img_text_r' => ".$this->sysParams('img_text_r',1).",",$fp_txt);
			$fp_txt=preg_replace("/'img_text_g' => .*?,/","'img_text_g' => ".$this->sysParams('img_text_g',1).",",$fp_txt);
			$fp_txt=preg_replace("/'img_text_b' => .*?,/","'img_text_b' => ".$this->sysParams('img_text_b',1).",",$fp_txt);
			$fp_txt=preg_replace("/'textwater_size' => .*?,/","'textwater_size' => ".$this->sysParams('textwater_size',1).",",$fp_txt);
			
			
			
			$fpt_tpl=@fopen($configfile,"w");
			
			@fwrite($fpt_tpl,$fp_txt);
			@fclose($fpt_tpl);
			
			$incfile='include/inc.php';
			$inc_tp=@fopen($incfile,"r");
			$inc_txt=@fread($inc_tp,filesize($incfile));
			@fclose($inc_tp);
		
			$inc=array('rewrite_open','rewrite_article','rewrite_article_type','rewrite_product','rewrite_product_type','rewrite_message_type','rewrite_special','rewrite_labelcus_custom','rewrite_dir');
			foreach($inc as $v){
				$vt=strtolower(trim($this->sysParams($v,1),'/'));
				if(preg_match("/^[a-zA-Z0-9_\{\}\.\-\/]*$/",$vt)==0)message_a("伪静态规则只能包含英文、数字、下划线、点、中划线、{、}、/");
				if($v=='rewrite_article'){//||stripos($vt,'{article}')===false)
					if((stripos($vt,'{id}')===false&&stripos($vt,'{file}')===false)){
					message_a("article规则中{id}与{file}必须至少包含一个，并且必须包含{article}");
					}
				}
				if($v=='rewrite_article_type'){//||stripos($vt,'{article}')===false||stripos($vt,'{type}')===false
					if((stripos($vt,'{tid}')===false&&stripos($vt,'{file}')===false)||stripos($vt,'{page}')===false){
					message_a("article栏目规则中{tid}与{file}必须至少包含一个，并且必须包含{page}与{article}与{type}");
					}
				}
				/*
				if($v=='rewrite_product'){//||stripos($vt,'{product}')===false
					if((stripos($vt,'{id}')===false&&stripos($vt,'{file}')===false)){
					message_a("product规则中{id}与{file}必须至少包含一个，并且必须包含product}");
					}
				}
				
				if($v=='rewrite_product_type'){//stripos($vt,'{product}')===false||
					if((stripos($vt,'{tid}')===false&&stripos($vt,'{file}')===false)||stripos($vt,'{page}')===false||stripos($vt,'{type}')===false){
					message_a("product栏目规则中{tid}与{file}必须至少包含一个，并且必须包含{page}与{product}与{type}");
					}
				}
				
				if($v=='rewrite_message_type'){//||stripos($vt,'{message}')===false
					if((stripos($vt,'{tid}')===false&&stripos($vt,'{file}')===false)||stripos($vt,'{page}')===false||stripos($vt,'{type}')===false){
					message_a("message规则中{tid}与{file}必须至少包含一个，并且必须包含{page}与{message}与{type}");
					}
				}
				if($v=='rewrite_special'){//||stripos($vt,'{special}')===false
					if((stripos($vt,'{sid}')===false&&stripos($vt,'{file}')===false)||stripos($vt,'{page}')===false){
					message_a("专题规则中{sid}与{file}必须至少包含一个，并且必须包含{page}与{special}");
					}
				}
				if($v=='rewrite_labelcus_custom'){
					if(stripos($vt,'{file}')===false){
					message_a("自定义页面规则中必须含有{file}");
					}
				}*/
				if($v=='rewrite_open'){
					$inc_txt=preg_replace("/'".$v."' => .*?,/","'".$v."' => ".$vt.",",$inc_txt);
				}else{
					$inc_txt=preg_replace("/'".$v."' => '.*?',/","'".$v."' => '".$vt."',",$inc_txt);
				}
				if($v=='rewrite_dir'){
					if($vt==''){$vt='/';}else{$vt='/'.$vt.'/';}
					$inc_txt=preg_replace("/'".$v."' => .*?,/","'".$v."' => '".$vt."',",$inc_txt);
				}
			}
			$inc_txt=preg_replace("/'mode' => '.*?',/","'mode' => '".$this->sysParams('mode',1)."',",$inc_txt);
			$inc_txt=preg_replace("/'vercode' => .*?,/","'vercode' => ".$this->sysParams('vercode',1).",",$inc_txt);
			$inc_tpl=@fopen($incfile,"w");
			@fwrite($inc_tpl,$inc_txt);
			@fclose($inc_tpl);
			$stat_code=syDB('sysconfig');
			$stat_code->update(array('code'=>'stat_code'),array('value'=>$this->sysParams('stat_code',1)));
			if($this->sysParams("qq_sync")==1) //设置成同步QQ
				{
					if(!isset($_SESSION['openid']))
					{
						//$this->go_qqlogin(1);
					}
				}
			message_a("系统设置修改成功",'?c=ld_sys');
		}
		$this->toptxt='系统设置';
		$this->d=$GLOBALS['LD_I']['ext'];
		$this->m=$GLOBALS['LD_I']['mobile'];
		$stat_code=syDB('sysconfig')->find("code='stat_code'");
		$this->stat_code=$stat_code['value'];
		$this->r=$GLOBALS['LD_I']['rewrite'];
		$this->inc=$GLOBALS['LD_I'];
		$this->display("sys.html");
	}
	function rewrite(){
		$url_article=preg_match_all('/\{(.*?)\}/si',$this->sysParams('article',1),$r_article);
		$url_article_type=preg_match_all('/\{(.*?)\}/si',$this->sysParams('article_type',1),$r_article_type);
		$url_product=preg_match_all('/\{(.*?)\}/si',$this->sysParams('product',1),$r_product);
		$url_product_type=preg_match_all('/\{(.*?)\}/si',$this->sysParams('product_type',1),$r_product_type);
		$url_message_type=preg_match_all('/\{(.*?)\}/si',$this->sysParams('message_type',1),$r_message_type);
		$url_special=preg_match_all('/\{(.*?)\}/si',$this->sysParams('special',1),$r_special);
		$url_labelcus_custom=preg_match_all('/\{(.*?)\}/si',$this->sysParams('labelcus_custom',1),$r_labelcus_custom);
		
		$r_article=$this->rewrite_for($r_article,$this->sysParams('article',1));
		$r_article_type=$this->rewrite_for($r_article_type,$this->sysParams('article_type',1));
		$r_product=$this->rewrite_for($r_product,$this->sysParams('product',1));
		$r_product_type=$this->rewrite_for($r_product_type,$this->sysParams('product_type',1));
		$r_message_type=$this->rewrite_for($r_message_type,$this->sysParams('message_type',1));
		$r_special=$this->rewrite_for($r_special,$this->sysParams('special',1));
		$r_labelcus_custom=$this->rewrite_for($r_labelcus_custom,$this->sysParams('labelcus_custom',1));
				
		//apache独立主机规则
		$at=str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_article[1]));
		$this->apache='RewriteCond %{QUERY_STRING} ^(.*)$'."\r\n";
		$this->apache.='RewriteRule ^(.*)/'.str_ireplace('.','\.',$r_article[0]).'$ $1/index.php?'.$at.'&%1'."\r\n";
		
		$at=str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_article_type[1]));
		$this->apache.='RewriteCond %{QUERY_STRING} ^(.*)$'."\r\n";
		$this->apache.='RewriteRule ^(.*)/'.str_ireplace('.','\.',$r_article_type[0]).'$ $1/index.php?'.$at.'%1'."\r\n";
		
		$at=str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_product[1]));
		$this->apache.='RewriteCond %{QUERY_STRING} ^(.*)$'."\r\n";
		$this->apache.='RewriteRule ^(.*)/'.str_ireplace('.','\.',$r_product[0]).'$ $1/index.php?'.$at.'&%1'."\r\n";
		
		$at=str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_product_type[1]));
		$this->apache.='RewriteCond %{QUERY_STRING} ^(.*)$'."\r\n";
		$this->apache.='RewriteRule ^(.*)/'.str_ireplace('.','\.',$r_product_type[0]).'$ $1/index.php?'.$at.'%1'."\r\n";
		
		$at=str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_message_type[1]));
		$this->apache.='RewriteCond %{QUERY_STRING} ^(.*)$'."\r\n";
		$this->apache.='RewriteRule ^(.*)/'.str_ireplace('.','\.',$r_message_type[0]).'$ $1/index.php?'.$at.'%1'."\r\n";
		
		$at=str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_special[1]));
		$this->apache.='RewriteCond %{QUERY_STRING} ^(.*)$'."\r\n";
		$this->apache.='RewriteRule ^(.*)/'.str_ireplace('.','\.',$r_special[0]).'$ $1/index.php?'.$at.'%1'."\r\n";
		
		$at=str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_labelcus_custom[1]));
		$this->apache.='RewriteCond %{QUERY_STRING} ^(.*)$'."\r\n";
		$this->apache.='RewriteRule ^(.*)/'.str_ireplace('.','\.',$r_labelcus_custom[0]).'$ $1/index.php?'.$at.'%1'."\r\n";
		
		//apache虚拟主机规则
		$this->apache1='RewriteCond %{QUERY_STRING} ^(.*)$'."\r\n";
		$this->apache1.='RewriteRule ^'.str_ireplace('.','\.',$r_article[0]).'$ index.php?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$this->thisnumjian($r_article[1]))).'&%1'."\r\n";
		$this->apache1.='RewriteCond %{QUERY_STRING} ^(.*)$'."\r\n";
		$this->apache1.='RewriteRule ^'.str_ireplace('.','\.',$r_article_type[0]).'$ index.php?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$this->thisnumjian($r_article_type[1]))).'%1'."\r\n";
		$this->apache1.='RewriteCond %{QUERY_STRING} ^(.*)$'."\r\n";
		$this->apache1.='RewriteRule ^'.str_ireplace('.','\.',$r_product[0]).'$ index.php?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$this->thisnumjian($r_product[1]))).'&%1'."\r\n";
		$this->apache1.='RewriteCond %{QUERY_STRING} ^(.*)$'."\r\n";
		$this->apache1.='RewriteRule ^'.str_ireplace('.','\.',$r_product_type[0]).'$ index.php?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$this->thisnumjian($r_product_type[1]))).'%1'."\r\n";
		$this->apache1.='RewriteCond %{QUERY_STRING} ^(.*)$'."\r\n";
		$this->apache1.='RewriteRule ^'.str_ireplace('.','\.',$r_message_type[0]).'$ index.php?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$this->thisnumjian($r_message_type[1]))).'%1'."\r\n";
		$this->apache1.='RewriteCond %{QUERY_STRING} ^(.*)$'."\r\n";
		$this->apache1.='RewriteRule ^'.str_ireplace('.','\.',$r_special[0]).'$ index.php?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$this->thisnumjian($r_special[1]))).'%1'."\r\n";
		$this->apache1.='RewriteCond %{QUERY_STRING} ^(.*)$'."\r\n";
		$this->apache1.='RewriteRule ^'.str_ireplace('.','\.',$r_labelcus_custom[0]).'$ index.php?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$this->thisnumjian($r_labelcus_custom[1]))).'%1'."\r\n";
		
		
		//iis规则
		$n=$r_article[2]+1;
		$this->iis='RewriteRule ^(.*)/'.str_ireplace('.','\.',$r_article[0]).'(\?(.*))*$ $1/index\.php\?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_article[1])).'&$'.$n."\r\n";
		$n=$r_article_type[2]+1;
		$this->iis.='RewriteRule ^(.*)/'.str_ireplace('.','\.',$r_article_type[0]).'(\?(.*))*$ $1/index\.php\?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_article_type[1])).'&$'.$n."\r\n";
		$n=$r_product[2]+1;
		$this->iis.='RewriteRule ^(.*)/'.str_ireplace('.','\.',$r_product[0]).'(\?(.*))*$ $1/index\.php\?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_product[1])).'&$'.$n."\r\n";
		$n=$r_product_type[2]+1;
		$this->iis.='RewriteRule ^(.*)/'.str_ireplace('.','\.',$r_product_type[0]).'(\?(.*))*$ $1/index\.php\?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_product_type[1])).'&$'.$n."\r\n";
		$n=$r_message_type[2]+1;
		$this->iis.='RewriteRule ^(.*)/'.str_ireplace('.','\.',$r_message_type[0]).'(\?(.*))*$ $1/index\.php\?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_message_type[1])).'&$'.$n."\r\n";
		$n=$r_special[2]+1;
		$this->iis.='RewriteRule ^(.*)/'.str_ireplace('.','\.',$r_special[0]).'(\?(.*))*$ $1/index\.php\?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_special[1])).'&$'.$n."\r\n";
		$n=$r_labelcus_custom[2]+1;
		$this->iis.='RewriteRule ^(.*)/'.str_ireplace('.','\.',$r_labelcus_custom[0]).'(\?(.*))*$ $1/index\.php\?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_labelcus_custom[1])).'&$'.$n."\r\n";
		
		//iis7规则
		$this->iis7='&lt;rule name="article"&gt;'."\r\n";
		$this->iis7.='	&lt;match url="^(.*/)*'.$r_article[0].'\?*(.*)$" /&gt;'."\r\n";
		$this->iis7.='	&lt;action type="Rewrite" url="{R:1}/index.php\?'.str_ireplace('[-|', '{R:',str_ireplace('|-]', '}',$r_article[1])).'&{R:'.$r_article[2].'}" /&gt;'."\r\n";
		$this->iis7.='&lt;/rule&gt;'."\r\n";
		
		$this->iis7.='&lt;rule name="article_type"&gt;'."\r\n";
		$this->iis7.='	&lt;match url="^(.*/)*'.$r_article_type[0].'\?*(.*)$" /&gt;'."\r\n";
		$this->iis7.='	&lt;action type="Rewrite" url="{R:1}/index.php\?'.str_ireplace('[-|', '{R:',str_ireplace('|-]', '}',$r_article_type[1])).'&a=type&{R:'.$r_article_type[2].'}" /&gt;'."\r\n";
		$this->iis7.='&lt;/rule&gt;'."\r\n";
		
		$this->iis7.='&lt;rule name="article"&gt;'."\r\n";
		$this->iis7.='	&lt;match url="^(.*/)*'.$r_product[0].'\?*(.*)$" /&gt;'."\r\n";
		$this->iis7.='	&lt;action type="Rewrite" url="{R:1}/index.php\?'.str_ireplace('[-|', '{R:',str_ireplace('|-]', '}',$r_product[1])).'&{R:'.$r_product[2].'}" /&gt;'."\r\n";
		$this->iis7.='&lt;/rule&gt;'."\r\n";
		
		$this->iis7.='&lt;rule name="article_type"&gt;'."\r\n";
		$this->iis7.='	&lt;match url="^(.*/)*'.$r_product_type[0].'\?*(.*)$" /&gt;'."\r\n";
		$this->iis7.='	&lt;action type="Rewrite" url="{R:1}/index.php\?'.str_ireplace('[-|', '{R:',str_ireplace('|-]', '}',$r_product_type[1])).'&a=type&{R:'.$r_product_type[2].'}" /&gt;'."\r\n";
		$this->iis7.='&lt;/rule&gt;'."\r\n";
		
		$this->iis7.='&lt;rule name="message_type"&gt;'."\r\n";
		$this->iis7.='	&lt;match url="^(.*/)*'.$r_message_type[0].'\?*(.*)$" /&gt;'."\r\n";
		$this->iis7.='	&lt;action type="Rewrite" url="{R:1}/index.php\?'.str_ireplace('[-|', '{R:',str_ireplace('|-]', '}',$r_message_type[1])).'&a=type&{R:'.$r_message_type[2].'}" /&gt;'."\r\n";
		$this->iis7.='&lt;/rule&gt;'."\r\n";
		
		$this->iis7.='&lt;rule name="special"&gt;'."\r\n";
		$this->iis7.='	&lt;match url="^(.*/)*'.$r_special[0].'\?*(.*)$" /&gt;'."\r\n";
		$this->iis7.='	&lt;action type="Rewrite" url="{R:1}/index.php\?'.str_ireplace('[-|', '{R:',str_ireplace('|-]', '}',$r_special[1])).'&{R:'.$r_special[2].'}" /&gt;'."\r\n";
		$this->iis7.='&lt;/rule&gt;'."\r\n";
		
		$this->iis7.='&lt;rule name="special"&gt;'."\r\n";
		$this->iis7.='	&lt;match url="^(.*/)*'.$r_labelcus_custom[0].'\?*(.*)$" /&gt;'."\r\n";
		$this->iis7.='	&lt;action type="Rewrite" url="{R:1}/index.php\?'.str_ireplace('[-|', '{R:',str_ireplace('|-]', '}',$r_labelcus_custom[1])).'&{R:'.$r_labelcus_custom[2].'}" /&gt;'."\r\n";
		$this->iis7.='&lt;/rule&gt;'."\r\n";
		
		
		//nginx规则
		$this->nginx='rewrite ^([^\.]*)/'.str_ireplace('.','\.',$r_article[0]).'$ $1/index.php?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_article[1])).' last;'."\r\n";
		$this->nginx.='rewrite ^([^\.]*)/'.str_ireplace('.','\.',$r_article_type[0]).'$ $1/index.php?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_article_type[1])).' last;'."\r\n";
		$this->nginx.='rewrite ^([^\.]*)/'.str_ireplace('.','\.',$r_product[0]).'$ $1/index.php?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_product[1])).' last;'."\r\n";
		$this->nginx.='rewrite ^([^\.]*)/'.str_ireplace('.','\.',$r_product_type[0]).'$ $1/index.php?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_product_type[1])).' last;'."\r\n";
		$this->nginx.='rewrite ^([^\.]*)/'.str_ireplace('.','\.',$r_message_type[0]).'$ $1/index.php?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_message_type[1])).' last;'."\r\n";
		$this->nginx.='rewrite ^([^\.]*)/'.str_ireplace('.','\.',$r_special[0]).'$ $1/index.php?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_special[1])).' last;'."\r\n";
		$this->nginx.='rewrite ^([^\.]*)/'.str_ireplace('.','\.',$r_labelcus_custom[0]).'$ $1/index.php?'.str_ireplace('[-|', '$',str_ireplace('|-]', '',$r_labelcus_custom[1])).' last;'."\r\n";
		
		$this->display("rewrite.html");
	}
	function ecache(){
		$this->toptxt='清空缓存';
		if($this->sysParams('run')==1){
			if($this->sysParams("tmp")==1)deleteDir($GLOBALS['LD_I']['sp_cache']);
			if($this->sysParams("tpl")==1)deleteDir($GLOBALS['LD_I']['view']['config']['template_tpl']);
			$this->checkdir('./template');
			$this->checkdir('config.php');
			message_a("缓存清理成功",'?c=ld_sys&a=ecache');
		}
		$this->display("sys.html");
	}
	private function thisnumjian($d){
		$d=str_ireplace(array('[-|2|-]','[-|3|-]','[-|4|-]','[-|5|-]','[-|6|-]'),array('[-|1|-]','[-|2|-]','[-|3|-]','[-|4|-]','[-|5|-]'),$d);
		return $d;
	}
	private function rewrite_for($d,$r){
		$num=1;
		foreach($d[1] as $k=>$v){
			if(stripos(',,type,special,article,product,message,,',','.$v.',')){
				if(stripos(',,special,article,product,message,,',','.$v.',')){
					$u.='&c='.$v;
				}else{
					$u.='&a='.$v;
				}
				$r=str_ireplace(array('{'.$v.'}'),$v,$r);
			}else{
				$num++;
				$u.='&'.$v.'=[-|'.$num.'|-]';
				if(stripos(',,id,tid,sid,page,,',','.$v.',')){
					$r=str_ireplace($d[0][$k],'([0-9]+)',$r);
				}else{
					$r=str_ireplace($d[0][$k],'(\w+)',$r);
				}
			}
		}
		return array($r,ltrim($u,'&'),$num+1);
	}
	private function checkdir($basedir){
		if (is_file($basedir)) {
			$this->checkBOM($basedir);
		}else{
			if ($dh = opendir($basedir)) {
				while (($file = readdir($dh)) !== false) {
					if ($file != '.' && $file != '..'){
						if (!is_dir($basedir."/".$file)) {
							$this->checkBOM("$basedir/$file");
						}else{
							$dirname = $basedir.'/'.$file;
							$this->checkdir($dirname);
						}
					}
				}
			closedir($dh);
			}
		}
	}
	private function checkBOM ($filename) {
		$contents = file_get_contents($filename);
		$charset[1] = substr($contents, 0, 1); 
		$charset[2] = substr($contents, 1, 1); 
		$charset[3] = substr($contents, 2, 1); 
		if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
			$rest = substr($contents, 3);
			$this->rewr ($filename, $rest);
		} 
	}
	private function rewr ($filename, $data) {
		$filenum = fopen($filename, "w");
		flock($filenum, LOCK_EX);
		fwrite($filenum, $data);
		fclose($filenum);
	}
}	