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