QT_DIR directory /***************************/ /* Settings /***************************/ var $Error_reporting= 3; // 3 - print error message and save messages in $this->Errors array // 2 - print error message in comment and save messages in $this->Errors array // 1 - only save messages in $this->Errors array var $UseLog = false; // Save error message in log file? var $LogFile = ''; // Full path to log file var $UndefinedTags= 'keep'; // remowe - remowe all undefined tags // comment - add tag into comment // keep - don`t change var $Lt="{"; // Left bracket var $Gt="}"; // Right bracket var $Errors = array(); // Errors var $UsePHP = false; // Can use PHP code in template? var $If_Functions = array('isset','sizeof', // Only this functions can used in "if" tag 'count','strlen', // if array empty then can used any PHP functions 'empty','strpos'); /***************************/ /* Private vars /***************************/ // Some private information, don`t change var $Filters = array( // Template filters 'output' => array(), // Output filters 'pre' => array(), // Precompilation filters 'post' => array()); // Postcompilation filters var $BlockedVars = array(); // Vars from this array can`t uses in template (use $this->BlockVar($VarName)) var $system = array(); var $md5_errors = array(); ## Constructor # function QuickTemplate($t='') { $this->Template = $t; $this->QT_DIR = dirname(__FILE__).'/'; // $this->TemplateDir = $this->QT_DIR . $this->TemplateDir; // $this->CompileDir = $this->QT_DIR . $this->CompileDir; // $this->CacheDir = $this->QT_DIR . $this->CacheDir; // $this->PluginsDir = $this->QT_DIR . $this->PluginsDir; } ## function check for compiled template # function DoCompile($t = '') { if (!$t) $t = $this->Template; if ((@$Modify = filemtime($this->TemplateDir . $t)) != true) return -1; if ((@$Compile = filemtime($this->CompileDir . $t)) !== true) { if ($Compile > $Modify and $Compile > filemtime(__FILE__) and $Compile > filemtime($this->QT_DIR . 'QuickTemplateCompiler.class.php')) return 1; else return 1; } else return 1; } ## # function is_cached($t, $cache_live_time=0, $cache_id='') { if ($t == '') return FALSE; if ($cache_live_time < 1) return FALSE; if($cache_id == '') $fname=$this->CacheDir.crc32($this->TemplateDir.$t).'.tpl'; else $fname=$this->CacheDir.crc32($cache_id).'/'.crc32($this->TemplateDir.$t).'.tpl'; $mtime=@filemtime($fname); $tpl_mtime=@filemtime($this->TemplateDir.$t); if (is_array($cache_live_time)) { $cache_live_time = null; } if($mtime < (time() - $cache_live_time) or $mtime < $tpl_mtime or $tpl_mtime == 0) return FALSE; return TRUE; } ## function for displaing template # function Display($t = '', $cache_live_time = 0, $cache_id = '') { global $lang; $prefix = ''; //if (file_exists($this->QT_DIR . $this->TemplateDir . $lang->getLangDir() . $t)) { // $prefix = $lang->getLangDir(); //} if($t) $this->Template=$prefix . $t; if($this->Template == '') return FALSE; $save_cache=false; if($cache_live_time > 0) { if($cache_id == '') $fname=$this->CacheDir.crc32($this->TemplateDir.$t).'.tpl'; else $fname=$this->CacheDir.crc32($cache_id).'/'.crc32($this->TemplateDir.$t).'.tpl'; if($this->is_cached($this->Template, $cache_live_time, $cache_id)) { $f=fopen($fname, 'r'); echo fread($f, filesize($fname)); fclose($f); return TRUE; } else $save_cache=true; } $r=$this->DoCompile(); if($r == -1) { echo 'QuickTemplate: Can`t load template - "'.$this->TemplateDir.$this->Template.'".'; return FALSE; } elseif($r == 1) $this->Compile(); if (!file_exists($this->CompileDir.$this->Template)) { echo 'QuickTemplate: Can`t load compiled template - "'.$this->Template.'".'; echo FALSE; } if(sizeof($this->Filters['output']) > 0 or $save_cache == true) ob_start(); include($this->CompileDir.$this->Template); if(sizeof($this->Filters['output']) > 0 or $save_cache == true) { $TemplateContent=ob_get_contents(); ob_end_clean(); foreach($this->Filters['output'] as $name => $file) { $func='qt_outputfilter_'.$name; include_once($this->QT_DIR.$this->PluginsDir.$file); $func(&$TemplateContent, &$this); } /* if($save_cache == true) { if($cache_id != '') @mkdir($this->CacheDir.crc32($cache_id)); $f=fopen($fname, 'a+'); flock($f, 2); ftruncate($f, 0); fwrite($f, $TemplateContent); flock($f, 3); fclose($f); } */ echo $TemplateContent; Unset($TemplateContent); } return TRUE; } ## # function GetTemplate($t='', $cache_live_time=0, $cache_id='') { global $lang; $prefix = ''; if (file_exists($this->QT_DIR . $this->TemplateDir . $lang->getLangDir() . $t)) { $prefix = $lang->getLangDir(); } if($t) $this->Template=$prefix . $t; ob_start(); $bool=$this->Display('', $cache_live_time, $cache_id); $c=ob_get_contents(); ob_end_clean(); if($bool === FALSE) return FALSE; return $c; } ## # function clear_cache($t='', $cache_id='') { if($t == '' and $cache_id == '') return FALSE; elseif($t == '' and $cache_id != '') { $dir=$this->CacheDir.crc32($cache_id); if(!is_dir($dir)) return FALSE; $d=opendir($dir); while(($e=readdir($d)) !== false) { if($e == '.' or $e == '..') continue; unlink($dir.'/'.$e); } closedir($d); rmdir($dir); } elseif($t != '' and $cache_id != '') { return @unlink($this->CacheDir.crc32($cache_id)."/".crc32($this->TemplateDir.$t).".tpl"); } else { return @unlink($this->CacheDir.crc32($this->TemplateDir.$t).".tpl"); } return TRUE; } ## Function delete all cache files for templates # function clear_all_cache() { static $dir=''; if($dir == '') { $dir=$this->CacheDir; $dir=substr($dir, 0, -1); } $d=opendir($dir); while(($e=readdir($d)) !== false) { if($e == '.' or $e == '..') continue; if(is_file($dir.'/'.$e)) { unlink($dir.'/'.$e); continue; } $_dir=$dir; $dir=$dir.'/'.$e; $this->clear_all_cache(); $dir=$_dir; } closedir($d); if($dir.'/' != $this->CacheDir) rmdir($dir.'/'.$e); } ## Template compiling # function Compile($t='') { if(!$t) $t=$this->Template; include_once($this->QT_DIR.'QuickTemplateCompiler.class.php'); $QT_c = new QuickTemplateCompiler($t); $QT_c->BlockedVars=$this->BlockedVars; $QT_c->Filters=$this->Filters; $QT_c->Lt=$this->Lt; $QT_c->Gt=$this->Gt; $QT_c->QT_DIR=$this->QT_DIR; $QT_c->TemplateDir=$this->TemplateDir; $QT_c->CompileDir=$this->CompileDir; $QT_c->PluginsDir=$this->PluginsDir; $QT_c->UsePHP=$this->UsePHP; $QT_c->If_Functions=$this->If_Functions; $QT_c->UndefinedTags=$this->UndefinedTags; $QT_c->Load(); $QT_c->Compile(); $QT_c->Save(); Unset($QT_c); } ## Print var # function _var(&$link, $var_name, $template, $line, $check='') { if($check != '') { if($this->IsBlocked($check)) { $this->error('You don`t have permision to access '.$var_name, $template, $line); } return FALSE; } if (isSet($link)) return $link; else // $this->error('Undefined variable '.$var_name, $template, $line); return FALSE; } ## Blocked vars can`t be print and used in templates # function BlockVar($var) { if(!is_array($var)) { if($var[0] != '$') $var='$'.$var; $var=str_replace('::', '->', $var); $var=$this->PrepairVar($var); $var[0]=''; $this->BlockedVars[trim($var)]=1; } else for($i = 0, $max = sizeof($var); $i < $max; $i++) { if($var[$i][0] != '$') $var[$i]='$'.$var[$i]; $var[$i]=str_replace('::', '->', $var[$i]); $var[$i]=$this->PrepairVar($var[$i]); $this->BlockedVars[$var[$i]]=1; } } ## Function checking var for blocking # function IsBlocked($var) { if(isSet($this->BlockedVars[$var])) return TRUE; $_var=''; $f=0; while(1) { $t=strpos($var, ']', $f); if($t !== FALSE and $t != strlen($var)-1) { $_var=substr($var, 0, $t+1); if(isSet($this->BlockedVars[$_var])) return TRUE; $f=$t+1; } else break; } return FALSE; } ## Checking var before show # function check_var(&$link, $var_name, $regular, $atr_name, $template, $line) { if (!isSet($link)) { // $this->error('Undefined variable '.$var_name, $template, $line); return false; } switch ($regular) { case '1': if (!preg_match("/^[0-9]+$/", $link)) { $this->error('"'.$atr_name.'" attribute should consist only of figures', $template, $line); return false; } case '2': if (!preg_match("/^[\w]+$/", $link)) { $this->error('"'.$atr_name.'" attribute should consist only of figures, letters and "_"', $template, $line); return false; } } return true; } ## Function for show error messages # function error($msg, $template, $line) { $md5=md5($msg.$template.$line); if(!IsSet($this->md5_errors[$md5])) $this->md5_errors[$md5]=1; if ($this->Error_reporting == 3) { echo '
QuickTemplate: '.$msg.' in '.$template.' on line '.$line.''; } elseif($this->Error_reporting == 2) echo "\n"; $this->Errors[]='QuickTemplate: '.$msg.' in '.$template.' on line '.$line.''; if($this->UseLog === true) { $f=fopen($this->LogFile, "a+") or die('QuickTemplate: Can`t open log file - '.$this->LogFile.'.'); flock($f, 2); fwrite($f, date("d.m.Y H:i:s", time()).": ".$msg." in ".$template." on line ".$line."\n"); flock($f, 3); fclose($f); } } ## Register/Unregister filters # function RegOutputFilter($name) { $this->Filters['output'][$name]='outputfilter.'.$name.'.php'; } function UnregOutputFilter($name='') { if($name) Unset($this->Filters['output'][$name]); else $this->Filters['output']=array(); } function RegPreFilter($name) { $this->Filters['prefilter'][$name]='prefilter.'.$name.'.php'; } function UnregPreFilter($name='') { if($name) Unset($this->Filters['prefilter'][$name]); else $this->Filters['prefilter']=array(); } function RegPostFilter($name) { $this->Filters['postfilter'][$name]='postfilter.'.$name.'.php'; } function UnregPostFilter($name='') { if($name) Unset($this->Filters['postfilter'][$name]); else $this->Filters['postfilter']=array(); } ## Make "normal" vars ## (modify $array.key1.key2 to $array['key1']['key2'] and put var in $GLOBALS array) # function PrepairVar($str, $make_globals=true) { $first=true; $_str=''; for($i = 0, $max = strlen($str); $i < $max; $i++) { if($str[$i] == '.') { if($first === true) { $_str.='['; $first=false; } else $_str.=']['; } else $_str.=$str[$i]; } if ($first !== true) $_str.=']'; if(substr($_str, 1, 3) == 'qt[') $_str="\$this->qt".substr($_str, 3); elseif($make_globals === true) $_str=preg_replace("/\\\$([a-zA-Z0-9_]+)/", "\$GLOBALS['\\1']", $_str); $_str=preg_replace("/\[[\"|']?([a-zA-Z0-9_]+)[\"|']?\]/", "['\\1']", $_str); return $_str; } } function QTemplate($template) { if (!$template) { return null; } $qt = new QuickTemplate($template); return $qt->GetTemplate(); } ?>