1 : <?php
2 : /**
3 : * Smarty Internal Plugin Smarty Template Base
4 : *
5 : * This file contains the basic shared methodes for template handling
6 : *
7 : * @package Smarty
8 : * @subpackage Template
9 : * @author Uwe Tews
10 : */
11 :
12 : /**
13 : * Class with shared template methodes
14 : *
15 : * @package Smarty
16 : * @subpackage Template
17 : */
18 : abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data {
19 :
20 : /**
21 : * fetches a rendered Smarty template
22 : *
23 : * @param string $template the resource handle of the template file or template object
24 : * @param mixed $cache_id cache id to be used with this template
25 : * @param mixed $compile_id compile id to be used with this template
26 : * @param object $parent next higher level of Smarty variables
27 : * @param bool $display true: display, false: fetch
28 : * @param bool $merge_tpl_vars if true parent template variables merged in to local scope
29 : * @param bool $no_output_filter if true do not run output filter
30 : * @return string rendered template output
31 : */
32 : public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null, $display = false, $merge_tpl_vars = true, $no_output_filter = false)
33 : {
34 0 : if ($template === null && $this instanceof $this->template_class) {
35 0 : $template = $this;
36 0 : }
37 0 : if (!empty($cache_id) && is_object($cache_id)) {
38 0 : $parent = $cache_id;
39 0 : $cache_id = null;
40 0 : }
41 0 : if ($parent === null && ($this instanceof Smarty || is_string($template))) {
42 0 : $parent = $this;
43 0 : }
44 : // create template object if necessary
45 0 : $_template = ($template instanceof $this->template_class)
46 0 : ? $template
47 0 : : $this->smarty->createTemplate($template, $cache_id, $compile_id, $parent, false);
48 : // if called by Smarty object make sure we use current caching status
49 0 : if ($this instanceof Smarty) {
50 0 : $_template->caching = $this->caching;
51 0 : }
52 : // merge all variable scopes into template
53 0 : if ($merge_tpl_vars) {
54 : // save local variables
55 0 : $save_tpl_vars = $_template->tpl_vars;
56 0 : $save_config_vars = $_template->config_vars;
57 0 : $ptr_array = array($_template);
58 0 : $ptr = $_template;
59 0 : while (isset($ptr->parent)) {
60 0 : $ptr_array[] = $ptr = $ptr->parent;
61 0 : }
62 0 : $ptr_array = array_reverse($ptr_array);
63 0 : $parent_ptr = reset($ptr_array);
64 0 : $tpl_vars = $parent_ptr->tpl_vars;
65 0 : $config_vars = $parent_ptr->config_vars;
66 0 : while ($parent_ptr = next($ptr_array)) {
67 0 : if (!empty($parent_ptr->tpl_vars)) {
68 0 : $tpl_vars = array_merge($tpl_vars, $parent_ptr->tpl_vars);
69 0 : }
70 0 : if (!empty($parent_ptr->config_vars)) {
71 0 : $config_vars = array_merge($config_vars, $parent_ptr->config_vars);
72 0 : }
73 0 : }
74 0 : if (!empty(Smarty::$global_tpl_vars)) {
75 0 : $tpl_vars = array_merge(Smarty::$global_tpl_vars, $tpl_vars);
76 0 : }
77 0 : $_template->tpl_vars = $tpl_vars;
78 0 : $_template->config_vars = $config_vars;
79 0 : }
80 : // dummy local smarty variable
81 0 : if (!isset($_template->tpl_vars['smarty'])) {
82 0 : $_template->tpl_vars['smarty'] = new Smarty_Variable;
83 0 : }
84 0 : if (isset($this->smarty->error_reporting)) {
85 0 : $_smarty_old_error_level = error_reporting($this->smarty->error_reporting);
86 0 : }
87 : // check URL debugging control
88 0 : if (!$this->smarty->debugging && $this->smarty->debugging_ctrl == 'URL') {
89 0 : if (isset($_SERVER['QUERY_STRING'])) {
90 0 : $_query_string = $_SERVER['QUERY_STRING'];
91 0 : } else {
92 0 : $_query_string = '';
93 : }
94 0 : if (false !== strpos($_query_string, $this->smarty->smarty_debug_id)) {
95 0 : if (false !== strpos($_query_string, $this->smarty->smarty_debug_id . '=on')) {
96 : // enable debugging for this browser session
97 0 : setcookie('SMARTY_DEBUG', true);
98 0 : $this->smarty->debugging = true;
99 0 : } elseif (false !== strpos($_query_string, $this->smarty->smarty_debug_id . '=off')) {
100 : // disable debugging for this browser session
101 0 : setcookie('SMARTY_DEBUG', false);
102 0 : $this->smarty->debugging = false;
103 0 : } else {
104 : // enable debugging for this page
105 0 : $this->smarty->debugging = true;
106 : }
107 0 : } else {
108 0 : if (isset($_COOKIE['SMARTY_DEBUG'])) {
109 0 : $this->smarty->debugging = true;
110 0 : }
111 : }
112 0 : }
113 : // must reset merge template date
114 0 : $_template->smarty->merged_templates_func = array();
115 : // get rendered template
116 : // disable caching for evaluated code
117 0 : if ($_template->source->recompiled) {
118 0 : $_template->caching = false;
119 0 : }
120 : // checks if template exists
121 0 : if (!$_template->source->exists) {
122 0 : if ($_template->parent instanceof Smarty_Internal_Template) {
123 0 : $parent_resource = " in '{$_template->parent->template_resource}'";
124 0 : } else {
125 0 : $parent_resource = '';
126 : }
127 0 : throw new SmartyException("Unable to load template {$_template->source->type} '{$_template->source->name}'{$parent_resource}");
128 : }
129 : // read from cache or render
130 0 : if (!($_template->caching == Smarty::CACHING_LIFETIME_CURRENT || $_template->caching == Smarty::CACHING_LIFETIME_SAVED) || !$_template->cached->valid) {
131 : // render template (not loaded and not in cache)
132 0 : if (!$_template->source->uncompiled) {
133 0 : $_smarty_tpl = $_template;
134 0 : if ($_template->source->recompiled) {
135 0 : if ($this->smarty->debugging) {
136 0 : Smarty_Internal_Debug::start_compile($_template);
137 0 : }
138 0 : $code = $_template->compiler->compileTemplate($_template);
139 0 : if ($this->smarty->debugging) {
140 0 : Smarty_Internal_Debug::end_compile($_template);
141 0 : }
142 0 : if ($this->smarty->debugging) {
143 0 : Smarty_Internal_Debug::start_render($_template);
144 0 : }
145 : try {
146 0 : ob_start();
147 0 : eval("?>" . $code);
148 0 : unset($code);
149 0 : } catch (Exception $e) {
150 0 : ob_get_clean();
151 0 : throw $e;
152 : }
153 0 : } else {
154 0 : if (!$_template->compiled->exists || ($_template->smarty->force_compile && !$_template->compiled->isCompiled)) {
155 0 : $_template->compileTemplateSource();
156 0 : }
157 0 : if ($this->smarty->debugging) {
158 0 : Smarty_Internal_Debug::start_render($_template);
159 0 : }
160 0 : if (!$_template->compiled->loaded) {
161 0 : include($_template->compiled->filepath);
162 0 : if ($_template->mustCompile) {
163 : // recompile and load again
164 0 : $_template->compileTemplateSource();
165 0 : include($_template->compiled->filepath);
166 0 : }
167 0 : $_template->compiled->loaded = true;
168 0 : } else {
169 0 : $_template->decodeProperties($_template->compiled->_properties, false);
170 : }
171 : try {
172 0 : ob_start();
173 0 : if (empty($_template->properties['unifunc']) || !is_callable($_template->properties['unifunc'])) {
174 0 : throw new SmartyException("Invalid compiled template for '{$_template->template_resource}'");
175 : }
176 0 : $_template->properties['unifunc']($_template);
177 0 : if (isset($_template->_capture_stack[0])) {
178 0 : $_template->capture_error();
179 0 : }
180 0 : } catch (Exception $e) {
181 0 : ob_get_clean();
182 0 : throw $e;
183 : }
184 : }
185 0 : } else {
186 0 : if ($_template->source->uncompiled) {
187 0 : if ($this->smarty->debugging) {
188 0 : Smarty_Internal_Debug::start_render($_template);
189 0 : }
190 : try {
191 0 : ob_start();
192 0 : $_template->source->renderUncompiled($_template);
193 0 : } catch (Exception $e) {
194 0 : ob_get_clean();
195 0 : throw $e;
196 : }
197 0 : } else {
198 0 : throw new SmartyException("Resource '$_template->source->type' must have 'renderUncompiled' method");
199 : }
200 : }
201 0 : $_output = ob_get_clean();
202 0 : if (!$_template->source->recompiled && empty($_template->properties['file_dependency'][$_template->source->uid])) {
203 0 : $_template->properties['file_dependency'][$_template->source->uid] = array($_template->source->filepath, $_template->source->timestamp, $_template->source->type);
204 0 : }
205 0 : if ($_template->parent instanceof Smarty_Internal_Template) {
206 0 : $_template->parent->properties['file_dependency'] = array_merge($_template->parent->properties['file_dependency'], $_template->properties['file_dependency']);
207 0 : foreach ($_template->required_plugins as $code => $tmp1) {
208 0 : foreach ($tmp1 as $name => $tmp) {
209 0 : foreach ($tmp as $type => $data) {
210 0 : $_template->parent->required_plugins[$code][$name][$type] = $data;
211 0 : }
212 0 : }
213 0 : }
214 0 : }
215 0 : if ($this->smarty->debugging) {
216 0 : Smarty_Internal_Debug::end_render($_template);
217 0 : }
218 : // write to cache when nessecary
219 0 : if (!$_template->source->recompiled && ($_template->caching == Smarty::CACHING_LIFETIME_SAVED || $_template->caching == Smarty::CACHING_LIFETIME_CURRENT)) {
220 0 : if ($this->smarty->debugging) {
221 0 : Smarty_Internal_Debug::start_cache($_template);
222 0 : }
223 0 : $_template->properties['has_nocache_code'] = false;
224 : // get text between non-cached items
225 0 : $cache_split = preg_split("!/\*%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*/!s", $_output);
226 : // get non-cached items
227 0 : preg_match_all("!/\*%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*/!s", $_output, $cache_parts);
228 0 : $output = '';
229 : // loop over items, stitch back together
230 0 : foreach ($cache_split as $curr_idx => $curr_split) {
231 : // escape PHP tags in template content
232 0 : $output .= preg_replace('/(<%|%>|<\?php|<\?|\?>)/', '<?php echo \'$1\'; ?>', $curr_split);
233 0 : if (isset($cache_parts[0][$curr_idx])) {
234 0 : $_template->properties['has_nocache_code'] = true;
235 : // remove nocache tags from cache output
236 0 : $output .= preg_replace("!/\*/?%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*/!", '', $cache_parts[0][$curr_idx]);
237 0 : }
238 0 : }
239 0 : if (!$no_output_filter && (isset($this->smarty->autoload_filters['output']) || isset($this->smarty->registered_filters['output']))) {
240 0 : $output = Smarty_Internal_Filter_Handler::runFilter('output', $output, $_template);
241 0 : }
242 : // rendering (must be done before writing cache file because of {function} nocache handling)
243 0 : $_smarty_tpl = $_template;
244 : try {
245 0 : ob_start();
246 0 : eval("?>" . $output);
247 0 : $_output = ob_get_clean();
248 0 : } catch (Exception $e) {
249 0 : ob_get_clean();
250 0 : throw $e;
251 : }
252 : // write cache file content
253 0 : $_template->writeCachedContent($output);
254 0 : if ($this->smarty->debugging) {
255 0 : Smarty_Internal_Debug::end_cache($_template);
256 0 : }
257 0 : } else {
258 : // var_dump('renderTemplate', $_template->has_nocache_code, $_template->template_resource, $_template->properties['nocache_hash'], $_template->parent->properties['nocache_hash'], $_output);
259 0 : if (!empty($_template->properties['nocache_hash']) && !empty($_template->parent->properties['nocache_hash'])) {
260 : // replace nocache_hash
261 0 : $_output = preg_replace("/{$_template->properties['nocache_hash']}/", $_template->parent->properties['nocache_hash'], $_output);
262 0 : $_template->parent->has_nocache_code = $_template->parent->has_nocache_code || $_template->has_nocache_code;
263 0 : }
264 : }
265 0 : } else {
266 0 : if ($this->smarty->debugging) {
267 0 : Smarty_Internal_Debug::start_cache($_template);
268 0 : }
269 : try {
270 0 : ob_start();
271 0 : $_template->properties['unifunc']($_template);
272 0 : if (isset($_template->_capture_stack[0])) {
273 0 : $_template->capture_error();
274 0 : }
275 0 : $_output = ob_get_clean();
276 0 : } catch (Exception $e) {
277 0 : ob_get_clean();
278 0 : throw $e;
279 : }
280 0 : if ($this->smarty->debugging) {
281 0 : Smarty_Internal_Debug::end_cache($_template);
282 0 : }
283 : }
284 0 : if ((!$this->caching || $_template->source->recompiled) && !$no_output_filter && (isset($this->smarty->autoload_filters['output']) || isset($this->smarty->registered_filters['output']))) {
285 0 : $_output = Smarty_Internal_Filter_Handler::runFilter('output', $_output, $_template);
286 0 : }
287 0 : if (isset($this->error_reporting)) {
288 0 : error_reporting($_smarty_old_error_level);
289 0 : }
290 : // display or fetch
291 0 : if ($display) {
292 0 : if ($this->caching && $this->cache_modified_check) {
293 0 : $_isCached = $_template->isCached() && !$_template->has_nocache_code;
294 0 : $_last_modified_date = @substr($_SERVER['HTTP_IF_MODIFIED_SINCE'], 0, strpos($_SERVER['HTTP_IF_MODIFIED_SINCE'], 'GMT') + 3);
295 0 : if ($_isCached && $_template->cached->timestamp <= strtotime($_last_modified_date)) {
296 : switch (PHP_SAPI) {
297 0 : case 'cgi': // php-cgi < 5.3
298 0 : case 'cgi-fcgi': // php-cgi >= 5.3
299 0 : case 'fpm-fcgi': // php-fpm >= 5.3.3
300 0 : header('Status: 304 Not Modified');
301 0 : break;
302 :
303 0 : case 'cli':
304 0 : if (/* ^phpunit */!empty($_SERVER['SMARTY_PHPUNIT_DISABLE_HEADERS'])/* phpunit$ */) {
305 0 : $_SERVER['SMARTY_PHPUNIT_HEADERS'][] = '304 Not Modified';
306 0 : }
307 0 : break;
308 :
309 0 : default:
310 0 : header('HTTP/1.1 304 Not Modified');
311 0 : break;
312 0 : }
313 0 : } else {
314 : switch (PHP_SAPI) {
315 0 : case 'cli':
316 0 : if (/* ^phpunit */!empty($_SERVER['SMARTY_PHPUNIT_DISABLE_HEADERS'])/* phpunit$ */) {
317 0 : $_SERVER['SMARTY_PHPUNIT_HEADERS'][] = 'Last-Modified: ' . gmdate('D, d M Y H:i:s', $_template->cached->timestamp) . ' GMT';
318 0 : }
319 0 : break;
320 :
321 0 : default:
322 0 : header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $_template->cached->timestamp) . ' GMT');
323 0 : break;
324 0 : }
325 0 : echo $_output;
326 : }
327 0 : } else {
328 0 : echo $_output;
329 : }
330 : // debug output
331 0 : if ($this->smarty->debugging) {
332 0 : Smarty_Internal_Debug::display_debug($this);
333 0 : }
334 0 : if ($merge_tpl_vars) {
335 : // restore local variables
336 0 : $_template->tpl_vars = $save_tpl_vars;
337 0 : $_template->config_vars = $save_config_vars;
338 0 : }
339 0 : return;
340 : } else {
341 0 : if ($merge_tpl_vars) {
342 : // restore local variables
343 0 : $_template->tpl_vars = $save_tpl_vars;
344 0 : $_template->config_vars = $save_config_vars;
345 0 : }
346 : // return fetched content
347 0 : return $_output;
348 : }
349 : }
350 :
351 : /**
352 : * displays a Smarty template
353 : *
354 : * @param string $template the resource handle of the template file or template object
355 : * @param mixed $cache_id cache id to be used with this template
356 : * @param mixed $compile_id compile id to be used with this template
357 : * @param object $parent next higher level of Smarty variables
358 : */
359 : public function display($template = null, $cache_id = null, $compile_id = null, $parent = null)
360 : {
361 : // display template
362 0 : $this->fetch($template, $cache_id, $compile_id, $parent, true);
363 0 : }
364 :
365 : /**
366 : * test if cache is valid
367 : *
368 : * @param string|object $template the resource handle of the template file or template object
369 : * @param mixed $cache_id cache id to be used with this template
370 : * @param mixed $compile_id compile id to be used with this template
371 : * @param object $parent next higher level of Smarty variables
372 : * @return boolean cache status
373 : */
374 : public function isCached($template = null, $cache_id = null, $compile_id = null, $parent = null)
375 : {
376 0 : if ($template === null && $this instanceof $this->template_class) {
377 0 : return $this->cached->valid;
378 : }
379 0 : if (!($template instanceof $this->template_class)) {
380 0 : if ($parent === null) {
381 0 : $parent = $this;
382 0 : }
383 0 : $template = $this->smarty->createTemplate($template, $cache_id, $compile_id, $parent, false);
384 0 : }
385 : // return cache status of template
386 0 : return $template->cached->valid;
387 : }
388 :
389 : /**
390 : * creates a data object
391 : *
392 : * @param object $parent next higher level of Smarty variables
393 : * @returns Smarty_Data data object
394 : */
395 : public function createData($parent = null)
396 : {
397 0 : return new Smarty_Data($parent, $this);
398 : }
399 :
400 : /**
401 : * Registers plugin to be used in templates
402 : *
403 : * @param string $type plugin type
404 : * @param string $tag name of template tag
405 : * @param callback $callback PHP callback to register
406 : * @param boolean $cacheable if true (default) this fuction is cachable
407 : * @param array $cache_attr caching attributes if any
408 : * @throws SmartyException when the plugin tag is invalid
409 : */
410 : public function registerPlugin($type, $tag, $callback, $cacheable = true, $cache_attr = null)
411 : {
412 1 : if (isset($this->smarty->registered_plugins[$type][$tag])) {
413 0 : throw new SmartyException("Plugin tag \"{$tag}\" already registered");
414 1 : } elseif (!is_callable($callback)) {
415 0 : throw new SmartyException("Plugin \"{$tag}\" not callable");
416 : } else {
417 1 : $this->smarty->registered_plugins[$type][$tag] = array($callback, (bool) $cacheable, (array) $cache_attr);
418 : }
419 1 : }
420 :
421 : /**
422 : * Unregister Plugin
423 : *
424 : * @param string $type of plugin
425 : * @param string $tag name of plugin
426 : */
427 : public function unregisterPlugin($type, $tag)
428 : {
429 0 : if (isset($this->smarty->registered_plugins[$type][$tag])) {
430 0 : unset($this->smarty->registered_plugins[$type][$tag]);
431 0 : }
432 0 : }
433 :
434 : /**
435 : * Registers a resource to fetch a template
436 : *
437 : * @param string $type name of resource type
438 : * @param Smarty_Resource|array $callback or instance of Smarty_Resource, or array of callbacks to handle resource (deprecated)
439 : */
440 : public function registerResource($type, $callback)
441 : {
442 0 : $this->smarty->registered_resources[$type] = $callback instanceof Smarty_Resource ? $callback : array($callback, false);
443 0 : }
444 :
445 : /**
446 : * Unregisters a resource
447 : *
448 : * @param string $type name of resource type
449 : */
450 : public function unregisterResource($type)
451 : {
452 0 : if (isset($this->smarty->registered_resources[$type])) {
453 0 : unset($this->smarty->registered_resources[$type]);
454 0 : }
455 0 : }
456 :
457 : /**
458 : * Registers a cache resource to cache a template's output
459 : *
460 : * @param string $type name of cache resource type
461 : * @param Smarty_CacheResource $callback instance of Smarty_CacheResource to handle output caching
462 : */
463 : public function registerCacheResource($type, Smarty_CacheResource $callback)
464 : {
465 0 : $this->smarty->registered_cache_resources[$type] = $callback;
466 0 : }
467 :
468 : /**
469 : * Unregisters a cache resource
470 : *
471 : * @param string $type name of cache resource type
472 : */
473 : public function unregisterCacheResource($type)
474 : {
475 0 : if (isset($this->smarty->registered_cache_resources[$type])) {
476 0 : unset($this->smarty->registered_cache_resources[$type]);
477 0 : }
478 0 : }
479 :
480 : /**
481 : * Registers object to be used in templates
482 : *
483 : * @param string $object name of template object
484 : * @param object $object_impl the referenced PHP object to register
485 : * @param array $allowed list of allowed methods (empty = all)
486 : * @param boolean $smarty_args smarty argument format, else traditional
487 : * @param array $block_methods list of block-methods
488 : * @param array $block_functs list of methods that are block format
489 : * @throws SmartyException if any of the methods in $allowed or $block_methods are invalid
490 : */
491 : public function registerObject($object_name, $object_impl, $allowed = array(), $smarty_args = true, $block_methods = array())
492 : {
493 : // test if allowed methodes callable
494 0 : if (!empty($allowed)) {
495 0 : foreach ((array) $allowed as $method) {
496 0 : if (!is_callable(array($object_impl, $method))) {
497 0 : throw new SmartyException("Undefined method '$method' in registered object");
498 : }
499 0 : }
500 0 : }
501 : // test if block methodes callable
502 0 : if (!empty($block_methods)) {
503 0 : foreach ((array) $block_methods as $method) {
504 0 : if (!is_callable(array($object_impl, $method))) {
505 0 : throw new SmartyException("Undefined method '$method' in registered object");
506 : }
507 0 : }
508 0 : }
509 : // register the object
510 0 : $this->smarty->registered_objects[$object_name] =
511 0 : array($object_impl, (array) $allowed, (boolean) $smarty_args, (array) $block_methods);
512 0 : }
513 :
514 : /**
515 : * return a reference to a registered object
516 : *
517 : * @param string $name object name
518 : * @return object
519 : * @throws SmartyException if no such object is found
520 : */
521 : public function getRegisteredObject($name)
522 : {
523 0 : if (!isset($this->smarty->registered_objects[$name])) {
524 0 : throw new SmartyException("'$name' is not a registered object");
525 : }
526 0 : if (!is_object($this->smarty->registered_objects[$name][0])) {
527 0 : throw new SmartyException("registered '$name' is not an object");
528 : }
529 0 : return $this->smarty->registered_objects[$name][0];
530 : }
531 :
532 : /**
533 : * unregister an object
534 : *
535 : * @param string $name object name
536 : * @throws SmartyException if no such object is found
537 : */
538 : public function unregisterObject($name)
539 : {
540 0 : unset($this->smarty->registered_objects[$name]);
541 0 : return;
542 : }
543 :
544 : /**
545 : * Registers static classes to be used in templates
546 : *
547 : * @param string $class name of template class
548 : * @param string $class_impl the referenced PHP class to register
549 : * @throws SmartyException if $class_impl does not refer to an existing class
550 : */
551 : public function registerClass($class_name, $class_impl)
552 : {
553 : // test if exists
554 0 : if (!class_exists($class_impl)) {
555 0 : throw new SmartyException("Undefined class '$class_impl' in register template class");
556 : }
557 : // register the class
558 0 : $this->smarty->registered_classes[$class_name] = $class_impl;
559 0 : }
560 :
561 : /**
562 : * Registers a default plugin handler
563 : *
564 : * @param callable $callback class/method name
565 : * @throws SmartyException if $callback is not callable
566 : */
567 : public function registerDefaultPluginHandler($callback)
568 : {
569 0 : if (is_callable($callback)) {
570 0 : $this->smarty->default_plugin_handler_func = $callback;
571 0 : } else {
572 0 : throw new SmartyException("Default plugin handler '$callback' not callable");
573 : }
574 0 : }
575 :
576 : /**
577 : * Registers a default template handler
578 : *
579 : * @param callable $callback class/method name
580 : * @throws SmartyException if $callback is not callable
581 : */
582 : public function registerDefaultTemplateHandler($callback)
583 : {
584 0 : if (is_callable($callback)) {
585 0 : $this->smarty->default_template_handler_func = $callback;
586 0 : } else {
587 0 : throw new SmartyException("Default template handler '$callback' not callable");
588 : }
589 0 : }
590 :
591 : /**
592 : * Registers a default template handler
593 : *
594 : * @param callable $callback class/method name
595 : * @throws SmartyException if $callback is not callable
596 : */
597 : public function registerDefaultConfigHandler($callback)
598 : {
599 0 : if (is_callable($callback)) {
600 0 : $this->smarty->default_config_handler_func = $callback;
601 0 : } else {
602 0 : throw new SmartyException("Default config handler '$callback' not callable");
603 : }
604 0 : }
605 :
606 : /**
607 : * Registers a filter function
608 : *
609 : * @param string $type filter type
610 : * @param callback $callback
611 : */
612 : public function registerFilter($type, $callback)
613 : {
614 0 : $this->smarty->registered_filters[$type][$this->_get_filter_name($callback)] = $callback;
615 0 : }
616 :
617 : /**
618 : * Unregisters a filter function
619 : *
620 : * @param string $type filter type
621 : * @param callback $callback
622 : */
623 : public function unregisterFilter($type, $callback)
624 : {
625 0 : $name = $this->_get_filter_name($callback);
626 0 : if (isset($this->smarty->registered_filters[$type][$name])) {
627 0 : unset($this->smarty->registered_filters[$type][$name]);
628 0 : }
629 0 : }
630 :
631 : /**
632 : * Return internal filter name
633 : *
634 : * @param callback $function_name
635 : */
636 : public function _get_filter_name($function_name)
637 : {
638 0 : if (is_array($function_name)) {
639 0 : $_class_name = (is_object($function_name[0]) ?
640 0 : get_class($function_name[0]) : $function_name[0]);
641 0 : return $_class_name . '_' . $function_name[1];
642 : } else {
643 0 : return $function_name;
644 : }
645 : }
646 :
647 : /**
648 : * load a filter of specified type and name
649 : *
650 : * @param string $type filter type
651 : * @param string $name filter name
652 : * @return bool
653 : */
654 : public function loadFilter($type, $name)
655 : {
656 0 : $_plugin = "smarty_{$type}filter_{$name}";
657 0 : $_filter_name = $_plugin;
658 0 : if ($this->smarty->loadPlugin($_plugin)) {
659 0 : if (class_exists($_plugin, false)) {
660 0 : $_plugin = array($_plugin, 'execute');
661 0 : }
662 0 : if (is_callable($_plugin)) {
663 0 : $this->smarty->registered_filters[$type][$_filter_name] = $_plugin;
664 0 : return true;
665 : }
666 0 : }
667 0 : throw new SmartyException("{$type}filter \"{$name}\" not callable");
668 : return false;
669 : }
670 :
671 : /**
672 : * unload a filter of specified type and name
673 : *
674 : * @param string $type filter type
675 : * @param string $name filter name
676 : * @return bool
677 : */
678 : public function unloadFilter($type, $name)
679 : {
680 0 : $_filter_name = "smarty_{$type}filter_{$name}";
681 0 : if (isset($this->smarty->registered_filters[$type][$_filter_name])) {
682 0 : unset ($this->smarty->registered_filters[$type][$_filter_name]);
683 0 : return true;
684 : } else {
685 0 : return false;
686 : }
687 : }
688 :
689 : /**
690 : * preg_replace callback to convert camelcase getter/setter to underscore property names
691 : *
692 : * @param string $match match string
693 : * @return string replacemant
694 : */
695 : private function replaceCamelcase($match) {
696 0 : return "_" . strtolower($match[1]);
697 : }
698 :
699 : /**
700 : * Handle unknown class methods
701 : *
702 : * @param string $name unknown method-name
703 : * @param array $args argument array
704 : */
705 : public function __call($name, $args)
706 : {
707 0 : static $_prefixes = array('set' => true, 'get' => true);
708 0 : static $_resolved_property_name = array();
709 0 : static $_resolved_property_source = array();
710 :
711 : // method of Smarty object?
712 0 : if (method_exists($this->smarty, $name)) {
713 0 : return call_user_func_array(array($this->smarty, $name), $args);
714 : }
715 : // see if this is a set/get for a property
716 0 : $first3 = strtolower(substr($name, 0, 3));
717 0 : if (isset($_prefixes[$first3]) && isset($name[3]) && $name[3] !== '_') {
718 0 : if (isset($_resolved_property_name[$name])) {
719 0 : $property_name = $_resolved_property_name[$name];
720 0 : } else {
721 : // try to keep case correct for future PHP 6.0 case-sensitive class methods
722 : // lcfirst() not available < PHP 5.3.0, so improvise
723 0 : $property_name = strtolower(substr($name, 3, 1)) . substr($name, 4);
724 : // convert camel case to underscored name
725 0 : $property_name = preg_replace_callback('/([A-Z])/', array($this,'replaceCamelcase'), $property_name);
726 0 : $_resolved_property_name[$name] = $property_name;
727 : }
728 0 : if (isset($_resolved_property_source[$property_name])) {
729 0 : $_is_this = $_resolved_property_source[$property_name];
730 0 : } else {
731 0 : $_is_this = null;
732 0 : if (property_exists($this, $property_name)) {
733 0 : $_is_this = true;
734 0 : } else if (property_exists($this->smarty, $property_name)) {
735 0 : $_is_this = false;
736 0 : }
737 0 : $_resolved_property_source[$property_name] = $_is_this;
738 : }
739 0 : if ($_is_this) {
740 0 : if ($first3 == 'get')
741 0 : return $this->$property_name;
742 : else
743 0 : return $this->$property_name = $args[0];
744 0 : } else if ($_is_this === false) {
745 0 : if ($first3 == 'get')
746 0 : return $this->smarty->$property_name;
747 : else
748 0 : return $this->smarty->$property_name = $args[0];
749 : } else {
750 0 : throw new SmartyException("property '$property_name' does not exist.");
751 : return false;
752 : }
753 : }
754 : // must be unknown
755 0 : throw new SmartyException("Call of unknown method '$name'.");
756 : }
757 :
758 : }
759 :
|