4 * Project: Smarty: the PHP compiling template engine
5 * File: Smarty.class.php
6 * SVN: $Id: Smarty.class.php 3895 2010-12-31 13:47:12Z uwe.tews@googlemail.com $
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * For questions, help, comments, discussion, etc., please join the
23 * Smarty mailing list. Send a blank e-mail to
24 * smarty-discussion-subscribe@googlegroups.com
26 * @link http://www.smarty.net/
27 * @copyright 2008 New Digital Group, Inc.
28 * @author Monte Ohrt <monte at ohrt dot com>
35 * define shorthand directory separator constant
38 define('DS', DIRECTORY_SEPARATOR);
42 * set SMARTY_DIR to absolute path to Smarty library files.
43 * Sets SMARTY_DIR only if user application has not already defined it.
45 if (!defined('SMARTY_DIR')) {
46 define('SMARTY_DIR', dirname(__FILE__) . DS);
50 * set SMARTY_SYSPLUGINS_DIR to absolute path to Smarty internal plugins.
51 * Sets SMARTY_SYSPLUGINS_DIR only if user application has not already defined it.
53 if (!defined('SMARTY_SYSPLUGINS_DIR')) {
54 define('SMARTY_SYSPLUGINS_DIR', SMARTY_DIR . 'sysplugins' . DS);
56 if (!defined('SMARTY_PLUGINS_DIR')) {
57 define('SMARTY_PLUGINS_DIR', SMARTY_DIR . 'plugins' . DS);
59 if (!defined('SMARTY_RESOURCE_CHAR_SET')) {
60 define('SMARTY_RESOURCE_CHAR_SET', 'UTF-8');
62 if (!defined('SMARTY_RESOURCE_DATE_FORMAT')) {
63 define('SMARTY_RESOURCE_DATE_FORMAT', '%b %e, %Y');
67 * register the class autoloader
69 if (!defined('SMARTY_SPL_AUTOLOAD')) {
70 define('SMARTY_SPL_AUTOLOAD', 0);
73 if (SMARTY_SPL_AUTOLOAD && set_include_path(get_include_path() . PATH_SEPARATOR . SMARTY_SYSPLUGINS_DIR) !== false) {
74 $registeredAutoLoadFunctions = spl_autoload_functions();
75 if (!isset($registeredAutoLoadFunctions['spl_autoload'])) {
76 spl_autoload_register();
79 spl_autoload_register('smartyAutoload');
83 * This is the main Smarty class
85 class Smarty extends Smarty_Internal_Data {
87 * constant definitions
90 const SMARTY_VERSION = 'Smarty-3.0.7';
91 //define variable scopes
92 const SCOPE_LOCAL = 0;
93 const SCOPE_PARENT = 1;
95 const SCOPE_GLOBAL = 3;
96 // define caching modes
97 const CACHING_OFF = 0;
98 const CACHING_LIFETIME_CURRENT = 1;
99 const CACHING_LIFETIME_SAVED = 2;
100 /** modes for handling of "<?php ... ?>" tags in templates. **/
101 const PHP_PASSTHRU = 0; //-> print tags as plain text
102 const PHP_QUOTE = 1; //-> escape tags as entities
103 const PHP_REMOVE = 2; //-> escape tags as entities
104 const PHP_ALLOW = 3; //-> escape tags as entities
106 const FILTER_POST = 'post';
107 const FILTER_PRE = 'pre';
108 const FILTER_OUTPUT = 'output';
109 const FILTER_VARIABLE = 'variable';
111 const PLUGIN_FUNCTION = 'function';
112 const PLUGIN_BLOCK = 'block';
113 const PLUGIN_COMPILER = 'compiler';
114 const PLUGIN_MODIFIER = 'modifier';
119 // assigned global tpl vars
120 static $global_tpl_vars = array();
125 // auto literal on delimiters with whitspace
126 public $auto_literal = true;
127 // display error on not assigned variables
128 public $error_unassigned = false;
129 // template directory
130 public $template_dir = null;
131 // default template handler
132 public $default_template_handler_func = null;
134 public $compile_dir = null;
136 public $plugins_dir = null;
138 public $cache_dir = null;
140 public $config_dir = null;
141 // force template compiling?
142 public $force_compile = false;
143 // check template for modifications?
144 public $compile_check = true;
145 // locking concurrent compiles
146 public $compile_locking = true;
147 // use sub dirs for compiled/cached files?
148 public $use_sub_dirs = false;
150 public $compile_error = false;
152 public $caching = false;
153 // merge compiled includes
154 public $merge_compiled_includes = false;
156 public $cache_lifetime = 3600;
157 // force cache file creation
158 public $force_cache = false;
160 public $cache_id = null;
162 public $compile_id = null;
163 // template delimiters
164 public $left_delimiter = "{";
165 public $right_delimiter = "}";
167 public $security_class = 'Smarty_Security';
168 public $security_policy = null;
169 public $php_handling = self::PHP_PASSTHRU;
170 public $allow_php_tag = false;
171 public $allow_php_templates = false;
172 public $direct_access_security = true;
173 public $trusted_dir = array();
175 public $debugging = false;
176 public $debugging_ctrl = 'NONE';
177 public $smarty_debug_id = 'SMARTY_DEBUG';
178 public $debug_tpl = null;
179 // When set, smarty does uses this value as error_reporting-level.
180 public $error_reporting = null;
181 // config var settings
182 public $config_overwrite = true; //Controls whether variables with the same name overwrite each other.
183 public $config_booleanize = true; //Controls whether config values of on/true/yes and off/false/no get converted to boolean
184 public $config_read_hidden = false; //Controls whether hidden config sections/vars are read from the file.
186 public $config_vars = array();
188 public $tpl_vars = array();
189 // dummy parent object
190 public $parent = null;
191 // global template functions
192 public $template_functions = array();
193 // resource type used if none given
194 public $default_resource_type = 'file';
196 public $caching_type = 'file';
197 // internal cache resource types
198 public $cache_resource_types = array('file');
199 // internal config properties
200 public $properties = array();
202 public $default_config_type = 'file';
203 // cached template objects
204 public $template_objects = null;
205 // check If-Modified-Since headers
206 public $cache_modified_check = false;
207 // registered plugins
208 public $registered_plugins = array();
209 // plugin search order
210 public $plugin_search_order = array('function', 'block', 'compiler', 'class');
211 // registered objects
212 public $registered_objects = array();
213 // registered classes
214 public $registered_classes = array();
215 // registered filters
216 public $registered_filters = array();
217 // registered resources
218 public $registered_resources = array();
220 public $autoload_filters = array();
221 // status of filter on variable output
222 public $variable_filter = true;
224 public $default_modifiers = array();
225 // global internal smarty vars
226 static $_smarty_vars = array();
227 // start time for execution time calculation
228 public $start_time = 0;
229 // default file permissions
230 public $_file_perms = 0644;
231 // default dir permissions
232 public $_dir_perms = 0771;
233 // block tag hierarchy
234 public $_tag_stack = array();
235 // flag if {block} tag is compiled for template inheritance
236 public $inheritance = false;
237 // generate deprecated function call notices?
238 public $deprecation_notices = true;
240 public $_version = self::SMARTY_VERSION;
241 // self pointer to Smarty object
245 * Class constructor, initializes basic smarty properties
247 public function __construct()
249 // selfpointer need by some other class methods
250 $this->smarty = $this;
251 if (is_callable('mb_internal_encoding')) {
252 mb_internal_encoding(SMARTY_RESOURCE_CHAR_SET);
254 $this->start_time = microtime(true);
256 $this->template_dir = array('.' . DS . 'templates' . DS);
257 $this->compile_dir = '.' . DS . 'templates_c' . DS;
258 $this->plugins_dir = array(SMARTY_PLUGINS_DIR);
259 $this->cache_dir = '.' . DS . 'cache' . DS;
260 $this->config_dir = '.' . DS . 'configs' . DS;
261 $this->debug_tpl = SMARTY_DIR . 'debug.tpl';
262 if (isset($_SERVER['SCRIPT_NAME'])) {
263 $this->assignGlobal('SCRIPT_NAME', $_SERVER['SCRIPT_NAME']);
270 public function __destruct()
275 * fetches a rendered Smarty template
277 * @param string $template the resource handle of the template file or template object
278 * @param mixed $cache_id cache id to be used with this template
279 * @param mixed $compile_id compile id to be used with this template
280 * @param object $ |null $parent next higher level of Smarty variables
281 * @return string rendered template output
283 public function fetch($template, $cache_id = null, $compile_id = null, $parent = null, $display = false)
285 if (!empty($cache_id) && is_object($cache_id)) {
289 if ($parent === null) {
290 // get default Smarty data object
293 // create template object if necessary
294 ($template instanceof $this->template_class)? $_template = $template :
295 $_template = $this->createTemplate ($template, $cache_id, $compile_id, $parent, false);
296 if (isset($this->error_reporting)) {
297 $_smarty_old_error_level = error_reporting($this->error_reporting);
299 // check URL debugging control
300 if (!$this->debugging && $this->debugging_ctrl == 'URL') {
301 if (isset($_SERVER['QUERY_STRING'])) {
302 $_query_string = $_SERVER['QUERY_STRING'];
306 if (false !== strpos($_query_string, $this->smarty_debug_id)) {
307 if (false !== strpos($_query_string, $this->smarty_debug_id . '=on')) {
308 // enable debugging for this browser session
309 setcookie('SMARTY_DEBUG', true);
310 $this->debugging = true;
311 } elseif (false !== strpos($_query_string, $this->smarty_debug_id . '=off')) {
312 // disable debugging for this browser session
313 setcookie('SMARTY_DEBUG', false);
314 $this->debugging = false;
316 // enable debugging for this page
317 $this->debugging = true;
320 if (isset($_COOKIE['SMARTY_DEBUG'])) {
321 $this->debugging = true;
325 // obtain data for cache modified check
326 if ($this->cache_modified_check && $this->caching && $display) {
327 $_isCached = $_template->isCached() && !$_template->has_nocache_code;
329 $_gmt_mtime = gmdate('D, d M Y H:i:s', $_template->getCachedTimestamp()) . ' GMT';
334 // return rendered template
335 if ((!$this->caching || $_template->resource_object->isEvaluated) && (isset($this->autoload_filters['output']) || isset($this->registered_filters['output']))) {
336 $_output = Smarty_Internal_Filter_Handler::runFilter('output', $_template->getRenderedTemplate(), $_template);
338 $_output = $_template->getRenderedTemplate();
340 $_template->rendered_content = null;
341 if (isset($this->error_reporting)) {
342 error_reporting($_smarty_old_error_level);
346 if ($this->caching && $this->cache_modified_check) {
347 $_last_modified_date = @substr($_SERVER['HTTP_IF_MODIFIED_SINCE'], 0, strpos($_SERVER['HTTP_IF_MODIFIED_SINCE'], 'GMT') + 3);
348 if ($_isCached && $_gmt_mtime == $_last_modified_date) {
349 if (php_sapi_name() == 'cgi')
350 header('Status: 304 Not Modified');
352 header('HTTP/1.1 304 Not Modified');
354 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $_template->getCachedTimestamp()) . ' GMT');
361 if ($this->debugging) {
362 Smarty_Internal_Debug::display_debug($this);
366 // return fetched content
372 * displays a Smarty template
374 * @param string $ |object $template the resource handle of the template file or template object
375 * @param mixed $cache_id cache id to be used with this template
376 * @param mixed $compile_id compile id to be used with this template
377 * @param object $parent next higher level of Smarty variables
379 public function display($template, $cache_id = null, $compile_id = null, $parent = null)
382 $this->fetch ($template, $cache_id, $compile_id, $parent, true);
386 * test if cache i valid
388 * @param string $ |object $template the resource handle of the template file or template object
389 * @param mixed $cache_id cache id to be used with this template
390 * @param mixed $compile_id compile id to be used with this template
391 * @param object $parent next higher level of Smarty variables
392 * @return boolean cache status
394 public function isCached($template, $cache_id = null, $compile_id = null, $parent = null)
396 if ($parent === null) {
399 if (!($template instanceof $this->template_class)) {
400 $template = $this->createTemplate ($template, $cache_id, $compile_id, $parent, false);
402 // return cache status of template
403 return $template->isCached();
407 * creates a data object
409 * @param object $parent next higher level of Smarty variables
410 * @returns object data object
412 public function createData($parent = null)
414 return new Smarty_Data($parent, $this);
418 * creates a template object
420 * @param string $template the resource handle of the template file
421 * @param mixed $cache_id cache id to be used with this template
422 * @param mixed $compile_id compile id to be used with this template
423 * @param object $parent next higher level of Smarty variables
424 * @param boolean $do_clone flag is Smarty object shall be cloned
425 * @returns object template object
427 public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null, $do_clone = true)
429 if (!empty($cache_id) && (is_object($cache_id) || is_array($cache_id))) {
433 if (!empty($parent) && is_array($parent)) {
439 if (!is_object($template)) {
440 // we got a template resource
441 // already in template cache?
442 $_templateId = sha1($template . $cache_id . $compile_id);
443 if (isset($this->template_objects[$_templateId]) && $this->caching) {
444 // return cached template object
445 $tpl = $this->template_objects[$_templateId];
447 // create new template object
449 $tpl = new $this->template_class($template, clone $this, $parent, $cache_id, $compile_id);
451 $tpl = new $this->template_class($template, $this, $parent, $cache_id, $compile_id);
455 // just return a copy of template class
458 // fill data if present
459 if (!empty($data) && is_array($data)) {
460 // set up variable values
461 foreach ($data as $_key => $_val) {
462 $tpl->tpl_vars[$_key] = new Smarty_variable($_val);
471 * Check if a template resource exists
473 * @param string $resource_name template name
474 * @return boolean status
476 function templateExists($resource_name)
478 // create template object
479 $save = $this->template_objects;
480 $tpl = new $this->template_class($resource_name, $this);
481 // check if it does exists
482 $result = $tpl->isExisting();
483 $this->template_objects = $save;
488 * Returns a single or all global variables
490 * @param object $smarty
491 * @param string $varname variable name or null
492 * @return string variable value or or array of variables
494 function getGlobal($varname = null)
496 if (isset($varname)) {
497 if (isset(self::$global_tpl_vars[$varname])) {
498 return self::$global_tpl_vars[$varname]->value;
504 foreach (self::$global_tpl_vars AS $key => $var) {
505 $_result[$key] = $var->value;
514 * @param integer $exp_time expiration time
515 * @param string $type resource type
516 * @return integer number of cache files deleted
518 function clearAllCache($exp_time = null, $type = null)
520 // load cache resource and call clearAll
521 return $this->loadCacheResource($type)->clearAll($exp_time);
525 * Empty cache for a specific template
527 * @param string $template_name template name
528 * @param string $cache_id cache id
529 * @param string $compile_id compile id
530 * @param integer $exp_time expiration time
531 * @param string $type resource type
532 * @return integer number of cache files deleted
534 function clearCache($template_name, $cache_id = null, $compile_id = null, $exp_time = null, $type = null)
536 // load cache resource and call clear
537 return $this->loadCacheResource($type)->clear($template_name, $cache_id, $compile_id, $exp_time);
541 * Loads security class and enables security
543 public function enableSecurity($security_class = null)
545 if ($security_class instanceof Smarty_Security) {
546 $this->security_policy = $security_class;
549 if ($security_class == null) {
550 $security_class = $this->security_class;
552 if (class_exists($security_class)) {
553 $this->security_policy = new $security_class($this);
555 throw new SmartyException("Security class '$security_class' is not defined");
562 public function disableSecurity()
564 $this->security_policy = null;
568 * Loads cache resource.
570 * @param string $type cache resource type
571 * @return object of cache resource
573 public function loadCacheResource($type = null) {
575 $type = $this->caching_type;
577 if (in_array($type, $this->cache_resource_types)) {
578 $cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($type);
579 return new $cache_resource_class($this);
583 $cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type);
584 if ($this->loadPlugin($cache_resource_class)) {
585 return new $cache_resource_class($this);
588 throw new SmartyException("Unable to load cache resource '{$type}'");
595 * Set template directory
597 * @param string $ |array $template_dir folder(s) of template sorces
599 public function setTemplateDir($template_dir)
601 $this->template_dir = (array)$template_dir;
606 * Adds template directory(s) to existing ones
608 * @param string $ |array $template_dir folder(s) of template sources
610 public function addTemplateDir($template_dir)
612 $this->template_dir = array_unique(array_merge((array)$this->template_dir, (array)$template_dir));
617 * Adds directory of plugin files
619 * @param object $smarty
620 * @param string $ |array $ plugins folder
623 function addPluginsDir($plugins_dir)
625 $this->plugins_dir = array_unique(array_merge((array)$this->plugins_dir, (array)$plugins_dir));
631 * return a reference to a registered object
633 * @param string $name object name
636 function getRegisteredObject($name)
638 if (!isset($this->registered_objects[$name]))
639 throw new SmartyException("'$name' is not a registered object");
641 if (!is_object($this->registered_objects[$name][0]))
642 throw new SmartyException("registered '$name' is not an object");
644 return $this->registered_objects[$name][0];
649 * return name of debugging template
653 function getDebugTemplate()
655 return $this->debug_tpl;
659 * set the debug template
661 * @param string $tpl_name
664 function setDebugTemplate($tpl_name)
666 return $this->debug_tpl = $tpl_name;
670 * Takes unknown classes and loads plugin files for them
671 * class name format: Smarty_PluginType_PluginName
672 * plugin filename format: plugintype.pluginname.php
674 * @param string $plugin_name class plugin name to load
675 * @return string |boolean filepath of loaded file or false
677 public function loadPlugin($plugin_name, $check = true)
679 // if function or class exists, exit silently (already loaded)
680 if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false)))
682 // Plugin name is expected to be: Smarty_[Type]_[Name]
683 $_plugin_name = strtolower($plugin_name);
684 $_name_parts = explode('_', $_plugin_name, 3);
685 // class name must have three parts to be valid plugin
686 if (count($_name_parts) < 3 || $_name_parts[0] !== 'smarty') {
687 throw new SmartyException("plugin {$plugin_name} is not a valid name format");
690 // if type is "internal", get plugin from sysplugins
691 if ($_name_parts[1] == 'internal') {
692 $file = SMARTY_SYSPLUGINS_DIR . $_plugin_name . '.php';
693 if (file_exists($file)) {
700 // plugin filename is expected to be: [type].[name].php
701 $_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php";
702 // loop through plugin dirs and find the plugin
703 foreach((array)$this->plugins_dir as $_plugin_dir) {
704 if (strpos('/\\', substr($_plugin_dir, -1)) === false) {
707 $file = $_plugin_dir . $_plugin_filename;
708 if (file_exists($file)) {
718 * clean up properties on cloned object
720 public function __clone()
723 $this->config_vars = array();
724 // clear assigned tpl vars
725 $this->tpl_vars = array();
726 // clear objects for external methods
727 unset($this->register);
728 unset($this->filter);
733 * Handle unknown class methods
735 * @param string $name unknown methode name
736 * @param array $args aurgument array
738 public function __call($name, $args)
741 if (!isset($camel_func))
742 $camel_func = create_function('$c', 'return "_" . strtolower($c[1]);');
743 // see if this is a set/get for a property
744 $first3 = strtolower(substr($name, 0, 3));
745 if (in_array($first3, array('set', 'get')) && substr($name, 3, 1) !== '_') {
746 // try to keep case correct for future PHP 6.0 case-sensitive class methods
747 // lcfirst() not available < PHP 5.3.0, so improvise
748 $property_name = strtolower(substr($name, 3, 1)) . substr($name, 4);
749 // convert camel case to underscored name
750 $property_name = preg_replace_callback('/([A-Z])/', $camel_func, $property_name);
751 if (!property_exists($this, $property_name)) {
752 throw new SmartyException("property '$property_name' does not exist.");
755 if ($first3 == 'get')
756 return $this->$property_name;
758 return $this->$property_name = $args[0];
760 // Smarty Backward Compatible wrapper
761 if (strpos($name,'_') !== false) {
762 if (!isset($this->wrapper)) {
763 $this->wrapper = new Smarty_Internal_Wrapper($this);
765 return $this->wrapper->convert($name, $args);
767 // external Smarty methods ?
768 foreach(array('filter','register') as $external) {
769 if (method_exists("Smarty_Internal_{$external}",$name)) {
770 if (!isset($this->$external)) {
771 $class = "Smarty_Internal_{$external}";
772 $this->$external = new $class($this);
774 return call_user_func_array(array($this->$external,$name), $args);
777 if (in_array($name,array('clearCompiledTemplate','compileAllTemplates','compileAllConfig','testInstall','getTags'))) {
778 if (!isset($this->utility)) {
779 $this->utility = new Smarty_Internal_Utility($this);
781 return call_user_func_array(array($this->utility,$name), $args);
783 // PHP4 call to constructor?
784 if (strtolower($name) == 'smarty') {
785 throw new SmartyException('Please use parent::__construct() to call parent constuctor');
788 throw new SmartyException("Call of unknown function '$name'.");
795 function smartyAutoload($class)
797 $_class = strtolower($class);
798 if (substr($_class, 0, 16) === 'smarty_internal_' || $_class == 'smarty_security') {
799 include SMARTY_SYSPLUGINS_DIR . $_class . '.php';
804 * Smarty exception class
806 Class SmartyException extends Exception {
810 * Smarty compiler exception class
812 Class SmartyCompilerException extends SmartyException {