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.24';
117 * define variable scopes
119 const SCOPE_LOCAL
= 0;
121 const SCOPE_PARENT
= 1;
123 const SCOPE_ROOT
= 2;
125 const SCOPE_GLOBAL
= 3;
128 * define caching modes
130 const CACHING_OFF
= 0;
132 const CACHING_LIFETIME_CURRENT
= 1;
134 const CACHING_LIFETIME_SAVED
= 2;
137 * define constant for clearing cache files be saved expiration datees
139 const CLEAR_EXPIRED
= - 1;
142 * define compile check modes
144 const COMPILECHECK_OFF
= 0;
146 const COMPILECHECK_ON
= 1;
148 const COMPILECHECK_CACHEMISS
= 2;
157 const DEBUG_INDIVIDUAL
= 2;
160 * modes for handling of "<?php ... ?>" tags in templates.
162 const PHP_PASSTHRU
= 0; //-> print tags as plain text
164 const PHP_QUOTE
= 1; //-> escape tags as entities
166 const PHP_REMOVE
= 2; //-> escape tags as entities
168 const PHP_ALLOW
= 3; //-> escape tags as entities
173 const FILTER_POST
= 'post';
175 const FILTER_PRE
= 'pre';
177 const FILTER_OUTPUT
= 'output';
179 const FILTER_VARIABLE
= 'variable';
184 const PLUGIN_FUNCTION
= 'function';
186 const PLUGIN_BLOCK
= 'block';
188 const PLUGIN_COMPILER
= 'compiler';
190 const PLUGIN_MODIFIER
= 'modifier';
192 const PLUGIN_MODIFIERCOMPILER
= 'modifiercompiler';
197 * assigned global tpl vars
199 public static $global_tpl_vars = array();
202 * error handler returned by set_error_hanlder() in Smarty::muteExpectedErrors()
204 public static $_previous_error_handler = null;
207 * contains directories outside of SMARTY_DIR that are to be muted by muteExpectedErrors()
209 public static $_muted_directories = array('./templates_c/' => null,
213 * Flag denoting if Multibyte String functions are available
215 public static $_MBSTRING = SMARTY_MBSTRING
;
218 * The character set to adhere to (e.g. "UTF-8")
220 public static $_CHARSET = SMARTY_RESOURCE_CHAR_SET
;
223 * The date format to be used internally
224 * (accepts date() and strftime())
226 public static $_DATE_FORMAT = SMARTY_RESOURCE_DATE_FORMAT
;
229 * Flag denoting if PCRE should run in UTF-8 mode
231 public static $_UTF8_MODIFIER = 'u';
234 * Flag denoting if operating system is windows
236 public static $_IS_WINDOWS = false;
243 * auto literal on delimiters with whitspace
247 public $auto_literal = true;
250 * display error on not assigned variables
254 public $error_unassigned = false;
257 * look up relative filepaths in include_path
261 public $use_include_path = false;
268 private $template_dir = array('./templates/');
271 * joined template directory string used in cache keys
275 public $joined_template_dir = './templates/';
278 * joined config directory string used in cache keys
282 public $joined_config_dir = './configs/';
285 * default template handler
289 public $default_template_handler_func = null;
292 * default config handler
296 public $default_config_handler_func = null;
299 * default plugin handler
303 public $default_plugin_handler_func = null;
310 private $compile_dir = './templates_c/';
317 private $plugins_dir = null;
324 private $cache_dir = './cache/';
331 private $config_dir = array('./configs/');
334 * force template compiling?
338 public $force_compile = false;
341 * check template for modifications?
345 public $compile_check = true;
348 * use sub dirs for compiled/cached files?
352 public $use_sub_dirs = false;
355 * allow ambiguous resources (that are made unique by the resource handler)
359 public $allow_ambiguous_resources = false;
362 * merge compiled includes
366 public $merge_compiled_includes = false;
369 * template inheritance merge compiled includes
373 public $inheritance_merge_compiled_includes = true;
376 * force cache file creation
380 public $force_cache = false;
383 * template left-delimiter
387 public $left_delimiter = "{";
390 * template right-delimiter
394 public $right_delimiter = "}";
401 * This should be instance of Smarty_Security.
404 * @see Smarty_Security
406 public $security_class = 'Smarty_Security';
409 * implementation of security class
411 * @var Smarty_Security
413 public $security_policy = null;
416 * controls handling of PHP-blocks
420 public $php_handling = self
::PHP_PASSTHRU
;
423 * controls if the php template file resource is allowed
427 public $allow_php_templates = false;
430 * Should compiled-templates be prevented from being called directly?
432 * Currently used by Smarty_Internal_Template only.
437 public $direct_access_security = true;
442 * Setting this to true enables the debug-console.
446 public $debugging = false;
449 * This determines if debugging is enable-able from the browser.
451 * <li>NONE => no debugging control allowed</li>
452 * <li>URL => enable debugging when SMARTY_DEBUG is found in the URL.</li>
457 public $debugging_ctrl = 'NONE';
460 * Name of debugging URL-param.
461 * Only used when $debugging_ctrl is set to 'URL'.
462 * The name of the URL-parameter that activates debugging.
466 public $smarty_debug_id = 'SMARTY_DEBUG';
469 * Path of debug template.
473 public $debug_tpl = null;
476 * When set, smarty uses this value as error_reporting-level.
480 public $error_reporting = null;
483 * Internal flag for getTags()
487 public $get_used_tags = false;
490 * config var settings
494 * Controls whether variables with the same name overwrite each other.
498 public $config_overwrite = true;
501 * Controls whether config values of on/true/yes and off/false/no get converted to boolean.
505 public $config_booleanize = true;
508 * Controls whether hidden config sections/vars are read from the file.
512 public $config_read_hidden = false;
521 * locking concurrent compiles
525 public $compile_locking = true;
528 * Controls whether cache resources should emply locking mechanism
532 public $cache_locking = false;
535 * seconds to wait for acquiring a lock before ignoring the write lock
539 public $locking_timeout = 10;
544 * resource type used if none given
545 * Must be an valid key of $registered_resources.
549 public $default_resource_type = 'file';
553 * Must be an element of $cache_resource_types.
557 public $caching_type = 'file';
560 * internal config properties
564 public $properties = array();
571 public $default_config_type = 'file';
574 * cached template objects
578 public $source_objects = array();
581 * cached template objects
585 public $template_objects = array();
588 * enable resource caching
592 public $resource_caching = false;
595 * enable template resource caching
599 public $template_resource_caching = true;
602 * check If-Modified-Since headers
606 public $cache_modified_check = false;
613 public $registered_plugins = array();
616 * plugin search order
620 public $plugin_search_order = array('function',
630 public $registered_objects = array();
637 public $registered_classes = array();
644 public $registered_filters = array();
647 * registered resources
651 public $registered_resources = array();
654 * resource handler cache
658 public $_resource_handlers = array();
661 * registered cache resources
665 public $registered_cache_resources = array();
668 * cache resource handler cache
672 public $_cacheresource_handlers = array();
679 public $autoload_filters = array();
686 public $default_modifiers = array();
689 * autoescape variable output
693 public $escape_html = false;
696 * global internal smarty vars
700 public static $_smarty_vars = array();
703 * start time for execution time calculation
707 public $start_time = 0;
710 * default file permissions
714 public $_file_perms = 0644;
717 * default dir permissions
721 public $_dir_perms = 0771;
724 * block tag hierarchy
728 public $_tag_stack = array();
731 * required by the compiler for BC
735 public $_current_file = null;
738 * internal flag to enable parser debugging
742 public $_parserdebug = false;
745 * Cache of is_file results of loadPlugin()
749 public $_is_file_cache = array();
754 * Initialize new Smarty object
756 public function __construct()
758 if (is_callable('mb_internal_encoding')) {
759 mb_internal_encoding(Smarty
::$_CHARSET);
761 $this->start_time
= microtime(true);
762 // check default dirs for overloading
763 if ($this->template_dir
[0] !== './templates/' ||
isset($this->template_dir
[1])) {
764 $this->setTemplateDir($this->template_dir
);
766 if ($this->config_dir
[0] !== './configs/' ||
isset($this->config_dir
[1])) {
767 $this->setConfigDir($this->config_dir
);
769 if ($this->compile_dir
!== './templates_c/') {
770 unset(self
::$_muted_directories['./templates_c/']);
771 $this->setCompileDir($this->compile_dir
);
773 if ($this->cache_dir
!== './cache/') {
774 unset(self
::$_muted_directories['./cache/']);
775 $this->setCacheDir($this->cache_dir
);
777 if (isset($this->plugins_dir
)) {
778 $this->setPluginsDir($this->plugins_dir
);
780 $this->setPluginsDir(SMARTY_PLUGINS_DIR
);
782 if (isset($_SERVER['SCRIPT_NAME'])) {
783 Smarty
::$global_tpl_vars['SCRIPT_NAME'] = new Smarty_Variable($_SERVER['SCRIPT_NAME']);
786 // Check if we're running on windows
787 Smarty
::$_IS_WINDOWS = strtoupper(substr(PHP_OS
, 0, 3)) === 'WIN';
789 // let PCRE (preg_*) treat strings as ISO-8859-1 if we're not dealing with UTF-8
790 if (Smarty
::$_CHARSET !== 'UTF-8') {
791 Smarty
::$_UTF8_MODIFIER = '';
796 * fetches a rendered Smarty template
798 * @param string $template the resource handle of the template file or template object
799 * @param mixed $cache_id cache id to be used with this template
800 * @param mixed $compile_id compile id to be used with this template
801 * @param object $parent next higher level of Smarty variables
802 * @param bool $display true: display, false: fetch
803 * @param bool $merge_tpl_vars not used - left for BC
804 * @param bool $no_output_filter not used - left for BC
807 * @throws SmartyException
808 * @return string rendered template output
810 public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null, $display = false, $merge_tpl_vars = true, $no_output_filter = false)
812 if ($cache_id !== null && is_object($cache_id)) {
816 if ($parent === null) {
819 // get template object
820 $_template = is_object($template) ?
$template : $this->createTemplate($template, $cache_id, $compile_id, $parent, false);
821 // set caching in template object
822 $_template->caching
= $this->caching
;
823 // fetch template content
824 return $_template->render(true, false, $display);
828 * displays a Smarty template
830 * @param string $template the resource handle of the template file or template object
831 * @param mixed $cache_id cache id to be used with this template
832 * @param mixed $compile_id compile id to be used with this template
833 * @param object $parent next higher level of Smarty variables
835 public function display($template = null, $cache_id = null, $compile_id = null, $parent = null)
838 $this->fetch($template, $cache_id, $compile_id, $parent, true);
842 * Check if a template resource exists
844 * @param string $resource_name template name
846 * @return boolean status
848 public function templateExists($resource_name)
850 // create template object
851 $save = $this->template_objects
;
852 $tpl = new $this->template_class($resource_name, $this);
853 // check if it does exists
854 $result = $tpl->source
->exists
;
855 $this->template_objects
= $save;
861 * Returns a single or all global variables
863 * @param string $varname variable name or null
865 * @return string variable value or or array of variables
867 public function getGlobal($varname = null)
869 if (isset($varname)) {
870 if (isset(self
::$global_tpl_vars[$varname])) {
871 return self
::$global_tpl_vars[$varname]->value
;
877 foreach (self
::$global_tpl_vars AS $key => $var) {
878 $_result[$key] = $var->value
;
888 * @param integer $exp_time expiration time
889 * @param string $type resource type
891 * @return integer number of cache files deleted
893 public function clearAllCache($exp_time = null, $type = null)
895 // load cache resource and call clearAll
896 $_cache_resource = Smarty_CacheResource
::load($this, $type);
897 Smarty_CacheResource
::invalidLoadedCache($this);
899 return $_cache_resource->clearAll($this, $exp_time);
903 * Empty cache for a specific template
905 * @param string $template_name template name
906 * @param string $cache_id cache id
907 * @param string $compile_id compile id
908 * @param integer $exp_time expiration time
909 * @param string $type resource type
911 * @return integer number of cache files deleted
913 public function clearCache($template_name, $cache_id = null, $compile_id = null, $exp_time = null, $type = null)
915 // load cache resource and call clear
916 $_cache_resource = Smarty_CacheResource
::load($this, $type);
917 Smarty_CacheResource
::invalidLoadedCache($this);
919 return $_cache_resource->clear($this, $template_name, $cache_id, $compile_id, $exp_time);
923 * Loads security class and enables security
925 * @param string|Smarty_Security $security_class if a string is used, it must be class-name
927 * @return Smarty current Smarty instance for chaining
928 * @throws SmartyException when an invalid class name is provided
930 public function enableSecurity($security_class = null)
932 if ($security_class instanceof Smarty_Security
) {
933 $this->security_policy
= $security_class;
936 } elseif (is_object($security_class)) {
937 throw new SmartyException("Class '" . get_class($security_class) . "' must extend Smarty_Security.");
939 if ($security_class == null) {
940 $security_class = $this->security_class
;
942 if (!class_exists($security_class)) {
943 throw new SmartyException("Security class '$security_class' is not defined");
944 } elseif ($security_class !== 'Smarty_Security' && !is_subclass_of($security_class, 'Smarty_Security')) {
945 throw new SmartyException("Class '$security_class' must extend Smarty_Security.");
947 $this->security_policy
= new $security_class($this);
956 * @return Smarty current Smarty instance for chaining
958 public function disableSecurity()
960 $this->security_policy
= null;
966 * Set template directory
968 * @param string|array $template_dir directory(s) of template sources
970 * @return Smarty current Smarty instance for chaining
972 public function setTemplateDir($template_dir)
974 $this->template_dir
= array();
975 foreach ((array) $template_dir as $k => $v) {
976 $this->template_dir
[$k] = rtrim(strtr($v, '\\', '/'), '/') . '/';
978 $this->joined_template_dir
= join(' # ', $this->template_dir
);
983 * Add template directory(s)
985 * @param string|array $template_dir directory(s) of template sources
986 * @param string $key of the array element to assign the template dir to
988 * @return Smarty current Smarty instance for chaining
989 * @throws SmartyException when the given template directory is not valid
991 public function addTemplateDir($template_dir, $key = null)
993 $this->_addDir('template_dir', $template_dir, $key);
994 $this->joined_template_dir
= join(' # ', $this->template_dir
);
999 * Get template directories
1001 * @param mixed $index index of directory to get, null to get all
1003 * @return array|string list of template directories, or directory of $index
1005 public function getTemplateDir($index = null)
1007 if ($index !== null) {
1008 return isset($this->template_dir
[$index]) ?
$this->template_dir
[$index] : null;
1010 return (array) $this->template_dir
;
1014 * Set config directory
1016 * @param $config_dir
1018 * @return Smarty current Smarty instance for chaining
1020 public function setConfigDir($config_dir)
1022 $this->config_dir
= array();
1023 foreach ((array) $config_dir as $k => $v) {
1024 $this->config_dir
[$k] = rtrim(strtr($v, '\\', '/'), '/') . '/';
1026 $this->joined_config_dir
= join(' # ', $this->config_dir
);
1031 * Add config directory(s)
1033 * @param string|array $config_dir directory(s) of config sources
1034 * @param mixed $key key of the array element to assign the config dir to
1036 * @return Smarty current Smarty instance for chaining
1038 public function addConfigDir($config_dir, $key = null)
1040 $this->_addDir('config_dir', $config_dir, $key);
1041 $this->joined_config_dir
= join(' # ', $this->config_dir
);
1046 * Get config directory
1048 * @param mixed $index index of directory to get, null to get all
1050 * @return array|string configuration directory
1052 public function getConfigDir($index = null)
1054 if ($index !== null) {
1055 return isset($this->config_dir
[$index]) ?
$this->config_dir
[$index] : null;
1057 return (array) $this->config_dir
;
1061 * Set plugins directory
1063 * @param string|array $plugins_dir directory(s) of plugins
1065 * @return Smarty current Smarty instance for chaining
1067 public function setPluginsDir($plugins_dir)
1069 $this->plugins_dir
= array();
1070 foreach ((array) $plugins_dir as $k => $v) {
1071 $this->plugins_dir
[$k] = rtrim(strtr($v, '\\', '/'), '/') . '/';
1073 $this->_is_file_cache
= array();
1078 * Adds directory of plugin files
1080 * @param $plugins_dir
1082 * @return Smarty current Smarty instance for chaining
1084 public function addPluginsDir($plugins_dir)
1086 $this->_addDir('plugins_dir', $plugins_dir);
1087 $this->plugins_dir
= array_unique($this->plugins_dir
);
1088 $this->_is_file_cache
= array();
1093 * Get plugin directories
1095 * @return array list of plugin directories
1097 public function getPluginsDir()
1099 return (array) $this->plugins_dir
;
1103 * Set compile directory
1105 * @param string $compile_dir directory to store compiled templates in
1107 * @return Smarty current Smarty instance for chaining
1109 public function setCompileDir($compile_dir)
1111 $this->compile_dir
= rtrim(strtr($compile_dir, '\\', '/'), '/') . '/';
1112 if (!isset(Smarty
::$_muted_directories[$this->compile_dir
])) {
1113 Smarty
::$_muted_directories[$this->compile_dir
] = null;
1120 * Get compiled directory
1122 * @return string path to compiled templates
1124 public function getCompileDir()
1126 return $this->compile_dir
;
1130 * Set cache directory
1132 * @param string $cache_dir directory to store cached templates in
1134 * @return Smarty current Smarty instance for chaining
1136 public function setCacheDir($cache_dir)
1138 $this->cache_dir
= rtrim(strtr($cache_dir, '\\', '/'), '/') . '/';
1139 if (!isset(Smarty
::$_muted_directories[$this->cache_dir
])) {
1140 Smarty
::$_muted_directories[$this->cache_dir
] = null;
1147 * Get cache directory
1149 * @return string path of cache directory
1151 public function getCacheDir()
1153 return $this->cache_dir
;
1157 * add directories to given property name
1159 * @param string $dirName directory property name
1160 * @param string|array $dir directory string or array of strings
1161 * @param mixed $key optional key
1163 private function _addDir($dirName, $dir, $key = null)
1165 // make sure we're dealing with an array
1166 $this->$dirName = (array) $this->$dirName;
1168 if (is_array($dir)) {
1169 foreach ($dir as $k => $v) {
1170 $v = rtrim(strtr($v, '\\', '/'), '/') . '/';
1172 // indexes are not merged but appended
1173 $this->{$dirName}[] = $v;
1175 // string indexes are overridden
1176 $this->{$dirName}[$k] = $v;
1180 $v = rtrim(strtr($dir, '\\', '/'), '/') . '/';
1181 if ($key !== null) {
1182 // override directory at specified index
1183 $this->{$dirName}[$key] = $v;
1185 // append new directory
1186 $this->{$dirName}[] = $v;
1192 * Set default modifiers
1194 * @param array|string $modifiers modifier or list of modifiers to set
1196 * @return Smarty current Smarty instance for chaining
1198 public function setDefaultModifiers($modifiers)
1200 $this->default_modifiers
= (array) $modifiers;
1206 * Add default modifiers
1208 * @param array|string $modifiers modifier or list of modifiers to add
1210 * @return Smarty current Smarty instance for chaining
1212 public function addDefaultModifiers($modifiers)
1214 if (is_array($modifiers)) {
1215 $this->default_modifiers
= array_merge($this->default_modifiers
, $modifiers);
1217 $this->default_modifiers
[] = $modifiers;
1224 * Get default modifiers
1226 * @return array list of default modifiers
1228 public function getDefaultModifiers()
1230 return $this->default_modifiers
;
1234 * Set autoload filters
1236 * @param array $filters filters to load automatically
1237 * @param string $type "pre", "output", … specify the filter type to set. Defaults to none treating $filters'
1238 * keys as the appropriate types
1240 * @return Smarty current Smarty instance for chaining
1242 public function setAutoloadFilters($filters, $type = null)
1244 if ($type !== null) {
1245 $this->autoload_filters
[$type] = (array) $filters;
1247 $this->autoload_filters
= (array) $filters;
1254 * Add autoload filters
1256 * @param array $filters filters to load automatically
1257 * @param string $type "pre", "output", … specify the filter type to set. Defaults to none treating $filters'
1258 * keys as the appropriate types
1260 * @return Smarty current Smarty instance for chaining
1262 public function addAutoloadFilters($filters, $type = null)
1264 if ($type !== null) {
1265 if (!empty($this->autoload_filters
[$type])) {
1266 $this->autoload_filters
[$type] = array_merge($this->autoload_filters
[$type], (array) $filters);
1268 $this->autoload_filters
[$type] = (array) $filters;
1271 foreach ((array) $filters as $key => $value) {
1272 if (!empty($this->autoload_filters
[$key])) {
1273 $this->autoload_filters
[$key] = array_merge($this->autoload_filters
[$key], (array) $value);
1275 $this->autoload_filters
[$key] = (array) $value;
1284 * Get autoload filters
1286 * @param string $type type of filter to get autoloads for. Defaults to all autoload filters
1288 * @return array array( 'type1' => array( 'filter1', 'filter2', … ) ) or array( 'filter1', 'filter2', …) if $type
1291 public function getAutoloadFilters($type = null)
1293 if ($type !== null) {
1294 return isset($this->autoload_filters
[$type]) ?
$this->autoload_filters
[$type] : array();
1297 return $this->autoload_filters
;
1301 * return name of debugging template
1305 public function getDebugTemplate()
1307 return $this->debug_tpl
;
1311 * set the debug template
1313 * @param string $tpl_name
1315 * @return Smarty current Smarty instance for chaining
1316 * @throws SmartyException if file is not readable
1318 public function setDebugTemplate($tpl_name)
1320 if (!is_readable($tpl_name)) {
1321 throw new SmartyException("Unknown file '{$tpl_name}'");
1323 $this->debug_tpl
= $tpl_name;
1329 * creates a template object
1331 * @param string $template the resource handle of the template file
1332 * @param mixed $cache_id cache id to be used with this template
1333 * @param mixed $compile_id compile id to be used with this template
1334 * @param object $parent next higher level of Smarty variables
1335 * @param boolean $do_clone flag is Smarty object shall be cloned
1337 * @return object template object
1339 public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null, $do_clone = true)
1341 if ($cache_id !== null && (is_object($cache_id) ||
is_array($cache_id))) {
1342 $parent = $cache_id;
1345 if ($parent !== null && is_array($parent)) {
1351 $_templateId = $this->getTemplateId($template, $cache_id, $compile_id);
1352 if (isset($this->template_objects
[$_templateId])) {
1354 $tpl = clone $this->template_objects
[$_templateId];
1355 $tpl->smarty
= clone $tpl->smarty
;
1357 $tpl = $this->template_objects
[$_templateId];
1359 $tpl->parent
= $parent;
1360 $tpl->tpl_vars
= array();
1361 $tpl->config_vars
= array();
1363 $tpl = new $this->template_class($template, $this, $parent, $cache_id, $compile_id);
1365 $tpl->smarty
= clone $tpl->smarty
;
1367 $tpl->templateId
= $_templateId;
1369 // fill data if present
1370 if (!empty($data) && is_array($data)) {
1371 // set up variable values
1372 foreach ($data as $_key => $_val) {
1373 $tpl->tpl_vars
[$_key] = new Smarty_Variable($_val);
1376 if ($this->debugging
) {
1377 Smarty_Internal_Debug
::register_template($tpl);
1383 * Takes unknown classes and loads plugin files for them
1384 * class name format: Smarty_PluginType_PluginName
1385 * plugin filename format: plugintype.pluginname.php
1387 * @param string $plugin_name class plugin name to load
1388 * @param bool $check check if already loaded
1390 * @throws SmartyException
1391 * @return string |boolean filepath of loaded file or false
1393 public function loadPlugin($plugin_name, $check = true)
1395 // if function or class exists, exit silently (already loaded)
1396 if ($check && (is_callable($plugin_name) ||
class_exists($plugin_name, false))) {
1399 // Plugin name is expected to be: Smarty_[Type]_[Name]
1400 $_name_parts = explode('_', $plugin_name, 3);
1401 // class name must have three parts to be valid plugin
1402 // count($_name_parts) < 3 === !isset($_name_parts[2])
1403 if (!isset($_name_parts[2]) ||
strtolower($_name_parts[0]) !== 'smarty') {
1404 throw new SmartyException("plugin {$plugin_name} is not a valid name format");
1406 // if type is "internal", get plugin from sysplugins
1407 if (strtolower($_name_parts[1]) == 'internal') {
1408 $file = SMARTY_SYSPLUGINS_DIR
. strtolower($plugin_name) . '.php';
1409 if (isset($this->_is_file_cache
[$file]) ?
$this->_is_file_cache
[$file] : $this->_is_file_cache
[$file] = is_file($file)) {
1410 require_once($file);
1416 // plugin filename is expected to be: [type].[name].php
1417 $_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php";
1419 $_stream_resolve_include_path = function_exists('stream_resolve_include_path');
1421 // loop through plugin dirs and find the plugin
1422 foreach ($this->getPluginsDir() as $_plugin_dir) {
1423 $names = array($_plugin_dir . $_plugin_filename,
1424 $_plugin_dir . strtolower($_plugin_filename),);
1425 foreach ($names as $file) {
1426 if (isset($this->_is_file_cache
[$file]) ?
$this->_is_file_cache
[$file] : $this->_is_file_cache
[$file] = is_file($file)) {
1427 require_once($file);
1430 if ($this->use_include_path
&& !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_plugin_dir)) {
1431 // try PHP include_path
1432 if ($_stream_resolve_include_path) {
1433 $file = stream_resolve_include_path($file);
1435 $file = Smarty_Internal_Get_Include_Path
::getIncludePath($file);
1438 if ($file !== false) {
1439 require_once($file);
1451 * Compile all template files
1453 * @param string $extension file extension
1454 * @param bool $force_compile force all to recompile
1455 * @param int $time_limit
1456 * @param int $max_errors
1458 * @return integer number of template files recompiled
1460 public function compileAllTemplates($extension = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null)
1462 return Smarty_Internal_Utility
::compileAllTemplates($extension, $force_compile, $time_limit, $max_errors, $this);
1466 * Compile all config files
1468 * @param string $extension file extension
1469 * @param bool $force_compile force all to recompile
1470 * @param int $time_limit
1471 * @param int $max_errors
1473 * @return integer number of template files recompiled
1475 public function compileAllConfig($extension = '.conf', $force_compile = false, $time_limit = 0, $max_errors = null)
1477 return Smarty_Internal_Utility
::compileAllConfig($extension, $force_compile, $time_limit, $max_errors, $this);
1481 * Delete compiled template file
1483 * @param string $resource_name template name
1484 * @param string $compile_id compile id
1485 * @param integer $exp_time expiration time
1487 * @return integer number of template files deleted
1489 public function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
1491 return Smarty_Internal_Utility
::clearCompiledTemplate($resource_name, $compile_id, $exp_time, $this);
1495 * Return array of tag/attributes of all tags used by an template
1497 * @param Smarty_Internal_Template $template
1499 * @return array of tag/attributes
1501 public function getTags(Smarty_Internal_Template
$template)
1503 return Smarty_Internal_Utility
::getTags($template);
1507 * Run installation test
1509 * @param array $errors Array to write errors into, rather than outputting them
1511 * @return boolean true if setup is fine, false if something is wrong
1513 public function testInstall(&$errors = null)
1515 return Smarty_Internal_TestInstall
::testInstall($this, $errors);
1519 * @param boolean $compile_check
1521 public function setCompileCheck($compile_check)
1523 $this->compile_check
= $compile_check;
1527 * @param boolean $use_sub_dirs
1529 public function setUseSubDirs($use_sub_dirs)
1531 $this->use_sub_dirs
= $use_sub_dirs;
1535 * @param boolean $caching
1537 public function setCaching($caching)
1539 $this->caching
= $caching;
1543 * @param int $cache_lifetime
1545 public function setCacheLifetime($cache_lifetime)
1547 $this->cache_lifetime
= $cache_lifetime;
1551 * @param string $compile_id
1553 public function setCompileId($compile_id)
1555 $this->compile_id
= $compile_id;
1559 * @param string $cache_id
1561 public function setCacheId($cache_id)
1563 $this->cache_id
= $cache_id;
1567 * @param int $error_reporting
1569 public function setErrorReporting($error_reporting)
1571 $this->error_reporting
= $error_reporting;
1575 * @param boolean $escape_html
1577 public function setEscapeHtml($escape_html)
1579 $this->escape_html
= $escape_html;
1583 * @param boolean $auto_literal
1585 public function setAutoLiteral($auto_literal)
1587 $this->auto_literal
= $auto_literal;
1591 * @param boolean $force_compile
1593 public function setForceCompile($force_compile)
1595 $this->force_compile
= $force_compile;
1599 * @param boolean $merge_compiled_includes
1601 public function setMergeCompiledIncludes($merge_compiled_includes)
1603 $this->merge_compiled_includes
= $merge_compiled_includes;
1607 * @param string $left_delimiter
1609 public function setLeftDelimiter($left_delimiter)
1611 $this->left_delimiter
= $left_delimiter;
1615 * @param string $right_delimiter
1617 public function setRightDelimiter($right_delimiter)
1619 $this->right_delimiter
= $right_delimiter;
1623 * @param boolean $debugging
1625 public function setDebugging($debugging)
1627 $this->debugging
= $debugging;
1631 * @param boolean $config_overwrite
1633 public function setConfigOverwrite($config_overwrite)
1635 $this->config_overwrite
= $config_overwrite;
1639 * @param boolean $config_booleanize
1641 public function setConfigBooleanize($config_booleanize)
1643 $this->config_booleanize
= $config_booleanize;
1647 * @param boolean $config_read_hidden
1649 public function setConfigReadHidden($config_read_hidden)
1651 $this->config_read_hidden
= $config_read_hidden;
1655 * @param boolean $compile_locking
1657 public function setCompileLocking($compile_locking)
1659 $this->compile_locking
= $compile_locking;
1665 public function __destruct()
1667 // intentionally left blank
1671 * <<magic>> Generic getter.
1672 * Calls the appropriate getter function.
1673 * Issues an E_USER_NOTICE if no valid getter is found.
1675 * @param string $name property name
1679 public function __get($name)
1681 $allowed = array('template_dir' => 'getTemplateDir',
1682 'config_dir' => 'getConfigDir',
1683 'plugins_dir' => 'getPluginsDir',
1684 'compile_dir' => 'getCompileDir',
1685 'cache_dir' => 'getCacheDir',);
1687 if (isset($allowed[$name])) {
1688 return $this->{$allowed[$name]}();
1690 trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE
);
1695 * <<magic>> Generic setter.
1696 * Calls the appropriate setter function.
1697 * Issues an E_USER_NOTICE if no valid setter is found.
1699 * @param string $name property name
1700 * @param mixed $value parameter passed to setter
1702 public function __set($name, $value)
1704 $allowed = array('template_dir' => 'setTemplateDir',
1705 'config_dir' => 'setConfigDir',
1706 'plugins_dir' => 'setPluginsDir',
1707 'compile_dir' => 'setCompileDir',
1708 'cache_dir' => 'setCacheDir',);
1710 if (isset($allowed[$name])) {
1711 $this->{$allowed[$name]}($value);
1713 trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE
);
1718 * Error Handler to mute expected messages
1720 * @link http://php.net/set_error_handler
1722 * @param integer $errno Error level
1726 * @param $errcontext
1730 public static function mutingErrorHandler($errno, $errstr, $errfile, $errline, $errcontext)
1732 $_is_muted_directory = false;
1734 // add the SMARTY_DIR to the list of muted directories
1735 if (!isset(Smarty
::$_muted_directories[SMARTY_DIR
])) {
1736 $smarty_dir = realpath(SMARTY_DIR
);
1737 if ($smarty_dir !== false) {
1738 Smarty
::$_muted_directories[SMARTY_DIR
] = array('file' => $smarty_dir,
1739 'length' => strlen($smarty_dir),);
1743 // walk the muted directories and test against $errfile
1744 foreach (Smarty
::$_muted_directories as $key => &$dir) {
1746 // resolve directory and length for speedy comparisons
1747 $file = realpath($key);
1748 if ($file === false) {
1749 // this directory does not exist, remove and skip it
1750 unset(Smarty
::$_muted_directories[$key]);
1753 $dir = array('file' => $file,
1754 'length' => strlen($file),);
1756 if (!strncmp($errfile, $dir['file'], $dir['length'])) {
1757 $_is_muted_directory = true;
1762 // pass to next error handler if this error did not occur inside SMARTY_DIR
1763 // or the error was within smarty but masked to be ignored
1764 if (!$_is_muted_directory ||
($errno && $errno & error_reporting())) {
1765 if (Smarty
::$_previous_error_handler) {
1766 return call_user_func(Smarty
::$_previous_error_handler, $errno, $errstr, $errfile, $errline, $errcontext);
1774 * Enable error handler to mute expected messages
1778 public static function muteExpectedErrors()
1781 error muting is done because some people implemented custom error_handlers using
1782 http://php.net/set_error_handler and for some reason did not understand the following paragraph:
1784 It is important to remember that the standard PHP error handler is completely bypassed for the
1785 error types specified by error_types unless the callback function returns FALSE.
1786 error_reporting() settings will have no effect and your error handler will be called regardless -
1787 however you are still able to read the current value of error_reporting and act appropriately.
1788 Of particular note is that this value will be 0 if the statement that caused the error was
1789 prepended by the @ error-control operator.
1791 Smarty deliberately uses @filemtime() over file_exists() and filemtime() in some places. Reasons include
1792 - @filemtime() is almost twice as fast as using an additional file_exists()
1793 - between file_exists() and filemtime() a possible race condition is opened,
1794 which does not exist using the simple @filemtime() approach.
1796 $error_handler = array('Smarty',
1797 'mutingErrorHandler');
1798 $previous = set_error_handler($error_handler);
1801 if ($previous !== $error_handler) {
1802 Smarty
::$_previous_error_handler = $previous;
1807 * Disable error handler muting expected messages
1811 public static function unmuteExpectedErrors()
1813 restore_error_handler();