init
[garradin.git] / include / libs / template_lite / class.template.php
1 <?php
2 /*
3 * Project: template_lite, a smarter template engine
4 * File: class.template.php
5 * Author: Paul Lockaby <paul@paullockaby.com>, Mark Dickenson <akapanamajack@sourceforge.net>
6 * Copyright: 2003,2004,2005 by Paul Lockaby, 2005,2006 Mark Dickenson
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * The latest version of template_lite can be obtained from:
23 * http://templatelite.sourceforge.net
24 *
25 */
26
27 if (!defined('TEMPLATE_LITE_DIR')) {
28 define('TEMPLATE_LITE_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR);
29 }
30
31 class Template_Exception extends RuntimeException
32 {
33 public function __construct($message, &$tpl = null)
34 {
35 if (!is_null($tpl) && is_object($tpl))
36 {
37 $message = '[in ' . $tpl->_file . ' line ' . $tpl->_linenum . '] ' . $message;
38 }
39
40 parent::__construct($message);
41 }
42 }
43
44 class Template_Lite {
45 // public configuration variables
46 var $left_delimiter = "{"; // the left delimiter for template tags
47 var $right_delimiter = "}"; // the right delimiter for template tags
48 var $cache = false; // whether or not to allow caching of files
49 var $force_compile = false; // force a compile regardless of saved state
50 var $template_dir = "templates"; // where the templates are to be found
51 var $plugins_dir = array("plugins"); // where the plugins are to be found
52 var $compile_dir = "compiled"; // the directory to store the compiled files in
53 var $config_dir = "templates"; // where the config files are
54 var $cache_dir = "cached"; // where cache files are stored
55 var $config_overwrite = false;
56 var $config_booleanize = true;
57 var $config_fix_new_lines = true;
58 var $config_read_hidden = true;
59 var $cache_lifetime = 0; // how long the file in cache should be considered "fresh"
60 var $encode_file_name = true; // Set this to false if you do not want the name of the compiled/cached file to be md5 encoded.
61 var $php_extract_vars = false; // Set this to true if you want the $this->_tpl variables to be extracted for use by PHP code inside the template.
62 var $reserved_template_varname = "templatelite";
63 var $default_modifiers = array();
64 var $debugging = false;
65
66 var $compiler_file = 'class.compiler.php';
67 var $compiler_class = 'Template_Lite_Compiler';
68 var $config_class = 'config';
69
70 // gzip output configuration
71 var $send_now = 1;
72 var $force_compression = 0;
73 var $compression_level = 9;
74 var $enable_gzip = 1;
75
76 // private internal variables
77 var $_vars = array(); // stores all internal assigned variables
78 var $_confs = array(); // stores all internal config variables
79 var $_plugins = array( 'modifier' => array(),
80 'function' => array(),
81 'block' => array(),
82 'compiler' => array(),
83 'resource' => array(),
84 'prefilter' => array(),
85 'postfilter' => array(),
86 'outputfilter' => array());
87 var $_linenum = 0; // the current line number in the file we are processing
88 var $_file = ""; // the current file we are processing
89 var $_config_obj = null;
90 var $_compile_obj = null;
91 var $_cache_id = null;
92 var $_cache_dir = ""; // stores where this specific file is going to be cached
93 var $_cache_info = array('config' => array(), 'template' => array());
94 var $_sl_md5 = '39fc70570b8b60cbc1b85839bf242aff';
95 var $_version = 'V2.10 Template Lite 4 January 2007 (c) 2005-2007 Mark Dickenson. All rights reserved. Released LGPL.';
96 var $_version_date = "2007-01-04 10:34:21";
97 var $_config_module_loaded = false;
98 var $_templatelite_debug_info = array();
99 var $_templatelite_debug_loop = false;
100 var $_templatelite_debug_dir = "";
101 var $_inclusion_depth = 0;
102 var $_null = null;
103 var $_resource_type = 1;
104 var $_resource_time;
105 var $_sections = array();
106 var $_foreach = array();
107
108 function Template_Lite()
109 {
110 $this->_version_date = strtotime($this->_version_date);
111 $this->_base_plugins = $this->_plugins;
112 }
113
114 function load_filter($type, $name)
115 {
116 switch ($type)
117 {
118 case 'output':
119 include_once( $this->_get_plugin_dir($type . "filter." . $name . ".php") . $type . "filter." . $name . ".php");
120 $this->_plugins['outputfilter'][$name] = "template_" . $type . "filter_" . $name;
121 break;
122 case 'pre':
123 case 'post':
124 if (!isset($this->_plugins[$type . 'filter'][$name]))
125 {
126 $this->_plugins[$type . 'filter'][$name] = "template_" . $type . "filter_" . $name;
127 }
128 break;
129 }
130 }
131
132 function assign($key, $value = null)
133 {
134 if (is_array($key))
135 {
136 foreach($key as $var => $val)
137 if ($var != "")
138 {
139 $this->_vars[$var] = $val;
140 }
141 }
142 else
143 {
144 if ($key != "")
145 {
146 $this->_vars[$key] = $value;
147 }
148 }
149 }
150
151 function assign_by_ref($key, $value = null)
152 {
153 if ($key != '')
154 {
155 $this->_vars[$key] = &$value;
156 }
157 }
158
159 function assign_config($key, $value = null)
160 {
161 if (is_array($key))
162 {
163 foreach($key as $var => $val)
164 {
165 if ($var != "")
166 {
167 $this->_confs[$var] = $val;
168 }
169 }
170 }
171 else
172 {
173 if ($key != "")
174 {
175 $this->_confs[$key] = $value;
176 }
177 }
178 }
179
180 function append($key, $value=null, $merge=false)
181 {
182 if (is_array($key))
183 {
184 foreach ($key as $_key => $_value)
185 {
186 if ($_key != '')
187 {
188 if(!@is_array($this->_vars[$_key]))
189 {
190 settype($this->_vars[$_key],'array');
191 }
192 if($merge && is_array($_value))
193 {
194 foreach($_value as $_mergekey => $_mergevalue)
195 {
196 $this->_vars[$_key][$_mergekey] = $_mergevalue;
197 }
198 }
199 else
200 {
201 $this->_vars[$_key][] = $_value;
202 }
203 }
204 }
205 }
206 else
207 {
208 if ($key != '' && isset($value))
209 {
210 if(!@is_array($this->_vars[$key]))
211 {
212 settype($this->_vars[$key],'array');
213 }
214 if($merge && is_array($value))
215 {
216 foreach($value as $_mergekey => $_mergevalue)
217 {
218 $this->_vars[$key][$_mergekey] = $_mergevalue;
219 }
220 }
221 else
222 {
223 $this->_vars[$key][] = $value;
224 }
225 }
226 }
227 }
228
229 function append_by_ref($key, &$value, $merge=false)
230 {
231 if ($key != '' && isset($value))
232 {
233 if(!@is_array($this->_vars[$key]))
234 {
235 settype($this->_vars[$key],'array');
236 }
237 if ($merge && is_array($value))
238 {
239 foreach($value as $_key => $_val)
240 {
241 $this->_vars[$key][$_key] = &$value[$_key];
242 }
243 }
244 else
245 {
246 $this->_vars[$key][] = &$value;
247 }
248 }
249 }
250
251 function clear_assign($key = null)
252 {
253 if ($key == null)
254 {
255 $this->_vars = array();
256 }
257 else
258 {
259 if (is_array($key))
260 {
261 foreach($key as $index => $value)
262 {
263 if (in_array($value, $this->_vars))
264 {
265 unset($this->_vars[$index]);
266 }
267 }
268 }
269 else
270 {
271 if (in_array($key, $this->_vars))
272 {
273 unset($this->_vars[$index]);
274 }
275 }
276 }
277 }
278
279 function clear_all_assign()
280 {
281 $this->_vars = array();
282 }
283
284 function clear_config($key = null)
285 {
286 if ($key == null)
287 {
288 $this->_conf = array();
289 }
290 else
291 {
292 if (is_array($key))
293 {
294 foreach($key as $index => $value)
295 {
296 if (in_array($value, $this->_conf))
297 {
298 unset($this->_conf[$index]);
299 }
300 }
301 }
302 else
303 {
304 if (in_array($key, $this->_conf))
305 {
306 unset($this->_conf[$key]);
307 }
308 }
309 }
310 }
311
312 function &get_template_vars($key = null)
313 {
314 if ($key == null)
315 {
316 return $this->_vars;
317 }
318 else
319 {
320 if (isset($this->_vars[$key]))
321 {
322 return $this->_vars[$key];
323 }
324 else
325 {
326 return $this->_null;
327 }
328 }
329 }
330
331 function &get_config_vars($key = null)
332 {
333 if ($key == null)
334 {
335 return $this->_confs;
336 }
337 else
338 {
339 if (isset($this->_confs[$key]))
340 {
341 return $this->_confs[$key];
342 }
343 else
344 {
345 return $this->_null;
346 }
347 }
348 }
349
350 function clear_compiled_tpl($file = null)
351 {
352 $this->_destroy_dir($file, null, $this->_get_dir($this->compile_dir));
353 }
354
355 function clear_cache($file = null, $cache_id = null, $compile_id = null, $exp_time = null)
356 {
357 $this->_destroy_dir($file, $cache_id, $this->_get_dir($this->cache_dir));
358 }
359
360 function clear_all_cache($exp_time = null)
361 {
362 $this->clear_cache();
363 }
364
365 function is_cached($file, $cache_id = null)
366 {
367 if (!$this->force_compile && $this->_is_cached($file, $cache_id))
368 {
369 return true;
370 }
371 else
372 {
373 return false;
374 }
375 }
376
377 // Warning : don't use $base, it's a temporary fix because plugin management is very crappy actually
378 function register_modifier($modifier, $implementation, $base=true)
379 {
380 if (!is_callable($implementation))
381 throw new Template_Exception("'$implementation' modifier doesn't seem to be a valid callback");
382
383 $this->_plugins['modifier'][$modifier] = $implementation;
384 if ($base)
385 $this->_base_plugins['modifier'][$modifier] = $implementation;
386 }
387
388 function unregister_modifier($modifier)
389 {
390 unset($this->_plugins['modifier'][$modifier]);
391 }
392
393 function register_function($function, $implementation, $base=true)
394 {
395 if (!is_callable($implementation))
396 throw new Template_Exception("Function '$function' doesn't seem to be a valid callback");
397
398 // Non-static object callbacks are not supported now
399 if (is_array($implementation) && !is_string($function[0]))
400 {
401 throw new Template_Exception("Unsupported object callback for function '$function'");
402 }
403
404 if (is_object($implementation))
405 {
406 throw new Template_Exception("Closures are not supported for functions.");
407 }
408
409 $this->_plugins['function'][$function] = $implementation;
410 if ($base)
411 $this->_base_plugins['function'][$function] = $implementation;
412 }
413
414 function unregister_function($function)
415 {
416 unset($this->_plugins['function'][$function]);
417 }
418
419 function register_block($function, $implementation, $base=true)
420 {
421 if (!is_callable($implementation))
422 throw new Template_Exception("'$implementation' block function doesn't seem to be a valid callback");
423
424 $this->_plugins['block'][$function] = $implementation;
425 if ($base)
426 $this->_base_plugins['block'][$function] = $implementation;
427 }
428
429 function unregister_block($function)
430 {
431 unset($this->_plugins['block'][$function]);
432 }
433
434 function register_compiler($function, $implementation, $base=true)
435 {
436 if (!is_callable($implementation))
437 throw new Template_Exception("'$implementation' compiler function doesn't seem to be a valid callback");
438
439 $this->_plugins['compiler'][$function] = $implementation;
440 if ($base)
441 $this->_base_plugins['compiler'][$function] = $implementation;
442 }
443
444 function unregister_compiler($function)
445 {
446 unset($this->_plugins['compiler'][$function]);
447 }
448
449 function register_prefilter($function)
450 {
451 $_name = (is_array($function)) ? $function[1] : $function;
452 $this->_plugins['prefilter'][$_name] = $_name;
453 }
454
455 function unregister_prefilter($function)
456 {
457 unset($this->_plugins['prefilter'][$function]);
458 }
459
460 function register_postfilter($function)
461 {
462 $_name = (is_array($function)) ? $function[1] : $function;
463 $this->_plugins['postfilter'][$_name] = $_name;
464 }
465
466 function unregister_postfilter($function)
467 {
468 unset($this->_plugins['postfilter'][$function]);
469 }
470
471 function register_outputfilter($function)
472 {
473 $_name = (is_array($function)) ? $function[1] : $function;
474 $this->_plugins['outputfilter'][$_name] = $_name;
475 }
476
477 function unregister_outputfilter($function)
478 {
479 unset($this->_plugins['outputfilter'][$function]);
480 }
481
482 function register_resource($type, $functions)
483 {
484 if (count($functions) == 4)
485 {
486 $this->_plugins['resource'][$type] = $functions;
487 }
488 else
489 {
490 throw new Template_Exception("malformed function-list for '$type' in register_resource", $this);
491 }
492 }
493
494 function unregister_resource($type)
495 {
496 unset($this->_plugins['resource'][$type]);
497 }
498
499 function template_exists($file)
500 {
501 if (file_exists($this->_get_dir($this->template_dir).$file))
502 {
503 $this->_resource_time = filemtime($this->_get_dir($this->template_dir).$file);
504 $this->_resource_type = 1;
505 return true;
506 }
507 else
508 {
509 if (file_exists($file))
510 {
511 $this->_resource_time = filemtime($file);
512 $this->_resource_type = "file";
513 return true;
514 }
515 return false;
516 }
517 }
518
519 function _get_resource($file)
520 {
521 $_resource_name = explode(':', trim($file));
522
523 if (count($_resource_name) == 1 || $_resource_name[0] == "file" || $_resource_name[0] == 'phar')
524 {
525 if($_resource_name[0] == "file")
526 {
527 $file = substr($file, 5);
528 }
529
530 $exists = $this->template_exists($file);
531
532 if (!$exists)
533 {
534 throw new Template_Exception("file '$file' does not exist", $this);
535 }
536 }
537 else
538 {
539 $this->_resource_type = $_resource_name[0];
540 $file = substr($file, strlen($this->_resource_type) + 1);
541 $exists = isset($this->_plugins['resource'][$this->_resource_type]) && call_user_func_array($this->_plugins['resource'][$this->_resource_type][1], array($file, &$resource_timestamp, &$this));
542
543 if (!$exists)
544 {
545 throw new Template_Exception("file '$file' does not exist", $this);
546 }
547 $this->_resource_time = $resource_timestamp;
548 }
549 return $file;
550 }
551
552 function display($file, $cache_id = null)
553 {
554 $this->fetch($file, $cache_id, true);
555 }
556
557 function fetch($file, $cache_id = null, $display = false)
558 {
559 $file = $this->_get_resource($file);
560
561 if ($this->debugging)
562 {
563 $this->_templatelite_debug_info[] = array('type' => 'template',
564 'filename' => $file,
565 'depth' => 0,
566 'exec_time' => array_sum(explode(' ', microtime())) );
567 $included_tpls_idx = count($this->_templatelite_debug_info) - 1;
568 }
569
570 $this->_cache_id = $cache_id;
571 $this->template_dir = $this->_get_dir($this->template_dir);
572 $this->compile_dir = $this->_get_dir($this->compile_dir);
573 if ($this->cache)
574 {
575 $this->_cache_dir = $this->_build_dir($this->cache_dir, $this->_cache_id);
576 }
577
578 $name = ($this->encode_file_name) ? md5((($this->_resource_type == 1) ? $this->template_dir.$file : $this->_resource_type . "_" . $file)).'.php' : str_replace(".", "_", str_replace("/", "_", $this->_resource_type . "_" . $file)).'.php';
579
580 $this->_error_level = $this->debugging ? error_reporting() : error_reporting(error_reporting() & ~E_NOTICE);
581 // $this->_error_level = error_reporting(E_ALL);
582
583 if (!$this->force_compile && $this->cache && $this->_is_cached($file, $cache_id))
584 {
585 ob_start();
586 include($this->_cache_dir.$name);
587 $output = ob_get_contents();
588 ob_end_clean();
589 $output = substr($output, strpos($output, "\n") + 1);
590 }
591 else
592 {
593
594 $output = $this->_fetch_compile($file);
595
596 if ($this->cache)
597 {
598 $f = fopen($this->_cache_dir.$name, "w");
599 fwrite($f, serialize($this->_cache_info) . "\n" . str_replace('<?xml', "<?php echo '<?xml'; ?>", $output));
600 fclose($f);
601 }
602 }
603
604 if (strpos($output, $this->_sl_md5) !== false)
605 {
606 preg_match_all('!' . $this->_sl_md5 . '{_run_insert (.*)}' . $this->_sl_md5 . '!U',$output,$_match);
607 foreach($_match[1] as $value)
608 {
609 $arguments = unserialize($value);
610 $output = str_replace($this->_sl_md5 . '{_run_insert ' . $value . '}' . $this->_sl_md5, call_user_func_array('insert_' . $arguments['name'], array((array)$arguments, $this)), $output);
611 }
612 }
613
614 foreach ($this->_plugins['outputfilter'] as $function)
615 {
616 $output = $function($output, $this);
617 }
618
619 error_reporting($this->_error_level);
620
621 if ($this->debugging)
622 {
623 $this->_templatelite_debug_info[$included_tpls_idx]['exec_time'] = array_sum(explode(' ', microtime())) - $this->_templatelite_debug_info[$included_tpls_idx]['exec_time'];
624 }
625
626 if ($display)
627 {
628 echo $output;
629 if($this->debugging && !$this->_templatelite_debug_loop)
630 {
631 $this->debugging = false;
632 if(!function_exists("template_generate_debug_output"))
633 {
634 require_once(TEMPLATE_LITE_DIR . "internal/template.generate_debug_output.php");
635 }
636 $debug_output = template_generate_debug_output($this);
637 $this->debugging = true;
638 echo $debug_output;
639 }
640 }
641 else
642 {
643 return $output;
644 }
645 }
646
647 function config_load($file, $section_name = null, $var_name = null)
648 {
649 require_once(TEMPLATE_LITE_DIR . "internal/template.config_loader.php");
650 }
651
652 function _is_cached($file, $cache_id)
653 {
654 $this->_cache_dir = $this->_get_dir($this->cache_dir, $cache_id);
655 $this->config_dir = $this->_get_dir($this->config_dir);
656 $this->template_dir = $this->_get_dir($this->template_dir);
657
658 $file = $this->_get_resource($file);
659
660 $name = ($this->encode_file_name) ? md5((($this->_resource_type == 1) ? $this->template_dir.$file : $this->_resource_type . "_" . $file)).'.php' : str_replace(".", "_", str_replace("/", "_", $this->_resource_type . "_" . $file)).'.php';
661
662 if (file_exists($this->_cache_dir.$name) && (((time() - filemtime($this->_cache_dir.$name)) < $this->cache_lifetime) || $this->cache_lifetime == -1) && (filemtime($this->_cache_dir.$name) > $this->_resource_time))
663 {
664 $fh = fopen($this->_cache_dir.$name, "r");
665 if (!feof($fh) && ($line = fgets($fh, filesize($this->_cache_dir.$name))))
666 {
667 $includes = unserialize($line);
668 if (isset($includes['template']))
669 {
670 foreach($includes['template'] as $value)
671 {
672 if (!(file_exists($this->template_dir.$value) && (filemtime($this->_cache_dir.$name) > filemtime($this->template_dir.$value))))
673 {
674 return false;
675 }
676 }
677 }
678 if (isset($includes['config']))
679 {
680 foreach($includes['config'] as $value)
681 {
682 if (!(file_exists($this->config_dir.$value) && (filemtime($this->_cache_dir.$name) > filemtime($this->config_dir.$value))))
683 {
684 return false;
685 }
686 }
687 }
688 }
689 fclose($fh);
690 }
691 else
692 {
693 return false;
694 }
695 return true;
696 }
697
698 function _fetch_compile_include($_templatelite_include_file, $_templatelite_include_vars)
699 {
700 if(!function_exists("template_fetch_compile_include"))
701 {
702 require_once(TEMPLATE_LITE_DIR . "internal/template.fetch_compile_include.php");
703 }
704 return template_fetch_compile_include($_templatelite_include_file, $_templatelite_include_vars, $this);
705 }
706
707 function _fetch_compile($file, $include=false)
708 {
709 $this->template_dir = $this->_get_dir($this->template_dir);
710
711 $name = ($this->encode_file_name) ? md5((($this->_resource_type == 1) ? $this->template_dir.$file : $this->_resource_type . "_" . $file)).'.php' : str_replace(".", "_", str_replace("/", "_", $this->_resource_type . "_" . $file)).'.php';
712
713 if ($this->cache)
714 {
715 array_push($this->_cache_info['template'], $file);
716 }
717
718 if (!$this->force_compile && file_exists($this->compile_dir.'c_'.$name)
719 && (filemtime($this->compile_dir.'c_'.$name) > $this->_resource_time)
720 && (filemtime($this->compile_dir.'c_'.$name) > $this->_version_date))
721 {
722 ob_start();
723 include($this->compile_dir.'c_'.$name);
724 $output = ob_get_contents();
725 ob_end_clean();
726 error_reporting($this->_error_level);
727 return $output;
728 }
729
730 $file_contents = "";
731 if($this->_resource_type == 1)
732 {
733 $f = fopen($this->template_dir . $file, "r");
734 $size = filesize($this->template_dir . $file);
735 if ($size > 0)
736 {
737 $file_contents = fread($f, $size);
738 }
739 }
740 else
741 if($this->_resource_type == "file")
742 {
743 $f = fopen($file, "r");
744 $size = filesize($file);
745 if ($size > 0)
746 {
747 $file_contents = fread($f, $size);
748 }
749 }
750 else
751 {
752 call_user_func_array($this->_plugins['resource'][$this->_resource_type][0], array($file, &$file_contents, &$this));
753 }
754
755 $this->_file = $file;
756 fclose($f);
757
758 if (!is_object($this->_compile_obj))
759 {
760 if (file_exists(TEMPLATE_LITE_DIR . $this->compiler_file)) {
761 require_once(TEMPLATE_LITE_DIR . $this->compiler_file);
762 } else {
763 require_once($this->compiler_file);
764 }
765 $this->_compile_obj = new $this->compiler_class;
766 }
767 $this->_compile_obj->left_delimiter = $this->left_delimiter;
768 $this->_compile_obj->right_delimiter = $this->right_delimiter;
769 $this->_compile_obj->plugins_dir = &$this->plugins_dir;
770 $this->_compile_obj->template_dir = &$this->template_dir;
771 $this->_compile_obj->_vars = &$this->_vars;
772 $this->_compile_obj->_confs = &$this->_confs;
773 $this->_compile_obj->_linenum = &$this->_linenum;
774 $this->_compile_obj->_file = &$this->_file;
775 $this->_compile_obj->php_extract_vars = &$this->php_extract_vars;
776 $this->_compile_obj->reserved_template_varname = &$this->reserved_template_varname;
777 $this->_compile_obj->default_modifiers = $this->default_modifiers;
778
779 // FIXME: the is a lot of bugs with _plugins because it's crappy
780 // _plugins is used to register plugins, and that's cool, but register_plugins is used also
781 // in compiled templates, so it creates a lot of bugs, we'll have to rewrite this later,
782 // but for now the most simple patch is to use a new thing : _base_plugins
783 $this->_compile_obj->_plugins = $this->_base_plugins;
784
785 $output = $this->_compile_obj->_compile_file($file_contents);
786
787 $f = fopen($this->compile_dir.'c_'.$name, "w");
788 fwrite($f, $output);
789 fclose($f);
790
791 ob_start();
792 eval(' ?>' . $output . '<?php ');
793 $output = ob_get_contents();
794 ob_end_clean();
795
796 // We're putting back used plugins before the inclusion
797 if ($include && isset($old_plugins))
798 {
799 $this->_plugins = $old_plugins;
800 $this->_compile_obj->_plugins = &$this->_plugins;
801 }
802
803 return $output;
804 }
805
806 function _run_modifier()
807 {
808 $arguments = func_get_args();
809 list($variable, $modifier, $php_function, $_map_array) = array_splice($arguments, 0, 4);
810 array_unshift($arguments, $variable);
811 if ($_map_array && is_array($variable))
812 {
813 foreach($variable as $key => $value)
814 {
815 if($php_function == "PHP")
816 {
817 $variable[$key] = call_user_func_array($modifier, $arguments);
818 }
819 else
820 {
821 $variable[$key] = call_user_func_array($this->_plugins["modifier"][$modifier], $arguments);
822 }
823 }
824 }
825 else
826 {
827 if($php_function == "PHP")
828 {
829 $variable = call_user_func_array($modifier, $arguments);
830 }
831 else
832 {
833 $variable = call_user_func_array($this->_plugins["modifier"][$modifier], $arguments);
834 }
835 }
836
837 return $variable;
838 }
839
840 function _run_insert($arguments)
841 {
842 if ($this->cache)
843 {
844 return $this->_sl_md5 . '{_run_insert ' . serialize((array)$arguments) . '}' . $this->_sl_md5;
845 }
846 else
847 {
848 if (!function_exists('insert_' . $arguments['name']))
849 {
850 throw new Template_Exception("function 'insert_" . $arguments['name'] . "' does not exist in 'insert'", $this);
851 }
852 if (isset($arguments['assign']))
853 {
854 $this->assign($arguments['assign'], call_user_func_array('insert_' . $arguments['name'], array((array)$arguments, $this)));
855 }
856 else
857 {
858 return call_user_func_array('insert_' . $arguments['name'], array((array)$arguments, $this));
859 }
860 }
861 }
862
863 function _get_dir($dir, $id = null)
864 {
865 if (empty($dir))
866 {
867 $dir = '.';
868 }
869 if (substr($dir, -1) != DIRECTORY_SEPARATOR)
870 {
871 $dir .= DIRECTORY_SEPARATOR;
872 }
873 if (!empty($id))
874 {
875 $_args = explode('|', $id);
876 if (count($_args) == 1 && empty($_args[0]))
877 {
878 return $dir;
879 }
880 foreach($_args as $value)
881 {
882 $dir .= $value.DIRECTORY_SEPARATOR;
883 }
884 }
885 return $dir;
886 }
887
888 function _get_plugin_dir($plugin_name)
889 {
890 static $_path_array = null;
891
892 $plugin_dir_path = "";
893 $_plugin_dir_list = is_array($this->plugins_dir) ? $this->plugins_dir : (array)$this->plugins_dir;
894 foreach ($_plugin_dir_list as $_plugin_dir)
895 {
896 if (!preg_match("/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/", $_plugin_dir))
897 {
898 // path is relative
899 if (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . $_plugin_dir . DIRECTORY_SEPARATOR . $plugin_name))
900 {
901 $plugin_dir_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . $_plugin_dir . DIRECTORY_SEPARATOR;
902 break;
903 }
904 }
905 else
906 {
907 // path is absolute
908 if(!isset($_path_array))
909 {
910 $_ini_include_path = ini_get('include_path');
911
912 if(strstr($_ini_include_path,';'))
913 {
914 // windows pathnames
915 $_path_array = explode(';',$_ini_include_path);
916 }
917 else
918 {
919 $_path_array = explode(':',$_ini_include_path);
920 }
921 }
922
923 if(!in_array($_plugin_dir,$_path_array))
924 {
925 array_unshift($_path_array,$_plugin_dir);
926 }
927
928 foreach ($_path_array as $_include_path)
929 {
930 if (file_exists($_include_path . DIRECTORY_SEPARATOR . $plugin_name))
931 {
932 $plugin_dir_path = $_include_path . DIRECTORY_SEPARATOR;
933 break 2;
934 }
935 }
936 }
937 }
938 return $plugin_dir_path;
939 }
940
941 // function _parse_resource_link($resource_link)
942 // {
943 // $stuffing = "file:/this/is/the/time_5-23.tpl";
944 // $stuffing_data = explode(":", $stuffing);
945 // preg_match_all('/(?:([0-9a-z._-]+))/i', $stuffing, $stuff);
946 // print_r($stuff);
947 // echo "<br>Path: " . str_replace($stuff[0][count($stuff[0]) - 1], "", $stuffing);
948 // echo "<br>Filename: " . $stuff[0][count($stuff[0]) - 1];
949 // }
950
951 function _build_dir($dir, $id)
952 {
953 if(!function_exists("template_build_dir"))
954 {
955 require_once(TEMPLATE_LITE_DIR . "internal/template.build_dir.php");
956 }
957 return template_build_dir($dir, $id, $this);
958 }
959
960 function _destroy_dir($file, $id, $dir)
961 {
962 if(!function_exists("template_destroy_dir"))
963 {
964 require_once(TEMPLATE_LITE_DIR . "internal/template.destroy_dir.php");
965 }
966 return template_destroy_dir($file, $id, $dir, $this);
967 }
968 }
969 ?>