1<?php
2/**
3 * Smarty Internal Plugin Compile Function
4 * Compiles the {function} {/function} tags
5 *
6 * @package    Smarty
7 * @subpackage Compiler
8 * @author     Uwe Tews
9 */
10
11/**
12 * Smarty Internal Plugin Compile Function Class
13 *
14 * @package    Smarty
15 * @subpackage Compiler
16 */
17class Smarty_Internal_Compile_Function extends Smarty_Internal_CompileBase
18{
19    /**
20     * Attribute definition: Overwrites base class.
21     *
22     * @var array
23     * @see Smarty_Internal_CompileBase
24     */
25    public $required_attributes = array('name');
26
27    /**
28     * Attribute definition: Overwrites base class.
29     *
30     * @var array
31     * @see Smarty_Internal_CompileBase
32     */
33    public $shorttag_order = array('name');
34
35    /**
36     * Attribute definition: Overwrites base class.
37     *
38     * @var array
39     * @see Smarty_Internal_CompileBase
40     */
41    public $optional_attributes = array('_any');
42
43    /**
44     * Compiles code for the {function} tag
45     *
46     * @param array                                 $args     array with attributes from parser
47     * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
48     *
49     * @return bool true
50     * @throws \SmartyCompilerException
51     */
52    public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
53    {
54        // check and get attributes
55        $_attr = $this->getAttributes($compiler, $args);
56        if ($_attr[ 'nocache' ] === true) {
57            $compiler->trigger_template_error('nocache option not allowed', null, true);
58        }
59        unset($_attr[ 'nocache' ]);
60        $_name = trim($_attr[ 'name' ], '\'"');
61        $compiler->parent_compiler->tpl_function[ $_name ] = array();
62        $save = array(
63            $_attr, $compiler->parser->current_buffer, $compiler->template->compiled->has_nocache_code,
64            $compiler->template->caching
65        );
66        $this->openTag($compiler, 'function', $save);
67        // Init temporary context
68        $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
69        $compiler->template->compiled->has_nocache_code = false;
70        $compiler->saveRequiredPlugins(true);
71        return true;
72    }
73}
74
75/**
76 * Smarty Internal Plugin Compile Functionclose Class
77 *
78 * @package    Smarty
79 * @subpackage Compiler
80 */
81class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
82{
83    /**
84     * Compiler object
85     *
86     * @var object
87     */
88    private $compiler = null;
89
90    /**
91     * Compiles code for the {/function} tag
92     *
93     * @param array                                        $args     array with attributes from parser
94     * @param object|\Smarty_Internal_TemplateCompilerBase $compiler compiler object
95     *
96     * @return bool true
97     */
98    public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
99    {
100        $this->compiler = $compiler;
101        $saved_data = $this->closeTag($compiler, array('function'));
102        $_attr = $saved_data[ 0 ];
103        $_name = trim($_attr[ 'name' ], '\'"');
104        $compiler->parent_compiler->tpl_function[ $_name ][ 'compiled_filepath' ] =
105            $compiler->parent_compiler->template->compiled->filepath;
106        $compiler->parent_compiler->tpl_function[ $_name ][ 'uid' ] = $compiler->template->source->uid;
107        $_parameter = $_attr;
108        unset($_parameter[ 'name' ]);
109        // default parameter
110        $_paramsArray = array();
111        foreach ($_parameter as $_key => $_value) {
112            if (is_int($_key)) {
113                $_paramsArray[] = "$_key=>$_value";
114            } else {
115                $_paramsArray[] = "'$_key'=>$_value";
116            }
117        }
118        if (!empty($_paramsArray)) {
119            $_params = 'array(' . implode(',', $_paramsArray) . ')';
120            $_paramsCode = "\$params = array_merge($_params, \$params);\n";
121        } else {
122            $_paramsCode = '';
123        }
124        $_functionCode = $compiler->parser->current_buffer;
125        // setup buffer for template function code
126        $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
127        $_funcName = "smarty_template_function_{$_name}_{$compiler->template->compiled->nocache_hash}";
128        $_funcNameCaching = $_funcName . '_nocache';
129        if ($compiler->template->compiled->has_nocache_code) {
130            $compiler->parent_compiler->tpl_function[ $_name ][ 'call_name_caching' ] = $_funcNameCaching;
131            $output = "<?php\n";
132            $output .= "/* {$_funcNameCaching} */\n";
133            $output .= "if (!function_exists('{$_funcNameCaching}')) {\n";
134            $output .= "function {$_funcNameCaching} (Smarty_Internal_Template \$_smarty_tpl,\$params) {\n";
135            $output .= "ob_start();\n";
136            $output .= $compiler->compileRequiredPlugins();
137            $output .= "\$_smarty_tpl->compiled->has_nocache_code = true;\n";
138            $output .= $_paramsCode;
139            $output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_Variable(\$value, \$_smarty_tpl->isRenderingCache);\n}\n";
140            $output .= "\$params = var_export(\$params, true);\n";
141            $output .= "echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php ";
142            $output .= "\\\$_smarty_tpl->smarty->ext->_tplFunction->saveTemplateVariables(\\\$_smarty_tpl, '{$_name}');\nforeach (\$params as \\\$key => \\\$value) {\n\\\$_smarty_tpl->tpl_vars[\\\$key] = new Smarty_Variable(\\\$value, \\\$_smarty_tpl->isRenderingCache);\n}\n?>";
143            $output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\";?>";
144            $compiler->parser->current_buffer->append_subtree(
145                $compiler->parser,
146                new Smarty_Internal_ParseTree_Tag(
147                    $compiler->parser,
148                    $output
149                )
150            );
151            $compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
152            $output = "<?php echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php ";
153            $output .= "\\\$_smarty_tpl->smarty->ext->_tplFunction->restoreTemplateVariables(\\\$_smarty_tpl, '{$_name}');?>\n";
154            $output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\";\n?>";
155            $output .= "<?php echo str_replace('{$compiler->template->compiled->nocache_hash}', \$_smarty_tpl->compiled->nocache_hash, ob_get_clean());\n";
156            $output .= "}\n}\n";
157            $output .= "/*/ {$_funcName}_nocache */\n\n";
158            $output .= "?>\n";
159            $compiler->parser->current_buffer->append_subtree(
160                $compiler->parser,
161                new Smarty_Internal_ParseTree_Tag(
162                    $compiler->parser,
163                    $output
164                )
165            );
166            $_functionCode = new Smarty_Internal_ParseTree_Tag(
167                $compiler->parser,
168                preg_replace_callback(
169                    "/((<\?php )?echo '\/\*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%\*\/([\S\s]*?)\/\*\/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%\*\/';(\?>\n)?)/",
170                    array($this, 'removeNocache'),
171                    $_functionCode->to_smarty_php($compiler->parser)
172                )
173            );
174        }
175        $compiler->parent_compiler->tpl_function[ $_name ][ 'call_name' ] = $_funcName;
176        $output = "<?php\n";
177        $output .= "/* {$_funcName} */\n";
178        $output .= "if (!function_exists('{$_funcName}')) {\n";
179        $output .= "function {$_funcName}(Smarty_Internal_Template \$_smarty_tpl,\$params) {\n";
180        $output .= $_paramsCode;
181        $output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_Variable(\$value, \$_smarty_tpl->isRenderingCache);\n}\n";
182        $output .= $compiler->compileCheckPlugins(array_merge($compiler->required_plugins[ 'compiled' ],
183            $compiler->required_plugins[ 'nocache' ]));
184        $output .= "?>\n";
185        $compiler->parser->current_buffer->append_subtree(
186            $compiler->parser,
187            new Smarty_Internal_ParseTree_Tag(
188                $compiler->parser,
189                $output
190            )
191        );
192        $compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
193        $output = "<?php\n}}\n";
194        $output .= "/*/ {$_funcName} */\n\n";
195        $output .= "?>\n";
196        $compiler->parser->current_buffer->append_subtree(
197            $compiler->parser,
198            new Smarty_Internal_ParseTree_Tag(
199                $compiler->parser,
200                $output
201            )
202        );
203        $compiler->parent_compiler->blockOrFunctionCode .= $compiler->parser->current_buffer->to_smarty_php($compiler->parser);
204        // restore old buffer
205        $compiler->parser->current_buffer = $saved_data[ 1 ];
206        // restore old status
207        $compiler->restoreRequiredPlugins();
208        $compiler->template->compiled->has_nocache_code = $saved_data[ 2 ];
209        $compiler->template->caching = $saved_data[ 3 ];
210        return true;
211    }
212
213    /**
214     * Remove nocache code
215     *
216     * @param $match
217     *
218     * @return string
219     */
220    public function removeNocache($match)
221    {
222        $code =
223            preg_replace(
224                "/((<\?php )?echo '\/\*%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/)|(\/\*\/%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/';(\?>\n)?)/",
225                '',
226                $match[ 0 ]
227            );
228        $code = str_replace(array('\\\'', '\\\\\''), array('\'', '\\\''), $code);
229        return $code;
230    }
231}
232