1 <?php |
|
2 /** |
|
3 * Smarty Internal Plugin Compile Include |
|
4 * Compiles the {include} tag |
|
5 * |
|
6 * @package Smarty |
|
7 * @subpackage Compiler |
|
8 * @author Uwe Tews |
|
9 */ |
|
10 |
|
11 /** |
|
12 * Smarty Internal Plugin Compile Include Class |
|
13 * |
|
14 * @package Smarty |
|
15 * @subpackage Compiler |
|
16 */ |
|
17 class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase { |
|
18 /** |
|
19 * caching mode to create nocache code but no cache file |
|
20 */ |
|
21 const CACHING_NOCACHE_CODE = 9999; |
|
22 /** |
|
23 * Attribute definition: Overwrites base class. |
|
24 * |
|
25 * @var array |
|
26 * @see Smarty_Internal_CompileBase |
|
27 */ |
|
28 public $required_attributes = array('file'); |
|
29 /** |
|
30 * Attribute definition: Overwrites base class. |
|
31 * |
|
32 * @var array |
|
33 * @see Smarty_Internal_CompileBase |
|
34 */ |
|
35 public $shorttag_order = array('file'); |
|
36 /** |
|
37 * Attribute definition: Overwrites base class. |
|
38 * |
|
39 * @var array |
|
40 * @see Smarty_Internal_CompileBase |
|
41 */ |
|
42 public $option_flags = array('nocache', 'inline', 'caching'); |
|
43 /** |
|
44 * Attribute definition: Overwrites base class. |
|
45 * |
|
46 * @var array |
|
47 * @see Smarty_Internal_CompileBase |
|
48 */ |
|
49 public $optional_attributes = array('_any'); |
|
50 |
|
51 /** |
|
52 * Compiles code for the {include} tag |
|
53 * |
|
54 * @param array $args array with attributes from parser |
|
55 * @param Smarty_Internal_SmartyTemplateCompiler $compiler compiler object |
|
56 * @param array $parameter array with compilation parameter |
|
57 * |
|
58 * @throws SmartyCompilerException |
|
59 * @return string compiled code |
|
60 */ |
|
61 public function compile($args, Smarty_Internal_SmartyTemplateCompiler $compiler, $parameter) { |
|
62 // check and get attributes |
|
63 $_attr = $this->getAttributes($compiler, $args); |
|
64 |
|
65 // save possible attributes |
|
66 $include_file = $_attr['file']; |
|
67 |
|
68 if (isset($_attr['assign'])) { |
|
69 // output will be stored in a smarty variable instead of being displayed |
|
70 $_assign = $_attr['assign']; |
|
71 } |
|
72 |
|
73 $_parent_scope = Smarty::SCOPE_LOCAL; |
|
74 if (isset($_attr['scope'])) { |
|
75 $_attr['scope'] = trim($_attr['scope'], "'\""); |
|
76 if ($_attr['scope'] == 'parent') { |
|
77 $_parent_scope = Smarty::SCOPE_PARENT; |
|
78 } elseif ($_attr['scope'] == 'root') { |
|
79 $_parent_scope = Smarty::SCOPE_ROOT; |
|
80 } elseif ($_attr['scope'] == 'global') { |
|
81 $_parent_scope = Smarty::SCOPE_GLOBAL; |
|
82 } |
|
83 } |
|
84 |
|
85 // assume caching is off |
|
86 $_caching = Smarty::CACHING_OFF; |
|
87 |
|
88 if ($_attr['nocache'] === true) { |
|
89 $compiler->tag_nocache = true; |
|
90 } |
|
91 |
|
92 $call_nocache = $compiler->tag_nocache || $compiler->nocache; |
|
93 |
|
94 // caching was on and {include} is not in nocache mode |
|
95 if ($compiler->template->caching && !$compiler->nocache && !$compiler->tag_nocache) { |
|
96 $_caching = self::CACHING_NOCACHE_CODE; |
|
97 } |
|
98 |
|
99 // flag if included template code should be merged into caller |
|
100 $merge_compiled_includes = ($compiler->smarty->merge_compiled_includes || ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes) || $_attr['inline'] === true) && !$compiler->template->source->recompiled; |
|
101 |
|
102 if ($merge_compiled_includes && $_attr['inline'] !== true) { |
|
103 // variable template name ? |
|
104 if ($compiler->has_variable_string || !((substr_count($include_file, '"') == 2 || substr_count($include_file, "'") == 2)) || substr_count($include_file, '(') != 0 || substr_count($include_file, '$_smarty_tpl->') != 0) { |
|
105 $merge_compiled_includes = false; |
|
106 if ($compiler->template->caching) { |
|
107 // must use individual cache file |
|
108 //$_attr['caching'] = 1; |
|
109 } |
|
110 if ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes && $_attr['inline'] !== true) { |
|
111 $compiler->trigger_template_error(' variable template file names not allow within {block} tags'); |
|
112 } |
|
113 } |
|
114 // variable compile_id? |
|
115 if (isset($_attr['compile_id'])) { |
|
116 if (!((substr_count($_attr['compile_id'], '"') == 2 || substr_count($_attr['compile_id'], "'") == 2 || is_numeric($_attr['compile_id']))) || substr_count($_attr['compile_id'], '(') != 0 || substr_count($_attr['compile_id'], '$_smarty_tpl->') != 0) { |
|
117 $merge_compiled_includes = false; |
|
118 if ($compiler->template->caching) { |
|
119 // must use individual cache file |
|
120 //$_attr['caching'] = 1; |
|
121 } |
|
122 if ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes && $_attr['inline'] !== true) { |
|
123 $compiler->trigger_template_error(' variable compile_id not allow within {block} tags'); |
|
124 } |
|
125 } |
|
126 } |
|
127 } |
|
128 |
|
129 /* |
|
130 * if the {include} tag provides individual parameter for caching or compile_id |
|
131 * the subtemplate must not be included into the core cache file and is treated like |
|
132 * a call in nocache mode. |
|
133 * |
|
134 */ |
|
135 if ($_attr['nocache'] !== true && $_attr['caching']) { |
|
136 $_caching = $_new_caching = (int)$_attr['caching']; |
|
137 $call_nocache = true; |
|
138 } else { |
|
139 $_new_caching = Smarty::CACHING_LIFETIME_CURRENT; |
|
140 } |
|
141 if (isset($_attr['cache_lifetime'])) { |
|
142 $_cache_lifetime = $_attr['cache_lifetime']; |
|
143 $call_nocache = true; |
|
144 $_caching = $_new_caching; |
|
145 } else { |
|
146 $_cache_lifetime = '$_smarty_tpl->cache_lifetime'; |
|
147 } |
|
148 if (isset($_attr['cache_id'])) { |
|
149 $_cache_id = $_attr['cache_id']; |
|
150 $call_nocache = true; |
|
151 $_caching = $_new_caching; |
|
152 } else { |
|
153 $_cache_id = '$_smarty_tpl->cache_id'; |
|
154 } |
|
155 if (isset($_attr['compile_id'])) { |
|
156 $_compile_id = $_attr['compile_id']; |
|
157 } else { |
|
158 $_compile_id = '$_smarty_tpl->compile_id'; |
|
159 } |
|
160 |
|
161 // if subtemplate will be called in nocache mode do not merge |
|
162 if ($compiler->template->caching && $call_nocache) { |
|
163 $merge_compiled_includes = false; |
|
164 } |
|
165 |
|
166 $has_compiled_template = false; |
|
167 if ($merge_compiled_includes) { |
|
168 if ($compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache) && $_caching != self::CACHING_NOCACHE_CODE) { |
|
169 // $merge_compiled_includes = false; |
|
170 if ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes) { |
|
171 $compiler->trigger_template_error(' invalid caching mode of subtemplate within {block} tags'); |
|
172 } |
|
173 } |
|
174 $c_id = isset($_attr['compile_id']) ? $_attr['compile_id'] : $compiler->template->compile_id; |
|
175 // we must observe different compile_id and caching |
|
176 $uid = sha1($c_id . ($_caching ? '--caching' : '--nocaching')); |
|
177 $tpl_name = null; |
|
178 |
|
179 /** @var Smarty_Internal_Template $_smarty_tpl |
|
180 * used in evaluated code |
|
181 */ |
|
182 $_smarty_tpl = $compiler->template; |
|
183 eval("\$tpl_name = $include_file;"); |
|
184 if (!isset($compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$uid])) { |
|
185 $compiler->smarty->allow_ambiguous_resources = true; |
|
186 $tpl = new $compiler->smarty->template_class ($tpl_name, $compiler->smarty, $compiler->template, $compiler->template->cache_id, $c_id, $_caching); |
|
187 // save unique function name |
|
188 $compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$uid]['func'] = $tpl->properties['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true)); |
|
189 if ($compiler->inheritance) { |
|
190 $tpl->compiler->inheritance = true; |
|
191 } |
|
192 // make sure whole chain gets compiled |
|
193 $tpl->mustCompile = true; |
|
194 if (!($tpl->source->uncompiled) && $tpl->source->exists) { |
|
195 $tpl->compiler->suppressTemplatePropertyHeader = true; |
|
196 $compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$uid]['nocache_hash'] = $tpl->properties['nocache_hash'] = str_replace(array('.', ','), '_', uniqid(rand(), true)); |
|
197 // get compiled code |
|
198 $compiled_code = Smarty_Internal_Extension_CodeFrame::createFunctionFrame($tpl, $tpl->compiler->compileTemplate($tpl, null, $compiler->parent_compiler)); |
|
199 unset($tpl->compiler); |
|
200 |
|
201 // remove header code |
|
202 $compiled_code = preg_replace("/(<\?php \/\*%%SmartyHeaderCode:{$tpl->properties['nocache_hash']}%%\*\/(.+?)\/\*\/%%SmartyHeaderCode%%\*\/\?>\n)/s", '', $compiled_code); |
|
203 if ($tpl->has_nocache_code) { |
|
204 // replace nocache_hash |
|
205 $compiled_code = str_replace("{$tpl->properties['nocache_hash']}", $compiler->template->properties['nocache_hash'], $compiled_code); |
|
206 $compiler->template->has_nocache_code = true; |
|
207 } |
|
208 $compiler->parent_compiler->mergedSubTemplatesCode[$tpl->properties['unifunc']] = $compiled_code; |
|
209 $has_compiled_template = true; |
|
210 if (!empty($tpl->required_plugins['compiled'])) { |
|
211 foreach ($tpl->required_plugins['compiled'] as $name => $callBack) { |
|
212 if (!isset($compiler->template->required_plugins['compiled'][$name])) { |
|
213 $compiler->template->required_plugins['compiled'][$name] = $callBack; |
|
214 } |
|
215 } |
|
216 } |
|
217 if (!empty($tpl->required_plugins['nocache'])) { |
|
218 foreach ($tpl->required_plugins['nocache'] as $name => $callBack) { |
|
219 if (!isset($compiler->template->required_plugins['nocache'][$name])) { |
|
220 $compiler->template->required_plugins['nocache'][$name] = $callBack; |
|
221 } |
|
222 } |
|
223 } |
|
224 unset ($tpl); |
|
225 } |
|
226 } else { |
|
227 $has_compiled_template = true; |
|
228 } |
|
229 } |
|
230 // delete {include} standard attributes |
|
231 unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['compile_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']); |
|
232 // remaining attributes must be assigned as smarty variable |
|
233 $_vars_nc = ''; |
|
234 if (!empty($_attr)) { |
|
235 if ($_parent_scope == Smarty::SCOPE_LOCAL) { |
|
236 $_pairs = array(); |
|
237 // create variables |
|
238 foreach ($_attr as $key => $value) { |
|
239 $_pairs[] = "'$key'=>$value"; |
|
240 $_vars_nc .= "\$_smarty_tpl->tpl_vars['$key'] = new Smarty_Variable($value);\n"; |
|
241 } |
|
242 $_vars = 'array(' . join(',', $_pairs) . ')'; |
|
243 } else { |
|
244 $compiler->trigger_template_error('variable passing not allowed in parent/global scope', $compiler->lex->taglineno); |
|
245 } |
|
246 } else { |
|
247 $_vars = 'array()'; |
|
248 } |
|
249 $update_compile_id = $compiler->template->caching && !$compiler->tag_nocache && !$compiler->nocache && $_compile_id != '$_smarty_tpl->compile_id'; |
|
250 if ($has_compiled_template && !$call_nocache) { |
|
251 // if ($has_compiled_template && !$compiler->tag_nocache && !$compiler->nocache) { |
|
252 // never call inline templates in nocache mode |
|
253 //$compiler->suppressNocacheProcessing = true; |
|
254 $_hash = $compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$uid]['nocache_hash']; |
|
255 $_output = "<?php /* Call merged included template \"" . $tpl_name . "\" */\n"; |
|
256 if ($update_compile_id) { |
|
257 $_output .= $compiler->makeNocacheCode("\$_compile_id_save[] = \$_smarty_tpl->compile_id;\n\$_smarty_tpl->compile_id = {$_compile_id};\n"); |
|
258 } |
|
259 if (!empty($_vars_nc) && $_caching == 9999 && $_smarty_tpl->caching) { |
|
260 //$compiler->suppressNocacheProcessing = false; |
|
261 $_output .= substr($compiler->processNocacheCode('<?php ' . $_vars_nc . "?>\n", true), 6, -3); |
|
262 //$compiler->suppressNocacheProcessing = true; |
|
263 } |
|
264 if (isset($_assign)) { |
|
265 $_output .= " \$_smarty_tpl->tpl_vars[$_assign] = new Smarty_Variable(\$_smarty_tpl->getInlineSubTemplate({$include_file}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_parent_scope}, '{$_hash}', '{$compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$uid]['func']}'));\n"; |
|
266 } else { |
|
267 $_output .= "echo \$_smarty_tpl->getInlineSubTemplate({$include_file}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_parent_scope}, '{$_hash}', '{$compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$uid]['func']}');\n"; |
|
268 } |
|
269 if ($update_compile_id) { |
|
270 $_output .= $compiler->makeNocacheCode("\$_smarty_tpl->compile_id = array_pop(\$_compile_id_save);\n"); |
|
271 } |
|
272 $_output .= "/* End of included template \"" . $tpl_name . "\" */?>\n"; |
|
273 |
|
274 return $_output; |
|
275 } |
|
276 |
|
277 if ($call_nocache) { |
|
278 $compiler->tag_nocache = true; |
|
279 } |
|
280 $_output = "<?php "; |
|
281 if ($update_compile_id) { |
|
282 $_output .= "\$_compile_id_save[] = \$_smarty_tpl->compile_id;\n\$_smarty_tpl->compile_id = {$_compile_id};\n"; |
|
283 } |
|
284 // was there an assign attribute |
|
285 if (isset($_assign)) { |
|
286 $_output .= "\$_smarty_tpl->tpl_vars[$_assign] = new Smarty_Variable(\$_smarty_tpl->getSubTemplate ($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope));\n"; |
|
287 } else { |
|
288 $_output .= "echo \$_smarty_tpl->getSubTemplate ($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope);\n"; |
|
289 } |
|
290 if ($update_compile_id) { |
|
291 $_output .= "\$_smarty_tpl->compile_id = array_pop(\$_compile_id_save);\n"; |
|
292 } |
|
293 $_output .= "?>\n"; |
|
294 return $_output; |
|
295 } |
|
296 } |
|