http://www.zoned.net/~xkahn/php/fasttemplate Portions written by Benjamin Kahn Copyright (c) 2000 CyberSites, Inc; xkahn@cybersites.com, All Rights Reserved originally by: "JP" http://www.phpbuilder.com/columns/jprins20000201.php3 with code by: Spencer D. Mindlin http://www.phpbuilder.com/columns/spencer20000208.php3 PHP3 port by CDI cdi@thewebmasters.net PHP3 Version Copyright (c) 1999 CDI, cdi@thewebmasters.net, All Rights Reserved. Perl Version Copyright (c) 1998 Jason Moore jmoore@sober.com. All Rights Reserved. Original Perl module CGI::FastTemplate by Jason Moore jmoore@sober.com */ class FastTemplate { var $FILELIST = array(); // Holds the array of filehandles // FILELIST[HANDLE] == "fileName" var $DYNAMIC = array(); // Holds the array of dynamic // blocks, and the fileHandles they // live in. var $PARSEVARS = array(); // Holds the array of Variable // handles. // PARSEVARS[HANDLE] == "value" var $LOADED = array(); // We only want to load a template // once - when it's used. // LOADED[FILEHANDLE] == 1 if loaded // undefined if not loaded yet. var $HANDLE = array(); // Holds the handle names assigned // by a call to parse() var $ROOT = ""; // Holds path-to-templates var $WIN32 = false; // Set to true if this is a WIN32 server var $ERROR = ""; // Holds the last error message var $LAST = ""; // Holds the HANDLE to the last // template parsed by parse() var $STRICT = true; // Strict template checking. // Unresolved vars in templates will // generate a warning when found. // ************************************************************ function FastTemplate ($pathToTemplates = "") { global $php_errormsg; if(!empty($pathToTemplates)) { $this->set_root($pathToTemplates); } } // end (new) FastTemplate () // New call: cache_expire (HANDLE, number of seconds); which allows you to cache a template // New call: cache_refresh (HANDLE, how often); which allows you to cache a template // New call: is_cached(RETURN, FileHandle); true if a valid cache file is available // New call: write_cache(HANDLE, FileHandle); call when a handle is done. // ********************* // Function: is_cached // Input: RETURN, FileHandle (FIXME: Plural FileHandles are not supported.) // Return: true or false depending on whether a cache file is available. // Side Effects: assigns variables as if a parse call had been made // // Notes: // Find the cache name, and see if the file exists // If it doesn't, return FALSE // If it does, check is the file is current (see ~/src/php-cache.php) // If is isn't, return FALSE // check if the section is dynamic. If so, use clear_dynamic to insert contents of file // If not, assign file contents to ReturnVar // return TRUE function is_cached ( $ReturnVar, $FileTags ) { // Fixme: We should do something smart if $FileTags is an array $str_cache_file = $this->cache_file_name( $ReturnVar, $FileTags ); // This function will wait until a cache has been created. (Lock file cleared.) clearstatcache($str_cache_file); // Here we perform some error correction. If a lock file // is left over from a previous request, this ensures that // it is deleted. // FIXME: If this takes too long, we should turn caching off for this file, and return FALSE; while (file_exists($str_cache_file . '.lock')) { sleep(2); clearstatcache(); continue; } clearstatcache(); // If the cache file doesn't exist, it isn't cached. QED. if (!file_exists($str_cache_file)) { return FALSE; } // Now we need to find out if we are caching. $refresh = $this->CACHE[$FileTags]["refresh"]; $expires = $this->CACHE[$FileTags]["expire"]; $cache_read = 0; // Default to no read if (!refresh && !expires) // Do we have any set time? $expires = 600; if (isset ($expires)) { if ((filectime ($str_cache_file) + $expires) > date ( "U")) { $cache_read = 1; } } if (isset ($refresh)) { switch (strtoupper($refresh)) { case 'MINUTE': if (checkRefreshMinute(mktime(), filemtime($str_cache_file))) { $cache_read = 1; } break; case 'QUARTERHOUR': if (checkRefreshQuarterHour(mktime(), filemtime($str_cache_file))) { $cache_read = 1; } break; case 'HALFHOUR': if (checkRefreshHalfHour(mktime(), filemtime($str_cache_file))) { $cache_read = 1; } break; case 'HOUR': if (checkRefreshHour(mktime(), filemtime($str_cache_file))) { $cache_read = 1; } break; case 'HALFDAY': if (checkExpiredHalfDay(mktime(), filemtime($str_cache_file))) { $cache_read = 1; } break; case 'DAY': if (checkRefreshDay(mktime(), filemtime($str_cache_file))) { $cache_read = 1; } break; case 'MONTH': if (checkRefreshMonth(mktime(), filemtime($str_cache_file))) { $cache_read = 1; } } } if ($cache_read == 1) { // We have a cache file and we should read from it! if ($f = fopen ($str_cache_file, "r")) { $buf = ""; while ($str = fgets ($f, 4096)) { $buf .= $str; } fclose ($f); $this->LAST = $ReturnVar; // This is the part that actually does all the work. // The template and the parsed version are the same when caching $this->assign( array( $ReturnVar => $buf ) ); $this->$ReturnVar = $buf; $this->$FileTags = $buf; $this->LOADED[$FileTags] = "1"; // Make sure the template doesn't get loaded. // If we were supposed to be appending, maybe we should assume a dynamic block if ( (substr ($FileTags, 0, 1)) == '.' ) { $this->add_cache_dynamic ($ReturnVar, $buf); } return TRUE; } } return FALSE; } // ********************* // Function: write_cache // Input: RETURN, FileHandle (FIXME: Plural FileHandles are not supported.) // Return: true or false depending on whether a cache file could be created // Side Effects: None // // Notes: // Find the cache name. // Write out the ReturnVar contents to the file // Return function write_cache ( $ReturnVar, $FileTags ) { // FIXME: I should do something clever if $FileTags is an array. $str_cache_file = $this->cache_file_name ( $ReturnVar, $FileTags ); $new = $this->parse_template_messy ($this->$FileTags, $this->PARSEVARS); // Time to write the cache -- but we need to look out for lock files // FIXME: I'm SURE there is a race condition here. while (file_exists($str_cache_file . '.lock')) { sleep(2); clearstatcache(); continue; } clearstatcache(); // We pray that nothing bad happens after this point. touch ($str_cache_file . '.lock'); $f = fopen ($str_cache_file, "w"); if (!$f) return FALSE; fputs ($f, $new); fclose ($f); // And remove the lock file. $this->remove_cache_lock ($str_cache_file); return TRUE; } // ********************* // Function: cache_expire // Input: RETURN, number_of_seconds // Return: None // Side Effects: Stores time to expire // // Notes: // tells in how many seconds the cached data will expire function cache_expire ( $ReturnVar, $seconds = 3000) { $this->CACHE[$ReturnVar]["expire"] = $seconds; } // ********************* // Function: cache_refresh // Input: RETURN, frequency // Return: None // Side Effects: Stores time to refresh // // Notes: // sets an event (turn of day) at which cache will expire function cache_refresh ( $ReturnVar, $freq = 'HOUR') { $this->CACHE[$ReturnVar]["refresh"] = $freq; } // ************************************************************ // All templates will be loaded from this "root" directory // Can be changed in mid-process by re-calling with a new // value. function set_root ($root) { $trailer = substr($root,-1); if(!$this->WIN32) { if( (ord($trailer)) != 47 ) { $root = "$root". chr(47); } if(is_dir($root)) { $this->ROOT = $root; } else { $this->ROOT = ""; $this->error("Specified ROOT dir [$root] is not a directory"); } } else { // WIN32 box - no testing if( (ord($trailer)) != 92 ) { $root = "$root" . chr(92); } $this->ROOT = $root; } } // End set_root() // ************************************************************** // Calculates current microtime // I throw this into all my classes for benchmarking purposes // It's not used by anything in this class and can be removed // if you don't need it. function utime () { $time = explode( " ", microtime()); $usec = (double)$time[0]; $sec = (double)$time[1]; return $sec + $usec; } // ************************************************************** // Strict template checking, if true sends warnings to STDOUT when // parsing a template with undefined variable references // Used for tracking down bugs-n-such. Use no_strict() to disable. function strict () { $this->STRICT = true; } // ************************************************************ // Silently discards (removes) undefined variable references // found in templates function no_strict () { $this->STRICT = false; } // ************************************************************ // A quick check of the template file before reading it. // This is -not- a reliable check, mostly due to inconsistencies // in the way PHP determines if a file is readable. function is_safe ($filename) { if(!file_exists($filename)) { $this->error("[$filename] does not exist",0); return false; } return true; } // ************************************************************ // Grabs a template from the root dir and // reads it into a (potentially REALLY) big string function get_template ($template) { if(empty($this->ROOT)) { $this->error("Cannot open template. Root not valid.",1); return false; } $filename = "$this->ROOT"."$template"; $contents = implode("",(@file($filename))); if( (!$contents) or (empty($contents)) ) { $this->error("get_template() failure: [$filename] $php_errormsg",1); } return $contents; } // end get_template // ************************************************************ // Prints the warnings for unresolved variable references // in template files. Used if STRICT is true function show_unknowns ($Line) { $unknown = array(); if (ereg("({[A-Z0-9_]+})",$Line,$unknown)) { $UnkVar = $unknown[1]; if(!(empty($UnkVar))) { @error_log("[FastTemplate] Warning: no value found for variable: $UnkVar ",0); } } } // end show_unknowns() // ************************************************************ // This routine get's called by parse() and does the actual // {VAR} to VALUE conversion within the template. function parse_template ($template, $tpl_array) { $a = strtok($template,"{"); $oldtemp = $template; while($a || $t) { $t = strtok("}"); if($t){ settype($tpl_array[$t],"string"); //if(empty($tpl_array[$t])){ // print($t . '
'); //} $toprint = $toprint . $a . $tpl_array[$t]; } else { $toprint = $toprint . $a; } //print('T-' . $t . '
'); //print('value' . htmlentities($tpl_array[$t]) . '
'); //print('A-' . htmlentities($a) . '
'); $a = strtok("{"); } $template = $toprint; return $template; } // end parse_template(); function parse_template_messy ($template, $tpl_array) { $a = strtok($template,"{"); $oldtemp = $template; while($a || $t) { $t = strtok("}"); if($t){ settype($tpl_array[$t],"string"); if(empty($tpl_array[$t])){ $toprint = $toprint . $a . "{". $t . "}"; // Recreate the substitution. } else { $toprint = $toprint . $a . $tpl_array[$t]; } } else { $toprint = $toprint . $a; } //print('T-' . $t . '
'); //print('value' . htmlentities($tpl_array[$t]) . '
'); //print('A-' . htmlentities($a) . '
'); $a = strtok("{"); } $template = $toprint; return $template; } // end parse_template(); // ************************************************************ // The meat of the whole class. The magic happens here. function parse ( $ReturnVar, $FileTags ) { $append = false; $this->LAST = $ReturnVar; $this->HANDLE[$ReturnVar] = 1; if (gettype($FileTags) == "array") { unset($this->$ReturnVar); // Clear any previous data while ( list ( $key , $val ) = each ( $FileTags ) ) { if ( (!isset($this->$val)) || (empty($this->$val)) ) { $this->LOADED["$val"] = 1; if(isset($this->DYNAMIC["$val"])) { $this->parse_dynamic($val,$ReturnVar); } else { $fileName = $this->FILELIST["$val"]; $this->$val = $this->get_template($fileName); } } // Array context implies overwrite $this->$ReturnVar = $this->parse_template($this->$val,$this->PARSEVARS); // For recursive calls. $this->assign( array( $ReturnVar => $this->$ReturnVar ) ); } } else { // end if FileTags is array() // FileTags is not an array $val = $FileTags; if( (substr($val,0,1)) == '.' ) { // Append this template to a previous ReturnVar $append = true; $val = substr($val,1); } if ( (!isset($this->$val)) || (empty($this->$val)) ) { $this->LOADED["$val"] = 1; if(isset($this->DYNAMIC["$val"])) { $this->parse_dynamic($val,$ReturnVar); } else { $fileName = $this->FILELIST["$val"]; $this->$val = $this->get_template($fileName); } } if($append) { $this->$ReturnVar .= $this->parse_template($this->$val,$this->PARSEVARS); } else { $this->$ReturnVar = $this->parse_template($this->$val,$this->PARSEVARS); } // For recursive calls. $this->assign(array( $ReturnVar => $this->$ReturnVar) ); } return; } // End parse() // ************************************************************ function FastPrint ( $template = "" ) { if(empty($template)) { $template = $this->LAST; } if( (!(isset($this->$template))) || (empty($this->$template)) ) { $this->error("Nothing parsed, nothing printed",0); return; } else { print $this->$template; } return; } // ************************************************************ function fetch ( $template = "" ) { if(empty($template)) { $template = $this->LAST; } if( (!(isset($this->$template))) || (empty($this->$template)) ) { $this->error("Nothing parsed, nothing printed",0); return ""; } return($this->$template); } // ************************************************************ function define_dynamic ($Macro, $ParentName) { // A dynamic block lives inside another template file. // It will be stripped from the template when parsed // and replaced with the {$Tag}. $this->DYNAMIC["$Macro"] = $ParentName; return true; } // ************************************************************ function parse_dynamic ($Macro,$MacroName) { // The file must already be in memory. $ParentTag = $this->DYNAMIC["$Macro"]; if( (!$this->$ParentTag) or (empty($this->$ParentTag)) ) { $fileName = $this->FILELIST[$ParentTag]; $this->$ParentTag = $this->get_template($fileName); $this->LOADED[$ParentTag] = 1; } if($this->$ParentTag) { $template = $this->$ParentTag; $DataArray = split("\n",$template); $newMacro = ""; $newParent = ""; $outside = true; $start = false; $end = false; while ( list ($lineNum,$lineData) = each ($DataArray) ) { $lineTest = trim($lineData); if("" == "$lineTest" ) { $start = true; $end = false; $outside = false; } if("" == "$lineTest" ) { $start = false; $end = true; $outside = true; } if( (!$outside) and (!$start) and (!$end) ) { $newMacro .= "$lineData\n"; // Restore linebreaks } if( ($outside) and (!$start) and (!$end) ) { $newParent .= "$lineData\n"; // Restore linebreaks } if($end) { // $newParent .= "{$MacroName}\n"; $newParent .= '{'."$MacroName}\n"; } // Next line please if($end) { $end = false; } if($start) { $start = false; } } // end While $this->$Macro = $newMacro; $this->$ParentTag = $newParent; return true; } else {// $ParentTag NOT loaded - MAJOR oopsie @error_log("ParentTag: [$ParentTag] not loaded!",0); $this->error("ParentTag: [$ParentTag] not loaded!",0); } return false; } // ************************************************************ // Strips a DYNAMIC BLOCK from a template. function clear_dynamic ($Macro="") { if(empty($Macro)) { return false; } // The file must already be in memory. $ParentTag = $this->DYNAMIC["$Macro"]; if( (!$this->$ParentTag) or (empty($this->$ParentTag)) ) { $fileName = $this->FILELIST[$ParentTag]; $this->$ParentTag = $this->get_template($fileName); $this->LOADED[$ParentTag] = 1; } if($this->$ParentTag) { $template = $this->$ParentTag; $DataArray = split("\n",$template); $newParent = ""; $outside = true; $start = false; $end = false; while ( list ($lineNum,$lineData) = each ($DataArray) ) { $lineTest = trim($lineData); if("" == "$lineTest" ) { $start = true; $end = false; $outside = false; } if("" == "$lineTest" ) { $start = false; $end = true; $outside = true; } if( ($outside) and (!$start) and (!$end) ) { $newParent .= "$lineData\n"; // Restore linebreaks } // Next line please if($end) { $end = false; } if($start) { $start = false; } } // end While $this->$ParentTag = $newParent; return true; } else {// $ParentTag NOT loaded - MAJOR oopsie @error_log("ParentTag: [$ParentTag] not loaded!",0); $this->error("ParentTag: [$ParentTag] not loaded!",0); } return false; } // ************************************************************ // Adds cached data to a DYNAMIC BLOCK from a template. function add_cache_dynamic ($Macro="", $cache_data="" ) { if(empty($Macro)) { return false; } // The file must already be in memory. $ParentTag = $this->DYNAMIC["$Macro"]; if( (!$this->$ParentTag) or (empty($this->$ParentTag)) ) { $fileName = $this->FILELIST[$ParentTag]; $this->$ParentTag = $this->get_template($fileName); $this->LOADED[$ParentTag] = 1; } if($this->$ParentTag) { $template = $this->$ParentTag; $DataArray = split("\n",$template); $newParent = ""; $outside = true; $start = false; $end = false; while ( list ($lineNum,$lineData) = each ($DataArray) ) { $lineTest = trim($lineData); if("" == "$lineTest" ) { $start = true; $end = false; $outside = false; } if("" == "$lineTest" ) { $start = false; $end = true; $outside = true; } if( ($outside) and (!$start) and (!$end) ) { $newParent .= "$lineData\n"; // Restore linebreaks } if ($end) { $newParent .= $cache_data; } // Next line please if($end) { $end = false; } if($start) { $start = false; } } // end While $this->$ParentTag = $newParent; return true; } else {// $ParentTag NOT loaded - MAJOR oopsie @error_log("ParentTag: [$ParentTag] not loaded!",0); $this->error("ParentTag: [$ParentTag] not loaded!",0); } return false; } // ************************************************************ function define ($fileList) { while ( list ($FileTag,$FileName) = each ($fileList) ) { $this->FILELIST["$FileTag"] = $FileName; } return true; } // ************************************************************ function clear_parse ( $ReturnVar = "") { $this->clear($ReturnVar); } // ************************************************************ function clear ( $ReturnVar = "" ) { // Clears out hash created by call to parse() if(!empty($ReturnVar)) { if( (gettype($ReturnVar)) != "array") { unset($this->$ReturnVar); return; } else { while ( list ($key,$val) = each ($ReturnVar) ) { unset($this->$val); } return; } } // Empty - clear all of them while ( list ( $key,$val) = each ($this->HANDLE) ) { $KEY = $key; unset($this->$KEY); } return; } // end clear() // ************************************************************ function clear_all () { $this->clear(); $this->clear_assign(); $this->clear_define(); $this->clear_tpl(); return; } // end clear_all // ************************************************************ function clear_tpl ($fileHandle = "") { if(empty($this->LOADED)) { // Nothing loaded, nothing to clear return true; } if(empty($fileHandle)) { // Clear ALL fileHandles while ( list ($key, $val) = each ($this->LOADED) ) { unset($this->$key); } unset($this->LOADED); return true; } else { if( (gettype($fileHandle)) != "array") { if( (isset($this->$fileHandle)) || (!empty($this->$fileHandle)) ) { unset($this->LOADED[$fileHandle]); unset($this->$fileHandle); return true; } } else { while ( list ($Key, $Val) = each ($fileHandle) ) { unset($this->LOADED[$Key]); unset($this->$Key); } return true; } } return false; } // end clear_tpl // ************************************************************ function clear_define ( $FileTag = "" ){ if(empty($FileTag)) { unset($this->FILELIST); return; } if( (gettype($Files)) != "array") { unset($this->FILELIST[$FileTag]); return; } else { while ( list ( $Tag, $Val) = each ($FileTag) ) { unset($this->FILELIST[$Tag]); } return; } } // ************************************************************ // Aliased function - used for compatibility with CGI::FastTemplate function clear_parse () { $this->clear_assign(); } // ************************************************************ // Clears all variables set by assign() function clear_assign () { if(!(empty($this->PARSEVARS))) { while(list($Ref,$Val) = each ($this->PARSEVARS) ) { unset($this->PARSEVARS["$Ref"]); } } } // ************************************************************ function clear_href ($href) { if(!empty($href)) { if( (gettype($href)) != "array") { unset($this->PARSEVARS[$href]); return; } else { while (list ($Ref,$val) = each ($href) ) { unset($this->PARSEVARS[$Ref]); } return; } } else { // Empty - clear them all $this->clear_assign(); } return; } // ************************************************************ function assign ($tpl_array, $trailer="") { if(gettype($tpl_array) == "array") { while ( list ($key,$val) = each ($tpl_array) ) { if (!(empty($key))) { // Empty values are allowed // Empty Keys are NOT $this->PARSEVARS["$key"] = $val; } } //if(empty($this->PARSEVARS)){ //$this->PARSEVARS = $tpl_array; //}else{ //$this->PARSEVARS = $this->PARSEVARS + $tpl_array; //} } else { // Empty values are allowed in non-array context now. if (!empty($tpl_array)) { $this->PARSEVARS["$tpl_array"] = $trailer; } } } // ************************************************************ // Return the value of an assigned variable. // Christian Brandel cbrandel@gmx.de function get_assigned($tpl_name = "") { if(empty($tpl_name)) { return false; } if(isset($this->PARSEVARS["$tpl_name"])) { return ($this->PARSEVARS["$tpl_name"]); } else { return false; } } // ************************************************************ function error ($errorMsg, $die = 0) { $this->ERROR = $errorMsg; echo "ERROR: $this->ERROR
\n"; if ($die == 1) { exit; } return; } // end error() // ************************************************************ // ************************************************************ // Caching system //*********************************/ // Some other functions function remove_cache_lock($str_cache_file) { clearstatcache(); if (file_exists($str_cache_file . '.lock')) { @unlink($str_cache_file . '.lock'); } } // Create the cache file name function cache_file_name ( $ReturnVar, $FileTags ) { // What are we going to do? $str_cache_root = "/tmp/"; $str_cache_dir = strtolower(getenv ("HTTP_HOST")) . getenv ("SCRIPT_URL"); if ( (substr ($FileTags, 0, 1)) == '.' ) { $str_cache_file = "/" . substr($FileTags,1); } else { $str_cache_file = "/" . $FileTags; } // First we need to make the directory the cache file is in if it doesn't already exist. if ($this->rmkdir ($str_cache_root . $str_cache_dir)) return $str_cache_root . $str_cache_dir . $str_cache_file; return; } // Create a directory tree function rmkdir ( $directory ) { // If we encounter this as a file, we can't make it a directory $type = filetype ( $directory ); if ($type != 'dir' && $type != 'FALSE' && !empty($type)) { return FALSE; } if ($type == 'dir') return TRUE; $pieces = explode ("/", $directory); for ($num = 0; $num < count($pieces); $num += 1) { $so_far = $so_far . "/" . $pieces[$num]; $type = filetype ( $so_far ); if ($type != 'dir' && $type != 'FALSE' && !empty($type)) { return FALSE; } if ($type == 'dir') { continue; } if (!mkdir ($so_far, 0700)) { return FALSE; } } return TRUE; } //*********************************/ // CACHE REFRESH CHECKING FUNCTIONS // Each function checks the next interval higher for verification function checkRefreshYear($systime, $filetime) { $sysYear = date( "y", $systime); $fileYear = date( "y", $filetime); if ($sysYear != $fileYear) { return false; } return true; } function checkRefreshMonth($systime, $filetime) { $sysMonth = date( "M", $systime); $fileMonth = date( "M", $filetime); if ($sysMonth != $fileMonth) { return false; } if (!(checkRefreshYear($systime, $filetime))) { return false; } return true; } function checkRefreshDay($systime, $filetime) { $sysDay = date( "j", $systime); $fileDay = date( "j", $filetime); if ($sysDay != $fileDay) { return false; } if (!(checkRefreshMonth($systime, $filetime))) { return false; } return true; } function checkRefreshDayHalf($systime, $filetime) { $sysHour = date( "H", $systime); $fileHour = date( "H", $filetime); if (($sysHour % 12) < ($fileHour % 12)) { return false; } if (!(checkRefreshDay($systime, $filetime))) { return false; } return true; } function checkRefreshHour($systime, $filetime) { $sysHour = date( "H", $systime); $fileHour = date( "H", $filetime); if ($sysHour != $fileHour) { return false; } if (!(checkRefreshDayHalf($systime, $filetime))) { return false; } return true; } function checkRefreshHalfHour($systime, $filetime) { $sysMin = date( "i", $systime); $fileMin = date( "i", $filetime); if (($sysMin % 30) < ($fileMin % 30)) { return false; } if (!(checkRefreshHour($systime, $filetime))) { return false; } return true; } function checkRefreshQuarterHour($systime, $filetime) { $sysMin = date( "i", $systime); $fileMin = date( "i", $filetime); if (($sysMin % 15) < ($fileMin % 15)) { return false; } if (!(checkRefreshHalfHour($systime, $filetime))) { return false; } return true; } function checkRefreshMinute($systime, $filetime) { $sysMin = date( "i", $systime); $fileMin = date( "i", $filetime); if ($sysMin != $fileMin) { return false; } if (!(checkRefreshQuarterHour($systime, $filetime))) { return false; } return true; } } // End class.FastTemplate ?>