1 : <?php
2 : /**
3 : * Project: Smarty: the PHP compiling template engine
4 : * File: Smarty.class.php
5 : * SVN: $Id: Smarty.class.php 4426 2011-10-19 19:20:58Z monte.ohrt $
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 2008 New Digital Group, Inc.
27 : * @author Monte Ohrt <monte at ohrt dot com>
28 : * @author Uwe Tews
29 : * @author Rodney Rehm
30 : * @package Smarty
31 : * @version 3.1.4
32 : */
33 :
34 : /**
35 : * define shorthand directory separator constant
36 : */
37 : if (!defined('DS')) {
38 : define('DS', DIRECTORY_SEPARATOR);
39 : }
40 :
41 : /**
42 : * set SMARTY_DIR to absolute path to Smarty library files.
43 : * Sets SMARTY_DIR only if user application has not already defined it.
44 : */
45 : if (!defined('SMARTY_DIR')) {
46 : define('SMARTY_DIR', dirname(__FILE__) . DS);
47 : }
48 :
49 : /**
50 : * set SMARTY_SYSPLUGINS_DIR to absolute path to Smarty internal plugins.
51 : * Sets SMARTY_SYSPLUGINS_DIR only if user application has not already defined it.
52 : */
53 : if (!defined('SMARTY_SYSPLUGINS_DIR')) {
54 : define('SMARTY_SYSPLUGINS_DIR', SMARTY_DIR . 'sysplugins' . DS);
55 : }
56 : if (!defined('SMARTY_PLUGINS_DIR')) {
57 : define('SMARTY_PLUGINS_DIR', SMARTY_DIR . 'plugins' . DS);
58 : }
59 : if (!defined('SMARTY_MBSTRING')) {
60 : define('SMARTY_MBSTRING', function_exists('mb_strlen'));
61 : }
62 : if (!defined('SMARTY_RESOURCE_CHAR_SET')) {
63 : // UTF-8 can only be done properly when mbstring is available!
64 : define('SMARTY_RESOURCE_CHAR_SET', SMARTY_MBSTRING ? 'UTF-8' : 'ISO-8859-1');
65 : }
66 : if (!defined('SMARTY_RESOURCE_DATE_FORMAT')) {
67 : define('SMARTY_RESOURCE_DATE_FORMAT', '%b %e, %Y');
68 : }
69 :
70 : /**
71 : * register the class autoloader
72 : */
73 : if (!defined('SMARTY_SPL_AUTOLOAD')) {
74 : define('SMARTY_SPL_AUTOLOAD', 0);
75 : }
76 :
77 : if (SMARTY_SPL_AUTOLOAD && set_include_path(get_include_path() . PATH_SEPARATOR . SMARTY_SYSPLUGINS_DIR) !== false) {
78 : $registeredAutoLoadFunctions = spl_autoload_functions();
79 : if (!isset($registeredAutoLoadFunctions['spl_autoload'])) {
80 : spl_autoload_register();
81 : }
82 : } else {
83 : spl_autoload_register('smartyAutoload');
84 : }
85 :
86 : /**
87 : * Load always needed external class files
88 : */
89 : include_once SMARTY_SYSPLUGINS_DIR.'smarty_internal_data.php';
90 : include_once SMARTY_SYSPLUGINS_DIR.'smarty_internal_templatebase.php';
91 : include_once SMARTY_SYSPLUGINS_DIR.'smarty_internal_template.php';
92 : include_once SMARTY_SYSPLUGINS_DIR.'smarty_resource.php';
93 : include_once SMARTY_SYSPLUGINS_DIR.'smarty_internal_resource_file.php';
94 : include_once SMARTY_SYSPLUGINS_DIR.'smarty_cacheresource.php';
95 : include_once SMARTY_SYSPLUGINS_DIR.'smarty_internal_cacheresource_file.php';
96 :
97 : /**
98 : * This is the main Smarty class
99 : * @package Smarty
100 : */
101 : class Smarty extends Smarty_Internal_TemplateBase {
102 :
103 : /**#@+
104 : * constant definitions
105 : */
106 :
107 : /**
108 : * smarty version
109 : */
110 : const SMARTY_VERSION = 'Smarty 3.1.4';
111 :
112 : /**
113 : * define variable scopes
114 : */
115 : const SCOPE_LOCAL = 0;
116 : const SCOPE_PARENT = 1;
117 : const SCOPE_ROOT = 2;
118 : const SCOPE_GLOBAL = 3;
119 : /**
120 : * define caching modes
121 : */
122 : const CACHING_OFF = 0;
123 : const CACHING_LIFETIME_CURRENT = 1;
124 : const CACHING_LIFETIME_SAVED = 2;
125 : /**
126 : * define compile check modes
127 : */
128 : const COMPILECHECK_OFF = 0;
129 : const COMPILECHECK_ON = 1;
130 : const COMPILECHECK_CACHEMISS = 2;
131 : /**
132 : * modes for handling of "<?php ... ?>" tags in templates.
133 : */
134 : const PHP_PASSTHRU = 0; //-> print tags as plain text
135 : const PHP_QUOTE = 1; //-> escape tags as entities
136 : const PHP_REMOVE = 2; //-> escape tags as entities
137 : const PHP_ALLOW = 3; //-> escape tags as entities
138 : /**
139 : * filter types
140 : */
141 : const FILTER_POST = 'post';
142 : const FILTER_PRE = 'pre';
143 : const FILTER_OUTPUT = 'output';
144 : const FILTER_VARIABLE = 'variable';
145 : /**
146 : * plugin types
147 : */
148 : const PLUGIN_FUNCTION = 'function';
149 : const PLUGIN_BLOCK = 'block';
150 : const PLUGIN_COMPILER = 'compiler';
151 : const PLUGIN_MODIFIER = 'modifier';
152 : const PLUGIN_MODIFIERCOMPILER = 'modifiercompiler';
153 :
154 : /**#@-*/
155 :
156 : /**
157 : * assigned global tpl vars
158 : */
159 : public static $global_tpl_vars = array();
160 :
161 : /**
162 : * error handler returned by set_error_hanlder() in Smarty::muteExpectedErrors()
163 : */
164 : public static $_previous_error_handler = null;
165 : /**
166 : * contains directories outside of SMARTY_DIR that are to be muted by muteExpectedErrors()
167 : */
168 : public static $_muted_directories = array();
169 :
170 : /**#@+
171 : * variables
172 : */
173 :
174 : /**
175 : * auto literal on delimiters with whitspace
176 : * @var boolean
177 : */
178 : public $auto_literal = true;
179 : /**
180 : * display error on not assigned variables
181 : * @var boolean
182 : */
183 : public $error_unassigned = false;
184 : /**
185 : * look up relative filepaths in include_path
186 : * @var boolean
187 : */
188 : public $use_include_path = false;
189 : /**
190 : * template directory
191 : * @var array
192 : */
193 : protected $template_dir = array();
194 : /**
195 : * joined template directory string used in cache keys
196 : * @var string
197 : */
198 : public $joined_template_dir = null;
199 : /**
200 : * joined config directory string used in cache keys
201 : * @var string
202 : */
203 : public $joined_config_dir = null;
204 : /**
205 : * default template handler
206 : * @var callable
207 : */
208 : public $default_template_handler_func = null;
209 : /**
210 : * default config handler
211 : * @var callable
212 : */
213 : public $default_config_handler_func = null;
214 : /**
215 : * default plugin handler
216 : * @var callable
217 : */
218 : public $default_plugin_handler_func = null;
219 : /**
220 : * compile directory
221 : * @var string
222 : */
223 : protected $compile_dir = null;
224 : /**
225 : * plugins directory
226 : * @var array
227 : */
228 : protected $plugins_dir = array();
229 : /**
230 : * cache directory
231 : * @var string
232 : */
233 : protected $cache_dir = null;
234 : /**
235 : * config directory
236 : * @var array
237 : */
238 : protected $config_dir = array();
239 : /**
240 : * force template compiling?
241 : * @var boolean
242 : */
243 : public $force_compile = false;
244 : /**
245 : * check template for modifications?
246 : * @var boolean
247 : */
248 : public $compile_check = true;
249 : /**
250 : * use sub dirs for compiled/cached files?
251 : * @var boolean
252 : */
253 : public $use_sub_dirs = false;
254 : /**
255 : * caching enabled
256 : * @var boolean
257 : */
258 : public $caching = false;
259 : /**
260 : * merge compiled includes
261 : * @var boolean
262 : */
263 : public $merge_compiled_includes = false;
264 : /**
265 : * cache lifetime in seconds
266 : * @var integer
267 : */
268 : public $cache_lifetime = 3600;
269 : /**
270 : * force cache file creation
271 : * @var boolean
272 : */
273 : public $force_cache = false;
274 : /**
275 : * Set this if you want different sets of cache files for the same
276 : * templates.
277 : *
278 : * @var string
279 : */
280 : public $cache_id = null;
281 : /**
282 : * Set this if you want different sets of compiled files for the same
283 : * templates.
284 : *
285 : * @var string
286 : */
287 : public $compile_id = null;
288 : /**
289 : * template left-delimiter
290 : * @var string
291 : */
292 : public $left_delimiter = "{";
293 : /**
294 : * template right-delimiter
295 : * @var string
296 : */
297 : public $right_delimiter = "}";
298 : /**#@+
299 : * security
300 : */
301 : /**
302 : * class name
303 : *
304 : * This should be instance of Smarty_Security.
305 : *
306 : * @var string
307 : * @see Smarty_Security
308 : */
309 : public $security_class = 'Smarty_Security';
310 : /**
311 : * implementation of security class
312 : *
313 : * @var Smarty_Security
314 : */
315 : public $security_policy = null;
316 : /**
317 : * controls handling of PHP-blocks
318 : *
319 : * @var integer
320 : */
321 : public $php_handling = self::PHP_PASSTHRU;
322 : /**
323 : * controls if the php template file resource is allowed
324 : *
325 : * @var bool
326 : */
327 : public $allow_php_templates = false;
328 : /**
329 : * Should compiled-templates be prevented from being called directly?
330 : *
331 : * {@internal
332 : * Currently used by Smarty_Internal_Template only.
333 : * }}
334 : *
335 : * @var boolean
336 : */
337 : public $direct_access_security = true;
338 : /**#@-*/
339 : /**
340 : * debug mode
341 : *
342 : * Setting this to true enables the debug-console.
343 : *
344 : * @var boolean
345 : */
346 : public $debugging = false;
347 : /**
348 : * This determines if debugging is enable-able from the browser.
349 : * <ul>
350 : * <li>NONE => no debugging control allowed</li>
351 : * <li>URL => enable debugging when SMARTY_DEBUG is found in the URL.</li>
352 : * </ul>
353 : * @var string
354 : */
355 : public $debugging_ctrl = 'NONE';
356 : /**
357 : * Name of debugging URL-param.
358 : *
359 : * Only used when $debugging_ctrl is set to 'URL'.
360 : * The name of the URL-parameter that activates debugging.
361 : *
362 : * @var type
363 : */
364 : public $smarty_debug_id = 'SMARTY_DEBUG';
365 : /**
366 : * Path of debug template.
367 : * @var string
368 : */
369 : public $debug_tpl = null;
370 : /**
371 : * When set, smarty uses this value as error_reporting-level.
372 : * @var int
373 : */
374 : public $error_reporting = null;
375 : /**
376 : * Internal flag for getTags()
377 : * @var boolean
378 : */
379 : public $get_used_tags = false;
380 :
381 : /**#@+
382 : * config var settings
383 : */
384 :
385 : /**
386 : * Controls whether variables with the same name overwrite each other.
387 : * @var boolean
388 : */
389 : public $config_overwrite = true;
390 : /**
391 : * Controls whether config values of on/true/yes and off/false/no get converted to boolean.
392 : * @var boolean
393 : */
394 : public $config_booleanize = true;
395 : /**
396 : * Controls whether hidden config sections/vars are read from the file.
397 : * @var boolean
398 : */
399 : public $config_read_hidden = false;
400 :
401 : /**#@-*/
402 :
403 : /**#@+
404 : * resource locking
405 : */
406 :
407 : /**
408 : * locking concurrent compiles
409 : * @var boolean
410 : */
411 : public $compile_locking = true;
412 : /**
413 : * Controls whether cache resources should emply locking mechanism
414 : * @var boolean
415 : */
416 : public $cache_locking = false;
417 : /**
418 : * seconds to wait for acquiring a lock before ignoring the write lock
419 : * @var float
420 : */
421 : public $locking_timeout = 10;
422 :
423 : /**#@-*/
424 :
425 : /**
426 : * global template functions
427 : * @var array
428 : */
429 : public $template_functions = array();
430 : /**
431 : * resource type used if none given
432 : *
433 : * Must be an valid key of $registered_resources.
434 : * @var string
435 : */
436 : public $default_resource_type = 'file';
437 : /**
438 : * caching type
439 : *
440 : * Must be an element of $cache_resource_types.
441 : *
442 : * @var string
443 : */
444 : public $caching_type = 'file';
445 : /**
446 : * internal config properties
447 : * @var array
448 : */
449 : public $properties = array();
450 : /**
451 : * config type
452 : * @var string
453 : */
454 : public $default_config_type = 'file';
455 : /**
456 : * cached template objects
457 : * @var array
458 : */
459 : public $template_objects = array();
460 : /**
461 : * check If-Modified-Since headers
462 : * @var boolean
463 : */
464 : public $cache_modified_check = false;
465 : /**
466 : * registered plugins
467 : * @var array
468 : */
469 : public $registered_plugins = array();
470 : /**
471 : * plugin search order
472 : * @var array
473 : */
474 : public $plugin_search_order = array('function', 'block', 'compiler', 'class');
475 : /**
476 : * registered objects
477 : * @var array
478 : */
479 : public $registered_objects = array();
480 : /**
481 : * registered classes
482 : * @var array
483 : */
484 : public $registered_classes = array();
485 : /**
486 : * registered filters
487 : * @var array
488 : */
489 : public $registered_filters = array();
490 : /**
491 : * registered resources
492 : * @var array
493 : */
494 : public $registered_resources = array();
495 : /**
496 : * resource handler cache
497 : * @var array
498 : */
499 : public $_resource_handlers = array();
500 : /**
501 : * registered cache resources
502 : * @var array
503 : */
504 : public $registered_cache_resources = array();
505 : /**
506 : * cache resource handler cache
507 : * @var array
508 : */
509 : public $_cacheresource_handlers = array();
510 : /**
511 : * autoload filter
512 : * @var array
513 : */
514 : public $autoload_filters = array();
515 : /**
516 : * default modifier
517 : * @var array
518 : */
519 : public $default_modifiers = array();
520 : /**
521 : * autoescape variable output
522 : * @var boolean
523 : */
524 : public $escape_html = false;
525 : /**
526 : * global internal smarty vars
527 : * @var array
528 : */
529 : public static $_smarty_vars = array();
530 : /**
531 : * start time for execution time calculation
532 : * @var int
533 : */
534 : public $start_time = 0;
535 : /**
536 : * default file permissions
537 : * @var int
538 : */
539 : public $_file_perms = 0644;
540 : /**
541 : * default dir permissions
542 : * @var int
543 : */
544 : public $_dir_perms = 0771;
545 : /**
546 : * block tag hierarchy
547 : * @var array
548 : */
549 : public $_tag_stack = array();
550 : /**
551 : * self pointer to Smarty object
552 : * @var Smarty
553 : */
554 : public $smarty;
555 : /**
556 : * required by the compiler for BC
557 : * @var string
558 : */
559 : public $_current_file = null;
560 : /**
561 : * internal flag to enable parser debugging
562 : * @var bool
563 : */
564 : public $_parserdebug = false;
565 : /**
566 : * Saved parameter of merged templates during compilation
567 : *
568 : * @var array
569 : */
570 : public $merged_templates_func = array();
571 : /**#@-*/
572 :
573 : /**
574 : * Initialize new Smarty object
575 : *
576 : */
577 : public function __construct()
578 : {
579 : // selfpointer needed by some other class methods
580 1 : $this->smarty = $this;
581 1 : if (is_callable('mb_internal_encoding')) {
582 1 : mb_internal_encoding(SMARTY_RESOURCE_CHAR_SET);
583 1 : }
584 1 : $this->start_time = microtime(true);
585 : // set default dirs
586 1 : $this->setTemplateDir('.' . DS . 'templates' . DS)
587 1 : ->setCompileDir('.' . DS . 'templates_c' . DS)
588 1 : ->setPluginsDir(SMARTY_PLUGINS_DIR)
589 1 : ->setCacheDir('.' . DS . 'cache' . DS)
590 1 : ->setConfigDir('.' . DS . 'configs' . DS);
591 :
592 1 : $this->debug_tpl = 'file:' . dirname(__FILE__) . '/debug.tpl';
593 1 : if (isset($_SERVER['SCRIPT_NAME'])) {
594 1 : $this->assignGlobal('SCRIPT_NAME', $_SERVER['SCRIPT_NAME']);
595 1 : }
596 1 : }
597 :
598 :
599 : /**
600 : * Class destructor
601 : */
602 : public function __destruct()
603 : {
604 : // intentionally left blank
605 0 : }
606 :
607 : /**
608 : * <<magic>> set selfpointer on cloned object
609 : */
610 : public function __clone()
611 : {
612 0 : $this->smarty = $this;
613 0 : }
614 :
615 :
616 : /**
617 : * <<magic>> Generic getter.
618 : *
619 : * Calls the appropriate getter function.
620 : * Issues an E_USER_NOTICE if no valid getter is found.
621 : *
622 : * @param string $name property name
623 : * @return mixed
624 : */
625 : public function __get($name)
626 : {
627 : $allowed = array(
628 0 : 'template_dir' => 'getTemplateDir',
629 0 : 'config_dir' => 'getConfigDir',
630 0 : 'plugins_dir' => 'getPluginsDir',
631 0 : 'compile_dir' => 'getCompileDir',
632 0 : 'cache_dir' => 'getCacheDir',
633 0 : );
634 :
635 0 : if (isset($allowed[$name])) {
636 0 : return $this->{$allowed[$name]}();
637 : } else {
638 0 : trigger_error('Undefined property: '. get_class($this) .'::$'. $name, E_USER_NOTICE);
639 : }
640 0 : }
641 :
642 : /**
643 : * <<magic>> Generic setter.
644 : *
645 : * Calls the appropriate setter function.
646 : * Issues an E_USER_NOTICE if no valid setter is found.
647 : *
648 : * @param string $name property name
649 : * @param mixed $value parameter passed to setter
650 : */
651 : public function __set($name, $value)
652 : {
653 : $allowed = array(
654 0 : 'template_dir' => 'setTemplateDir',
655 0 : 'config_dir' => 'setConfigDir',
656 0 : 'plugins_dir' => 'setPluginsDir',
657 0 : 'compile_dir' => 'setCompileDir',
658 0 : 'cache_dir' => 'setCacheDir',
659 0 : );
660 :
661 0 : if (isset($allowed[$name])) {
662 0 : $this->{$allowed[$name]}($value);
663 0 : } else {
664 0 : trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE);
665 : }
666 0 : }
667 :
668 : /**
669 : * Check if a template resource exists
670 : *
671 : * @param string $resource_name template name
672 : * @return boolean status
673 : */
674 : public function templateExists($resource_name)
675 : {
676 : // create template object
677 0 : $save = $this->template_objects;
678 0 : $tpl = new $this->template_class($resource_name, $this);
679 : // check if it does exists
680 0 : $result = $tpl->source->exists;
681 0 : $this->template_objects = $save;
682 0 : return $result;
683 : }
684 :
685 : /**
686 : * Returns a single or all global variables
687 : *
688 : * @param object $smarty
689 : * @param string $varname variable name or null
690 : * @return string variable value or or array of variables
691 : */
692 : public function getGlobal($varname = null)
693 : {
694 0 : if (isset($varname)) {
695 0 : if (isset(self::$global_tpl_vars[$varname])) {
696 0 : return self::$global_tpl_vars[$varname]->value;
697 : } else {
698 0 : return '';
699 : }
700 : } else {
701 0 : $_result = array();
702 0 : foreach (self::$global_tpl_vars AS $key => $var) {
703 0 : $_result[$key] = $var->value;
704 0 : }
705 0 : return $_result;
706 : }
707 : }
708 :
709 : /**
710 : * Empty cache folder
711 : *
712 : * @param integer $exp_time expiration time
713 : * @param string $type resource type
714 : * @return integer number of cache files deleted
715 : */
716 : function clearAllCache($exp_time = null, $type = null)
717 : {
718 : // load cache resource and call clearAll
719 0 : $_cache_resource = Smarty_CacheResource::load($this, $type);
720 0 : Smarty_CacheResource::invalidLoadedCache($this);
721 0 : return $_cache_resource->clearAll($this, $exp_time);
722 : }
723 :
724 : /**
725 : * Empty cache for a specific template
726 : *
727 : * @param string $template_name template name
728 : * @param string $cache_id cache id
729 : * @param string $compile_id compile id
730 : * @param integer $exp_time expiration time
731 : * @param string $type resource type
732 : * @return integer number of cache files deleted
733 : */
734 : public function clearCache($template_name, $cache_id = null, $compile_id = null, $exp_time = null, $type = null)
735 : {
736 : // load cache resource and call clear
737 0 : $_cache_resource = Smarty_CacheResource::load($this, $type);
738 0 : Smarty_CacheResource::invalidLoadedCache($this);
739 0 : return $_cache_resource->clear($this, $template_name, $cache_id, $compile_id, $exp_time);
740 : }
741 :
742 : /**
743 : * Loads security class and enables security
744 : *
745 : * @param string|Smarty_Security $security_class if a string is used, it must be class-name
746 : * @return Smarty current Smarty instance for chaining
747 : * @throws SmartyException when an invalid class name is provided
748 : */
749 : public function enableSecurity($security_class = null)
750 : {
751 0 : if ($security_class instanceof Smarty_Security) {
752 0 : $this->security_policy = $security_class;
753 0 : return $this;
754 0 : } elseif (is_object($security_class)) {
755 0 : throw new SmartyException("Class '" . get_class($security_class) . "' must extend Smarty_Security.");
756 : }
757 0 : if ($security_class == null) {
758 0 : $security_class = $this->security_class;
759 0 : }
760 0 : if (!class_exists($security_class)) {
761 0 : throw new SmartyException("Security class '$security_class' is not defined");
762 0 : } elseif ($security_class !== 'Smarty_Security' && !is_subclass_of($security_class, 'Smarty_Security')) {
763 0 : throw new SmartyException("Class '$security_class' must extend Smarty_Security.");
764 : } else {
765 0 : $this->security_policy = new $security_class($this);
766 : }
767 :
768 0 : return $this;
769 : }
770 :
771 : /**
772 : * Disable security
773 : * @return Smarty current Smarty instance for chaining
774 : */
775 : public function disableSecurity()
776 : {
777 0 : $this->security_policy = null;
778 :
779 0 : return $this;
780 : }
781 :
782 : /**
783 : * Set template directory
784 : *
785 : * @param string|array $template_dir directory(s) of template sources
786 : * @return Smarty current Smarty instance for chaining
787 : */
788 : public function setTemplateDir($template_dir)
789 : {
790 1 : $this->template_dir = array();
791 1 : foreach ((array) $template_dir as $k => $v) {
792 1 : $this->template_dir[$k] = rtrim($v, '/\\') . DS;
793 1 : }
794 :
795 1 : $this->joined_template_dir = join(DIRECTORY_SEPARATOR, $this->template_dir);
796 1 : return $this;
797 : }
798 :
799 : /**
800 : * Add template directory(s)
801 : *
802 : * @param string|array $template_dir directory(s) of template sources
803 : * @param string $key of the array element to assign the template dir to
804 : * @return Smarty current Smarty instance for chaining
805 : * @throws SmartyException when the given template directory is not valid
806 : */
807 : public function addTemplateDir($template_dir, $key=null)
808 : {
809 : // make sure we're dealing with an array
810 0 : $this->template_dir = (array) $this->template_dir;
811 :
812 0 : if (is_array($template_dir)) {
813 0 : foreach ($template_dir as $k => $v) {
814 0 : if (is_int($k)) {
815 : // indexes are not merged but appended
816 0 : $this->template_dir[] = rtrim($v, '/\\') . DS;
817 0 : } else {
818 : // string indexes are overridden
819 0 : $this->template_dir[$k] = rtrim($v, '/\\') . DS;
820 : }
821 0 : }
822 0 : } elseif ($key !== null) {
823 : // override directory at specified index
824 0 : $this->template_dir[$key] = rtrim($template_dir, '/\\') . DS;
825 0 : } else {
826 : // append new directory
827 0 : $this->template_dir[] = rtrim($template_dir, '/\\') . DS;
828 : }
829 0 : $this->joined_template_dir = join(DIRECTORY_SEPARATOR, $this->template_dir);
830 0 : return $this;
831 : }
832 :
833 : /**
834 : * Get template directories
835 : *
836 : * @param mixed index of directory to get, null to get all
837 : * @return array|string list of template directories, or directory of $index
838 : */
839 : public function getTemplateDir($index=null)
840 : {
841 0 : if ($index !== null) {
842 0 : return isset($this->template_dir[$index]) ? $this->template_dir[$index] : null;
843 : }
844 :
845 0 : return (array)$this->template_dir;
846 : }
847 :
848 : /**
849 : * Set config directory
850 : *
851 : * @param string|array $template_dir directory(s) of configuration sources
852 : * @return Smarty current Smarty instance for chaining
853 : */
854 : public function setConfigDir($config_dir)
855 : {
856 1 : $this->config_dir = array();
857 1 : foreach ((array) $config_dir as $k => $v) {
858 1 : $this->config_dir[$k] = rtrim($v, '/\\') . DS;
859 1 : }
860 :
861 1 : $this->joined_config_dir = join(DIRECTORY_SEPARATOR, $this->config_dir);
862 1 : return $this;
863 : }
864 :
865 : /**
866 : * Add config directory(s)
867 : *
868 : * @param string|array $config_dir directory(s) of config sources
869 : * @param string key of the array element to assign the config dir to
870 : * @return Smarty current Smarty instance for chaining
871 : */
872 : public function addConfigDir($config_dir, $key=null)
873 : {
874 : // make sure we're dealing with an array
875 0 : $this->config_dir = (array) $this->config_dir;
876 :
877 0 : if (is_array($config_dir)) {
878 0 : foreach ($config_dir as $k => $v) {
879 0 : if (is_int($k)) {
880 : // indexes are not merged but appended
881 0 : $this->config_dir[] = rtrim($v, '/\\') . DS;
882 0 : } else {
883 : // string indexes are overridden
884 0 : $this->config_dir[$k] = rtrim($v, '/\\') . DS;
885 : }
886 0 : }
887 0 : } elseif( $key !== null ) {
888 : // override directory at specified index
889 0 : $this->config_dir[$key] = rtrim($config_dir, '/\\') . DS;
890 0 : } else {
891 : // append new directory
892 0 : $this->config_dir[] = rtrim($config_dir, '/\\') . DS;
893 : }
894 :
895 0 : $this->joined_config_dir = join(DIRECTORY_SEPARATOR, $this->config_dir);
896 0 : return $this;
897 : }
898 :
899 : /**
900 : * Get config directory
901 : *
902 : * @param mixed index of directory to get, null to get all
903 : * @return array|string configuration directory
904 : */
905 : public function getConfigDir($index=null)
906 : {
907 0 : if ($index !== null) {
908 0 : return isset($this->config_dir[$index]) ? $this->config_dir[$index] : null;
909 : }
910 :
911 0 : return (array)$this->config_dir;
912 : }
913 :
914 : /**
915 : * Set plugins directory
916 : *
917 : * @param string|array $plugins_dir directory(s) of plugins
918 : * @return Smarty current Smarty instance for chaining
919 : */
920 : public function setPluginsDir($plugins_dir)
921 : {
922 1 : $this->plugins_dir = array();
923 1 : foreach ((array)$plugins_dir as $k => $v) {
924 1 : $this->plugins_dir[$k] = rtrim($v, '/\\') . DS;
925 1 : }
926 :
927 1 : return $this;
928 : }
929 :
930 : /**
931 : * Adds directory of plugin files
932 : *
933 : * @param object $smarty
934 : * @param string $ |array $ plugins folder
935 : * @return Smarty current Smarty instance for chaining
936 : */
937 : public function addPluginsDir($plugins_dir)
938 : {
939 : // make sure we're dealing with an array
940 0 : $this->plugins_dir = (array) $this->plugins_dir;
941 :
942 0 : if (is_array($plugins_dir)) {
943 0 : foreach ($plugins_dir as $k => $v) {
944 0 : if (is_int($k)) {
945 : // indexes are not merged but appended
946 0 : $this->plugins_dir[] = rtrim($v, '/\\') . DS;
947 0 : } else {
948 : // string indexes are overridden
949 0 : $this->plugins_dir[$k] = rtrim($v, '/\\') . DS;
950 : }
951 0 : }
952 0 : } else {
953 : // append new directory
954 0 : $this->plugins_dir[] = rtrim($plugins_dir, '/\\') . DS;
955 : }
956 :
957 0 : $this->plugins_dir = array_unique($this->plugins_dir);
958 0 : return $this;
959 : }
960 :
961 : /**
962 : * Get plugin directories
963 : *
964 : * @return array list of plugin directories
965 : */
966 : public function getPluginsDir()
967 : {
968 0 : return (array)$this->plugins_dir;
969 : }
970 :
971 : /**
972 : * Set compile directory
973 : *
974 : * @param string $compile_dir directory to store compiled templates in
975 : * @return Smarty current Smarty instance for chaining
976 : */
977 : public function setCompileDir($compile_dir)
978 : {
979 1 : $this->compile_dir = rtrim($compile_dir, '/\\') . DS;
980 1 : if (!isset(Smarty::$_muted_directories[$this->compile_dir])) {
981 1 : Smarty::$_muted_directories[$this->compile_dir] = null;
982 1 : }
983 1 : return $this;
984 : }
985 :
986 : /**
987 : * Get compiled directory
988 : *
989 : * @return string path to compiled templates
990 : */
991 : public function getCompileDir()
992 : {
993 0 : return $this->compile_dir;
994 : }
995 :
996 : /**
997 : * Set cache directory
998 : *
999 : * @param string $cache_dir directory to store cached templates in
1000 : * @return Smarty current Smarty instance for chaining
1001 : */
1002 : public function setCacheDir($cache_dir)
1003 : {
1004 1 : $this->cache_dir = rtrim($cache_dir, '/\\') . DS;
1005 1 : if (!isset(Smarty::$_muted_directories[$this->cache_dir])) {
1006 1 : Smarty::$_muted_directories[$this->cache_dir] = null;
1007 1 : }
1008 1 : return $this;
1009 : }
1010 :
1011 : /**
1012 : * Get cache directory
1013 : *
1014 : * @return string path of cache directory
1015 : */
1016 : public function getCacheDir()
1017 : {
1018 0 : return $this->cache_dir;
1019 : }
1020 :
1021 : /**
1022 : * Set default modifiers
1023 : *
1024 : * @param array|string $modifiers modifier or list of modifiers to set
1025 : * @return Smarty current Smarty instance for chaining
1026 : */
1027 : public function setDefaultModifiers($modifiers)
1028 : {
1029 0 : $this->default_modifiers = (array) $modifiers;
1030 0 : return $this;
1031 : }
1032 :
1033 : /**
1034 : * Add default modifiers
1035 : *
1036 : * @param array|string $modifiers modifier or list of modifiers to add
1037 : * @return Smarty current Smarty instance for chaining
1038 : */
1039 : public function addDefaultModifiers($modifiers)
1040 : {
1041 0 : if (is_array($modifiers)) {
1042 0 : $this->default_modifiers = array_merge($this->default_modifiers, $modifiers);
1043 0 : } else {
1044 0 : $this->default_modifiers[] = $modifiers;
1045 : }
1046 :
1047 0 : return $this;
1048 : }
1049 :
1050 : /**
1051 : * Get default modifiers
1052 : *
1053 : * @return array list of default modifiers
1054 : */
1055 : public function getDefaultModifiers()
1056 : {
1057 0 : return $this->default_modifiers;
1058 : }
1059 :
1060 :
1061 : /**
1062 : * Set autoload filters
1063 : *
1064 : * @param array $filters filters to load automatically
1065 : * @param string $type "pre", "output", … specify the filter type to set. Defaults to none treating $filters' keys as the appropriate types
1066 : * @return Smarty current Smarty instance for chaining
1067 : */
1068 : public function setAutoloadFilters($filters, $type=null)
1069 : {
1070 0 : if ($type !== null) {
1071 0 : $this->autoload_filters[$type] = (array) $filters;
1072 0 : } else {
1073 0 : $this->autoload_filters = (array) $filters;
1074 : }
1075 :
1076 0 : return $this;
1077 : }
1078 :
1079 : /**
1080 : * Add autoload filters
1081 : *
1082 : * @param array $filters filters to load automatically
1083 : * @param string $type "pre", "output", … specify the filter type to set. Defaults to none treating $filters' keys as the appropriate types
1084 : * @return Smarty current Smarty instance for chaining
1085 : */
1086 : public function addAutoloadFilters($filters, $type=null)
1087 : {
1088 0 : if ($type !== null) {
1089 0 : if (!empty($this->autoload_filters[$type])) {
1090 0 : $this->autoload_filters[$type] = array_merge($this->autoload_filters[$type], (array) $filters);
1091 0 : } else {
1092 0 : $this->autoload_filters[$type] = (array) $filters;
1093 : }
1094 0 : } else {
1095 0 : foreach ((array) $filters as $key => $value) {
1096 0 : if (!empty($this->autoload_filters[$key])) {
1097 0 : $this->autoload_filters[$key] = array_merge($this->autoload_filters[$key], (array) $value);
1098 0 : } else {
1099 0 : $this->autoload_filters[$key] = (array) $value;
1100 : }
1101 0 : }
1102 : }
1103 :
1104 0 : return $this;
1105 : }
1106 :
1107 : /**
1108 : * Get autoload filters
1109 : *
1110 : * @param string $type type of filter to get autoloads for. Defaults to all autoload filters
1111 : * @return array array( 'type1' => array( 'filter1', 'filter2', … ) ) or array( 'filter1', 'filter2', …) if $type was specified
1112 : */
1113 : public function getAutoloadFilters($type=null)
1114 : {
1115 0 : if ($type !== null) {
1116 0 : return isset($this->autoload_filters[$type]) ? $this->autoload_filters[$type] : array();
1117 : }
1118 :
1119 0 : return $this->autoload_filters;
1120 : }
1121 :
1122 : /**
1123 : * return name of debugging template
1124 : *
1125 : * @return string
1126 : */
1127 : public function getDebugTemplate()
1128 : {
1129 0 : return $this->debug_tpl;
1130 : }
1131 :
1132 : /**
1133 : * set the debug template
1134 : *
1135 : * @param string $tpl_name
1136 : * @return Smarty current Smarty instance for chaining
1137 : * @throws SmartyException if file is not readable
1138 : */
1139 : public function setDebugTemplate($tpl_name)
1140 : {
1141 0 : if (!is_readable($tpl_name)) {
1142 0 : throw new SmartyException("Unknown file '{$tpl_name}'");
1143 : }
1144 0 : $this->debug_tpl = $tpl_name;
1145 :
1146 0 : return $this;
1147 : }
1148 :
1149 : /**
1150 : * creates a template object
1151 : *
1152 : * @param string $template the resource handle of the template file
1153 : * @param mixed $cache_id cache id to be used with this template
1154 : * @param mixed $compile_id compile id to be used with this template
1155 : * @param object $parent next higher level of Smarty variables
1156 : * @param boolean $do_clone flag is Smarty object shall be cloned
1157 : * @return object template object
1158 : */
1159 : public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null, $do_clone = true)
1160 : {
1161 0 : if (!empty($cache_id) && (is_object($cache_id) || is_array($cache_id))) {
1162 0 : $parent = $cache_id;
1163 0 : $cache_id = null;
1164 0 : }
1165 0 : if (!empty($parent) && is_array($parent)) {
1166 0 : $data = $parent;
1167 0 : $parent = null;
1168 0 : } else {
1169 0 : $data = null;
1170 : }
1171 : // default to cache_id and compile_id of Smarty object
1172 0 : $cache_id = $cache_id === null ? $this->cache_id : $cache_id;
1173 0 : $compile_id = $compile_id === null ? $this->compile_id : $compile_id;
1174 : // already in template cache?
1175 0 : $unique_template_name = Smarty_Resource::getUniqueTemplateName($this, $template);
1176 0 : $_templateId = sha1($unique_template_name . $cache_id . $compile_id);
1177 0 : if ($do_clone) {
1178 0 : if (isset($this->template_objects[$_templateId])) {
1179 : // return cached template object
1180 0 : $tpl = clone $this->template_objects[$_templateId];
1181 0 : $tpl->smarty = clone $tpl->smarty;
1182 0 : $tpl->parent = $parent;
1183 0 : } else {
1184 0 : $tpl = new $this->template_class($template, clone $this, $parent, $cache_id, $compile_id);
1185 : }
1186 0 : } else {
1187 0 : if (isset($this->template_objects[$_templateId])) {
1188 : // return cached template object
1189 0 : $tpl = $this->template_objects[$_templateId];
1190 0 : } else {
1191 0 : $tpl = new $this->template_class($template, $this, $parent, $cache_id, $compile_id);
1192 : }
1193 : }
1194 : // fill data if present
1195 0 : if (!empty($data) && is_array($data)) {
1196 : // set up variable values
1197 0 : foreach ($data as $_key => $_val) {
1198 0 : $tpl->tpl_vars[$_key] = new Smarty_variable($_val);
1199 0 : }
1200 0 : }
1201 0 : return $tpl;
1202 : }
1203 :
1204 :
1205 : /**
1206 : * Takes unknown classes and loads plugin files for them
1207 : * class name format: Smarty_PluginType_PluginName
1208 : * plugin filename format: plugintype.pluginname.php
1209 : *
1210 : * @param string $plugin_name class plugin name to load
1211 : * @param bool $check check if already loaded
1212 : * @return string |boolean filepath of loaded file or false
1213 : */
1214 : public function loadPlugin($plugin_name, $check = true)
1215 : {
1216 : // if function or class exists, exit silently (already loaded)
1217 0 : if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false))) {
1218 0 : return true;
1219 : }
1220 : // Plugin name is expected to be: Smarty_[Type]_[Name]
1221 0 : $_name_parts = explode('_', $plugin_name, 3);
1222 : // class name must have three parts to be valid plugin
1223 : // count($_name_parts) < 3 === !isset($_name_parts[2])
1224 0 : if (!isset($_name_parts[2]) || strtolower($_name_parts[0]) !== 'smarty') {
1225 0 : throw new SmartyException("plugin {$plugin_name} is not a valid name format");
1226 : return false;
1227 : }
1228 : // if type is "internal", get plugin from sysplugins
1229 0 : if (strtolower($_name_parts[1]) == 'internal') {
1230 0 : $file = SMARTY_SYSPLUGINS_DIR . strtolower($plugin_name) . '.php';
1231 0 : if (file_exists($file)) {
1232 0 : require_once($file);
1233 0 : return $file;
1234 : } else {
1235 0 : return false;
1236 : }
1237 : }
1238 : // plugin filename is expected to be: [type].[name].php
1239 0 : $_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php";
1240 :
1241 : // loop through plugin dirs and find the plugin
1242 0 : foreach($this->getPluginsDir() as $_plugin_dir) {
1243 : $names = array(
1244 0 : $_plugin_dir . $_plugin_filename,
1245 0 : $_plugin_dir . strtolower($_plugin_filename),
1246 0 : );
1247 0 : foreach ($names as $file) {
1248 0 : if (file_exists($file)) {
1249 0 : require_once($file);
1250 0 : return $file;
1251 : }
1252 0 : if ($this->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_plugin_dir)) {
1253 : // try PHP include_path
1254 0 : if (($file = Smarty_Internal_Get_Include_Path::getIncludePath($file)) !== false) {
1255 0 : require_once($file);
1256 0 : return $file;
1257 : }
1258 0 : }
1259 0 : }
1260 0 : }
1261 : // no plugin loaded
1262 0 : return false;
1263 : }
1264 :
1265 : /**
1266 : * Compile all template files
1267 : *
1268 : * @param string $extension file extension
1269 : * @param bool $force_compile force all to recompile
1270 : * @param int $time_limit
1271 : * @param int $max_errors
1272 : * @return integer number of template files recompiled
1273 : */
1274 : public function compileAllTemplates($extention = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null)
1275 : {
1276 0 : return Smarty_Internal_Utility::compileAllTemplates($extention, $force_compile, $time_limit, $max_errors, $this);
1277 : }
1278 :
1279 : /**
1280 : * Compile all config files
1281 : *
1282 : * @param string $extension file extension
1283 : * @param bool $force_compile force all to recompile
1284 : * @param int $time_limit
1285 : * @param int $max_errors
1286 : * @return integer number of template files recompiled
1287 : */
1288 : public function compileAllConfig($extention = '.conf', $force_compile = false, $time_limit = 0, $max_errors = null)
1289 : {
1290 0 : return Smarty_Internal_Utility::compileAllConfig($extention, $force_compile, $time_limit, $max_errors, $this);
1291 : }
1292 :
1293 : /**
1294 : * Delete compiled template file
1295 : *
1296 : * @param string $resource_name template name
1297 : * @param string $compile_id compile id
1298 : * @param integer $exp_time expiration time
1299 : * @return integer number of template files deleted
1300 : */
1301 : public function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
1302 : {
1303 0 : return Smarty_Internal_Utility::clearCompiledTemplate($resource_name, $compile_id, $exp_time, $this);
1304 : }
1305 :
1306 :
1307 : /**
1308 : * Return array of tag/attributes of all tags used by an template
1309 : *
1310 : * @param object $templae template object
1311 : * @return array of tag/attributes
1312 : */
1313 : public function getTags(Smarty_Internal_Template $template)
1314 : {
1315 0 : return Smarty_Internal_Utility::getTags($template);
1316 : }
1317 :
1318 : /**
1319 : * Run installation test
1320 : *
1321 : * @param array $errors Array to write errors into, rather than outputting them
1322 : * @return boolean true if setup is fine, false if something is wrong
1323 : */
1324 : public function testInstall(&$errors=null)
1325 : {
1326 0 : return Smarty_Internal_Utility::testInstall($this, $errors);
1327 : }
1328 :
1329 : /**
1330 : * Error Handler to mute expected messages
1331 : *
1332 : * @link http://php.net/set_error_handler
1333 : * @param integer $errno Error level
1334 : * @return boolean
1335 : */
1336 : public static function mutingErrorHandler($errno, $errstr, $errfile, $errline, $errcontext)
1337 : {
1338 0 : $_is_muted_directory = false;
1339 :
1340 : // add the SMARTY_DIR to the list of muted directories
1341 0 : if (!isset(Smarty::$_muted_directories[SMARTY_DIR])) {
1342 0 : $smarty_dir = realpath(SMARTY_DIR);
1343 0 : Smarty::$_muted_directories[SMARTY_DIR] = array(
1344 0 : 'file' => $smarty_dir,
1345 0 : 'length' => strlen($smarty_dir),
1346 : );
1347 0 : }
1348 :
1349 : // walk the muted directories and test against $errfile
1350 0 : foreach (Smarty::$_muted_directories as $key => &$dir) {
1351 0 : if (!$dir) {
1352 : // resolve directory and length for speedy comparisons
1353 0 : $file = realpath($key);
1354 : $dir = array(
1355 0 : 'file' => $file,
1356 0 : 'length' => strlen($file),
1357 0 : );
1358 0 : }
1359 0 : if (!strncmp($errfile, $dir['file'], $dir['length'])) {
1360 0 : $_is_muted_directory = true;
1361 0 : break;
1362 : }
1363 0 : }
1364 :
1365 : // pass to next error handler if this error did not occur inside SMARTY_DIR
1366 : // or the error was within smarty but masked to be ignored
1367 0 : if (!$_is_muted_directory || ($errno && $errno & error_reporting())) {
1368 0 : if (Smarty::$_previous_error_handler) {
1369 0 : return call_user_func(Smarty::$_previous_error_handler, $errno, $errstr, $errfile, $errline, $errcontext);
1370 : } else {
1371 0 : return false;
1372 : }
1373 : }
1374 0 : }
1375 :
1376 : /**
1377 : * Enable error handler to mute expected messages
1378 : *
1379 : * @return void
1380 : */
1381 : public static function muteExpectedErrors()
1382 : {
1383 : /*
1384 : error muting is done because some people implemented custom error_handlers using
1385 : http://php.net/set_error_handler and for some reason did not understand the following paragraph:
1386 :
1387 : It is important to remember that the standard PHP error handler is completely bypassed for the
1388 : error types specified by error_types unless the callback function returns FALSE.
1389 : error_reporting() settings will have no effect and your error handler will be called regardless -
1390 : however you are still able to read the current value of error_reporting and act appropriately.
1391 : Of particular note is that this value will be 0 if the statement that caused the error was
1392 : prepended by the @ error-control operator.
1393 :
1394 : Smarty deliberately uses @filemtime() over file_exists() and filemtime() in some places. Reasons include
1395 : - @filemtime() is almost twice as fast as using an additional file_exists()
1396 : - between file_exists() and filemtime() a possible race condition is opened,
1397 : which does not exist using the simple @filemtime() approach.
1398 : */
1399 0 : $error_handler = array('Smarty', 'mutingErrorHandler');
1400 0 : $previous = set_error_handler($error_handler);
1401 :
1402 : // avoid dead loops
1403 0 : if ($previous !== $error_handler) {
1404 0 : Smarty::$_previous_error_handler = $previous;
1405 0 : }
1406 0 : }
1407 :
1408 : /**
1409 : * Disable error handler muting expected messages
1410 : *
1411 : * @return void
1412 : */
1413 : public static function unmuteExpectedErrors()
1414 : {
1415 0 : restore_error_handler();
1416 0 : }
1417 : }
1418 :
1419 : /**
1420 : * Smarty exception class
1421 : * @package Smarty
1422 : */
1423 : class SmartyException extends Exception {
1424 : }
1425 :
1426 : /**
1427 : * Smarty compiler exception class
1428 : * @package Smarty
1429 : */
1430 : class SmartyCompilerException extends SmartyException {
1431 : }
1432 :
1433 : /**
1434 : * Autoloader
1435 : */
1436 : function smartyAutoload($class)
1437 : {
1438 0 : $_class = strtolower($class);
1439 : $_classes = array(
1440 0 : 'smarty_config_source' => true,
1441 0 : 'smarty_config_compiled' => true,
1442 0 : 'smarty_security' => true,
1443 0 : 'smarty_cacheresource' => true,
1444 0 : 'smarty_cacheresource_custom' => true,
1445 0 : 'smarty_cacheresource_keyvaluestore' => true,
1446 0 : 'smarty_resource' => true,
1447 0 : 'smarty_resource_custom' => true,
1448 0 : 'smarty_resource_uncompiled' => true,
1449 0 : 'smarty_resource_recompiled' => true,
1450 0 : );
1451 :
1452 0 : if (!strncmp($_class, 'smarty_internal_', 16) || isset($_classes[$_class])) {
1453 0 : include SMARTY_SYSPLUGINS_DIR . $_class . '.php';
1454 0 : }
1455 0 : }
1456 :
1457 : ?>
|