1 | <?php |
---|
2 | /** |
---|
3 | * Smarty Internal Plugin Templateparser Parsetrees |
---|
4 | * |
---|
5 | * These are classes to build parsetrees in the template parser |
---|
6 | * |
---|
7 | * @package Smarty |
---|
8 | * @subpackage Compiler |
---|
9 | * @author Thue Kristensen |
---|
10 | * @author Uwe Tews |
---|
11 | */ |
---|
12 | |
---|
13 | /** |
---|
14 | * @package Smarty |
---|
15 | * @subpackage Compiler |
---|
16 | * @ignore |
---|
17 | */ |
---|
18 | abstract class _smarty_parsetree { |
---|
19 | |
---|
20 | /** |
---|
21 | * Parser object |
---|
22 | * @var object |
---|
23 | */ |
---|
24 | public $parser; |
---|
25 | /** |
---|
26 | * Buffer content |
---|
27 | * @var mixed |
---|
28 | */ |
---|
29 | public $data; |
---|
30 | |
---|
31 | /** |
---|
32 | * Return buffer |
---|
33 | * |
---|
34 | * @return string buffer content |
---|
35 | */ |
---|
36 | abstract public function to_smarty_php(); |
---|
37 | |
---|
38 | } |
---|
39 | |
---|
40 | /** |
---|
41 | * A complete smarty tag. |
---|
42 | * |
---|
43 | * @package Smarty |
---|
44 | * @subpackage Compiler |
---|
45 | * @ignore |
---|
46 | */ |
---|
47 | class _smarty_tag extends _smarty_parsetree { |
---|
48 | |
---|
49 | /** |
---|
50 | * Saved block nesting level |
---|
51 | * @var int |
---|
52 | */ |
---|
53 | public $saved_block_nesting; |
---|
54 | |
---|
55 | /** |
---|
56 | * Create parse tree buffer for Smarty tag |
---|
57 | * |
---|
58 | * @param object $parser parser object |
---|
59 | * @param string $data content |
---|
60 | */ |
---|
61 | public function __construct($parser, $data) |
---|
62 | { |
---|
63 | $this->parser = $parser; |
---|
64 | $this->data = $data; |
---|
65 | $this->saved_block_nesting = $parser->block_nesting_level; |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * Return buffer content |
---|
70 | * |
---|
71 | * @return string content |
---|
72 | */ |
---|
73 | public function to_smarty_php() |
---|
74 | { |
---|
75 | return $this->data; |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * Return complied code that loads the evaluated outout of buffer content into a temporary variable |
---|
80 | * |
---|
81 | * @return string template code |
---|
82 | */ |
---|
83 | public function assign_to_var() |
---|
84 | { |
---|
85 | $var = sprintf('$_tmp%d', ++$this->parser->prefix_number); |
---|
86 | $this->parser->compiler->prefix_code[] = sprintf('<?php ob_start();?>%s<?php %s=ob_get_clean();?>', $this->data, $var); |
---|
87 | return $var; |
---|
88 | } |
---|
89 | |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Code fragment inside a tag. |
---|
94 | * |
---|
95 | * @package Smarty |
---|
96 | * @subpackage Compiler |
---|
97 | * @ignore |
---|
98 | */ |
---|
99 | class _smarty_code extends _smarty_parsetree { |
---|
100 | |
---|
101 | |
---|
102 | /** |
---|
103 | * Create parse tree buffer for code fragment |
---|
104 | * |
---|
105 | * @param object $parser parser object |
---|
106 | * @param string $data content |
---|
107 | */ |
---|
108 | public function __construct($parser, $data) |
---|
109 | { |
---|
110 | $this->parser = $parser; |
---|
111 | $this->data = $data; |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * Return buffer content in parentheses |
---|
116 | * |
---|
117 | * @return string content |
---|
118 | */ |
---|
119 | public function to_smarty_php() |
---|
120 | { |
---|
121 | return sprintf("(%s)", $this->data); |
---|
122 | } |
---|
123 | |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * Double quoted string inside a tag. |
---|
128 | * |
---|
129 | * @package Smarty |
---|
130 | * @subpackage Compiler |
---|
131 | * @ignore |
---|
132 | */ |
---|
133 | class _smarty_doublequoted extends _smarty_parsetree { |
---|
134 | |
---|
135 | /** |
---|
136 | * Create parse tree buffer for double quoted string subtrees |
---|
137 | * |
---|
138 | * @param object $parser parser object |
---|
139 | * @param _smarty_parsetree $subtree parsetree buffer |
---|
140 | */ |
---|
141 | public function __construct($parser, _smarty_parsetree $subtree) |
---|
142 | { |
---|
143 | $this->parser = $parser; |
---|
144 | $this->subtrees[] = $subtree; |
---|
145 | if ($subtree instanceof _smarty_tag) { |
---|
146 | $this->parser->block_nesting_level = count($this->parser->compiler->_tag_stack); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * Append buffer to subtree |
---|
152 | * |
---|
153 | * @param _smarty_parsetree $subtree parsetree buffer |
---|
154 | */ |
---|
155 | public function append_subtree(_smarty_parsetree $subtree) |
---|
156 | { |
---|
157 | $last_subtree = count($this->subtrees) - 1; |
---|
158 | if ($last_subtree >= 0 && $this->subtrees[$last_subtree] instanceof _smarty_tag && $this->subtrees[$last_subtree]->saved_block_nesting < $this->parser->block_nesting_level) { |
---|
159 | if ($subtree instanceof _smarty_code) { |
---|
160 | $this->subtrees[$last_subtree]->data .= '<?php echo ' . $subtree->data . ';?>'; |
---|
161 | } elseif ($subtree instanceof _smarty_dq_content) { |
---|
162 | $this->subtrees[$last_subtree]->data .= '<?php echo "' . $subtree->data . '";?>'; |
---|
163 | } else { |
---|
164 | $this->subtrees[$last_subtree]->data .= $subtree->data; |
---|
165 | } |
---|
166 | } else { |
---|
167 | $this->subtrees[] = $subtree; |
---|
168 | } |
---|
169 | if ($subtree instanceof _smarty_tag) { |
---|
170 | $this->parser->block_nesting_level = count($this->parser->compiler->_tag_stack); |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | * Merge subtree buffer content together |
---|
176 | * |
---|
177 | * @return string compiled template code |
---|
178 | */ |
---|
179 | public function to_smarty_php() |
---|
180 | { |
---|
181 | $code = ''; |
---|
182 | foreach ($this->subtrees as $subtree) { |
---|
183 | if ($code !== "") { |
---|
184 | $code .= "."; |
---|
185 | } |
---|
186 | if ($subtree instanceof _smarty_tag) { |
---|
187 | $more_php = $subtree->assign_to_var(); |
---|
188 | } else { |
---|
189 | $more_php = $subtree->to_smarty_php(); |
---|
190 | } |
---|
191 | |
---|
192 | $code .= $more_php; |
---|
193 | |
---|
194 | if (!$subtree instanceof _smarty_dq_content) { |
---|
195 | $this->parser->compiler->has_variable_string = true; |
---|
196 | } |
---|
197 | } |
---|
198 | return $code; |
---|
199 | } |
---|
200 | |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * Raw chars as part of a double quoted string. |
---|
205 | * |
---|
206 | * @package Smarty |
---|
207 | * @subpackage Compiler |
---|
208 | * @ignore |
---|
209 | */ |
---|
210 | class _smarty_dq_content extends _smarty_parsetree { |
---|
211 | |
---|
212 | |
---|
213 | /** |
---|
214 | * Create parse tree buffer with string content |
---|
215 | * |
---|
216 | * @param object $parser parser object |
---|
217 | * @param string $data string section |
---|
218 | */ |
---|
219 | public function __construct($parser, $data) |
---|
220 | { |
---|
221 | $this->parser = $parser; |
---|
222 | $this->data = $data; |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | * Return content as double quoted string |
---|
227 | * |
---|
228 | * @return string doubled quoted string |
---|
229 | */ |
---|
230 | public function to_smarty_php() |
---|
231 | { |
---|
232 | return '"' . $this->data . '"'; |
---|
233 | } |
---|
234 | |
---|
235 | } |
---|
236 | |
---|
237 | /** |
---|
238 | * Template element |
---|
239 | * |
---|
240 | * @package Smarty |
---|
241 | * @subpackage Compiler |
---|
242 | * @ignore |
---|
243 | */ |
---|
244 | class _smarty_template_buffer extends _smarty_parsetree { |
---|
245 | |
---|
246 | /** |
---|
247 | * Array of template elements |
---|
248 | * |
---|
249 | * @var array |
---|
250 | */ |
---|
251 | public $subtrees = Array(); |
---|
252 | |
---|
253 | /** |
---|
254 | * Create root of parse tree for template elements |
---|
255 | * |
---|
256 | * @param object $parser parse object |
---|
257 | */ |
---|
258 | public function __construct($parser) |
---|
259 | { |
---|
260 | $this->parser = $parser; |
---|
261 | } |
---|
262 | |
---|
263 | /** |
---|
264 | * Append buffer to subtree |
---|
265 | * |
---|
266 | * @param _smarty_parsetree $subtree |
---|
267 | */ |
---|
268 | public function append_subtree(_smarty_parsetree $subtree) |
---|
269 | { |
---|
270 | $this->subtrees[] = $subtree; |
---|
271 | } |
---|
272 | |
---|
273 | /** |
---|
274 | * Sanitize and merge subtree buffers together |
---|
275 | * |
---|
276 | * @return string template code content |
---|
277 | */ |
---|
278 | public function to_smarty_php() |
---|
279 | { |
---|
280 | $code = ''; |
---|
281 | for ($key = 0, $cnt = count($this->subtrees); $key < $cnt; $key++) { |
---|
282 | if ($key + 2 < $cnt) { |
---|
283 | if ($this->subtrees[$key] instanceof _smarty_linebreak && $this->subtrees[$key + 1] instanceof _smarty_tag && $this->subtrees[$key + 1]->data == '' && $this->subtrees[$key + 2] instanceof _smarty_linebreak) { |
---|
284 | $key = $key + 1; |
---|
285 | continue; |
---|
286 | } |
---|
287 | if (substr($this->subtrees[$key]->data, -1) == '<' && $this->subtrees[$key + 1]->data == '' && substr($this->subtrees[$key + 2]->data, -1) == '?') { |
---|
288 | $key = $key + 2; |
---|
289 | continue; |
---|
290 | } |
---|
291 | } |
---|
292 | if (substr($code, -1) == '<') { |
---|
293 | $subtree = $this->subtrees[$key]->to_smarty_php(); |
---|
294 | if (substr($subtree, 0, 1) == '?') { |
---|
295 | $code = substr($code, 0, strlen($code) - 1) . '<<?php ?>?' . substr($subtree, 1); |
---|
296 | } elseif ($this->parser->asp_tags && substr($subtree, 0, 1) == '%') { |
---|
297 | $code = substr($code, 0, strlen($code) - 1) . '<<?php ?>%' . substr($subtree, 1); |
---|
298 | } else { |
---|
299 | $code .= $subtree; |
---|
300 | } |
---|
301 | continue; |
---|
302 | } |
---|
303 | if ($this->parser->asp_tags && substr($code, -1) == '%') { |
---|
304 | $subtree = $this->subtrees[$key]->to_smarty_php(); |
---|
305 | if (substr($subtree, 0, 1) == '>') { |
---|
306 | $code = substr($code, 0, strlen($code) - 1) . '%<?php ?>>' . substr($subtree, 1); |
---|
307 | } else { |
---|
308 | $code .= $subtree; |
---|
309 | } |
---|
310 | continue; |
---|
311 | } |
---|
312 | if (substr($code, -1) == '?') { |
---|
313 | $subtree = $this->subtrees[$key]->to_smarty_php(); |
---|
314 | if (substr($subtree, 0, 1) == '>') { |
---|
315 | $code = substr($code, 0, strlen($code) - 1) . '?<?php ?>>' . substr($subtree, 1); |
---|
316 | } else { |
---|
317 | $code .= $subtree; |
---|
318 | } |
---|
319 | continue; |
---|
320 | } |
---|
321 | $code .= $this->subtrees[$key]->to_smarty_php(); |
---|
322 | } |
---|
323 | return $code; |
---|
324 | } |
---|
325 | |
---|
326 | } |
---|
327 | |
---|
328 | /** |
---|
329 | * template text |
---|
330 | * |
---|
331 | * @package Smarty |
---|
332 | * @subpackage Compiler |
---|
333 | * @ignore |
---|
334 | */ |
---|
335 | class _smarty_text extends _smarty_parsetree { |
---|
336 | |
---|
337 | |
---|
338 | /** |
---|
339 | * Create template text buffer |
---|
340 | * |
---|
341 | * @param object $parser parser object |
---|
342 | * @param string $data text |
---|
343 | */ |
---|
344 | public function __construct($parser, $data) |
---|
345 | { |
---|
346 | $this->parser = $parser; |
---|
347 | $this->data = $data; |
---|
348 | } |
---|
349 | |
---|
350 | /** |
---|
351 | * Return buffer content |
---|
352 | * |
---|
353 | * @return strint text |
---|
354 | */ |
---|
355 | public function to_smarty_php() |
---|
356 | { |
---|
357 | return $this->data; |
---|
358 | } |
---|
359 | |
---|
360 | } |
---|
361 | |
---|
362 | /** |
---|
363 | * template linebreaks |
---|
364 | * |
---|
365 | * @package Smarty |
---|
366 | * @subpackage Compiler |
---|
367 | * @ignore |
---|
368 | */ |
---|
369 | class _smarty_linebreak extends _smarty_parsetree { |
---|
370 | |
---|
371 | /** |
---|
372 | * Create buffer with linebreak content |
---|
373 | * |
---|
374 | * @param object $parser parser object |
---|
375 | * @param string $data linebreak string |
---|
376 | */ |
---|
377 | public function __construct($parser, $data) |
---|
378 | { |
---|
379 | $this->parser = $parser; |
---|
380 | $this->data = $data; |
---|
381 | } |
---|
382 | |
---|
383 | /** |
---|
384 | * Return linebrak |
---|
385 | * |
---|
386 | * @return string linebreak |
---|
387 | */ |
---|
388 | public function to_smarty_php() |
---|
389 | { |
---|
390 | return $this->data; |
---|
391 | } |
---|
392 | |
---|
393 | } |
---|
394 | |
---|
395 | ?> |
---|