Guest User

Untitled

a guest
Nov 14th, 2011
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 63.67 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Project: Smarty: the PHP compiling template engine
  5. * File: Smarty.class.php
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * For questions, help, comments, discussion, etc., please join the
  22. * Smarty mailing list. Send a blank e-mail to
  23. *
  24. * @link http://smarty.php.net/
  25. * @copyright 2001-2005 New Digital Group, Inc.
  26. * @author Monte Ohrt <monte at ohrt dot com>
  27. * @author Andrei Zmievski <[email protected]>
  28. * @package Smarty
  29. * @version 2.6.18
  30. */
  31.  
  32. /* $Id: Smarty.class.php,v 1.1 2007/07/18 05:14:00 newbie455 Exp $ */
  33.  
  34. /**
  35. * DIR_SEP isn't used anymore, but third party apps might
  36. */
  37. if(!defined('DIR_SEP')) {
  38. define('DIR_SEP', DIRECTORY_SEPARATOR);
  39. }
  40.  
  41. /**
  42. * set SMARTY_DIR to absolute path to Smarty library files.
  43. * if not defined, include_path will be used. Sets SMARTY_DIR only if user
  44. * application has not already defined it.
  45. */
  46.  
  47. if (!defined('SMARTY_DIR')) {
  48. define('SMARTY_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR);
  49. }
  50.  
  51. if (!defined('SMARTY_CORE_DIR')) {
  52. define('SMARTY_CORE_DIR', SMARTY_DIR . 'internals' . DIRECTORY_SEPARATOR);
  53. }
  54.  
  55. define('SMARTY_PHP_PASSTHRU', 0);
  56. define('SMARTY_PHP_QUOTE', 1);
  57. define('SMARTY_PHP_REMOVE', 2);
  58. define('SMARTY_PHP_ALLOW', 3);
  59.  
  60. /**
  61. * @package Smarty
  62. */
  63. class Smarty
  64. {
  65. /**#@+
  66. * Smarty Configuration Section
  67. */
  68.  
  69. /**
  70. * The name of the directory where templates are located.
  71. *
  72. * @var string
  73. */
  74. var $template_dir = 'templates';
  75.  
  76. /**
  77. * The directory where compiled templates are located.
  78. *
  79. * @var string
  80. */
  81. var $compile_dir = 'templates_c';
  82.  
  83. /**
  84. * The directory where config files are located.
  85. *
  86. * @var string
  87. */
  88. var $config_dir = 'configs';
  89.  
  90. /**
  91. * An array of directories searched for plugins.
  92. *
  93. * @var array
  94. */
  95. var $plugins_dir = array('plugins');
  96.  
  97. /**
  98. * If debugging is enabled, a debug console window will display
  99. * when the page loads (make sure your browser allows unrequested
  100. * popup windows)
  101. *
  102. * @var boolean
  103. */
  104. var $debugging = false;
  105.  
  106. /**
  107. * When set, smarty does uses this value as error_reporting-level.
  108. *
  109. * @var boolean
  110. */
  111. var $error_reporting = null;
  112.  
  113. /**
  114. * This is the path to the debug console template. If not set,
  115. * the default one will be used.
  116. *
  117. * @var string
  118. */
  119. var $debug_tpl = '';
  120.  
  121. /**
  122. * This determines if debugging is enable-able from the browser.
  123. * <ul>
  124. * <li>NONE => no debugging control allowed</li>
  125. * <li>URL => enable debugging when SMARTY_DEBUG is found in the URL.</li>
  126. * </ul>
  127. * @link http://www.foo.dom/index.php?SMARTY_DEBUG
  128. * @var string
  129. */
  130. var $debugging_ctrl = 'NONE';
  131.  
  132. /**
  133. * This tells Smarty whether to check for recompiling or not. Recompiling
  134. * does not need to happen unless a template or config file is changed.
  135. * Typically you enable this during development, and disable for
  136. * production.
  137. *
  138. * @var boolean
  139. */
  140. var $compile_check = true;
  141.  
  142. /**
  143. * This forces templates to compile every time. Useful for development
  144. * or debugging.
  145. *
  146. * @var boolean
  147. */
  148. var $force_compile = false;
  149.  
  150. /**
  151. * This enables template caching.
  152. * <ul>
  153. * <li>0 = no caching</li>
  154. * <li>1 = use class cache_lifetime value</li>
  155. * <li>2 = use cache_lifetime in cache file</li>
  156. * </ul>
  157. * @var integer
  158. */
  159. var $caching = 0;
  160.  
  161. /**
  162. * The name of the directory for cache files.
  163. *
  164. * @var string
  165. */
  166. var $cache_dir = 'cache';
  167.  
  168. /**
  169. * This is the number of seconds cached content will persist.
  170. * <ul>
  171. * <li>0 = always regenerate cache</li>
  172. * <li>-1 = never expires</li>
  173. * </ul>
  174. *
  175. * @var integer
  176. */
  177. var $cache_lifetime = 3600;
  178.  
  179. /**
  180. * Only used when $caching is enabled. If true, then If-Modified-Since headers
  181. * are respected with cached content, and appropriate HTTP headers are sent.
  182. * This way repeated hits to a cached page do not send the entire page to the
  183. * client every time.
  184. *
  185. * @var boolean
  186. */
  187. var $cache_modified_check = false;
  188.  
  189. /**
  190. * This determines how Smarty handles "<?php ... ?>" tags in templates.
  191. * possible values:
  192. * <ul>
  193. * <li>SMARTY_PHP_PASSTHRU -> print tags as plain text</li>
  194. * <li>SMARTY_PHP_QUOTE -> escape tags as entities</li>
  195. * <li>SMARTY_PHP_REMOVE -> remove php tags</li>
  196. * <li>SMARTY_PHP_ALLOW -> execute php tags</li>
  197. * </ul>
  198. *
  199. * @var integer
  200. */
  201. var $php_handling = SMARTY_PHP_PASSTHRU;
  202.  
  203. /**
  204. * This enables template security. When enabled, many things are restricted
  205. * in the templates that normally would go unchecked. This is useful when
  206. * untrusted parties are editing templates and you want a reasonable level
  207. * of security. (no direct execution of PHP in templates for example)
  208. *
  209. * @var boolean
  210. */
  211. var $security = false;
  212.  
  213. /**
  214. * This is the list of template directories that are considered secure. This
  215. * is used only if {@link $security} is enabled. One directory per array
  216. * element. {@link $template_dir} is in this list implicitly.
  217. *
  218. * @var array
  219. */
  220. var $secure_dir = array();
  221.  
  222. /**
  223. * These are the security settings for Smarty. They are used only when
  224. * {@link $security} is enabled.
  225. *
  226. * @var array
  227. */
  228. var $security_settings = array(
  229. 'PHP_HANDLING' => false,
  230. 'IF_FUNCS' => array('array', 'list',
  231. 'isset', 'empty',
  232. 'count', 'sizeof',
  233. 'in_array', 'is_array',
  234. 'true', 'false', 'null'),
  235. 'INCLUDE_ANY' => false,
  236. 'PHP_TAGS' => false,
  237. 'MODIFIER_FUNCS' => array('count'),
  238. 'ALLOW_CONSTANTS' => false
  239. );
  240.  
  241. /**
  242. * This is an array of directories where trusted php scripts reside.
  243. * {@link $security} is disabled during their inclusion/execution.
  244. *
  245. * @var array
  246. */
  247. var $trusted_dir = array();
  248.  
  249. /**
  250. * The left delimiter used for the template tags.
  251. *
  252. * @var string
  253. */
  254. var $left_delimiter = '<!--{';
  255.  
  256. /**
  257. * The right delimiter used for the template tags.
  258. *
  259. * @var string
  260. */
  261. var $right_delimiter = '}-->';
  262.  
  263. /**
  264. * The order in which request variables are registered, similar to
  265. * variables_order in php.ini E = Environment, G = GET, P = POST,
  266. * C = Cookies, S = Server
  267. *
  268. * @var string
  269. */
  270. var $request_vars_order = 'EGPCS';
  271.  
  272. /**
  273. * Indicates wether $HTTP_*_VARS[] (request_use_auto_globals=false)
  274. * are uses as request-vars or $_*[]-vars. note: if
  275. * request_use_auto_globals is true, then $request_vars_order has
  276. * no effect, but the php-ini-value "gpc_order"
  277. *
  278. * @var boolean
  279. */
  280. var $request_use_auto_globals = true;
  281.  
  282. /**
  283. * Set this if you want different sets of compiled files for the same
  284. * templates. This is useful for things like different languages.
  285. * Instead of creating separate sets of templates per language, you
  286. * set different compile_ids like 'en' and 'de'.
  287. *
  288. * @var string
  289. */
  290. var $compile_id = null;
  291.  
  292. /**
  293. * This tells Smarty whether or not to use sub dirs in the cache/ and
  294. * templates_c/ directories. sub directories better organized, but
  295. * may not work well with PHP safe mode enabled.
  296. *
  297. * @var boolean
  298. *
  299. */
  300. var $use_sub_dirs = false;
  301.  
  302. /**
  303. * This is a list of the modifiers to apply to all template variables.
  304. * Put each modifier in a separate array element in the order you want
  305. * them applied. example: <code>array('escape:"htmlall"');</code>
  306. *
  307. * @var array
  308. */
  309. var $default_modifiers = array();
  310.  
  311. /**
  312. * This is the resource type to be used when not specified
  313. * at the beginning of the resource path. examples:
  314. * $smarty->display('file:index.tpl');
  315. * $smarty->display('db:index.tpl');
  316. * $smarty->display('index.tpl'); // will use default resource type
  317. * {include file="file:index.tpl"}
  318. * {include file="db:index.tpl"}
  319. * {include file="index.tpl"} {* will use default resource type *}
  320. *
  321. * @var array
  322. */
  323. var $default_resource_type = 'file';
  324.  
  325. /**
  326. * The function used for cache file handling. If not set, built-in caching is used.
  327. *
  328. * @var null|string function name
  329. */
  330. var $cache_handler_func = null;
  331.  
  332. /**
  333. * This indicates which filters are automatically loaded into Smarty.
  334. *
  335. * @var array array of filter names
  336. */
  337. var $autoload_filters = array();
  338.  
  339. /**#@+
  340. * @var boolean
  341. */
  342. /**
  343. * This tells if config file vars of the same name overwrite each other or not.
  344. * if disabled, same name variables are accumulated in an array.
  345. */
  346. var $config_overwrite = true;
  347.  
  348. /**
  349. * This tells whether or not to automatically booleanize config file variables.
  350. * If enabled, then the strings "on", "true", and "yes" are treated as boolean
  351. * true, and "off", "false" and "no" are treated as boolean false.
  352. */
  353. var $config_booleanize = true;
  354.  
  355. /**
  356. * This tells whether hidden sections [.foobar] are readable from the
  357. * tempalates or not. Normally you would never allow this since that is
  358. * the point behind hidden sections: the application can access them, but
  359. * the templates cannot.
  360. */
  361. var $config_read_hidden = false;
  362.  
  363. /**
  364. * This tells whether or not automatically fix newlines in config files.
  365. * It basically converts \r (mac) or \r\n (dos) to \n
  366. */
  367. var $config_fix_newlines = true;
  368. /**#@-*/
  369.  
  370. /**
  371. * If a template cannot be found, this PHP function will be executed.
  372. * Useful for creating templates on-the-fly or other special action.
  373. *
  374. * @var string function name
  375. */
  376. var $default_template_handler_func = '';
  377.  
  378. /**
  379. * The file that contains the compiler class. This can a full
  380. * pathname, or relative to the php_include path.
  381. *
  382. * @var string
  383. */
  384. var $compiler_file = 'Smarty_Compiler.class.php';
  385.  
  386. /**
  387. * The class used for compiling templates.
  388. *
  389. * @var string
  390. */
  391. var $compiler_class = 'Smarty_Compiler';
  392.  
  393. /**
  394. * The class used to load config vars.
  395. *
  396. * @var string
  397. */
  398. var $config_class = 'Config_File';
  399.  
  400. /**#@+
  401. * END Smarty Configuration Section
  402. * There should be no need to touch anything below this line.
  403. * @access private
  404. */
  405. /**
  406. * where assigned template vars are kept
  407. *
  408. * @var array
  409. */
  410. var $_tpl_vars = array();
  411.  
  412. /**
  413. * stores run-time $smarty.* vars
  414. *
  415. * @var null|array
  416. */
  417. var $_smarty_vars = null;
  418.  
  419. /**
  420. * keeps track of sections
  421. *
  422. * @var array
  423. */
  424. var $_sections = array();
  425.  
  426. /**
  427. * keeps track of foreach blocks
  428. *
  429. * @var array
  430. */
  431. var $_foreach = array();
  432.  
  433. /**
  434. * keeps track of tag hierarchy
  435. *
  436. * @var array
  437. */
  438. var $_tag_stack = array();
  439.  
  440. /**
  441. * configuration object
  442. *
  443. * @var Config_file
  444. */
  445. var $_conf_obj = null;
  446.  
  447. /**
  448. * loaded configuration settings
  449. *
  450. * @var array
  451. */
  452. var $_config = array(array('vars' => array(), 'files' => array()));
  453.  
  454. /**
  455. * md5 checksum of the string 'Smarty'
  456. *
  457. * @var string
  458. */
  459. var $_smarty_md5 = 'f8d698aea36fcbead2b9d5359ffca76f';
  460.  
  461. /**
  462. * Smarty version number
  463. *
  464. * @var string
  465. */
  466. var $_version = '2.6.18';
  467.  
  468. /**
  469. * current template inclusion depth
  470. *
  471. * @var integer
  472. */
  473. var $_inclusion_depth = 0;
  474.  
  475. /**
  476. * for different compiled templates
  477. *
  478. * @var string
  479. */
  480. var $_compile_id = null;
  481.  
  482. /**
  483. * text in URL to enable debug mode
  484. *
  485. * @var string
  486. */
  487. var $_smarty_debug_id = 'SMARTY_DEBUG';
  488.  
  489. /**
  490. * debugging information for debug console
  491. *
  492. * @var array
  493. */
  494. var $_smarty_debug_info = array();
  495.  
  496. /**
  497. * info that makes up a cache file
  498. *
  499. * @var array
  500. */
  501. var $_cache_info = array();
  502.  
  503. /**
  504. * default file permissions
  505. *
  506. * @var integer
  507. */
  508. var $_file_perms = 0644;
  509.  
  510. /**
  511. * default dir permissions
  512. *
  513. * @var integer
  514. */
  515. var $_dir_perms = 0771;
  516.  
  517. /**
  518. * registered objects
  519. *
  520. * @var array
  521. */
  522. var $_reg_objects = array();
  523.  
  524. /**
  525. * table keeping track of plugins
  526. *
  527. * @var array
  528. */
  529. var $_plugins = array(
  530. 'modifier' => array(),
  531. 'function' => array(),
  532. 'block' => array(),
  533. 'compiler' => array(),
  534. 'prefilter' => array(),
  535. 'postfilter' => array(),
  536. 'outputfilter' => array(),
  537. 'resource' => array(),
  538. 'insert' => array());
  539.  
  540.  
  541. /**
  542. * cache serials
  543. *
  544. * @var array
  545. */
  546. var $_cache_serials = array();
  547.  
  548. /**
  549. * name of optional cache include file
  550. *
  551. * @var string
  552. */
  553. var $_cache_include = null;
  554.  
  555. /**
  556. * indicate if the current code is used in a compiled
  557. * include
  558. *
  559. * @var string
  560. */
  561. var $_cache_including = false;
  562.  
  563. /**#@-*/
  564. /**
  565. * The class constructor.
  566. */
  567. function Smarty()
  568. {
  569. $this->assign('SCRIPT_NAME', isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME']
  570. : @$GLOBALS['HTTP_SERVER_VARS']['SCRIPT_NAME']);
  571. }
  572.  
  573. /**
  574. * assigns values to template variables
  575. *
  576. * @param array|string $tpl_var the template variable name(s)
  577. * @param mixed $value the value to assign
  578. */
  579. function assign($tpl_var, $value = null)
  580. {
  581. if (is_array($tpl_var)){
  582. foreach ($tpl_var as $key => $val) {
  583. if ($key != '') {
  584. $this->_tpl_vars[$key] = $val;
  585. }
  586. }
  587. } else {
  588. if ($tpl_var != '')
  589. $this->_tpl_vars[$tpl_var] = $value;
  590. }
  591. }
  592.  
  593. /**
  594. * assigns values to template variables by reference
  595. *
  596. * @param string $tpl_var the template variable name
  597. * @param mixed $value the referenced value to assign
  598. */
  599. function assign_by_ref($tpl_var, &$value)
  600. {
  601. if ($tpl_var != '')
  602. $this->_tpl_vars[$tpl_var] = &$value;
  603. }
  604.  
  605. /**
  606. * appends values to template variables
  607. *
  608. * @param array|string $tpl_var the template variable name(s)
  609. * @param mixed $value the value to append
  610. */
  611. function append($tpl_var, $value=null, $merge=false)
  612. {
  613. if (is_array($tpl_var)) {
  614. // $tpl_var is an array, ignore $value
  615. foreach ($tpl_var as $_key => $_val) {
  616. if ($_key != '') {
  617. if(!@is_array($this->_tpl_vars[$_key])) {
  618. settype($this->_tpl_vars[$_key],'array');
  619. }
  620. if($merge && is_array($_val)) {
  621. foreach($_val as $_mkey => $_mval) {
  622. $this->_tpl_vars[$_key][$_mkey] = $_mval;
  623. }
  624. } else {
  625. $this->_tpl_vars[$_key][] = $_val;
  626. }
  627. }
  628. }
  629. } else {
  630. if ($tpl_var != '' && isset($value)) {
  631. if(!@is_array($this->_tpl_vars[$tpl_var])) {
  632. settype($this->_tpl_vars[$tpl_var],'array');
  633. }
  634. if($merge && is_array($value)) {
  635. foreach($value as $_mkey => $_mval) {
  636. $this->_tpl_vars[$tpl_var][$_mkey] = $_mval;
  637. }
  638. } else {
  639. $this->_tpl_vars[$tpl_var][] = $value;
  640. }
  641. }
  642. }
  643. }
  644.  
  645. /**
  646. * appends values to template variables by reference
  647. *
  648. * @param string $tpl_var the template variable name
  649. * @param mixed $value the referenced value to append
  650. */
  651. function append_by_ref($tpl_var, &$value, $merge=false)
  652. {
  653. if ($tpl_var != '' && isset($value)) {
  654. if(!@is_array($this->_tpl_vars[$tpl_var])) {
  655. settype($this->_tpl_vars[$tpl_var],'array');
  656. }
  657. if ($merge && is_array($value)) {
  658. foreach($value as $_key => $_val) {
  659. $this->_tpl_vars[$tpl_var][$_key] = &$value[$_key];
  660. }
  661. } else {
  662. $this->_tpl_vars[$tpl_var][] = &$value;
  663. }
  664. }
  665. }
  666.  
  667.  
  668. /**
  669. * clear the given assigned template variable.
  670. *
  671. * @param string $tpl_var the template variable to clear
  672. */
  673. function clear_assign($tpl_var)
  674. {
  675. if (is_array($tpl_var))
  676. foreach ($tpl_var as $curr_var)
  677. unset($this->_tpl_vars[$curr_var]);
  678. else
  679. unset($this->_tpl_vars[$tpl_var]);
  680. }
  681.  
  682.  
  683. /**
  684. * Registers custom function to be used in templates
  685. *
  686. * @param string $function the name of the template function
  687. * @param string $function_impl the name of the PHP function to register
  688. */
  689. function register_function($function, $function_impl, $cacheable=true, $cache_attrs=null)
  690. {
  691. $this->_plugins['function'][$function] =
  692. array($function_impl, null, null, false, $cacheable, $cache_attrs);
  693.  
  694. }
  695.  
  696. /**
  697. * Unregisters custom function
  698. *
  699. * @param string $function name of template function
  700. */
  701. function unregister_function($function)
  702. {
  703. unset($this->_plugins['function'][$function]);
  704. }
  705.  
  706. /**
  707. * Registers object to be used in templates
  708. *
  709. * @param string $object name of template object
  710. * @param object &$object_impl the referenced PHP object to register
  711. * @param null|array $allowed list of allowed methods (empty = all)
  712. * @param boolean $smarty_args smarty argument format, else traditional
  713. * @param null|array $block_functs list of methods that are block format
  714. */
  715. function register_object($object, &$object_impl, $allowed = array(), $smarty_args = true, $block_methods = array())
  716. {
  717. settype($allowed, 'array');
  718. settype($smarty_args, 'boolean');
  719. $this->_reg_objects[$object] =
  720. array(&$object_impl, $allowed, $smarty_args, $block_methods);
  721. }
  722.  
  723. /**
  724. * Unregisters object
  725. *
  726. * @param string $object name of template object
  727. */
  728. function unregister_object($object)
  729. {
  730. unset($this->_reg_objects[$object]);
  731. }
  732.  
  733.  
  734. /**
  735. * Registers block function to be used in templates
  736. *
  737. * @param string $block name of template block
  738. * @param string $block_impl PHP function to register
  739. */
  740. function register_block($block, $block_impl, $cacheable=true, $cache_attrs=null)
  741. {
  742. $this->_plugins['block'][$block] =
  743. array($block_impl, null, null, false, $cacheable, $cache_attrs);
  744. }
  745.  
  746. /**
  747. * Unregisters block function
  748. *
  749. * @param string $block name of template function
  750. */
  751. function unregister_block($block)
  752. {
  753. unset($this->_plugins['block'][$block]);
  754. }
  755.  
  756. /**
  757. * Registers compiler function
  758. *
  759. * @param string $function name of template function
  760. * @param string $function_impl name of PHP function to register
  761. */
  762. function register_compiler_function($function, $function_impl, $cacheable=true)
  763. {
  764. $this->_plugins['compiler'][$function] =
  765. array($function_impl, null, null, false, $cacheable);
  766. }
  767.  
  768. /**
  769. * Unregisters compiler function
  770. *
  771. * @param string $function name of template function
  772. */
  773. function unregister_compiler_function($function)
  774. {
  775. unset($this->_plugins['compiler'][$function]);
  776. }
  777.  
  778. /**
  779. * Registers modifier to be used in templates
  780. *
  781. * @param string $modifier name of template modifier
  782. * @param string $modifier_impl name of PHP function to register
  783. */
  784. function register_modifier($modifier, $modifier_impl)
  785. {
  786. $this->_plugins['modifier'][$modifier] =
  787. array($modifier_impl, null, null, false);
  788. }
  789.  
  790. /**
  791. * Unregisters modifier
  792. *
  793. * @param string $modifier name of template modifier
  794. */
  795. function unregister_modifier($modifier)
  796. {
  797. unset($this->_plugins['modifier'][$modifier]);
  798. }
  799.  
  800. /**
  801. * Registers a resource to fetch a template
  802. *
  803. * @param string $type name of resource
  804. * @param array $functions array of functions to handle resource
  805. */
  806. function register_resource($type, $functions)
  807. {
  808. if (count($functions)==4) {
  809. $this->_plugins['resource'][$type] =
  810. array($functions, false);
  811.  
  812. } elseif (count($functions)==5) {
  813. $this->_plugins['resource'][$type] =
  814. array(array(array(&$functions[0], $functions[1])
  815. ,array(&$functions[0], $functions[2])
  816. ,array(&$functions[0], $functions[3])
  817. ,array(&$functions[0], $functions[4]))
  818. ,false);
  819.  
  820. } else {
  821. $this->trigger_error("malformed function-list for '$type' in register_resource");
  822.  
  823. }
  824. }
  825.  
  826. /**
  827. * Unregisters a resource
  828. *
  829. * @param string $type name of resource
  830. */
  831. function unregister_resource($type)
  832. {
  833. unset($this->_plugins['resource'][$type]);
  834. }
  835.  
  836. /**
  837. * Registers a prefilter function to apply
  838. * to a template before compiling
  839. *
  840. * @param string $function name of PHP function to register
  841. */
  842. function register_prefilter($function)
  843. {
  844. $_name = (is_array($function)) ? $function[1] : $function;
  845. $this->_plugins['prefilter'][$_name]
  846. = array($function, null, null, false);
  847. }
  848.  
  849. /**
  850. * Unregisters a prefilter function
  851. *
  852. * @param string $function name of PHP function
  853. */
  854. function unregister_prefilter($function)
  855. {
  856. unset($this->_plugins['prefilter'][$function]);
  857. }
  858.  
  859. /**
  860. * Registers a postfilter function to apply
  861. * to a compiled template after compilation
  862. *
  863. * @param string $function name of PHP function to register
  864. */
  865. function register_postfilter($function)
  866. {
  867. $_name = (is_array($function)) ? $function[1] : $function;
  868. $this->_plugins['postfilter'][$_name]
  869. = array($function, null, null, false);
  870. }
  871.  
  872. /**
  873. * Unregisters a postfilter function
  874. *
  875. * @param string $function name of PHP function
  876. */
  877. function unregister_postfilter($function)
  878. {
  879. unset($this->_plugins['postfilter'][$function]);
  880. }
  881.  
  882. /**
  883. * Registers an output filter function to apply
  884. * to a template output
  885. *
  886. * @param string $function name of PHP function
  887. */
  888. function register_outputfilter($function)
  889. {
  890. $_name = (is_array($function)) ? $function[1] : $function;
  891. $this->_plugins['outputfilter'][$_name]
  892. = array($function, null, null, false);
  893. }
  894.  
  895. /**
  896. * Unregisters an outputfilter function
  897. *
  898. * @param string $function name of PHP function
  899. */
  900. function unregister_outputfilter($function)
  901. {
  902. unset($this->_plugins['outputfilter'][$function]);
  903. }
  904.  
  905. /**
  906. * load a filter of specified type and name
  907. *
  908. * @param string $type filter type
  909. * @param string $name filter name
  910. */
  911. function load_filter($type, $name)
  912. {
  913. switch ($type) {
  914. case 'output':
  915. $_params = array('plugins' => array(array($type . 'filter', $name, null, null, false)));
  916. require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');
  917. smarty_core_load_plugins($_params, $this);
  918. break;
  919.  
  920. case 'pre':
  921. case 'post':
  922. if (!isset($this->_plugins[$type . 'filter'][$name]))
  923. $this->_plugins[$type . 'filter'][$name] = false;
  924. break;
  925. }
  926. }
  927.  
  928. /**
  929. * clear cached content for the given template and cache id
  930. *
  931. * @param string $tpl_file name of template file
  932. * @param string $cache_id name of cache_id
  933. * @param string $compile_id name of compile_id
  934. * @param string $exp_time expiration time
  935. * @return boolean
  936. */
  937. function clear_cache($tpl_file = null, $cache_id = null, $compile_id = null, $exp_time = null)
  938. {
  939.  
  940. if (!isset($compile_id))
  941. $compile_id = $this->compile_id;
  942.  
  943. if (!isset($tpl_file))
  944. $compile_id = null;
  945.  
  946. $_auto_id = $this->_get_auto_id($cache_id, $compile_id);
  947.  
  948. if (!empty($this->cache_handler_func)) {
  949. return call_user_func_array($this->cache_handler_func,
  950. array('clear', &$this, &$dummy, $tpl_file, $cache_id, $compile_id, $exp_time));
  951. } else {
  952. $_params = array('auto_base' => $this->cache_dir,
  953. 'auto_source' => $tpl_file,
  954. 'auto_id' => $_auto_id,
  955. 'exp_time' => $exp_time);
  956. require_once(SMARTY_CORE_DIR . 'core.rm_auto.php');
  957. return smarty_core_rm_auto($_params, $this);
  958. }
  959.  
  960. }
  961.  
  962.  
  963. /**
  964. * clear the entire contents of cache (all templates)
  965. *
  966. * @param string $exp_time expire time
  967. * @return boolean results of {@link smarty_core_rm_auto()}
  968. */
  969. function clear_all_cache($exp_time = null)
  970. {
  971. return $this->clear_cache(null, null, null, $exp_time);
  972. }
  973.  
  974.  
  975. /**
  976. * test to see if valid cache exists for this template
  977. *
  978. * @param string $tpl_file name of template file
  979. * @param string $cache_id
  980. * @param string $compile_id
  981. * @return string|false results of {@link _read_cache_file()}
  982. */
  983. function is_cached($tpl_file, $cache_id = null, $compile_id = null)
  984. {
  985. if (!$this->caching)
  986. return false;
  987.  
  988. if (!isset($compile_id))
  989. $compile_id = $this->compile_id;
  990.  
  991. $_params = array(
  992. 'tpl_file' => $tpl_file,
  993. 'cache_id' => $cache_id,
  994. 'compile_id' => $compile_id
  995. );
  996. require_once(SMARTY_CORE_DIR . 'core.read_cache_file.php');
  997. return smarty_core_read_cache_file($_params, $this);
  998. }
  999.  
  1000.  
  1001. /**
  1002. * clear all the assigned template variables.
  1003. *
  1004. */
  1005. function clear_all_assign()
  1006. {
  1007. $this->_tpl_vars = array();
  1008. }
  1009.  
  1010. /**
  1011. * clears compiled version of specified template resource,
  1012. * or all compiled template files if one is not specified.
  1013. * This function is for advanced use only, not normally needed.
  1014. *
  1015. * @param string $tpl_file
  1016. * @param string $compile_id
  1017. * @param string $exp_time
  1018. * @return boolean results of {@link smarty_core_rm_auto()}
  1019. */
  1020. function clear_compiled_tpl($tpl_file = null, $compile_id = null, $exp_time = null)
  1021. {
  1022. if (!isset($compile_id)) {
  1023. $compile_id = $this->compile_id;
  1024. }
  1025. $_params = array('auto_base' => $this->compile_dir,
  1026. 'auto_source' => $tpl_file,
  1027. 'auto_id' => $compile_id,
  1028. 'exp_time' => $exp_time,
  1029. 'extensions' => array('.inc', '.php'));
  1030. require_once(SMARTY_CORE_DIR . 'core.rm_auto.php');
  1031. return smarty_core_rm_auto($_params, $this);
  1032. }
  1033.  
  1034. /**
  1035. * Checks whether requested template exists.
  1036. *
  1037. * @param string $tpl_file
  1038. * @return boolean
  1039. */
  1040. function template_exists($tpl_file)
  1041. {
  1042. $_params = array('resource_name' => $tpl_file, 'quiet'=>true, 'get_source'=>false);
  1043. return $this->_fetch_resource_info($_params);
  1044. }
  1045.  
  1046. /**
  1047. * Returns an array containing template variables
  1048. *
  1049. * @param string $name
  1050. * @param string $type
  1051. * @return array
  1052. */
  1053. function &get_template_vars($name=null)
  1054. {
  1055. if(!isset($name)) {
  1056. return $this->_tpl_vars;
  1057. } elseif(isset($this->_tpl_vars[$name])) {
  1058. return $this->_tpl_vars[$name];
  1059. } else {
  1060. // var non-existant, return valid reference
  1061. $_tmp = null;
  1062. return $_tmp;
  1063. }
  1064. }
  1065.  
  1066. /**
  1067. * Returns an array containing config variables
  1068. *
  1069. * @param string $name
  1070. * @param string $type
  1071. * @return array
  1072. */
  1073. function &get_config_vars($name=null)
  1074. {
  1075. if(!isset($name) && is_array($this->_config[0])) {
  1076. return $this->_config[0]['vars'];
  1077. } else if(isset($this->_config[0]['vars'][$name])) {
  1078. return $this->_config[0]['vars'][$name];
  1079. } else {
  1080. // var non-existant, return valid reference
  1081. $_tmp = null;
  1082. return $_tmp;
  1083. }
  1084. }
  1085.  
  1086. /**
  1087. * trigger Smarty error
  1088. *
  1089. * @param string $error_msg
  1090. * @param integer $error_type
  1091. */
  1092. function trigger_error($error_msg, $error_type = E_USER_WARNING)
  1093. {
  1094. trigger_error("Smarty error: $error_msg", $error_type);
  1095. }
  1096.  
  1097.  
  1098. /**
  1099. * executes & displays the template results
  1100. *
  1101. * @param string $resource_name
  1102. * @param string $cache_id
  1103. * @param string $compile_id
  1104. */
  1105. function display($resource_name, $cache_id = null, $compile_id = null)
  1106. {
  1107. $this->fetch($resource_name, $cache_id, $compile_id, true);
  1108. }
  1109.  
  1110. /**
  1111. * executes & returns or displays the template results
  1112. *
  1113. * @param string $resource_name
  1114. * @param string $cache_id
  1115. * @param string $compile_id
  1116. * @param boolean $display
  1117. */
  1118. function fetch($resource_name, $cache_id = null, $compile_id = null, $display = false)
  1119. {
  1120. static $_cache_info = array();
  1121.  
  1122. $_smarty_old_error_level = $this->debugging ? error_reporting() : error_reporting(isset($this->error_reporting)
  1123. ? $this->error_reporting : error_reporting() & ~E_NOTICE);
  1124.  
  1125. if (!$this->debugging && $this->debugging_ctrl == 'URL') {
  1126. $_query_string = $this->request_use_auto_globals ? $_SERVER['QUERY_STRING'] : $GLOBALS['HTTP_SERVER_VARS']['QUERY_STRING'];
  1127. if (@strstr($_query_string, $this->_smarty_debug_id)) {
  1128. if (@strstr($_query_string, $this->_smarty_debug_id . '=on')) {
  1129. // enable debugging for this browser session
  1130. @setcookie('SMARTY_DEBUG', true);
  1131. $this->debugging = true;
  1132. } elseif (@strstr($_query_string, $this->_smarty_debug_id . '=off')) {
  1133. // disable debugging for this browser session
  1134. @setcookie('SMARTY_DEBUG', false);
  1135. $this->debugging = false;
  1136. } else {
  1137. // enable debugging for this page
  1138. $this->debugging = true;
  1139. }
  1140. } else {
  1141. $this->debugging = (bool)($this->request_use_auto_globals ? @$_COOKIE['SMARTY_DEBUG'] : @$GLOBALS['HTTP_COOKIE_VARS']['SMARTY_DEBUG']);
  1142. }
  1143. }
  1144.  
  1145. if ($this->debugging) {
  1146. // capture time for debugging info
  1147. $_params = array();
  1148. require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
  1149. $_debug_start_time = smarty_core_get_microtime($_params, $this);
  1150. $this->_smarty_debug_info[] = array('type' => 'template',
  1151. 'filename' => $resource_name,
  1152. 'depth' => 0);
  1153. $_included_tpls_idx = count($this->_smarty_debug_info) - 1;
  1154. }
  1155.  
  1156. if (!isset($compile_id)) {
  1157. $compile_id = $this->compile_id;
  1158. }
  1159.  
  1160. $this->_compile_id = $compile_id;
  1161. $this->_inclusion_depth = 0;
  1162.  
  1163. if ($this->caching) {
  1164. // save old cache_info, initialize cache_info
  1165. array_push($_cache_info, $this->_cache_info);
  1166. $this->_cache_info = array();
  1167. $_params = array(
  1168. 'tpl_file' => $resource_name,
  1169. 'cache_id' => $cache_id,
  1170. 'compile_id' => $compile_id,
  1171. 'results' => null
  1172. );
  1173. require_once(SMARTY_CORE_DIR . 'core.read_cache_file.php');
  1174. if (smarty_core_read_cache_file($_params, $this)) {
  1175. $_smarty_results = $_params['results'];
  1176. if (!empty($this->_cache_info['insert_tags'])) {
  1177. $_params = array('plugins' => $this->_cache_info['insert_tags']);
  1178. require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');
  1179. smarty_core_load_plugins($_params, $this);
  1180. $_params = array('results' => $_smarty_results);
  1181. require_once(SMARTY_CORE_DIR . 'core.process_cached_inserts.php');
  1182. $_smarty_results = smarty_core_process_cached_inserts($_params, $this);
  1183. }
  1184. if (!empty($this->_cache_info['cache_serials'])) {
  1185. $_params = array('results' => $_smarty_results);
  1186. require_once(SMARTY_CORE_DIR . 'core.process_compiled_include.php');
  1187. $_smarty_results = smarty_core_process_compiled_include($_params, $this);
  1188. }
  1189.  
  1190.  
  1191. if ($display) {
  1192. if ($this->debugging)
  1193. {
  1194. // capture time for debugging info
  1195. $_params = array();
  1196. require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
  1197. $this->_smarty_debug_info[$_included_tpls_idx]['exec_time'] = smarty_core_get_microtime($_params, $this) - $_debug_start_time;
  1198. require_once(SMARTY_CORE_DIR . 'core.display_debug_console.php');
  1199. $_smarty_results .= smarty_core_display_debug_console($_params, $this);
  1200. }
  1201. if ($this->cache_modified_check) {
  1202. $_server_vars = ($this->request_use_auto_globals) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
  1203. $_last_modified_date = @substr($_server_vars['HTTP_IF_MODIFIED_SINCE'], 0, strpos($_server_vars['HTTP_IF_MODIFIED_SINCE'], 'GMT') + 3);
  1204. $_gmt_mtime = gmdate('D, d M Y H:i:s', $this->_cache_info['timestamp']).' GMT';
  1205. if (@count($this->_cache_info['insert_tags']) == 0
  1206. && !$this->_cache_serials
  1207. && $_gmt_mtime == $_last_modified_date) {
  1208. if (php_sapi_name()=='cgi')
  1209. header('Status: 304 Not Modified');
  1210. else
  1211. header('HTTP/1.1 304 Not Modified');
  1212.  
  1213. } else {
  1214. header('Last-Modified: '.$_gmt_mtime);
  1215. echo $_smarty_results;
  1216. }
  1217. } else {
  1218. echo $_smarty_results;
  1219. }
  1220. error_reporting($_smarty_old_error_level);
  1221. // restore initial cache_info
  1222. $this->_cache_info = array_pop($_cache_info);
  1223. return true;
  1224. } else {
  1225. error_reporting($_smarty_old_error_level);
  1226. // restore initial cache_info
  1227. $this->_cache_info = array_pop($_cache_info);
  1228. return $_smarty_results;
  1229. }
  1230. } else {
  1231. $this->_cache_info['template'][$resource_name] = true;
  1232. if ($this->cache_modified_check && $display) {
  1233. header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
  1234. }
  1235. }
  1236. }
  1237.  
  1238. // load filters that are marked as autoload
  1239. if (count($this->autoload_filters)) {
  1240. foreach ($this->autoload_filters as $_filter_type => $_filters) {
  1241. foreach ($_filters as $_filter) {
  1242. $this->load_filter($_filter_type, $_filter);
  1243. }
  1244. }
  1245. }
  1246.  
  1247. $_smarty_compile_path = $this->_get_compile_path($resource_name);
  1248.  
  1249. // if we just need to display the results, don't perform output
  1250. // buffering - for speed
  1251. $_cache_including = $this->_cache_including;
  1252. $this->_cache_including = false;
  1253. if ($display && !$this->caching && count($this->_plugins['outputfilter']) == 0) {
  1254. if ($this->_is_compiled($resource_name, $_smarty_compile_path)
  1255. || $this->_compile_resource($resource_name, $_smarty_compile_path))
  1256. {
  1257. include($_smarty_compile_path);
  1258. }
  1259. } else {
  1260. ob_start();
  1261. if ($this->_is_compiled($resource_name, $_smarty_compile_path)
  1262. || $this->_compile_resource($resource_name, $_smarty_compile_path))
  1263. {
  1264. include($_smarty_compile_path);
  1265. }
  1266. $_smarty_results = ob_get_contents();
  1267. ob_end_clean();
  1268.  
  1269. foreach ((array)$this->_plugins['outputfilter'] as $_output_filter) {
  1270. $_smarty_results = call_user_func_array($_output_filter[0], array($_smarty_results, &$this));
  1271. }
  1272. }
  1273.  
  1274. if ($this->caching) {
  1275. $_params = array('tpl_file' => $resource_name,
  1276. 'cache_id' => $cache_id,
  1277. 'compile_id' => $compile_id,
  1278. 'results' => $_smarty_results);
  1279. require_once(SMARTY_CORE_DIR . 'core.write_cache_file.php');
  1280. smarty_core_write_cache_file($_params, $this);
  1281. require_once(SMARTY_CORE_DIR . 'core.process_cached_inserts.php');
  1282. $_smarty_results = smarty_core_process_cached_inserts($_params, $this);
  1283.  
  1284. if ($this->_cache_serials) {
  1285. // strip nocache-tags from output
  1286. $_smarty_results = preg_replace('!(\{/?nocache\:[0-9a-f]{32}#\d+\})!s'
  1287. ,''
  1288. ,$_smarty_results);
  1289. }
  1290. // restore initial cache_info
  1291. $this->_cache_info = array_pop($_cache_info);
  1292. }
  1293. $this->_cache_including = $_cache_including;
  1294.  
  1295. if ($display) {
  1296. if (isset($_smarty_results)) { echo $_smarty_results; }
  1297. if ($this->debugging) {
  1298. // capture time for debugging info
  1299. $_params = array();
  1300. require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
  1301. $this->_smarty_debug_info[$_included_tpls_idx]['exec_time'] = (smarty_core_get_microtime($_params, $this) - $_debug_start_time);
  1302. require_once(SMARTY_CORE_DIR . 'core.display_debug_console.php');
  1303. echo smarty_core_display_debug_console($_params, $this);
  1304. }
  1305. error_reporting($_smarty_old_error_level);
  1306. return;
  1307. } else {
  1308. error_reporting($_smarty_old_error_level);
  1309. if (isset($_smarty_results)) { return $_smarty_results; }
  1310. }
  1311. }
  1312.  
  1313. /**
  1314. * load configuration values
  1315. *
  1316. * @param string $file
  1317. * @param string $section
  1318. * @param string $scope
  1319. */
  1320. function config_load($file, $section = null, $scope = 'global')
  1321. {
  1322. require_once($this->_get_plugin_filepath('function', 'config_load'));
  1323. smarty_function_config_load(array('file' => $file, 'section' => $section, 'scope' => $scope), $this);
  1324. }
  1325.  
  1326. /**
  1327. * return a reference to a registered object
  1328. *
  1329. * @param string $name
  1330. * @return object
  1331. */
  1332. function &get_registered_object($name) {
  1333. if (!isset($this->_reg_objects[$name]))
  1334. $this->_trigger_fatal_error("'$name' is not a registered object");
  1335.  
  1336. if (!is_object($this->_reg_objects[$name][0]))
  1337. $this->_trigger_fatal_error("registered '$name' is not an object");
  1338.  
  1339. return $this->_reg_objects[$name][0];
  1340. }
  1341.  
  1342. /**
  1343. * clear configuration values
  1344. *
  1345. * @param string $var
  1346. */
  1347. function clear_config($var = null)
  1348. {
  1349. if(!isset($var)) {
  1350. // clear all values
  1351. $this->_config = array(array('vars' => array(),
  1352. 'files' => array()));
  1353. } else {
  1354. unset($this->_config[0]['vars'][$var]);
  1355. }
  1356. }
  1357.  
  1358. /**
  1359. * get filepath of requested plugin
  1360. *
  1361. * @param string $type
  1362. * @param string $name
  1363. * @return string|false
  1364. */
  1365. function _get_plugin_filepath($type, $name)
  1366. {
  1367. $_params = array('type' => $type, 'name' => $name);
  1368. require_once(SMARTY_CORE_DIR . 'core.assemble_plugin_filepath.php');
  1369. return smarty_core_assemble_plugin_filepath($_params, $this);
  1370. }
  1371.  
  1372. /**
  1373. * test if resource needs compiling
  1374. *
  1375. * @param string $resource_name
  1376. * @param string $compile_path
  1377. * @return boolean
  1378. */
  1379. function _is_compiled($resource_name, $compile_path)
  1380. {
  1381. if (!$this->force_compile && file_exists($compile_path)) {
  1382. if (!$this->compile_check) {
  1383. // no need to check compiled file
  1384. return true;
  1385. } else {
  1386. // get file source and timestamp
  1387. $_params = array('resource_name' => $resource_name, 'get_source'=>false);
  1388. if (!$this->_fetch_resource_info($_params)) {
  1389. return false;
  1390. }
  1391. if ($_params['resource_timestamp'] <= filemtime($compile_path)) {
  1392. // template not expired, no recompile
  1393. return true;
  1394. } else {
  1395. // compile template
  1396. return false;
  1397. }
  1398. }
  1399. } else {
  1400. // compiled template does not exist, or forced compile
  1401. return false;
  1402. }
  1403. }
  1404.  
  1405. /**
  1406. * compile the template
  1407. *
  1408. * @param string $resource_name
  1409. * @param string $compile_path
  1410. * @return boolean
  1411. */
  1412. function _compile_resource($resource_name, $compile_path)
  1413. {
  1414.  
  1415. $_params = array('resource_name' => $resource_name);
  1416. if (!$this->_fetch_resource_info($_params)) {
  1417. return false;
  1418. }
  1419.  
  1420. $_source_content = $_params['source_content'];
  1421. $_cache_include = substr($compile_path, 0, -4).'.inc';
  1422.  
  1423. if ($this->_compile_source($resource_name, $_source_content, $_compiled_content, $_cache_include)) {
  1424. // if a _cache_serial was set, we also have to write an include-file:
  1425. if ($this->_cache_include_info) {
  1426. require_once(SMARTY_CORE_DIR . 'core.write_compiled_include.php');
  1427. smarty_core_write_compiled_include(array_merge($this->_cache_include_info, array('compiled_content'=>$_compiled_content, 'resource_name'=>$resource_name)), $this);
  1428. }
  1429.  
  1430. $_params = array('compile_path'=>$compile_path, 'compiled_content' => $_compiled_content);
  1431. require_once(SMARTY_CORE_DIR . 'core.write_compiled_resource.php');
  1432. smarty_core_write_compiled_resource($_params, $this);
  1433.  
  1434. return true;
  1435. } else {
  1436. return false;
  1437. }
  1438.  
  1439. }
  1440.  
  1441. /**
  1442. * compile the given source
  1443. *
  1444. * @param string $resource_name
  1445. * @param string $source_content
  1446. * @param string $compiled_content
  1447. * @return boolean
  1448. */
  1449. function _compile_source($resource_name, &$source_content, &$compiled_content, $cache_include_path=null)
  1450. {
  1451. if (file_exists(SMARTY_DIR . $this->compiler_file)) {
  1452. require_once(SMARTY_DIR . $this->compiler_file);
  1453. } else {
  1454. // use include_path
  1455. require_once($this->compiler_file);
  1456. }
  1457.  
  1458.  
  1459. $smarty_compiler = new $this->compiler_class;
  1460.  
  1461. $smarty_compiler->template_dir = $this->template_dir;
  1462. $smarty_compiler->compile_dir = $this->compile_dir;
  1463. $smarty_compiler->plugins_dir = $this->plugins_dir;
  1464. $smarty_compiler->config_dir = $this->config_dir;
  1465. $smarty_compiler->force_compile = $this->force_compile;
  1466. $smarty_compiler->caching = $this->caching;
  1467. $smarty_compiler->php_handling = $this->php_handling;
  1468. $smarty_compiler->left_delimiter = $this->left_delimiter;
  1469. $smarty_compiler->right_delimiter = $this->right_delimiter;
  1470. $smarty_compiler->_version = $this->_version;
  1471. $smarty_compiler->security = $this->security;
  1472. $smarty_compiler->secure_dir = $this->secure_dir;
  1473. $smarty_compiler->security_settings = $this->security_settings;
  1474. $smarty_compiler->trusted_dir = $this->trusted_dir;
  1475. $smarty_compiler->use_sub_dirs = $this->use_sub_dirs;
  1476. $smarty_compiler->_reg_objects = &$this->_reg_objects;
  1477. $smarty_compiler->_plugins = &$this->_plugins;
  1478. $smarty_compiler->_tpl_vars = &$this->_tpl_vars;
  1479. $smarty_compiler->default_modifiers = $this->default_modifiers;
  1480. $smarty_compiler->compile_id = $this->_compile_id;
  1481. $smarty_compiler->_config = $this->_config;
  1482. $smarty_compiler->request_use_auto_globals = $this->request_use_auto_globals;
  1483.  
  1484. if (isset($cache_include_path) && isset($this->_cache_serials[$cache_include_path])) {
  1485. $smarty_compiler->_cache_serial = $this->_cache_serials[$cache_include_path];
  1486. }
  1487. $smarty_compiler->_cache_include = $cache_include_path;
  1488.  
  1489.  
  1490. $_results = $smarty_compiler->_compile_file($resource_name, $source_content, $compiled_content);
  1491.  
  1492. if ($smarty_compiler->_cache_serial) {
  1493. $this->_cache_include_info = array(
  1494. 'cache_serial'=>$smarty_compiler->_cache_serial
  1495. ,'plugins_code'=>$smarty_compiler->_plugins_code
  1496. ,'include_file_path' => $cache_include_path);
  1497.  
  1498. } else {
  1499. $this->_cache_include_info = null;
  1500.  
  1501. }
  1502.  
  1503. return $_results;
  1504. }
  1505.  
  1506. /**
  1507. * Get the compile path for this resource
  1508. *
  1509. * @param string $resource_name
  1510. * @return string results of {@link _get_auto_filename()}
  1511. */
  1512. function _get_compile_path($resource_name)
  1513. {
  1514. return $this->_get_auto_filename($this->compile_dir, $resource_name,
  1515. $this->_compile_id) . '.php';
  1516. }
  1517.  
  1518. /**
  1519. * fetch the template info. Gets timestamp, and source
  1520. * if get_source is true
  1521. *
  1522. * sets $source_content to the source of the template, and
  1523. * $resource_timestamp to its time stamp
  1524. * @param string $resource_name
  1525. * @param string $source_content
  1526. * @param integer $resource_timestamp
  1527. * @param boolean $get_source
  1528. * @param boolean $quiet
  1529. * @return boolean
  1530. */
  1531.  
  1532. function _fetch_resource_info(&$params)
  1533. {
  1534. if(!isset($params['get_source'])) { $params['get_source'] = true; }
  1535. if(!isset($params['quiet'])) { $params['quiet'] = false; }
  1536.  
  1537. $_return = false;
  1538. $_params = array('resource_name' => $params['resource_name']) ;
  1539. if (isset($params['resource_base_path']))
  1540. $_params['resource_base_path'] = $params['resource_base_path'];
  1541. else
  1542. $_params['resource_base_path'] = $this->template_dir;
  1543.  
  1544. if ($this->_parse_resource_name($_params)) {
  1545. $_resource_type = $_params['resource_type'];
  1546. $_resource_name = $_params['resource_name'];
  1547. switch ($_resource_type) {
  1548. case 'file':
  1549. if ($params['get_source']) {
  1550. $params['source_content'] = $this->_read_file($_resource_name);
  1551. }
  1552. $params['resource_timestamp'] = filemtime($_resource_name);
  1553. $_return = is_file($_resource_name);
  1554. break;
  1555.  
  1556. default:
  1557. // call resource functions to fetch the template source and timestamp
  1558. if ($params['get_source']) {
  1559. $_source_return = isset($this->_plugins['resource'][$_resource_type]) &&
  1560. call_user_func_array($this->_plugins['resource'][$_resource_type][0][0],
  1561. array($_resource_name, &$params['source_content'], &$this));
  1562. } else {
  1563. $_source_return = true;
  1564. }
  1565.  
  1566. $_timestamp_return = isset($this->_plugins['resource'][$_resource_type]) &&
  1567. call_user_func_array($this->_plugins['resource'][$_resource_type][0][1],
  1568. array($_resource_name, &$params['resource_timestamp'], &$this));
  1569.  
  1570. $_return = $_source_return && $_timestamp_return;
  1571. break;
  1572. }
  1573. }
  1574.  
  1575. if (!$_return) {
  1576. // see if we can get a template with the default template handler
  1577. if (!empty($this->default_template_handler_func)) {
  1578. if (!is_callable($this->default_template_handler_func)) {
  1579. $this->trigger_error("default template handler function \"$this->default_template_handler_func\" doesn't exist.");
  1580. } else {
  1581. $_return = call_user_func_array(
  1582. $this->default_template_handler_func,
  1583. array($_params['resource_type'], $_params['resource_name'], &$params['source_content'], &$params['resource_timestamp'], &$this));
  1584. }
  1585. }
  1586. }
  1587.  
  1588. if (!$_return) {
  1589. if (!$params['quiet']) {
  1590. $this->trigger_error('unable to read resource: "' . $params['resource_name'] . '"');
  1591. }
  1592. } else if ($_return && $this->security) {
  1593. require_once(SMARTY_CORE_DIR . 'core.is_secure.php');
  1594. if (!smarty_core_is_secure($_params, $this)) {
  1595. if (!$params['quiet'])
  1596. $this->trigger_error('(secure mode) accessing "' . $params['resource_name'] . '" is not allowed');
  1597. $params['source_content'] = null;
  1598. $params['resource_timestamp'] = null;
  1599. return false;
  1600. }
  1601. }
  1602. return $_return;
  1603. }
  1604.  
  1605.  
  1606. /**
  1607. * parse out the type and name from the resource
  1608. *
  1609. * @param string $resource_base_path
  1610. * @param string $resource_name
  1611. * @param string $resource_type
  1612. * @param string $resource_name
  1613. * @return boolean
  1614. */
  1615.  
  1616. function _parse_resource_name(&$params)
  1617. {
  1618.  
  1619. // split tpl_path by the first colon
  1620. $_resource_name_parts = explode(':', $params['resource_name'], 2);
  1621.  
  1622. if (count($_resource_name_parts) == 1) {
  1623. // no resource type given
  1624. $params['resource_type'] = $this->default_resource_type;
  1625. $params['resource_name'] = $_resource_name_parts[0];
  1626. } else {
  1627. if(strlen($_resource_name_parts[0]) == 1) {
  1628. // 1 char is not resource type, but part of filepath
  1629. $params['resource_type'] = $this->default_resource_type;
  1630. $params['resource_name'] = $params['resource_name'];
  1631. } else {
  1632. $params['resource_type'] = $_resource_name_parts[0];
  1633. $params['resource_name'] = $_resource_name_parts[1];
  1634. }
  1635. }
  1636.  
  1637. if ($params['resource_type'] == 'file') {
  1638. if (!preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $params['resource_name'])) {
  1639. // relative pathname to $params['resource_base_path']
  1640. // use the first directory where the file is found
  1641. foreach ((array)$params['resource_base_path'] as $_curr_path) {
  1642. $_fullpath = $_curr_path . DIRECTORY_SEPARATOR . $params['resource_name'];
  1643. if (file_exists($_fullpath) && is_file($_fullpath)) {
  1644. $params['resource_name'] = $_fullpath;
  1645. return true;
  1646. }
  1647. // didn't find the file, try include_path
  1648. $_params = array('file_path' => $_fullpath);
  1649. require_once(SMARTY_CORE_DIR . 'core.get_include_path.php');
  1650. if(smarty_core_get_include_path($_params, $this)) {
  1651. $params['resource_name'] = $_params['new_file_path'];
  1652. return true;
  1653. }
  1654. }
  1655. return false;
  1656. } else {
  1657. /* absolute path */
  1658. return file_exists($params['resource_name']);
  1659. }
  1660. } elseif (empty($this->_plugins['resource'][$params['resource_type']])) {
  1661. $_params = array('type' => $params['resource_type']);
  1662. require_once(SMARTY_CORE_DIR . 'core.load_resource_plugin.php');
  1663. smarty_core_load_resource_plugin($_params, $this);
  1664. }
  1665.  
  1666. return true;
  1667. }
  1668.  
  1669.  
  1670. /**
  1671. * Handle modifiers
  1672. *
  1673. * @param string|null $modifier_name
  1674. * @param array|null $map_array
  1675. * @return string result of modifiers
  1676. */
  1677. function _run_mod_handler()
  1678. {
  1679. $_args = func_get_args();
  1680. list($_modifier_name, $_map_array) = array_splice($_args, 0, 2);
  1681. list($_func_name, $_tpl_file, $_tpl_line) =
  1682. $this->_plugins['modifier'][$_modifier_name];
  1683.  
  1684. $_var = $_args[0];
  1685. foreach ($_var as $_key => $_val) {
  1686. $_args[0] = $_val;
  1687. $_var[$_key] = call_user_func_array($_func_name, $_args);
  1688. }
  1689. return $_var;
  1690. }
  1691.  
  1692. /**
  1693. * Remove starting and ending quotes from the string
  1694. *
  1695. * @param string $string
  1696. * @return string
  1697. */
  1698. function _dequote($string)
  1699. {
  1700. if ((substr($string, 0, 1) == "'" || substr($string, 0, 1) == '"') &&
  1701. substr($string, -1) == substr($string, 0, 1))
  1702. return substr($string, 1, -1);
  1703. else
  1704. return $string;
  1705. }
  1706.  
  1707.  
  1708. /**
  1709. * read in a file
  1710. *
  1711. * @param string $filename
  1712. * @return string
  1713. */
  1714. function _read_file($filename)
  1715. {
  1716. if ( file_exists($filename) && ($fd = @fopen($filename, 'rb')) ) {
  1717. $contents = '';
  1718. while (!feof($fd)) {
  1719. $contents .= fread($fd, 8192);
  1720. }
  1721. fclose($fd);
  1722. return $contents;
  1723. } else {
  1724. return false;
  1725. }
  1726. }
  1727.  
  1728. /**
  1729. * get a concrete filename for automagically created content
  1730. *
  1731. * @param string $auto_base
  1732. * @param string $auto_source
  1733. * @param string $auto_id
  1734. * @return string
  1735. * @staticvar string|null
  1736. * @staticvar string|null
  1737. */
  1738. function _get_auto_filename($auto_base, $auto_source = null, $auto_id = null)
  1739. {
  1740. $_compile_dir_sep = $this->use_sub_dirs ? DIRECTORY_SEPARATOR : '^';
  1741. $_return = $auto_base . DIRECTORY_SEPARATOR;
  1742.  
  1743. if(isset($auto_id)) {
  1744. // make auto_id safe for directory names
  1745. $auto_id = str_replace('%7C',$_compile_dir_sep,(urlencode($auto_id)));
  1746. // split into separate directories
  1747. $_return .= $auto_id . $_compile_dir_sep;
  1748. }
  1749.  
  1750. if(isset($auto_source)) {
  1751. // make source name safe for filename
  1752. $_filename = urlencode(basename($auto_source));
  1753. $_crc32 = sprintf('%08X', crc32($auto_source));
  1754. // prepend %% to avoid name conflicts with
  1755. // with $params['auto_id'] names
  1756. $_crc32 = substr($_crc32, 0, 2) . $_compile_dir_sep .
  1757. substr($_crc32, 0, 3) . $_compile_dir_sep . $_crc32;
  1758. $_return .= '%%' . $_crc32 . '%%' . $_filename;
  1759. }
  1760.  
  1761. return $_return;
  1762. }
  1763.  
  1764. /**
  1765. * unlink a file, possibly using expiration time
  1766. *
  1767. * @param string $resource
  1768. * @param integer $exp_time
  1769. */
  1770. function _unlink($resource, $exp_time = null)
  1771. {
  1772. if(isset($exp_time)) {
  1773. if(time() - @filemtime($resource) >= $exp_time) {
  1774. return @unlink($resource);
  1775. }
  1776. } else {
  1777. return @unlink($resource);
  1778. }
  1779. }
  1780.  
  1781. /**
  1782. * returns an auto_id for auto-file-functions
  1783. *
  1784. * @param string $cache_id
  1785. * @param string $compile_id
  1786. * @return string|null
  1787. */
  1788. function _get_auto_id($cache_id=null, $compile_id=null) {
  1789. if (isset($cache_id))
  1790. return (isset($compile_id)) ? $cache_id . '|' . $compile_id : $cache_id;
  1791. elseif(isset($compile_id))
  1792. return $compile_id;
  1793. else
  1794. return null;
  1795. }
  1796.  
  1797. /**
  1798. * trigger Smarty plugin error
  1799. *
  1800. * @param string $error_msg
  1801. * @param string $tpl_file
  1802. * @param integer $tpl_line
  1803. * @param string $file
  1804. * @param integer $line
  1805. * @param integer $error_type
  1806. */
  1807. function _trigger_fatal_error($error_msg, $tpl_file = null, $tpl_line = null,
  1808. $file = null, $line = null, $error_type = E_USER_ERROR)
  1809. {
  1810. if(isset($file) && isset($line)) {
  1811. $info = ' ('.basename($file).", line $line)";
  1812. } else {
  1813. $info = '';
  1814. }
  1815. if (isset($tpl_line) && isset($tpl_file)) {
  1816. $this->trigger_error('[in ' . $tpl_file . ' line ' . $tpl_line . "]: $error_msg$info", $error_type);
  1817. } else {
  1818. $this->trigger_error($error_msg . $info, $error_type);
  1819. }
  1820. }
  1821.  
  1822.  
  1823. /**
  1824. * callback function for preg_replace, to call a non-cacheable block
  1825. * @return string
  1826. */
  1827. function _process_compiled_include_callback($match) {
  1828. $_func = '_smarty_tplfunc_'.$match[2].'_'.$match[3];
  1829. ob_start();
  1830. $_func($this);
  1831. $_ret = ob_get_contents();
  1832. ob_end_clean();
  1833. return $_ret;
  1834. }
  1835.  
  1836.  
  1837. /**
  1838. * called for included templates
  1839. *
  1840. * @param string $_smarty_include_tpl_file
  1841. * @param string $_smarty_include_vars
  1842. */
  1843.  
  1844. // $_smarty_include_tpl_file, $_smarty_include_vars
  1845.  
  1846. function _smarty_include($params)
  1847. {
  1848. if ($this->debugging) {
  1849. $_params = array();
  1850. require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
  1851. $debug_start_time = smarty_core_get_microtime($_params, $this);
  1852. $this->_smarty_debug_info[] = array('type' => 'template',
  1853. 'filename' => $params['smarty_include_tpl_file'],
  1854. 'depth' => ++$this->_inclusion_depth);
  1855. $included_tpls_idx = count($this->_smarty_debug_info) - 1;
  1856. }
  1857.  
  1858. $this->_tpl_vars = array_merge($this->_tpl_vars, $params['smarty_include_vars']);
  1859.  
  1860. // config vars are treated as local, so push a copy of the
  1861. // current ones onto the front of the stack
  1862. array_unshift($this->_config, $this->_config[0]);
  1863.  
  1864. $_smarty_compile_path = $this->_get_compile_path($params['smarty_include_tpl_file']);
  1865.  
  1866.  
  1867. if ($this->_is_compiled($params['smarty_include_tpl_file'], $_smarty_compile_path)
  1868. || $this->_compile_resource($params['smarty_include_tpl_file'], $_smarty_compile_path))
  1869. {
  1870. include($_smarty_compile_path);
  1871. }
  1872.  
  1873. // pop the local vars off the front of the stack
  1874. array_shift($this->_config);
  1875.  
  1876. $this->_inclusion_depth--;
  1877.  
  1878. if ($this->debugging) {
  1879. // capture time for debugging info
  1880. $_params = array();
  1881. require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
  1882. $this->_smarty_debug_info[$included_tpls_idx]['exec_time'] = smarty_core_get_microtime($_params, $this) - $debug_start_time;
  1883. }
  1884.  
  1885. if ($this->caching) {
  1886. $this->_cache_info['template'][$params['smarty_include_tpl_file']] = true;
  1887. }
  1888. }
  1889.  
  1890.  
  1891. /**
  1892. * get or set an array of cached attributes for function that is
  1893. * not cacheable
  1894. * @return array
  1895. */
  1896. function &_smarty_cache_attrs($cache_serial, $count) {
  1897. $_cache_attrs =& $this->_cache_info['cache_attrs'][$cache_serial][$count];
  1898.  
  1899. if ($this->_cache_including) {
  1900. /* return next set of cache_attrs */
  1901. $_return = current($_cache_attrs);
  1902. next($_cache_attrs);
  1903. return $_return;
  1904.  
  1905. } else {
  1906. /* add a reference to a new set of cache_attrs */
  1907. $_cache_attrs[] = array();
  1908. return $_cache_attrs[count($_cache_attrs)-1];
  1909.  
  1910. }
  1911.  
  1912. }
  1913.  
  1914.  
  1915. /**
  1916. * wrapper for include() retaining $this
  1917. * @return mixed
  1918. */
  1919. function _include($filename, $once=false, $params=null)
  1920. {
  1921. if ($once) {
  1922. return include_once($filename);
  1923. } else {
  1924. return include($filename);
  1925. }
  1926. }
  1927.  
  1928.  
  1929. /**
  1930. * wrapper for eval() retaining $this
  1931. * @return mixed
  1932. */
  1933. function _eval($code, $params=null)
  1934. {
  1935. return eval($code);
  1936. }
  1937. /**#@-*/
  1938.  
  1939. }
  1940.  
  1941. /* vim: set expandtab: */
  1942.  
  1943. ?>
  1944.  
  1945.  
Advertisement
Add Comment
Please, Sign In to add comment