diff options
Diffstat (limited to 'lib/smarty3/Smarty.class.php')
-rw-r--r-- | lib/smarty3/Smarty.class.php | 776 |
1 files changed, 336 insertions, 440 deletions
diff --git a/lib/smarty3/Smarty.class.php b/lib/smarty3/Smarty.class.php index 8d18450..a896992 100644 --- a/lib/smarty3/Smarty.class.php +++ b/lib/smarty3/Smarty.class.php @@ -21,41 +21,44 @@ * smarty-discussion-subscribe@googlegroups.com * * @link http://www.smarty.net/ - * @copyright 2016 New Digital Group, Inc. - * @copyright 2016 Uwe Tews + * @copyright 2018 New Digital Group, Inc. + * @copyright 2018 Uwe Tews * @author Monte Ohrt <monte at ohrt dot com> - * @author Uwe Tews + * @author Uwe Tews <uwe dot tews at gmail dot com> * @author Rodney Rehm * @package Smarty - * @version 3.1.30 + * @version 3.1.33 */ - -/** - * define shorthand directory separator constant - */ -if (!defined('DS')) { - define('DS', DIRECTORY_SEPARATOR); -} - /** * set SMARTY_DIR to absolute path to Smarty library files. * Sets SMARTY_DIR only if user application has not already defined it. */ if (!defined('SMARTY_DIR')) { - define('SMARTY_DIR', dirname(__FILE__) . DS); + /** + * + */ + define('SMARTY_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR); } - /** * set SMARTY_SYSPLUGINS_DIR to absolute path to Smarty internal plugins. * Sets SMARTY_SYSPLUGINS_DIR only if user application has not already defined it. */ if (!defined('SMARTY_SYSPLUGINS_DIR')) { - define('SMARTY_SYSPLUGINS_DIR', SMARTY_DIR . 'sysplugins' . DS); + /** + * + */ + define('SMARTY_SYSPLUGINS_DIR', SMARTY_DIR . 'sysplugins' . DIRECTORY_SEPARATOR); } if (!defined('SMARTY_PLUGINS_DIR')) { - define('SMARTY_PLUGINS_DIR', SMARTY_DIR . 'plugins' . DS); + /** + * + */ + define('SMARTY_PLUGINS_DIR', SMARTY_DIR . 'plugins' . DIRECTORY_SEPARATOR); } if (!defined('SMARTY_MBSTRING')) { + /** + * + */ define('SMARTY_MBSTRING', function_exists('mb_get_info')); } if (!defined('SMARTY_RESOURCE_CHAR_SET')) { @@ -71,25 +74,16 @@ if (!defined('SMARTY_RESOURCE_DATE_FORMAT')) { */ define('SMARTY_RESOURCE_DATE_FORMAT', '%b %e, %Y'); } - /** - * Try loading the Smarty_Internal_Data class - * If we fail we must load Smarty's autoloader. - * Otherwise we may have a global autoloader like Composer + * Load Smarty_Autoloader */ -if (!class_exists('Smarty_Autoloader', false)) { - if (!class_exists('Smarty_Internal_Data', true)) { - require_once dirname(__FILE__) . '/Autoloader.php'; - Smarty_Autoloader::registerBC(); - } +if (!class_exists('Smarty_Autoloader')) { + include dirname(__FILE__) . '/bootstrap.php'; } - /** * Load always needed external class files */ -if (!class_exists('Smarty_Internal_Data', false)) { - require_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_data.php'; -} +require_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_data.php'; require_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_extension_handler.php'; require_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_templatebase.php'; require_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_template.php'; @@ -97,6 +91,7 @@ require_once SMARTY_SYSPLUGINS_DIR . 'smarty_resource.php'; require_once SMARTY_SYSPLUGINS_DIR . 'smarty_variable.php'; require_once SMARTY_SYSPLUGINS_DIR . 'smarty_template_source.php'; require_once SMARTY_SYSPLUGINS_DIR . 'smarty_template_resource_base.php'; +require_once SMARTY_SYSPLUGINS_DIR . 'smarty_internal_resource_file.php'; /** * This is the main Smarty class @@ -108,133 +103,76 @@ require_once SMARTY_SYSPLUGINS_DIR . 'smarty_template_resource_base.php'; * * @method int clearAllCache(int $exp_time = null, string $type = null) * @method int clearCache(string $template_name, string $cache_id = null, string $compile_id = null, int $exp_time = null, string $type = null) - * @method int compileAllTemplates(string $extension = '.tpl', bool $force_compile = false, int $time_limit = 0, int $max_errors = null) - * @method int compileAllConfig(string $extension = '.conf', bool $force_compile = false, int $time_limit = 0, int $max_errors = null) + * @method int compileAllTemplates(string $extension = '.tpl', bool $force_compile = false, int $time_limit = 0, $max_errors = null) + * @method int compileAllConfig(string $extension = '.conf', bool $force_compile = false, int $time_limit = 0, $max_errors = null) * @method int clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null) */ class Smarty extends Smarty_Internal_TemplateBase { - /**#@+ - * constant definitions - */ - /** * smarty version */ - const SMARTY_VERSION = '3.1.30'; - + const SMARTY_VERSION = '3.1.33'; /** * define variable scopes */ - const SCOPE_LOCAL = 1; - - const SCOPE_PARENT = 2; - + const SCOPE_LOCAL = 1; + const SCOPE_PARENT = 2; const SCOPE_TPL_ROOT = 4; - - const SCOPE_ROOT = 8; - - const SCOPE_SMARTY = 16; - - const SCOPE_GLOBAL = 32; - + const SCOPE_ROOT = 8; + const SCOPE_SMARTY = 16; + const SCOPE_GLOBAL = 32; /** * define caching modes */ - const CACHING_OFF = 0; - + const CACHING_OFF = 0; const CACHING_LIFETIME_CURRENT = 1; - - const CACHING_LIFETIME_SAVED = 2; - + const CACHING_LIFETIME_SAVED = 2; /** * define constant for clearing cache files be saved expiration dates */ - const CLEAR_EXPIRED = - 1; - + const CLEAR_EXPIRED = -1; /** * define compile check modes */ - const COMPILECHECK_OFF = 0; - - const COMPILECHECK_ON = 1; - + const COMPILECHECK_OFF = 0; + const COMPILECHECK_ON = 1; const COMPILECHECK_CACHEMISS = 2; - /** * define debug modes */ - const DEBUG_OFF = 0; - - const DEBUG_ON = 1; - + const DEBUG_OFF = 0; + const DEBUG_ON = 1; const DEBUG_INDIVIDUAL = 2; - /** * modes for handling of "<?php ... ?>" tags in templates. */ const PHP_PASSTHRU = 0; //-> print tags as plain text - - const PHP_QUOTE = 1; //-> escape tags as entities - - const PHP_REMOVE = 2; //-> escape tags as entities - - const PHP_ALLOW = 3; //-> escape tags as entities - + const PHP_QUOTE = 1; //-> escape tags as entities + const PHP_REMOVE = 2; //-> escape tags as entities + const PHP_ALLOW = 3; //-> escape tags as entities /** * filter types */ - const FILTER_POST = 'post'; - - const FILTER_PRE = 'pre'; - - const FILTER_OUTPUT = 'output'; - + const FILTER_POST = 'post'; + const FILTER_PRE = 'pre'; + const FILTER_OUTPUT = 'output'; const FILTER_VARIABLE = 'variable'; - /** * plugin types */ - const PLUGIN_FUNCTION = 'function'; - - const PLUGIN_BLOCK = 'block'; - - const PLUGIN_COMPILER = 'compiler'; - - const PLUGIN_MODIFIER = 'modifier'; - + const PLUGIN_FUNCTION = 'function'; + const PLUGIN_BLOCK = 'block'; + const PLUGIN_COMPILER = 'compiler'; + const PLUGIN_MODIFIER = 'modifier'; const PLUGIN_MODIFIERCOMPILER = 'modifiercompiler'; /** - * Resource caching modes - * (not used since 3.1.30) - */ - const RESOURCE_CACHE_OFF = 0; - - const RESOURCE_CACHE_AUTOMATIC = 1; // cache template objects by rules - - const RESOURCE_CACHE_TEMPLATE = 2; // cache all template objects - - const RESOURCE_CACHE_ON = 4; // cache source and compiled resources - - /**#@-*/ - - /** * assigned global tpl vars */ public static $global_tpl_vars = array(); /** - * error handler returned by set_error_handler() in Smarty::muteExpectedErrors() - */ - public static $_previous_error_handler = null; - - /** - * contains directories outside of SMARTY_DIR that are to be muted by muteExpectedErrors() - */ - public static $_muted_directories = array(); - - /** * Flag denoting if Multibyte String functions are available */ public static $_MBSTRING = SMARTY_MBSTRING; @@ -260,10 +198,6 @@ class Smarty extends Smarty_Internal_TemplateBase */ public static $_IS_WINDOWS = false; - /**#@+ - * variables - */ - /** * auto literal on delimiters with whitespace * @@ -286,20 +220,6 @@ class Smarty extends Smarty_Internal_TemplateBase public $use_include_path = false; /** - * template directory - * - * @var array - */ - protected $template_dir = array('./templates/'); - - /** - * flags for normalized template directory entries - * - * @var array - */ - protected $_processedTemplateDir = array(); - - /** * flag if template_dir is normalized * * @var bool @@ -314,20 +234,6 @@ class Smarty extends Smarty_Internal_TemplateBase public $_joined_template_dir = null; /** - * config directory - * - * @var array - */ - protected $config_dir = array('./configs/'); - - /** - * flags for normalized template directory entries - * - * @var array - */ - protected $_processedConfigDir = array(); - - /** * flag if config_dir is normalized * * @var bool @@ -363,13 +269,6 @@ class Smarty extends Smarty_Internal_TemplateBase public $default_plugin_handler_func = null; /** - * compile directory - * - * @var string - */ - protected $compile_dir = './templates_c/'; - - /** * flag if template_dir is normalized * * @var bool @@ -377,13 +276,6 @@ class Smarty extends Smarty_Internal_TemplateBase public $_compileDirNormalized = false; /** - * plugins directory - * - * @var array - */ - protected $plugins_dir = array(); - - /** * flag if plugins_dir is normalized * * @var bool @@ -391,13 +283,6 @@ class Smarty extends Smarty_Internal_TemplateBase public $_pluginsDirNormalized = false; /** - * cache directory - * - * @var string - */ - protected $cache_dir = './cache/'; - - /** * flag if template_dir is normalized * * @var bool @@ -412,13 +297,6 @@ class Smarty extends Smarty_Internal_TemplateBase public $force_compile = false; /** - * check template for modifications? - * - * @var boolean - */ - public $compile_check = true; - - /** * use sub dirs for compiled/cached files? * * @var boolean @@ -439,6 +317,15 @@ class Smarty extends Smarty_Internal_TemplateBase */ public $merge_compiled_includes = false; + /* + * flag for behaviour when extends: resource and {extends} tag are used simultaneous + * if false disable execution of {extends} in templates called by extends resource. + * (behaviour as versions < 3.1.28) + * + * @var boolean + */ + public $extends_recursion = true; + /** * force cache file creation * @@ -460,9 +347,13 @@ class Smarty extends Smarty_Internal_TemplateBase */ public $right_delimiter = "}"; - /**#@+ - * security + /** + * array of strings which shall be treated as literal by compiler + * + * @var array string */ + public $literals = array(); + /** * class name * This should be instance of Smarty_Security. @@ -493,7 +384,6 @@ class Smarty extends Smarty_Internal_TemplateBase */ public $allow_php_templates = false; - /**#@-*/ /** * debug mode * Setting this to true enables the debug-console. @@ -536,10 +426,6 @@ class Smarty extends Smarty_Internal_TemplateBase */ public $error_reporting = null; - /**#@+ - * config var settings - */ - /** * Controls whether variables with the same name overwrite each other. * @@ -561,12 +447,6 @@ class Smarty extends Smarty_Internal_TemplateBase */ public $config_read_hidden = false; - /**#@-*/ - - /**#@+ - * resource locking - */ - /** * locking concurrent compiles * @@ -588,8 +468,6 @@ class Smarty extends Smarty_Internal_TemplateBase */ public $locking_timeout = 10; - /**#@-*/ - /** * resource type used if none given * Must be an valid key of $registered_resources. @@ -719,40 +597,90 @@ class Smarty extends Smarty_Internal_TemplateBase public $_debug = null; /** + * template directory + * + * @var array + */ + protected $template_dir = array('./templates/'); + + /** + * flags for normalized template directory entries + * + * @var array + */ + protected $_processedTemplateDir = array(); + + /** + * config directory + * + * @var array + */ + protected $config_dir = array('./configs/'); + + /** + * flags for normalized template directory entries + * + * @var array + */ + protected $_processedConfigDir = array(); + + /** + * compile directory + * + * @var string + */ + protected $compile_dir = './templates_c/'; + + /** + * plugins directory + * + * @var array + */ + protected $plugins_dir = array(); + + /** + * cache directory + * + * @var string + */ + protected $cache_dir = './cache/'; + + /** * removed properties * * @var string[] */ - private $obsoleteProperties = array('resource_caching', 'template_resource_caching', 'direct_access_security', - '_dir_perms', '_file_perms', 'plugin_search_order', - 'inheritance_merge_compiled_includes', 'resource_cache_mode',); + protected $obsoleteProperties = array( + 'resource_caching', 'template_resource_caching', 'direct_access_security', + '_dir_perms', '_file_perms', 'plugin_search_order', + 'inheritance_merge_compiled_includes', 'resource_cache_mode', + ); /** * List of private properties which will call getter/setter on a direct access * * @var string[] */ - private $accessMap = array('template_dir' => 'TemplateDir', 'config_dir' => 'ConfigDir', - 'plugins_dir' => 'PluginsDir', 'compile_dir' => 'CompileDir', - 'cache_dir' => 'CacheDir',); - - /**#@-*/ + protected $accessMap = array( + 'template_dir' => 'TemplateDir', 'config_dir' => 'ConfigDir', + 'plugins_dir' => 'PluginsDir', 'compile_dir' => 'CompileDir', + 'cache_dir' => 'CacheDir', + ); /** * Initialize new Smarty object */ public function __construct() { + $this->_clearTemplateCache(); parent::__construct(); if (is_callable('mb_internal_encoding')) { mb_internal_encoding(Smarty::$_CHARSET); } $this->start_time = microtime(true); - if (isset($_SERVER[ 'SCRIPT_NAME' ])) { Smarty::$global_tpl_vars[ 'SCRIPT_NAME' ] = new Smarty_Variable($_SERVER[ 'SCRIPT_NAME' ]); } - // Check if we're running on windows Smarty::$_IS_WINDOWS = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; // let PCRE (preg_*) treat strings as ISO-8859-1 if we're not dealing with UTF-8 @@ -762,11 +690,33 @@ class Smarty extends Smarty_Internal_TemplateBase } /** + * Enable error handler to mute expected messages + * + * @return boolean + * @deprecated + */ + public static function muteExpectedErrors() + { + return Smarty_Internal_ErrorHandler::muteExpectedErrors(); + } + + /** + * Disable error handler muting expected messages + * + * @deprecated + */ + public static function unmuteExpectedErrors() + { + restore_error_handler(); + } + + /** * Check if a template resource exists * - * @param string $resource_name template name + * @param string $resource_name template name * - * @return boolean status + * @return bool status + * @throws \SmartyException */ public function templateExists($resource_name) { @@ -778,10 +728,10 @@ class Smarty extends Smarty_Internal_TemplateBase /** * Loads security class and enables security * - * @param string|Smarty_Security $security_class if a string is used, it must be class-name + * @param string|Smarty_Security $security_class if a string is used, it must be class-name * * @return Smarty current Smarty instance for chaining - * @throws SmartyException when an invalid class name is provided + * @throws \SmartyException */ public function enableSecurity($security_class = null) { @@ -797,37 +747,15 @@ class Smarty extends Smarty_Internal_TemplateBase public function disableSecurity() { $this->security_policy = null; - - return $this; - } - - /** - * Set template directory - * - * @param string|array $template_dir directory(s) of template sources - * @param bool $isConfig true for config_dir - * - * @return \Smarty current Smarty instance for chaining - */ - public function setTemplateDir($template_dir, $isConfig = false) - { - if ($isConfig) { - $this->config_dir = array(); - $this->_processedConfigDir = array(); - } else { - $this->template_dir = array(); - $this->_processedTemplateDir = array(); - } - $this->addTemplateDir($template_dir, null, $isConfig); return $this; } /** * Add template directory(s) * - * @param string|array $template_dir directory(s) of template sources - * @param string $key of the array element to assign the template dir to - * @param bool $isConfig true for config_dir + * @param string|array $template_dir directory(s) of template sources + * @param string $key of the array element to assign the template dir to + * @param bool $isConfig true for config_dir * * @return Smarty current Smarty instance for chaining */ @@ -882,7 +810,7 @@ class Smarty extends Smarty_Internal_TemplateBase $dir = &$this->template_dir; } if ($isConfig ? !$this->_configDirNormalized : !$this->_templateDirNormalized) { - $this->_nomalizeTemplateConfig($isConfig); + $this->_normalizeTemplateConfig($isConfig); } if ($index !== null) { return isset($dir[ $index ]) ? $dir[ $index ] : null; @@ -891,15 +819,24 @@ class Smarty extends Smarty_Internal_TemplateBase } /** - * Set config directory + * Set template directory * - * @param $config_dir + * @param string|array $template_dir directory(s) of template sources + * @param bool $isConfig true for config_dir * - * @return Smarty current Smarty instance for chaining + * @return \Smarty current Smarty instance for chaining */ - public function setConfigDir($config_dir) + public function setTemplateDir($template_dir, $isConfig = false) { - return $this->setTemplateDir($config_dir, true); + if ($isConfig) { + $this->config_dir = array(); + $this->_processedConfigDir = array(); + } else { + $this->template_dir = array(); + $this->_processedTemplateDir = array(); + } + $this->addTemplateDir($template_dir, null, $isConfig); + return $this; } /** @@ -928,23 +865,21 @@ class Smarty extends Smarty_Internal_TemplateBase } /** - * Set plugins directory + * Set config directory * - * @param string|array $plugins_dir directory(s) of plugins + * @param $config_dir * * @return Smarty current Smarty instance for chaining */ - public function setPluginsDir($plugins_dir) + public function setConfigDir($config_dir) { - $this->plugins_dir = (array) $plugins_dir; - $this->_pluginsDirNormalized = false; - return $this; + return $this->setTemplateDir($config_dir, true); } /** * Adds directory of plugin files * - * @param null|array $plugins_dir + * @param null|array|string $plugins_dir * * @return Smarty current Smarty instance for chaining */ @@ -953,7 +888,7 @@ class Smarty extends Smarty_Internal_TemplateBase if (empty($this->plugins_dir)) { $this->plugins_dir[] = SMARTY_PLUGINS_DIR; } - $this->plugins_dir = array_merge($this->plugins_dir, (array) $plugins_dir); + $this->plugins_dir = array_merge($this->plugins_dir, (array)$plugins_dir); $this->_pluginsDirNormalized = false; return $this; } @@ -971,10 +906,10 @@ class Smarty extends Smarty_Internal_TemplateBase } if (!$this->_pluginsDirNormalized) { if (!is_array($this->plugins_dir)) { - $this->plugins_dir = (array) $this->plugins_dir; + $this->plugins_dir = (array)$this->plugins_dir; } foreach ($this->plugins_dir as $k => $v) { - $this->plugins_dir[ $k ] = $this->_realpath(rtrim($v, "/\\") . DS, true); + $this->plugins_dir[ $k ] = $this->_realpath(rtrim($v, '/\\') . DIRECTORY_SEPARATOR, true); } $this->_cache[ 'plugin_files' ] = array(); $this->_pluginsDirNormalized = true; @@ -983,15 +918,16 @@ class Smarty extends Smarty_Internal_TemplateBase } /** + * Set plugins directory * - * @param string $compile_dir directory to store compiled templates in + * @param string|array $plugins_dir directory(s) of plugins * - * @return Smarty current Smarty instance for chaining + * @return Smarty current Smarty instance for chaining */ - public function setCompileDir($compile_dir) + public function setPluginsDir($plugins_dir) { - $this->_normalizeDir('compile_dir', $compile_dir); - $this->_compileDirNormalized = true; + $this->plugins_dir = (array)$plugins_dir; + $this->_pluginsDirNormalized = false; return $this; } @@ -1010,16 +946,15 @@ class Smarty extends Smarty_Internal_TemplateBase } /** - * Set cache directory * - * @param string $cache_dir directory to store cached templates in + * @param string $compile_dir directory to store compiled templates in * * @return Smarty current Smarty instance for chaining */ - public function setCacheDir($cache_dir) + public function setCompileDir($compile_dir) { - $this->_normalizeDir('cache_dir', $cache_dir); - $this->_cacheDirNormalized = true; + $this->_normalizeDir('compile_dir', $compile_dir); + $this->_compileDirNormalized = true; return $this; } @@ -1038,58 +973,30 @@ class Smarty extends Smarty_Internal_TemplateBase } /** - * Normalize and set directory string - * - * @param string $dirName cache_dir or compile_dir - * @param string $dir filepath of folder - */ - private function _normalizeDir($dirName, $dir) - { - $this->{$dirName} = $this->_realpath(rtrim($dir, "/\\") . DS, true); - if (!isset(Smarty::$_muted_directories[ $this->{$dirName} ])) { - Smarty::$_muted_directories[ $this->{$dirName} ] = null; - } - } - - /** - * Normalize template_dir or config_dir + * Set cache directory * - * @param bool $isConfig true for config_dir + * @param string $cache_dir directory to store cached templates in * + * @return Smarty current Smarty instance for chaining */ - private function _nomalizeTemplateConfig($isConfig) + public function setCacheDir($cache_dir) { - if ($isConfig) { - $processed = &$this->_processedConfigDir; - $dir = &$this->config_dir; - } else { - $processed = &$this->_processedTemplateDir; - $dir = &$this->template_dir; - } - if (!is_array($dir)) { - $dir = (array) $dir; - } - foreach ($dir as $k => $v) { - if (!isset($processed[ $k ])) { - $dir[ $k ] = $v = $this->_realpath(rtrim($v, "/\\") . DS, true); - $processed[ $k ] = true; - } - } - $isConfig ? $this->_configDirNormalized = true : $this->_templateDirNormalized = true; - $isConfig ? $this->_joined_config_dir = join('#', $this->config_dir) : - $this->_joined_template_dir = join('#', $this->template_dir); + $this->_normalizeDir('cache_dir', $cache_dir); + $this->_cacheDirNormalized = true; + return $this; } /** * creates a template object * - * @param string $template the resource handle of the template file - * @param mixed $cache_id cache id to be used with this template - * @param mixed $compile_id compile id to be used with this template - * @param object $parent next higher level of Smarty variables - * @param boolean $do_clone flag is Smarty object shall be cloned + * @param string $template the resource handle of the template file + * @param mixed $cache_id cache id to be used with this template + * @param mixed $compile_id compile id to be used with this template + * @param object $parent next higher level of Smarty variables + * @param boolean $do_clone flag is Smarty object shall be cloned * - * @return object template object + * @return \Smarty_Internal_Template template object + * @throws \SmartyException */ public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null, $do_clone = true) { @@ -1103,14 +1010,20 @@ class Smarty extends Smarty_Internal_TemplateBase } else { $data = null; } + if (!$this->_templateDirNormalized) { + $this->_normalizeTemplateConfig(false); + } $_templateId = $this->_getTemplateId($template, $cache_id, $compile_id); $tpl = null; - if ($this->caching && isset($this->_cache[ 'isCached' ][ $_templateId ])) { - $tpl = $do_clone ? clone $this->_cache[ 'isCached' ][ $_templateId ] : - $this->_cache[ 'isCached' ][ $_templateId ]; + if ($this->caching && isset(Smarty_Internal_Template::$isCacheTplObj[ $_templateId ])) { + $tpl = $do_clone ? clone Smarty_Internal_Template::$isCacheTplObj[ $_templateId ] : + Smarty_Internal_Template::$isCacheTplObj[ $_templateId ]; + $tpl->inheritance = null; + $tpl->tpl_vars = $tpl->config_vars = array(); + } elseif (!$do_clone && isset(Smarty_Internal_Template::$tplObjCache[ $_templateId ])) { + $tpl = clone Smarty_Internal_Template::$tplObjCache[ $_templateId ]; + $tpl->inheritance = null; $tpl->tpl_vars = $tpl->config_vars = array(); - } else if (!$do_clone && isset($this->_cache[ 'tplObjects' ][ $_templateId ])) { - $tpl = clone $this->_cache[ 'tplObjects' ][ $_templateId ]; } else { /* @var Smarty_Internal_Template $tpl */ $tpl = new $this->template_class($template, $this, null, $cache_id, $compile_id, null, null); @@ -1127,10 +1040,10 @@ class Smarty extends Smarty_Internal_TemplateBase $tpl->tpl_vars[ $_key ] = new Smarty_Variable($_val); } } - if ($this->debugging || $this->debugging_ctrl == 'URL') { + if ($this->debugging || $this->debugging_ctrl === 'URL') { $tpl->smarty->_debug = new Smarty_Internal_Debug(); // check URL debugging control - if (!$this->debugging && $this->debugging_ctrl == 'URL') { + if (!$this->debugging && $this->debugging_ctrl === 'URL') { $tpl->smarty->_debug->debugUrl($tpl->smarty); } } @@ -1142,11 +1055,11 @@ class Smarty extends Smarty_Internal_TemplateBase * class name format: Smarty_PluginType_PluginName * plugin filename format: plugintype.pluginname.php * - * @param string $plugin_name class plugin name to load - * @param bool $check check if already loaded + * @param string $plugin_name class plugin name to load + * @param bool $check check if already loaded * - * @throws SmartyException * @return string |boolean filepath of loaded file or false + * @throws \SmartyException */ public function loadPlugin($plugin_name, $check = true) { @@ -1163,16 +1076,20 @@ class Smarty extends Smarty_Internal_TemplateBase * @param \Smarty_Internal_Template $template * * @return string - */ - public function _getTemplateId($template_name, $cache_id = null, $compile_id = null, $caching = null, - Smarty_Internal_Template $template = null) - { + * @throws \SmartyException + */ + public function _getTemplateId( + $template_name, + $cache_id = null, + $compile_id = null, + $caching = null, + Smarty_Internal_Template $template = null + ) { $template_name = (strpos($template_name, ':') === false) ? "{$this->default_resource_type}:{$template_name}" : $template_name; $cache_id = $cache_id === null ? $this->cache_id : $cache_id; $compile_id = $compile_id === null ? $this->compile_id : $compile_id; - $caching = (int) ($caching === null ? $this->caching : $caching); - + $caching = (int)($caching === null ? $this->caching : $caching); if ((isset($template) && strpos($template_name, ':.') !== false) || $this->allow_ambiguous_resources) { $_templateId = Smarty_Resource::getUniqueTemplateName((isset($template) ? $template : $this), $template_name) . @@ -1191,47 +1108,43 @@ class Smarty extends Smarty_Internal_TemplateBase * - remove /./ and /../ * - make it absolute if required * - * @param string $path file path - * @param bool $realpath if true - convert to absolute - * false - convert to relative - * null - keep as it is but remove /./ /../ + * @param string $path file path + * @param bool $realpath if true - convert to absolute + * false - convert to relative + * null - keep as it is but + * remove /./ /../ * * @return string */ public function _realpath($path, $realpath = null) { - $nds = DS == '/' ? '\\' : '/'; - // normalize DS - $path = str_replace($nds, DS, $path); - preg_match('%^(?<root>(?:[[:alpha:]]:[\\\\]|/|[\\\\]{2}[[:alpha:]]+|[[:print:]]{2,}:[/]{2}|[\\\\])?)(?<path>(?:[[:print:]]*))$%', - $path, $parts); + $nds = array('/' => '\\', '\\' => '/'); + preg_match( + '%^(?<root>(?:[[:alpha:]]:[\\\\/]|/|[\\\\]{2}[[:alpha:]]+|[[:print:]]{2,}:[/]{2}|[\\\\])?)(?<path>(.*))$%u', + $path, + $parts + ); $path = $parts[ 'path' ]; - if ($parts[ 'root' ] == '\\') { + if ($parts[ 'root' ] === '\\') { $parts[ 'root' ] = substr(getcwd(), 0, 2) . $parts[ 'root' ]; } else { if ($realpath !== null && !$parts[ 'root' ]) { - $path = getcwd() . DS . $path; + $path = getcwd() . DIRECTORY_SEPARATOR . $path; } } - // remove noop 'DS DS' and 'DS.DS' patterns - $path = preg_replace('#([\\\\/]([.]?[\\\\/])+)#', DS, $path); - // resolve '..DS' pattern, smallest first - if (strpos($path, '..' . DS) != false && - preg_match_all('#(([.]?[\\\\/])*([.][.])[\\\\/]([.]?[\\\\/])*)+#', $path, $match) - ) { - $counts = array(); - foreach ($match[ 0 ] as $m) { - $counts[] = (int) ((strlen($m) - 1) / 3); - } - sort($counts); - foreach ($counts as $count) { - $path = preg_replace('#(([\\\\/]([.]?[\\\\/])*[^\\\\/.]+){' . $count . - '}[\\\\/]([.]?[\\\\/])*([.][.][\\\\/]([.]?[\\\\/])*){' . $count . '})(?=[^.])#', - DS, $path); - } - } - - return $parts[ 'root' ] . $path; + // normalize DIRECTORY_SEPARATOR + $path = str_replace($nds[ DIRECTORY_SEPARATOR ], DIRECTORY_SEPARATOR, $path); + $parts[ 'root' ] = str_replace($nds[ DIRECTORY_SEPARATOR ], DIRECTORY_SEPARATOR, $parts[ 'root' ]); + do { + $path = preg_replace( + array('#[\\\\/]{2}#', '#[\\\\/][.][\\\\/]#', '#[\\\\/]([^\\\\/.]+)[\\\\/][.][.][\\\\/]#'), + DIRECTORY_SEPARATOR, + $path, + -1, + $count + ); + } while ($count > 0); + return $realpath !== false ? $parts[ 'root' ] . $path : str_ireplace(getcwd(), '.', $parts[ 'root' ] . $path); } /** @@ -1239,16 +1152,8 @@ class Smarty extends Smarty_Internal_TemplateBase */ public function _clearTemplateCache() { - $this->_cache[ 'isCached' ] = array(); - $this->_cache[ 'tplObjects' ] = array(); - } - - /** - * @param boolean $compile_check - */ - public function setCompileCheck($compile_check) - { - $this->compile_check = $compile_check; + Smarty_Internal_Template::$isCacheTplObj = array(); + Smarty_Internal_Template::$tplObjCache = array(); } /** @@ -1276,9 +1181,21 @@ class Smarty extends Smarty_Internal_TemplateBase } /** + * Return auto_literal flag + * + * @return boolean + */ + public function getAutoLiteral() + { + return $this->auto_literal; + } + + /** + * Set auto_literal flag + * * @param boolean $auto_literal */ - public function setAutoLiteral($auto_literal) + public function setAutoLiteral($auto_literal = true) { $this->auto_literal = $auto_literal; } @@ -1300,6 +1217,18 @@ class Smarty extends Smarty_Internal_TemplateBase } /** + * Get left delimiter + * + * @return string + */ + public function getLeftDelimiter() + { + return $this->left_delimiter; + } + + /** + * Set left delimiter + * * @param string $left_delimiter */ public function setLeftDelimiter($left_delimiter) @@ -1308,7 +1237,19 @@ class Smarty extends Smarty_Internal_TemplateBase } /** - * @param string $right_delimiter + * Get right delimiter + * + * @return string $right_delimiter + */ + public function getRightDelimiter() + { + return $this->right_delimiter; + } + + /** + * Set right delimiter + * + * @param string */ public function setRightDelimiter($right_delimiter) { @@ -1382,11 +1323,21 @@ class Smarty extends Smarty_Internal_TemplateBase } /** + * Get Smarty object + * + * @return Smarty + */ + public function _getSmartyObj() + { + return $this; + } + + /** * <<magic>> Generic getter. * Calls the appropriate getter function. * Issues an E_USER_NOTICE if no valid getter is found. * - * @param string $name property name + * @param string $name property name * * @return mixed */ @@ -1412,6 +1363,7 @@ class Smarty extends Smarty_Internal_TemplateBase * * @param string $name property name * @param mixed $value parameter passed to setter + * */ public function __set($name, $value) { @@ -1420,110 +1372,54 @@ class Smarty extends Smarty_Internal_TemplateBase $this->{$method}($value); } elseif (in_array($name, $this->obsoleteProperties)) { return; + } elseif (is_object($value) && method_exists($value, $name)) { + $this->$name = $value; } else { - if (is_object($value) && method_exists($value, $name)) { - $this->$name = $value; - } else { - trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE); - } + trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE); } } /** - * Error Handler to mute expected messages - * - * @link http://php.net/set_error_handler - * - * @param integer $errno Error level - * @param $errstr - * @param $errfile - * @param $errline - * @param $errcontext + * Normalize and set directory string * - * @return bool|void + * @param string $dirName cache_dir or compile_dir + * @param string $dir filepath of folder */ - public static function mutingErrorHandler($errno, $errstr, $errfile, $errline, $errcontext) + private function _normalizeDir($dirName, $dir) { - $_is_muted_directory = false; - - // add the SMARTY_DIR to the list of muted directories - if (!isset(Smarty::$_muted_directories[ SMARTY_DIR ])) { - $smarty_dir = realpath(SMARTY_DIR); - if ($smarty_dir !== false) { - Smarty::$_muted_directories[ SMARTY_DIR ] = - array('file' => $smarty_dir, 'length' => strlen($smarty_dir),); - } - } - - // walk the muted directories and test against $errfile - foreach (Smarty::$_muted_directories as $key => &$dir) { - if (!$dir) { - // resolve directory and length for speedy comparisons - $file = realpath($key); - if ($file === false) { - // this directory does not exist, remove and skip it - unset(Smarty::$_muted_directories[ $key ]); - continue; - } - $dir = array('file' => $file, 'length' => strlen($file),); - } - if (!strncmp($errfile, $dir[ 'file' ], $dir[ 'length' ])) { - $_is_muted_directory = true; - break; - } - } - // pass to next error handler if this error did not occur inside SMARTY_DIR - // or the error was within smarty but masked to be ignored - if (!$_is_muted_directory || ($errno && $errno & error_reporting())) { - if (Smarty::$_previous_error_handler) { - return call_user_func(Smarty::$_previous_error_handler, $errno, $errstr, $errfile, $errline, - $errcontext); - } else { - return false; + $this->{$dirName} = $this->_realpath(rtrim($dir, "/\\") . DIRECTORY_SEPARATOR, true); + if (class_exists('Smarty_Internal_ErrorHandler', false)) { + if (!isset(Smarty_Internal_ErrorHandler::$mutedDirectories[ $this->{$dirName} ])) { + Smarty_Internal_ErrorHandler::$mutedDirectories[ $this->{$dirName} ] = null; } } - return; } /** - * Enable error handler to mute expected messages + * Normalize template_dir or config_dir * - * @return void + * @param bool $isConfig true for config_dir */ - public static function muteExpectedErrors() + private function _normalizeTemplateConfig($isConfig) { - /* - error muting is done because some people implemented custom error_handlers using - http://php.net/set_error_handler and for some reason did not understand the following paragraph: - - It is important to remember that the standard PHP error handler is completely bypassed for the - error types specified by error_types unless the callback function returns FALSE. - error_reporting() settings will have no effect and your error handler will be called regardless - - however you are still able to read the current value of error_reporting and act appropriately. - Of particular note is that this value will be 0 if the statement that caused the error was - prepended by the @ error-control operator. - - Smarty deliberately uses @filemtime() over file_exists() and filemtime() in some places. Reasons include - - @filemtime() is almost twice as fast as using an additional file_exists() - - between file_exists() and filemtime() a possible race condition is opened, - which does not exist using the simple @filemtime() approach. - */ - $error_handler = array('Smarty', 'mutingErrorHandler'); - $previous = set_error_handler($error_handler); - - // avoid dead loops - if ($previous !== $error_handler) { - Smarty::$_previous_error_handler = $previous; + if ($isConfig) { + $processed = &$this->_processedConfigDir; + $dir = &$this->config_dir; + } else { + $processed = &$this->_processedTemplateDir; + $dir = &$this->template_dir; } - } - - /** - * Disable error handler muting expected messages - * - * @return void - */ - public static function unmuteExpectedErrors() - { - restore_error_handler(); + if (!is_array($dir)) { + $dir = (array)$dir; + } + foreach ($dir as $k => $v) { + if (!isset($processed[ $k ])) { + $dir[ $k ] = $v = $this->_realpath(rtrim($v, "/\\") . DIRECTORY_SEPARATOR, true); + $processed[ $k ] = true; + } + } + $isConfig ? $this->_configDirNormalized = true : $this->_templateDirNormalized = true; + $isConfig ? $this->_joined_config_dir = join('#', $this->config_dir) : + $this->_joined_template_dir = join('#', $this->template_dir); } } |