Ticket #989: Smarty.class.php

File Smarty.class.php, 64.5 KB (added by INETvisio, 9 years ago)

Für mich bräuchte ich eine Kind-Template (MY_MASTER_*), das kann man ignorieren.

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