http://www3.to/KOBAYASHI/ * 2002.03.28 Ver. 1.3.1+ * update 2003.02.21 by ahiru * @access public **/ // memo. // Should use "$this", when referring to an object. // ex.) // $this->_parsesrc($all); class HtmlTemplate{ /** * Interprit a file on memory and output the result. * @access public * @param String $file Filename * @param Array $data a tree-like array * @return void */ function t_Include($file,$data){ $val=$data; $fp = fopen($file,"rb"); $all=fread($fp, filesize($file)); $code=HtmlTemplate::_parsesrc($all); echo eval('?>' .$code); fclose($fp); /* * DEBUGOutputCode($code); /* */ } /** * Interprit a file on memory and require the result as a string. * @access public * @param String $file Filename * @param Array $data a tree-like array * @return void */ function t_Buffer($file,$data){ $val=$data; $fp = fopen($file,"rb"); $all=fread($fp, filesize($file)); fclose($fp); $code=HtmlTemplate::_parsesrc($all); ob_start(); echo eval('?>' .$code); $ans=ob_get_contents(); ob_end_clean(); return $ans; } /** * Includes HTML file . * @access public * @param String $file filename * @param Array $data tree-like array * @param Array $dirname directoryname for .tmp file * @return void */ function t_Include_file($file,$data,$dirname="./"){ HtmlTemplate::_htmltmp2($file,$data,$dirname); } /** * Require HTML file as a string. * @access public * @param String $file filename * @param Array $data tree-like array * @param Array $dirname directoryname for .tmp file * @return String */ function t_Buffer_file($file,$data,$dirname="./"){ flush(); ob_start(); HtmlTemplate::_htmltmp2($file,$data,$dirname); $ans=ob_get_contents(); ob_end_clean(); return $ans; } /** * Compare the timestamp between .tmp & .html * bug fixed by STam on 04/29/2002.thanks. * @access private * @param String $file filename * @param Array $data tree-like array * @param String $dirname directoryname for .tmp file * @return void */ function _htmltmp2($file,$data,$dirname){ $val=$data; $t=$file.".tmp"; //if(substr($dirname,0,1)!="/") $dirname="/".$dirname; if(substr($dirname,-1) != "/") { $dirname .= "/"; } $_work_dir_name = $dirname.ereg_replace("^[\.|/]{2}", "", dirname($t)); $tmpfile=$_work_dir_name."/".basename($t); if(!is_dir($_work_dir_name)) { htmltemplate::_ForceDirectories($_work_dir_name); } if(file_exists($tmpfile)) { if (filemtime($file) > filemtime($tmpfile)) { htmltemplate::_compile($file, $tmpfile, $dirname); include($tmpfile); } else { include($tmpfile); } } else { htmltemplate::_compile($file, $tmpfile, $dirname); include($tmpfile); } } /** * Create directories for .tmp files * @access private * @param String $path path name * @param Integer $mode mode of the dir. * @return void */ function _ForceDirectories($path,$mode=0777) { if ( strlen($path) == 0) return 0; if ( strlen($path) < 3) return 1; // avoid 'xyz:\' problem. elseif ( @is_dir($path)) return 1; // avoid 'xyz:\' problem. elseif ( dirname($path) == $path) return 1; // avoid 'xyz:\' problem. return ( HtmlTemplate::_ForceDirectories(dirname($path),$mode) and mkdir(ereg_replace("/$","",$path), $mode) and chmod($path, $mode)); } /** * Create .tmp file * @access private * @param String $file template file path * @param String $tmpfile tempfile file path * @param String $dirname tempfile dir path * @return void */ function _compile($file, $tmpfile, $dirname="./"){ if(file_exists($file)) { $file_fp = fopen($file,"rb"); $all = fread($file_fp, filesize($file)); $temp_fp = fopen($tmpfile,"w"); fwrite($temp_fp, HtmlTemplate::_parsesrc($all)); ftruncate($temp_fp, ftell($temp_fp)); fclose($file_fp); fclose($temp_fp); chmod($tmpfile, 0777); } else { die("Template file is not found."); } } /** * Parse HTML strings. * @access private * @param String $str HTML strings. * @return String */ function _parsesrc($str){ #translate \r\n to \n (WindowsOS to UNIX) $str=str_replace("\r\n","\n",$str); #translate \r to \n (MacOS to UNIX) $str=str_replace("\r","\n",$str); $kuri=array(); $str2=$str; $acc=1; # interpretation of $kuri=array(); preg_match_all("//i",$str2,$k,PREG_SET_ORDER); while(list(,$x)=each($k)){ $kuri[]=$x[1]; } while(list(,$m)=each($kuri)){ $ar=split("/",$m); $ind=""; $rui=array(); $mattan=0; $loopid1=1; while(list(,$x)=each($ar)){ array_push($rui,$x); if($mattan!=count($ar)-1 && in_array(join("/",$rui),$kuri)) {$ind.="[\"$x\"][\$cnt[\"".join("_",$rui)."\"]]";} else {$ind.="[\"$x\"]";} $mattan++; } $n=str_replace("/","_",$m); $str2=str_replace("", "", $str2); } reset($kuri); $str2=str_replace("", "",$str2); # interpretation of {val } while(preg_match('/\{val ([^\}]+)\}/',$str2,$match)){ $m=$match[1]; $ar=split("/",$m); $ind=""; $rui=array(); foreach($ar as $x) { array_push($rui,$x); if(in_array(join("/",$rui),$kuri)) { $ind.="[\"".$x."\"][\$cnt[\"".join("_",$rui)."\"]]"; } else { $ind.="[\"". $x."\"]"; } } $str2=str_replace("{val $m}", "", $str2); } # interpretation of {rval } while(preg_match('/\{rval ([^\}]+)\}/',$str2,$match)){ $m=$match[1]; $ar=split("/",$m); $ind=""; $rui=array(); foreach($ar as $x) { array_push($rui,$x); if(in_array(join("/",$rui),$kuri)) { $ind.="[\"".$x."\"][\$cnt[\"".join("_",$rui)."\"]]"; } else { $ind.="[\"".$x."\"]"; } } $str2=str_replace("{rval $m}", "", $str2); } # interpretation of {echo } while(preg_match('/\{echo ([^\}]+)\}/',$str2,$match)){ $m=$match[1]; $ar=split("/",$m); $ind=""; $rui=array(); foreach($ar as $x) { array_push($rui,$x); if(in_array(join("/",$rui),$kuri)) { $ind.="[\"".$x."\"][\$cnt[\"".join("_",$rui)."\"]]"; } else { $ind.="[\"".$x."\"]"; } } $str2=str_replace("{rval $m}", "", $str2); } # interpretation of while(preg_match('//i',$str2,$match)){ $m=$match[1]; $ar=split("/",$m); $ind=""; $rui=array(); $mattan=0; foreach($ar as $x){ array_push($rui,$x); if($mattan!=count($ar)-1 && in_array(join("/",$rui),$kuri)) { $ind.="[\"".$x."\"][\$cnt[\"".join("_",$rui)."\"]]"; } else { $ind.="[\"".$x."\"]"; } $mattan++; } $str2=str_replace("", "0)){\n". "?>", $str2); } $str2=str_replace("", "", $str2); // xval, xrval and undef patch by Yoshitake Shinohara # interpretation of {xval } while(preg_match('/\{xval ([^\}]+)\}/',$str2,$match)){ $m=$match[1]; $ar=split("/",$m); $ind=""; $rui=array(); foreach($ar as $x){ array_push($rui,$x); if(in_array(join("/",$rui),$kuri)) { $ind.="[\"".$x."\"][\$cnt[\"".join("_",$rui)."\"]]"; } else { $ind.="[\"".$x."\"]"; } } $str2=str_replace("{xval $m}", "", $str2); } # interpretation of {xrval } while(preg_match('/\{xrval ([^\}]+)\}/',$str2,$match)){ $m=$match[1]; $ar=split("/",$m); $ind=""; $rui=array(); foreach($ar as $x){ array_push($rui,$x); if(in_array(join("/",$rui),$kuri)) { $ind.="[\"".$x."\"][\$cnt[\"".join("_",$rui)."\"]]"; } else { $ind.="[\"".$x."\"]"; } } $str2=str_replace("{xrval $m}", "", $str2); } # interpretation of while(preg_match('//i',$str2,$match)){ $m=$match[1]; $ar=split("/",$m); $ind=""; $rui=array(); $mattan=0; foreach($ar as $x){ array_push($rui,$x); if ($mattan!=count($ar)-1 && in_array(join("/",$rui),$kuri)) { $ind.="[\"".$x."\"][\$cnt[\"".join("_",$rui)."\"]]"; } else { $ind.="[\"".$x."\"]"; } $mattan++; } $str2=str_replace("", "", $str2); } $str2=str_replace("", "", $str2); // returned function return $str2; } /** * XSS Sanitizeing * * @access public * @param String $data * @return String * @author Yoshitake Shinohara */ function xss_check($data){ $from = array("&","<",">","\"","\'"); $to = array("&","<",">",""","'"); return str_replace($from, $to, $data); } } // end class /** * Show compiled code. * * !! Warning !! * This code is debug code. * Please don't use except a test. * * @access private * @param String $code compiled code * @return void */ function DEBUGOutputCode($code) { $code = explode("\n", $code); echo '
';
		for ($i=0,reset($code); $i';
}

/**
* remove PHP scripts prefix.
* before:
*   
* after:
*   <?php ?>
*/
?>