Reworking and optimizing the cache system.

This commit is contained in:
andrey
2001-02-06 21:17:37 +00:00
parent 114740b3bb
commit 8b86a0743e
5 changed files with 275 additions and 269 deletions

10
NEWS
View File

@@ -3,18 +3,18 @@
(Andrei) (Andrei)
- added register_custom_function() and register_modifier() API calls - added register_custom_function() and register_modifier() API calls
to make registering stuff easier. (Andrei) to make registering stuff easier. (Andrei)
- added template results caching capability. (Monte, Andrei) - added template results caching capability. (Monte, Andrei)
- added optional 'options' attribute to html_options custom function - added optional 'options' attribute to html_options custom function
that allows passing associative arrays for values/output. (Andrei) that allows passing associative arrays for values/output. (Andrei)
- modifier arguments can now contain '|' and ':' characters inside quoted - modifier arguments can now contain '|' and ':' characters inside quoted
strings. (Andrei) strings. (Andrei)
Version 1.2.2 Version 1.2.2
------------- -------------
- fixed bug that would not respect nested template directories and would - fixed bug that would not respect nested template directories and would
put all compiled files into top-level one. (Andrei) put all compiled files into top-level one. (Andrei)
- fixed bug using $PHP_VERSION instead of environment var PHP_VERSION. - fixed bug using $PHP_VERSION instead of environment var PHP_VERSION.
(Monte) (Monte)
- a couple small warning fixes. (Monte) - a couple small warning fixes. (Monte)
Version 1.2.1 Version 1.2.1

View File

