<?php
/*
===========================用户控制器
^^^^^^^^^^^^^^^^^^成员
------添加(on_insert)
------修改(on_update)
------删除(on_del)

*/
class cls_ctl_templates
{
	private $arr_config=array();
	private $arr_perms=array();
	function __construct()
	{
		$this->arr_config["html_root"]=format_path(WEB_ROOT."/".TEMPLATES_DIR."/temp");
		$this->arr_config["css_root"]=WEB_CSS_PATH;
		$this->arr_config["html_ext"]=".html";
		$this->arr_config["php_root"]=format_path(WEB_ROOT."/".CACHE_DIR."/temp");
		$this->arr_config["lib_root"]=format_path(WEB_ROOT."/".TEMPLATES_DIR."/lib");
		$this->arr_config["fun_root"]=format_path(WEB_ROOT."/".TEMPLATES_DIR."/temp/sys/fun");
		$this->arr_perms["path"]="";
		$this->arr_perms["tempdir"]="/default"; //
	}
	function on_save_file($arr_fields=array())
	{
		$info=chk_file_name($arr_fields["name"]);
		if($info!="") return $info;
		$str_path=self::get_real_path($this->arr_perms["path"]);
		$str_path.="/".$arr_fields["name"];
		if(!is_file($str_path))
		{
			$str_path.=$this->arr_config["html_ext"];
		}
		if(isset($arr_fields["lib"])&&$arr_fields["lib"]!=""){
			$str_lib=$arr_fields["lib"];
			if(is_file($this->arr_config["lib_root"]."/".$str_lib)){
				$str_lib="{lib(".$str_lib.")}";
				$arr_fields["content"]=$str_lib.chr(13).$arr_fields["content"];
			}
		}
		file_create($str_path,$arr_fields["content"]);

		return "";
	}
	function on_del_file($msgfile)
	{
		if($msgfile=="") return "删除文件名不能为空！";
		$str_path=self::get_real_path($this->arr_perms["path"]);
		$str_path.="/".$msgfile;
		$return_info=file_delete($str_path);
		return $return_info;
	}
	function on_open_file($msgfile)
	{
		$str_path=self::get_real_path($this->arr_perms["path"]);
		$str_path.="/".$msgfile;
		if(!is_file($str_path)) return "打开的文件不存在！";
		$arr_item=array();
		$arr_item["name"]=$msgfile;
		$arr_item["content"]=file_get_contents($str_path);
		$arr_item["lib"]="";
		$str_content=preg_replace("/\{lib\((\/[\/a-z0-9_.]+)\)\}/is", "\\1---lib---", $arr_item["content"]);
		$arr_content=explode("---lib---",$str_content);
		if(count($arr_content)>1){
			$arr_item["lib"]=$arr_content[0];
			$arr_item["content"]=$arr_content[1];
		}
		return $arr_item;
	}
	function on_create_folder($msgfolder)
	{
		$info=chk_dir_name($msgfolder);
		if($info!="") return $info;
		$str_path=self::get_real_path($this->arr_perms["path"]);
		$str_path.="/".$msgfolder;
		dir_create($str_path);
		return "";
	}
	function on_del_folder($msgfile)
	{
		if($msgfile=="") return "删除目录名不能为空！";
		$str_path=self::get_real_path($this->arr_perms["path"]);
		$str_path.="/".$msgfile;
		$return_info=dir_delete($str_path);
		return $return_info;
	}

	function get_config($msgx)
	{
		if(isset($this->arr_config[$msgx])) return $this->arr_config[$msgx];
	}
	function set_perms($msgx,$msgy)
	{
		$this->arr_perms[$msgx]=$msgy;
	}
	function get_perms($msgx)
	{
		if(isset($this->arr_perms[$msgx])) return $this->arr_perms[$msgx];
	}
	function get_dirs()
	{
		$arr_dir=array();
		$str_path=self::get_real_path($this->arr_perms["path"]);
		$arr_dir=get_dirs($str_path);
		return $arr_dir;
	}
	function get_files()
	{
		$arr_file=array();
		$str_path=self::get_real_path($this->arr_perms["path"]);
		$arr_file=get_files($str_path);
		return $arr_file;
	}
	function get_lib_dirs()
	{
		$arr_dir=array();
		$str_path=self::get_lib_path($this->arr_perms["path"]);
		$arr_dir=get_dirs($str_path);
		return $arr_dir;
	}
	function get_lib_files()
	{
		$arr_file=array();
		$str_path=self::get_lib_path($this->arr_perms["path"]);
		$arr_file=get_files($str_path);
		return $arr_file;
	}

