[ Index ]

krapohl.info

title

Body

[close]

/pnincludes/Smarty/ -> Smarty.class.php (source)

   1  <?php
   2  /*
   3   * Project:     Smarty: the PHP compiling template engine
   4   * File:        Smarty.class.php
   5   * Author:      Monte Ohrt <monte@ispi.net>
   6   *              Andrei Zmievski <andrei@php.net>
   7   *
   8   * Version:     2.3.1
   9   * Copyright:   2001,2002 ispi of Lincoln, Inc.
  10   *
  11   * This library is free software; you can redistribute it and/or
  12   * modify it under the terms of the GNU Lesser General Public
  13   * License as published by the Free Software Foundation; either
  14   * version 2.1 of the License, or (at your option) any later version.
  15   *
  16   * This library is distributed in the hope that it will be useful,
  17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19   * Lesser General Public License for more details.
  20   *
  21   * You should have received a copy of the GNU Lesser General Public
  22   * License along with this library; if not, write to the Free Software
  23   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  24   *
  25   * For questions, help, comments, discussion, etc., please join the
  26   * Smarty mailing list. Send a blank e-mail to
  27   * smarty-general-subscribe@lists.php.net
  28   *
  29   * You may contact the authors of Smarty by e-mail at:
  30   * monte@ispi.net
  31   * andrei@php.net
  32   *
  33   * Or, write to:
  34   * Monte Ohrt
  35   * Director of Technology, ispi
  36   * 237 S. 70th suite 220
  37   * Lincoln, NE 68510
  38   *
  39   * The latest version of Smarty can be obtained from:
  40   * http://www.phpinsider.com/
  41   *
  42   */
  43  
  44  // set SMARTY_DIR to absolute path to Smarty library files.
  45  // if not defined, include_path will be used.
  46  
  47  define('DIR_SEP', DIRECTORY_SEPARATOR);
  48  
  49  if (!defined('SMARTY_DIR')) {
  50      define('SMARTY_DIR', dirname(__FILE__) . DIR_SEP);
  51  }
  52  
  53  define('SMARTY_PHP_PASSTHRU',   0);
  54  define('SMARTY_PHP_QUOTE',      1);
  55  define('SMARTY_PHP_REMOVE',     2);
  56  define('SMARTY_PHP_ALLOW',      3);
  57  
  58  class Smarty
  59  {
  60  
  61  /**************************************************************************/
  62  /* BEGIN SMARTY CONFIGURATION SECTION                                     */
  63  /* Set the following config variables to your liking.                     */
  64  /**************************************************************************/
  65  
  66      // public vars
  67      var $template_dir    =  'templates';       // name of directory for templates
  68      var $compile_dir     =  'templates_c';     // name of directory for compiled templates
  69      var $config_dir      =  'configs';         // directory where config files are located
  70      var $plugins_dir     =  array('plugins');  // plugin directories
  71  
  72      var $debugging       =  false;             // enable debugging console true/false
  73      var $debug_tpl       =  '';                // path to debug console template
  74                                                 // (this gets set in the constructor)
  75      var $debugging_ctrl  =  'NONE';            // Possible values:
  76                                                 // NONE - no debug control allowed
  77                                                 // URL - enable debugging when keyword
  78                                                 //       SMARTY_DEBUG is found in $QUERY_STRING
  79  
  80      var $global_assign   =  array( 'HTTP_SERVER_VARS' => array( 'SCRIPT_NAME' )
  81                                   );     // variables from the GLOBALS array
  82                                          // that are implicitly assigned
  83                                          // to all templates
  84      var $undefined       =  null;       // undefined variables in $global_assign will be
  85                                          // created with this value
  86      var $autoload_filters = array();    // indicates which filters will be auto-loaded
  87  
  88      var $compile_check   =  true;       // whether to check for compiling step or not:
  89                                          // This is generally set to false once the
  90                                          // application is entered into production and
  91                                          // initially compiled. Leave set to true
  92                                          // during development. true/false default true.
  93  
  94      var $force_compile   =  false;      // force templates to compile every time,
  95                                          // overrides cache settings. default false.
  96  
  97      var $caching         =  0;             // enable caching. can be one of 0/1/2.
  98                                          // 0 = no caching
  99                                          // 1 = use class cache_lifetime value
 100                                          // 2 = use cache_lifetime in cache file
 101                                          // default = 0.
 102      var $cache_dir       =  'cache';    // name of directory for template cache files
 103      var $cache_lifetime  =  3600;       // number of seconds cached content will persist.
 104                                          // 0 = always regenerate cache,
 105                                          // -1 = never expires. default is one hour (3600)
 106      var $cache_handler_func   = null;   // function used for cached content. this is
 107                                          // an alternative to using the built-in file
 108                                          // based caching.
 109      var $cache_modified_check = false;  // respect If-Modified-Since headers on cached content
 110  
 111  
 112      var $default_template_handler_func = ''; // function to handle missing templates
 113  
 114      var $php_handling    =  SMARTY_PHP_PASSTHRU;
 115                                          // how smarty handles php tags in the templates
 116                                          // possible values:
 117                                          // SMARTY_PHP_PASSTHRU -> echo tags as is
 118                                          // SMARTY_PHP_QUOTE    -> escape tags as entities
 119                                          // SMARTY_PHP_REMOVE   -> remove php tags
 120                                          // SMARTY_PHP_ALLOW    -> execute php tags
 121                                          // default: SMARTY_PHP_PASSTHRU
 122  
 123  
 124      var $security       =   false;      // enable template security (default false)
 125      var $secure_dir     =   array('templates'); // array of directories considered secure
 126      var $security_settings  = array(
 127                                      'PHP_HANDLING'    => false,
 128                                      'IF_FUNCS'        => array('array', 'list',
 129                                                                 'isset', 'empty',
 130                                                                 'count', 'sizeof',
 131                                                                 'in_array', 'is_array'),
 132                                      'INCLUDE_ANY'     => false,
 133                                      'PHP_TAGS'        => false,
 134                                      'MODIFIER_FUNCS'  => array('count')
 135                                     );
 136      var $trusted_dir        = array();  // directories where trusted templates & php scripts
 137                                          // reside ($security is disabled during their
 138                                          // inclusion/execution).
 139  
 140      var $left_delimiter  =  '{';        // template tag delimiters.
 141      var $right_delimiter =  '}';
 142  
 143      var $compiler_class        =   'Smarty_Compiler'; // the compiler class used by
 144                                                        // Smarty to compile templates
 145  
 146      var $request_vars_order    = "EGPCS";   // the order in which request variables are
 147                                              // registered, similar to variables_order
 148                                              // in php.ini
 149  
 150      var $compile_id            = null;      // persistent compile identifier
 151      var $use_sub_dirs          = true;        // use sub dirs for cache and compiled files?
 152                                              // sub directories are more efficient, but
 153                                              // you can set this to false if your PHP environment
 154                                              // does not allow the creation of them.
 155      var $default_modifiers        = array();
 156                                              // modifiers to implicitly append to every var
 157                                              // example: array('escape:"htmlall"');
 158  
 159  /**************************************************************************/
 160  /* END SMARTY CONFIGURATION SECTION                                       */
 161  /* There should be no need to touch anything below this line.             */
 162  /**************************************************************************/
 163  
 164      // internal vars
 165      var $_error_msg            = false;      // error messages. true/false
 166      var $_tpl_vars             = array();    // where assigned template vars are kept
 167      var $_smarty_vars          = null;       // stores run-time $smarty.* vars
 168      var $_sections             = array();    // keeps track of sections
 169      var $_foreach              = array();    // keeps track of foreach blocks
 170      var $_tag_stack            = array();    // keeps track of tag hierarchy
 171      var $_conf_obj             = null;       // configuration object
 172      var $_config               = array();    // loaded configuration settings
 173      var $_smarty_md5           = 'f8d698aea36fcbead2b9d5359ffca76f'; // md5 checksum of the string 'Smarty'
 174      var $_version              = '2.3.1';    // Smarty version number
 175      var $_extract              = false;      // flag for custom functions
 176      var $_inclusion_depth      = 0;          // current template inclusion depth
 177      var $_compile_id           = null;       // for different compiled templates
 178      var $_smarty_debug_id      = 'SMARTY_DEBUG'; // text in URL to enable debug mode
 179      var $_smarty_debug_info    = array();    // debugging information for debug console
 180      var $_cache_info           = array();    // info that makes up a cache file
 181      var $_plugins              = array(      // table keeping track of plugins
 182                                         'modifier'      => array(),
 183                                         'function'      => array(),
 184                                         'block'         => array(),
 185                                         'compiler'      => array(),
 186                                         'prefilter'     => array(),
 187                                         'postfilter'    => array(),
 188                                         'outputfilter'  => array(),
 189                                         'resource'      => array(),
 190                                         'insert'        => array());
 191  
 192  
 193  /*======================================================================*\
 194      Function: Smarty
 195      Purpose:  Constructor
 196  \*======================================================================*/
 197      function Smarty()
 198      {
 199          foreach ($this->global_assign as $key => $var_name) {
 200              if (is_array($var_name)) {
 201                  foreach ($var_name as $var) {
 202                      if (isset($GLOBALS[$key][$var])) {
 203                          $this->assign($var, $GLOBALS[$key][$var]);
 204                      } else {
 205                          $this->assign($var, $this->undefined);
 206                      }
 207                  }
 208              } else {
 209                  if (isset($GLOBALS[$var_name])) {
 210                      $this->assign($var_name, $GLOBALS[$var_name]);
 211                  } else {
 212                      $this->assign($var_name, $this->undefined);
 213                  }
 214              }
 215          }
 216  
 217          if(empty($this->debug_tpl)) {
 218              // set path to debug template from SMARTY_DIR
 219              $this->debug_tpl = 'file:'.SMARTY_DIR.'debug.tpl';
 220          }
 221      }
 222  
 223  
 224  /*======================================================================*\
 225      Function:   assign()
 226      Purpose:    assigns values to template variables
 227  \*======================================================================*/
 228      function assign($tpl_var, $value = NULL)
 229      {
 230          if (is_array($tpl_var)){
 231              foreach ($tpl_var as $key => $val) {
 232                  if ($key != '' && isset($val)) {
 233                      $this->_tpl_vars[$key] = $val;
 234                  }
 235              }
 236          } else {
 237              if ($tpl_var != '' && isset($value))
 238                  $this->_tpl_vars[$tpl_var] = $value;
 239          }
 240          $this->_extract = true;
 241      }
 242  
 243  /*======================================================================*\
 244      Function:   assign_by_ref()
 245      Purpose:    assigns values to template variables by reference
 246  \*======================================================================*/
 247      function assign_by_ref($tpl_var, &$value)
 248      {
 249          if ($tpl_var != '' && isset($value))
 250              $this->_tpl_vars[$tpl_var] = &$value;
 251          $this->_extract = true;
 252      }
 253      
 254  /*======================================================================*\
 255      Function: append
 256      Purpose:  appends values to template variables
 257  \*======================================================================*/
 258      function append($tpl_var, $value = NULL)
 259      {
 260          if (is_array($tpl_var)) {
 261              foreach ($tpl_var as $key => $val) {
 262                  if ($key != '') {
 263                      if(!@is_array($this->_tpl_vars[$key])) {
 264                          settype($this->_tpl_vars[$key],'array');
 265                      }
 266                      $this->_tpl_vars[$key][] = $val;
 267                  }
 268              }
 269          } else {
 270              if ($tpl_var != '' && isset($value)) {
 271                  if(!@is_array($this->_tpl_vars[$tpl_var])) {
 272                      settype($this->_tpl_vars[$tpl_var],'array');
 273                  }
 274                  $this->_tpl_vars[$tpl_var][] = $value;
 275              }
 276          }
 277          $this->_extract = true;
 278      }
 279  
 280  /*======================================================================*\
 281      Function: append_by_ref
 282      Purpose:  appends values to template variables by reference
 283  \*======================================================================*/
 284      function append_by_ref($tpl_var, &$value)
 285      {
 286          if ($tpl_var != '' && isset($value)) {
 287              if(!@is_array($this->_tpl_vars[$tpl_var])) {
 288                  settype($this->_tpl_vars[$tpl_var],'array');
 289              }
 290              $this->_tpl_vars[$tpl_var][] = &$value;
 291          }
 292          $this->_extract = true;
 293      }
 294  
 295  
 296  /*======================================================================*\
 297      Function:   clear_assign()
 298      Purpose:    clear the given assigned template variable.
 299  \*======================================================================*/
 300      function clear_assign($tpl_var)
 301      {
 302          if (is_array($tpl_var))
 303              foreach ($tpl_var as $curr_var)
 304                  unset($this->_tpl_vars[$curr_var]);
 305          else
 306              unset($this->_tpl_vars[$tpl_var]);
 307      }
 308  
 309  
 310  /*======================================================================*\
 311      Function: register_function
 312      Purpose:  Registers custom function to be used in templates
 313  \*======================================================================*/
 314      function register_function($function, $function_impl)
 315      {
 316          $this->_plugins['function'][$function] =
 317              array($function_impl, null, null, false);
 318      }
 319  
 320  /*======================================================================*\
 321      Function: unregister_function
 322      Purpose:  Unregisters custom function
 323  \*======================================================================*/
 324      function unregister_function($function)
 325      {
 326          unset($this->_plugins['function'][$function]);
 327      }
 328  
 329  /*======================================================================*\
 330      Function: register_block
 331      Purpose:  Registers block function to be used in templates
 332  \*======================================================================*/
 333      function register_block($block, $block_impl)
 334      {
 335          $this->_plugins['block'][$block] =
 336              array($block_impl, null, null, false);
 337      }
 338  
 339  /*======================================================================*\
 340      Function: unregister_block
 341      Purpose:  Unregisters block function
 342  \*======================================================================*/
 343      function unregister_block($block)
 344      {
 345          unset($this->_plugins['block'][$block]);
 346      }
 347  
 348  /*======================================================================*\
 349      Function: register_compiler_function
 350      Purpose:  Registers compiler function
 351  \*======================================================================*/
 352      function register_compiler_function($function, $function_impl)
 353      {
 354          $this->_plugins['compiler'][$function] =
 355              array($function_impl, null, null, false);
 356      }
 357  
 358  /*======================================================================*\
 359      Function: unregister_compiler_function
 360      Purpose:  Unregisters compiler function
 361  \*======================================================================*/
 362      function unregister_compiler_function($function)
 363      {
 364          unset($this->_plugins['compiler'][$function]);
 365      }
 366  
 367  /*======================================================================*\
 368      Function: register_modifier
 369      Purpose:  Registers modifier to be used in templates
 370  \*======================================================================*/
 371      function register_modifier($modifier, $modifier_impl)
 372      {
 373          $this->_plugins['modifier'][$modifier] =
 374              array($modifier_impl, null, null, false);
 375      }
 376  
 377  /*======================================================================*\
 378      Function: unregister_modifier
 379      Purpose:  Unregisters modifier
 380  \*======================================================================*/
 381      function unregister_modifier($modifier)
 382      {
 383          unset($this->_plugins['modifier'][$modifier]);
 384      }
 385  
 386  /*======================================================================*\
 387      Function: register_resource
 388      Purpose:  Registers a resource to fetch a template
 389  \*======================================================================*/
 390      function register_resource($type, $functions)
 391      {
 392          $this->_plugins['resource'][$type] =
 393              array((array)$functions, false);
 394      }
 395  
 396  /*======================================================================*\
 397      Function: unregister_resource
 398      Purpose:  Unregisters a resource
 399  \*======================================================================*/
 400      function unregister_resource($type)
 401      {
 402          unset($this->_plugins['resource'][$type]);
 403      }
 404  
 405  /*======================================================================*\
 406      Function: register_prefilter
 407      Purpose:  Registers a prefilter function to apply
 408                to a template before compiling
 409  \*======================================================================*/
 410      function register_prefilter($function)
 411      {
 412          $this->_plugins['prefilter'][$function]
 413              = array($function, null, null, false);
 414      }
 415  
 416  /*======================================================================*\
 417      Function: unregister_prefilter
 418      Purpose:  Unregisters a prefilter function
 419  \*======================================================================*/
 420      function unregister_prefilter($function)
 421      {
 422          unset($this->_plugins['prefilter'][$function]);
 423      }
 424  
 425  /*======================================================================*\
 426      Function: register_postfilter
 427      Purpose:  Registers a postfilter function to apply
 428                to a compiled template after compilation
 429  \*======================================================================*/
 430      function register_postfilter($function)
 431      {
 432          $this->_plugins['postfilter'][$function]
 433              = array($function, null, null, false);
 434      }
 435  
 436  /*======================================================================*\
 437      Function: unregister_postfilter
 438      Purpose:  Unregisters a postfilter function
 439  \*======================================================================*/
 440      function unregister_postfilter($function)
 441      {
 442          unset($this->_plugins['postfilter'][$function]);
 443      }
 444  
 445  /*======================================================================*\
 446      Function: register_outputfilter
 447      Purpose:  Registers an output filter function to apply
 448                to a template output
 449  \*======================================================================*/
 450      function register_outputfilter($function)
 451      {
 452          $this->_plugins['outputfilter'][$function]
 453              = array($function, null, null, false);
 454      }
 455  
 456  /*======================================================================*\
 457      Function: unregister_outputfilter
 458      Purpose:  Unregisters an outputfilter function
 459  \*======================================================================*/
 460      function unregister_outputfilter($function)
 461      {
 462          unset($this->_plugins['outputfilter'][$function]);
 463      }
 464  
 465  /*======================================================================*\
 466      Function:   load_filter()
 467      Purpose:    load a filter of specified type and name
 468  \*======================================================================*/
 469      function load_filter($type, $name)
 470      {
 471          switch ($type) {
 472              case 'output':
 473                  $this->_load_plugins(array(array($type . 'filter', $name, null, null, false)));
 474                  break;
 475  
 476              case 'pre':
 477              case 'post':
 478                  if (!isset($this->_plugins[$type . 'filter'][$name]))
 479                      $this->_plugins[$type . 'filter'][$name] = false;
 480                  break;
 481          }
 482      }
 483  
 484  /*======================================================================*\
 485      Function:   clear_cache()
 486      Purpose:    clear cached content for the given template and cache id
 487  \*======================================================================*/
 488      function clear_cache($tpl_file = null, $cache_id = null, $compile_id = null, $exp_time = null)
 489      {
 490          
 491          if (!isset($compile_id))
 492              $compile_id = $this->compile_id;
 493  
 494          if (isset($cache_id))
 495              $auto_id = (isset($compile_id)) ? $cache_id . '|' . $compile_id : $cache_id;
 496          elseif(isset($compile_id))
 497              $auto_id = $compile_id;
 498          else
 499              $auto_id = null;
 500  
 501          if (!empty($this->cache_handler_func)) {
 502              $funcname = $this->cache_handler_func;
 503              return $funcname('clear', $this, $dummy, $tpl_file, $cache_id, $compile_id);
 504          } else {
 505              return $this->_rm_auto($this->cache_dir, $tpl_file, $auto_id, $exp_time);
 506          }
 507          
 508      }
 509  
 510  
 511  /*======================================================================*\
 512      Function:   clear_all_cache()
 513      Purpose:    clear the entire contents of cache (all templates)
 514  \*======================================================================*/
 515      function clear_all_cache($exp_time = null)
 516      {
 517          if (!empty($this->cache_handler_func)) {
 518              $funcname = $this->cache_handler_func;
 519              return $funcname('clear', $this, $dummy);
 520          } else {
 521              return $this->_rm_auto($this->cache_dir,null,null,$exp_time);
 522          }
 523      }
 524  
 525  
 526  /*======================================================================*\
 527      Function:   is_cached()
 528      Purpose:    test to see if valid cache exists for this template
 529  \*======================================================================*/
 530      function is_cached($tpl_file, $cache_id = null, $compile_id = null)
 531      {
 532          if (!$this->caching)
 533              return false;
 534  
 535          if (!isset($compile_id))
 536              $compile_id = $this->compile_id;
 537  
 538          return $this->_read_cache_file($tpl_file, $cache_id, $compile_id, $results);
 539      }
 540  
 541  
 542  /*======================================================================*\
 543      Function:   clear_all_assign()
 544      Purpose:    clear all the assigned template variables.
 545  \*======================================================================*/
 546      function clear_all_assign()
 547      {
 548          $this->_tpl_vars = array();
 549      }
 550  
 551  /*======================================================================*\
 552      Function:   clear_compiled_tpl()
 553      Purpose:    clears compiled version of specified template resource,
 554                  or all compiled template files if one is not specified.
 555                  This function is for advanced use only, not normally needed.
 556  \*======================================================================*/
 557      function clear_compiled_tpl($tpl_file = null, $compile_id = null, $exp_time = null)
 558      {
 559          if (!isset($compile_id))
 560              $compile_id = $this->compile_id;
 561          return $this->_rm_auto($this->compile_dir, $tpl_file, $compile_id, $exp_time);
 562      }
 563  
 564   /*======================================================================*\
 565      Function:   template_exists()
 566      Purpose:    Checks whether requested template exists.
 567  \*======================================================================*/
 568      function template_exists($tpl_file)
 569      {
 570          return $this->_fetch_template_info($tpl_file, $source, $timestamp, true, true);
 571      }
 572  
 573  /*======================================================================*\
 574      Function: get_template_vars
 575      Purpose:  Returns an array containing template variables
 576  \*======================================================================*/
 577      function &get_template_vars()
 578      {
 579          return $this->_tpl_vars;
 580      }
 581  
 582  
 583  /*======================================================================*\
 584      Function: trigger_error
 585      Purpose:  trigger Smarty error
 586  \*======================================================================*/
 587      function trigger_error($error_msg, $error_type = E_USER_WARNING)
 588      {
 589          trigger_error("Smarty error: $error_msg", $error_type);
 590      }
 591  
 592  
 593  /*===================================================