栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

php封装的smarty类完整实例

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

php封装的smarty类完整实例

本文实例讲述了php封装的smarty类。分享给大家供大家参考,具体如下:

 print tags as plain text
  const PHP_QUOTE = 1; //-> escape tags as entities
  const PHP_REMOVE = 2; //-> escape tags as entities
  const PHP_ALLOW = 3; //-> escape tags as entities
  
  const FILTER_POST = 'post';
  const FILTER_PRE = 'pre';
  const FILTER_OUTPUT = 'output';
  const FILTER_VARIABLE = 'variable';
  
  const PLUGIN_FUNCTION = 'function';
  const PLUGIN_BLOCK = 'block';
  const PLUGIN_COMPILER = 'compiler';
  const PLUGIN_MODIFIER = 'modifier';
  const PLUGIN_MODIFIERCOMPILER = 'modifiercompiler';
  
  
  public static $global_tpl_vars = array();
  
  public static $_previous_error_handler = null;
  
  public static $_muted_directories = array();
  
  public static $_MBSTRING = SMARTY_MBSTRING;
  
  public static $_CHARSET = SMARTY_RESOURCE_CHAR_SET;
  
  public static $_DATE_FORMAT = SMARTY_RESOURCE_DATE_FORMAT;
  
  public static $_UTF8_MODIFIER = 'u';
  
  public static $_IS_WINDOWS = false;
  
  
  public $auto_literal = true;
  
  public $error_unassigned = false;
  
  public $use_include_path = false;
  
  private $template_dir = array();
  
  public $joined_template_dir = null;
  
  public $joined_config_dir = null;
  
  public $default_template_handler_func = null;
  
  public $default_config_handler_func = null;
  
  public $default_plugin_handler_func = null;
  
  private $compile_dir = null;
  
  private $plugins_dir = array();
  
  private $cache_dir = null;
  
  private $config_dir = array();
  
  public $force_compile = false;
  
  public $compile_check = true;
  
  public $use_sub_dirs = false;
  
  public $allow_ambiguous_resources = false;
  
  public $caching = false;
  
  public $merge_compiled_includes = false;
  
  public $inheritance_merge_compiled_includes = true;
  
  public $cache_lifetime = 3600;
  
  public $force_cache = false;
  
  public $cache_id = null;
  
  public $compile_id = null;
  
  public $left_delimiter = "{";
  
  public $right_delimiter = "}";
  
  
  public $security_class = 'Smarty_Security';
  
  public $security_policy = null;
  
  public $php_handling = self::PHP_PASSTHRU;
  
  public $allow_php_templates = false;
  
  public $direct_access_security = true;
  
  
  public $debugging = false;
  
  public $debugging_ctrl = 'NONE';
  
  public $smarty_debug_id = 'SMARTY_DEBUG';
  
  public $debug_tpl = null;
  
  public $error_reporting = null;
  
  public $get_used_tags = false;
  
  
  public $config_overwrite = true;
  
  public $config_booleanize = true;
  
  public $config_read_hidden = false;
  
  
  
  public $compile_locking = true;
  
  public $cache_locking = false;
  
  public $locking_timeout = 10;
  
  
  public $template_functions = array();
  
  public $default_resource_type = 'file';
  
  public $caching_type = 'file';
  
  public $properties = array();
  
  public $default_config_type = 'file';
  
  public $template_objects = array();
  
  public $cache_modified_check = false;
  
  public $registered_plugins = array();
  
  public $plugin_search_order = array('function', 'block', 'compiler', 'class');
  
  public $registered_objects = array();
  
  public $registered_classes = array();
  
  public $registered_filters = array();
  
  public $registered_resources = array();
  
  public $_resource_handlers = array();
  
  public $registered_cache_resources = array();
  
  public $_cacheresource_handlers = array();
  
  public $autoload_filters = array();
  
  public $default_modifiers = array();
  
  public $escape_html = false;
  
  public static $_smarty_vars = array();
  
  public $start_time = 0;
  
  public $_file_perms = 0644;
  
  public $_dir_perms = 0771;
  
  public $_tag_stack = array();
  
  public $smarty;
  
  public $_current_file = null;
  
  public $_parserdebug = false;
  
  public $merged_templates_func = array();
  
  
  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']);
    }
  }
  
  public function __destruct()
  {
    // intentionally left blank
  }
  
  public function __clone()
  {
    $this->smarty = $this;
  }
  
  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);
    }
  }
  
  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);
    }
  }
  
  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;
  }
  
  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;
    }
  }
  
  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);
  }
  
  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);
  }
  
  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;
  }
  
  public function disableSecurity()
  {
    $this->security_policy = null;
    return $this;
  }
  
  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;
  }
  
  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;
  }
  
  public function getTemplateDir($index = null)
  {
    if ($index !== null) {
      return isset($this->template_dir[$index]) ? $this->template_dir[$index] : null;
    }
    return (array) $this->template_dir;
  }
  
  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;
  }
  
  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;
  }
  
  public function getConfigDir($index = null)
  {
    if ($index !== null) {
      return isset($this->config_dir[$index]) ? $this->config_dir[$index] : null;
    }
    return (array) $this->config_dir;
  }
  
  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;
  }
  
  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;
  }
  
  public function getPluginsDir()
  {
    return (array) $this->plugins_dir;
  }
  
  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;
  }
  
  public function getCompileDir()
  {
    return $this->compile_dir;
  }
  
  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;
  }
  
  public function getCacheDir()
  {
    return $this->cache_dir;
  }
  
  public function setDefaultModifiers($modifiers)
  {
    $this->default_modifiers = (array) $modifiers;
    return $this;
  }
  
  public function addDefaultModifiers($modifiers)
  {
    if (is_array($modifiers)) {
      $this->default_modifiers = array_merge($this->default_modifiers, $modifiers);
    } else {
      $this->default_modifiers[] = $modifiers;
    }
    return $this;
  }
  
  public function getDefaultModifiers()
  {
    return $this->default_modifiers;
  }
  
  public function setAutoloadFilters($filters, $type = null)
  {
    if ($type !== null) {
      $this->autoload_filters[$type] = (array) $filters;
    } else {
      $this->autoload_filters = (array) $filters;
    }
    return $this;
  }
  
  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;
  }
  
  public function getAutoloadFilters($type = null)
  {
    if ($type !== null) {
      return isset($this->autoload_filters[$type]) ? $this->autoload_filters[$type] : array();
    }
    return $this->autoload_filters;
  }
  
  public function getDebugTemplate()
  {
    return $this->debug_tpl;
  }
  
  public function setDebugTemplate($tpl_name)
  {
    if (!is_readable($tpl_name)) {
      throw new SmartyException("Unknown file '{$tpl_name}'");
    }
    $this->debug_tpl = $tpl_name;
    return $this;
  }
  
  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;
  }
  
  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;
  }
  
  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);
  }
  
  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);
  }
  
  public function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
  {
    return Smarty_Internal_Utility::clearCompiledTemplate($resource_name, $compile_id, $exp_time, $this);
  }
  
  public function getTags(Smarty_Internal_Template $template)
  {
    return Smarty_Internal_Utility::getTags($template);
  }
  
  public function testInstall(&$errors = null)
  {
    return Smarty_Internal_Utility::testInstall($this, $errors);
  }
  
  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;
      }
    }
  }
  
  public static function muteExpectedErrors()
  {
    
    $error_handler = array('Smarty', 'mutingErrorHandler');
    $previous = set_error_handler($error_handler);
    // avoid dead loops
    if ($previous !== $error_handler) {
      Smarty::$_previous_error_handler = $previous;
    }
  }
  
  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 = '';
}

