/[cvs]/nfo/php/libs/net.php.smarty/Smarty.class.php
ViewVC logotype

Contents of /nfo/php/libs/net.php.smarty/Smarty.class.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Wed Oct 9 00:45:29 2002 UTC (21 years, 9 months ago) by cvsjoko
Branch: MAIN
no message

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed