Commit | Line | Data |
---|---|---|
2aa91ff2 S |
1 | <?php |
2 | /** | |
3 | * Smarty Internal Plugin Function Call Handler | |
4 | * | |
5 | * @package Smarty | |
6 | * @subpackage PluginsInternal | |
7 | * @author Uwe Tews | |
8 | */ | |
9 | ||
10 | /** | |
ccd27f54 S |
11 | * This class does handles template functions defined with the {function} tag missing in cache file. |
12 | * It can happen when the template function was called with the nocache option or within a nocache section. | |
13 | * The template function will be loaded from it's compiled template file, executed and added to the cache file | |
14 | * for later use. | |
2aa91ff2 S |
15 | * |
16 | * @package Smarty | |
17 | * @subpackage PluginsInternal | |
18 | */ | |
19 | class Smarty_Internal_Function_Call_Handler | |
20 | { | |
21 | /** | |
22 | * This function handles calls to template functions defined by {function} | |
23 | * It does create a PHP function at the first call | |
24 | * | |
25 | * @param string $_name template function name | |
ccd27f54 S |
26 | * @param Smarty_Internal_Template $_smarty_tpl |
27 | * @param string $_function PHP function name | |
2aa91ff2 | 28 | * @param array $_params Smarty variables passed as call parameter |
2aa91ff2 | 29 | * @param bool $_nocache nocache flag |
ccd27f54 S |
30 | * |
31 | * @return bool | |
2aa91ff2 | 32 | */ |
ccd27f54 | 33 | public static function call($_name, Smarty_Internal_Template $_smarty_tpl, $_function, $_params, $_nocache) |
2aa91ff2 | 34 | { |
ccd27f54 S |
35 | $funcParam = $_smarty_tpl->properties['tpl_function'][$_name]; |
36 | if (is_file($funcParam['compiled_filepath'])) { | |
37 | // read compiled file | |
38 | $code = file_get_contents($funcParam['compiled_filepath']); | |
39 | // grab template function | |
40 | if (preg_match("/\/\* {$_function} \*\/([\S\s]*?)\/\*\/ {$_function} \*\//", $code, $match)) { | |
41 | // grab source info from file dependency | |
42 | preg_match("/\s*'{$funcParam['uid']}'([\S\s]*?)\),/", $code, $match1); | |
43 | unset($code); | |
44 | $output = ''; | |
45 | // make PHP function known | |
46 | eval($match[0]); | |
47 | if (function_exists($_function)) { | |
48 | // search cache file template | |
49 | $tplPtr = $_smarty_tpl; | |
50 | while (!isset($tplPtr->cached) && isset($tplPtr->parent)) { | |
51 | $tplPtr = $tplPtr->parent; | |
52 | } | |
53 | // add template function code to cache file | |
54 | if (isset($tplPtr->cached)) { | |
55 | $cache = $tplPtr->cached; | |
56 | $content = $cache->read($tplPtr); | |
57 | if ($content) { | |
58 | // check if we must update file dependency | |
59 | if (!preg_match("/'{$funcParam['uid']}'([\S\s]*?)'nocache_hash'/", $content, $match2)) { | |
60 | $content = preg_replace("/('file_dependency'([\S\s]*?)\()/", "\\1{$match1[0]}", $content); | |
61 | } | |
62 | $cache->write($tplPtr, $content . "<?php " . $match[0] . "?>\n"); | |
63 | } | |
64 | } | |
65 | return true; | |
66 | } | |
2aa91ff2 | 67 | } |
2aa91ff2 | 68 | } |
ccd27f54 | 69 | return false; |
2aa91ff2 S |
70 | } |
71 | } |