<?
set_time_limit(0);
//转换
function conv($matchs){
	return "{%".$matchs[1].$matchs[2]."%}";
}
//遍历目录
function doUpdate($path="template") { 
  $d = dir($path);
  while($v = $d->read()){
	  echo $v."<br>";
	  if($v == "." || $v == ".." || $v==".noencode") continue; 
	  $file = $d->path."/".$v;
	  if(is_dir($file)){
		  doUpdate($file); 
	  }else{
		  $ext_name = strtolower(str_replace(".", "", strrchr($file, ".")));
		  mk_dir("temp/".$d->path."/");
		  if($ext_name=="tpl"){
			    $fp = fopen($file,r);
				$content = fread($fp,filesize($file));
				$newcontent = convTemplate($content);
				fclose($fp);
				$fp2 = fopen("temp/".$file,w);
				fputs($fp2,$newcontent);
				fclose($fp2);
		  }else{
				copy($file,"temp/".$file);
		  }
	  }
  }
  $d->close(); 
} 
//转换模板
function convTemplate($str){
	$patt = "/{(section|\\/|\\$|if|insert|else|cycle|literal|html_options|textformat)(.*)}/isU";
	preg_match_all($patt,$str,$o);
	$str=preg_replace_callback($patt,"conv",$str);
	return  $str;
}
include "config.inc.php";
doUpdate("template");
?>