fix problem with filenames on windows, add ability to supply expire time in seconds when clearing cache or compiled files

This commit is contained in:
mohrt
2002-07-16 15:04:02 +00:00
parent 3a413e56e4
commit 13772a2c44
7 changed files with 233 additions and 52 deletions

3
NEWS
View File

@@ -1,3 +1,6 @@
- add {debug} plugin to distribution (Monte)
- fixed bug with insert tags, loading from "script" attribute
when caching is enabled (Monte)
- fix bug with debug_tpl file path with Windows (.SMK., Monte)
- fix append() function with string/array problem (Monte)

View File

@@ -457,8 +457,9 @@ class Smarty
Function: clear_cache()
Purpose: clear cached content for the given template and cache id
\*======================================================================*/
function clear_cache($tpl_file = null, $cache_id = null, $compile_id = null)
function clear_cache($tpl_file = null, $cache_id = null, $compile_id = null, $exp_time = null)
{
if (!isset($compile_id))
$compile_id = $this->compile_id;
@@ -473,8 +474,9 @@ class Smarty
$funcname = $this->cache_handler_func;
return $funcname('clear', $this, $dummy, $tpl_file, $cache_id, $compile_id);
} else {
return $this->_rm_auto($this->cache_dir, $tpl_file, $auto_id);
return $this->_rm_auto($this->cache_dir, $tpl_file, $auto_id, $exp_time);
}
}
@@ -482,13 +484,13 @@ class Smarty
Function: clear_all_cache()
Purpose: clear the entire contents of cache (all templates)
\*======================================================================*/
function clear_all_cache()
function clear_all_cache($exp_time = null)
{
if (!empty($this->cache_handler_func)) {
$funcname = $this->cache_handler_func;
return $funcname('clear', $this, $dummy);
} else {
return $this->_rm_auto($this->cache_dir);
return $this->_rm_auto($this->cache_dir,null,null,$exp_time);
}
}
@@ -524,11 +526,11 @@ class Smarty
or all compiled template files if one is not specified.
This function is for advanced use only, not normally needed.
\*======================================================================*/
function clear_compiled_tpl($tpl_file = null, $compile_id = null)
function clear_compiled_tpl($tpl_file = null, $compile_id = null, $exp_time = null)
{
if (!isset($compile_id))
$compile_id = $this->compile_id;
return $this->_rm_auto($this->compile_dir, $tpl_file, $compile_id);
return $this->_rm_auto($this->compile_dir, $tpl_file, $compile_id, $exp_time);
}
/*======================================================================*\
@@ -727,26 +729,30 @@ class Smarty
function _generate_debug_output() {
// we must force compile the debug template in case the environment
// changed between separate applications.
$_orig_ldelim = $this->left_delimiter;
$_orig_rdelim = $this->right_delimiter;
$_ldelim_orig = $this->left_delimiter;
$_rdelim_orig = $this->right_delimiter;
$this->left_delimiter = '{';
$this->right_delimiter = '}';
ob_start();
$force_compile_orig = $this->force_compile;
$_force_compile_orig = $this->force_compile;
$this->force_compile = true;
$_compile_id_orig = $this->_compile_id;
$this->_compile_id = null;
$compile_path = $this->_get_compile_path($this->debug_tpl);
if ($this->_process_template($this->debug_tpl, $compile_path))
{
ob_start();
include($compile_path);
$results = ob_get_contents();
ob_end_clean();
}
$results = ob_get_contents();
$this->force_compile = $force_compile_orig;
ob_end_clean();
$this->force_compile = $_force_compile_orig;
$this->_compile_id = $_compile_id_orig;
$this->left_delimiter = $_orig_ldelim;
$this->right_delimiter = $_orig_rdelim;
$this->left_delimiter = $_ldelim_orig;
$this->right_delimiter = $_rdelim_orig;
return $results;
}
@@ -1458,11 +1464,11 @@ function _run_insert_handler($args)
if(isset($auto_source)) {
// make source name safe for filename
if($this->use_sub_dirs) {
$_filename = basename($auto_source);
$_filename = urlencode(basename($auto_source));
$_crc32 = crc32($auto_source) . $_dir_sep;
// prepend N in case crc32 was negative to avoid possible
// OS issues with directory names starting with a "-"
$_crc32 = 'N' . substr($_crc32,0,3) . $_dir_sep . 'N' . $_crc32;
// prepend %% to avoid name conflicts with
// with $auto_id names
$_crc32 = '%%' . substr($_crc32,0,3) . $_dir_sep . '%%' . $_crc32;
$res .= $_crc32 . $_filename . '.php';
} else {
$res .= str_replace($_dir_sep_enc,'^',urlencode($auto_source));
@@ -1476,20 +1482,20 @@ function _run_insert_handler($args)
Function: _rm_auto
Purpose: delete an automagically created file by name and id
\*======================================================================*/
function _rm_auto($auto_base, $auto_source = null, $auto_id = null)
function _rm_auto($auto_base, $auto_source = null, $auto_id = null, $exp_time = null)
{
if (!is_dir($auto_base))
return false;
if(!isset($auto_id) && !isset($auto_source)) {
$res = $this->_rmdir($auto_base, 0);
$res = $this->_rmdir($auto_base, 0, $exp_time);
} else {
$tname = $this->_get_auto_filename($auto_base, $auto_source, $auto_id);
if(isset($auto_source)) {
$res = @unlink($tname);
} elseif ($this->use_sub_dirs) {
$res = $this->_rmdir($tname, 1);
$res = $this->_rmdir($tname, 1, $exp_time);
} else {
// remove matching file names
$handle = opendir($auto_base);
@@ -1497,7 +1503,7 @@ function _run_insert_handler($args)
if($filename == '.' || $filename == '..') {
continue;
} elseif (substr($auto_base . DIR_SEP . $filename,0,strlen($tname)) == $tname) {
unlink($auto_base . DIR_SEP . $filename);
$this->_unlink($auto_base . DIR_SEP . $filename, $exp_time);
}
}
}
@@ -1511,7 +1517,7 @@ function _run_insert_handler($args)
Purpose: delete a dir recursively (level=0 -> keep root)
WARNING: no security whatsoever!!
\*======================================================================*/
function _rmdir($dirname, $level = 1)
function _rmdir($dirname, $level = 1, $exp_time = null)
{
if($handle = @opendir($dirname)) {
@@ -1519,10 +1525,10 @@ function _run_insert_handler($args)
while ($entry = readdir($handle)) {
if ($entry != '.' && $entry != '..') {
if (is_dir($dirname . DIR_SEP . $entry)) {
$this->_rmdir($dirname . DIR_SEP . $entry, $level + 1);
$this->_rmdir($dirname . DIR_SEP . $entry, $level + 1, $exp_time);
}
else {
unlink($dirname . DIR_SEP . $entry);
$this->_unlink($dirname . DIR_SEP . $entry, $exp_time);
}
}
}
@@ -1539,6 +1545,21 @@ function _run_insert_handler($args)
}
}
/*======================================================================*\
Function: _unlink
Purpose: unlink a file, possibly using expiration time
\*======================================================================*/
function _unlink($resource, $exp_time = null)
{
if(isset($exp_time)) {
if(time() - filemtime($resource) >= $exp_time) {
unlink($resource);
}
} else {
unlink($resource);
}
}
/*======================================================================*\
Function: _create_dir_structure
Purpose: create full directory structure

View File

@@ -4,6 +4,31 @@
{assign_debug_info}
{if $_smarty_debug_output eq "html"}
<HTML><TITLE>Smarty Debug Console_"+self.name+"</TITLE><BODY bgcolor=#ffffff>
<table border=0 width=100%>
<tr bgcolor=#cccccc><th colspan=2>Smarty Debug Console</th></tr>
<tr bgcolor=#cccccc><td colspan=2><b>included templates & config files (load time in seconds):</b></td></tr>
{section name=templates loop=$_debug_tpls}
<tr bgcolor={if %templates.index% is even}#eeeeee{else}#fafafa{/if}><td colspan=2><tt>{section name=indent loop=$_debug_tpls[templates].depth}&nbsp;&nbsp;&nbsp;{/section}<font color={if $_debug_tpls[templates].type eq "template"}brown{elseif $_debug_tpls[templates].type eq "insert"}black{else}green{/if}>{$_debug_tpls[templates].filename}</font> <font size=-1><i>({$_debug_tpls[templates].exec_time|string_format:"%.5f"}){if %templates.index% eq 0} (total){/if}</i></font></tt></td></tr>
{sectionelse}
<tr bgcolor=#eeeeee><td colspan=2><tt><i>no templates included</i></tt></td></tr>
{/section}
<tr bgcolor=#cccccc><td colspan=2><b>assigned template variables:</b></td></tr>
{section name=vars loop=$_debug_keys}
<tr bgcolor={if %vars.index% is even}#eeeeee{else}#fafafa{/if}><td valign=top><tt><font color=blue>{ldelim}${$_debug_keys[vars]}{rdelim}</font></tt></td><td nowrap><tt><font color=green>{$_debug_vals[vars]|@debug_print_var}</font></tt></td></tr>
{sectionelse}
<tr bgcolor=#eeeeee><td colspan=2><tt><i>no template variables assigned</i></tt></td></tr>
{/section}
<tr bgcolor=#cccccc><td colspan=2><b>assigned config file variables (outter template scope):</b></td></tr>
{section name=config_vars loop=$_debug_config_keys}
<tr bgcolor={if %config_vars.index% is even}#eeeeee{else}#fafafa{/if}><td valign=top><tt><font color=maroon>{ldelim}#{$_debug_config_keys[config_vars]}#{rdelim}</font></tt></td><td><tt><font color=green>{$_debug_config_vals[config_vars]|@debug_print_var}</font></tt></td></tr>
{sectionelse}
<tr bgcolor=#eeeeee><td colspan=2><tt><i>no config vars assigned</i></tt></td></tr>
{/section}
</table>
</BODY></HTML>
{else}
<SCRIPT language=javascript>
if( self.name == '' ) {ldelim}
var title = 'Console';
@@ -37,3 +62,4 @@
_smarty_console.document.write("</BODY></HTML>");
_smarty_console.document.close();
</SCRIPT>
{/if}

View File

@@ -457,8 +457,9 @@ class Smarty
Function: clear_cache()
Purpose: clear cached content for the given template and cache id
\*======================================================================*/
function clear_cache($tpl_file = null, $cache_id = null, $compile_id = null)
function clear_cache($tpl_file = null, $cache_id = null, $compile_id = null, $exp_time = null)
{
if (!isset($compile_id))
$compile_id = $this->compile_id;
@@ -473,8 +474,9 @@ class Smarty
$funcname = $this->cache_handler_func;
return $funcname('clear', $this, $dummy, $tpl_file, $cache_id, $compile_id);
} else {
return $this->_rm_auto($this->cache_dir, $tpl_file, $auto_id);
return $this->_rm_auto($this->cache_dir, $tpl_file, $auto_id, $exp_time);
}
}
@@ -482,13 +484,13 @@ class Smarty
Function: clear_all_cache()
Purpose: clear the entire contents of cache (all templates)
\*======================================================================*/
function clear_all_cache()
function clear_all_cache($exp_time = null)
{
if (!empty($this->cache_handler_func)) {
$funcname = $this->cache_handler_func;
return $funcname('clear', $this, $dummy);
} else {
return $this->_rm_auto($this->cache_dir);
return $this->_rm_auto($this->cache_dir,null,null,$exp_time);
}
}
@@ -524,11 +526,11 @@ class Smarty
or all compiled template files if one is not specified.
This function is for advanced use only, not normally needed.
\*======================================================================*/
function clear_compiled_tpl($tpl_file = null, $compile_id = null)
function clear_compiled_tpl($tpl_file = null, $compile_id = null, $exp_time = null)
{
if (!isset($compile_id))
$compile_id = $this->compile_id;
return $this->_rm_auto($this->compile_dir, $tpl_file, $compile_id);
return $this->_rm_auto($this->compile_dir, $tpl_file, $compile_id, $exp_time);
}
/*======================================================================*\
@@ -727,26 +729,30 @@ class Smarty
function _generate_debug_output() {
// we must force compile the debug template in case the environment
// changed between separate applications.
$_orig_ldelim = $this->left_delimiter;
$_orig_rdelim = $this->right_delimiter;
$_ldelim_orig = $this->left_delimiter;
$_rdelim_orig = $this->right_delimiter;
$this->left_delimiter = '{';
$this->right_delimiter = '}';
ob_start();
$force_compile_orig = $this->force_compile;
$_force_compile_orig = $this->force_compile;
$this->force_compile = true;
$_compile_id_orig = $this->_compile_id;
$this->_compile_id = null;
$compile_path = $this->_get_compile_path($this->debug_tpl);
if ($this->_process_template($this->debug_tpl, $compile_path))
{
ob_start();
include($compile_path);
$results = ob_get_contents();
ob_end_clean();
}
$results = ob_get_contents();
$this->force_compile = $force_compile_orig;
ob_end_clean();
$this->force_compile = $_force_compile_orig;
$this->_compile_id = $_compile_id_orig;
$this->left_delimiter = $_orig_ldelim;
$this->right_delimiter = $_orig_rdelim;
$this->left_delimiter = $_ldelim_orig;
$this->right_delimiter = $_rdelim_orig;
return $results;
}
@@ -1458,11 +1464,11 @@ function _run_insert_handler($args)
if(isset($auto_source)) {
// make source name safe for filename
if($this->use_sub_dirs) {
$_filename = basename($auto_source);
$_filename = urlencode(basename($auto_source));
$_crc32 = crc32($auto_source) . $_dir_sep;
// prepend N in case crc32 was negative to avoid possible
// OS issues with directory names starting with a "-"
$_crc32 = 'N' . substr($_crc32,0,3) . $_dir_sep . 'N' . $_crc32;
// prepend %% to avoid name conflicts with
// with $auto_id names
$_crc32 = '%%' . substr($_crc32,0,3) . $_dir_sep . '%%' . $_crc32;
$res .= $_crc32 . $_filename . '.php';
} else {
$res .= str_replace($_dir_sep_enc,'^',urlencode($auto_source));
@@ -1476,20 +1482,20 @@ function _run_insert_handler($args)
Function: _rm_auto
Purpose: delete an automagically created file by name and id
\*======================================================================*/
function _rm_auto($auto_base, $auto_source = null, $auto_id = null)
function _rm_auto($auto_base, $auto_source = null, $auto_id = null, $exp_time = null)
{
if (!is_dir($auto_base))
return false;
if(!isset($auto_id) && !isset($auto_source)) {
$res = $this->_rmdir($auto_base, 0);
$res = $this->_rmdir($auto_base, 0, $exp_time);
} else {
$tname = $this->_get_auto_filename($auto_base, $auto_source, $auto_id);
if(isset($auto_source)) {
$res = @unlink($tname);
} elseif ($this->use_sub_dirs) {
$res = $this->_rmdir($tname, 1);
$res = $this->_rmdir($tname, 1, $exp_time);
} else {
// remove matching file names
$handle = opendir($auto_base);
@@ -1497,7 +1503,7 @@ function _run_insert_handler($args)
if($filename == '.' || $filename == '..') {
continue;
} elseif (substr($auto_base . DIR_SEP . $filename,0,strlen($tname)) == $tname) {
unlink($auto_base . DIR_SEP . $filename);
$this->_unlink($auto_base . DIR_SEP . $filename, $exp_time);
}
}
}
@@ -1511,7 +1517,7 @@ function _run_insert_handler($args)
Purpose: delete a dir recursively (level=0 -> keep root)
WARNING: no security whatsoever!!
\*======================================================================*/
function _rmdir($dirname, $level = 1)
function _rmdir($dirname, $level = 1, $exp_time = null)
{
if($handle = @opendir($dirname)) {
@@ -1519,10 +1525,10 @@ function _run_insert_handler($args)
while ($entry = readdir($handle)) {
if ($entry != '.' && $entry != '..') {
if (is_dir($dirname . DIR_SEP . $entry)) {
$this->_rmdir($dirname . DIR_SEP . $entry, $level + 1);
$this->_rmdir($dirname . DIR_SEP . $entry, $level + 1, $exp_time);
}
else {
unlink($dirname . DIR_SEP . $entry);
$this->_unlink($dirname . DIR_SEP . $entry, $exp_time);
}
}
}
@@ -1539,6 +1545,21 @@ function _run_insert_handler($args)
}
}
/*======================================================================*\
Function: _unlink
Purpose: unlink a file, possibly using expiration time
\*======================================================================*/
function _unlink($resource, $exp_time = null)
{
if(isset($exp_time)) {
if(time() - filemtime($resource) >= $exp_time) {
unlink($resource);
}
} else {
unlink($resource);
}
}
/*======================================================================*\
Function: _create_dir_structure
Purpose: create full directory structure

View File

@@ -4,6 +4,31 @@
{assign_debug_info}
{if $_smarty_debug_output eq "html"}
<HTML><TITLE>Smarty Debug Console_"+self.name+"</TITLE><BODY bgcolor=#ffffff>
<table border=0 width=100%>
<tr bgcolor=#cccccc><th colspan=2>Smarty Debug Console</th></tr>
<tr bgcolor=#cccccc><td colspan=2><b>included templates & config files (load time in seconds):</b></td></tr>
{section name=templates loop=$_debug_tpls}
<tr bgcolor={if %templates.index% is even}#eeeeee{else}#fafafa{/if}><td colspan=2><tt>{section name=indent loop=$_debug_tpls[templates].depth}&nbsp;&nbsp;&nbsp;{/section}<font color={if $_debug_tpls[templates].type eq "template"}brown{elseif $_debug_tpls[templates].type eq "insert"}black{else}green{/if}>{$_debug_tpls[templates].filename}</font> <font size=-1><i>({$_debug_tpls[templates].exec_time|string_format:"%.5f"}){if %templates.index% eq 0} (total){/if}</i></font></tt></td></tr>
{sectionelse}
<tr bgcolor=#eeeeee><td colspan=2><tt><i>no templates included</i></tt></td></tr>
{/section}
<tr bgcolor=#cccccc><td colspan=2><b>assigned template variables:</b></td></tr>
{section name=vars loop=$_debug_keys}
<tr bgcolor={if %vars.index% is even}#eeeeee{else}#fafafa{/if}><td valign=top><tt><font color=blue>{ldelim}${$_debug_keys[vars]}{rdelim}</font></tt></td><td nowrap><tt><font color=green>{$_debug_vals[vars]|@debug_print_var}</font></tt></td></tr>
{sectionelse}
<tr bgcolor=#eeeeee><td colspan=2><tt><i>no template variables assigned</i></tt></td></tr>
{/section}
<tr bgcolor=#cccccc><td colspan=2><b>assigned config file variables (outter template scope):</b></td></tr>
{section name=config_vars loop=$_debug_config_keys}
<tr bgcolor={if %config_vars.index% is even}#eeeeee{else}#fafafa{/if}><td valign=top><tt><font color=maroon>{ldelim}#{$_debug_config_keys[config_vars]}#{rdelim}</font></tt></td><td><tt><font color=green>{$_debug_config_vals[config_vars]|@debug_print_var}</font></tt></td></tr>
{sectionelse}
<tr bgcolor=#eeeeee><td colspan=2><tt><i>no config vars assigned</i></tt></td></tr>
{/section}
</table>
</BODY></HTML>
{else}
<SCRIPT language=javascript>
if( self.name == '' ) {ldelim}
var title = 'Console';
@@ -37,3 +62,4 @@
_smarty_console.document.write("</BODY></HTML>");
_smarty_console.document.close();
</SCRIPT>
{/if}

View File

@@ -0,0 +1,42 @@
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: function
* Name: cycle
* Version: 1.3
* Date: May 3, 2002
* Author: Monte Ohrt <monte@ispi.net>
* Credits: Mark Priatel <mpriatel@rogers.com>
* Gerard <gerard@interfold.com>
* Jason Sweat <jsweat_php@yahoo.com>
* Purpose: cycle through given values
* Input: name = name of cycle (optional)
* values = comma separated list of values to cycle,
* or an array of values to cycle
* (this can be left out for subsequent calls)
*
* reset = boolean - resets given var to true
* print = boolean - print var or not. default is true
* advance = boolean - whether or not to advance the cycle
* delimiter = the value delimiter, default is ","
* assign = boolean, assigns to template var instead of
* printed.
*
* Examples: {cycle values="#eeeeee,#d0d0d0d"}
* {cycle name=row values="one,two,three" reset=true}
* {cycle name=row}
* -------------------------------------------------------------
*/
function smarty_function_debug($params, &$smarty)
{
if($params['output']) {
$smarty->assign('_smarty_debug_output',$params['output']);
}
echo $smarty->_generate_debug_output();
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,42 @@
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: function
* Name: cycle
* Version: 1.3
* Date: May 3, 2002
* Author: Monte Ohrt <monte@ispi.net>
* Credits: Mark Priatel <mpriatel@rogers.com>
* Gerard <gerard@interfold.com>
* Jason Sweat <jsweat_php@yahoo.com>
* Purpose: cycle through given values
* Input: name = name of cycle (optional)
* values = comma separated list of values to cycle,
* or an array of values to cycle
* (this can be left out for subsequent calls)
*
* reset = boolean - resets given var to true
* print = boolean - print var or not. default is true
* advance = boolean - whether or not to advance the cycle
* delimiter = the value delimiter, default is ","
* assign = boolean, assigns to template var instead of
* printed.
*
* Examples: {cycle values="#eeeeee,#d0d0d0d"}
* {cycle name=row values="one,two,three" reset=true}
* {cycle name=row}
* -------------------------------------------------------------
*/
function smarty_function_debug($params, &$smarty)
{
if($params['output']) {
$smarty->assign('_smarty_debug_output',$params['output']);
}
echo $smarty->_generate_debug_output();
}
/* vim: set expandtab: */
?>