/[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.2 - (show annotations)
Thu Dec 19 16:40:20 2002 UTC (21 years, 6 months ago) by joko
Branch: MAIN
Changes since 1.1: +41 -16 lines
+ updated to version 2.3.1

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.1
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.1'; // 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, $template_timestamp);
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, $template_timestamp);
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, $template_timestamp)
965 {
966 // we save everything into $compile_dir
967 $this->_write_file($compile_path, $template_compiled, true);
968 touch($compile_path, $template_timestamp);
969 return true;
970 }
971
972 /*======================================================================*\
973 Function: _parse_file_path
974 Purpose: parse out the type and name from the template resource
975 \*======================================================================*/
976 function _parse_file_path($file_base_path, $file_path, &$resource_type, &$resource_name)
977 {
978 // split tpl_path by the first colon
979 $_file_path_parts = explode(':', $file_path, 2);
980
981 if (count($_file_path_parts) == 1) {
982 // no resource type, treat as type "file"
983 $resource_type = 'file';
984 $resource_name = $_file_path_parts[0];
985 } else {
986 $resource_type = $_file_path_parts[0];
987 $resource_name = $_file_path_parts[1];
988 if ($resource_type != 'file') {
989 $this->_load_resource_plugin($resource_type);
990 }
991 }
992
993 if ($resource_type == 'file') {
994 if (!preg_match("/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/", $resource_name)) {
995 // relative pathname to $file_base_path
996 // use the first directory where the file is found
997 foreach ((array)$file_base_path as $_curr_path) {
998 $_fullpath = $_curr_path . DIR_SEP . $resource_name;
999 if (@is_file($_fullpath)) {
1000 $resource_name = $_fullpath;
1001 return true;
1002 }
1003 // didn't find the file, try include_path
1004 if($this->_get_include_path($_fullpath, $_include_path)) {
1005 $resource_name = $_include_path;
1006 return true;
1007 }
1008 }
1009 return false;
1010 }
1011 }
1012
1013 // resource type != file
1014 return true;
1015 }
1016
1017
1018 /*======================================================================*\
1019 Function: _fetch_template_info()
1020 Purpose: fetch the template info. Gets timestamp, and source
1021 if get_source is true
1022 \*======================================================================*/
1023 function _fetch_template_info($tpl_path, &$template_source, &$template_timestamp, $get_source = true, $quiet = false)
1024 {
1025 $_return = false;
1026 if ($this->_parse_file_path($this->template_dir, $tpl_path, $resource_type, $resource_name)) {
1027 switch ($resource_type) {
1028 case 'file':
1029 if ($get_source) {
1030 $template_source = $this->_read_file($resource_name);
1031 }
1032 $template_timestamp = filemtime($resource_name);
1033 $_return = true;
1034 break;
1035
1036 default:
1037 // call resource functions to fetch the template source and timestamp
1038 if ($get_source) {
1039 $resource_func = $this->_plugins['resource'][$resource_type][0][0];
1040 $_source_return = $resource_func($resource_name, $template_source, $this);
1041 } else {
1042 $_source_return = true;
1043 }
1044 $resource_func = $this->_plugins['resource'][$resource_type][0][1];
1045 $_timestamp_return = $resource_func($resource_name, $template_timestamp, $this);
1046 $_return = $_source_return && $_timestamp_return;
1047 break;
1048 }
1049 }
1050
1051 if (!$_return) {
1052 // see if we can get a template with the default template handler
1053 if (!empty($this->default_template_handler_func)) {
1054 if (!function_exists($this->default_template_handler_func)) {
1055 $this->trigger_error("default template handler function \"$this->default_template_handler_func\" doesn't exist.");
1056 $_return = false;
1057 }
1058 $funcname = $this->default_template_handler_func;
1059 $_return = $funcname($resource_type, $resource_name, $template_source, $template_timestamp, $this);
1060 }
1061 }
1062
1063 if (!$_return) {
1064 if (!$quiet) {
1065 $this->trigger_error("unable to read template resource: \"$tpl_path\"");
1066 }
1067 } else if ($_return && $this->security && !$this->_is_secure($resource_type, $resource_name)) {
1068 if (!$quiet)
1069 $this->trigger_error("(secure mode) accessing \"$tpl_path\" is not allowed");
1070 $template_source = null;
1071 $template_timestamp = null;
1072 return false;
1073 }
1074
1075 return $_return;
1076 }
1077
1078
1079 /*======================================================================*\
1080 Function: _compile_template()
1081 Purpose: called to compile the templates
1082 \*======================================================================*/
1083 function _compile_template($tpl_file, $template_source, &$template_compiled)
1084 {
1085 require_once SMARTY_DIR.$this->compiler_class . '.class.php';
1086
1087 $smarty_compiler = new $this->compiler_class;
1088
1089 $smarty_compiler->template_dir = $this->template_dir;
1090 $smarty_compiler->compile_dir = $this->compile_dir;
1091 $smarty_compiler->plugins_dir = $this->plugins_dir;
1092 $smarty_compiler->config_dir = $this->config_dir;
1093 $smarty_compiler->force_compile = $this->force_compile;
1094 $smarty_compiler->caching = $this->caching;
1095 $smarty_compiler->php_handling = $this->php_handling;
1096 $smarty_compiler->left_delimiter = $this->left_delimiter;
1097 $smarty_compiler->right_delimiter = $this->right_delimiter;
1098 $smarty_compiler->_version = $this->_version;
1099 $smarty_compiler->security = $this->security;
1100 $smarty_compiler->secure_dir = $this->secure_dir;
1101 $smarty_compiler->security_settings = $this->security_settings;
1102 $smarty_compiler->trusted_dir = $this->trusted_dir;
1103 $smarty_compiler->_plugins = &$this->_plugins;
1104 $smarty_compiler->_tpl_vars = &$this->_tpl_vars;
1105 $smarty_compiler->default_modifiers = $this->default_modifiers;
1106
1107 if ($smarty_compiler->_compile_file($tpl_file, $template_source, $template_compiled))
1108 return true;
1109 else {
1110 $this->trigger_error($smarty_compiler->_error_msg);
1111 return false;
1112 }
1113 }
1114
1115 /*======================================================================*\
1116 Function: _smarty_include()
1117 Purpose: called for included templates
1118 \*======================================================================*/
1119 function _smarty_include($_smarty_include_tpl_file, $_smarty_include_vars)
1120 {
1121 if ($this->debugging) {
1122 $debug_start_time = $this->_get_microtime();
1123 $this->_smarty_debug_info[] = array('type' => 'template',
1124 'filename' => $_smarty_include_tpl_file,
1125 'depth' => ++$this->_inclusion_depth);
1126 $included_tpls_idx = count($this->_smarty_debug_info) - 1;
1127 }
1128
1129 $this->_tpl_vars = array_merge($this->_tpl_vars, $_smarty_include_vars);
1130 extract($this->_tpl_vars);
1131
1132 array_unshift($this->_config, $this->_config[0]);
1133 $_smarty_compile_path = $this->_get_compile_path($_smarty_include_tpl_file);
1134
1135 if ($this->_process_template($_smarty_include_tpl_file, $_smarty_compile_path)) {
1136 include($_smarty_compile_path);
1137 }
1138
1139 array_shift($this->_config);
1140 $this->_inclusion_depth--;
1141
1142 if ($this->debugging) {
1143 // capture time for debugging info
1144 $this->_smarty_debug_info[$included_tpls_idx]['exec_time'] = $this->_get_microtime() - $debug_start_time;
1145 }
1146
1147 if ($this->caching) {
1148 $this->_cache_info['template'][] = $_smarty_include_tpl_file;
1149 }
1150 }
1151
1152 /*======================================================================*\
1153 Function: _smarty_include_php()
1154 Purpose: called for included templates
1155 \*======================================================================*/
1156 function _smarty_include_php($_smarty_include_php_file, $_smarty_assign, $_smarty_once)
1157 {
1158 $this->_get_php_resource($_smarty_include_php_file, $_smarty_resource_type,
1159 $_smarty_php_resource);
1160
1161 if (!empty($_smarty_assign)) {
1162 ob_start();
1163 if ($_smarty_resource_type == 'file') {
1164 if($_smarty_once) {
1165 include_once($_smarty_php_resource);
1166 } else {
1167 include($_smarty_php_resource);
1168 }
1169 } else {
1170 eval($_smarty_php_resource);
1171 }
1172 $this->assign($_smarty_assign, ob_get_contents());
1173 ob_end_clean();
1174 } else {
1175 if ($_smarty_resource_type == 'file') {
1176 if($_smarty_once) {
1177 include_once($_smarty_php_resource);
1178 } else {
1179 include($_smarty_php_resource);
1180 }
1181 } else {
1182 eval($_smarty_php_resource);
1183 }
1184 }
1185 }
1186
1187 /*======================================================================*\
1188 Function: _config_load
1189 Purpose: load configuration values
1190 \*======================================================================*/
1191 function _config_load($file, $section, $scope)
1192 {
1193 if(@is_dir($this->config_dir)) {
1194 $_config_dir = $this->config_dir;
1195 } else {
1196 // config_dir not found, try include_path
1197 $this->_get_include_path($this->config_dir,$_config_dir);
1198 }
1199
1200 if ($this->_conf_obj === null) {
1201 /* Prepare the configuration object. */
1202 if (!class_exists('Config_File'))
1203 require_once SMARTY_DIR.'Config_File.class.php';
1204 $this->_conf_obj = new Config_File($_config_dir);
1205 $this->_conf_obj->read_hidden = false;
1206 } else {
1207 $this->_conf_obj->set_path($_config_dir);
1208 }
1209
1210 if ($this->debugging) {
1211 $debug_start_time = $this->_get_microtime();
1212 }
1213
1214 if ($this->caching) {
1215 $this->_cache_info['config'][] = $file;
1216 }
1217
1218 if (!isset($this->_config[0]['files'][$file])) {
1219 $this->_config[0]['vars'] = array_merge($this->_config[0]['vars'], $this->_conf_obj->get($file));
1220 $this->_config[0]['files'][$file] = true;
1221 }
1222 if ($scope == 'parent') {
1223 if (count($this->_config) > 0 && !isset($this->_config[1]['files'][$file])) {
1224 $this->_config[1]['vars'] = array_merge($this->_config[1]['vars'], $this->_conf_obj->get($file));
1225 $this->_config[1]['files'][$file] = true;
1226 }
1227 } else if ($scope == 'global')
1228 for ($i = 1, $for_max = count($this->_config); $i < $for_max; $i++) {
1229 if (!isset($this->_config[$i]['files'][$file])) {
1230 $this->_config[$i]['vars'] = array_merge($this->_config[$i]['vars'], $this->_conf_obj->get($file));
1231 $this->_config[$i]['files'][$file] = true;
1232 }
1233 }
1234
1235 if (!empty($section)) {
1236 $this->_config[0]['vars'] = array_merge($this->_config[0]['vars'], $this->_conf_obj->get($file, $section));
1237 if ($scope == 'parent') {
1238 if (count($this->_config) > 0)
1239 $this->_config[1]['vars'] = array_merge($this->_config[1]['vars'], $this->_conf_obj->get($file, $section));
1240 } else if ($scope == 'global')
1241 for ($i = 1, $for_max = count($this->_config); $i < $for_max; $i++)
1242 $this->_config[$i]['vars'] = array_merge($this->_config[$i]['vars'], $this->_conf_obj->get($file, $section));
1243 }
1244
1245 if ($this->debugging) {
1246 $debug_start_time = $this->_get_microtime();
1247 $this->_smarty_debug_info[] = array('type' => 'config',
1248 'filename' => $file.' ['.$section.'] '.$scope,
1249 'depth' => $this->_inclusion_depth,
1250 'exec_time' => $this->_get_microtime() - $debug_start_time);
1251 }
1252 }
1253
1254
1255 /*======================================================================*\
1256 Function: _process_cached_inserts
1257 Purpose: Replace cached inserts with the actual results
1258 \*======================================================================*/
1259 function _process_cached_inserts($results)
1260 {
1261 preg_match_all('!'.$this->_smarty_md5.'{insert_cache (.*)}'.$this->_smarty_md5.'!Uis',
1262 $results, $match);
1263 list($cached_inserts, $insert_args) = $match;
1264
1265 for ($i = 0, $for_max = count($cached_inserts); $i < $for_max; $i++) {
1266 if ($this->debugging) {
1267 $debug_start_time = $this->_get_microtime();
1268 }
1269
1270 $args = unserialize($insert_args[$i]);
1271
1272 $name = $args['name'];
1273 unset($args['name']);
1274
1275 if (isset($args['script'])) {
1276 if (!$this->_get_php_resource($this->_dequote($args['script']), $resource_type, $php_resource)) {
1277 return false;
1278 }
1279
1280 if ($resource_type == 'file') {
1281 include_once($php_resource);
1282 } else {
1283 eval($php_resource);
1284 }
1285 unset($args['script']);
1286 }
1287
1288 $function_name = $this->_plugins['insert'][$name][0];
1289 $replace = $function_name($args, $this);
1290
1291 $results = str_replace($cached_inserts[$i], $replace, $results);
1292 if ($this->debugging) {
1293 $this->_smarty_debug_info[] = array('type' => 'insert',
1294 'filename' => 'insert_'.$name,
1295 'depth' => $this->_inclusion_depth,
1296 'exec_time' => $this->_get_microtime() - $debug_start_time);
1297 }
1298 }
1299
1300 return $results;
1301 }
1302
1303
1304 /*======================================================================*\
1305 Function: _run_insert_handler
1306 Purpose: Handle insert tags
1307 \*======================================================================*/
1308 function _run_insert_handler($args)
1309 {
1310 if ($this->debugging) {
1311 $debug_start_time = $this->_get_microtime();
1312 }
1313
1314 if ($this->caching) {
1315 $arg_string = serialize($args);
1316 $name = $args['name'];
1317 if (!isset($this->_cache_info['insert_tags'][$name])) {
1318 $this->_cache_info['insert_tags'][$name] = array('insert',
1319 $name,
1320 $this->_plugins['insert'][$name][1],
1321 $this->_plugins['insert'][$name][2],
1322 !empty($args['script']) ? true : false);
1323 }
1324 return $this->_smarty_md5."{insert_cache $arg_string}".$this->_smarty_md5;
1325 } else {
1326 if (isset($args['script'])) {
1327 if (!$this->_get_php_resource($this->_dequote($args['script']), $resource_type, $php_resource)) {
1328 return false;
1329 }
1330
1331 if ($resource_type == 'file') {
1332 include_once($php_resource);
1333 } else {
1334 eval($php_resource);
1335 }
1336 unset($args['script']);
1337 }
1338
1339 $function_name = $this->_plugins['insert'][$args['name']][0];
1340 $content = $function_name($args, $this);
1341 if ($this->debugging) {
1342 $this->_smarty_debug_info[] = array('type' => 'insert',
1343 'filename' => 'insert_'.$args['name'],
1344 'depth' => $this->_inclusion_depth,
1345 'exec_time' => $this->_get_microtime() - $debug_start_time);
1346 }
1347
1348 if (!empty($args["assign"])) {
1349 $this->assign($args["assign"], $content);
1350 } else {
1351 return $content;
1352 }
1353 }
1354 }
1355
1356
1357 /*======================================================================*\
1358 Function: _run_mod_handler
1359 Purpose: Handle modifiers
1360 \*======================================================================*/
1361 function _run_mod_handler()
1362 {
1363 $args = func_get_args();
1364 list($modifier_name, $map_array) = array_splice($args, 0, 2);
1365 list($func_name, $tpl_file, $tpl_line) =
1366 $this->_plugins['modifier'][$modifier_name];
1367 $var = $args[0];
1368
1369 if ($map_array && is_array($var)) {
1370 foreach ($var as $key => $val) {
1371 $args[0] = $val;
1372 $var[$key] = call_user_func_array($func_name, $args);
1373 }
1374 return $var;
1375 } else {
1376 return call_user_func_array($func_name, $args);
1377 }
1378 }
1379
1380
1381 /*======================================================================*\
1382 Function: _dequote
1383 Purpose: Remove starting and ending quotes from the string
1384 \*======================================================================*/
1385 function _dequote($string)
1386 {
1387 if (($string{0} == "'" || $string{0} == '"') &&
1388 $string{strlen($string)-1} == $string{0})
1389 return substr($string, 1, -1);
1390 else
1391 return $string;
1392 }
1393
1394
1395 /*======================================================================*\
1396 Function: _read_file()
1397 Purpose: read in a file from line $start for $lines.
1398 read the entire file if $start and $lines are null.
1399 \*======================================================================*/
1400 function _read_file($filename, $start=null, $lines=null)
1401 {
1402 if (!($fd = @fopen($filename, 'r'))) {
1403 return false;
1404 }
1405 flock($fd, LOCK_SH);
1406 if ($start == null && $lines == null) {
1407 // read the entire file
1408 $contents = fread($fd, filesize($filename));
1409 } else {
1410 if ( $start > 1 ) {
1411 // skip the first lines before $start
1412 for ($loop=1; $loop < $start; $loop++) {
1413 fgets($fd, 65536);
1414 }
1415 }
1416 if ( $lines == null ) {
1417 // read the rest of the file
1418 while (!feof($fd)) {
1419 $contents .= fgets($fd, 65536);
1420 }
1421 } else {
1422 // read up to $lines lines
1423 for ($loop=0; $loop < $lines; $loop++) {
1424 $contents .= fgets($fd, 65536);
1425 if (feof($fd)) {
1426 break;
1427 }
1428 }
1429 }
1430 }
1431 fclose($fd);
1432 return $contents;
1433 }
1434
1435 /*======================================================================*\
1436 Function: _write_file()
1437 Purpose: write out a file
1438 \*======================================================================*/
1439 function _write_file($filename, $contents, $create_dirs = false)
1440 {
1441 if ($create_dirs)
1442 $this->_create_dir_structure(dirname($filename));
1443
1444 if (!($fd = @fopen($filename, 'w'))) {
1445 $this->trigger_error("problem writing '$filename.'");
1446 return false;
1447 }
1448
1449 // flock doesn't seem to work on several windows platforms (98, NT4, NT5, ?),
1450 // so we'll not use it at all in windows.
1451
1452 if ( strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' || (flock($fd, LOCK_EX)) ) {
1453 fwrite( $fd, $contents );
1454 fclose($fd);
1455 chmod($filename, 0644);
1456 }
1457
1458 return true;
1459 }
1460
1461 /*======================================================================*\
1462 Function: _get_auto_filename
1463 Purpose: get a concrete filename for automagically created content
1464 \*======================================================================*/
1465 function _get_auto_filename($auto_base, $auto_source = null, $auto_id = null)
1466 {
1467 static $_dir_sep = null;
1468 static $_dir_sep_enc = null;
1469
1470 if(!isset($_dir_sep)) {
1471 $_dir_sep_enc = urlencode(DIR_SEP);
1472 if($this->use_sub_dirs) {
1473 $_dir_sep = DIR_SEP;
1474 } else {
1475 $_dir_sep = '^';
1476 }
1477 }
1478
1479 if(@is_dir($auto_base)) {
1480 $res = $auto_base . DIR_SEP;
1481 } else {
1482 // auto_base not found, try include_path
1483 $this->_get_include_path($auto_base,$_include_path);
1484 $res = $_include_path . DIR_SEP;
1485 }
1486
1487 if(isset($auto_id)) {
1488 // make auto_id safe for directory names
1489 $auto_id = str_replace('%7C','|',(urlencode($auto_id)));
1490 // split into separate directories
1491 $auto_id = str_replace('|', $_dir_sep, $auto_id);
1492 $res .= $auto_id . $_dir_sep;
1493 }
1494
1495 if(isset($auto_source)) {
1496 // make source name safe for filename
1497 if($this->use_sub_dirs) {
1498 $_filename = urlencode(basename($auto_source));
1499 $_crc32 = crc32($auto_source) . $_dir_sep;
1500 // prepend %% to avoid name conflicts with
1501 // with $auto_id names
1502 $_crc32 = '%%' . substr($_crc32,0,3) . $_dir_sep . '%%' . $_crc32;
1503 $res .= $_crc32 . $_filename . '.php';
1504 } else {
1505 $res .= str_replace($_dir_sep_enc,'^',urlencode($auto_source));
1506 }
1507 }
1508
1509 return $res;
1510 }
1511
1512 /*======================================================================*\
1513 Function: _rm_auto
1514 Purpose: delete an automagically created file by name and id
1515 \*======================================================================*/
1516 function _rm_auto($auto_base, $auto_source = null, $auto_id = null, $exp_time = null)
1517 {
1518 if (!@is_dir($auto_base))
1519 return false;
1520
1521 if(!isset($auto_id) && !isset($auto_source)) {
1522 $res = $this->_rmdir($auto_base, 0, $exp_time);
1523 } else {
1524 $tname = $this->_get_auto_filename($auto_base, $auto_source, $auto_id);
1525
1526 if(isset($auto_source)) {
1527 $res = $this->_unlink($tname);
1528 } elseif ($this->use_sub_dirs) {
1529 $res = $this->_rmdir($tname, 1, $exp_time);
1530 } else {
1531 // remove matching file names
1532 $handle = opendir($auto_base);
1533 while ($filename = readdir($handle)) {
1534 if($filename == '.' || $filename == '..') {
1535 continue;
1536 } elseif (substr($auto_base . DIR_SEP . $filename,0,strlen($tname)) == $tname) {
1537 $this->_unlink($auto_base . DIR_SEP . $filename, $exp_time);
1538 }
1539 }
1540 }
1541 }
1542
1543 return $res;
1544 }
1545
1546 /*======================================================================*\
1547 Function: _rmdir
1548 Purpose: delete a dir recursively (level=0 -> keep root)
1549 WARNING: no security whatsoever!!
1550 \*======================================================================*/
1551 function _rmdir($dirname, $level = 1, $exp_time = null)
1552 {
1553
1554 if($handle = @opendir($dirname)) {
1555
1556 while ($entry = readdir($handle)) {
1557 if ($entry != '.' && $entry != '..') {
1558 if (@is_dir($dirname . DIR_SEP . $entry)) {
1559 $this->_rmdir($dirname . DIR_SEP . $entry, $level + 1, $exp_time);
1560 }
1561 else {
1562 $this->_unlink($dirname . DIR_SEP . $entry, $exp_time);
1563 }
1564 }
1565 }
1566
1567 closedir($handle);
1568
1569 if ($level)
1570 @rmdir($dirname);
1571
1572 return true;
1573
1574 } else {
1575 return false;
1576 }
1577 }
1578
1579 /*======================================================================*\
1580 Function: _unlink
1581 Purpose: unlink a file, possibly using expiration time
1582 \*======================================================================*/
1583 function _unlink($resource, $exp_time = null)
1584 {
1585 if(isset($exp_time)) {
1586 if(time() - filemtime($resource) >= $exp_time) {
1587 @unlink($resource);
1588 }
1589 } else {
1590 @unlink($resource);
1591 }
1592 }
1593
1594 /*======================================================================*\
1595 Function: _create_dir_structure
1596 Purpose: create full directory structure
1597 \*======================================================================*/
1598 function _create_dir_structure($dir)
1599 {
1600 if (!@file_exists($dir)) {
1601 $_dir_parts = preg_split('!\\'.DIR_SEP.'+!', $dir, -1, PREG_SPLIT_NO_EMPTY);
1602 $_new_dir = ($dir{0} == DIR_SEP) ? DIR_SEP : '';
1603
1604 // do not attempt to test or make directories outside of open_basedir
1605 $_open_basedir_ini = ini_get('open_basedir');
1606 if(!empty($_open_basedir_ini)) {
1607 $_use_open_basedir = true;
1608 $_open_basedir_sep = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') ? ';' : ':';
1609 $_open_basedirs = explode($_open_basedir_sep, $_open_basedir_ini);
1610 } else {
1611 $_use_open_basedir = false;
1612 }
1613
1614 foreach ($_dir_parts as $_dir_part) {
1615 $_new_dir .= $_dir_part;
1616
1617 if ($_use_open_basedir) {
1618 $_make_new_dir = false;
1619 foreach ($_open_basedirs as $_open_basedir) {
1620 if (substr($_new_dir.'/', 0, strlen($_open_basedir)) == $_open_basedir) {
1621 $_make_new_dir = true;
1622 break;
1623 }
1624 }
1625 } else {
1626 $_make_new_dir = true;
1627 }
1628
1629 if ($_make_new_dir && !@file_exists($_new_dir) && !@mkdir($_new_dir, 0771)) {
1630 $this->trigger_error("problem creating directory \"$dir\"");
1631 return false;
1632 }
1633 $_new_dir .= DIR_SEP;
1634 }
1635 }
1636 }
1637
1638 /*======================================================================*\
1639 Function: _write_cache_file
1640 Purpose: Prepend the cache information to the cache file
1641 and write it
1642 \*======================================================================*/
1643 function _write_cache_file($tpl_file, $cache_id, $compile_id, $results)
1644 {
1645 // put timestamp in cache header
1646 $this->_cache_info['timestamp'] = time();
1647 if ($this->cache_lifetime > -1){
1648 // expiration set
1649 $this->_cache_info['expires'] = $this->_cache_info['timestamp'] + $this->cache_lifetime;
1650 } else {
1651 // cache will never expire
1652 $this->_cache_info['expires'] = -1;
1653 }
1654
1655 // prepend the cache header info into cache file
1656 $results = serialize($this->_cache_info)."\n".$results;
1657
1658 if (!empty($this->cache_handler_func)) {
1659 // use cache_handler function
1660 $funcname = $this->cache_handler_func;
1661 return $funcname('write', $this, $results, $tpl_file, $cache_id, $compile_id);
1662 } else {
1663 // use local cache file
1664 if (isset($cache_id))
1665 $auto_id = (isset($compile_id)) ? $cache_id . '|' . $compile_id : $cache_id;
1666 elseif(isset($compile_id))
1667 $auto_id = $compile_id;
1668 else
1669 $auto_id = null;
1670
1671 $cache_file = $this->_get_auto_filename($this->cache_dir, $tpl_file, $auto_id);
1672 $this->_write_file($cache_file, $results, true);
1673 return true;
1674 }
1675 }
1676
1677 /*======================================================================*\
1678 Function: _read_cache_file
1679 Purpose: read a cache file, determine if it needs to be
1680 regenerated or not
1681 \*======================================================================*/
1682 function _read_cache_file($tpl_file, $cache_id, $compile_id, &$results)
1683 {
1684 static $content_cache = array();
1685
1686 if ($this->force_compile) {
1687 // force compile enabled, always regenerate
1688 return false;
1689 }
1690
1691 if (isset($content_cache["$tpl_file,$cache_id,$compile_id"])) {
1692 list($results, $this->_cache_info) = $content_cache["$tpl_file,$cache_id,$compile_id"];
1693 return true;
1694 }
1695
1696 if (!empty($this->cache_handler_func)) {
1697 // use cache_handler function
1698 $funcname = $this->cache_handler_func;
1699 $funcname('read', $this, $results, $tpl_file, $cache_id, $compile_id);
1700 } else {
1701 // use local cache file
1702 if (isset($cache_id))
1703 $auto_id = (isset($compile_id)) ? $cache_id . '|' . $compile_id : $cache_id;
1704 elseif(isset($compile_id))
1705 $auto_id = $compile_id;
1706 else
1707 $auto_id = null;
1708
1709 $cache_file = $this->_get_auto_filename($this->cache_dir, $tpl_file, $auto_id);
1710 $results = $this->_read_file($cache_file);
1711 }
1712
1713 if (empty($results)) {
1714 // nothing to parse (error?), regenerate cache
1715 return false;
1716 }
1717
1718 $cache_split = explode("\n", $results, 2);
1719 $cache_header = $cache_split[0];
1720
1721 $this->_cache_info = unserialize($cache_header);
1722
1723 if ($this->caching == 2 && isset ($this->_cache_info['expires'])){
1724 // caching by expiration time
1725 if ($this->_cache_info['expires'] > -1 && (time() > $this->_cache_info['expires'])) {
1726 // cache expired, regenerate
1727 return false;
1728 }
1729 } else {
1730 // caching by lifetime
1731 if ($this->cache_lifetime > -1 && (time() - $this->_cache_info['timestamp'] > $this->cache_lifetime)) {
1732 // cache expired, regenerate
1733 return false;
1734 }
1735 }
1736
1737 if ($this->compile_check) {
1738 foreach ($this->_cache_info['template'] as $template_dep) {
1739 $this->_fetch_template_info($template_dep, $template_source, $template_timestamp, false);
1740 if ($this->_cache_info['timestamp'] < $template_timestamp) {
1741 // template file has changed, regenerate cache
1742 return false;
1743 }
1744 }
1745
1746 if (isset($this->_cache_info['config'])) {
1747 foreach ($this->_cache_info['config'] as $config_dep) {
1748 if ($this->_cache_info['timestamp'] < filemtime($this->config_dir.DIR_SEP.$config_dep)) {
1749 // config file has changed, regenerate cache
1750 return false;
1751 }
1752 }
1753 }
1754 }
1755
1756 $results = $cache_split[1];
1757 $content_cache["$tpl_file,$cache_id,$compile_id"] = array($results, $this->_cache_info);
1758
1759 return true;
1760 }
1761
1762 /*======================================================================*\
1763 Function: _get_plugin_filepath
1764 Purpose: get filepath of requested plugin
1765 \*======================================================================*/
1766 function _get_plugin_filepath($type, $name)
1767 {
1768 $_plugin_filename = "$type.$name.php";
1769
1770 foreach ((array)$this->plugins_dir as $_plugin_dir) {
1771
1772 $_plugin_filepath = $_plugin_dir . DIR_SEP . $_plugin_filename;
1773
1774 // see if path is relative
1775 if (!preg_match("/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/", $_plugin_dir)) {
1776 $_relative_paths[] = $_plugin_dir;
1777 // relative path, see if it is in the SMARTY_DIR
1778 if (@is_readable(SMARTY_DIR . $_plugin_filepath)) {
1779 return SMARTY_DIR . $_plugin_filepath;
1780 }
1781 }
1782 // try relative to cwd (or absolute)
1783 if (@is_readable($_plugin_filepath)) {
1784 return $_plugin_filepath;
1785 }
1786 }
1787
1788 // still not found, try PHP include_path
1789 if(isset($_relative_paths)) {
1790 foreach ((array)$_relative_paths as $_plugin_dir) {
1791
1792 $_plugin_filepath = $_plugin_dir . DIR_SEP . $_plugin_filename;
1793
1794 if ($this->_get_include_path($_plugin_filepath, $_include_filepath)) {
1795 return $_include_filepath;
1796 }
1797 }
1798 }
1799
1800
1801 return false;
1802 }
1803
1804 /*======================================================================*\
1805 Function: _load_plugins
1806 Purpose: Load requested plugins
1807 \*======================================================================*/
1808 function _load_plugins($plugins)
1809 {
1810
1811 foreach ($plugins as $plugin_info) {
1812 list($type, $name, $tpl_file, $tpl_line, $delayed_loading) = $plugin_info;
1813 $plugin = &$this->_plugins[$type][$name];
1814
1815 /*
1816 * We do not load plugin more than once for each instance of Smarty.
1817 * The following code checks for that. The plugin can also be
1818 * registered dynamically at runtime, in which case template file
1819 * and line number will be unknown, so we fill them in.
1820 *
1821 * The final element of the info array is a flag that indicates
1822 * whether the dynamically registered plugin function has been
1823 * checked for existence yet or not.
1824 */
1825 if (isset($plugin)) {
1826 if (!$plugin[3]) {
1827 if (!function_exists($plugin[0])) {
1828 $this->_trigger_plugin_error("$type '$name' is not implemented", $tpl_file, $tpl_line);
1829 } else {
1830 $plugin[1] = $tpl_file;
1831 $plugin[2] = $tpl_line;
1832 $plugin[3] = true;
1833 }
1834 }
1835 continue;
1836 } else if ($type == 'insert') {
1837 /*
1838 * For backwards compatibility, we check for insert functions in
1839 * the symbol table before trying to load them as a plugin.
1840 */
1841 $plugin_func = 'insert_' . $name;
1842 if (function_exists($plugin_func)) {
1843 $plugin = array($plugin_func, $tpl_file, $tpl_line, true);
1844 continue;
1845 }
1846 }
1847
1848 $plugin_file = $this->_get_plugin_filepath($type, $name);
1849
1850 if ($found = ($plugin_file != false)) {
1851 $message = "could not load plugin file '$type.$name.php'\n";
1852 }
1853
1854 /*
1855 * If plugin file is found, it -must- provide the properly named
1856 * plugin function. In case it doesn't, simply output the error and
1857 * do not fall back on any other method.
1858 */
1859 if ($found) {
1860 include_once $plugin_file;
1861
1862 $plugin_func = 'smarty_' . $type . '_' . $name;
1863 if (!function_exists($plugin_func)) {
1864 $this->_trigger_plugin_error("plugin function $plugin_func() not found in $plugin_file", $tpl_file, $tpl_line);
1865 continue;
1866 }
1867 }
1868 /*
1869 * In case of insert plugins, their code may be loaded later via
1870 * 'script' attribute.
1871 */
1872 else if ($type == 'insert' && $delayed_loading) {
1873 $plugin_func = 'smarty_' . $type . '_' . $name;
1874 $found = true;
1875 }
1876
1877 /*
1878 * Plugin specific processing and error checking.
1879 */
1880 if (!$found) {
1881 if ($type == 'modifier') {
1882 /*
1883 * In case modifier falls back on using PHP functions
1884 * directly, we only allow those specified in the security
1885 * context.
1886 */
1887 if ($this->security && !in_array($name, $this->security_settings['MODIFIER_FUNCS'])) {
1888 $message = "(secure mode) modifier '$name' is not allowed";
1889 } else {
1890 if (!function_exists($name)) {
1891 $message = "modifier '$name' is not implemented";
1892 } else {
1893 $plugin_func = $name;
1894 $found = true;
1895 }
1896 }
1897 } else if ($type == 'function') {
1898 /*
1899 * This is a catch-all situation.
1900 */
1901 $message = "unknown tag - '$name'";
1902 }
1903 }
1904
1905 if ($found) {
1906 $this->_plugins[$type][$name] = array($plugin_func, $tpl_file, $tpl_line, true);
1907 } else {
1908 // output error
1909 $this->_trigger_plugin_error($message, $tpl_file, $tpl_line);
1910 }
1911 }
1912 }
1913
1914 /*======================================================================*\
1915 Function: _load_resource_plugin
1916 Purpose:
1917 \*======================================================================*/
1918 function _load_resource_plugin($type)
1919 {
1920 /*
1921 * Resource plugins are not quite like the other ones, so they are
1922 * handled differently. The first element of plugin info is the array of
1923 * functions provided by the plugin, the second one indicates whether
1924 * all of them exist or not.
1925 */
1926
1927 $plugin = &$this->_plugins['resource'][$type];
1928 if (isset($plugin)) {
1929 if (!$plugin[1] && count($plugin[0])) {
1930 $plugin[1] = true;
1931 foreach ($plugin[0] as $plugin_func) {
1932 if (!function_exists($plugin_func)) {
1933 $plugin[1] = false;
1934 break;
1935 }
1936 }
1937 }
1938
1939 if (!$plugin[1]) {
1940 $this->_trigger_plugin_error("resource '$type' is not implemented");
1941 }
1942
1943 return;
1944 }
1945
1946 $plugin_file = $this->_get_plugin_filepath('resource', $type);
1947 $found = ($plugin_file != false);
1948
1949 if ($found) { /*
1950 * If the plugin file is found, it -must- provide the properly named
1951 * plugin functions.
1952 */
1953 include_once $plugin_file;
1954
1955 /*
1956 * Locate functions that we require the plugin to provide.
1957 */
1958 $resource_ops = array('source', 'timestamp', 'secure', 'trusted');
1959 $resource_funcs = array();
1960 foreach ($resource_ops as $op) {
1961 $plugin_func = 'smarty_resource_' . $type . '_' . $op;
1962 if (!function_exists($plugin_func)) {
1963 $this->_trigger_plugin_error("plugin function $plugin_func() not found in $plugin_file");
1964 return;
1965 } else {
1966 $resource_funcs[] = $plugin_func;
1967 }
1968 }
1969
1970 $this->_plugins['resource'][$type] = array($resource_funcs, true);
1971 }
1972 }
1973
1974 /*======================================================================*\
1975 Function: _autoload_filters()
1976 Purpose: automatically load a set of filters
1977 \*======================================================================*/
1978 function _autoload_filters()
1979 {
1980 foreach ($this->autoload_filters as $filter_type => $filters) {
1981 foreach ($filters as $filter) {
1982 $this->load_filter($filter_type, $filter);
1983 }
1984 }
1985 }
1986
1987 /*======================================================================*\
1988 Function: quote_replace
1989 Purpose: Quote subpattern references
1990 \*======================================================================*/
1991 function quote_replace($string)
1992 {
1993 return preg_replace('![\\$]\d!', '\\\\\\0', $string);
1994 }
1995
1996
1997 /*======================================================================*\
1998 Function: _trigger_plugin_error
1999 Purpose: trigger Smarty plugin error
2000 \*======================================================================*/
2001 function _trigger_plugin_error($error_msg, $tpl_file = null, $tpl_line = null, $error_type = E_USER_ERROR)
2002 {
2003 if (isset($tpl_line) && isset($tpl_file)) {
2004 trigger_error("Smarty plugin error: [in " . $tpl_file . " line " .
2005 $tpl_line . "]: $error_msg", $error_type);
2006 } else {
2007 trigger_error("Smarty plugin error: $error_msg", $error_type);
2008 }
2009 }
2010
2011 /*======================================================================*\
2012 Function: _get_microtime
2013 Purpose: Get seconds and microseconds
2014 \*======================================================================*/
2015 function _get_microtime()
2016 {
2017 $mtime = microtime();
2018 $mtime = explode(" ", $mtime);
2019 $mtime = (double)($mtime[1]) + (double)($mtime[0]);
2020 return ($mtime);
2021 }
2022
2023 /*======================================================================*\
2024 Function: _get_include_path
2025 Purpose: Get path to file from include_path
2026 \*======================================================================*/
2027 function _get_include_path($file_path,&$new_file_path)
2028 {
2029 static $_path_array = null;
2030
2031 if(!isset($_path_array)) {
2032 $_ini_include_path = ini_get('include_path');
2033
2034 if(strstr($_ini_include_path,';')) {
2035 // windows pathnames
2036 $_path_array = explode(';',$_ini_include_path);
2037 } else {
2038 $_path_array = explode(':',$_ini_include_path);
2039 }
2040 }
2041 foreach ($_path_array as $_include_path) {
2042 if (@file_exists($_include_path . DIR_SEP . $file_path)) {
2043 $new_file_path = $_include_path . DIR_SEP . $file_path;
2044 return true;
2045 }
2046 }
2047 return false;
2048 }
2049
2050 }
2051
2052 /* vim: set expandtab: */
2053
2054 ?>

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