php封装的smarty类完整实例
程序员文章站
2024-03-02 14:37:04
本文实例讲述了php封装的smarty类。分享给大家供大家参考,具体如下:
本文实例讲述了php封装的smarty类。分享给大家供大家参考,具体如下:
<?php /** * project: smarty: the php compiling template engine * file: smarty.class.php * svn: $id: smarty.class.php 4848 2014-06-08 18:12:09z uwe.tews@googlemail.com $ * this library is free software; you can redistribute it and/or * modify it under the terms of the gnu lesser general public * license as published by the free software foundation; either * version 2.1 of the license, or (at your option) any later version. * this library is distributed in the hope that it will be useful, * but without any warranty; without even the implied warranty of * merchantability or fitness for a particular purpose. see the gnu * lesser general public license for more details. * you should have received a copy of the gnu lesser general public * license along with this library; if not, write to the free software * foundation, inc., 59 temple place, suite 330, boston, ma 02111-1307 usa * for questions, help, comments, discussion, etc., please join the * smarty mailing list. send a blank e-mail to * smarty-discussion-subscribe@googlegroups.com * * @link http://www.smarty.net/ * @copyright 2008 new digital group, inc. * @author monte ohrt <monte at ohrt dot com> * @author uwe tews * @author rodney rehm * @package smarty * @version 3.1.19 */ /** * define shorthand directory separator constant */ if (!defined('ds')) { define('ds', directory_separator); } /** * set smarty_dir to absolute path to smarty library files. * sets smarty_dir only if user application has not already defined it. */ if (!defined('smarty_dir')) { define('smarty_dir', dirname(__file__) . ds); } /** * set smarty_sysplugins_dir to absolute path to smarty internal plugins. * sets smarty_sysplugins_dir only if user application has not already defined it. */ if (!defined('smarty_sysplugins_dir')) { define('smarty_sysplugins_dir', smarty_dir . 'sysplugins' . ds); } if (!defined('smarty_plugins_dir')) { define('smarty_plugins_dir', smarty_dir . 'plugins' . ds); } if (!defined('smarty_mbstring')) { define('smarty_mbstring', function_exists('mb_split')); } if (!defined('smarty_resource_char_set')) { // utf-8 can only be done properly when mbstring is available! /** * @deprecated in favor of smarty::$_charset */ define('smarty_resource_char_set', smarty_mbstring ? 'utf-8' : 'iso-8859-1'); } if (!defined('smarty_resource_date_format')) { /** * @deprecated in favor of smarty::$_date_format */ define('smarty_resource_date_format', '%b %e, %y'); } /** * register the class autoloader */ if (!defined('smarty_spl_autoload')) { define('smarty_spl_autoload', 0); } if (smarty_spl_autoload && set_include_path(get_include_path() . path_separator . smarty_sysplugins_dir) !== false) { $registeredautoloadfunctions = spl_autoload_functions(); if (!isset($registeredautoloadfunctions['spl_autoload'])) { spl_autoload_register(); } } else { spl_autoload_register('smartyautoload'); } /** * load always needed external class files */ include_once smarty_sysplugins_dir . 'smarty_internal_data.php'; include_once smarty_sysplugins_dir . 'smarty_internal_templatebase.php'; include_once smarty_sysplugins_dir . 'smarty_internal_template.php'; include_once smarty_sysplugins_dir . 'smarty_resource.php'; include_once smarty_sysplugins_dir . 'smarty_internal_resource_file.php'; include_once smarty_sysplugins_dir . 'smarty_cacheresource.php'; include_once smarty_sysplugins_dir . 'smarty_internal_cacheresource_file.php'; /** * this is the main smarty class * * @package smarty */ class smarty extends smarty_internal_templatebase { /**#@+ * constant definitions */ /** * smarty version */ const smarty_version = 'smarty-3.1.19'; /** * define variable scopes */ const scope_local = 0; const scope_parent = 1; const scope_root = 2; const scope_global = 3; /** * define caching modes */ const caching_off = 0; const caching_lifetime_current = 1; const caching_lifetime_saved = 2; /** * define constant for clearing cache files be saved expiration datees */ const clear_expired = - 1; /** * define compile check modes */ const compilecheck_off = 0; const compilecheck_on = 1; const compilecheck_cachemiss = 2; /** * modes for handling of "<?php ... ?>" tags in templates. */ const php_passthru = 0; //-> print tags as plain text const php_quote = 1; //-> escape tags as entities const php_remove = 2; //-> escape tags as entities const php_allow = 3; //-> escape tags as entities /** * filter types */ const filter_post = 'post'; const filter_pre = 'pre'; const filter_output = 'output'; const filter_variable = 'variable'; /** * plugin types */ const plugin_function = 'function'; const plugin_block = 'block'; const plugin_compiler = 'compiler'; const plugin_modifier = 'modifier'; const plugin_modifiercompiler = 'modifiercompiler'; /**#@-*/ /** * assigned global tpl vars */ public static $global_tpl_vars = array(); /** * error handler returned by set_error_hanlder() in smarty::muteexpectederrors() */ public static $_previous_error_handler = null; /** * contains directories outside of smarty_dir that are to be muted by muteexpectederrors() */ public static $_muted_directories = array(); /** * flag denoting if multibyte string functions are available */ public static $_mbstring = smarty_mbstring; /** * the character set to adhere to (e.g. "utf-8") */ public static $_charset = smarty_resource_char_set; /** * the date format to be used internally * (accepts date() and strftime()) */ public static $_date_format = smarty_resource_date_format; /** * flag denoting if pcre should run in utf-8 mode */ public static $_utf8_modifier = 'u'; /** * flag denoting if operating system is windows */ public static $_is_windows = false; /**#@+ * variables */ /** * auto literal on delimiters with whitspace * * @var boolean */ public $auto_literal = true; /** * display error on not assigned variables * * @var boolean */ public $error_unassigned = false; /** * look up relative filepaths in include_path * * @var boolean */ public $use_include_path = false; /** * template directory * * @var array */ private $template_dir = array(); /** * joined template directory string used in cache keys * * @var string */ public $joined_template_dir = null; /** * joined config directory string used in cache keys * * @var string */ public $joined_config_dir = null; /** * default template handler * * @var callable */ public $default_template_handler_func = null; /** * default config handler * * @var callable */ public $default_config_handler_func = null; /** * default plugin handler * * @var callable */ public $default_plugin_handler_func = null; /** * compile directory * * @var string */ private $compile_dir = null; /** * plugins directory * * @var array */ private $plugins_dir = array(); /** * cache directory * * @var string */ private $cache_dir = null; /** * config directory * * @var array */ private $config_dir = array(); /** * force template compiling? * * @var boolean */ public $force_compile = false; /** * check template for modifications? * * @var boolean */ public $compile_check = true; /** * use sub dirs for compiled/cached files? * * @var boolean */ public $use_sub_dirs = false; /** * allow ambiguous resources (that are made unique by the resource handler) * * @var boolean */ public $allow_ambiguous_resources = false; /** * caching enabled * * @var boolean */ public $caching = false; /** * merge compiled includes * * @var boolean */ public $merge_compiled_includes = false; /** * template inheritance merge compiled includes * * @var boolean */ public $inheritance_merge_compiled_includes = true; /** * cache lifetime in seconds * * @var integer */ public $cache_lifetime = 3600; /** * force cache file creation * * @var boolean */ public $force_cache = false; /** * set this if you want different sets of cache files for the same * templates. * * @var string */ public $cache_id = null; /** * set this if you want different sets of compiled files for the same * templates. * * @var string */ public $compile_id = null; /** * template left-delimiter * * @var string */ public $left_delimiter = "{"; /** * template right-delimiter * * @var string */ public $right_delimiter = "}"; /**#@+ * security */ /** * class name * this should be instance of smarty_security. * * @var string * @see smarty_security */ public $security_class = 'smarty_security'; /** * implementation of security class * * @var smarty_security */ public $security_policy = null; /** * controls handling of php-blocks * * @var integer */ public $php_handling = self::php_passthru; /** * controls if the php template file resource is allowed * * @var bool */ public $allow_php_templates = false; /** * should compiled-templates be prevented from being called directly? * {@internal * currently used by smarty_internal_template only. * }} * * @var boolean */ public $direct_access_security = true; /**#@-*/ /** * debug mode * setting this to true enables the debug-console. * * @var boolean */ public $debugging = false; /** * this determines if debugging is enable-able from the browser. * <ul> * <li>none => no debugging control allowed</li> * <li>url => enable debugging when smarty_debug is found in the url.</li> * </ul> * * @var string */ public $debugging_ctrl = 'none'; /** * name of debugging url-param. * only used when $debugging_ctrl is set to 'url'. * the name of the url-parameter that activates debugging. * * @var type */ public $smarty_debug_id = 'smarty_debug'; /** * path of debug template. * * @var string */ public $debug_tpl = null; /** * when set, smarty uses this value as error_reporting-level. * * @var int */ public $error_reporting = null; /** * internal flag for gettags() * * @var boolean */ public $get_used_tags = false; /**#@+ * config var settings */ /** * controls whether variables with the same name overwrite each other. * * @var boolean */ public $config_overwrite = true; /** * controls whether config values of on/true/yes and off/false/no get converted to boolean. * * @var boolean */ public $config_booleanize = true; /** * controls whether hidden config sections/vars are read from the file. * * @var boolean */ public $config_read_hidden = false; /**#@-*/ /**#@+ * resource locking */ /** * locking concurrent compiles * * @var boolean */ public $compile_locking = true; /** * controls whether cache resources should emply locking mechanism * * @var boolean */ public $cache_locking = false; /** * seconds to wait for acquiring a lock before ignoring the write lock * * @var float */ public $locking_timeout = 10; /**#@-*/ /** * global template functions * * @var array */ public $template_functions = array(); /** * resource type used if none given * must be an valid key of $registered_resources. * * @var string */ public $default_resource_type = 'file'; /** * caching type * must be an element of $cache_resource_types. * * @var string */ public $caching_type = 'file'; /** * internal config properties * * @var array */ public $properties = array(); /** * config type * * @var string */ public $default_config_type = 'file'; /** * cached template objects * * @var array */ public $template_objects = array(); /** * check if-modified-since headers * * @var boolean */ public $cache_modified_check = false; /** * registered plugins * * @var array */ public $registered_plugins = array(); /** * plugin search order * * @var array */ public $plugin_search_order = array('function', 'block', 'compiler', 'class'); /** * registered objects * * @var array */ public $registered_objects = array(); /** * registered classes * * @var array */ public $registered_classes = array(); /** * registered filters * * @var array */ public $registered_filters = array(); /** * registered resources * * @var array */ public $registered_resources = array(); /** * resource handler cache * * @var array */ public $_resource_handlers = array(); /** * registered cache resources * * @var array */ public $registered_cache_resources = array(); /** * cache resource handler cache * * @var array */ public $_cacheresource_handlers = array(); /** * autoload filter * * @var array */ public $autoload_filters = array(); /** * default modifier * * @var array */ public $default_modifiers = array(); /** * autoescape variable output * * @var boolean */ public $escape_html = false; /** * global internal smarty vars * * @var array */ public static $_smarty_vars = array(); /** * start time for execution time calculation * * @var int */ public $start_time = 0; /** * default file permissions * * @var int */ public $_file_perms = 0644; /** * default dir permissions * * @var int */ public $_dir_perms = 0771; /** * block tag hierarchy * * @var array */ public $_tag_stack = array(); /** * self pointer to smarty object * * @var smarty */ public $smarty; /** * required by the compiler for bc * * @var string */ public $_current_file = null; /** * internal flag to enable parser debugging * * @var bool */ public $_parserdebug = false; /** * saved parameter of merged templates during compilation * * @var array */ public $merged_templates_func = array(); /**#@-*/ /** * initialize new smarty object */ public function __construct() { // selfpointer needed by some other class methods $this->smarty = $this; if (is_callable('mb_internal_encoding')) { mb_internal_encoding(smarty::$_charset); } $this->start_time = microtime(true); // set default dirs $this->settemplatedir('.' . ds . 'templates' . ds) ->setcompiledir('.' . ds . 'templates_c' . ds) ->setpluginsdir(smarty_plugins_dir) ->setcachedir('.' . ds . 'cache' . ds) ->setconfigdir('.' . ds . 'configs' . ds); $this->debug_tpl = 'file:' . dirname(__file__) . '/debug.tpl'; if (isset($_server['script_name'])) { $this->assignglobal('script_name', $_server['script_name']); } } /** * class destructor */ public function __destruct() { // intentionally left blank } /** * <<magic>> set selfpointer on cloned object */ public function __clone() { $this->smarty = $this; } /** * <<magic>> generic getter. * calls the appropriate getter function. * issues an e_user_notice if no valid getter is found. * * @param string $name property name * * @return mixed */ public function __get($name) { $allowed = array( 'template_dir' => 'gettemplatedir', 'config_dir' => 'getconfigdir', 'plugins_dir' => 'getpluginsdir', 'compile_dir' => 'getcompiledir', 'cache_dir' => 'getcachedir', ); if (isset($allowed[$name])) { return $this->{$allowed[$name]}(); } else { trigger_error('undefined property: ' . get_class($this) . '::$' . $name, e_user_notice); } } /** * <<magic>> generic setter. * calls the appropriate setter function. * issues an e_user_notice if no valid setter is found. * * @param string $name property name * @param mixed $value parameter passed to setter */ public function __set($name, $value) { $allowed = array( 'template_dir' => 'settemplatedir', 'config_dir' => 'setconfigdir', 'plugins_dir' => 'setpluginsdir', 'compile_dir' => 'setcompiledir', 'cache_dir' => 'setcachedir', ); if (isset($allowed[$name])) { $this->{$allowed[$name]}($value); } else { trigger_error('undefined property: ' . get_class($this) . '::$' . $name, e_user_notice); } } /** * check if a template resource exists * * @param string $resource_name template name * * @return boolean status */ public function templateexists($resource_name) { // create template object $save = $this->template_objects; $tpl = new $this->template_class($resource_name, $this); // check if it does exists $result = $tpl->source->exists; $this->template_objects = $save; return $result; } /** * returns a single or all global variables * * @param string $varname variable name or null * * @return string variable value or or array of variables */ public function getglobal($varname = null) { if (isset($varname)) { if (isset(self::$global_tpl_vars[$varname])) { return self::$global_tpl_vars[$varname]->value; } else { return ''; } } else { $_result = array(); foreach (self::$global_tpl_vars as $key => $var) { $_result[$key] = $var->value; } return $_result; } } /** * empty cache folder * * @param integer $exp_time expiration time * @param string $type resource type * * @return integer number of cache files deleted */ public function clearallcache($exp_time = null, $type = null) { // load cache resource and call clearall $_cache_resource = smarty_cacheresource::load($this, $type); smarty_cacheresource::invalidloadedcache($this); return $_cache_resource->clearall($this, $exp_time); } /** * empty cache for a specific template * * @param string $template_name template name * @param string $cache_id cache id * @param string $compile_id compile id * @param integer $exp_time expiration time * @param string $type resource type * * @return integer number of cache files deleted */ public function clearcache($template_name, $cache_id = null, $compile_id = null, $exp_time = null, $type = null) { // load cache resource and call clear $_cache_resource = smarty_cacheresource::load($this, $type); smarty_cacheresource::invalidloadedcache($this); return $_cache_resource->clear($this, $template_name, $cache_id, $compile_id, $exp_time); } /** * loads security class and enables security * * @param string|smarty_security $security_class if a string is used, it must be class-name * * @return smarty current smarty instance for chaining * @throws smartyexception when an invalid class name is provided */ public function enablesecurity($security_class = null) { if ($security_class instanceof smarty_security) { $this->security_policy = $security_class; return $this; } elseif (is_object($security_class)) { throw new smartyexception("class '" . get_class($security_class) . "' must extend smarty_security."); } if ($security_class == null) { $security_class = $this->security_class; } if (!class_exists($security_class)) { throw new smartyexception("security class '$security_class' is not defined"); } elseif ($security_class !== 'smarty_security' && !is_subclass_of($security_class, 'smarty_security')) { throw new smartyexception("class '$security_class' must extend smarty_security."); } else { $this->security_policy = new $security_class($this); } return $this; } /** * disable security * * @return smarty current smarty instance for chaining */ public function disablesecurity() { $this->security_policy = null; return $this; } /** * set template directory * * @param string|array $template_dir directory(s) of template sources * * @return smarty current smarty instance for chaining */ public function settemplatedir($template_dir) { $this->template_dir = array(); foreach ((array) $template_dir as $k => $v) { $this->template_dir[$k] = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($v, '/\\')) . ds; } $this->joined_template_dir = join(directory_separator, $this->template_dir); return $this; } /** * add template directory(s) * * @param string|array $template_dir directory(s) of template sources * @param string $key of the array element to assign the template dir to * * @return smarty current smarty instance for chaining * @throws smartyexception when the given template directory is not valid */ public function addtemplatedir($template_dir, $key = null) { // make sure we're dealing with an array $this->template_dir = (array) $this->template_dir; if (is_array($template_dir)) { foreach ($template_dir as $k => $v) { $v = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($v, '/\\')) . ds; if (is_int($k)) { // indexes are not merged but appended $this->template_dir[] = $v; } else { // string indexes are overridden $this->template_dir[$k] = $v; } } } else { $v = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($template_dir, '/\\')) . ds; if ($key !== null) { // override directory at specified index $this->template_dir[$key] = $v; } else { // append new directory $this->template_dir[] = $v; } } $this->joined_template_dir = join(directory_separator, $this->template_dir); return $this; } /** * get template directories * * @param mixed $index index of directory to get, null to get all * * @return array|string list of template directories, or directory of $index */ public function gettemplatedir($index = null) { if ($index !== null) { return isset($this->template_dir[$index]) ? $this->template_dir[$index] : null; } return (array) $this->template_dir; } /** * set config directory * * @param $config_dir * * @return smarty current smarty instance for chaining */ public function setconfigdir($config_dir) { $this->config_dir = array(); foreach ((array) $config_dir as $k => $v) { $this->config_dir[$k] = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($v, '/\\')) . ds; } $this->joined_config_dir = join(directory_separator, $this->config_dir); return $this; } /** * add config directory(s) * * @param string|array $config_dir directory(s) of config sources * @param mixed $key key of the array element to assign the config dir to * * @return smarty current smarty instance for chaining */ public function addconfigdir($config_dir, $key = null) { // make sure we're dealing with an array $this->config_dir = (array) $this->config_dir; if (is_array($config_dir)) { foreach ($config_dir as $k => $v) { $v = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($v, '/\\')) . ds; if (is_int($k)) { // indexes are not merged but appended $this->config_dir[] = $v; } else { // string indexes are overridden $this->config_dir[$k] = $v; } } } else { $v = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($config_dir, '/\\')) . ds; if ($key !== null) { // override directory at specified index $this->config_dir[$key] = rtrim($v, '/\\') . ds; } else { // append new directory $this->config_dir[] = rtrim($v, '/\\') . ds; } } $this->joined_config_dir = join(directory_separator, $this->config_dir); return $this; } /** * get config directory * * @param mixed $index index of directory to get, null to get all * * @return array|string configuration directory */ public function getconfigdir($index = null) { if ($index !== null) { return isset($this->config_dir[$index]) ? $this->config_dir[$index] : null; } return (array) $this->config_dir; } /** * set plugins directory * * @param string|array $plugins_dir directory(s) of plugins * * @return smarty current smarty instance for chaining */ public function setpluginsdir($plugins_dir) { $this->plugins_dir = array(); foreach ((array) $plugins_dir as $k => $v) { $this->plugins_dir[$k] = rtrim($v, '/\\') . ds; } return $this; } /** * adds directory of plugin files * * @param $plugins_dir * * @return smarty current smarty instance for chaining */ public function addpluginsdir($plugins_dir) { // make sure we're dealing with an array $this->plugins_dir = (array) $this->plugins_dir; if (is_array($plugins_dir)) { foreach ($plugins_dir as $k => $v) { if (is_int($k)) { // indexes are not merged but appended $this->plugins_dir[] = rtrim($v, '/\\') . ds; } else { // string indexes are overridden $this->plugins_dir[$k] = rtrim($v, '/\\') . ds; } } } else { // append new directory $this->plugins_dir[] = rtrim($plugins_dir, '/\\') . ds; } $this->plugins_dir = array_unique($this->plugins_dir); return $this; } /** * get plugin directories * * @return array list of plugin directories */ public function getpluginsdir() { return (array) $this->plugins_dir; } /** * set compile directory * * @param string $compile_dir directory to store compiled templates in * * @return smarty current smarty instance for chaining */ public function setcompiledir($compile_dir) { $this->compile_dir = rtrim($compile_dir, '/\\') . ds; if (!isset(smarty::$_muted_directories[$this->compile_dir])) { smarty::$_muted_directories[$this->compile_dir] = null; } return $this; } /** * get compiled directory * * @return string path to compiled templates */ public function getcompiledir() { return $this->compile_dir; } /** * set cache directory * * @param string $cache_dir directory to store cached templates in * * @return smarty current smarty instance for chaining */ public function setcachedir($cache_dir) { $this->cache_dir = rtrim($cache_dir, '/\\') . ds; if (!isset(smarty::$_muted_directories[$this->cache_dir])) { smarty::$_muted_directories[$this->cache_dir] = null; } return $this; } /** * get cache directory * * @return string path of cache directory */ public function getcachedir() { return $this->cache_dir; } /** * set default modifiers * * @param array|string $modifiers modifier or list of modifiers to set * * @return smarty current smarty instance for chaining */ public function setdefaultmodifiers($modifiers) { $this->default_modifiers = (array) $modifiers; return $this; } /** * add default modifiers * * @param array|string $modifiers modifier or list of modifiers to add * * @return smarty current smarty instance for chaining */ public function adddefaultmodifiers($modifiers) { if (is_array($modifiers)) { $this->default_modifiers = array_merge($this->default_modifiers, $modifiers); } else { $this->default_modifiers[] = $modifiers; } return $this; } /** * get default modifiers * * @return array list of default modifiers */ public function getdefaultmodifiers() { return $this->default_modifiers; } /** * set autoload filters * * @param array $filters filters to load automatically * @param string $type "pre", "output", … specify the filter type to set. defaults to none treating $filters' keys as the appropriate types * * @return smarty current smarty instance for chaining */ public function setautoloadfilters($filters, $type = null) { if ($type !== null) { $this->autoload_filters[$type] = (array) $filters; } else { $this->autoload_filters = (array) $filters; } return $this; } /** * add autoload filters * * @param array $filters filters to load automatically * @param string $type "pre", "output", … specify the filter type to set. defaults to none treating $filters' keys as the appropriate types * * @return smarty current smarty instance for chaining */ public function addautoloadfilters($filters, $type = null) { if ($type !== null) { if (!empty($this->autoload_filters[$type])) { $this->autoload_filters[$type] = array_merge($this->autoload_filters[$type], (array) $filters); } else { $this->autoload_filters[$type] = (array) $filters; } } else { foreach ((array) $filters as $key => $value) { if (!empty($this->autoload_filters[$key])) { $this->autoload_filters[$key] = array_merge($this->autoload_filters[$key], (array) $value); } else { $this->autoload_filters[$key] = (array) $value; } } } return $this; } /** * get autoload filters * * @param string $type type of filter to get autoloads for. defaults to all autoload filters * * @return array array( 'type1' => array( 'filter1', 'filter2', … ) ) or array( 'filter1', 'filter2', …) if $type was specified */ public function getautoloadfilters($type = null) { if ($type !== null) { return isset($this->autoload_filters[$type]) ? $this->autoload_filters[$type] : array(); } return $this->autoload_filters; } /** * return name of debugging template * * @return string */ public function getdebugtemplate() { return $this->debug_tpl; } /** * set the debug template * * @param string $tpl_name * * @return smarty current smarty instance for chaining * @throws smartyexception if file is not readable */ public function setdebugtemplate($tpl_name) { if (!is_readable($tpl_name)) { throw new smartyexception("unknown file '{$tpl_name}'"); } $this->debug_tpl = $tpl_name; return $this; } /** * creates a template object * * @param string $template the resource handle of the template file * @param mixed $cache_id cache id to be used with this template * @param mixed $compile_id compile id to be used with this template * @param object $parent next higher level of smarty variables * @param boolean $do_clone flag is smarty object shall be cloned * * @return object template object */ public function createtemplate($template, $cache_id = null, $compile_id = null, $parent = null, $do_clone = true) { if ($cache_id !== null && (is_object($cache_id) || is_array($cache_id))) { $parent = $cache_id; $cache_id = null; } if ($parent !== null && is_array($parent)) { $data = $parent; $parent = null; } else { $data = null; } // default to cache_id and compile_id of smarty object $cache_id = $cache_id === null ? $this->cache_id : $cache_id; $compile_id = $compile_id === null ? $this->compile_id : $compile_id; // already in template cache? if ($this->allow_ambiguous_resources) { $_templateid = smarty_resource::getuniquetemplatename($this, $template) . $cache_id . $compile_id; } else { $_templateid = $this->joined_template_dir . '#' . $template . $cache_id . $compile_id; } if (isset($_templateid[150])) { $_templateid = sha1($_templateid); } if ($do_clone) { if (isset($this->template_objects[$_templateid])) { // return cached template object $tpl = clone $this->template_objects[$_templateid]; $tpl->smarty = clone $tpl->smarty; $tpl->parent = $parent; $tpl->tpl_vars = array(); $tpl->config_vars = array(); } else { $tpl = new $this->template_class($template, clone $this, $parent, $cache_id, $compile_id); } } else { if (isset($this->template_objects[$_templateid])) { // return cached template object $tpl = $this->template_objects[$_templateid]; $tpl->parent = $parent; $tpl->tpl_vars = array(); $tpl->config_vars = array(); } else { $tpl = new $this->template_class($template, $this, $parent, $cache_id, $compile_id); } } // fill data if present if (!empty($data) && is_array($data)) { // set up variable values foreach ($data as $_key => $_val) { $tpl->tpl_vars[$_key] = new smarty_variable($_val); } } return $tpl; } /** * takes unknown classes and loads plugin files for them * class name format: smarty_plugintype_pluginname * plugin filename format: plugintype.pluginname.php * * @param string $plugin_name class plugin name to load * @param bool $check check if already loaded * * @throws smartyexception * @return string |boolean filepath of loaded file or false */ public function loadplugin($plugin_name, $check = true) { // if function or class exists, exit silently (already loaded) if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false))) { return true; } // plugin name is expected to be: smarty_[type]_[name] $_name_parts = explode('_', $plugin_name, 3); // class name must have three parts to be valid plugin // count($_name_parts) < 3 === !isset($_name_parts[2]) if (!isset($_name_parts[2]) || strtolower($_name_parts[0]) !== 'smarty') { throw new smartyexception("plugin {$plugin_name} is not a valid name format"); } // if type is "internal", get plugin from sysplugins if (strtolower($_name_parts[1]) == 'internal') { $file = smarty_sysplugins_dir . strtolower($plugin_name) . '.php'; if (file_exists($file)) { require_once($file); return $file; } else { return false; } } // plugin filename is expected to be: [type].[name].php $_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php"; $_stream_resolve_include_path = function_exists('stream_resolve_include_path'); // loop through plugin dirs and find the plugin foreach ($this->getpluginsdir() as $_plugin_dir) { $names = array( $_plugin_dir . $_plugin_filename, $_plugin_dir . strtolower($_plugin_filename), ); foreach ($names as $file) { if (file_exists($file)) { require_once($file); return $file; } if ($this->use_include_path && !preg_match('/^([\/\\\\]|[a-za-z]:[\/\\\\])/', $_plugin_dir)) { // try php include_path if ($_stream_resolve_include_path) { $file = stream_resolve_include_path($file); } else { $file = smarty_internal_get_include_path::getincludepath($file); } if ($file !== false) { require_once($file); return $file; } } } } // no plugin loaded return false; } /** * compile all template files * * @param string $extension file extension * @param bool $force_compile force all to recompile * @param int $time_limit * @param int $max_errors * * @return integer number of template files recompiled */ public function compilealltemplates($extension = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null) { return smarty_internal_utility::compilealltemplates($extension, $force_compile, $time_limit, $max_errors, $this); } /** * compile all config files * * @param string $extension file extension * @param bool $force_compile force all to recompile * @param int $time_limit * @param int $max_errors * * @return integer number of template files recompiled */ public function compileallconfig($extension = '.conf', $force_compile = false, $time_limit = 0, $max_errors = null) { return smarty_internal_utility::compileallconfig($extension, $force_compile, $time_limit, $max_errors, $this); } /** * delete compiled template file * * @param string $resource_name template name * @param string $compile_id compile id * @param integer $exp_time expiration time * * @return integer number of template files deleted */ public function clearcompiledtemplate($resource_name = null, $compile_id = null, $exp_time = null) { return smarty_internal_utility::clearcompiledtemplate($resource_name, $compile_id, $exp_time, $this); } /** * return array of tag/attributes of all tags used by an template * * @param smarty_internal_template $template * * @return array of tag/attributes */ public function gettags(smarty_internal_template $template) { return smarty_internal_utility::gettags($template); } /** * run installation test * * @param array $errors array to write errors into, rather than outputting them * * @return boolean true if setup is fine, false if something is wrong */ public function testinstall(&$errors = null) { return smarty_internal_utility::testinstall($this, $errors); } /** * error handler to mute expected messages * * @link http://php.net/set_error_handler * * @param integer $errno error level * @param $errstr * @param $errfile * @param $errline * @param $errcontext * * @return boolean */ public static function mutingerrorhandler($errno, $errstr, $errfile, $errline, $errcontext) { $_is_muted_directory = false; // add the smarty_dir to the list of muted directories if (!isset(smarty::$_muted_directories[smarty_dir])) { $smarty_dir = realpath(smarty_dir); if ($smarty_dir !== false) { smarty::$_muted_directories[smarty_dir] = array( 'file' => $smarty_dir, 'length' => strlen($smarty_dir), ); } } // walk the muted directories and test against $errfile foreach (smarty::$_muted_directories as $key => &$dir) { if (!$dir) { // resolve directory and length for speedy comparisons $file = realpath($key); if ($file === false) { // this directory does not exist, remove and skip it unset(smarty::$_muted_directories[$key]); continue; } $dir = array( 'file' => $file, 'length' => strlen($file), ); } if (!strncmp($errfile, $dir['file'], $dir['length'])) { $_is_muted_directory = true; break; } } // pass to next error handler if this error did not occur inside smarty_dir // or the error was within smarty but masked to be ignored if (!$_is_muted_directory || ($errno && $errno & error_reporting())) { if (smarty::$_previous_error_handler) { return call_user_func(smarty::$_previous_error_handler, $errno, $errstr, $errfile, $errline, $errcontext); } else { return false; } } } /** * enable error handler to mute expected messages * * @return void */ public static function muteexpectederrors() { /* error muting is done because some people implemented custom error_handlers using http://php.net/set_error_handler and for some reason did not understand the following paragraph: it is important to remember that the standard php error handler is completely bypassed for the error types specified by error_types unless the callback function returns false. error_reporting() settings will have no effect and your error handler will be called regardless - however you are still able to read the current value of error_reporting and act appropriately. of particular note is that this value will be 0 if the statement that caused the error was prepended by the @ error-control operator. smarty deliberately uses @filemtime() over file_exists() and filemtime() in some places. reasons include - @filemtime() is almost twice as fast as using an additional file_exists() - between file_exists() and filemtime() a possible race condition is opened, which does not exist using the simple @filemtime() approach. */ $error_handler = array('smarty', 'mutingerrorhandler'); $previous = set_error_handler($error_handler); // avoid dead loops if ($previous !== $error_handler) { smarty::$_previous_error_handler = $previous; } } /** * disable error handler muting expected messages * * @return void */ public static function unmuteexpectederrors() { restore_error_handler(); } } // check if we're running on windows smarty::$_is_windows = strtoupper(substr(php_os, 0, 3)) === 'win'; // let pcre (preg_*) treat strings as iso-8859-1 if we're not dealing with utf-8 if (smarty::$_charset !== 'utf-8') { smarty::$_utf8_modifier = ''; } /** * smarty exception class * * @package smarty */ class smartyexception extends exception { public static $escape = false; public function __tostring() { return ' --> smarty: ' . (self::$escape ? htmlentities($this->message) : $this->message) . ' <-- '; } } /** * smarty compiler exception class * * @package smarty */ class smartycompilerexception extends smartyexception { public function __tostring() { return ' --> smarty compiler: ' . $this->message . ' <-- '; } /** * the line number of the template error * * @type int|null */ public $line = null; /** * the template source snippet relating to the error * * @type string|null */ public $source = null; /** * the raw text of the error message * * @type string|null */ public $desc = null; /** * the resource identifier or template name * * @type string|null */ public $template = null; } /** * autoloader */ function smartyautoload($class) { $_class = strtolower($class); static $_classes = array( 'smarty_config_source' => true, 'smarty_config_compiled' => true, 'smarty_security' => true, 'smarty_cacheresource' => true, 'smarty_cacheresource_custom' => true, 'smarty_cacheresource_keyvaluestore' => true, 'smarty_resource' => true, 'smarty_resource_custom' => true, 'smarty_resource_uncompiled' => true, 'smarty_resource_recompiled' => true, ); if (!strncmp($_class, 'smarty_internal_', 16) || isset($_classes[$_class])) { include smarty_sysplugins_dir . $_class . '.php'; } } <?php /** * project: smarty: the php compiling template engine * file: smarty.class.php * svn: $id: smarty.class.php 4848 2014-06-08 18:12:09z uwe.tews@googlemail.com $ * this library is free software; you can redistribute it and/or * modify it under the terms of the gnu lesser general public * license as published by the free software foundation; either * version 2.1 of the license, or (at your option) any later version. * this library is distributed in the hope that it will be useful, * but without any warranty; without even the implied warranty of * merchantability or fitness for a particular purpose. see the gnu * lesser general public license for more details. * you should have received a copy of the gnu lesser general public * license along with this library; if not, write to the free software * foundation, inc., 59 temple place, suite 330, boston, ma 02111-1307 usa * for questions, help, comments, discussion, etc., please join the * smarty mailing list. send a blank e-mail to * smarty-discussion-subscribe@googlegroups.com * * @link http://www.smarty.net/ * @copyright 2008 new digital group, inc. * @author monte ohrt <monte at ohrt dot com> * @author uwe tews * @author rodney rehm * @package smarty * @version 3.1.19 */ /** * define shorthand directory separator constant */ if (!defined('ds')) { define('ds', directory_separator); } /** * set smarty_dir to absolute path to smarty library files. * sets smarty_dir only if user application has not already defined it. */ if (!defined('smarty_dir')) { define('smarty_dir', dirname(__file__) . ds); } /** * set smarty_sysplugins_dir to absolute path to smarty internal plugins. * sets smarty_sysplugins_dir only if user application has not already defined it. */ if (!defined('smarty_sysplugins_dir')) { define('smarty_sysplugins_dir', smarty_dir . 'sysplugins' . ds); } if (!defined('smarty_plugins_dir')) { define('smarty_plugins_dir', smarty_dir . 'plugins' . ds); } if (!defined('smarty_mbstring')) { define('smarty_mbstring', function_exists('mb_split')); } if (!defined('smarty_resource_char_set')) { // utf-8 can only be done properly when mbstring is available! /** * @deprecated in favor of smarty::$_charset */ define('smarty_resource_char_set', smarty_mbstring ? 'utf-8' : 'iso-8859-1'); } if (!defined('smarty_resource_date_format')) { /** * @deprecated in favor of smarty::$_date_format */ define('smarty_resource_date_format', '%b %e, %y'); } /** * register the class autoloader */ if (!defined('smarty_spl_autoload')) { define('smarty_spl_autoload', 0); } if (smarty_spl_autoload && set_include_path(get_include_path() . path_separator . smarty_sysplugins_dir) !== false) { $registeredautoloadfunctions = spl_autoload_functions(); if (!isset($registeredautoloadfunctions['spl_autoload'])) { spl_autoload_register(); } } else { spl_autoload_register('smartyautoload'); } /** * load always needed external class files */ include_once smarty_sysplugins_dir . 'smarty_internal_data.php'; include_once smarty_sysplugins_dir . 'smarty_internal_templatebase.php'; include_once smarty_sysplugins_dir . 'smarty_internal_template.php'; include_once smarty_sysplugins_dir . 'smarty_resource.php'; include_once smarty_sysplugins_dir . 'smarty_internal_resource_file.php'; include_once smarty_sysplugins_dir . 'smarty_cacheresource.php'; include_once smarty_sysplugins_dir . 'smarty_internal_cacheresource_file.php'; /** * this is the main smarty class * * @package smarty */ class smarty extends smarty_internal_templatebase { /**#@+ * constant definitions */ /** * smarty version */ const smarty_version = 'smarty-3.1.19'; /** * define variable scopes */ const scope_local = 0; const scope_parent = 1; const scope_root = 2; const scope_global = 3; /** * define caching modes */ const caching_off = 0; const caching_lifetime_current = 1; const caching_lifetime_saved = 2; /** * define constant for clearing cache files be saved expiration datees */ const clear_expired = - 1; /** * define compile check modes */ const compilecheck_off = 0; const compilecheck_on = 1; const compilecheck_cachemiss = 2; /** * modes for handling of "<?php ... ?>" tags in templates. */ const php_passthru = 0; //-> print tags as plain text const php_quote = 1; //-> escape tags as entities const php_remove = 2; //-> escape tags as entities const php_allow = 3; //-> escape tags as entities /** * filter types */ const filter_post = 'post'; const filter_pre = 'pre'; const filter_output = 'output'; const filter_variable = 'variable'; /** * plugin types */ const plugin_function = 'function'; const plugin_block = 'block'; const plugin_compiler = 'compiler'; const plugin_modifier = 'modifier'; const plugin_modifiercompiler = 'modifiercompiler'; /**#@-*/ /** * assigned global tpl vars */ public static $global_tpl_vars = array(); /** * error handler returned by set_error_hanlder() in smarty::muteexpectederrors() */ public static $_previous_error_handler = null; /** * contains directories outside of smarty_dir that are to be muted by muteexpectederrors() */ public static $_muted_directories = array(); /** * flag denoting if multibyte string functions are available */ public static $_mbstring = smarty_mbstring; /** * the character set to adhere to (e.g. "utf-8") */ public static $_charset = smarty_resource_char_set; /** * the date format to be used internally * (accepts date() and strftime()) */ public static $_date_format = smarty_resource_date_format; /** * flag denoting if pcre should run in utf-8 mode */ public static $_utf8_modifier = 'u'; /** * flag denoting if operating system is windows */ public static $_is_windows = false; /**#@+ * variables */ /** * auto literal on delimiters with whitspace * * @var boolean */ public $auto_literal = true; /** * display error on not assigned variables * * @var boolean */ public $error_unassigned = false; /** * look up relative filepaths in include_path * * @var boolean */ public $use_include_path = false; /** * template directory * * @var array */ private $template_dir = array(); /** * joined template directory string used in cache keys * * @var string */ public $joined_template_dir = null; /** * joined config directory string used in cache keys * * @var string */ public $joined_config_dir = null; /** * default template handler * * @var callable */ public $default_template_handler_func = null; /** * default config handler * * @var callable */ public $default_config_handler_func = null; /** * default plugin handler * * @var callable */ public $default_plugin_handler_func = null; /** * compile directory * * @var string */ private $compile_dir = null; /** * plugins directory * * @var array */ private $plugins_dir = array(); /** * cache directory * * @var string */ private $cache_dir = null; /** * config directory * * @var array */ private $config_dir = array(); /** * force template compiling? * * @var boolean */ public $force_compile = false; /** * check template for modifications? * * @var boolean */ public $compile_check = true; /** * use sub dirs for compiled/cached files? * * @var boolean */ public $use_sub_dirs = false; /** * allow ambiguous resources (that are made unique by the resource handler) * * @var boolean */ public $allow_ambiguous_resources = false; /** * caching enabled * * @var boolean */ public $caching = false; /** * merge compiled includes * * @var boolean */ public $merge_compiled_includes = false; /** * template inheritance merge compiled includes * * @var boolean */ public $inheritance_merge_compiled_includes = true; /** * cache lifetime in seconds * * @var integer */ public $cache_lifetime = 3600; /** * force cache file creation * * @var boolean */ public $force_cache = false; /** * set this if you want different sets of cache files for the same * templates. * * @var string */ public $cache_id = null; /** * set this if you want different sets of compiled files for the same * templates. * * @var string */ public $compile_id = null; /** * template left-delimiter * * @var string */ public $left_delimiter = "{"; /** * template right-delimiter * * @var string */ public $right_delimiter = "}"; /**#@+ * security */ /** * class name * this should be instance of smarty_security. * * @var string * @see smarty_security */ public $security_class = 'smarty_security'; /** * implementation of security class * * @var smarty_security */ public $security_policy = null; /** * controls handling of php-blocks * * @var integer */ public $php_handling = self::php_passthru; /** * controls if the php template file resource is allowed * * @var bool */ public $allow_php_templates = false; /** * should compiled-templates be prevented from being called directly? * {@internal * currently used by smarty_internal_template only. * }} * * @var boolean */ public $direct_access_security = true; /**#@-*/ /** * debug mode * setting this to true enables the debug-console. * * @var boolean */ public $debugging = false; /** * this determines if debugging is enable-able from the browser. * <ul> * <li>none => no debugging control allowed</li> * <li>url => enable debugging when smarty_debug is found in the url.</li> * </ul> * * @var string */ public $debugging_ctrl = 'none'; /** * name of debugging url-param. * only used when $debugging_ctrl is set to 'url'. * the name of the url-parameter that activates debugging. * * @var type */ public $smarty_debug_id = 'smarty_debug'; /** * path of debug template. * * @var string */ public $debug_tpl = null; /** * when set, smarty uses this value as error_reporting-level. * * @var int */ public $error_reporting = null; /** * internal flag for gettags() * * @var boolean */ public $get_used_tags = false; /**#@+ * config var settings */ /** * controls whether variables with the same name overwrite each other. * * @var boolean */ public $config_overwrite = true; /** * controls whether config values of on/true/yes and off/false/no get converted to boolean. * * @var boolean */ public $config_booleanize = true; /** * controls whether hidden config sections/vars are read from the file. * * @var boolean */ public $config_read_hidden = false; /**#@-*/ /**#@+ * resource locking */ /** * locking concurrent compiles * * @var boolean */ public $compile_locking = true; /** * controls whether cache resources should emply locking mechanism * * @var boolean */ public $cache_locking = false; /** * seconds to wait for acquiring a lock before ignoring the write lock * * @var float */ public $locking_timeout = 10; /**#@-*/ /** * global template functions * * @var array */ public $template_functions = array(); /** * resource type used if none given * must be an valid key of $registered_resources. * * @var string */ public $default_resource_type = 'file'; /** * caching type * must be an element of $cache_resource_types. * * @var string */ public $caching_type = 'file'; /** * internal config properties * * @var array */ public $properties = array(); /** * config type * * @var string */ public $default_config_type = 'file'; /** * cached template objects * * @var array */ public $template_objects = array(); /** * check if-modified-since headers * * @var boolean */ public $cache_modified_check = false; /** * registered plugins * * @var array */ public $registered_plugins = array(); /** * plugin search order * * @var array */ public $plugin_search_order = array('function', 'block', 'compiler', 'class'); /** * registered objects * * @var array */ public $registered_objects = array(); /** * registered classes * * @var array */ public $registered_classes = array(); /** * registered filters * * @var array */ public $registered_filters = array(); /** * registered resources * * @var array */ public $registered_resources = array(); /** * resource handler cache * * @var array */ public $_resource_handlers = array(); /** * registered cache resources * * @var array */ public $registered_cache_resources = array(); /** * cache resource handler cache * * @var array */ public $_cacheresource_handlers = array(); /** * autoload filter * * @var array */ public $autoload_filters = array(); /** * default modifier * * @var array */ public $default_modifiers = array(); /** * autoescape variable output * * @var boolean */ public $escape_html = false; /** * global internal smarty vars * * @var array */ public static $_smarty_vars = array(); /** * start time for execution time calculation * * @var int */ public $start_time = 0; /** * default file permissions * * @var int */ public $_file_perms = 0644; /** * default dir permissions * * @var int */ public $_dir_perms = 0771; /** * block tag hierarchy * * @var array */ public $_tag_stack = array(); /** * self pointer to smarty object * * @var smarty */ public $smarty; /** * required by the compiler for bc * * @var string */ public $_current_file = null; /** * internal flag to enable parser debugging * * @var bool */ public $_parserdebug = false; /** * saved parameter of merged templates during compilation * * @var array */ public $merged_templates_func = array(); /**#@-*/ /** * initialize new smarty object */ public function __construct() { // selfpointer needed by some other class methods $this->smarty = $this; if (is_callable('mb_internal_encoding')) { mb_internal_encoding(smarty::$_charset); } $this->start_time = microtime(true); // set default dirs $this->settemplatedir('.' . ds . 'templates' . ds) ->setcompiledir('.' . ds . 'templates_c' . ds) ->setpluginsdir(smarty_plugins_dir) ->setcachedir('.' . ds . 'cache' . ds) ->setconfigdir('.' . ds . 'configs' . ds); $this->debug_tpl = 'file:' . dirname(__file__) . '/debug.tpl'; if (isset($_server['script_name'])) { $this->assignglobal('script_name', $_server['script_name']); } } /** * class destructor */ public function __destruct() { // intentionally left blank } /** * <<magic>> set selfpointer on cloned object */ public function __clone() { $this->smarty = $this; } /** * <<magic>> generic getter. * calls the appropriate getter function. * issues an e_user_notice if no valid getter is found. * * @param string $name property name * * @return mixed */ public function __get($name) { $allowed = array( 'template_dir' => 'gettemplatedir', 'config_dir' => 'getconfigdir', 'plugins_dir' => 'getpluginsdir', 'compile_dir' => 'getcompiledir', 'cache_dir' => 'getcachedir', ); if (isset($allowed[$name])) { return $this->{$allowed[$name]}(); } else { trigger_error('undefined property: ' . get_class($this) . '::$' . $name, e_user_notice); } } /** * <<magic>> generic setter. * calls the appropriate setter function. * issues an e_user_notice if no valid setter is found. * * @param string $name property name * @param mixed $value parameter passed to setter */ public function __set($name, $value) { $allowed = array( 'template_dir' => 'settemplatedir', 'config_dir' => 'setconfigdir', 'plugins_dir' => 'setpluginsdir', 'compile_dir' => 'setcompiledir', 'cache_dir' => 'setcachedir', ); if (isset($allowed[$name])) { $this->{$allowed[$name]}($value); } else { trigger_error('undefined property: ' . get_class($this) . '::$' . $name, e_user_notice); } } /** * check if a template resource exists * * @param string $resource_name template name * * @return boolean status */ public function templateexists($resource_name) { // create template object $save = $this->template_objects; $tpl = new $this->template_class($resource_name, $this); // check if it does exists $result = $tpl->source->exists; $this->template_objects = $save; return $result; } /** * returns a single or all global variables * * @param string $varname variable name or null * * @return string variable value or or array of variables */ public function getglobal($varname = null) { if (isset($varname)) { if (isset(self::$global_tpl_vars[$varname])) { return self::$global_tpl_vars[$varname]->value; } else { return ''; } } else { $_result = array(); foreach (self::$global_tpl_vars as $key => $var) { $_result[$key] = $var->value; } return $_result; } } /** * empty cache folder * * @param integer $exp_time expiration time * @param string $type resource type * * @return integer number of cache files deleted */ public function clearallcache($exp_time = null, $type = null) { // load cache resource and call clearall $_cache_resource = smarty_cacheresource::load($this, $type); smarty_cacheresource::invalidloadedcache($this); return $_cache_resource->clearall($this, $exp_time); } /** * empty cache for a specific template * * @param string $template_name template name * @param string $cache_id cache id * @param string $compile_id compile id * @param integer $exp_time expiration time * @param string $type resource type * * @return integer number of cache files deleted */ public function clearcache($template_name, $cache_id = null, $compile_id = null, $exp_time = null, $type = null) { // load cache resource and call clear $_cache_resource = smarty_cacheresource::load($this, $type); smarty_cacheresource::invalidloadedcache($this); return $_cache_resource->clear($this, $template_name, $cache_id, $compile_id, $exp_time); } /** * loads security class and enables security * * @param string|smarty_security $security_class if a string is used, it must be class-name * * @return smarty current smarty instance for chaining * @throws smartyexception when an invalid class name is provided */ public function enablesecurity($security_class = null) { if ($security_class instanceof smarty_security) { $this->security_policy = $security_class; return $this; } elseif (is_object($security_class)) { throw new smartyexception("class '" . get_class($security_class) . "' must extend smarty_security."); } if ($security_class == null) { $security_class = $this->security_class; } if (!class_exists($security_class)) { throw new smartyexception("security class '$security_class' is not defined"); } elseif ($security_class !== 'smarty_security' && !is_subclass_of($security_class, 'smarty_security')) { throw new smartyexception("class '$security_class' must extend smarty_security."); } else { $this->security_policy = new $security_class($this); } return $this; } /** * disable security * * @return smarty current smarty instance for chaining */ public function disablesecurity() { $this->security_policy = null; return $this; } /** * set template directory * * @param string|array $template_dir directory(s) of template sources * * @return smarty current smarty instance for chaining */ public function settemplatedir($template_dir) { $this->template_dir = array(); foreach ((array) $template_dir as $k => $v) { $this->template_dir[$k] = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($v, '/\\')) . ds; } $this->joined_template_dir = join(directory_separator, $this->template_dir); return $this; } /** * add template directory(s) * * @param string|array $template_dir directory(s) of template sources * @param string $key of the array element to assign the template dir to * * @return smarty current smarty instance for chaining * @throws smartyexception when the given template directory is not valid */ public function addtemplatedir($template_dir, $key = null) { // make sure we're dealing with an array $this->template_dir = (array) $this->template_dir; if (is_array($template_dir)) { foreach ($template_dir as $k => $v) { $v = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($v, '/\\')) . ds; if (is_int($k)) { // indexes are not merged but appended $this->template_dir[] = $v; } else { // string indexes are overridden $this->template_dir[$k] = $v; } } } else { $v = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($template_dir, '/\\')) . ds; if ($key !== null) { // override directory at specified index $this->template_dir[$key] = $v; } else { // append new directory $this->template_dir[] = $v; } } $this->joined_template_dir = join(directory_separator, $this->template_dir); return $this; } /** * get template directories * * @param mixed $index index of directory to get, null to get all * * @return array|string list of template directories, or directory of $index */ public function gettemplatedir($index = null) { if ($index !== null) { return isset($this->template_dir[$index]) ? $this->template_dir[$index] : null; } return (array) $this->template_dir; } /** * set config directory * * @param $config_dir * * @return smarty current smarty instance for chaining */ public function setconfigdir($config_dir) { $this->config_dir = array(); foreach ((array) $config_dir as $k => $v) { $this->config_dir[$k] = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($v, '/\\')) . ds; } $this->joined_config_dir = join(directory_separator, $this->config_dir); return $this; } /** * add config directory(s) * * @param string|array $config_dir directory(s) of config sources * @param mixed $key key of the array element to assign the config dir to * * @return smarty current smarty instance for chaining */ public function addconfigdir($config_dir, $key = null) { // make sure we're dealing with an array $this->config_dir = (array) $this->config_dir; if (is_array($config_dir)) { foreach ($config_dir as $k => $v) { $v = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($v, '/\\')) . ds; if (is_int($k)) { // indexes are not merged but appended $this->config_dir[] = $v; } else { // string indexes are overridden $this->config_dir[$k] = $v; } } } else { $v = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($config_dir, '/\\')) . ds; if ($key !== null) { // override directory at specified index $this->config_dir[$key] = rtrim($v, '/\\') . ds; } else { // append new directory $this->config_dir[] = rtrim($v, '/\\') . ds; } } $this->joined_config_dir = join(directory_separator, $this->config_dir); return $this; } /** * get config directory * * @param mixed $index index of directory to get, null to get all * * @return array|string configuration directory */ public function getconfigdir($index = null) { if ($index !== null) { return isset($this->config_dir[$index]) ? $this->config_dir[$index] : null; } return (array) $this->config_dir; } /** * set plugins directory * * @param string|array $plugins_dir directory(s) of plugins * * @return smarty current smarty instance for chaining */ public function setpluginsdir($plugins_dir) { $this->plugins_dir = array(); foreach ((array) $plugins_dir as $k => $v) { $this->plugins_dir[$k] = rtrim($v, '/\\') . ds; } return $this; } /** * adds directory of plugin files * * @param $plugins_dir * * @return smarty current smarty instance for chaining */ public function addpluginsdir($plugins_dir) { // make sure we're dealing with an array $this->plugins_dir = (array) $this->plugins_dir; if (is_array($plugins_dir)) { foreach ($plugins_dir as $k => $v) { if (is_int($k)) { // indexes are not merged but appended $this->plugins_dir[] = rtrim($v, '/\\') . ds; } else { // string indexes are overridden $this->plugins_dir[$k] = rtrim($v, '/\\') . ds; } } } else { // append new directory $this->plugins_dir[] = rtrim($plugins_dir, '/\\') . ds; } $this->plugins_dir = array_unique($this->plugins_dir); return $this; } /** * get plugin directories * * @return array list of plugin directories */ public function getpluginsdir() { return (array) $this->plugins_dir; } /** * set compile directory * * @param string $compile_dir directory to store compiled templates in * * @return smarty current smarty instance for chaining */ public function setcompiledir($compile_dir) { $this->compile_dir = rtrim($compile_dir, '/\\') . ds; if (!isset(smarty::$_muted_directories[$this->compile_dir])) { smarty::$_muted_directories[$this->compile_dir] = null; } return $this; } /** * get compiled directory * * @return string path to compiled templates */ public function getcompiledir() { return $this->compile_dir; } /** * set cache directory * * @param string $cache_dir directory to store cached templates in * * @return smarty current smarty instance for chaining */ public function setcachedir($cache_dir) { $this->cache_dir = rtrim($cache_dir, '/\\') . ds; if (!isset(smarty::$_muted_directories[$this->cache_dir])) { smarty::$_muted_directories[$this->cache_dir] = null; } return $this; } /** * get cache directory * * @return string path of cache directory */ public function getcachedir() { return $this->cache_dir; } /** * set default modifiers * * @param array|string $modifiers modifier or list of modifiers to set * * @return smarty current smarty instance for chaining */ public function setdefaultmodifiers($modifiers) { $this->default_modifiers = (array) $modifiers; return $this; } /** * add default modifiers * * @param array|string $modifiers modifier or list of modifiers to add * * @return smarty current smarty instance for chaining */ public function adddefaultmodifiers($modifiers) { if (is_array($modifiers)) { $this->default_modifiers = array_merge($this->default_modifiers, $modifiers); } else { $this->default_modifiers[] = $modifiers; } return $this; } /** * get default modifiers * * @return array list of default modifiers */ public function getdefaultmodifiers() { return $this->default_modifiers; } /** * set autoload filters * * @param array $filters filters to load automatically * @param string $type "pre", "output", … specify the filter type to set. defaults to none treating $filters' keys as the appropriate types * * @return smarty current smarty instance for chaining */ public function setautoloadfilters($filters, $type = null) { if ($type !== null) { $this->autoload_filters[$type] = (array) $filters; } else { $this->autoload_filters = (array) $filters; } return $this; } /** * add autoload filters * * @param array $filters filters to load automatically * @param string $type "pre", "output", … specify the filter type to set. defaults to none treating $filters' keys as the appropriate types * * @return smarty current smarty instance for chaining */ public function addautoloadfilters($filters, $type = null) { if ($type !== null) { if (!empty($this->autoload_filters[$type])) { $this->autoload_filters[$type] = array_merge($this->autoload_filters[$type], (array) $filters); } else { $this->autoload_filters[$type] = (array) $filters; } } else { foreach ((array) $filters as $key => $value) { if (!empty($this->autoload_filters[$key])) { $this->autoload_filters[$key] = array_merge($this->autoload_filters[$key], (array) $value); } else { $this->autoload_filters[$key] = (array) $value; } } } return $this; } /** * get autoload filters * * @param string $type type of filter to get autoloads for. defaults to all autoload filters * * @return array array( 'type1' => array( 'filter1', 'filter2', … ) ) or array( 'filter1', 'filter2', …) if $type was specified */ public function getautoloadfilters($type = null) { if ($type !== null) { return isset($this->autoload_filters[$type]) ? $this->autoload_filters[$type] : array(); } return $this->autoload_filters; } /** * return name of debugging template * * @return string */ public function getdebugtemplate() { return $this->debug_tpl; } /** * set the debug template * * @param string $tpl_name * * @return smarty current smarty instance for chaining * @throws smartyexception if file is not readable */ public function setdebugtemplate($tpl_name) { if (!is_readable($tpl_name)) { throw new smartyexception("unknown file '{$tpl_name}'"); } $this->debug_tpl = $tpl_name; return $this; } /** * creates a template object * * @param string $template the resource handle of the template file * @param mixed $cache_id cache id to be used with this template * @param mixed $compile_id compile id to be used with this template * @param object $parent next higher level of smarty variables * @param boolean $do_clone flag is smarty object shall be cloned * * @return object template object */ public function createtemplate($template, $cache_id = null, $compile_id = null, $parent = null, $do_clone = true) { if ($cache_id !== null && (is_object($cache_id) || is_array($cache_id))) { $parent = $cache_id; $cache_id = null; } if ($parent !== null && is_array($parent)) { $data = $parent; $parent = null; } else { $data = null; } // default to cache_id and compile_id of smarty object $cache_id = $cache_id === null ? $this->cache_id : $cache_id; $compile_id = $compile_id === null ? $this->compile_id : $compile_id; // already in template cache? if ($this->allow_ambiguous_resources) { $_templateid = smarty_resource::getuniquetemplatename($this, $template) . $cache_id . $compile_id; } else { $_templateid = $this->joined_template_dir . '#' . $template . $cache_id . $compile_id; } if (isset($_templateid[150])) { $_templateid = sha1($_templateid); } if ($do_clone) { if (isset($this->template_objects[$_templateid])) { // return cached template object $tpl = clone $this->template_objects[$_templateid]; $tpl->smarty = clone $tpl->smarty; $tpl->parent = $parent; $tpl->tpl_vars = array(); $tpl->config_vars = array(); } else { $tpl = new $this->template_class($template, clone $this, $parent, $cache_id, $compile_id); } } else { if (isset($this->template_objects[$_templateid])) { // return cached template object $tpl = $this->template_objects[$_templateid]; $tpl->parent = $parent; $tpl->tpl_vars = array(); $tpl->config_vars = array(); } else { $tpl = new $this->template_class($template, $this, $parent, $cache_id, $compile_id); } } // fill data if present if (!empty($data) && is_array($data)) { // set up variable values foreach ($data as $_key => $_val) { $tpl->tpl_vars[$_key] = new smarty_variable($_val); } } return $tpl; } /** * takes unknown classes and loads plugin files for them * class name format: smarty_plugintype_pluginname * plugin filename format: plugintype.pluginname.php * * @param string $plugin_name class plugin name to load * @param bool $check check if already loaded * * @throws smartyexception * @return string |boolean filepath of loaded file or false */ public function loadplugin($plugin_name, $check = true) { // if function or class exists, exit silently (already loaded) if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false))) { return true; } // plugin name is expected to be: smarty_[type]_[name] $_name_parts = explode('_', $plugin_name, 3); // class name must have three parts to be valid plugin // count($_name_parts) < 3 === !isset($_name_parts[2]) if (!isset($_name_parts[2]) || strtolower($_name_parts[0]) !== 'smarty') { throw new smartyexception("plugin {$plugin_name} is not a valid name format"); } // if type is "internal", get plugin from sysplugins if (strtolower($_name_parts[1]) == 'internal') { $file = smarty_sysplugins_dir . strtolower($plugin_name) . '.php'; if (file_exists($file)) { require_once($file); return $file; } else { return false; } } // plugin filename is expected to be: [type].[name].php $_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php"; $_stream_resolve_include_path = function_exists('stream_resolve_include_path'); // loop through plugin dirs and find the plugin foreach ($this->getpluginsdir() as $_plugin_dir) { $names = array( $_plugin_dir . $_plugin_filename, $_plugin_dir . strtolower($_plugin_filename), ); foreach ($names as $file) { if (file_exists($file)) { require_once($file); return $file; } if ($this->use_include_path && !preg_match('/^([\/\\\\]|[a-za-z]:[\/\\\\])/', $_plugin_dir)) { // try php include_path if ($_stream_resolve_include_path) { $file = stream_resolve_include_path($file); } else { $file = smarty_internal_get_include_path::getincludepath($file); } if ($file !== false) { require_once($file); return $file; } } } } // no plugin loaded return false; } /** * compile all template files * * @param string $extension file extension * @param bool $force_compile force all to recompile * @param int $time_limit * @param int $max_errors * * @return integer number of template files recompiled */ public function compilealltemplates($extension = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null) { return smarty_internal_utility::compilealltemplates($extension, $force_compile, $time_limit, $max_errors, $this); } /** * compile all config files * * @param string $extension file extension * @param bool $force_compile force all to recompile * @param int $time_limit * @param int $max_errors * * @return integer number of template files recompiled */ public function compileallconfig($extension = '.conf', $force_compile = false, $time_limit = 0, $max_errors = null) { return smarty_internal_utility::compileallconfig($extension, $force_compile, $time_limit, $max_errors, $this); } /** * delete compiled template file * * @param string $resource_name template name * @param string $compile_id compile id * @param integer $exp_time expiration time * * @return integer number of template files deleted */ public function clearcompiledtemplate($resource_name = null, $compile_id = null, $exp_time = null) { return smarty_internal_utility::clearcompiledtemplate($resource_name, $compile_id, $exp_time, $this); } /** * return array of tag/attributes of all tags used by an template * * @param smarty_internal_template $template * * @return array of tag/attributes */ public function gettags(smarty_internal_template $template) { return smarty_internal_utility::gettags($template); } /** * run installation test * * @param array $errors array to write errors into, rather than outputting them * * @return boolean true if setup is fine, false if something is wrong */ public function testinstall(&$errors = null) { return smarty_internal_utility::testinstall($this, $errors); } /** * error handler to mute expected messages * * @link http://php.net/set_error_handler * * @param integer $errno error level * @param $errstr * @param $errfile * @param $errline * @param $errcontext * * @return boolean */ public static function mutingerrorhandler($errno, $errstr, $errfile, $errline, $errcontext) { $_is_muted_directory = false; // add the smarty_dir to the list of muted directories if (!isset(smarty::$_muted_directories[smarty_dir])) { $smarty_dir = realpath(smarty_dir); if ($smarty_dir !== false) { smarty::$_muted_directories[smarty_dir] = array( 'file' => $smarty_dir, 'length' => strlen($smarty_dir), ); } } // walk the muted directories and test against $errfile foreach (smarty::$_muted_directories as $key => &$dir) { if (!$dir) { // resolve directory and length for speedy comparisons $file = realpath($key); if ($file === false) { // this directory does not exist, remove and skip it unset(smarty::$_muted_directories[$key]); continue; } $dir = array( 'file' => $file, 'length' => strlen($file), ); } if (!strncmp($errfile, $dir['file'], $dir['length'])) { $_is_muted_directory = true; break; } } // pass to next error handler if this error did not occur inside smarty_dir // or the error was within smarty but masked to be ignored if (!$_is_muted_directory || ($errno && $errno & error_reporting())) { if (smarty::$_previous_error_handler) { return call_user_func(smarty::$_previous_error_handler, $errno, $errstr, $errfile, $errline, $errcontext); } else { return false; } } } /** * enable error handler to mute expected messages * * @return void */ public static function muteexpectederrors() { /* error muting is done because some people implemented custom error_handlers using http://php.net/set_error_handler and for some reason did not understand the following paragraph: it is important to remember that the standard php error handler is completely bypassed for the error types specified by error_types unless the callback function returns false. error_reporting() settings will have no effect and your error handler will be called regardless - however you are still able to read the current value of error_reporting and act appropriately. of particular note is that this value will be 0 if the statement that caused the error was prepended by the @ error-control operator. smarty deliberately uses @filemtime() over file_exists() and filemtime() in some places. reasons include - @filemtime() is almost twice as fast as using an additional file_exists() - between file_exists() and filemtime() a possible race condition is opened, which does not exist using the simple @filemtime() approach. */ $error_handler = array('smarty', 'mutingerrorhandler'); $previous = set_error_handler($error_handler); // avoid dead loops if ($previous !== $error_handler) { smarty::$_previous_error_handler = $previous; } } /** * disable error handler muting expected messages * * @return void */ public static function unmuteexpectederrors() { restore_error_handler(); } } // check if we're running on windows smarty::$_is_windows = strtoupper(substr(php_os, 0, 3)) === 'win'; // let pcre (preg_*) treat strings as iso-8859-1 if we're not dealing with utf-8 if (smarty::$_charset !== 'utf-8') { smarty::$_utf8_modifier = ''; } /** * smarty exception class * * @package smarty */ class smartyexception extends exception { public static $escape = false; public function __tostring() { return ' --> smarty: ' . (self::$escape ? htmlentities($this->message) : $this->message) . ' <-- '; } } /** * smarty compiler exception class * * @package smarty */ class smartycompilerexception extends smartyexception { public function __tostring() { return ' --> smarty compiler: ' . $this->message . ' <-- '; } /** * the line number of the template error * * @type int|null */ public $line = null; /** * the template source snippet relating to the error * * @type string|null */ public $source = null; /** * the raw text of the error message * * @type string|null */ public $desc = null; /** * the resource identifier or template name * * @type string|null */ public $template = null; } /** * autoloader */ function smartyautoload($class) { $_class = strtolower($class); static $_classes = array( 'smarty_config_source' => true, 'smarty_config_compiled' => true, 'smarty_security' => true, 'smarty_cacheresource' => true, 'smarty_cacheresource_custom' => true, 'smarty_cacheresource_keyvaluestore' => true, 'smarty_resource' => true, 'smarty_resource_custom' => true, 'smarty_resource_uncompiled' => true, 'smarty_resource_recompiled' => true, ); if (!strncmp($_class, 'smarty_internal_', 16) || isset($_classes[$_class])) { include smarty_sysplugins_dir . $_class . '.php'; } }
更多关于smarty相关内容感兴趣的读者可查看本站专题:《smarty模板入门基础教程》、《php模板技术总结》、《php基于pdo操作数据库技巧总结》、《php运算与运算符用法总结》、《php网络编程技巧总结》、《php基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于smarty模板的php程序设计有所帮助。
上一篇: Python队列的定义与使用方法示例