3 * Project: Smarty: the PHP compiling template engine
4 * File: Smarty.class.php
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * For questions, help, comments, discussion, etc., please join the
20 * Smarty mailing list. Send a blank e-mail to
21 * smarty-discussion-subscribe@googlegroups.com
23 * @link http://www.smarty.net/
24 * @copyright 2015 New Digital Group, Inc.
25 * @copyright 2015 Uwe Tews
26 * @author Monte Ohrt <monte at ohrt dot com>
34 * define shorthand directory separator constant
37 define('DS', DIRECTORY_SEPARATOR
);
41 * set SMARTY_DIR to absolute path to Smarty library files.
42 * Sets SMARTY_DIR only if user application has not already defined it.
44 if (!defined('SMARTY_DIR')) {
45 define('SMARTY_DIR', dirname(__FILE__
) . DS
);
49 * set SMARTY_SYSPLUGINS_DIR to absolute path to Smarty internal plugins.
50 * Sets SMARTY_SYSPLUGINS_DIR only if user application has not already defined it.
52 if (!defined('SMARTY_SYSPLUGINS_DIR')) {
53 define('SMARTY_SYSPLUGINS_DIR', SMARTY_DIR
. 'sysplugins' . DS
);
55 if (!defined('SMARTY_PLUGINS_DIR')) {
56 define('SMARTY_PLUGINS_DIR', SMARTY_DIR
. 'plugins' . DS
);
58 if (!defined('SMARTY_MBSTRING')) {
59 define('SMARTY_MBSTRING', function_exists('mb_get_info'));
61 if (!defined('SMARTY_RESOURCE_CHAR_SET')) {
62 // UTF-8 can only be done properly when mbstring is available!
64 * @deprecated in favor of Smarty::$_CHARSET
66 define('SMARTY_RESOURCE_CHAR_SET', SMARTY_MBSTRING ?
'UTF-8' : 'ISO-8859-1');
68 if (!defined('SMARTY_RESOURCE_DATE_FORMAT')) {
70 * @deprecated in favor of Smarty::$_DATE_FORMAT
72 define('SMARTY_RESOURCE_DATE_FORMAT', '%b %e, %Y');
76 * Try loading the Smmarty_Internal_Data class
77 * If we fail we must load Smarty's autoloader.
78 * Otherwise we may have a global autoloader like Composer
80 if (!class_exists('Smarty_Autoloader', false)) {
81 if (!class_exists('Smarty_Internal_Data', true)) {
82 require_once 'Autoloader.php';
83 Smarty_Autoloader
::registerBC();
88 * Load always needed external class files
91 if (!class_exists('Smarty_Internal_Data', false)) {
92 require_once SMARTY_SYSPLUGINS_DIR
. 'smarty_internal_data.php';
94 require_once SMARTY_SYSPLUGINS_DIR
. 'smarty_internal_templatebase.php';
95 require_once SMARTY_SYSPLUGINS_DIR
. 'smarty_internal_template.php';
96 require_once SMARTY_SYSPLUGINS_DIR
. 'smarty_resource.php';
97 require_once SMARTY_SYSPLUGINS_DIR
. 'smarty_variable.php';
98 require_once SMARTY_SYSPLUGINS_DIR
. 'smarty_template_source.php';
101 * This is the main Smarty class
105 class Smarty
extends Smarty_Internal_TemplateBase
108 * constant definitions
114 const SMARTY_VERSION
= '3.1.22';
117 * define variable scopes
119 const SCOPE_LOCAL
= 0;
120 const SCOPE_PARENT
= 1;
121 const SCOPE_ROOT
= 2;
122 const SCOPE_GLOBAL
= 3;
124 * define caching modes
126 const CACHING_OFF
= 0;
127 const CACHING_LIFETIME_CURRENT
= 1;
128 const CACHING_LIFETIME_SAVED
= 2;
130 * define constant for clearing cache files be saved expiration datees
132 const CLEAR_EXPIRED
= - 1;
135 * define compile check modes
137 const COMPILECHECK_OFF
= 0;
138 const COMPILECHECK_ON
= 1;
139 const COMPILECHECK_CACHEMISS
= 2;
146 const DEBUG_INDIVIDUAL
= 2;
148 * modes for handling of "<?php ... ?>" tags in templates.
150 const PHP_PASSTHRU
= 0; //-> print tags as plain text
151 const PHP_QUOTE
= 1; //-> escape tags as entities
152 const PHP_REMOVE
= 2; //-> escape tags as entities
153 const PHP_ALLOW
= 3; //-> escape tags as entities
157 const FILTER_POST
= 'post';
158 const FILTER_PRE
= 'pre';
159 const FILTER_OUTPUT
= 'output';
160 const FILTER_VARIABLE
= 'variable';
164 const PLUGIN_FUNCTION
= 'function';
165 const PLUGIN_BLOCK
= 'block';
166 const PLUGIN_COMPILER
= 'compiler';
167 const PLUGIN_MODIFIER
= 'modifier';
168 const PLUGIN_MODIFIERCOMPILER
= 'modifiercompiler';
173 * assigned global tpl vars
175 public static $global_tpl_vars = array();
178 * error handler returned by set_error_hanlder() in Smarty::muteExpectedErrors()
180 public static $_previous_error_handler = null;
182 * contains directories outside of SMARTY_DIR that are to be muted by muteExpectedErrors()
184 public static $_muted_directories = array('./templates_c/' => null, './cache/' => null);
186 * Flag denoting if Multibyte String functions are available
188 public static $_MBSTRING = SMARTY_MBSTRING
;
190 * The character set to adhere to (e.g. "UTF-8")
192 public static $_CHARSET = SMARTY_RESOURCE_CHAR_SET
;
194 * The date format to be used internally
195 * (accepts date() and strftime())
197 public static $_DATE_FORMAT = SMARTY_RESOURCE_DATE_FORMAT
;
199 * Flag denoting if PCRE should run in UTF-8 mode
201 public static $_UTF8_MODIFIER = 'u';
204 * Flag denoting if operating system is windows
206 public static $_IS_WINDOWS = false;
213 * auto literal on delimiters with whitspace
217 public $auto_literal = true;
219 * display error on not assigned variables
223 public $error_unassigned = false;
225 * look up relative filepaths in include_path
229 public $use_include_path = false;
235 private $template_dir = array('./templates/');
237 * joined template directory string used in cache keys
241 public $joined_template_dir = './templates/';
243 * joined config directory string used in cache keys
247 public $joined_config_dir = './configs/';
249 * default template handler
253 public $default_template_handler_func = null;
255 * default config handler
259 public $default_config_handler_func = null;
261 * default plugin handler
265 public $default_plugin_handler_func = null;
271 private $compile_dir = './templates_c/';
277 private $plugins_dir = null;
283 private $cache_dir = './cache/';
289 private $config_dir = array('./configs/');
291 * force template compiling?
295 public $force_compile = false;
297 * check template for modifications?
301 public $compile_check = true;
303 * use sub dirs for compiled/cached files?
307 public $use_sub_dirs = false;
309 * allow ambiguous resources (that are made unique by the resource handler)
313 public $allow_ambiguous_resources = false;
315 * merge compiled includes
319 public $merge_compiled_includes = false;
321 * template inheritance merge compiled includes
325 public $inheritance_merge_compiled_includes = true;
327 * force cache file creation
331 public $force_cache = false;
333 * template left-delimiter
337 public $left_delimiter = "{";
339 * template right-delimiter
343 public $right_delimiter = "}";
349 * This should be instance of Smarty_Security.
352 * @see Smarty_Security
354 public $security_class = 'Smarty_Security';
356 * implementation of security class
358 * @var Smarty_Security
360 public $security_policy = null;
362 * controls handling of PHP-blocks
366 public $php_handling = self
::PHP_PASSTHRU
;
368 * controls if the php template file resource is allowed
372 public $allow_php_templates = false;
374 * Should compiled-templates be prevented from being called directly?
376 * Currently used by Smarty_Internal_Template only.
381 public $direct_access_security = true;
385 * Setting this to true enables the debug-console.
389 public $debugging = false;
391 * This determines if debugging is enable-able from the browser.
393 * <li>NONE => no debugging control allowed</li>
394 * <li>URL => enable debugging when SMARTY_DEBUG is found in the URL.</li>
399 public $debugging_ctrl = 'NONE';
401 * Name of debugging URL-param.
402 * Only used when $debugging_ctrl is set to 'URL'.
403 * The name of the URL-parameter that activates debugging.
407 public $smarty_debug_id = 'SMARTY_DEBUG';
409 * Path of debug template.
413 public $debug_tpl = null;
415 * When set, smarty uses this value as error_reporting-level.
419 public $error_reporting = null;
422 * Internal flag for getTags()
426 public $get_used_tags = false;
429 * config var settings
433 * Controls whether variables with the same name overwrite each other.
437 public $config_overwrite = true;
439 * Controls whether config values of on/true/yes and off/false/no get converted to boolean.
443 public $config_booleanize = true;
445 * Controls whether hidden config sections/vars are read from the file.
449 public $config_read_hidden = false;
458 * locking concurrent compiles
462 public $compile_locking = true;
464 * Controls whether cache resources should emply locking mechanism
468 public $cache_locking = false;
470 * seconds to wait for acquiring a lock before ignoring the write lock
474 public $locking_timeout = 10;
479 * resource type used if none given
480 * Must be an valid key of $registered_resources.
484 public $default_resource_type = 'file';
487 * Must be an element of $cache_resource_types.
491 public $caching_type = 'file';
493 * internal config properties
497 public $properties = array();
503 public $default_config_type = 'file';
505 * cached template objects
509 public $source_objects = array();
511 * cached template objects
515 public $template_objects = array();
518 * enable resource caching
522 public $resource_caching = false;
524 * enable template resource caching
528 public $template_resource_caching = true;
530 * check If-Modified-Since headers
534 public $cache_modified_check = false;
540 public $registered_plugins = array();
542 * plugin search order
546 public $plugin_search_order = array('function', 'block', 'compiler', 'class');
552 public $registered_objects = array();
558 public $registered_classes = array();
564 public $registered_filters = array();
566 * registered resources
570 public $registered_resources = array();
572 * resource handler cache
576 public $_resource_handlers = array();
578 * registered cache resources
582 public $registered_cache_resources = array();
584 * cache resource handler cache
588 public $_cacheresource_handlers = array();
594 public $autoload_filters = array();
600 public $default_modifiers = array();
602 * autoescape variable output
606 public $escape_html = false;
608 * global internal smarty vars
612 public static $_smarty_vars = array();
614 * start time for execution time calculation
618 public $start_time = 0;
620 * default file permissions
624 public $_file_perms = 0644;
626 * default dir permissions
630 public $_dir_perms = 0771;
632 * block tag hierarchy
636 public $_tag_stack = array();
638 * required by the compiler for BC
642 public $_current_file = null;
644 * internal flag to enable parser debugging
648 public $_parserdebug = false;
651 * Cache of is_file results of loadPlugin()
655 public $_is_file_cache = array();
660 * Initialize new Smarty object
663 public function __construct()
665 if (is_callable('mb_internal_encoding')) {
666 mb_internal_encoding(Smarty
::$_CHARSET);
668 $this->start_time
= microtime(true);
669 // check default dirs for overloading
670 if ($this->template_dir
[0] !== './templates/' ||
isset($this->template_dir
[1])) {
671 $this->setTemplateDir($this->template_dir
);
673 if ($this->config_dir
[0] !== './configs/' ||
isset($this->config_dir
[1])) {
674 $this->setConfigDir($this->config_dir
);
676 if ($this->compile_dir
!== './templates_c/') {
677 unset(self
::$_muted_directories['./templates_c/']);
678 $this->setCompileDir($this->compile_dir
);
680 if ($this->cache_dir
!== './cache/') {
681 unset(self
::$_muted_directories['./cache/']);
682 $this->setCacheDir($this->cache_dir
);
684 if (isset($this->plugins_dir
)) {
685 $this->setPluginsDir($this->plugins_dir
);
687 $this->setPluginsDir(SMARTY_PLUGINS_DIR
);
690 $this->debug_tpl
= 'file:' . dirname(__FILE__
) . '/debug.tpl';
691 if (isset($_SERVER['SCRIPT_NAME'])) {
692 Smarty
::$global_tpl_vars['SCRIPT_NAME'] = new Smarty_Variable($_SERVER['SCRIPT_NAME']);
695 // Check if we're running on windows
696 Smarty
::$_IS_WINDOWS = strtoupper(substr(PHP_OS
, 0, 3)) === 'WIN';
698 // let PCRE (preg_*) treat strings as ISO-8859-1 if we're not dealing with UTF-8
699 if (Smarty
::$_CHARSET !== 'UTF-8') {
700 Smarty
::$_UTF8_MODIFIER = '';
705 * fetches a rendered Smarty template
707 * @param string $template the resource handle of the template file or template object
708 * @param mixed $cache_id cache id to be used with this template
709 * @param mixed $compile_id compile id to be used with this template
710 * @param object $parent next higher level of Smarty variables
711 * @param bool $display true: display, false: fetch
712 * @param bool $merge_tpl_vars not used - left for BC
713 * @param bool $no_output_filter not used - left for BC
716 * @throws SmartyException
717 * @return string rendered template output
719 public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null, $display = false, $merge_tpl_vars = true, $no_output_filter = false)
721 if ($cache_id !== null && is_object($cache_id)) {
725 if ($parent === null) {
728 // get template object
729 $_template = is_object($template) ?
$template : $this->createTemplate($template, $cache_id, $compile_id, $parent, false);
730 // set caching in template object
731 $_template->caching
= $this->caching
;
732 // fetch template content
733 return $_template->render(true, false, $display);
737 * displays a Smarty template
739 * @param string $template the resource handle of the template file or template object
740 * @param mixed $cache_id cache id to be used with this template
741 * @param mixed $compile_id compile id to be used with this template
742 * @param object $parent next higher level of Smarty variables
744 public function display($template = null, $cache_id = null, $compile_id = null, $parent = null)
747 $this->fetch($template, $cache_id, $compile_id, $parent, true);
751 * Check if a template resource exists
753 * @param string $resource_name template name
755 * @return boolean status
757 public function templateExists($resource_name)
759 // create template object
760 $save = $this->template_objects
;
761 $tpl = new $this->template_class($resource_name, $this);
762 // check if it does exists
763 $result = $tpl->source
->exists
;
764 $this->template_objects
= $save;
770 * Returns a single or all global variables
772 * @param string $varname variable name or null
774 * @return string variable value or or array of variables
776 public function getGlobal($varname = null)
778 if (isset($varname)) {
779 if (isset(self
::$global_tpl_vars[$varname])) {
780 return self
::$global_tpl_vars[$varname]->value
;
786 foreach (self
::$global_tpl_vars AS $key => $var) {
787 $_result[$key] = $var->value
;
797 * @param integer $exp_time expiration time
798 * @param string $type resource type
800 * @return integer number of cache files deleted
802 public function clearAllCache($exp_time = null, $type = null)
804 // load cache resource and call clearAll
805 $_cache_resource = Smarty_CacheResource
::load($this, $type);
806 Smarty_CacheResource
::invalidLoadedCache($this);
808 return $_cache_resource->clearAll($this, $exp_time);
812 * Empty cache for a specific template
814 * @param string $template_name template name
815 * @param string $cache_id cache id
816 * @param string $compile_id compile id
817 * @param integer $exp_time expiration time
818 * @param string $type resource type
820 * @return integer number of cache files deleted
822 public function clearCache($template_name, $cache_id = null, $compile_id = null, $exp_time = null, $type = null)
824 // load cache resource and call clear
825 $_cache_resource = Smarty_CacheResource
::load($this, $type);
826 Smarty_CacheResource
::invalidLoadedCache($this);
828 return $_cache_resource->clear($this, $template_name, $cache_id, $compile_id, $exp_time);
832 * Loads security class and enables security
834 * @param string|Smarty_Security $security_class if a string is used, it must be class-name
836 * @return Smarty current Smarty instance for chaining
837 * @throws SmartyException when an invalid class name is provided
839 public function enableSecurity($security_class = null)
841 if ($security_class instanceof Smarty_Security
) {
842 $this->security_policy
= $security_class;
845 } elseif (is_object($security_class)) {
846 throw new SmartyException("Class '" . get_class($security_class) . "' must extend Smarty_Security.");
848 if ($security_class == null) {
849 $security_class = $this->security_class
;
851 if (!class_exists($security_class)) {
852 throw new SmartyException("Security class '$security_class' is not defined");
853 } elseif ($security_class !== 'Smarty_Security' && !is_subclass_of($security_class, 'Smarty_Security')) {
854 throw new SmartyException("Class '$security_class' must extend Smarty_Security.");
856 $this->security_policy
= new $security_class($this);
865 * @return Smarty current Smarty instance for chaining
867 public function disableSecurity()
869 $this->security_policy
= null;
875 * Set template directory
877 * @param string|array $template_dir directory(s) of template sources
879 * @return Smarty current Smarty instance for chaining
881 public function setTemplateDir($template_dir)
883 $this->template_dir
= array();
884 foreach ((array) $template_dir as $k => $v) {
885 $this->template_dir
[$k] = rtrim(strtr($v, '\\', '/'), '/') . '/';
887 $this->joined_template_dir
= join(' # ', $this->template_dir
);
892 * Add template directory(s)
894 * @param string|array $template_dir directory(s) of template sources
895 * @param string $key of the array element to assign the template dir to
897 * @return Smarty current Smarty instance for chaining
898 * @throws SmartyException when the given template directory is not valid
900 public function addTemplateDir($template_dir, $key = null)
902 $this->_addDir('template_dir', $template_dir, $key);
903 $this->joined_template_dir
= join(' # ', $this->template_dir
);
908 * Get template directories
910 * @param mixed $index index of directory to get, null to get all
912 * @return array|string list of template directories, or directory of $index
914 public function getTemplateDir($index = null)
916 if ($index !== null) {
917 return isset($this->template_dir
[$index]) ?
$this->template_dir
[$index] : null;
919 return (array) $this->template_dir
;
923 * Set config directory
927 * @return Smarty current Smarty instance for chaining
929 public function setConfigDir($config_dir)
931 $this->config_dir
= array();
932 foreach ((array) $config_dir as $k => $v) {
933 $this->config_dir
[$k] = rtrim(strtr($v, '\\', '/'), '/') . '/';
935 $this->joined_config_dir
= join(' # ', $this->config_dir
);
940 * Add config directory(s)
942 * @param string|array $config_dir directory(s) of config sources
943 * @param mixed $key key of the array element to assign the config dir to
945 * @return Smarty current Smarty instance for chaining
947 public function addConfigDir($config_dir, $key = null)
949 $this->_addDir('config_dir', $config_dir, $key);
950 $this->joined_config_dir
= join(' # ', $this->config_dir
);
955 * Get config directory
957 * @param mixed $index index of directory to get, null to get all
959 * @return array|string configuration directory
961 public function getConfigDir($index = null)
963 if ($index !== null) {
964 return isset($this->config_dir
[$index]) ?
$this->config_dir
[$index] : null;
966 return (array) $this->config_dir
;
970 * Set plugins directory
972 * @param string|array $plugins_dir directory(s) of plugins
974 * @return Smarty current Smarty instance for chaining
976 public function setPluginsDir($plugins_dir)
978 $this->plugins_dir
= array();
979 foreach ((array) $plugins_dir as $k => $v) {
980 $this->plugins_dir
[$k] = rtrim(strtr($v, '\\', '/'), '/') . '/';
982 $this->_is_file_cache
= array();
987 * Adds directory of plugin files
989 * @param $plugins_dir
991 * @return Smarty current Smarty instance for chaining
993 public function addPluginsDir($plugins_dir)
995 $this->_addDir('plugins_dir', $plugins_dir);
996 $this->plugins_dir
= array_unique($this->plugins_dir
);
997 $this->_is_file_cache
= array();
1002 * Get plugin directories
1004 * @return array list of plugin directories
1006 public function getPluginsDir()
1008 return (array) $this->plugins_dir
;
1012 * Set compile directory
1014 * @param string $compile_dir directory to store compiled templates in
1016 * @return Smarty current Smarty instance for chaining
1018 public function setCompileDir($compile_dir)
1020 $this->compile_dir
= rtrim(strtr($compile_dir, '\\', '/'), '/') . '/';
1021 if (!isset(Smarty
::$_muted_directories[$this->compile_dir
])) {
1022 Smarty
::$_muted_directories[$this->compile_dir
] = null;
1029 * Get compiled directory
1031 * @return string path to compiled templates
1033 public function getCompileDir()
1035 return $this->compile_dir
;
1039 * Set cache directory
1041 * @param string $cache_dir directory to store cached templates in
1043 * @return Smarty current Smarty instance for chaining
1045 public function setCacheDir($cache_dir)
1047 $this->cache_dir
= rtrim(strtr($cache_dir, '\\', '/'), '/') . '/';
1048 if (!isset(Smarty
::$_muted_directories[$this->cache_dir
])) {
1049 Smarty
::$_muted_directories[$this->cache_dir
] = null;
1056 * Get cache directory
1058 * @return string path of cache directory
1060 public function getCacheDir()
1062 return $this->cache_dir
;
1066 * add directories to given property name
1068 * @param string $dirName directory property name
1069 * @param string|array $dir directory string or array of strings
1070 * @param mixed $key optional key
1072 private function _addDir($dirName, $dir, $key = null)
1074 // make sure we're dealing with an array
1075 $this->$dirName = (array) $this->$dirName;
1077 if (is_array($dir)) {
1078 foreach ($dir as $k => $v) {
1079 $v = rtrim(strtr($v, '\\', '/'), '/') . '/';
1081 // indexes are not merged but appended
1082 $this->{$dirName}[] = $v;
1084 // string indexes are overridden
1085 $this->{$dirName}[$k] = $v;
1089 $v = rtrim(strtr($dir, '\\', '/'), '/') . '/';
1090 if ($key !== null) {
1091 // override directory at specified index
1092 $this->{$dirName}[$key] = $v;
1094 // append new directory
1095 $this->{$dirName}[] = $v;
1101 * Set default modifiers
1103 * @param array|string $modifiers modifier or list of modifiers to set
1105 * @return Smarty current Smarty instance for chaining
1107 public function setDefaultModifiers($modifiers)
1109 $this->default_modifiers
= (array) $modifiers;
1115 * Add default modifiers
1117 * @param array|string $modifiers modifier or list of modifiers to add
1119 * @return Smarty current Smarty instance for chaining
1121 public function addDefaultModifiers($modifiers)
1123 if (is_array($modifiers)) {
1124 $this->default_modifiers
= array_merge($this->default_modifiers
, $modifiers);
1126 $this->default_modifiers
[] = $modifiers;
1133 * Get default modifiers
1135 * @return array list of default modifiers
1137 public function getDefaultModifiers()
1139 return $this->default_modifiers
;
1143 * Set autoload filters
1145 * @param array $filters filters to load automatically
1146 * @param string $type "pre", "output", … specify the filter type to set. Defaults to none treating $filters'
1147 * keys as the appropriate types
1149 * @return Smarty current Smarty instance for chaining
1151 public function setAutoloadFilters($filters, $type = null)
1153 if ($type !== null) {
1154 $this->autoload_filters
[$type] = (array) $filters;
1156 $this->autoload_filters
= (array) $filters;
1163 * Add autoload filters
1165 * @param array $filters filters to load automatically
1166 * @param string $type "pre", "output", … specify the filter type to set. Defaults to none treating $filters'
1167 * keys as the appropriate types
1169 * @return Smarty current Smarty instance for chaining
1171 public function addAutoloadFilters($filters, $type = null)
1173 if ($type !== null) {
1174 if (!empty($this->autoload_filters
[$type])) {
1175 $this->autoload_filters
[$type] = array_merge($this->autoload_filters
[$type], (array) $filters);
1177 $this->autoload_filters
[$type] = (array) $filters;
1180 foreach ((array) $filters as $key => $value) {
1181 if (!empty($this->autoload_filters
[$key])) {
1182 $this->autoload_filters
[$key] = array_merge($this->autoload_filters
[$key], (array) $value);
1184 $this->autoload_filters
[$key] = (array) $value;
1193 * Get autoload filters
1195 * @param string $type type of filter to get autoloads for. Defaults to all autoload filters
1197 * @return array array( 'type1' => array( 'filter1', 'filter2', … ) ) or array( 'filter1', 'filter2', …) if $type
1200 public function getAutoloadFilters($type = null)
1202 if ($type !== null) {
1203 return isset($this->autoload_filters
[$type]) ?
$this->autoload_filters
[$type] : array();
1206 return $this->autoload_filters
;
1210 * return name of debugging template
1214 public function getDebugTemplate()
1216 return $this->debug_tpl
;
1220 * set the debug template
1222 * @param string $tpl_name
1224 * @return Smarty current Smarty instance for chaining
1225 * @throws SmartyException if file is not readable
1227 public function setDebugTemplate($tpl_name)
1229 if (!is_readable($tpl_name)) {
1230 throw new SmartyException("Unknown file '{$tpl_name}'");
1232 $this->debug_tpl
= $tpl_name;
1238 * creates a template object
1240 * @param string $template the resource handle of the template file
1241 * @param mixed $cache_id cache id to be used with this template
1242 * @param mixed $compile_id compile id to be used with this template
1243 * @param object $parent next higher level of Smarty variables
1244 * @param boolean $do_clone flag is Smarty object shall be cloned
1246 * @return object template object
1248 public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null, $do_clone = true)
1250 if ($cache_id !== null && (is_object($cache_id) ||
is_array($cache_id))) {
1251 $parent = $cache_id;
1254 if ($parent !== null && is_array($parent)) {
1260 $_templateId = $this->getTemplateId($template, $cache_id, $compile_id);
1261 if (isset($this->template_objects
[$_templateId])) {
1263 $tpl = clone $this->template_objects
[$_templateId];
1264 $tpl->smarty
= clone $tpl->smarty
;
1266 $tpl = $this->template_objects
[$_templateId];
1268 $tpl->parent
= $parent;
1269 $tpl->tpl_vars
= array();
1270 $tpl->config_vars
= array();
1272 $tpl = new $this->template_class($template, $this, $parent, $cache_id, $compile_id);
1274 $tpl->smarty
= clone $tpl->smarty
;
1276 $tpl->templateId
= $_templateId;
1278 // fill data if present
1279 if (!empty($data) && is_array($data)) {
1280 // set up variable values
1281 foreach ($data as $_key => $_val) {
1282 $tpl->tpl_vars
[$_key] = new Smarty_Variable($_val);
1285 if ($this->debugging
) {
1286 if ($this->debugging
) {
1287 Smarty_Internal_Debug
::register_template($tpl);
1294 * Takes unknown classes and loads plugin files for them
1295 * class name format: Smarty_PluginType_PluginName
1296 * plugin filename format: plugintype.pluginname.php
1298 * @param string $plugin_name class plugin name to load
1299 * @param bool $check check if already loaded
1301 * @throws SmartyException
1302 * @return string |boolean filepath of loaded file or false
1304 public function loadPlugin($plugin_name, $check = true)
1306 // if function or class exists, exit silently (already loaded)
1307 if ($check && (is_callable($plugin_name) ||
class_exists($plugin_name, false))) {
1310 // Plugin name is expected to be: Smarty_[Type]_[Name]
1311 $_name_parts = explode('_', $plugin_name, 3);
1312 // class name must have three parts to be valid plugin
1313 // count($_name_parts) < 3 === !isset($_name_parts[2])
1314 if (!isset($_name_parts[2]) ||
strtolower($_name_parts[0]) !== 'smarty') {
1315 throw new SmartyException("plugin {$plugin_name} is not a valid name format");
1317 // if type is "internal", get plugin from sysplugins
1318 if (strtolower($_name_parts[1]) == 'internal') {
1319 $file = SMARTY_SYSPLUGINS_DIR
. strtolower($plugin_name) . '.php';
1320 if (isset($this->_is_file_cache
[$file]) ?
$this->_is_file_cache
[$file] : $this->_is_file_cache
[$file] = is_file($file)) {
1321 require_once($file);
1327 // plugin filename is expected to be: [type].[name].php
1328 $_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php";
1330 $_stream_resolve_include_path = function_exists('stream_resolve_include_path');
1332 // loop through plugin dirs and find the plugin
1333 foreach ($this->getPluginsDir() as $_plugin_dir) {
1335 $_plugin_dir . $_plugin_filename,
1336 $_plugin_dir . strtolower($_plugin_filename),
1338 foreach ($names as $file) {
1339 if (isset($this->_is_file_cache
[$file]) ?
$this->_is_file_cache
[$file] : $this->_is_file_cache
[$file] = is_file($file)) {
1340 require_once($file);
1343 if ($this->use_include_path
&& !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_plugin_dir)) {
1344 // try PHP include_path
1345 if ($_stream_resolve_include_path) {
1346 $file = stream_resolve_include_path($file);
1348 $file = Smarty_Internal_Get_Include_Path
::getIncludePath($file);
1351 if ($file !== false) {
1352 require_once($file);
1364 * Compile all template files
1366 * @param string $extension file extension
1367 * @param bool $force_compile force all to recompile
1368 * @param int $time_limit
1369 * @param int $max_errors
1371 * @return integer number of template files recompiled
1373 public function compileAllTemplates($extension = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null)
1375 return Smarty_Internal_Utility
::compileAllTemplates($extension, $force_compile, $time_limit, $max_errors, $this);
1379 * Compile all config files
1381 * @param string $extension file extension
1382 * @param bool $force_compile force all to recompile
1383 * @param int $time_limit
1384 * @param int $max_errors
1386 * @return integer number of template files recompiled
1388 public function compileAllConfig($extension = '.conf', $force_compile = false, $time_limit = 0, $max_errors = null)
1390 return Smarty_Internal_Utility
::compileAllConfig($extension, $force_compile, $time_limit, $max_errors, $this);
1394 * Delete compiled template file
1396 * @param string $resource_name template name
1397 * @param string $compile_id compile id
1398 * @param integer $exp_time expiration time
1400 * @return integer number of template files deleted
1402 public function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
1404 return Smarty_Internal_Utility
::clearCompiledTemplate($resource_name, $compile_id, $exp_time, $this);
1408 * Return array of tag/attributes of all tags used by an template
1410 * @param Smarty_Internal_Template $template
1412 * @return array of tag/attributes
1414 public function getTags(Smarty_Internal_Template
$template)
1416 return Smarty_Internal_Utility
::getTags($template);
1420 * Run installation test
1422 * @param array $errors Array to write errors into, rather than outputting them
1424 * @return boolean true if setup is fine, false if something is wrong
1426 public function testInstall(&$errors = null)
1428 return Smarty_Internal_TestInstall
::testInstall($this, $errors);
1432 * @param boolean $compile_check
1434 public function setCompileCheck($compile_check)
1436 $this->compile_check
= $compile_check;
1440 * @param boolean $use_sub_dirs
1442 public function setUseSubDirs($use_sub_dirs)
1444 $this->use_sub_dirs
= $use_sub_dirs;
1448 * @param boolean $caching
1450 public function setCaching($caching)
1452 $this->caching
= $caching;
1456 * @param int $cache_lifetime
1458 public function setCacheLifetime($cache_lifetime)
1460 $this->cache_lifetime
= $cache_lifetime;
1464 * @param string $compile_id
1466 public function setCompileId($compile_id)
1468 $this->compile_id
= $compile_id;
1472 * @param string $cache_id
1474 public function setCacheId($cache_id)
1476 $this->cache_id
= $cache_id;
1480 * @param int $error_reporting
1482 public function setErrorReporting($error_reporting)
1484 $this->error_reporting
= $error_reporting;
1488 * @param boolean $escape_html
1490 public function setEscapeHtml($escape_html)
1492 $this->escape_html
= $escape_html;
1496 * @param boolean $auto_literal
1498 public function setAutoLiteral($auto_literal)
1500 $this->auto_literal
= $auto_literal;
1504 * @param boolean $force_compile
1506 public function setForceCompile($force_compile)
1508 $this->force_compile
= $force_compile;
1512 * @param boolean $merge_compiled_includes
1514 public function setMergeCompiledIncludes($merge_compiled_includes)
1516 $this->merge_compiled_includes
= $merge_compiled_includes;
1520 * @param string $left_delimiter
1522 public function setLeftDelimiter($left_delimiter)
1524 $this->left_delimiter
= $left_delimiter;
1528 * @param string $right_delimiter
1530 public function setRightDelimiter($right_delimiter)
1532 $this->right_delimiter
= $right_delimiter;
1536 * @param boolean $debugging
1538 public function setDebugging($debugging)
1540 $this->debugging
= $debugging;
1544 * @param boolean $config_overwrite
1546 public function setConfigOverwrite($config_overwrite)
1548 $this->config_overwrite
= $config_overwrite;
1552 * @param boolean $config_booleanize
1554 public function setConfigBooleanize($config_booleanize)
1556 $this->config_booleanize
= $config_booleanize;
1560 * @param boolean $config_read_hidden
1562 public function setConfigReadHidden($config_read_hidden)
1564 $this->config_read_hidden
= $config_read_hidden;
1568 * @param boolean $compile_locking
1570 public function setCompileLocking($compile_locking)
1572 $this->compile_locking
= $compile_locking;
1578 public function __destruct()
1580 // intentionally left blank
1584 * <<magic>> Generic getter.
1585 * Calls the appropriate getter function.
1586 * Issues an E_USER_NOTICE if no valid getter is found.
1588 * @param string $name property name
1592 public function __get($name)
1595 'template_dir' => 'getTemplateDir',
1596 'config_dir' => 'getConfigDir',
1597 'plugins_dir' => 'getPluginsDir',
1598 'compile_dir' => 'getCompileDir',
1599 'cache_dir' => 'getCacheDir',
1602 if (isset($allowed[$name])) {
1603 return $this->{$allowed[$name]}();
1605 trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE
);
1610 * <<magic>> Generic setter.
1611 * Calls the appropriate setter function.
1612 * Issues an E_USER_NOTICE if no valid setter is found.
1614 * @param string $name property name
1615 * @param mixed $value parameter passed to setter
1617 public function __set($name, $value)
1620 'template_dir' => 'setTemplateDir',
1621 'config_dir' => 'setConfigDir',
1622 'plugins_dir' => 'setPluginsDir',
1623 'compile_dir' => 'setCompileDir',
1624 'cache_dir' => 'setCacheDir',
1627 if (isset($allowed[$name])) {
1628 $this->{$allowed[$name]}($value);
1630 trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE
);
1635 * Error Handler to mute expected messages
1637 * @link http://php.net/set_error_handler
1639 * @param integer $errno Error level
1643 * @param $errcontext
1647 public static function mutingErrorHandler($errno, $errstr, $errfile, $errline, $errcontext)
1649 $_is_muted_directory = false;
1651 // add the SMARTY_DIR to the list of muted directories
1652 if (!isset(Smarty
::$_muted_directories[SMARTY_DIR
])) {
1653 $smarty_dir = realpath(SMARTY_DIR
);
1654 if ($smarty_dir !== false) {
1655 Smarty
::$_muted_directories[SMARTY_DIR
] = array(
1656 'file' => $smarty_dir,
1657 'length' => strlen($smarty_dir),
1662 // walk the muted directories and test against $errfile
1663 foreach (Smarty
::$_muted_directories as $key => &$dir) {
1665 // resolve directory and length for speedy comparisons
1666 $file = realpath($key);
1667 if ($file === false) {
1668 // this directory does not exist, remove and skip it
1669 unset(Smarty
::$_muted_directories[$key]);
1674 'length' => strlen($file),
1677 if (!strncmp($errfile, $dir['file'], $dir['length'])) {
1678 $_is_muted_directory = true;
1683 // pass to next error handler if this error did not occur inside SMARTY_DIR
1684 // or the error was within smarty but masked to be ignored
1685 if (!$_is_muted_directory ||
($errno && $errno & error_reporting())) {
1686 if (Smarty
::$_previous_error_handler) {
1687 return call_user_func(Smarty
::$_previous_error_handler, $errno, $errstr, $errfile, $errline, $errcontext);
1695 * Enable error handler to mute expected messages
1699 public static function muteExpectedErrors()
1702 error muting is done because some people implemented custom error_handlers using
1703 http://php.net/set_error_handler and for some reason did not understand the following paragraph:
1705 It is important to remember that the standard PHP error handler is completely bypassed for the
1706 error types specified by error_types unless the callback function returns FALSE.
1707 error_reporting() settings will have no effect and your error handler will be called regardless -
1708 however you are still able to read the current value of error_reporting and act appropriately.
1709 Of particular note is that this value will be 0 if the statement that caused the error was
1710 prepended by the @ error-control operator.
1712 Smarty deliberately uses @filemtime() over file_exists() and filemtime() in some places. Reasons include
1713 - @filemtime() is almost twice as fast as using an additional file_exists()
1714 - between file_exists() and filemtime() a possible race condition is opened,
1715 which does not exist using the simple @filemtime() approach.
1717 $error_handler = array('Smarty', 'mutingErrorHandler');
1718 $previous = set_error_handler($error_handler);
1721 if ($previous !== $error_handler) {
1722 Smarty
::$_previous_error_handler = $previous;
1727 * Disable error handler muting expected messages
1731 public static function unmuteExpectedErrors()
1733 restore_error_handler();