	function get_real_path($msg_path)
	{
		$str_path=get_real_path($this->arr_config["html_root"],$msg_path);
		return $str_path;
	}
	function get_lib_path($msg_path)
	{
		$str_path=get_real_path($this->arr_config["lib_root"],$msg_path);
		return $str_path;
	}
	function get_php_path($msg_path)
	{
		$str_path=format_path($msg_path);
		$str_path=str_replace($this->arr_config["html_root"],"",$str_path);			
		$str_path=get_real_path($this->arr_config["php_root"],$str_path);
		$str_dir=dirname($str_path);	
		$str_name=basename($str_path,$this->arr_config["html_ext"]);
		$str_path=$str_dir."/".$str_name.".php";
		return $str_path;
	}
	function get_php_folder($msg_path)
	{
		$arr_return=array("dir"=>"","dir_bak"=>"");
		$str_path=substr($msg_path,strlen($this->arr_config["php_root"]));
		$arr_return["dir"]=dirname($str_path);
		if(isset($this->arr_perms["tempcur"]) && $this->arr_perms["tempcur"]!=""){
			$arr_x=explode("/",$str_path);
			$str_x="/".$arr_x[1];
			$arr_return["dir_bak"]=$arr_return["dir"];
			$arr_return["dir"]=dirname($this->arr_perms["tempcur"].substr($str_path,strlen($str_x)));
		}
		return $arr_return;
	}
	function on_load($msgfile)
	{
		if($msgfile=="") return "";
		$str_name=basename($msgfile);
		$arr_name=explode(".",$str_name);
		if(count($arr_name)<2){
			$msgfile.=$this->arr_config["html_ext"];
		}
		$html_path=self::get_real_path($msgfile);
		$html_old_path="";
		$msgfile_old=$msgfile;
		if(!is_file($html_path)){
			$arr_x=explode("/",$msgfile);
			$str_x="/".$arr_x[1];
			$this->arr_perms["tempcur"]=$str_x;
			$msgfile=$this->arr_perms["tempdir"].substr($msgfile,strlen($str_x));
			$html_old_path=$html_path;
			$html_path=self::get_real_path($msgfile);
		}
		$php_path=self::get_php_path($msgfile_old);
		$arr_html=get_file_perms($html_path);
		$arr_php=get_file_perms($php_path);
		if(!is_file($php_path))
		{
			self::on_refresh($html_path,$html_old_path);
		}else if($arr_html["mtime"]>$arr_php["mtime"]){
			self::on_refresh($html_path,$html_old_path);
		}
		return $php_path;
	}
	function get_temp_url($msg_path){
		$arr_return=array("dir"=>"","dir_bak"=>"");
		$str_path=substr($msg_path,strlen($this->arr_config["html_root"]));
		$str_url=$this->arr_config["css_root"].dirname($str_path)."/";
		return $str_url;
	}
	function on_refresh($msghtml,$msghtml2="")
	{
		$html_path=$msghtml;
		if(!is_file($html_path)) return "";
		$php_path=self::get_php_path($msghtml);
		$php_cache_path=$php_path;
		if($msghtml2!=""){
			$php_cache_path=self::get_php_path($msghtml2);
		}
		$arr_php_folder=self::get_php_folder($php_path);
		$php_folder=$arr_php_folder["dir"];
		$php_folder_bak=$arr_php_folder["dir_bak"];
		$str_now_path=self::get_temp_url($msghtml);
		$str_source=file_get_contents($html_path);
		$str_source=str_replace('{$temp_url}',$str_now_path,$str_source);
		$str_source=str_replace('{$temp_baseurl}',$this->arr_config["css_root"]."/",$str_source);
		if(strtolower(DB_CHARSET)=="gbk"){
			$str_utf8_u="";
			$str_chinacode=chr(0xa1)."-".chr(0xff);
		}else{
			$str_utf8_u="u";
			$str_chinacode="\x{4e00}-\x{9fa5}";
		}
		//libs
		//转换{include()} 包函文件代码
		$str_source=preg_replace("/^\{lib\((\/[\/a-z0-9_.]+)\)\}/is", "<?php include '".$this->arr_config["lib_root"]."\\1';?>", $str_source);
		$str_source=preg_replace("/\{lib\(([^\/][\/a-z0-9_.]+)\)\}/is", "<?php include temp_load_lib('".$this->arr_config["lib_root"].$php_folder."/\\1','".$this->arr_config["lib_root"].$php_folder_bak."/\\1');?>", $str_source);
		//转换{include()} 包函文件代码
		$str_source=preg_replace("/\{include\((\/[_.\/a-z0-9".$str_chinacode."]{1,20})\)\}/".$str_utf8_u."is", "<?php include temp_load('\\1')?>", $str_source);
		$str_source=preg_replace("/\{include\(([^\/][_.\/a-z0-9".$str_chinacode."]{1,20})\)\}/".$str_utf8_u."is", "<?php include temp_load('".$php_folder."\/\\1')?>", $str_source);
		//转换{fun()} 包函文件代码
		$str_source=preg_replace("/\{fun\(([\/]{0,1}[_.\/a-z0-9".$str_chinacode."]{1,20})\)\}/".$str_utf8_u."is", "<?php include temp_load('/sys/fun/\\1.html')?>", $str_source);
		//转换{define()} 定义文件代码
		$str_source=preg_replace("/\{define[(]([\$][a-z0-9_]+),\"([^\"]*)\"[)]\}/is", "<?php \\1=\"\\2\";?>", $str_source);
		//转换{=} 输出代码
		$str_source=preg_replace("/\{([\$]+[a-z0-9_\]\[\"\'->\)\(.$]+)}/is", "<?php echo \\1;?>", $str_source);
		//sql语句
		$str_source=preg_replace("/{get_sql\((.+?)\)\}/is", "<?php get_sql(\\1);?>", $str_source);
		$str_source=preg_replace("/{get_sql_one\((.+?)\)\}/is", "<?php get_sql_one(\\1);?>", $str_source);
		$str_source=preg_replace("/{get_sql_page\((.+?)\)\}/is", "<?php get_sql_page(\\1);?>", $str_source);
		$str_source=preg_replace("/{get_([a-z0-9_]+)\((.+?)\)\}/is", "<?php echo get_\\1(\\2);?>", $str_source);
		//config类
		$str_source=preg_replace("/{cfg:\(([a-z0-9_-]+?),([a-z0-9_-]+?)\)\}/is", '<?php echo $obj_tab_sys_config->get_val("\\1","\\2");?>', $str_source);
		$str_source=preg_replace("/{get_cache_pic\((.+?)\)\}/is", "<?php echo get_cache_pic(\\1);?>", $str_source);
		//format语句
		$str_source=preg_replace("/{format_([a-z0-9_]+)\((.+?)\)\}/is", "<?php echo format_\\1(\\2);?>", $str_source);
		$str_source=preg_replace("/{website_([a-z0-9_]+)\((.+?)\)\}/is", "<?php echo website_\\1(\\2);?>", $str_source);
		//url相对路径
		$str_source=preg_replace("/(<link[a-zA-Z0-9\s\"_=\/]*href=[\"|\'])((?!http)(?!\/)[a-z0-9_.\/]*)([\"|\'])/is", "\\1".$str_now_path."\\2\\3", $str_source);
		$str_source=preg_replace("/(<img[\s\"\'.=a-z0-9_]*src=[\"|\'])((?!http)(?!\/)[a-z0-9_.\/]*)([\"|\'])/is", "\\1".$str_now_path."\\2\\3", $str_source);
		$str_source=preg_replace("/(background:url\()((?!http)(?!\/)[a-z0-9_.\/]*)(\))/is", "\\1".$str_now_path."\\2\\3", $str_source);
		$str_source=preg_replace("/(<script[\s\"\'=a-z0-9_]*src=[\"|\'])((?!http)(?!\/)[a-z0-9_.\/]*)([\"|\'])/is", "\\1".$str_now_path."\\2\\3", $str_source);
		//url根目录路径
		$str_source=preg_replace("/(<link[a-zA-Z0-9\s\"_=\/]*href=[\"|\'])\/([a-z0-9_.\/]*)([\"|\'])/is", "\\1".$this->arr_config["css_root"]."/\\2\\3", $str_source);
		$str_source=preg_replace("/(<img[\s\"\'=a-z0-9_]*src=[\"|\'])\/([a-z0-9_.\/]*)([\"|\'])/is", "\\1".$this->arr_config["css_root"]."/\\2\\3", $str_source);
		$str_source=preg_replace("/(<script[\s\"\'=a-z0-9_]*src=[\"|\'])\/([a-z0-9_.\/]*)([\"|\'])/is", "\\1".$this->arr_config["css_root"]."/\\2\\3", $str_source);
		$str_source=preg_replace("/(background:url\()\/([a-z0-9_.\/]*)(\))/is", "\\1".$this->arr_config["css_root"]."/\\2\\3", $str_source);

		$str_source=self::temp_replace_if($str_source);
		$str_source=self::temp_replace_foreach($str_source);
		//缓存为html
		if(strstr(strtolower($str_source),"{cache_to_html")){
			$str_source="<?php ob_start();?>".$str_source;
			$str_source=preg_replace("/{cache_to_html\((.+?)\)\}/is", "<?php cache_to_html(\\1);?>", $str_source);
		}
		//缓存为php
		if(strstr(strtolower($str_source),"{cache_to_php")){
			$str_parms=preg_replace("/.*{cache_to_php\((.+?)\)\}.*/is", "\\1", $str_source);
			$str_source=preg_replace("/{cache_to_php\((.+?)\)\}/is", "", $str_source);
			$arr_parms=explode(",",$str_parms);//0:文件名 , 1:缓存时间
			$str_x='<?php $lng_time=0;$str_file=get_real_path("",CACHE_DIR."/to_php/'.$arr_parms[0].'/".get_query_key().".html");if(is_file($str_file)){$arr_file=get_file_perms($str_file);$lng_time=strtotime($arr_file["mtime"]);}';
			$str_x.='if(TIME-$lng_time>'.$arr_parms[1].'){ob_start();?>'.$str_source.'<?php $str_content=ob_get_contents();file_create($str_file,$str_content,1);}else{include $str_file;}?>';
			$str_source=$str_x;
			unset($str_x);
		}
		$str_dir=dirname($php_cache_path);
		if(!is_dir($str_dir)) dir_create($str_dir);
		file_create($php_cache_path,$str_source);
		
	}

