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