@@ -37,15 +37,15 @@
* *
*/ */
require("Smarty.addons.php"); require('Smarty.addons.php');
class Smarty class Smarty
{ {
// public vars // public vars
var $template_dir = "./templates"; // name of directory for templates var $template_dir = './templates'; // name of directory for templates
var $compile_dir = "./templates_c"; // name of directory for compiled templates var $compile_dir = './templates_c'; // name of directory for compiled templates
var $config_dir = "./configs"; // directory where config files are located var $config_dir = './configs'; // directory where config files are located
var $global_assign = array('SCRIPT_NAME'); // variables from the GLOBALS array var $global_assign = array('SCRIPT_NAME'); // variables from the GLOBALS array
// that are implicitly assigned // that are implicitly assigned
@@ -61,18 +61,18 @@ class Smarty
// override compile_check and force_compile. // override compile_check and force_compile.
// true/false. default false. // true/false. default false.
var $caching = false; // whether to use caching or not. true/false var $caching = false; // whether to use caching or not. true/false
var $cache_dir = "./cache"; // name of directory for template cache var $cache_dir = './cache'; // name of directory for template cache
var $cache_lifetime = 3600; // number of seconds cached content will persist. var $cache_lifetime = 3600; // number of seconds cached content will persist.
// 0 = never expires. default is one hour (3600) // 0 = never expires. default is one hour (3600)
var $tpl_file_ext = ".tpl"; // template file extention var $tpl_file_ext = '.tpl'; // template file extention
var $allow_php = false; // whether or not to allow embedded php var $allow_php = false; // whether or not to allow embedded php
// in the templates. By default, php tags // in the templates. By default, php tags
// are escaped. true/false. default false. // are escaped. true/false. default false.
var $left_delimiter = "{"; // template tag delimiters. var $left_delimiter = '{'; // template tag delimiters.
var $right_delimiter = "}"; var $right_delimiter = '}';
var $custom_funcs = array( 'html_options' => 'smarty_func_html_options', var $custom_funcs = array( 'html_options' => 'smarty_func_html_options',
@@ -189,50 +189,36 @@ class Smarty
/*======================================================================*\ /*======================================================================*\
Function: clear_cache() Function: clear_cache()
Purpose: clear all cached template files for given template Purpose: clear cached content for the given template
\*======================================================================*/ \*======================================================================*/
function clear_cache($tpl_file) function clear_cache($tpl_file)
{ {
if(is_dir($this->cache_dir)) $cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
$dir_handle = opendir($this->cache_dir); return $this->_clear_tpl_cache_dir($cache_tpl_md5);
else
return false;
// remove leading . or /
$tpl_file = preg_replace("/^(\.{0,2}\/)*/","",$tpl_file);
$tpl_file_url = urlencode($tpl_file);
while($curr_file = readdir($dir_handle)) {
if ($curr_file == '.' || $curr_file == '..')
continue;
if(is_file($this->cache_dir."/".$curr_file) &&
substr($curr_file,0,strlen($tpl_file_url)) == $tpl_file_url)
unlink($this->cache_dir."/".$curr_file);
}
return true;
} }
/*======================================================================*\ /*======================================================================*\
Function: clear_all_cache() Function: clear_all_cache()
Purpose: clear all the cached template files. Purpose: clear the entire contents of cache (all templates)
\*======================================================================*/ \*======================================================================*/
function clear_all_cache() function clear_all_cache()
{ {
if(is_dir($this->cache_dir)) if (!is_dir($this->cache_dir))
$dir_handle = opendir($this->cache_dir);
else
return false; return false;
while($curr_file = readdir($dir_handle)) { $dir_handle = opendir($this->cache_dir);
if ($curr_file == '.' || $curr_file == '..') while ($curr_dir = readdir($dir_handle)) {
if ($curr_dir == '.' || $curr_dir == '..' ||
!is_dir($this->cache_dir.'/'.$curr_dir))
continue; continue;
if(is_file($this->cache_dir."/".$curr_file) && substr($curr_file,-6) == '.cache')
unlink($this->cache_dir."/".$curr_file); $this->_clear_tpl_cache_dir($curr_dir);
} }
closedir($dir_handle);
return true; return true;
} }
@@ -243,22 +229,18 @@ class Smarty
function is_cached($tpl_file) function is_cached($tpl_file)
{ {
global $PHP_SELF;
// cache id = template path + the invoked script // cache id = template path + the invoked script
$cache_tpl_md5 = md5($tpl_file); $cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
$cache_path_md5 = md5($PHP_SELF); $cache_path_md5 = md5($HTTP_SERVER_VARS['SERVER_NAME'].'/'.$HTTP_SERVER_VARS['PHP_SELF']);
$cache_path_dir = substr($cache_path_md5,0,2); $cache_path_dir = substr($cache_path_md5, 0, 2);
$cache_file = $this->cache_dir."/".$cache_tpl_md5."/$cache_path_dir/$cache_tpl_md5"."_"."$cache_path_md5.cache"; $cache_file = $this->cache_dir."/$cache_tpl_md5/$cache_path_dir/$cache_tpl_md5_$cache_path_md5.cache";
if(!$this->cache_force && if (file_exists($cache_file) &&
(file_exists($cache_file) && ($this->cache_lifetime == 0 ||
($this->cache_lifetime == 0 || (time() - filemtime($cache_file) <= $this->cache_lifetime)))
(mktime() - filemtime($cache_file) <= $this->cache_lifetime) return true;
))) else
return true; return false;
else
return false;
} }
@@ -301,27 +283,25 @@ class Smarty
function fetch($tpl_file, $display = false) function fetch($tpl_file, $display = false)
{ {
global $PHP_SELF; global $HTTP_SERVER_VARS;
if($this->caching) { if ($this->caching) {
// cache id = template path + the invoked script // cache id = template path + the invoked script
$cache_tpl_md5 = md5($tpl_file); $cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
$cache_path_md5 = md5($PHP_SELF); $cache_path_md5 = md5($HTTP_SERVER_VARS['SERVER_NAME'].'/'.$HTTP_SERVER_VARS['PHP_SELF']);
$cache_path_dir = substr($cache_path_md5,0,2); $cache_path_dir = substr($cache_path_md5, 0, 2);
$cache_file = $this->cache_dir."/".$cache_tpl_md5."/$cache_path_dir/$cache_tpl_md5"."_"."$cache_path_md5.cache"; $cache_file = $this->cache_dir."/$cache_tpl_md5/$cache_path_dir/$cache_tpl_md5_$cache_path_md5.cache";
if(!$this->cache_force && if (file_exists($cache_file) &&
(file_exists($cache_file) && ($this->cache_lifetime == 0 ||
($this->cache_lifetime == 0 || (time() - filemtime($cache_file) <= $this->cache_lifetime))) {
(mktime() - filemtime($cache_file) <= $this->cache_lifetime) $results = $this->_read_file($cache_file);
))) { $results = $this->_process_cached_inserts($results);
$results = $this->_read_file($cache_file); if ($display) {
$results = $this->_process_cached_inserts($results); echo $results;
if ($display) { return;
echo $results; } else
return; return $results;
} else
return $results;
} }
} }
@@ -368,8 +348,7 @@ class Smarty
return true; return true;
else else
return false; return false;
} } else
else
return false; return false;
} }
@@ -380,23 +359,26 @@ class Smarty
function _traverse_files($tpl_dir, $depth) function _traverse_files($tpl_dir, $depth)
{ {
if(is_dir($tpl_dir)) { $retval = true;
if($tpl_dir)
$dir_handle = opendir($tpl_dir);
while($curr_file = readdir($dir_handle)) { if (is_dir($tpl_dir)) {
$dir_handle = opendir($tpl_dir);
while ($curr_file = readdir($dir_handle)) {
if ($curr_file == '.' || $curr_file == '..') if ($curr_file == '.' || $curr_file == '..')
continue; continue;
$filepath = $tpl_dir."/".$curr_file; $filepath = $tpl_dir.'/'.$curr_file;
if(is_readable($filepath)) { if (is_readable($filepath)) {
if (is_file($filepath) && substr($curr_file, -strlen($this->tpl_file_ext)) == $this->tpl_file_ext) {
if(is_file($filepath) && substr($curr_file, -strlen($this->tpl_file_ext)) == $this->tpl_file_ext) { if (!$this->_process_file($filepath)) {
if(!$this->_process_file($filepath)) $retval = false;
return false; break;
}
} else if (is_dir($filepath)) { } else if (is_dir($filepath)) {
if (!$this->_traverse_files($filepath, $depth + 1)) if (!$this->_traverse_files($filepath, $depth + 1)) {
return false; $retval = false;
break;
}
} else { } else {
// invalid file type, skipping // invalid file type, skipping
$this->_set_error_msg("Invalid filetype for $filepath, skipping"); $this->_set_error_msg("Invalid filetype for $filepath, skipping");
@@ -404,12 +386,13 @@ class Smarty
} }
} }
} }
closedir($dir_handle);
return $retval;
} else { } else {
$this->_set_error_msg("Directory \"$tpl_dir\" does not exist or is not a directory."); $this->_set_error_msg("Directory \"$tpl_dir\" does not exist or is not a directory.");
return false; return false;
} }
return true;
} }
/*======================================================================*\ /*======================================================================*\
@@ -423,23 +406,13 @@ class Smarty
{ {
if(preg_match("/^(.+)\/([^\/]+)$/", $filepath, $match)) { if(preg_match("/^(.+)\/([^\/]+)$/", $filepath, $match)) {
$tpl_file_dir = $match[1]; $tpl_file_dir = $match[1];
$tpl_file_name = $match[2] . ".php"; $tpl_file_name = $match[2] . '.php';
$compile_dir = preg_replace('!^' . preg_quote($this->template_dir, '!') . '!', $compile_dir = preg_replace('!^' . preg_quote($this->template_dir, '!') . '!',
$this->compile_dir, $match[1]); $this->compile_dir, $match[1]);
//create directory if none exists //create directory if none exists
if(!file_exists($compile_dir)) { $this->_create_dir_structure($compile_dir);
$compile_dir_parts = preg_split('!/+!', $compile_dir);
$new_dir = "";
foreach ($compile_dir_parts as $dir_part) {
$new_dir .= $dir_part."/";
if (!file_exists($new_dir) && !mkdir($new_dir, 0755)) {
$this->_set_error_msg("problem creating directory \"$compile_dir\"");
return false;
}
}
}
// compile the template file if none exists or has been modified or recompile is forced // compile the template file if none exists or has been modified or recompile is forced
if ($this->force_compile || !file_exists($compile_dir."/".$tpl_file_name) || if ($this->force_compile || !file_exists($compile_dir."/".$tpl_file_name) ||
@@ -458,6 +431,26 @@ class Smarty
return true; return true;
} }
/*======================================================================*\
Function: _create_dir_structure
Purpose: create full directory structure
\*======================================================================*/
function _create_dir_structure($dir)
{
if (!file_exists($dir)) {
$dir_parts = preg_split('!/+!', $dir);
$new_dir = '';
foreach ($dir_parts as $dir_part) {
$new_dir .= $dir_part;
if (!file_exists($new_dir) && !mkdir($new_dir, 0755)) {
$this->_set_error_msg("problem creating directory \"$dir\"");
return false;
}
$new_dir .= '/';
}
}
}
/*======================================================================*\ /*======================================================================*\
Function: _modified_file() Function: _modified_file()
Input: return comparison of modification times of files Input: return comparison of modification times of files
@@ -465,7 +458,7 @@ class Smarty
function _modified_file($filepath, $compilepath) function _modified_file($filepath, $compilepath)
{ {
if(filemtime($filepath) >= filemtime($compilepath)) if (filemtime($filepath) >= filemtime($compilepath))
return true; return true;
return false; return false;
} }
@@ -509,7 +502,7 @@ class Smarty
$this->_current_line_no += substr_count($template_tags[$i], "\n"); $this->_current_line_no += substr_count($template_tags[$i], "\n");
} }
$compiled_contents = ""; $compiled_contents = '';
/* Interleave the compiled contents and text blocks to get the final result. */ /* Interleave the compiled contents and text blocks to get the final result. */
for ($i = 0; $i < count($compiled_tags); $i++) { for ($i = 0; $i < count($compiled_tags); $i++) {
@@ -1192,7 +1185,7 @@ class Smarty
function _read_file($filename) function _read_file($filename)
{ {
if(!($fd = fopen($filename, 'r'))) { if (!($fd = fopen($filename, 'r'))) {
$this->_set_error_msg("problem reading '$filename.'"); $this->_set_error_msg("problem reading '$filename.'");
return false; return false;
} }
@@ -1206,40 +1199,12 @@ class Smarty
Purpose: write out a file Purpose: write out a file
\*======================================================================*/ \*======================================================================*/
function _write_file($filename,$contents,$create_dirs=false) function _write_file($filename, $contents, $create_dirs = false)
{ {
if($create_dirs) { if($create_dirs)
$filename_split = split("\/+",$filename); $this->_create_dir_structure(dirname($filename));
foreach($filename_split as $curr_dir) {
if(empty($curr_dir)) {
if(empty($dir_sum))
$dir_sum = "/";
continue;
}
if($curr_dir == ".." || $curr_dir == ".") {
$dir_sum .= $curr_dir."/";
continue;
}
if(substr($curr_dir,-6) == ".cache")
break;
if(empty($dir_sum))
$dir_sum .= $curr_dir;
else
$dir_sum .= "/".$curr_dir;
if(is_dir($dir_sum))
continue;
if(file_exists($dir_sum))
continue;
mkdir($dir_sum,0755);
}
}
if(!($fd = fopen($filename, 'w'))) { if (!($fd = fopen($filename, 'w'))) {
$this->_set_error_msg("problem writing '$filename.'"); $this->_set_error_msg("problem writing '$filename.'");
return false; return false;
} }
@@ -1247,7 +1212,45 @@ class Smarty
fclose($fd); fclose($fd);
return true; return true;
} }
/*======================================================================*\
Function: _clear_tpl_cache_dir
Purpose: Clear the specified template cache directory
\*======================================================================*/
function _clear_tpl_cache_dir($tpl_cache_dir)
{
$cache_dir = $this->cache_dir.'/'.$tpl_cache_dir;
if (!is_dir($cache_dir))
return false;
$dir_handle = opendir($cache_dir);
while ($curr_dir = readdir($dir_handle)) {
$cache_path_dir = $cache_dir.'/'.$curr_dir;
if ($curr_dir == '.' || $curr_dir == '..' ||
!is_dir($cache_path_dir))
continue;
$dir_handle2 = opendir($cache_path_dir);
while ($curr_file = readdir($dir_handle2)) {
if ($curr_file == '.' || $curr_file == '..')
continue;
$cache_file = $cache_path_dir.'/'.$curr_file;
if (is_file($cache_file))
unlink($cache_file);
}
closedir($dir_handle2);
@rmdir($cache_path_dir);
}
closedir($dir_handle);
@rmdir($cache_dir);
return true;
}
/*======================================================================*\ /*======================================================================*\
Function: _set_error_msg() Function: _set_error_msg()
Purpose: set the error message Purpose: set the error message

View File

@@ -8,7 +8,7 @@
Title: {#title#|capitalize} Title: {#title#|capitalize}
{if #bold#}</b>{/if} {if #bold#}</b>{/if}
the value of $SCRIPT_NAME is {$SCRIPT_NAME} the value of $SCRIPT_NAME is {$SCRIPT_NAME}
{* A simple variable test. print $Name in uppercase *} {* A simple variable test. print $Name in uppercase *}
hello, my name is {$Name|upper} hello, my name is {$Name|upper}

View File

@@ -37,15 +37,15 @@
* *
*/ */
require("Smarty.addons.php"); require('Smarty.addons.php');
class Smarty class Smarty
{ {
// public vars // public vars
var $template_dir = "./templates"; // name of directory for templates var $template_dir = './templates'; // name of directory for templates
var $compile_dir = "./templates_c"; // name of directory for compiled templates var $compile_dir = './templates_c'; // name of directory for compiled templates
var $config_dir = "./configs"; // directory where config files are located var $config_dir = './configs'; // directory where config files are located
var $global_assign = array('SCRIPT_NAME'); // variables from the GLOBALS array var $global_assign = array('SCRIPT_NAME'); // variables from the GLOBALS array
// that are implicitly assigned // that are implicitly assigned
@@ -61,18 +61,18 @@ class Smarty
// override compile_check and force_compile. // override compile_check and force_compile.
// true/false. default false. // true/false. default false.
var $caching = false; // whether to use caching or not. true/false var $caching = false; // whether to use caching or not. true/false
var $cache_dir = "./cache"; // name of directory for template cache var $cache_dir = './cache'; // name of directory for template cache
var $cache_lifetime = 3600; // number of seconds cached content will persist. var $cache_lifetime = 3600; // number of seconds cached content will persist.
// 0 = never expires. default is one hour (3600) // 0 = never expires. default is one hour (3600)
var $tpl_file_ext = ".tpl"; // template file extention var $tpl_file_ext = '.tpl'; // template file extention
var $allow_php = false; // whether or not to allow embedded php var $allow_php = false; // whether or not to allow embedded php
// in the templates. By default, php tags // in the templates. By default, php tags
// are escaped. true/false. default false. // are escaped. true/false. default false.
var $left_delimiter = "{"; // template tag delimiters. var $left_delimiter = '{'; // template tag delimiters.
var $right_delimiter = "}"; var $right_delimiter = '}';
var $custom_funcs = array( 'html_options' => 'smarty_func_html_options', var $custom_funcs = array( 'html_options' => 'smarty_func_html_options',
@@ -189,50 +189,36 @@ class Smarty
/*======================================================================*\ /*======================================================================*\
Function: clear_cache() Function: clear_cache()
Purpose: clear all cached template files for given template Purpose: clear cached content for the given template
\*======================================================================*/ \*======================================================================*/
function clear_cache($tpl_file) function clear_cache($tpl_file)
{ {
if(is_dir($this->cache_dir)) $cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
$dir_handle = opendir($this->cache_dir); return $this->_clear_tpl_cache_dir($cache_tpl_md5);
else
return false;
// remove leading . or /
$tpl_file = preg_replace("/^(\.{0,2}\/)*/","",$tpl_file);
$tpl_file_url = urlencode($tpl_file);
while($curr_file = readdir($dir_handle)) {
if ($curr_file == '.' || $curr_file == '..')
continue;
if(is_file($this->cache_dir."/".$curr_file) &&
substr($curr_file,0,strlen($tpl_file_url)) == $tpl_file_url)
unlink($this->cache_dir."/".$curr_file);
}
return true;
} }
/*======================================================================*\ /*======================================================================*\
Function: clear_all_cache() Function: clear_all_cache()
Purpose: clear all the cached template files. Purpose: clear the entire contents of cache (all templates)
\*======================================================================*/ \*======================================================================*/
function clear_all_cache() function clear_all_cache()
{ {
if(is_dir($this->cache_dir)) if (!is_dir($this->cache_dir))
$dir_handle = opendir($this->cache_dir);
else
return false; return false;
while($curr_file = readdir($dir_handle)) { $dir_handle = opendir($this->cache_dir);
if ($curr_file == '.' || $curr_file == '..') while ($curr_dir = readdir($dir_handle)) {
if ($curr_dir == '.' || $curr_dir == '..' ||
!is_dir($this->cache_dir.'/'.$curr_dir))
continue; continue;
if(is_file($this->cache_dir."/".$curr_file) && substr($curr_file,-6) == '.cache')
unlink($this->cache_dir."/".$curr_file); $this->_clear_tpl_cache_dir($curr_dir);
} }
closedir($dir_handle);
return true; return true;
} }
@@ -243,22 +229,18 @@ class Smarty
function is_cached($tpl_file) function is_cached($tpl_file)
{ {
global $PHP_SELF;
// cache id = template path + the invoked script // cache id = template path + the invoked script
$cache_tpl_md5 = md5($tpl_file); $cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
$cache_path_md5 = md5($PHP_SELF); $cache_path_md5 = md5($HTTP_SERVER_VARS['SERVER_NAME'].'/'.$HTTP_SERVER_VARS['PHP_SELF']);
$cache_path_dir = substr($cache_path_md5,0,2); $cache_path_dir = substr($cache_path_md5, 0, 2);
$cache_file = $this->cache_dir."/".$cache_tpl_md5."/$cache_path_dir/$cache_tpl_md5"."_"."$cache_path_md5.cache"; $cache_file = $this->cache_dir."/$cache_tpl_md5/$cache_path_dir/$cache_tpl_md5_$cache_path_md5.cache";
if(!$this->cache_force && if (file_exists($cache_file) &&
(file_exists($cache_file) && ($this->cache_lifetime == 0 ||
($this->cache_lifetime == 0 || (time() - filemtime($cache_file) <= $this->cache_lifetime)))
(mktime() - filemtime($cache_file) <= $this->cache_lifetime) return true;
))) else
return true; return false;
else
return false;
} }
@@ -301,27 +283,25 @@ class Smarty
function fetch($tpl_file, $display = false) function fetch($tpl_file, $display = false)
{ {
global $PHP_SELF; global $HTTP_SERVER_VARS;
if($this->caching) { if ($this->caching) {
// cache id = template path + the invoked script // cache id = template path + the invoked script
$cache_tpl_md5 = md5($tpl_file); $cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file));
$cache_path_md5 = md5($PHP_SELF); $cache_path_md5 = md5($HTTP_SERVER_VARS['SERVER_NAME'].'/'.$HTTP_SERVER_VARS['PHP_SELF']);
$cache_path_dir = substr($cache_path_md5,0,2); $cache_path_dir = substr($cache_path_md5, 0, 2);
$cache_file = $this->cache_dir."/".$cache_tpl_md5."/$cache_path_dir/$cache_tpl_md5"."_"."$cache_path_md5.cache"; $cache_file = $this->cache_dir."/$cache_tpl_md5/$cache_path_dir/$cache_tpl_md5_$cache_path_md5.cache";
if(!$this->cache_force && if (file_exists($cache_file) &&
(file_exists($cache_file) && ($this->cache_lifetime == 0 ||
($this->cache_lifetime == 0 || (time() - filemtime($cache_file) <= $this->cache_lifetime))) {
(mktime() - filemtime($cache_file) <= $this->cache_lifetime) $results = $this->_read_file($cache_file);
))) { $results = $this->_process_cached_inserts($results);
$results = $this->_read_file($cache_file); if ($display) {
$results = $this->_process_cached_inserts($results); echo $results;
if ($display) { return;
echo $results; } else
return; return $results;
} else
return $results;
} }
} }
@@ -368,8 +348,7 @@ class Smarty
return true; return true;
else else
return false; return false;
} } else
else
return false; return false;
} }
@@ -380,23 +359,26 @@ class Smarty
function _traverse_files($tpl_dir, $depth) function _traverse_files($tpl_dir, $depth)
{ {
if(is_dir($tpl_dir)) { $retval = true;
if($tpl_dir)
$dir_handle = opendir($tpl_dir);
while($curr_file = readdir($dir_handle)) { if (is_dir($tpl_dir)) {
$dir_handle = opendir($tpl_dir);
while ($curr_file = readdir($dir_handle)) {
if ($curr_file == '.' || $curr_file == '..') if ($curr_file == '.' || $curr_file == '..')
continue; continue;
$filepath = $tpl_dir."/".$curr_file; $filepath = $tpl_dir.'/'.$curr_file;
if(is_readable($filepath)) { if (is_readable($filepath)) {
if (is_file($filepath) && substr($curr_file, -strlen($this->tpl_file_ext)) == $this->tpl_file_ext) {
if(is_file($filepath) && substr($curr_file, -strlen($this->tpl_file_ext)) == $this->tpl_file_ext) { if (!$this->_process_file($filepath)) {
if(!$this->_process_file($filepath)) $retval = false;
return false; break;
}
} else if (is_dir($filepath)) { } else if (is_dir($filepath)) {
if (!$this->_traverse_files($filepath, $depth + 1)) if (!$this->_traverse_files($filepath, $depth + 1)) {
return false; $retval = false;
break;
}
} else { } else {
// invalid file type, skipping // invalid file type, skipping
$this->_set_error_msg("Invalid filetype for $filepath, skipping"); $this->_set_error_msg("Invalid filetype for $filepath, skipping");
@@ -404,12 +386,13 @@ class Smarty
} }
} }
} }
closedir($dir_handle);
return $retval;
} else { } else {
$this->_set_error_msg("Directory \"$tpl_dir\" does not exist or is not a directory."); $this->_set_error_msg("Directory \"$tpl_dir\" does not exist or is not a directory.");
return false; return false;
} }
return true;
} }
/*======================================================================*\ /*======================================================================*\
@@ -423,23 +406,13 @@ class Smarty
{ {
if(preg_match("/^(.+)\/([^\/]+)$/", $filepath, $match)) { if(preg_match("/^(.+)\/([^\/]+)$/", $filepath, $match)) {
$tpl_file_dir = $match[1]; $tpl_file_dir = $match[1];
$tpl_file_name = $match[2] . ".php"; $tpl_file_name = $match[2] . '.php';
$compile_dir = preg_replace('!^' . preg_quote($this->template_dir, '!') . '!', $compile_dir = preg_replace('!^' . preg_quote($this->template_dir, '!') . '!',
$this->compile_dir, $match[1]); $this->compile_dir, $match[1]);
//create directory if none exists //create directory if none exists
if(!file_exists($compile_dir)) { $this->_create_dir_structure($compile_dir);
$compile_dir_parts = preg_split('!/+!', $compile_dir);
$new_dir = "";
foreach ($compile_dir_parts as $dir_part) {
$new_dir .= $dir_part."/";
if (!file_exists($new_dir) && !mkdir($new_dir, 0755)) {
$this->_set_error_msg("problem creating directory \"$compile_dir\"");
return false;
}
}
}
// compile the template file if none exists or has been modified or recompile is forced // compile the template file if none exists or has been modified or recompile is forced
if ($this->force_compile || !file_exists($compile_dir."/".$tpl_file_name) || if ($this->force_compile || !file_exists($compile_dir."/".$tpl_file_name) ||
@@ -458,6 +431,26 @@ class Smarty
return true; return true;
} }
/*======================================================================*\
Function: _create_dir_structure
Purpose: create full directory structure
\*======================================================================*/
function _create_dir_structure($dir)
{
if (!file_exists($dir)) {
$dir_parts = preg_split('!/+!', $dir);
$new_dir = '';
foreach ($dir_parts as $dir_part) {
$new_dir .= $dir_part;
if (!file_exists($new_dir) && !mkdir($new_dir, 0755)) {
$this->_set_error_msg("problem creating directory \"$dir\"");
return false;
}
$new_dir .= '/';
}
}
}
/*======================================================================*\ /*======================================================================*\
Function: _modified_file() Function: _modified_file()
Input: return comparison of modification times of files Input: return comparison of modification times of files
@@ -465,7 +458,7 @@ class Smarty
function _modified_file($filepath, $compilepath) function _modified_file($filepath, $compilepath)
{ {
if(filemtime($filepath) >= filemtime($compilepath)) if (filemtime($filepath) >= filemtime($compilepath))
return true; return true;
return false; return false;
} }
@@ -509,7 +502,7 @@ class Smarty
$this->_current_line_no += substr_count($template_tags[$i], "\n"); $this->_current_line_no += substr_count($template_tags[$i], "\n");
} }
$compiled_contents = ""; $compiled_contents = '';
/* Interleave the compiled contents and text blocks to get the final result. */ /* Interleave the compiled contents and text blocks to get the final result. */
for ($i = 0; $i < count($compiled_tags); $i++) { for ($i = 0; $i < count($compiled_tags); $i++) {
@@ -1192,7 +1185,7 @@ class Smarty
function _read_file($filename) function _read_file($filename)
{ {
if(!($fd = fopen($filename, 'r'))) { if (!($fd = fopen($filename, 'r'))) {
$this->_set_error_msg("problem reading '$filename.'"); $this->_set_error_msg("problem reading '$filename.'");
return false; return false;
} }
@@ -1206,40 +1199,12 @@ class Smarty
Purpose: write out a file Purpose: write out a file
\*======================================================================*/ \*======================================================================*/
function _write_file($filename,$contents,$create_dirs=false) function _write_file($filename, $contents, $create_dirs = false)
{ {
if($create_dirs) { if($create_dirs)
$filename_split = split("\/+",$filename); $this->_create_dir_structure(dirname($filename));
foreach($filename_split as $curr_dir) {
if(empty($curr_dir)) {
if(empty($dir_sum))
$dir_sum = "/";
continue;
}
if($curr_dir == ".." || $curr_dir == ".") {
$dir_sum .= $curr_dir."/";
continue;
}
if(substr($curr_dir,-6) == ".cache")
break;
if(empty($dir_sum))
$dir_sum .= $curr_dir;
else
$dir_sum .= "/".$curr_dir;
if(is_dir($dir_sum))
continue;
if(file_exists($dir_sum))
continue;
mkdir($dir_sum,0755);
}
}
if(!($fd = fopen($filename, 'w'))) { if (!($fd = fopen($filename, 'w'))) {
$this->_set_error_msg("problem writing '$filename.'"); $this->_set_error_msg("problem writing '$filename.'");
return false; return false;
} }
@@ -1247,7 +1212,45 @@ class Smarty
fclose($fd); fclose($fd);
return true; return true;
} }
/*======================================================================*\
Function: _clear_tpl_cache_dir
Purpose: Clear the specified template cache directory
\*======================================================================*/
function _clear_tpl_cache_dir($tpl_cache_dir)
{
$cache_dir = $this->cache_dir.'/'.$tpl_cache_dir;
if (!is_dir($cache_dir))
return false;
$dir_handle = opendir($cache_dir);
while ($curr_dir = readdir($dir_handle)) {
$cache_path_dir = $cache_dir.'/'.$curr_dir;
if ($curr_dir == '.' || $curr_dir == '..' ||
!is_dir($cache_path_dir))
continue;
$dir_handle2 = opendir($cache_path_dir);
while ($curr_file = readdir($dir_handle2)) {
if ($curr_file == '.' || $curr_file == '..')
continue;
$cache_file = $cache_path_dir.'/'.$curr_file;
if (is_file($cache_file))
unlink($cache_file);
}
closedir($dir_handle2);
@rmdir($cache_path_dir);
}
closedir($dir_handle);
@rmdir($cache_dir);
return true;
}
/*======================================================================*\ /*======================================================================*\
Function: _set_error_msg() Function: _set_error_msg()
Purpose: set the error message Purpose: set the error message

View File

@@ -8,7 +8,7 @@
Title: {#title#|capitalize} Title: {#title#|capitalize}
{if #bold#}</b>{/if} {if #bold#}</b>{/if}
the value of $SCRIPT_NAME is {$SCRIPT_NAME} the value of $SCRIPT_NAME is {$SCRIPT_NAME}
{* A simple variable test. print $Name in uppercase *} {* A simple variable test. print $Name in uppercase *}
hello, my name is {$Name|upper} hello, my name is {$Name|upper}