	function temp_replace_if($str_source)
	{
		$str_source = preg_replace("/([\n\r\t]*)\{else if\((.+?)\)\}([\n\r\t]*)/is", "\\1<?php } else if(\\2) { ?>\\3", $str_source);
		$str_source = preg_replace("/([\n\r\t]*)\{else\}([\n\r\t]*)/is", "\\1<?php } else { ?>\\2", $str_source);
		$str_source = preg_replace("/([\n\r\t]*)\{if\((.+?)\)\}([\n\r\t]*)/is", "\\1<?php if(\\2){?>\\3", $str_source);
		$str_source = preg_replace("/([\n\r\t]*)\{\/if\}([\n\r\t]*)/is", "\\1<?php }?>\\2", $str_source);
		return $str_source;
	}
	function temp_replace_foreach($msgstr)
	{
		$str_source=$msgstr;
		$str_source=preg_replace("/{foreach\(([\s\$\/\*a-zA-Z0-9+-_=><\'\"]+)\)}/is", "<?php foreach(\\1){ ?>", $str_source);
		$str_source=preg_replace("/{\/foreach}/is", "<?php }?>", $str_source);
		$str_source=preg_replace("/{for\(([\s\$\/\*a-zA-Z0-9+-_=><\'\"\;\,\.]+)\)}/is", "<?php for(\\1){ ?>", $str_source);
		$str_source=preg_replace("/{\/for}/is", "<?php }?>", $str_source);
		return $str_source;
	}

}
?>