class SmartyException extends Exception
{
  public static $escape = false;
  public function __toString()
  {
    return ' --> Smarty: ' . (self::$escape ? htmlentities($this->message) : $this->message) . ' <-- ';
  }
}

class SmartyCompilerException extends SmartyException
{
  public function __toString()
  {
    return ' --> Smarty Compiler: ' . $this->message . ' <-- ';
  }
  
  public $line = null;
  
  public $source = null;
  
  public $desc = null;
  
  public $template = null;
}

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';
  }
}
 print tags as plain text
  const PHP_QUOTE = 1; //-> escape tags as entities
  const PHP_REMOVE = 2; //-> escape tags as entities
  const PHP_ALLOW = 3; //-> escape tags as entities
  
  const FILTER_POST = 'post';
  const FILTER_PRE = 'pre';
  const FILTER_OUTPUT = 'output';
  const FILTER_VARIABLE = 'variable';
  
  const PLUGIN_FUNCTION = 'function';
  const PLUGIN_BLOCK = 'block';
  const PLUGIN_COMPILER = 'compiler';
  const PLUGIN_MODIFIER = 'modifier';
  const PLUGIN_MODIFIERCOMPILER = 'modifiercompiler';
  
  
  public static $global_tpl_vars = array();
  
  public static $_previous_error_handler = null;
  
  public static $_muted_directories = array();
  
  public static $_MBSTRING = SMARTY_MBSTRING;
  
  public static $_CHARSET = SMARTY_RESOURCE_CHAR_SET;
  
  public static $_DATE_FORMAT = SMARTY_RESOURCE_DATE_FORMAT;
  
  public static $_UTF8_MODIFIER = 'u';
  
  public static $_IS_WINDOWS = false;
  
  
  public $auto_literal = true;
  
  public $error_unassigned = false;
  
  public $use_include_path = false;
  
  private $template_dir = array();
  
  public $joined_template_dir = null;
  
  public $joined_config_dir = null;
  
  public $default_template_handler_func = null;
  
  public $default_config_handler_func = null;
  
  public $default_plugin_handler_func = null;
  
  private $compile_dir = null;
  
  private $plugins_dir = array();
  
  private $cache_dir = null;
  
  private $config_dir = array();
  
  public $force_compile = false;
  
  public $compile_check = true;
  
  public $use_sub_dirs = false;
  
  public $allow_ambiguous_resources = false;
  
  public $caching = false;
  
  public $merge_compiled_includes = false;
  
  public $inheritance_merge_compiled_includes = true;
  
  public $cache_lifetime = 3600;
  
  public $force_cache = false;
  
  public $cache_id = null;
  
  public $compile_id = null;
  
  public $left_delimiter = "{";
  
  public $right_delimiter = "}";
  
  
  public $security_class = 'Smarty_Security';
  
  public $security_policy = null;
  
  public $php_handling = self::PHP_PASSTHRU;
  
  public $allow_php_templates = false;
  
  public $direct_access_security = true;
  
  
  public $debugging = false;
  
  public $debugging_ctrl = 'NONE';
  
  public $smarty_debug_id = 'SMARTY_DEBUG';
  
  public $debug_tpl = null;
  
  public $error_reporting = null;
  
  public $get_used_tags = false;
  
  
  public $config_overwrite = true;
  
  public $config_booleanize = true;
  
  public $config_read_hidden = false;
  
  
  
  public $compile_locking = true;
  
  public $cache_locking = false;
  
  public $locking_timeout = 10;
  
  
  public $template_functions = array();
  
  public $default_resource_type = 'file';
  
  public $caching_type = 'file';
  
  public $properties = array();
  
  public $default_config_type = 'file';
  
  public $template_objects = array();
  
  public $cache_modified_check = false;
  
  public $registered_plugins = array();
  
  public $plugin_search_order = array('function', 'block', 'compiler', 'class');
  
  public $registered_objects = array();
  
  public $registered_classes = array();
  
  public $registered_filters = array();
  
  public $registered_resources = array();
  
  public $_resource_handlers = array();
  
  public $registered_cache_resources = array();
  
  public $_cacheresource_handlers = array();
  
  public $autoload_filters = array();
  
  public $default_modifiers = array();
  
  public $escape_html = false;
  
  public static $_smarty_vars = array();
  
  public $start_time = 0;
  
  public $_file_perms = 0644;
  
  public $_dir_perms = 0771;
  
  public $_tag_stack = array();
  
  public $smarty;
  
  public $_current_file = null;
  
  public $_parserdebug = false;
  
  public $merged_templates_func = array();
  
  
  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']);
    }
  }
  
  public function __destruct()
  {
    // intentionally left blank
  }
  
  public function __clone()
  {
    $this->smarty = $this;
  }
  
  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);
    }
  }
  
  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);
    }
  }
  
  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;
  }
  
  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;
    }
  }
  
  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);
  }
  
  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);
  }
  
  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;
  }
  
  public function disableSecurity()
  {
    $this->security_policy = null;
    return $this;
  }
  
  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;
  }
  
  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;
  }
  
  public function getTemplateDir($index = null)
  {
    if ($index !== null) {
      return isset($this->template_dir[$index]) ? $this->template_dir[$index] : null;
    }
    return (array) $this->template_dir;
  }
  
  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;
  }
  
  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;
  }
  
  public function getConfigDir($index = null)
  {
    if ($index !== null) {
      return isset($this->config_dir[$index]) ? $this->config_dir[$index] : null;
    }
    return (array) $this->config_dir;
  }
  
  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;
  }
  
  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;
  }
  
  public function getPluginsDir()
  {
    return (array) $this->plugins_dir;
  }
  
  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;
  }
  
  public function getCompileDir()
  {
    return $this->compile_dir;
  }
  
  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;
  }
  
  public function getCacheDir()
  {
    return $this->cache_dir;
  }
  
  public function setDefaultModifiers($modifiers)
  {
    $this->default_modifiers = (array) $modifiers;
    return $this;
  }
  
  public function addDefaultModifiers($modifiers)
  {
    if (is_array($modifiers)) {
      $this->default_modifiers = array_merge($this->default_modifiers, $modifiers);
    } else {
      $this->default_modifiers[] = $modifiers;
    }
    return $this;
  }
  
  public function getDefaultModifiers()
  {
    return $this->default_modifiers;
  }
  
  public function setAutoloadFilters($filters, $type = null)
  {
    if ($type !== null) {
      $this->autoload_filters[$type] = (array) $filters;
    } else {
      $this->autoload_filters = (array) $filters;
    }
    return $this;
  }
  
  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;
  }
  
  public function getAutoloadFilters($type = null)
  {
    if ($type !== null) {
      return isset($this->autoload_filters[$type]) ? $this->autoload_filters[$type] : array();
    }
    return $this->autoload_filters;
  }
  
  public function getDebugTemplate()
  {
    return $this->debug_tpl;
  }
  
  public function setDebugTemplate($tpl_name)
  {
    if (!is_readable($tpl_name)) {
      throw new SmartyException("Unknown file '{$tpl_name}'");
    }
    $this->debug_tpl = $tpl_name;
    return $this;
  }
  
  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;
  }
  
  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;
  }
  
  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);
  }
  
  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);
  }
  
  public function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
  {
    return Smarty_Internal_Utility::clearCompiledTemplate($resource_name, $compile_id, $exp_time, $this);
  }
  
  public function getTags(Smarty_Internal_Template $template)
  {
    return Smarty_Internal_Utility::getTags($template);
  }
  
  public function testInstall(&$errors = null)
  {
    return Smarty_Internal_Utility::testInstall($this, $errors);
  }
  
  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;
      }
    }
  }
  
  public static function muteExpectedErrors()
  {
    
    $error_handler = array('Smarty', 'mutingErrorHandler');
    $previous = set_error_handler($error_handler);
    // avoid dead loops
    if ($previous !== $error_handler) {
      Smarty::$_previous_error_handler = $previous;
    }
  }
  
  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 = '';
}

class SmartyException extends Exception
{
  public static $escape = false;
  public function __toString()
  {
    return ' --> Smarty: ' . (self::$escape ? htmlentities($this->message) : $this->message) . ' <-- ';
  }
}

class SmartyCompilerException extends SmartyException
{
  public function __toString()
  {
    return ' --> Smarty Compiler: ' . $this->message . ' <-- ';
  }
  
  public $line = null;
  
  public $source = null;
  
  public $desc = null;
  
  public $template = null;
}

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程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/42315.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号