commit cache handler functionality

This commit is contained in:
mohrt
2001-11-26 22:44:23 +00:00
parent d8a95b1009
commit 65e56100db
8 changed files with 252 additions and 173 deletions

View File

@@ -78,7 +78,7 @@ class Config_File extends PEAR {
{ {
$this->PEAR(); $this->PEAR();
if (substr(PHP_OS, 1, 3) == "WIN") if (substr(PHP_OS, 1, 3) == "WIN" || substr(PHP_OS, 1, 4) == "OS/2")
$this->_separator = "\\"; $this->_separator = "\\";
else else
$this->_separator = "/"; $this->_separator = "/";

7
NEWS
View File

@@ -2,6 +2,13 @@
blocks. (Andrei, Alexander Belonosov) blocks. (Andrei, Alexander Belonosov)
- added 'field_array' attribute to html_select_time function. (Andrei, - added 'field_array' attribute to html_select_time function. (Andrei,
Michael Caplan) Michael Caplan)
- added {section} "max" attribute to docs (Monte)
- fixed notice message in Smarty_Compiler.class.php (Monte)
- fixed bug with clear_cache introduced in 1.4.6, third parameter should be
null (Monte)
- updated Config_File.class.php for "\" support in OS/2 (Monte, Francesco
Ciprianii)
- removed secure_ext setting (not used) (Monte)
- made cache reading process more efficient. (Monte) - made cache reading process more efficient. (Monte)
- fixed bug, is_cached() now supports new 1.4.6 caching behavior. (Monte) - fixed bug, is_cached() now supports new 1.4.6 caching behavior. (Monte)
- update FAQ with mailing list Reply-To header FAQ. (Monte) - update FAQ with mailing list Reply-To header FAQ. (Monte)

View File

@@ -101,8 +101,10 @@ class Smarty
// this will tell Smarty not to look for // this will tell Smarty not to look for
// insert tags and speed up cached page // insert tags and speed up cached page
// fetches. // fetches.
var $cache_handler_func = ''; // function used for cached content. this is
// an alternative to using the file based $cache_dir.
var $tpl_file_ext = '.tpl'; // template file extention var $tpl_file_ext = '.tpl'; // template file extention (deprecated)
var $php_handling = SMARTY_PHP_PASSTHRU; var $php_handling = SMARTY_PHP_PASSTHRU;
// how smarty handles php tags in the templates // how smarty handles php tags in the templates
@@ -116,7 +118,6 @@ class Smarty
var $security = false; // enable template security (default false) var $security = false; // enable template security (default false)
var $secure_dir = array('./templates'); // array of directories considered secure var $secure_dir = array('./templates'); // array of directories considered secure
var $secure_ext = array('.tpl'); // array of file extentions considered secure
var $security_settings = array( var $security_settings = array(
'PHP_HANDLING' => false, 'PHP_HANDLING' => false,
'IF_FUNCS' => array('array', 'list', 'IF_FUNCS' => array('array', 'list',
@@ -413,9 +414,13 @@ class Smarty
Function: clear_cache() Function: clear_cache()
Purpose: clear cached content for the given template and cache id Purpose: clear cached content for the given template and cache id
\*======================================================================*/ \*======================================================================*/
function clear_cache($tpl_file = null, $cache_id = null, $compile_id) function clear_cache($tpl_file = null, $cache_id = null, $compile_id = null)
{ {
return $this->_rm_auto($this->cache_dir, $tpl_file, $compile_id . $cache_id); if(!empty($this->cache_handler_func) {
return $$this->cache_handler_func('clear', $tpl_file, $cache_id, $compile_id);
} else {
return $this->_rm_auto($this->cache_dir, $tpl_file, $compile_id . $cache_id);
}
} }
@@ -425,7 +430,11 @@ class Smarty
\*======================================================================*/ \*======================================================================*/
function clear_all_cache() function clear_all_cache()
{ {
return $this->_rm_auto($this->cache_dir); if(!empty($this->cache_handler_func) {
return $$this->cache_handler_func('clear');
} else {
return $this->_rm_auto($this->cache_dir);
}
} }
@@ -438,20 +447,7 @@ class Smarty
if (!$this->caching) if (!$this->caching)
return false; return false;
$cache_file = $this->_get_auto_filename($this->cache_dir, $tpl_file, $compile_id . $cache_id); return $this->_read_cache_file($tpl_file,$cache_id,$compile_id,$results);
if (file_exists($cache_file) &&
($this->cache_lifetime == 0 ||
(time() - filemtime($cache_file) <= $this->cache_lifetime))) {
if($this->compile_check) {
return $this->_read_cache_file($cache_file,$results);
} else {
return true;
}
}
else
return false;
} }
@@ -524,29 +520,22 @@ class Smarty
$this->_cache_info[] = array('template', $tpl_file); $this->_cache_info[] = array('template', $tpl_file);
$cache_file = $this->_get_auto_filename($this->cache_dir, $tpl_file, $compile_id . $cache_id); if($this->_read_cache_file($tpl_file,$cache_id,$compile_id,$results)) {
if ($this->insert_tag_check) {
$results = $this->_process_cached_inserts($results);
}
if ($display) {
echo $results;
if ($this->debugging)
{
// capture time for debugging info
$this->_smarty_debug_info[$included_tpls_idx]['exec_time'] = $this->_get_microtime() - $debug_start_time;
if (file_exists($cache_file) && echo $this->_generate_debug_output();
($this->cache_lifetime == 0 ||
(time() - filemtime($cache_file) <= $this->cache_lifetime))) {
if($this->_read_cache_file($cache_file,$results)) {
if ($this->insert_tag_check) {
$results = $this->_process_cached_inserts($results);
}
if ($display) {
echo $results;
if ($this->debugging)
{
// capture time for debugging info
$this->_smarty_debug_info[$included_tpls_idx]['exec_time'] = $this->_get_microtime() - $debug_start_time;
echo $this->_generate_debug_output();
}
return;
} else {
return $results;
} }
return;
} else {
return $results;
} }
} }
} }
@@ -607,7 +596,7 @@ class Smarty
} }
if ($this->caching) { if ($this->caching) {
$this->_write_cache_file($cache_file, $results); $this->_write_cache_file($tpl_file,$cache_id,$compile_id,$results);
$results = $this->_process_cached_inserts($results); $results = $this->_process_cached_inserts($results);
} }
@@ -869,7 +858,6 @@ function _generate_debug_output() {
$smarty_compiler->compiler_funcs = $this->compiler_funcs; $smarty_compiler->compiler_funcs = $this->compiler_funcs;
$smarty_compiler->security = $this->security; $smarty_compiler->security = $this->security;
$smarty_compiler->secure_dir = $this->secure_dir; $smarty_compiler->secure_dir = $this->secure_dir;
$smarty_compiler->secure_ext = $this->secure_ext;
$smarty_compiler->security_settings = $this->security_settings; $smarty_compiler->security_settings = $this->security_settings;
if ($smarty_compiler->_compile_file($tpl_file, $template_source, $template_compiled)) if ($smarty_compiler->_compile_file($tpl_file, $template_source, $template_compiled))
@@ -1233,62 +1221,91 @@ function _run_insert_handler($args)
/*======================================================================*\ /*======================================================================*\
Function: _write_cache_file Function: _write_cache_file
Purpose: Prepend the cache information to the cache file Purpose: Prepend the cache information to the cache file
and write it to disk and write it
\*======================================================================*/ \*======================================================================*/
function _write_cache_file($cache_file,$results) function _write_cache_file($tpl_file, $cache_id, $compile_id, $results)
{ {
// put the templates involved with this cache in the first line // put timestamp in cache header
$cache_info = 'SMARTY_CACHE_INFO_HEADER'.serialize($this->_cache_info)."\n"; $this->_cache_info['timestamp'] = time();
$this->_write_file($cache_file, $cache_info.$results, true);
return true; // prepend the cache header info into cache file
$results = 'SMARTY_CACHE_INFO_HEADER'.serialize($this->_cache_info)."\n".$results;
if(!empty($this->cache_handler_func)) {
// use cache_write_handler function
return $$this->cache_handler_func('write', $tpl_file, $cache_id, $compile_id, $results, $this);
} else {
// use local cache file
$cache_file = $this->_get_auto_filename($this->cache_dir, $tpl_file, $compile_id . $cache_id);
$this->_write_file($cache_file, $results, true);
return true;
}
} }
/*======================================================================*\ /*======================================================================*\
Function: _read_cache_file Function: _read_cache_file
Purpose: See if any of the templates for this cache file Purpose: read a cache file, determine if it needs to be
have changed or not since the cache was created. regenerated or not
Remove the cache info from the cache results.
\*======================================================================*/ \*======================================================================*/
function _read_cache_file($cache_file,&$results) function _read_cache_file($tpl_file, $cache_id, $compile_id, &$results)
{ {
if( !($cache_header = $this->_read_file($cache_file,1,1) )) { if ($this->force_compile || $this->cache_lifetime == 0) {
// force compile enabled or cache lifetime is zero, always regenerate
return false; return false;
} }
if(!empty($this->cache_handler_func)) {
// use cache_read_handler function
return($$this->cache_handler_func('read',$tpl_file, $cache_id, $compile_id, $results, $this));
} else {
// use local file cache
$cache_file = $this->_get_auto_filename($this->cache_dir, $tpl_file, $compile_id . $cache_id);
return ($results = $this->_read_file($cache_file));
}
$cache_split = explode("\n",$results,2);
$cache_header = $cache_split[0];
if (substr($cache_header, 0, 24) == 'SMARTY_CACHE_INFO_HEADER') { if (substr($cache_header, 0, 24) == 'SMARTY_CACHE_INFO_HEADER') {
$cache_info = unserialize(substr($cache_header, 24)); $cache_info = unserialize(substr($cache_header, 24));
$cache_timestamp = $cache_info['timestamp'];
if (time() - $cache_timestamp > $this->cache_lifetime) {
// cache expired, regenerate
return false;
}
if ($this->compile_check) { if ($this->compile_check) {
$cache_filemtime = filemtime($cache_file);
foreach ($cache_info as $curr_cache_info) { foreach ($cache_info as $curr_cache_info) {
switch ($curr_cache_info[0]) { switch ($curr_cache_info[0]) {
case 'template': case 'template':
$this->_fetch_template_info($curr_cache_info[1], $template_source, $template_timestamp, false); $this->_fetch_template_info($curr_cache_info[1], $template_source, $template_timestamp, false);
if($cache_filemtime < $template_timestamp) { if($cache_timestamp < $template_timestamp) {
// template file has changed, regenerate cache // template file has changed, regenerate cache
return false; return false;
} }
break; break;
case 'config': case 'config':
if ($cache_filemtime < filemtime($this->config_dir.'/'.$curr_cache_info[1])) { if ($cache_timestamp < filemtime($this->config_dir.'/'.$curr_cache_info[1])) {
// config file file has changed, regenerate cache // config file file has changed, regenerate cache
return false; return false;
} }
break; break;
} }
} }
} }
$results = $this->_read_file($cache_file,2); $results = $cache_split[1];
return true;
} else { } else {
// no cache info header, pre Smarty 1.4.6 format // no cache info header, pre Smarty 1.4.6 format. regenerate
$results = $this->_read_file($cache_file); return false;
} }
return true;
} }

View File

@@ -374,7 +374,7 @@ class Smarty_Compiler extends Smarty {
$attrs['section'] = 'null'; $attrs['section'] = 'null';
} }
@$scope = $this->_dequote($attrs['scope']); $scope = @$this->_dequote($attrs['scope']);
if (!empty($scope)) { if (!empty($scope)) {
if ($scope != 'local' && if ($scope != 'local' &&
$scope != 'parent' && $scope != 'parent' &&

124
docs.sgml
View File

@@ -40,9 +40,9 @@
between programming/HTML a couple of times. Thus, it's important to between programming/HTML a couple of times. Thus, it's important to
have good template support because programmers don't want anything to have good template support because programmers don't want anything to
do with HTML and don't want HTML designers mucking around with PHP do with HTML and don't want HTML designers mucking around with PHP
code. Designers need support for config files, dynamic blocks and other code. Designers need support for config files, dynamic blocks and
stuff, but they don't want to have to deal with intricacies of the PHP other interface issues, but they don't want to have to deal with
programming language. intricacies of the PHP programming language.
</para> </para>
<para> <para>
Looking at many templating solutions available for PHP today, most of Looking at many templating solutions available for PHP today, most of
@@ -89,10 +89,12 @@
<title>What is Smarty?</title> <title>What is Smarty?</title>
<para> <para>
Smarty is a template engine for PHP. One of the unique aspects about Smarty is a template engine for PHP. One of the unique aspects about
Smarty that sets it apart from other templating solutions is that it Smarty is that it compiles the template files into native PHP scripts
compiles the templates into native PHP scripts upon the first upon the first invocation. After that, it just executes the compiled
invocation. After that, it merely executes the compiled PHP scripts. PHP scripts. Therefore, there is no costly template file parsing for
Therefore, there is no costly template file parsing for each request. each request, and each template can take full advantage of PHP compiler
cache solutions such as Zend Cache (http://www.zend.com) or APC
(http://apc.communityconnect.com).
</para> </para>
<para> <para>
Some of Smarty's features: Some of Smarty's features:
@@ -108,9 +110,9 @@
functions</link> and custom <link linkend="variable.modifiers">variable functions</link> and custom <link linkend="variable.modifiers">variable
modifiers</link>, so the template language is extremely extensible.</para></listitem> modifiers</link>, so the template language is extremely extensible.</para></listitem>
<listitem><para>Configurable template delimiter tag syntax, so you can use <listitem><para>Configurable template delimiter tag syntax, so you can use
{}, {{}}, &lt;!--{}--&gt;, or whatever you like.</para></listitem> {}, {{}}, &lt;!--{}--&gt;, etc.</para></listitem>
<listitem><para>The if/elseif/else/endif constructs are passed to the <listitem><para>The if/elseif/else/endif constructs are passed to the
PHP parser, so the if expression syntax can be as simple or as complex PHP parser, so the {if ...} expression syntax can be as simple or as complex
as you like.</para></listitem> as you like.</para></listitem>
<listitem><para>Unlimited nesting of sections, ifs, etc. allowed.</para></listitem> <listitem><para>Unlimited nesting of sections, ifs, etc. allowed.</para></listitem>
<listitem><para>It is possible to embed PHP code right in your template files, <listitem><para>It is possible to embed PHP code right in your template files,
@@ -118,6 +120,7 @@
since the engine is so customizable.</para></listitem> since the engine is so customizable.</para></listitem>
<listitem><para>Built-in caching support (new in 1.3.0)</para></listitem> <listitem><para>Built-in caching support (new in 1.3.0)</para></listitem>
<listitem><para>Arbitrary template sources (new in 1.4.0)</para></listitem> <listitem><para>Arbitrary template sources (new in 1.4.0)</para></listitem>
<listitem><para>Custom cache handling functions (new in 1.4.7)</para></listitem>
</itemizedlist> </itemizedlist>
</sect1> </sect1>
<sect1> <sect1>
@@ -148,12 +151,13 @@
<para> <para>
TECHNICAL NOTE: Any time you change a template, change values in TECHNICAL NOTE: Any time you change a template, change values in
config files or change the content that gets displayed in a config files or change the content that gets displayed in a
template, you must turn on compile_check (new behavior in 1.4.6), template, you can turn on compile_check to regenerate the caches
clear the caches that are affected, or wait for the cache to expire that are affected, or wait for the cache to expire to see the
to see the results of the changes. You can either do this manually results of the changes. You clear caches manually by deleting files
by deleting files from the cache directory, or programatically with from the cache directory, programatically with <link
<link linkend="api.clear.cache">clear_cache</link> or <link linkend="api.clear.cache">clear_cache</link> or <link
linkend="api.clear.all.cache">clear_all_cache</link>. linkend="api.clear.all.cache">clear_all_cache</link>, or turn on
$compile_check (or $force_compile).
</para> </para>
<para> <para>
TECHNICAL NOTE: As of Smarty 1.4.6, if you have caching enabled AND TECHNICAL NOTE: As of Smarty 1.4.6, if you have caching enabled AND
@@ -163,7 +167,8 @@
since it has to check the templates and config files for since it has to check the templates and config files for
modification times. Therefore if you are not actively changing modification times. Therefore if you are not actively changing
templates or config files, it is advisable to leave compile_check templates or config files, it is advisable to leave compile_check
off. off. As of Smarty 1.4.7, enabling $force_compile will cause cache
files to always be regenerated.
</para> </para>
</sect2> </sect2>
</sect1> </sect1>
@@ -180,16 +185,16 @@
<sect1> <sect1>
<title>Installing Smarty</title> <title>Installing Smarty</title>
<para> <para>
Installing Smarty is fairly straightforward, there is just one thing Installing Smarty is fairly straightforward, there are a few things to
you must be aware of. Smarty creates PHP scripts from the templates. be aware of. Smarty creates PHP scripts from the templates. This
This usually means allowing user "nobody" (or whomever the web server usually means allowing user "nobody" (or whomever the web server runs
runs as) to have permission to write the files. Each installation of a as) to have permission to write the files. Each installation of a
Smarty application minimally needs a templates directory and a compiled Smarty application minimally needs a templates directory and a compiled
templates directory. If you use configuration files you will also need templates directory. If you use configuration files you will also need
a directory for those. By default these are named "templates", and a directory for those. By default these are named "templates",
"templates_c" and "configs" respectively. If you plan on using caching, "templates_c" and "configs" respectively. If you plan on using caching,
you will need to create a "cache" directory, also with permission to you will need to create a "cache" directory, also with permission to
write files. write files.
</para> </para>
<para> <para>
TECHNICAL NOTE: You can get around the need to allow the web server TECHNICAL NOTE: You can get around the need to allow the web server
@@ -220,6 +225,11 @@
directory yourself before hand, and change the file ownership directory yourself before hand, and change the file ownership
accordingly. See below. accordingly. See below.
</para> </para>
<para>
TECHNICAL NOTE: If you don't want to use include_path to find the
Smarty files, you can set the SMARTY_DIR constant to the full path to
your Smarty library files. Be sure the path ends with a slash!
</para>
<example> <example>
<title>Example of installing Smarty</title> <title>Example of installing Smarty</title>
<programlisting> <programlisting>
@@ -392,7 +402,9 @@ require_once(SMARTY_DIR."Smarty.class.php");
$compile_check to "false" to improve performance! Note that if $compile_check to "false" to improve performance! Note that if
you change this to "false" and a template file is changed, you you change this to "false" and a template file is changed, you
will *not* see the change since the template will not get will *not* see the change since the template will not get
recompiled. See <link recompiled. If caching is enabled and compile_check is enabled,
then the cache files will get regenerated if an involved
template file was updated. See <link
linkend="setting.force.compile">$force_compile</link> or <link linkend="setting.force.compile">$force_compile</link> or <link
linkend="api.clear.compiled.tpl">clear_compiled_tpl</link>. linkend="api.clear.compiled.tpl">clear_compiled_tpl</link>.
</para> </para>
@@ -403,19 +415,27 @@ require_once(SMARTY_DIR."Smarty.class.php");
This forces Smarty to (re)compile templates on every This forces Smarty to (re)compile templates on every
invocation. This setting overrides $compile_check. By default invocation. This setting overrides $compile_check. By default
this is disabled. This is handy for development and debugging. this is disabled. This is handy for development and debugging.
It should never be used in a production environment. It should never be used in a production environment. If caching
is enabled, the cache file(s) will be regenerated every time.
</para> </para>
</sect2> </sect2>
<sect2 id="setting.caching"> <sect2 id="setting.caching">
<title>$caching</title> <title>$caching</title>
<para> <para>
This tells Smarty whether or not to cache the output of the This tells Smarty whether or not to cache the output of the
templates. By default this is set to false. If your templates templates. By default this is set to false. If your templates
generate the same content over and over, it is advisable to generate redundant redundant content over and over again and
turn on caching. This will result significant performance again repeatedly, it is advisable to turn on caching. This will
gains. You can also have multiple caches for the same template. result in significant performance gains. You can also have
See <link linkend="api.is.cached">is_cached</link> for details. multiple caches for the same template. See <link
This was added to Smarty 1.3.0. linkend="api.is.cached">is_cached</link> for details. This was
added to Smarty 1.3.0.
</para>
<para>
If $compile_check is enabled, the cached content will be
regenerated if any of the involved templates are changed. (new
to 1.4.6). If $force_compile is enabled, the cached content
will always be regenerated (new to 1.4.7).
</para> </para>
</sect2> </sect2>
<sect2 id="setting.cache.dir"> <sect2 id="setting.cache.dir">
@@ -425,6 +445,8 @@ require_once(SMARTY_DIR."Smarty.class.php");
stored. By default this is "./cache", meaning that it will stored. By default this is "./cache", meaning that it will
look for the cache directory in the same directory as the look for the cache directory in the same directory as the
executing php script. This was added to Smarty version 1.3.0. executing php script. This was added to Smarty version 1.3.0.
You can also use your own custom cache handler function to
control cache files, which will ignore this setting.
</para> </para>
<para> <para>
TECHNICAL NOTE: This setting must be either a relative or TECHNICAL NOTE: This setting must be either a relative or
@@ -455,6 +477,14 @@ require_once(SMARTY_DIR."Smarty.class.php");
search, speeding up cached page fetches. search, speeding up cached page fetches.
</para> </para>
</sect2> </sect2>
<sect2 id="setting.cache.handler.func">
<title>$cache_handler_func</title>
<para>
You can supply a custom function to handle cache files instead
of using the built-in method using the $cache_dir. See the
custom cache handler function section for details.
</para>
</sect2>
<sect2 id="setting.tpl.file.ext"> <sect2 id="setting.tpl.file.ext">
<title>$tpl_file_ext</title> <title>$tpl_file_ext</title>
<para> <para>
@@ -470,12 +500,12 @@ require_once(SMARTY_DIR."Smarty.class.php");
<sect2 id="setting.allow.php"> <sect2 id="setting.allow.php">
<title>$allow_php</title> <title>$allow_php</title>
<para> <para>
Whether or not to allow PHP code in the templates. If set to Whether or not to allow PHP code in the templates. If set to
false, PHP code is escaped and not interpreted. Embedding PHP false, PHP code is escaped and not interpreted. Embedding PHP
code into templates is highly discouraged. Use <link code into templates is not recommended. Use <link
linkend="custom.functions">custom functions</link> or <link linkend="custom.functions">custom functions</link> or <link
linkend="variable.modifiers">modifiers</link> instead. Default linkend="variable.modifiers">modifiers</link> instead. Default
is "false". is "false".
</para> </para>
<para> <para>
NOTE: $allow_php was removed in 1.3.0, and replaced with NOTE: $allow_php was removed in 1.3.0, and replaced with
@@ -1221,7 +1251,7 @@ var_dump($tpl_vars);
</funcsynopsis> </funcsynopsis>
<para> <para>
This displays the template. Supply a valid <link This displays the template. Supply a valid <link
linkend="section.template.resources"> template resource</link> linkend="section.template.resources">template resource</link>
type and path. As an optional second parameter, you can pass a type and path. As an optional second parameter, you can pass a
cache id. See the <link linkend="section.caching">caching cache id. See the <link linkend="section.caching">caching
section</link> for more information. section</link> for more information.
@@ -1508,7 +1538,7 @@ function add_header_comment($tpl_source) {
} }
// register the postfilter // register the postfilter
$smarty->register_postfilter("add_header_comments"); $smarty->register_postfilter("add_header_comment");
$smarty->display("index.tpl"); $smarty->display("index.tpl");
{* from within Smarty template *} {* from within Smarty template *}
@@ -2441,6 +2471,14 @@ OUTPUT:
0,2,4, etc. If step is negative, it will step through 0,2,4, etc. If step is negative, it will step through
the array backwards. (Added to Smarty 1.4.4.)</entry> the array backwards. (Added to Smarty 1.4.4.)</entry>
</row> </row>
<row>
<entry>max</entry>
<entry>integer</entry>
<entry>No</entry>
<entry><emphasis>1</emphasis></entry>
<entry>Sets the maximum number of times the section
will loop. (Added to Smarty 1.4.4.)</entry>
</row>
<row> <row>
<entry>show</entry> <entry>show</entry>
<entry>boolean</entry> <entry>boolean</entry>

View File

@@ -78,7 +78,7 @@ class Config_File extends PEAR {
{ {
$this->PEAR(); $this->PEAR();
if (substr(PHP_OS, 1, 3) == "WIN") if (substr(PHP_OS, 1, 3) == "WIN" || substr(PHP_OS, 1, 4) == "OS/2")
$this->_separator = "\\"; $this->_separator = "\\";
else else
$this->_separator = "/"; $this->_separator = "/";

View File

@@ -101,8 +101,10 @@ class Smarty
// this will tell Smarty not to look for // this will tell Smarty not to look for
// insert tags and speed up cached page // insert tags and speed up cached page
// fetches. // fetches.
var $cache_handler_func = ''; // function used for cached content. this is
// an alternative to using the file based $cache_dir.
var $tpl_file_ext = '.tpl'; // template file extention var $tpl_file_ext = '.tpl'; // template file extention (deprecated)
var $php_handling = SMARTY_PHP_PASSTHRU; var $php_handling = SMARTY_PHP_PASSTHRU;
// how smarty handles php tags in the templates // how smarty handles php tags in the templates
@@ -116,7 +118,6 @@ class Smarty
var $security = false; // enable template security (default false) var $security = false; // enable template security (default false)
var $secure_dir = array('./templates'); // array of directories considered secure var $secure_dir = array('./templates'); // array of directories considered secure
var $secure_ext = array('.tpl'); // array of file extentions considered secure
var $security_settings = array( var $security_settings = array(
'PHP_HANDLING' => false, 'PHP_HANDLING' => false,
'IF_FUNCS' => array('array', 'list', 'IF_FUNCS' => array('array', 'list',
@@ -413,9 +414,13 @@ class Smarty
Function: clear_cache() Function: clear_cache()
Purpose: clear cached content for the given template and cache id Purpose: clear cached content for the given template and cache id
\*======================================================================*/ \*======================================================================*/
function clear_cache($tpl_file = null, $cache_id = null, $compile_id) function clear_cache($tpl_file = null, $cache_id = null, $compile_id = null)
{ {
return $this->_rm_auto($this->cache_dir, $tpl_file, $compile_id . $cache_id); if(!empty($this->cache_handler_func) {
return $$this->cache_handler_func('clear', $tpl_file, $cache_id, $compile_id);
} else {
return $this->_rm_auto($this->cache_dir, $tpl_file, $compile_id . $cache_id);
}
} }
@@ -425,7 +430,11 @@ class Smarty
\*======================================================================*/ \*======================================================================*/
function clear_all_cache() function clear_all_cache()
{ {
return $this->_rm_auto($this->cache_dir); if(!empty($this->cache_handler_func) {
return $$this->cache_handler_func('clear');
} else {
return $this->_rm_auto($this->cache_dir);
}
} }
@@ -438,20 +447,7 @@ class Smarty
if (!$this->caching) if (!$this->caching)
return false; return false;
$cache_file = $this->_get_auto_filename($this->cache_dir, $tpl_file, $compile_id . $cache_id); return $this->_read_cache_file($tpl_file,$cache_id,$compile_id,$results);
if (file_exists($cache_file) &&
($this->cache_lifetime == 0 ||
(time() - filemtime($cache_file) <= $this->cache_lifetime))) {
if($this->compile_check) {
return $this->_read_cache_file($cache_file,$results);
} else {
return true;
}
}
else
return false;
} }
@@ -524,29 +520,22 @@ class Smarty
$this->_cache_info[] = array('template', $tpl_file); $this->_cache_info[] = array('template', $tpl_file);
$cache_file = $this->_get_auto_filename($this->cache_dir, $tpl_file, $compile_id . $cache_id); if($this->_read_cache_file($tpl_file,$cache_id,$compile_id,$results)) {
if ($this->insert_tag_check) {
$results = $this->_process_cached_inserts($results);
}
if ($display) {
echo $results;
if ($this->debugging)
{
// capture time for debugging info
$this->_smarty_debug_info[$included_tpls_idx]['exec_time'] = $this->_get_microtime() - $debug_start_time;
if (file_exists($cache_file) && echo $this->_generate_debug_output();
($this->cache_lifetime == 0 ||
(time() - filemtime($cache_file) <= $this->cache_lifetime))) {
if($this->_read_cache_file($cache_file,$results)) {
if ($this->insert_tag_check) {
$results = $this->_process_cached_inserts($results);
}
if ($display) {
echo $results;
if ($this->debugging)
{
// capture time for debugging info
$this->_smarty_debug_info[$included_tpls_idx]['exec_time'] = $this->_get_microtime() - $debug_start_time;
echo $this->_generate_debug_output();
}
return;
} else {
return $results;
} }
return;
} else {
return $results;
} }
} }
} }
@@ -607,7 +596,7 @@ class Smarty
} }
if ($this->caching) { if ($this->caching) {
$this->_write_cache_file($cache_file, $results); $this->_write_cache_file($tpl_file,$cache_id,$compile_id,$results);
$results = $this->_process_cached_inserts($results); $results = $this->_process_cached_inserts($results);
} }
@@ -869,7 +858,6 @@ function _generate_debug_output() {
$smarty_compiler->compiler_funcs = $this->compiler_funcs; $smarty_compiler->compiler_funcs = $this->compiler_funcs;
$smarty_compiler->security = $this->security; $smarty_compiler->security = $this->security;
$smarty_compiler->secure_dir = $this->secure_dir; $smarty_compiler->secure_dir = $this->secure_dir;
$smarty_compiler->secure_ext = $this->secure_ext;
$smarty_compiler->security_settings = $this->security_settings; $smarty_compiler->security_settings = $this->security_settings;
if ($smarty_compiler->_compile_file($tpl_file, $template_source, $template_compiled)) if ($smarty_compiler->_compile_file($tpl_file, $template_source, $template_compiled))
@@ -1233,62 +1221,91 @@ function _run_insert_handler($args)
/*======================================================================*\ /*======================================================================*\
Function: _write_cache_file Function: _write_cache_file
Purpose: Prepend the cache information to the cache file Purpose: Prepend the cache information to the cache file
and write it to disk and write it
\*======================================================================*/ \*======================================================================*/
function _write_cache_file($cache_file,$results) function _write_cache_file($tpl_file, $cache_id, $compile_id, $results)
{ {
// put the templates involved with this cache in the first line // put timestamp in cache header
$cache_info = 'SMARTY_CACHE_INFO_HEADER'.serialize($this->_cache_info)."\n"; $this->_cache_info['timestamp'] = time();
$this->_write_file($cache_file, $cache_info.$results, true);
return true; // prepend the cache header info into cache file
$results = 'SMARTY_CACHE_INFO_HEADER'.serialize($this->_cache_info)."\n".$results;
if(!empty($this->cache_handler_func)) {
// use cache_write_handler function
return $$this->cache_handler_func('write', $tpl_file, $cache_id, $compile_id, $results, $this);
} else {
// use local cache file
$cache_file = $this->_get_auto_filename($this->cache_dir, $tpl_file, $compile_id . $cache_id);
$this->_write_file($cache_file, $results, true);
return true;
}
} }
/*======================================================================*\ /*======================================================================*\
Function: _read_cache_file Function: _read_cache_file
Purpose: See if any of the templates for this cache file Purpose: read a cache file, determine if it needs to be
have changed or not since the cache was created. regenerated or not
Remove the cache info from the cache results.
\*======================================================================*/ \*======================================================================*/
function _read_cache_file($cache_file,&$results) function _read_cache_file($tpl_file, $cache_id, $compile_id, &$results)
{ {
if( !($cache_header = $this->_read_file($cache_file,1,1) )) { if ($this->force_compile || $this->cache_lifetime == 0) {
// force compile enabled or cache lifetime is zero, always regenerate
return false; return false;
} }
if(!empty($this->cache_handler_func)) {
// use cache_read_handler function
return($$this->cache_handler_func('read',$tpl_file, $cache_id, $compile_id, $results, $this));
} else {
// use local file cache
$cache_file = $this->_get_auto_filename($this->cache_dir, $tpl_file, $compile_id . $cache_id);
return ($results = $this->_read_file($cache_file));
}
$cache_split = explode("\n",$results,2);
$cache_header = $cache_split[0];
if (substr($cache_header, 0, 24) == 'SMARTY_CACHE_INFO_HEADER') { if (substr($cache_header, 0, 24) == 'SMARTY_CACHE_INFO_HEADER') {
$cache_info = unserialize(substr($cache_header, 24)); $cache_info = unserialize(substr($cache_header, 24));
$cache_timestamp = $cache_info['timestamp'];
if (time() - $cache_timestamp > $this->cache_lifetime) {
// cache expired, regenerate
return false;
}
if ($this->compile_check) { if ($this->compile_check) {
$cache_filemtime = filemtime($cache_file);
foreach ($cache_info as $curr_cache_info) { foreach ($cache_info as $curr_cache_info) {
switch ($curr_cache_info[0]) { switch ($curr_cache_info[0]) {
case 'template': case 'template':
$this->_fetch_template_info($curr_cache_info[1], $template_source, $template_timestamp, false); $this->_fetch_template_info($curr_cache_info[1], $template_source, $template_timestamp, false);
if($cache_filemtime < $template_timestamp) { if($cache_timestamp < $template_timestamp) {
// template file has changed, regenerate cache // template file has changed, regenerate cache
return false; return false;
} }
break; break;
case 'config': case 'config':
if ($cache_filemtime < filemtime($this->config_dir.'/'.$curr_cache_info[1])) { if ($cache_timestamp < filemtime($this->config_dir.'/'.$curr_cache_info[1])) {
// config file file has changed, regenerate cache // config file file has changed, regenerate cache
return false; return false;
} }
break; break;
} }
} }
} }
$results = $this->_read_file($cache_file,2); $results = $cache_split[1];
return true;
} else { } else {
// no cache info header, pre Smarty 1.4.6 format // no cache info header, pre Smarty 1.4.6 format. regenerate
$results = $this->_read_file($cache_file); return false;
} }
return true;
} }

View File

@@ -374,7 +374,7 @@ class Smarty_Compiler extends Smarty {
$attrs['section'] = 'null'; $attrs['section'] = 'null';
} }
@$scope = $this->_dequote($attrs['scope']); $scope = @$this->_dequote($attrs['scope']);
if (!empty($scope)) { if (!empty($scope)) {
if ($scope != 'local' && if ($scope != 'local' &&
$scope != 'parent' && $scope != 'parent' &&