Source for file Smarty.class.php
Documentation is available at Smarty.class.php
* Project: Smarty: the PHP compiling template engine
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* For questions, help, comments, discussion, etc., please join the
* Smarty mailing list. Send a blank e-mail to
* You may contact the authors of Smarty by e-mail at:
* Director of Technology, ispi
* The latest version of Smarty can be obtained from:
* @link http://smarty.php.net/
* @copyright 2001-2003 ispi of Lincoln, Inc.
/* $Id: Smarty.class.php,v 1.1 2005/10/17 18:37:39 jeichorn Exp $ */
* DIR_SEP isn't used anymore, but third party apps might
define('DIR_SEP', DIRECTORY_SEPARATOR);
* set SMARTY_DIR to absolute path to Smarty library files.
* if not defined, include_path will be used. Sets SMARTY_DIR only if user
* application has not already defined it.
define('SMARTY_DIR', dirname(__FILE__ ) . DIRECTORY_SEPARATOR);
define('SMARTY_PHP_PASSTHRU', 0);
define('SMARTY_PHP_QUOTE', 1);
define('SMARTY_PHP_REMOVE', 2);
define('SMARTY_PHP_ALLOW', 3);
* Smarty Configuration Section
* The name of the directory where templates are located.
* The directory where compiled templates are located.
* The directory where config files are located.
* An array of directories searched for plugins.
* If debugging is enabled, a debug console window will display
* when the page loads (make sure your browser allows unrequested
* This is the path to the debug console template. If not set,
* the default one will be used.
* This determines if debugging is enable-able from the browser.
* <li>NONE => no debugging control allowed</li>
* <li>URL => enable debugging when SMARTY_DEBUG is found in the URL.</li>
* @link http://www.foo.dom/index.php?SMARTY_DEBUG
* This tells Smarty whether to check for recompiling or not. Recompiling
* does not need to happen unless a template or config file is changed.
* Typically you enable this during development, and disable for
* This forces templates to compile every time. Useful for development
* This enables template caching.
* <li>0 = no caching</li>
* <li>1 = use class cache_lifetime value</li>
* <li>2 = use cache_lifetime in cache file</li>
* The name of the directory for cache files.
* This is the number of seconds cached content will persist.
* <li>0 = always regenerate cache</li>
* <li>-1 = never expires</li>
* Only used when $caching is enabled. If true, then If-Modified-Since headers
* are respected with cached content, and appropriate HTTP headers are sent.
* This way repeated hits to a cached page do not send the entire page to the
* This determines how Smarty handles "<?php ... ?>" tags in templates.
* <li>SMARTY_PHP_PASSTHRU -> print tags as plain text</li>
* <li>SMARTY_PHP_QUOTE -> escape tags as entities</li>
* <li>SMARTY_PHP_REMOVE -> remove php tags</li>
* <li>SMARTY_PHP_ALLOW -> execute php tags</li>
* This enables template security. When enabled, many things are restricted
* in the templates that normally would go unchecked. This is useful when
* untrusted parties are editing templates and you want a reasonable level
* of security. (no direct execution of PHP in templates for example)
* This is the list of template directories that are considered secure. This
* is used only if {@link $security} is enabled. One directory per array
* element. {@link $template_dir} is in this list implicitly.
* These are the security settings for Smarty. They are used only when
* {@link $security} is enabled.
'IF_FUNCS' => array('array', 'list',
'MODIFIER_FUNCS' => array('count'),
'ALLOW_CONSTANTS' => false
* This is an array of directories where trusted php scripts reside.
* {@link $security} is disabled during their inclusion/execution.
* The left delimiter used for the template tags.
* The right delimiter used for the template tags.
* The order in which request variables are registered, similar to
* variables_order in php.ini E = Environment, G = GET, P = POST,
* C = Cookies, S = Server
* Indicates wether $HTTP_*_VARS[] (request_use_auto_globals=false)
* are uses as request-vars or $_*[]-vars. note: if
* request_use_auto_globals is true, then $request_vars_order has
* no effect, but the php-ini-value "gpc_order"
* Set this if you want different sets of compiled files for the same
* templates. This is useful for things like different languages.
* Instead of creating separate sets of templates per language, you
* set different compile_ids like 'en' and 'de'.
* This tells Smarty whether or not to use sub dirs in the cache/ and
* templates_c/ directories. sub directories better organized, but
* may not work well with PHP safe mode enabled.
* This is a list of the modifiers to apply to all template variables.
* Put each modifier in a separate array element in the order you want
* them applied. example: <code>array('escape:"htmlall"');</code>
* This is the resource type to be used when not specified
* at the beginning of the resource path. examples:
* $smarty->display('file:index.tpl');
* $smarty->display('db:index.tpl');
* $smarty->display('index.tpl'); // will use default resource type
* {include file="file:index.tpl"}
* {include file="db:index.tpl"}
* {include file="index.tpl"} {* will use default resource type *}
* The function used for cache file handling. If not set, built-in caching is used.
* @var null|stringfunction name
* These are the variables from the globals array that are
* assigned to all templates automatically. This isn't really
* necessary any more, you can use the $smarty var to access them
var $global_assign = array('HTTP_SERVER_VARS' => array('SCRIPT_NAME'));
* The value of "undefined". Leave it alone :-)
* This indicates which filters are automatically loaded into Smarty.
* @var array array of filter names
* This tells if config file vars of the same name overwrite each other or not.
* if disabled, same name variables are accumulated in an array.
* This tells whether or not to automatically booleanize config file variables.
* If enabled, then the strings "on", "true", and "yes" are treated as boolean
* true, and "off", "false" and "no" are treated as boolean false.
* This tells whether hidden sections [.foobar] are readable from the
* tempalates or not. Normally you would never allow this since that is
* the point behind hidden sections: the application can access them, but
* This tells whether or not automatically fix newlines in config files.
* It basically converts \r (mac) or \r\n (dos) to \n
* If a template cannot be found, this PHP function will be executed.
* Useful for creating templates on-the-fly or other special action.
* @var string function name
* The file that contains the compiler class. This can a full
* pathname, or relative to the php_include path.
* The class used for compiling templates.
* The class used to load config vars.
* END Smarty Configuration Section
* There should be no need to touch anything below this line.
* error messages. true/false
* where assigned template vars are kept
var $_tpl_vars = array();
* stores run-time $smarty.* vars
var $_smarty_vars = null;
* keeps track of sections
var $_sections = array();
* keeps track of foreach blocks
* keeps track of tag hierarchy
var $_tag_stack = array();
* loaded configuration settings
var $_config = array(array('vars' => array(), 'files' => array()));
* md5 checksum of the string 'Smarty'
var $_smarty_md5 = 'f8d698aea36fcbead2b9d5359ffca76f';
* current template inclusion depth
var $_inclusion_depth = 0;
* for different compiled templates
* text in URL to enable debug mode
var $_smarty_debug_id = 'SMARTY_DEBUG';
* debugging information for debug console
var $_smarty_debug_info = array();
* info that makes up a cache file
var $_cache_info = array();
* default file permissions
* default dir permissions
var $_reg_objects = array();
* table keeping track of plugins
'outputfilter' => array(),
var $_cache_serials = array();
* name of optional cache include file
var $_cache_include = null;
* indicate if the current code is used in a compiled
var $_cache_including = false;
* @uses $global_assign uses {@link assign()} to assign each corresponding
* value from $GLOBALS to the template vars
foreach ($var_name as $var) {
if (isset ($GLOBALS[$key][$var])) {
$this->assign($var, $GLOBALS[$key][$var]);
if (isset ($GLOBALS[$var_name])) {
$this->assign($var_name, $GLOBALS[$var_name]);
* assigns values to template variables
* @param array|string$tpl_var the template variable name(s)
* @param mixed $value the value to assign
function assign($tpl_var, $value = null)
foreach ($tpl_var as $key => $val) {
$this->_tpl_vars[$key] = $val;
$this->_tpl_vars[$tpl_var] = $value;
* assigns values to template variables by reference
* @param string $tpl_var the template variable name
* @param mixed $value the referenced value to assign
$this->_tpl_vars[$tpl_var] = &$value;
* appends values to template variables
* @param array|string$tpl_var the template variable name(s)
* @param mixed $value the value to append
function append($tpl_var, $value= null, $merge= false)
// $tpl_var is an array, ignore $value
foreach ($tpl_var as $_key => $_val) {
if(!@is_array($this->_tpl_vars[$_key])) {
settype($this->_tpl_vars[$_key],'array');
foreach($_val as $_mkey => $_mval) {
$this->_tpl_vars[$_key][$_mkey] = $_mval;
$this->_tpl_vars[$_key][] = $_val;
if ($tpl_var != '' && isset ($value)) {
if(!@is_array($this->_tpl_vars[$tpl_var])) {
settype($this->_tpl_vars[$tpl_var],'array');
foreach($value as $_mkey => $_mval) {
$this->_tpl_vars[$tpl_var][$_mkey] = $_mval;
$this->_tpl_vars[$tpl_var][] = $value;
* appends values to template variables by reference
* @param string $tpl_var the template variable name
* @param mixed $value the referenced value to append
if ($tpl_var != '' && isset ($value)) {
if(!@is_array($this->_tpl_vars[$tpl_var])) {
settype($this->_tpl_vars[$tpl_var],'array');
foreach($value as $_key => $_val) {
$this->_tpl_vars[$tpl_var][$_key] = &$value[$_key];
$this->_tpl_vars[$tpl_var][] = &$value;
* clear the given assigned template variable.
* @param string $tpl_var the template variable to clear
foreach ($tpl_var as $curr_var)
unset ($this->_tpl_vars[$curr_var]);
unset ($this->_tpl_vars[$tpl_var]);
* Registers custom function to be used in templates
* @param string $function the name of the template function
* @param string $function_impl the name of the PHP function to register
function register_function($function, $function_impl, $cacheable= true, $cache_attrs= null)
$this->_plugins['function'][$function] =
array($function_impl, null, null, false, $cacheable, $cache_attrs);
* Unregisters custom function
* @param string $function name of template function
unset ($this->_plugins['function'][$function]);
* Registers object to be used in templates
* @param string $object name of template object
* @param object &$object_impl the referenced PHP object to register
* @param null|array$allowed list of allowed methods (empty = all)
* @param boolean $smarty_args smarty argument format, else traditional
* @param null|array$block_functs list of methods that are block format
function register_object($object, &$object_impl, $allowed = array(), $smarty_args = true, $block_methods = array())
$this->_reg_objects[$object] =
array(&$object_impl, $allowed, $smarty_args, $block_methods);
* @param string $object name of template object
unset ($this->_reg_objects[$object]);
* Registers block function to be used in templates
* @param string $block name of template block
* @param string $block_impl PHP function to register
function register_block($block, $block_impl, $cacheable= true, $cache_attrs= null)
$this->_plugins['block'][$block] =
array($block_impl, null, null, false, $cacheable, $cache_attrs);
* Unregisters block function
* @param string $block name of template function
unset ($this->_plugins['block'][$block]);
* Registers compiler function
* @param string $function name of template function
* @param string $function_impl name of PHP function to register
$this->_plugins['compiler'][$function] =
array($function_impl, null, null, false, $cacheable);
* Unregisters compiler function
* @param string $function name of template function
unset ($this->_plugins['compiler'][$function]);
* Registers modifier to be used in templates
* @param string $modifier name of template modifier
* @param string $modifier_impl name of PHP function to register
$this->_plugins['modifier'][$modifier] =
array($modifier_impl, null, null, false);
* @param string $modifier name of template modifier
unset ($this->_plugins['modifier'][$modifier]);
* Registers a resource to fetch a template
* @param string $type name of resource
* @param array $functions array of functions to handle resource
if (count($functions)== 4) {
$this->_plugins['resource'][$type] =
array($functions, false);
} elseif (count($functions)== 5) {
$this->_plugins['resource'][$type] =
array(array(array(&$functions[0], $functions[1])
,array(&$functions[0], $functions[2])
,array(&$functions[0], $functions[3])
,array(&$functions[0], $functions[4]))
$this->trigger_error("malformed function-list for '$type' in register_resource");
* @param string $type name of resource
unset ($this->_plugins['resource'][$type]);
* Registers a prefilter function to apply
* to a template before compiling
* @param string $function name of PHP function to register
$_name = (is_array($function)) ? $function[1] : $function;
$this->_plugins['prefilter'][$_name]
= array($function, null, null, false);
* Unregisters a prefilter function
* @param string $function name of PHP function
unset ($this->_plugins['prefilter'][$function]);
* Registers a postfilter function to apply
* to a compiled template after compilation
* @param string $function name of PHP function to register
$_name = (is_array($function)) ? $function[1] : $function;
$this->_plugins['postfilter'][$_name]
= array($function, null, null, false);
* Unregisters a postfilter function
* @param string $function name of PHP function
unset ($this->_plugins['postfilter'][$function]);
* Registers an output filter function to apply
* @param string $function name of PHP function
$_name = (is_array($function)) ? $function[1] : $function;
$this->_plugins['outputfilter'][$_name]
= array($function, null, null, false);
* Unregisters an outputfilter function
* @param string $function name of PHP function
unset ($this->_plugins['outputfilter'][$function]);
* load a filter of specified type and name
* @param string $type filter type
* @param string $name filter name
$_params = array('plugins' => array(array($type . 'filter', $name, null, null, false)));
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.load_plugins.php');
if (!isset ($this->_plugins[$type . 'filter'][$name]))
$this->_plugins[$type . 'filter'][$name] = false;
* clear cached content for the given template and cache id
* @param string $tpl_file name of template file
* @param string $cache_id name of cache_id
* @param string $compile_id name of compile_id
* @param string $exp_time expiration time
function clear_cache($tpl_file = null, $cache_id = null, $compile_id = null, $exp_time = null)
array('clear', &$this, &$dummy, $tpl_file, $cache_id, $compile_id, $exp_time));
$_params = array('auto_base' => $this->cache_dir,
'auto_source' => $tpl_file,
'exp_time' => $exp_time);
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.rm_auto.php');
* clear the entire contents of cache (all templates)
* @param string $exp_time expire time
* @return boolean results of {@link smarty_core_rm_auto()}
array('clear', &$this, &$dummy, null, null, null, $exp_time));
$_params = array('auto_base' => $this->cache_dir,
'exp_time' => $exp_time);
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.rm_auto.php');
* test to see if valid cache exists for this template
* @param string $tpl_file name of template file
* @param string $cache_id
* @param string $compile_id
* @return string|falseresults of {@link _read_cache_file()}
function is_cached($tpl_file, $cache_id = null, $compile_id = null)
'compile_id' => $compile_id
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.read_cache_file.php');
* clear all the assigned template variables.
$this->_tpl_vars = array();
* clears compiled version of specified template resource,
* or all compiled template files if one is not specified.
* This function is for advanced use only, not normally needed.
* @param string $tpl_file
* @param string $compile_id
* @param string $exp_time
* @return boolean results of {@link smarty_core_rm_auto()}
if (!isset ($compile_id)) {
'auto_source' => $tpl_file,
'auto_id' => $compile_id,
'extensions' => array('.inc', '.php'));
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.rm_auto.php');
* Checks whether requested template exists.
* @param string $tpl_file
$_params = array('resource_name' => $tpl_file, 'quiet'=> true, 'get_source'=> false);
* Returns an array containing template variables
if(isset ($this->_tpl_vars[$name])) {
return $this->_tpl_vars[$name];
* Returns an array containing config variables
if(!isset ($name) && is_array($this->_config[0])) {
return $this->_config[0]['vars'];
} else if(isset ($this->_config[0]['vars'][$name])) {
return $this->_config[0]['vars'][$name];
* @param string $error_msg
* @param integer $error_type
* executes & displays the template results
* @param string $resource_name
* @param string $cache_id
* @param string $compile_id
function display($resource_name, $cache_id = null, $compile_id = null)
$this->fetch($resource_name, $cache_id, $compile_id, true);
* executes & returns or displays the template results
* @param string $resource_name
* @param string $cache_id
* @param string $compile_id
* @param boolean $display
function fetch($resource_name, $cache_id = null, $compile_id = null, $display = false)
static $_cache_info = array();
$_smarty_old_error_level = $this->debugging ? error_reporting() : error_reporting(error_reporting() & ~ E_NOTICE);
if (!$this->debugging && $this->debugging_ctrl == 'URL'
&& @strstr($GLOBALS['HTTP_SERVER_VARS']['QUERY_STRING'], $this->_smarty_debug_id)) {
// enable debugging from URL
// capture time for debugging info
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.get_microtime.php');
$this->_smarty_debug_info[] = array('type' => 'template',
'filename' => $resource_name,
$_included_tpls_idx = count($this->_smarty_debug_info) - 1;
if (!isset ($compile_id)) {
$compile_id = $this->compile_id;
$this->_compile_id = $compile_id;
$this->_inclusion_depth = 0;
// save old cache_info, initialize cache_info
$this->_cache_info = array();
'tpl_file' => $resource_name,
'compile_id' => $compile_id,
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.read_cache_file.php');
$_smarty_results = $_params['results'];
if (@count($this->_cache_info['insert_tags'])) {
$_params = array('plugins' => $this->_cache_info['insert_tags']);
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.load_plugins.php');
$_params = array('results' => $_smarty_results);
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.process_cached_inserts.php');
if (@count($this->_cache_info['cache_serials'])) {
$_params = array('results' => $_smarty_results);
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.process_compiled_include.php');
// capture time for debugging info
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.get_microtime.php');
$this->_smarty_debug_info[$_included_tpls_idx]['exec_time'] = smarty_core_get_microtime($_params, $this) - $_debug_start_time;
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.display_debug_console.php');
$_last_modified_date = @substr($GLOBALS['HTTP_SERVER_VARS']['HTTP_IF_MODIFIED_SINCE'], 0, strpos($GLOBALS['HTTP_SERVER_VARS']['HTTP_IF_MODIFIED_SINCE'], 'GMT') + 3);
$_gmt_mtime = gmdate('D, d M Y H:i:s', $this->_cache_info['timestamp']). ' GMT';
if (@count($this->_cache_info['insert_tags']) == 0
&& !$this->_cache_serials
&& $_gmt_mtime == $_last_modified_date) {
header("Status: 304 Not Modified");
header("HTTP/1.1 304 Not Modified");
header("Last-Modified: ". $_gmt_mtime);
// restore initial cache_info
$this->_cache_info = array_pop($_cache_info);
// restore initial cache_info
$this->_cache_info = array_pop($_cache_info);
$this->_cache_info['template'][$resource_name] = true;
// load filters that are marked as autoload
foreach ($_filters as $_filter) {
// if we just need to display the results, don't perform output
$_cache_including = $this->_cache_including;
$this->_cache_including = false;
if ($display && !$this->caching && count($this->_plugins['outputfilter']) == 0) {
if ($this->_is_compiled($resource_name, $_smarty_compile_path)
include($_smarty_compile_path);
if ($this->_is_compiled($resource_name, $_smarty_compile_path)
include($_smarty_compile_path);
foreach ((array) $this->_plugins['outputfilter'] as $_output_filter) {
$_params = array('tpl_file' => $resource_name,
'compile_id' => $compile_id,
'results' => $_smarty_results);
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.write_cache_file.php');
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.process_cached_inserts.php');
if ($this->_cache_serials) {
// strip nocache-tags from output
$_smarty_results = preg_replace('!(\{/?nocache\:[0-9a-f]{32}#\d+\})!s'
// restore initial cache_info
$this->_cache_info = array_pop($_cache_info);
$this->_cache_including = $_cache_including;
if (isset ($_smarty_results)) { echo $_smarty_results; }
// capture time for debugging info
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.get_microtime.php');
$this->_smarty_debug_info[$_included_tpls_idx]['exec_time'] = (smarty_core_get_microtime($_params, $this) - $_debug_start_time);
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.display_debug_console.php');
if (isset ($_smarty_results)) { return $_smarty_results; }
* load configuration values
function config_load($file, $section = null, $scope = 'global')
* return a reference to a registered object
if (!isset ($this->_reg_objects[$name]))
if (!is_object($this->_reg_objects[$name][0]))
return $this->_reg_objects[$name][0];
* clear configuration values
$this->_config = array(array('vars' => array(),
unset ($this->_config[0]['vars'][$var]);
* get filepath of requested plugin
$_params = array('type' => $type, 'name' => $name);
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.assemble_plugin_filepath.php');
* test if resource needs compiling
* @param string $resource_name
* @param string $compile_path
// no need to check compiled file
// get file source and timestamp
$_params = array('resource_name' => $resource_name, 'get_source'=> false);
if ($_params['resource_timestamp'] <= filemtime($compile_path)) {
// template not expired, no recompile
// compiled template does not exist, or forced compile
* @param string $resource_name
* @param string $compile_path
$_params = array('resource_name' => $resource_name);
$_source_content = $_params['source_content'];
$_resource_timestamp = $_params['resource_timestamp'];
$_cache_include = substr($compile_path, 0, - 4). '.inc';
if ($this->_compile_source($resource_name, $_source_content, $_compiled_content, $_cache_include)) {
// if a _cache_serial was set, we also have to write an include-file:
if ($this->_cache_include_info) {
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.write_compiled_include.php');
$_params = array('compile_path'=> $compile_path, 'compiled_content' => $_compiled_content, 'resource_timestamp' => $_resource_timestamp);
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.write_compiled_resource.php');
* compile the given source
* @param string $resource_name
* @param string $source_content
* @param string $compiled_content
function _compile_source($resource_name, &$source_content, &$compiled_content, $cache_include_path= null)
$smarty_compiler->_version = $this->_version;
$smarty_compiler->_reg_objects = &$this->_reg_objects;
$smarty_compiler->_plugins = &$this->_plugins;
$smarty_compiler->_tpl_vars = &$this->_tpl_vars;
$smarty_compiler->compile_id = $this->_compile_id;
$smarty_compiler->_config = $this->_config;
$smarty_compiler->_cache_serial = null;
$smarty_compiler->_cache_include = $cache_include_path;
$_results = $smarty_compiler->_compile_file($resource_name, $source_content, $compiled_content);
if ($smarty_compiler->_cache_serial) {
$this->_cache_include_info = array(
'cache_serial'=> $smarty_compiler->_cache_serial
,'plugins_code'=> $smarty_compiler->_plugins_code
,'include_file_path' => $cache_include_path);
$this->_cache_include_info = null;
* Get the compile path for this resource
* @param string $resource_name
* @return string results of {@link _get_auto_filename()}
$this->_compile_id) . '.php';
* fetch the template info. Gets timestamp, and source
* sets $source_content to the source of the template, and
* $resource_timestamp to its time stamp
* @param string $resource_name
* @param string $source_content
* @param integer $resource_timestamp
* @param boolean $get_source
if(!isset ($params['get_source'])) { $params['get_source'] = true; }
if(!isset ($params['quiet'])) { $params['quiet'] = false; }
$_params = array('resource_name' => $params['resource_name']) ;
if (isset ($params['resource_base_path']))
$_params['resource_base_path'] = $params['resource_base_path'];
$_resource_type = $_params['resource_type'];
$_resource_name = $_params['resource_name'];
switch ($_resource_type) {
if ($params['get_source']) {
$params['source_content'] = $this->_read_file($_resource_name);
$params['resource_timestamp'] = filemtime($_resource_name);
$_return = is_file($_resource_name);
// call resource functions to fetch the template source and timestamp
if ($params['get_source']) {
$_source_return = isset ($this->_plugins['resource'][$_resource_type]) &&
array($_resource_name, &$params['source_content'], &$this));
$_timestamp_return = isset ($this->_plugins['resource'][$_resource_type]) &&
array($_resource_name, &$params['resource_timestamp'], &$this));
$_return = $_source_return && $_timestamp_return;
// see if we can get a template with the default template handler
$this->trigger_error("default template handler function \"$this->default_template_handler_func\" doesn't exist.");
array($_params['resource_type'], $_params['resource_name'], &$params['source_content'], &$params['resource_timestamp'], &$this));
$this->trigger_error('unable to read resource: "' . $params['resource_name'] . '"');
} else if ($_return && $this->security) {
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.is_secure.php');
$this->trigger_error('(secure mode) accessing "' . $params['resource_name'] . '" is not allowed');
$params['source_content'] = null;
$params['resource_timestamp'] = null;
* parse out the type and name from the resource
* @param string $resource_base_path
* @param string $resource_name
* @param string $resource_type
* @param string $resource_name
// split tpl_path by the first colon
$_resource_name_parts = explode(':', $params['resource_name'], 2);
if (count($_resource_name_parts) == 1) {
// no resource type given
$params['resource_name'] = $_resource_name_parts[0];
if(strlen($_resource_name_parts[0]) == 1) {
// 1 char is not resource type, but part of filepath
$params['resource_name'] = $params['resource_name'];
$params['resource_type'] = $_resource_name_parts[0];
$params['resource_name'] = $_resource_name_parts[1];
if ($params['resource_type'] == 'file') {
if (!preg_match("/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/", $params['resource_name'])) {
// relative pathname to $params['resource_base_path']
// use the first directory where the file is found
if (isset ($params['resource_base_path'])) {
$_resource_base_path = (array) $params['resource_base_path'];
$_resource_base_path[] = '.';
foreach ($_resource_base_path as $_curr_path) {
$_fullpath = $_curr_path . DIRECTORY_SEPARATOR . $params['resource_name'];
$params['resource_name'] = $_fullpath;
// didn't find the file, try include_path
$_params = array('file_path' => $_fullpath);
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.get_include_path.php');
$params['resource_name'] = $_params['new_file_path'];
} elseif (empty($this->_plugins['resource'][$params['resource_type']])) {
$_params = array('type' => $params['resource_type']);
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.load_resource_plugin.php');
* @param string|null$modifier_name
* @param array|null$map_array
* @return string result of modifiers
list ($_modifier_name, $_map_array) = array_splice($_args, 0, 2);
list ($_func_name, $_tpl_file, $_tpl_line) =
$this->_plugins['modifier'][$_modifier_name];
foreach ($_var as $_key => $_val) {
* Remove starting and ending quotes from the string
if (($string{0} == "'" || $string{0} == '"') &&
$string{strlen($string)- 1} == $string{0})
return substr($string, 1, - 1);
* read in a file from line $start for $lines.
* read the entire file if $start and $lines are null.
* @param string $filename
function _read_file($filename, $start= null, $lines= null)
if (!($fd = @fopen($filename, 'r'))) {
if ($start == null && $lines == null) {
// skip the first lines before $start
for ($loop= 1; $loop < $start; $loop++ ) {
// read the rest of the file
$contents .= fgets($fd, 65536);
// read up to $lines lines
for ($loop= 0; $loop < $lines; $loop++ ) {
$contents .= fgets($fd, 65536);
* get a concrete filename for automagically created content
* @param string $auto_base
* @param string $auto_source
$_compile_dir_sep = $this->use_sub_dirs ? DIRECTORY_SEPARATOR : '^';
$_return = $auto_base . DIRECTORY_SEPARATOR;
// auto_base not found, try include_path
$_params = array('file_path' => $auto_base);
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.get_include_path.php');
$_return = isset ($_params['new_file_path']) ? $_params['new_file_path'] . DIRECTORY_SEPARATOR : null;
// make auto_id safe for directory names
// split into separate directories
$_return .= $auto_id . $_compile_dir_sep;
if(isset ($auto_source)) {
// make source name safe for filename
$_crc32 = crc32($auto_source) . $_compile_dir_sep;
// prepend %% to avoid name conflicts with
// with $params['auto_id'] names
$_crc32 = '%%' . substr($_crc32,0,3) . $_compile_dir_sep . '%%' . $_crc32;
$_return .= $_crc32 . $_filename;
* unlink a file, possibly using expiration time
* @param string $resource
* @param integer $exp_time
function _unlink($resource, $exp_time = null)
* returns an auto_id for auto-file-functions
* @param string $cache_id
* @param string $compile_id
return (isset ($compile_id)) ? $cache_id . '|' . $compile_id : $cache_id;
elseif(isset ($compile_id))
* trigger Smarty plugin error
* @param string $error_msg
* @param string $tpl_file
* @param integer $tpl_line
* @param integer $error_type
$file = null, $line = null, $error_type = E_USER_ERROR)
if(isset ($file) && isset ($line)) {
$info = ' ('. basename($file). ", line $line)";
if (isset ($tpl_line) && isset ($tpl_file)) {
$tpl_line . "]: $error_msg$info", $error_type);
* callback function for preg_replace, to call a non-cacheable block
$_func = '_smarty_tplfunc_'. $match[2]. '_'. $match[3];
* called for included templates
* @param string $_smarty_include_tpl_file
* @param string $_smarty_include_vars
// $_smarty_include_tpl_file, $_smarty_include_vars
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.get_microtime.php');
$this->_smarty_debug_info[] = array('type' => 'template',
'filename' => $params['smarty_include_tpl_file'],
'depth' => ++ $this->_inclusion_depth);
$included_tpls_idx = count($this->_smarty_debug_info) - 1;
$this->_tpl_vars = array_merge($this->_tpl_vars, $params['smarty_include_vars']);
// config vars are treated as local, so push a copy of the
// current ones onto the front of the stack
$_smarty_compile_path = $this->_get_compile_path($params['smarty_include_tpl_file']);
if ($this->_is_compiled($params['smarty_include_tpl_file'], $_smarty_compile_path)
|| $this->_compile_resource($params['smarty_include_tpl_file'], $_smarty_compile_path))
include($_smarty_compile_path);
// pop the local vars off the front of the stack
$this->_inclusion_depth-- ;
// capture time for debugging info
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.get_microtime.php');
$this->_cache_info['template'][$params['smarty_include_tpl_file']] = true;
* get or set an array of cached attributes for function that is
$_cache_attrs = & $this->_cache_info['cache_attrs'][$cache_serial][$count];
if ($this->_cache_including) {
/* return next set of cache_attrs */
$_return = & current($_cache_attrs);
/* add a reference to a new set of cache_attrs */
$_cache_attrs[] = array();
return $_cache_attrs[count($_cache_attrs)- 1];
* wrapper for include() retaining $this
function _include($filename, $once= false, $params= null)
return include_once($filename);
return include($filename);
* wrapper for eval() retaining $this
function _eval($code, $params= null)
/* vim: set expandtab: */
|