كود PHP:
<?php
class template{
var $vars;
var $fid;
var $temp_dir;
var $cache_dir;
//Function to load a template file.
//Function to parse the Template Tags
function parse(){
//Foreach Statement
$this->files[$this->fid] = preg_replace("'<LOOP NAME=\"(.+)\">'i","<? foreach(\$this->vars[\\1] as \$key=>\$var){ ?>",$this->files[$this->fid]);
$this->files[$this->fid] = preg_replace("'</LOOP>'i","<? } ?>",$this->files[$this->fid]);
//Include Statement
$this->files[$this->fid] = preg_replace("'<INCLUDE FILENAME=\"(.+)\">'i","<? include(\"\\1\"); ?>",$this->files[$this->fid]);
//Foreach Variables
$this->files[$this->fid] = preg_replace("'{{(.+)}}'","<?= \$var[\\1]?>",$this->files[$this->fid]);
//Variables
$this->files[$this->fid] = preg_replace("'{(.+)}'","<?= \$this->vars[\\1]?>",$this->files[$this->fid]);
$this->files[$this->fid] = preg_replace("'<else>'","<? else{ ?>",$this->files[$this->fid]);
$this->files[$this->fid]= preg_replace('/\<if condtion\=\"(.*)\\ (.*)\\ (.*)\">/','<?if($GLOBALS[$1]$2$3){?>',$this->files[$this->fid]);
$this->files[$this->fid]= preg_replace('/\<\/if\>/','<?}?>',$this->files[$this->fid]);
$this->files[$this->fid] = preg_replace('/(<ELSE>|<ELSE \/>)/i','<? }else{ ?>',$this->files[$this->fid]);
$this->files[$this->fid]=str_replace("neq","!=",$this->files[$this->fid]);
$this->files[$this->fid]=str_replace("eq","==",$this->files[$this->fid]);
$this->files[$this->fid]=str_replace("neq","!=",$this->files[$this->fid]);
$this->files[$this->fid]=str_replace("gt",">",$this->files[$this->fid]);
$this->files[$this->fid]=str_replace("lt","<",$this->files[$this->fid]);
$this->files[$this->fid]=str_replace("and","&&",$this->files[$this->fid]);
$this->files[$this->fid]=str_replace("orrrr","||",$this->files[$this->fid]);
$this->files[$this->fid]=str_replace("not","!",$this->files[$this->fid]);
$this->files[$this->fid]=str_replace("mod","%",$this->files[$this->fid]);
return$this->files[$this->fid];
}
FUNCTION check_output($file)
{
if( file_exists($file))
{
$check = @fopen("$file",r);
$r = @fread($check,filesize("$file"));
@fclose($check);
}
return $r;
}
//Function to OUTPUT
function print_temp($filename) {
$this->fid = "$this->cache_dir/$filename.php";
$this->parse();
$this->files[$this->fid] = file_get_contents("$this->temp_dir/$filename");
$this->vars = &$GLOBALS;
$this->parse();
if (!is_writeable($this->cache_dir ) ) {
$m = @chmod($this->cache_dir, 0777);
$m = @chmod('class.php', 0777);
if($m){echo"تم";}
$html=$this->parse();
$check= $this->check_output($this->fid);
if($this->fid != $this->files[$this->fid]){
if($check != $html)
{
$fp = fopen($this->fid,"w");
fwrite($fp,$this->files[$this->fid]);
fclose($fp);
}
}
}else{
$html=$this->parse();
$check= $this->check_output($this->fid);
if($this->fid != $this->files[$this->fid]){
if($check != $html)
{
$fp = fopen($this->fid,"w");
fwrite($fp,$this->files[$this->fid]);
fclose($fp);
}
}
} include($this->fid);
}
}
?>
شكرا استاذ saanina