1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Project: Smarty: the PHP compiling template engine |
5
|
|
|
* File: Smarty_Compiler.class.php |
6
|
|
|
* |
7
|
|
|
* This library is free software; you can redistribute it and/or |
8
|
|
|
* modify it under the terms of the GNU Lesser General Public |
9
|
|
|
* License as published by the Free Software Foundation; either |
10
|
|
|
* version 2.1 of the License, or (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This library is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15
|
|
|
* Lesser General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Lesser General Public |
18
|
|
|
* License along with this library; if not, write to the Free Software |
19
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20
|
|
|
* |
21
|
|
|
* @link http://smarty.php.net/ |
22
|
|
|
* @author Monte Ohrt <monte at ohrt dot com> |
23
|
|
|
* @author Andrei Zmievski <[email protected]> |
24
|
|
|
* @version 2.6.25-dev |
25
|
|
|
* @copyright 2001-2005 New Digital Group, Inc. |
26
|
|
|
* @package Smarty |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
/* $Id$ */ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Template compiling class |
33
|
|
|
* @package Smarty |
34
|
|
|
*/ |
35
|
|
|
class Smarty_Compiler extends Smarty { |
36
|
|
|
|
37
|
|
|
// internal vars |
38
|
|
|
/**#@+ |
39
|
|
|
* @access private |
40
|
|
|
*/ |
41
|
|
|
var $_folded_blocks = array(); // keeps folded template blocks |
42
|
|
|
var $_current_file = null; // the current template being compiled |
43
|
|
|
var $_current_line_no = 1; // line number for error messages |
44
|
|
|
var $_capture_stack = array(); // keeps track of nested capture buffers |
45
|
|
|
var $_plugin_info = array(); // keeps track of plugins to load |
46
|
|
|
var $_init_smarty_vars = false; |
47
|
|
|
var $_permitted_tokens = array('true','false','yes','no','on','off','null'); |
48
|
|
|
var $_db_qstr_regexp = null; // regexps are setup in the constructor |
49
|
|
|
var $_si_qstr_regexp = null; |
50
|
|
|
var $_qstr_regexp = null; |
51
|
|
|
var $_func_regexp = null; |
52
|
|
|
var $_reg_obj_regexp = null; |
53
|
|
|
var $_var_bracket_regexp = null; |
54
|
|
|
var $_num_const_regexp = null; |
55
|
|
|
var $_dvar_guts_regexp = null; |
56
|
|
|
var $_dvar_regexp = null; |
57
|
|
|
var $_cvar_regexp = null; |
58
|
|
|
var $_svar_regexp = null; |
59
|
|
|
var $_avar_regexp = null; |
60
|
|
|
var $_mod_regexp = null; |
61
|
|
|
var $_var_regexp = null; |
62
|
|
|
var $_parenth_param_regexp = null; |
63
|
|
|
var $_func_call_regexp = null; |
64
|
|
|
var $_obj_ext_regexp = null; |
65
|
|
|
var $_obj_start_regexp = null; |
66
|
|
|
var $_obj_params_regexp = null; |
67
|
|
|
var $_obj_call_regexp = null; |
68
|
|
|
var $_cacheable_state = 0; |
69
|
|
|
var $_cache_attrs_count = 0; |
70
|
|
|
var $_nocache_count = 0; |
71
|
|
|
var $_cache_serial = null; |
72
|
|
|
var $_cache_include = null; |
73
|
|
|
|
74
|
|
|
var $_strip_depth = 0; |
75
|
|
|
var $_additional_newline = "\n"; |
76
|
|
|
|
77
|
|
|
/**#@-*/ |
78
|
|
|
/** |
79
|
|
|
* The class constructor. |
80
|
|
|
*/ |
81
|
|
|
public function __construct() |
82
|
|
|
{ |
83
|
|
|
// matches double quoted strings: |
84
|
|
|
// "foobar" |
85
|
|
|
// "foo\"bar" |
86
|
|
|
$this->_db_qstr_regexp = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"'; |
87
|
|
|
|
88
|
|
|
// matches single quoted strings: |
89
|
|
|
// 'foobar' |
90
|
|
|
// 'foo\'bar' |
91
|
|
|
$this->_si_qstr_regexp = '\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\''; |
92
|
|
|
|
93
|
|
|
// matches single or double quoted strings |
94
|
|
|
$this->_qstr_regexp = '(?:' . $this->_db_qstr_regexp . '|' . $this->_si_qstr_regexp . ')'; |
95
|
|
|
|
96
|
|
|
// matches bracket portion of vars |
97
|
|
|
// [0] |
98
|
|
|
// [foo] |
99
|
|
|
// [$bar] |
100
|
|
|
$this->_var_bracket_regexp = '\[\$?[\w\.]+\]'; |
101
|
|
|
|
102
|
|
|
// matches numerical constants |
103
|
|
|
// 30 |
104
|
|
|
// -12 |
105
|
|
|
// 13.22 |
106
|
|
|
$this->_num_const_regexp = '(?:\-?\d+(?:\.\d+)?)'; |
107
|
|
|
|
108
|
|
|
// matches $ vars (not objects): |
109
|
|
|
// $foo |
110
|
|
|
// $foo.bar |
111
|
|
|
// $foo.bar.foobar |
112
|
|
|
// $foo[0] |
113
|
|
|
// $foo[$bar] |
114
|
|
|
// $foo[5][blah] |
115
|
|
|
// $foo[5].bar[$foobar][4] |
116
|
|
|
$this->_dvar_math_regexp = '(?:[\+\*\/\%]|(?:-(?!>)))'; |
|
|
|
|
117
|
|
|
$this->_dvar_math_var_regexp = '[\$\w\.\+\-\*\/\%\d\>\[\]]'; |
|
|
|
|
118
|
|
|
$this->_dvar_guts_regexp = '\w+(?:' . $this->_var_bracket_regexp |
119
|
|
|
. ')*(?:\.\$?\w+(?:' . $this->_var_bracket_regexp . ')*)*(?:' . $this->_dvar_math_regexp . '(?:' . $this->_num_const_regexp . '|' . $this->_dvar_math_var_regexp . ')*)?'; |
120
|
|
|
$this->_dvar_regexp = '\$' . $this->_dvar_guts_regexp; |
121
|
|
|
|
122
|
|
|
// matches config vars: |
123
|
|
|
// #foo# |
124
|
|
|
// #foobar123_foo# |
125
|
|
|
$this->_cvar_regexp = '\#\w+\#'; |
126
|
|
|
|
127
|
|
|
// matches section vars: |
128
|
|
|
// %foo.bar% |
129
|
|
|
$this->_svar_regexp = '\%\w+\.\w+\%'; |
130
|
|
|
|
131
|
|
|
// matches all valid variables (no quotes, no modifiers) |
132
|
|
|
$this->_avar_regexp = '(?:' . $this->_dvar_regexp . '|' |
133
|
|
|
. $this->_cvar_regexp . '|' . $this->_svar_regexp . ')'; |
134
|
|
|
|
135
|
|
|
// matches valid variable syntax: |
136
|
|
|
// $foo |
137
|
|
|
// $foo |
138
|
|
|
// #foo# |
139
|
|
|
// #foo# |
140
|
|
|
// "text" |
141
|
|
|
// "text" |
142
|
|
|
$this->_var_regexp = '(?:' . $this->_avar_regexp . '|' . $this->_qstr_regexp . ')'; |
143
|
|
|
|
144
|
|
|
// matches valid object call (one level of object nesting allowed in parameters): |
145
|
|
|
// $foo->bar |
146
|
|
|
// $foo->bar() |
147
|
|
|
// $foo->bar("text") |
148
|
|
|
// $foo->bar($foo, $bar, "text") |
149
|
|
|
// $foo->bar($foo, "foo") |
150
|
|
|
// $foo->bar->foo() |
151
|
|
|
// $foo->bar->foo->bar() |
152
|
|
|
// $foo->bar($foo->bar) |
153
|
|
|
// $foo->bar($foo->bar()) |
154
|
|
|
// $foo->bar($foo->bar($blah,$foo,44,"foo",$foo[0].bar)) |
155
|
|
|
$this->_obj_ext_regexp = '\->(?:\$?' . $this->_dvar_guts_regexp . ')'; |
156
|
|
|
$this->_obj_restricted_param_regexp = '(?:' |
|
|
|
|
157
|
|
|
. '(?:' . $this->_var_regexp . '|' . $this->_num_const_regexp . ')(?:' . $this->_obj_ext_regexp . '(?:\((?:(?:' . $this->_var_regexp . '|' . $this->_num_const_regexp . ')' |
158
|
|
|
. '(?:\s*,\s*(?:' . $this->_var_regexp . '|' . $this->_num_const_regexp . '))*)?\))?)*)'; |
159
|
|
|
$this->_obj_single_param_regexp = '(?:\w+|' . $this->_obj_restricted_param_regexp . '(?:\s*,\s*(?:(?:\w+|' |
|
|
|
|
160
|
|
|
. $this->_var_regexp . $this->_obj_restricted_param_regexp . ')))*)'; |
161
|
|
|
$this->_obj_params_regexp = '\((?:' . $this->_obj_single_param_regexp |
162
|
|
|
. '(?:\s*,\s*' . $this->_obj_single_param_regexp . ')*)?\)'; |
163
|
|
|
$this->_obj_start_regexp = '(?:' . $this->_dvar_regexp . '(?:' . $this->_obj_ext_regexp . ')+)'; |
164
|
|
|
$this->_obj_call_regexp = '(?:' . $this->_obj_start_regexp . '(?:' . $this->_obj_params_regexp . ')?(?:' . $this->_dvar_math_regexp . '(?:' . $this->_num_const_regexp . '|' . $this->_dvar_math_var_regexp . ')*)?)'; |
165
|
|
|
|
166
|
|
|
// matches valid modifier syntax: |
167
|
|
|
// |foo |
168
|
|
|
// |@foo |
169
|
|
|
// |foo:"bar" |
170
|
|
|
// |foo:$bar |
171
|
|
|
// |foo:"bar":$foobar |
172
|
|
|
// |foo|bar |
173
|
|
|
// |foo:$foo->bar |
174
|
|
|
$this->_mod_regexp = '(?:\|@?\w+(?::(?:\w+|' . $this->_num_const_regexp . '|' |
175
|
|
|
. $this->_obj_call_regexp . '|' . $this->_avar_regexp . '|' . $this->_qstr_regexp .'))*)'; |
176
|
|
|
|
177
|
|
|
// matches valid function name: |
178
|
|
|
// foo123 |
179
|
|
|
// _foo_bar |
180
|
|
|
$this->_func_regexp = '[a-zA-Z_]\w*'; |
181
|
|
|
|
182
|
|
|
// matches valid registered object: |
183
|
|
|
// foo->bar |
184
|
|
|
$this->_reg_obj_regexp = '[a-zA-Z_]\w*->[a-zA-Z_]\w*'; |
185
|
|
|
|
186
|
|
|
// matches valid parameter values: |
187
|
|
|
// true |
188
|
|
|
// $foo |
189
|
|
|
// $foo|bar |
190
|
|
|
// #foo# |
191
|
|
|
// #foo#|bar |
192
|
|
|
// "text" |
193
|
|
|
// "text"|bar |
194
|
|
|
// $foo->bar |
195
|
|
|
$this->_param_regexp = '(?:\s*(?:' . $this->_obj_call_regexp . '|' |
|
|
|
|
196
|
|
|
. $this->_var_regexp . '|' . $this->_num_const_regexp . '|\w+)(?>' . $this->_mod_regexp . '*)\s*)'; |
197
|
|
|
|
198
|
|
|
// matches valid parenthesised function parameters: |
199
|
|
|
// |
200
|
|
|
// "text" |
201
|
|
|
// $foo, $bar, "text" |
202
|
|
|
// $foo|bar, "foo"|bar, $foo->bar($foo)|bar |
203
|
|
|
$this->_parenth_param_regexp = '(?:\((?:\w+|' |
204
|
|
|
. $this->_param_regexp . '(?:\s*,\s*(?:(?:\w+|' |
205
|
|
|
. $this->_param_regexp . ')))*)?\))'; |
206
|
|
|
|
207
|
|
|
// matches valid function call: |
208
|
|
|
// foo() |
209
|
|
|
// foo_bar($foo) |
210
|
|
|
// _foo_bar($foo,"bar") |
211
|
|
|
// foo123($foo,$foo->bar(),"foo") |
212
|
|
|
$this->_func_call_regexp = '(?:' . $this->_func_regexp . '\s*(?:' |
213
|
|
|
. $this->_parenth_param_regexp . '))'; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* compile a resource |
218
|
|
|
* |
219
|
|
|
* sets $compiled_content to the compiled source |
220
|
|
|
* @param string $resource_name |
221
|
|
|
* @param string $source_content |
222
|
|
|
* @param string $compiled_content |
223
|
|
|
* @return true |
224
|
|
|
*/ |
225
|
|
|
function _compile_file($resource_name, $source_content, &$compiled_content) |
|
|
|
|
226
|
|
|
{ |
227
|
|
|
|
228
|
|
|
if ($this->security) { |
229
|
|
|
// do not allow php syntax to be executed unless specified |
230
|
|
|
if ($this->php_handling == SMARTY_PHP_ALLOW && |
231
|
|
|
!$this->security_settings['PHP_HANDLING']) { |
232
|
|
|
$this->php_handling = SMARTY_PHP_PASSTHRU; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
$this->_load_filters(); |
237
|
|
|
|
238
|
|
|
$this->_current_file = $resource_name; |
239
|
|
|
$this->_current_line_no = 1; |
240
|
|
|
$ldq = preg_quote($this->left_delimiter, '~'); |
241
|
|
|
$rdq = preg_quote($this->right_delimiter, '~'); |
242
|
|
|
|
243
|
|
|
// run template source through prefilter functions |
244
|
|
|
if (count($this->_plugins['prefilter']) > 0) { |
245
|
|
|
foreach ($this->_plugins['prefilter'] as $filter_name => $prefilter) { |
246
|
|
|
if ($prefilter === false) continue; |
247
|
|
|
if ($prefilter[3] || is_callable($prefilter[0])) { |
248
|
|
|
$source_content = call_user_func_array($prefilter[0], |
249
|
|
|
array($source_content, &$this)); |
250
|
|
|
$this->_plugins['prefilter'][$filter_name][3] = true; |
251
|
|
|
} else { |
252
|
|
|
$this->_trigger_fatal_error("[plugin] prefilter '$filter_name' is not implemented"); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/* fetch all special blocks */ |
258
|
|
|
$search = "~{$ldq}\*(.*?)\*{$rdq}|{$ldq}\s*literal\s*{$rdq}(.*?){$ldq}\s*/literal\s*{$rdq}|{$ldq}\s*php\s*{$rdq}(.*?){$ldq}\s*/php\s*{$rdq}~s"; |
259
|
|
|
|
260
|
|
|
preg_match_all($search, $source_content, $match, PREG_SET_ORDER); |
261
|
|
|
$this->_folded_blocks = $match; |
262
|
|
|
|
263
|
|
|
/* replace special blocks by "{php}" */ |
264
|
|
|
$source_content = preg_replace_callback($search, array($this,'_preg_callback') |
265
|
|
|
, $source_content); |
266
|
|
|
|
267
|
|
|
/* Gather all template tags. */ |
268
|
|
|
preg_match_all("~{$ldq}\s*(.*?)\s*{$rdq}~s", $source_content, $_match); |
269
|
|
|
$template_tags = $_match[1]; |
270
|
|
|
/* Split content by template tags to obtain non-template content. */ |
271
|
|
|
$text_blocks = preg_split("~{$ldq}.*?{$rdq}~s", $source_content); |
272
|
|
|
|
273
|
|
|
/* loop through text blocks */ |
274
|
|
|
for ($curr_tb = 0, $for_max = count($text_blocks); $curr_tb < $for_max; $curr_tb++) { |
275
|
|
|
/* match anything resembling php tags */ |
276
|
|
|
if (preg_match_all('~(<\?(?:\w+|=)?|\?>|language\s*=\s*[\"\']?\s*php\s*[\"\']?)~is', $text_blocks[$curr_tb], $sp_match)) { |
277
|
|
|
/* replace tags with placeholders to prevent recursive replacements */ |
278
|
|
|
$sp_match[1] = array_unique($sp_match[1]); |
279
|
|
|
usort($sp_match[1], '_smarty_sort_length'); |
280
|
|
|
for ($curr_sp = 0, $for_max2 = count($sp_match[1]); $curr_sp < $for_max2; $curr_sp++) { |
281
|
|
|
$text_blocks[$curr_tb] = str_replace($sp_match[1][$curr_sp],'%%%SMARTYSP'.$curr_sp.'%%%',$text_blocks[$curr_tb]); |
282
|
|
|
} |
283
|
|
|
/* process each one */ |
284
|
|
|
for ($curr_sp = 0, $for_max2 = count($sp_match[1]); $curr_sp < $for_max2; $curr_sp++) { |
285
|
|
|
if ($this->php_handling == SMARTY_PHP_PASSTHRU) { |
286
|
|
|
/* echo php contents */ |
287
|
|
|
$text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', '<?php echo \''.str_replace("'", "\'", $sp_match[1][$curr_sp]).'\'; ?>'."\n", $text_blocks[$curr_tb]); |
288
|
|
|
} else if ($this->php_handling == SMARTY_PHP_QUOTE) { |
289
|
|
|
/* quote php tags */ |
290
|
|
|
$text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', htmlspecialchars($sp_match[1][$curr_sp]), $text_blocks[$curr_tb]); |
291
|
|
|
} else if ($this->php_handling == SMARTY_PHP_REMOVE) { |
292
|
|
|
/* remove php tags */ |
293
|
|
|
$text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', '', $text_blocks[$curr_tb]); |
294
|
|
|
} else { |
295
|
|
|
/* SMARTY_PHP_ALLOW, but echo non php starting tags */ |
296
|
|
|
$sp_match[1][$curr_sp] = preg_replace('~(<\?(?!php|=|$))~i', '<?php echo \'\\1\'?>'."\n", $sp_match[1][$curr_sp]); |
297
|
|
|
$text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', $sp_match[1][$curr_sp], $text_blocks[$curr_tb]); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/* Compile the template tags into PHP code. */ |
304
|
|
|
$compiled_tags = array(); |
305
|
|
|
for ($i = 0, $for_max = count($template_tags); $i < $for_max; $i++) { |
306
|
|
|
$this->_current_line_no += substr_count($text_blocks[$i], "\n"); |
307
|
|
|
$compiled_tags[] = $this->_compile_tag($template_tags[$i]); |
308
|
|
|
$this->_current_line_no += substr_count($template_tags[$i], "\n"); |
309
|
|
|
} |
310
|
|
|
if (count($this->_tag_stack)>0) { |
311
|
|
|
list($_open_tag, $_line_no) = end($this->_tag_stack); |
312
|
|
|
$this->_syntax_error("unclosed tag \{$_open_tag} (opened line $_line_no).", E_USER_ERROR, __FILE__, __LINE__); |
313
|
|
|
return; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/* Reformat $text_blocks between 'strip' and '/strip' tags, |
317
|
|
|
removing spaces, tabs and newlines. */ |
318
|
|
|
$strip = false; |
319
|
|
|
for ($i = 0, $for_max = count($compiled_tags); $i < $for_max; $i++) { |
320
|
|
|
if ($compiled_tags[$i] == '{strip}') { |
321
|
|
|
$compiled_tags[$i] = ''; |
322
|
|
|
$strip = true; |
323
|
|
|
/* remove leading whitespaces */ |
324
|
|
|
$text_blocks[$i + 1] = ltrim($text_blocks[$i + 1]); |
325
|
|
|
} |
326
|
|
|
if ($strip) { |
327
|
|
|
/* strip all $text_blocks before the next '/strip' */ |
328
|
|
|
for ($j = $i + 1; $j < $for_max; $j++) { |
329
|
|
|
/* remove leading and trailing whitespaces of each line */ |
330
|
|
|
$text_blocks[$j] = preg_replace('![\t ]*[\r\n]+[\t ]*!', '', $text_blocks[$j]); |
331
|
|
|
if ($compiled_tags[$j] == '{/strip}') { |
332
|
|
|
/* remove trailing whitespaces from the last text_block */ |
333
|
|
|
$text_blocks[$j] = rtrim($text_blocks[$j]); |
334
|
|
|
} |
335
|
|
|
$text_blocks[$j] = "<?php echo '" . strtr($text_blocks[$j], array("'"=>"\'", "\\"=>"\\\\")) . "'; ?>"; |
336
|
|
|
if ($compiled_tags[$j] == '{/strip}') { |
337
|
|
|
$compiled_tags[$j] = "\n"; /* slurped by php, but necessary |
338
|
|
|
if a newline is following the closing strip-tag */ |
339
|
|
|
$strip = false; |
340
|
|
|
$i = $j; |
341
|
|
|
break; |
342
|
|
|
} |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
$compiled_content = ''; |
347
|
|
|
|
348
|
|
|
$tag_guard = '%%%SMARTYOTG' . md5(uniqid(rand(), true)) . '%%%'; |
349
|
|
|
|
350
|
|
|
/* Interleave the compiled contents and text blocks to get the final result. */ |
351
|
|
|
for ($i = 0, $for_max = count($compiled_tags); $i < $for_max; $i++) { |
352
|
|
|
if ($compiled_tags[$i] == '') { |
353
|
|
|
// tag result empty, remove first newline from following text block |
354
|
|
|
$text_blocks[$i+1] = preg_replace('~^(\r\n|\r|\n)~', '', $text_blocks[$i+1]); |
355
|
|
|
} |
356
|
|
|
// replace legit PHP tags with placeholder |
357
|
|
|
$text_blocks[$i] = str_replace('<?', $tag_guard, $text_blocks[$i]); |
358
|
|
|
$compiled_tags[$i] = str_replace('<?', $tag_guard, $compiled_tags[$i]); |
359
|
|
|
|
360
|
|
|
$compiled_content .= $text_blocks[$i] . $compiled_tags[$i]; |
361
|
|
|
} |
362
|
|
|
$compiled_content .= str_replace('<?', $tag_guard, $text_blocks[$i]); |
363
|
|
|
|
364
|
|
|
// escape php tags created by interleaving |
365
|
|
|
$compiled_content = str_replace('<?', "<?php echo '<?' ?>\n", $compiled_content); |
366
|
|
|
$compiled_content = preg_replace("~(?<!')language\s*=\s*[\"\']?\s*php\s*[\"\']?~", "<?php echo 'language=php' ?>\n", $compiled_content); |
367
|
|
|
|
368
|
|
|
// recover legit tags |
369
|
|
|
$compiled_content = str_replace($tag_guard, '<?', $compiled_content); |
370
|
|
|
|
371
|
|
|
// remove \n from the end of the file, if any |
372
|
|
|
if (strlen($compiled_content) && (substr($compiled_content, -1) == "\n") ) { |
373
|
|
|
$compiled_content = substr($compiled_content, 0, -1); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
if (!empty($this->_cache_serial)) { |
377
|
|
|
$compiled_content = "<?php \$this->_cache_serials['".$this->_cache_include."'] = '".$this->_cache_serial."'; ?>" . $compiled_content; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
// run compiled template through postfilter functions |
381
|
|
|
if (count($this->_plugins['postfilter']) > 0) { |
382
|
|
|
foreach ($this->_plugins['postfilter'] as $filter_name => $postfilter) { |
383
|
|
|
if ($postfilter === false) continue; |
384
|
|
|
if ($postfilter[3] || is_callable($postfilter[0])) { |
385
|
|
|
$compiled_content = call_user_func_array($postfilter[0], |
386
|
|
|
array($compiled_content, &$this)); |
387
|
|
|
$this->_plugins['postfilter'][$filter_name][3] = true; |
388
|
|
|
} else { |
389
|
|
|
$this->_trigger_fatal_error("Smarty plugin error: postfilter '$filter_name' is not implemented"); |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
// put header at the top of the compiled template |
395
|
|
|
$template_header = "<?php /* Smarty version ".$this->_version.", created on ".strftime("%Y-%m-%d %H:%M:%S")."\n"; |
396
|
|
|
$template_header .= " compiled from ".strtr(urlencode($resource_name), array('%2F'=>'/', '%3A'=>':'))." */ ?>\n"; |
397
|
|
|
|
398
|
|
|
/* Emit code to load needed plugins. */ |
399
|
|
|
$this->_plugins_code = ''; |
|
|
|
|
400
|
|
|
if (count($this->_plugin_info)) { |
401
|
|
|
$_plugins_params = "array('plugins' => array("; |
402
|
|
|
foreach ($this->_plugin_info as $plugin_type => $plugins) { |
403
|
|
|
foreach ($plugins as $plugin_name => $plugin_info) { |
404
|
|
|
$_plugins_params .= "array('$plugin_type', '$plugin_name', '" . strtr($plugin_info[0], array("'" => "\\'", "\\" => "\\\\")) . "', $plugin_info[1], "; |
405
|
|
|
$_plugins_params .= $plugin_info[2] ? 'true),' : 'false),'; |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
$_plugins_params .= '))'; |
409
|
|
|
$plugins_code = "<?php require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');\nsmarty_core_load_plugins($_plugins_params, \$this); ?>\n"; |
410
|
|
|
$template_header .= $plugins_code; |
411
|
|
|
$this->_plugin_info = array(); |
412
|
|
|
$this->_plugins_code = $plugins_code; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
if ($this->_init_smarty_vars) { |
416
|
|
|
$template_header .= "<?php require_once(SMARTY_CORE_DIR . 'core.assign_smarty_interface.php');\nsmarty_core_assign_smarty_interface(null, \$this); ?>\n"; |
417
|
|
|
$this->_init_smarty_vars = false; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
$compiled_content = $template_header . $compiled_content; |
421
|
|
|
return true; |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* Compile a template tag |
426
|
|
|
* |
427
|
|
|
* @param string $template_tag |
428
|
|
|
* @return string |
429
|
|
|
*/ |
430
|
|
|
function _compile_tag($template_tag) |
|
|
|
|
431
|
|
|
{ |
432
|
|
|
/* Matched comment. */ |
433
|
|
|
if (substr($template_tag, 0, 1) == '*' && substr($template_tag, -1) == '*') |
434
|
|
|
return ''; |
435
|
|
|
|
436
|
|
|
/* Split tag into two three parts: command, command modifiers and the arguments. */ |
437
|
|
|
if(! preg_match('~^(?:(' . $this->_num_const_regexp . '|' . $this->_obj_call_regexp . '|' . $this->_var_regexp |
438
|
|
|
. '|\/?' . $this->_reg_obj_regexp . '|\/?' . $this->_func_regexp . ')(' . $this->_mod_regexp . '*)) |
439
|
|
|
(?:\s+(.*))?$ |
440
|
|
|
~xs', $template_tag, $match)) { |
441
|
|
|
$this->_syntax_error("unrecognized tag: $template_tag", E_USER_ERROR, __FILE__, __LINE__); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
$tag_command = $match[1]; |
445
|
|
|
$tag_modifier = isset($match[2]) ? $match[2] : null; |
446
|
|
|
$tag_args = isset($match[3]) ? $match[3] : null; |
447
|
|
|
|
448
|
|
|
if (preg_match('~^' . $this->_num_const_regexp . '|' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '$~', $tag_command)) { |
449
|
|
|
/* tag name is a variable or object */ |
450
|
|
|
$_return = $this->_parse_var_props($tag_command . $tag_modifier); |
451
|
|
|
return "<?php echo $_return; ?>" . $this->_additional_newline; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/* If the tag name is a registered object, we process it. */ |
455
|
|
|
if (preg_match('~^\/?' . $this->_reg_obj_regexp . '$~', $tag_command)) { |
456
|
|
|
return $this->_compile_registered_object_tag($tag_command, $this->_parse_attrs($tag_args), $tag_modifier); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
switch ($tag_command) { |
460
|
|
|
case 'include': |
461
|
|
|
return $this->_compile_include_tag($tag_args); |
462
|
|
|
|
463
|
|
|
case 'include_php': |
464
|
|
|
return $this->_compile_include_php_tag($tag_args); |
465
|
|
|
|
466
|
|
|
case 'if': |
467
|
|
|
$this->_push_tag('if'); |
468
|
|
|
return $this->_compile_if_tag($tag_args); |
469
|
|
|
|
470
|
|
|
case 'else': |
471
|
|
|
list($_open_tag) = end($this->_tag_stack); |
472
|
|
|
if ($_open_tag != 'if' && $_open_tag != 'elseif') |
473
|
|
|
$this->_syntax_error('unexpected {else}', E_USER_ERROR, __FILE__, __LINE__); |
474
|
|
|
else |
475
|
|
|
$this->_push_tag('else'); |
476
|
|
|
return '<?php else: ?>'; |
477
|
|
|
|
478
|
|
|
case 'elseif': |
479
|
|
|
list($_open_tag) = end($this->_tag_stack); |
480
|
|
|
if ($_open_tag != 'if' && $_open_tag != 'elseif') |
481
|
|
|
$this->_syntax_error('unexpected {elseif}', E_USER_ERROR, __FILE__, __LINE__); |
482
|
|
|
if ($_open_tag == 'if') |
483
|
|
|
$this->_push_tag('elseif'); |
484
|
|
|
return $this->_compile_if_tag($tag_args, true); |
485
|
|
|
|
486
|
|
|
case '/if': |
487
|
|
|
$this->_pop_tag('if'); |
488
|
|
|
return '<?php endif; ?>'; |
489
|
|
|
|
490
|
|
|
case 'capture': |
491
|
|
|
return $this->_compile_capture_tag(true, $tag_args); |
492
|
|
|
|
493
|
|
|
case '/capture': |
494
|
|
|
return $this->_compile_capture_tag(false); |
495
|
|
|
|
496
|
|
|
case 'ldelim': |
497
|
|
|
return $this->left_delimiter; |
498
|
|
|
|
499
|
|
|
case 'rdelim': |
500
|
|
|
return $this->right_delimiter; |
501
|
|
|
|
502
|
|
|
case 'section': |
503
|
|
|
$this->_push_tag('section'); |
504
|
|
|
return $this->_compile_section_start($tag_args); |
505
|
|
|
|
506
|
|
|
case 'sectionelse': |
507
|
|
|
$this->_push_tag('sectionelse'); |
508
|
|
|
return "<?php endfor; else: ?>"; |
509
|
|
|
break; |
|
|
|
|
510
|
|
|
|
511
|
|
|
case '/section': |
512
|
|
|
$_open_tag = $this->_pop_tag('section'); |
513
|
|
|
if ($_open_tag == 'sectionelse') |
514
|
|
|
return "<?php endif; ?>"; |
515
|
|
|
else |
516
|
|
|
return "<?php endfor; endif; ?>"; |
517
|
|
|
|
518
|
|
|
case 'foreach': |
519
|
|
|
$this->_push_tag('foreach'); |
520
|
|
|
return $this->_compile_foreach_start($tag_args); |
521
|
|
|
break; |
522
|
|
|
|
523
|
|
|
case 'foreachelse': |
524
|
|
|
$this->_push_tag('foreachelse'); |
525
|
|
|
return "<?php endforeach; else: ?>"; |
526
|
|
|
|
527
|
|
|
case '/foreach': |
528
|
|
|
$_open_tag = $this->_pop_tag('foreach'); |
529
|
|
|
if ($_open_tag == 'foreachelse') |
530
|
|
|
return "<?php endif; unset(\$_from); ?>"; |
531
|
|
|
else |
532
|
|
|
return "<?php endforeach; endif; unset(\$_from); ?>"; |
533
|
|
|
break; |
534
|
|
|
|
535
|
|
|
case 'strip': |
536
|
|
|
case '/strip': |
537
|
|
|
if (substr($tag_command, 0, 1)=='/') { |
538
|
|
|
$this->_pop_tag('strip'); |
539
|
|
|
if (--$this->_strip_depth==0) { /* outermost closing {/strip} */ |
540
|
|
|
$this->_additional_newline = "\n"; |
541
|
|
|
return '{' . $tag_command . '}'; |
542
|
|
|
} |
543
|
|
|
} else { |
544
|
|
|
$this->_push_tag('strip'); |
545
|
|
|
if ($this->_strip_depth++==0) { /* outermost opening {strip} */ |
546
|
|
|
$this->_additional_newline = ""; |
547
|
|
|
return '{' . $tag_command . '}'; |
548
|
|
|
} |
549
|
|
|
} |
550
|
|
|
return ''; |
551
|
|
|
|
552
|
|
|
case 'php': |
553
|
|
|
/* handle folded tags replaced by {php} */ |
554
|
|
|
$block = array_shift($this->_folded_blocks); |
555
|
|
|
$this->_current_line_no += substr_count($block[0], "\n"); |
556
|
|
|
/* the number of matched elements in the regexp in _compile_file() |
557
|
|
|
determins the type of folded tag that was found */ |
558
|
|
|
switch (count($block)) { |
559
|
|
|
case 2: /* comment */ |
560
|
|
|
return ''; |
561
|
|
|
|
562
|
|
|
case 3: /* literal */ |
563
|
|
|
return "<?php echo '" . strtr($block[2], array("'"=>"\'", "\\"=>"\\\\")) . "'; ?>" . $this->_additional_newline; |
564
|
|
|
|
565
|
|
|
case 4: /* php */ |
566
|
|
|
if ($this->security && !$this->security_settings['PHP_TAGS']) { |
567
|
|
|
$this->_syntax_error("(secure mode) php tags not permitted", E_USER_WARNING, __FILE__, __LINE__); |
568
|
|
|
return; |
569
|
|
|
} |
570
|
|
|
return '<?php ' . $block[3] .' ?>'; |
571
|
|
|
} |
572
|
|
|
break; |
573
|
|
|
|
574
|
|
|
case 'insert': |
575
|
|
|
return $this->_compile_insert_tag($tag_args); |
576
|
|
|
|
577
|
|
|
default: |
578
|
|
|
if ($this->_compile_compiler_tag($tag_command, $tag_args, $output)) { |
579
|
|
|
return $output; |
580
|
|
|
} else if ($this->_compile_block_tag($tag_command, $tag_args, $tag_modifier, $output)) { |
581
|
|
|
return $output; |
582
|
|
|
} else if ($this->_compile_custom_tag($tag_command, $tag_args, $tag_modifier, $output)) { |
583
|
|
|
return $output; |
584
|
|
|
} else { |
585
|
|
|
$this->_syntax_error("unrecognized tag '$tag_command'", E_USER_ERROR, __FILE__, __LINE__); |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
} |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
|
592
|
|
|
/** |
593
|
|
|
* compile the custom compiler tag |
594
|
|
|
* |
595
|
|
|
* sets $output to the compiled custom compiler tag |
596
|
|
|
* @param string $tag_command |
597
|
|
|
* @param string $tag_args |
598
|
|
|
* @param string $output |
599
|
|
|
* @return boolean |
600
|
|
|
*/ |
601
|
|
|
function _compile_compiler_tag($tag_command, $tag_args, &$output) |
|
|
|
|
602
|
|
|
{ |
603
|
|
|
$found = false; |
604
|
|
|
$have_function = true; |
605
|
|
|
|
606
|
|
|
/* |
607
|
|
|
* First we check if the compiler function has already been registered |
608
|
|
|
* or loaded from a plugin file. |
609
|
|
|
*/ |
610
|
|
|
if (isset($this->_plugins['compiler'][$tag_command])) { |
611
|
|
|
$found = true; |
612
|
|
|
$plugin_func = $this->_plugins['compiler'][$tag_command][0]; |
613
|
|
|
if (!is_callable($plugin_func)) { |
614
|
|
|
$message = "compiler function '$tag_command' is not implemented"; |
615
|
|
|
$have_function = false; |
616
|
|
|
} |
617
|
|
|
} |
618
|
|
|
/* |
619
|
|
|
* Otherwise we need to load plugin file and look for the function |
620
|
|
|
* inside it. |
621
|
|
|
*/ |
622
|
|
|
else if ($plugin_file = $this->_get_plugin_filepath('compiler', $tag_command)) { |
623
|
|
|
$found = true; |
624
|
|
|
|
625
|
|
|
include_once $plugin_file; |
626
|
|
|
|
627
|
|
|
$plugin_func = 'smarty_compiler_' . $tag_command; |
628
|
|
|
if (!is_callable($plugin_func)) { |
629
|
|
|
$message = "plugin function $plugin_func() not found in $plugin_file\n"; |
630
|
|
|
$have_function = false; |
631
|
|
|
} else { |
632
|
|
|
$this->_plugins['compiler'][$tag_command] = array($plugin_func, null, null, null, true); |
633
|
|
|
} |
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
/* |
637
|
|
|
* True return value means that we either found a plugin or a |
638
|
|
|
* dynamically registered function. False means that we didn't and the |
639
|
|
|
* compiler should now emit code to load custom function plugin for this |
640
|
|
|
* tag. |
641
|
|
|
*/ |
642
|
|
|
if ($found) { |
643
|
|
|
if ($have_function) { |
644
|
|
|
$output = call_user_func_array($plugin_func, array($tag_args, &$this)); |
|
|
|
|
645
|
|
|
if($output != '') { |
646
|
|
|
$output = '<?php ' . $this->_push_cacheable_state('compiler', $tag_command) |
647
|
|
|
. $output |
648
|
|
|
. $this->_pop_cacheable_state('compiler', $tag_command) . ' ?>'; |
649
|
|
|
} |
650
|
|
|
} else { |
651
|
|
|
$this->_syntax_error($message, E_USER_WARNING, __FILE__, __LINE__); |
|
|
|
|
652
|
|
|
} |
653
|
|
|
return true; |
654
|
|
|
} else { |
655
|
|
|
return false; |
656
|
|
|
} |
657
|
|
|
} |
658
|
|
|
|
659
|
|
|
|
660
|
|
|
/** |
661
|
|
|
* compile block function tag |
662
|
|
|
* |
663
|
|
|
* sets $output to compiled block function tag |
664
|
|
|
* @param string $tag_command |
665
|
|
|
* @param string $tag_args |
666
|
|
|
* @param string $tag_modifier |
667
|
|
|
* @param string $output |
668
|
|
|
* @return boolean |
669
|
|
|
*/ |
670
|
|
|
function _compile_block_tag($tag_command, $tag_args, $tag_modifier, &$output) |
|
|
|
|
671
|
|
|
{ |
672
|
|
|
if (substr($tag_command, 0, 1) == '/') { |
673
|
|
|
$start_tag = false; |
674
|
|
|
$tag_command = substr($tag_command, 1); |
675
|
|
|
} else |
676
|
|
|
$start_tag = true; |
677
|
|
|
|
678
|
|
|
$found = false; |
679
|
|
|
$have_function = true; |
680
|
|
|
|
681
|
|
|
/* |
682
|
|
|
* First we check if the block function has already been registered |
683
|
|
|
* or loaded from a plugin file. |
684
|
|
|
*/ |
685
|
|
|
if (isset($this->_plugins['block'][$tag_command])) { |
686
|
|
|
$found = true; |
687
|
|
|
$plugin_func = $this->_plugins['block'][$tag_command][0]; |
688
|
|
|
if (!is_callable($plugin_func)) { |
689
|
|
|
$message = "block function '$tag_command' is not implemented"; |
690
|
|
|
$have_function = false; |
691
|
|
|
} |
692
|
|
|
} |
693
|
|
|
/* |
694
|
|
|
* Otherwise we need to load plugin file and look for the function |
695
|
|
|
* inside it. |
696
|
|
|
*/ |
697
|
|
|
else if ($plugin_file = $this->_get_plugin_filepath('block', $tag_command)) { |
698
|
|
|
$found = true; |
699
|
|
|
|
700
|
|
|
include_once $plugin_file; |
701
|
|
|
|
702
|
|
|
$plugin_func = 'smarty_block_' . $tag_command; |
703
|
|
|
if (!function_exists($plugin_func)) { |
704
|
|
|
$message = "plugin function $plugin_func() not found in $plugin_file\n"; |
705
|
|
|
$have_function = false; |
706
|
|
|
} else { |
707
|
|
|
$this->_plugins['block'][$tag_command] = array($plugin_func, null, null, null, true); |
708
|
|
|
|
709
|
|
|
} |
710
|
|
|
} |
711
|
|
|
|
712
|
|
|
if (!$found) { |
713
|
|
|
return false; |
714
|
|
|
} else if (!$have_function) { |
715
|
|
|
$this->_syntax_error($message, E_USER_WARNING, __FILE__, __LINE__); |
|
|
|
|
716
|
|
|
return true; |
717
|
|
|
} |
718
|
|
|
|
719
|
|
|
/* |
720
|
|
|
* Even though we've located the plugin function, compilation |
721
|
|
|
* happens only once, so the plugin will still need to be loaded |
722
|
|
|
* at runtime for future requests. |
723
|
|
|
*/ |
724
|
|
|
$this->_add_plugin('block', $tag_command); |
725
|
|
|
|
726
|
|
|
if ($start_tag) |
727
|
|
|
$this->_push_tag($tag_command); |
728
|
|
|
else |
729
|
|
|
$this->_pop_tag($tag_command); |
730
|
|
|
|
731
|
|
|
if ($start_tag) { |
732
|
|
|
$output = '<?php ' . $this->_push_cacheable_state('block', $tag_command); |
733
|
|
|
$attrs = $this->_parse_attrs($tag_args); |
734
|
|
|
$_cache_attrs=''; |
735
|
|
|
$arg_list = $this->_compile_arg_list('block', $tag_command, $attrs, $_cache_attrs); |
736
|
|
|
$output .= "$_cache_attrs\$this->_tag_stack[] = array('$tag_command', array(".implode(',', $arg_list).')); '; |
737
|
|
|
$output .= '$_block_repeat=true;' . $this->_compile_plugin_call('block', $tag_command).'($this->_tag_stack[count($this->_tag_stack)-1][1], null, $this, $_block_repeat);'; |
738
|
|
|
$output .= 'while ($_block_repeat) { ob_start(); ?>'; |
739
|
|
|
} else { |
740
|
|
|
$output = '<?php $_block_content = ob_get_contents(); ob_end_clean(); '; |
741
|
|
|
$_out_tag_text = $this->_compile_plugin_call('block', $tag_command).'($this->_tag_stack[count($this->_tag_stack)-1][1], $_block_content, $this, $_block_repeat)'; |
742
|
|
|
if ($tag_modifier != '') { |
743
|
|
|
$this->_parse_modifiers($_out_tag_text, $tag_modifier); |
744
|
|
|
} |
745
|
|
|
$output .= '$_block_repeat=false;echo ' . $_out_tag_text . '; } '; |
746
|
|
|
$output .= " array_pop(\$this->_tag_stack); " . $this->_pop_cacheable_state('block', $tag_command) . '?>'; |
747
|
|
|
} |
748
|
|
|
|
749
|
|
|
return true; |
750
|
|
|
} |
751
|
|
|
|
752
|
|
|
function _preg_callback ($matches) { |
|
|
|
|
753
|
|
|
return $this->_quote_replace($this->left_delimiter) |
754
|
|
|
. 'php' |
755
|
|
|
. str_repeat("\n", substr_count($matches[1], "\n")) |
756
|
|
|
. $this->_quote_replace($this->right_delimiter); |
757
|
|
|
} |
758
|
|
|
/** |
759
|
|
|
* compile custom function tag |
760
|
|
|
* |
761
|
|
|
* @param string $tag_command |
762
|
|
|
* @param string $tag_args |
763
|
|
|
* @param string $tag_modifier |
764
|
|
|
* @return string |
765
|
|
|
*/ |
766
|
|
|
function _compile_custom_tag($tag_command, $tag_args, $tag_modifier, &$output) |
|
|
|
|
767
|
|
|
{ |
768
|
|
|
$found = false; |
769
|
|
|
$have_function = true; |
770
|
|
|
|
771
|
|
|
/* |
772
|
|
|
* First we check if the custom function has already been registered |
773
|
|
|
* or loaded from a plugin file. |
774
|
|
|
*/ |
775
|
|
|
if (isset($this->_plugins['function'][$tag_command])) { |
776
|
|
|
$found = true; |
777
|
|
|
$plugin_func = $this->_plugins['function'][$tag_command][0]; |
778
|
|
|
if (!is_callable($plugin_func)) { |
779
|
|
|
$message = "custom function '$tag_command' is not implemented"; |
780
|
|
|
$have_function = false; |
781
|
|
|
} |
782
|
|
|
} |
783
|
|
|
/* |
784
|
|
|
* Otherwise we need to load plugin file and look for the function |
785
|
|
|
* inside it. |
786
|
|
|
*/ |
787
|
|
|
else if ($plugin_file = $this->_get_plugin_filepath('function', $tag_command)) { |
788
|
|
|
$found = true; |
789
|
|
|
|
790
|
|
|
include_once $plugin_file; |
791
|
|
|
|
792
|
|
|
$plugin_func = 'smarty_function_' . $tag_command; |
793
|
|
|
if (!function_exists($plugin_func)) { |
794
|
|
|
$message = "plugin function $plugin_func() not found in $plugin_file\n"; |
795
|
|
|
$have_function = false; |
796
|
|
|
} else { |
797
|
|
|
$this->_plugins['function'][$tag_command] = array($plugin_func, null, null, null, true); |
798
|
|
|
|
799
|
|
|
} |
800
|
|
|
} |
801
|
|
|
|
802
|
|
|
if (!$found) { |
803
|
|
|
return false; |
|
|
|
|
804
|
|
|
} else if (!$have_function) { |
805
|
|
|
$this->_syntax_error($message, E_USER_WARNING, __FILE__, __LINE__); |
|
|
|
|
806
|
|
|
return true; |
|
|
|
|
807
|
|
|
} |
808
|
|
|
|
809
|
|
|
/* declare plugin to be loaded on display of the template that |
810
|
|
|
we compile right now */ |
811
|
|
|
$this->_add_plugin('function', $tag_command); |
812
|
|
|
|
813
|
|
|
$_cacheable_state = $this->_push_cacheable_state('function', $tag_command); |
814
|
|
|
$attrs = $this->_parse_attrs($tag_args); |
815
|
|
|
$_cache_attrs = ''; |
816
|
|
|
$arg_list = $this->_compile_arg_list('function', $tag_command, $attrs, $_cache_attrs); |
817
|
|
|
|
818
|
|
|
$output = $this->_compile_plugin_call('function', $tag_command).'(array('.implode(',', $arg_list)."), \$this)"; |
819
|
|
|
if($tag_modifier != '') { |
820
|
|
|
$this->_parse_modifiers($output, $tag_modifier); |
821
|
|
|
} |
822
|
|
|
|
823
|
|
|
if($output != '') { |
824
|
|
|
$output = '<?php ' . $_cacheable_state . $_cache_attrs . 'echo ' . $output . ';' |
825
|
|
|
. $this->_pop_cacheable_state('function', $tag_command) . "?>" . $this->_additional_newline; |
826
|
|
|
} |
827
|
|
|
|
828
|
|
|
return true; |
|
|
|
|
829
|
|
|
} |
830
|
|
|
|
831
|
|
|
/** |
832
|
|
|
* compile a registered object tag |
833
|
|
|
* |
834
|
|
|
* @param string $tag_command |
835
|
|
|
* @param array $attrs |
836
|
|
|
* @param string $tag_modifier |
837
|
|
|
* @return string |
838
|
|
|
*/ |
839
|
|
|
function _compile_registered_object_tag($tag_command, $attrs, $tag_modifier) |
|
|
|
|
840
|
|
|
{ |
841
|
|
|
if (substr($tag_command, 0, 1) == '/') { |
842
|
|
|
$start_tag = false; |
843
|
|
|
$tag_command = substr($tag_command, 1); |
844
|
|
|
} else { |
845
|
|
|
$start_tag = true; |
846
|
|
|
} |
847
|
|
|
|
848
|
|
|
list($object, $obj_comp) = explode('->', $tag_command); |
849
|
|
|
|
850
|
|
|
$arg_list = array(); |
851
|
|
|
if(count($attrs)) { |
852
|
|
|
$_assign_var = false; |
853
|
|
|
foreach ($attrs as $arg_name => $arg_value) { |
854
|
|
|
if($arg_name == 'assign') { |
855
|
|
|
$_assign_var = $arg_value; |
856
|
|
|
unset($attrs['assign']); |
857
|
|
|
continue; |
858
|
|
|
} |
859
|
|
|
if (is_bool($arg_value)) |
860
|
|
|
$arg_value = $arg_value ? 'true' : 'false'; |
861
|
|
|
$arg_list[] = "'$arg_name' => $arg_value"; |
862
|
|
|
} |
863
|
|
|
} |
864
|
|
|
|
865
|
|
|
if($this->_reg_objects[$object][2]) { |
866
|
|
|
// smarty object argument format |
867
|
|
|
$args = "array(".implode(',', (array)$arg_list)."), \$this"; |
868
|
|
|
} else { |
869
|
|
|
// traditional argument format |
870
|
|
|
$args = implode(',', array_values($attrs)); |
871
|
|
|
if (empty($args)) { |
872
|
|
|
$args = ''; |
873
|
|
|
} |
874
|
|
|
} |
875
|
|
|
|
876
|
|
|
$prefix = ''; |
877
|
|
|
$postfix = ''; |
878
|
|
|
$newline = ''; |
879
|
|
|
if(!is_object($this->_reg_objects[$object][0])) { |
880
|
|
|
$this->_trigger_fatal_error("registered '$object' is not an object" , $this->_current_file, $this->_current_line_no, __FILE__, __LINE__); |
881
|
|
|
} elseif(!empty($this->_reg_objects[$object][1]) && !in_array($obj_comp, $this->_reg_objects[$object][1])) { |
882
|
|
|
$this->_trigger_fatal_error("'$obj_comp' is not a registered component of object '$object'", $this->_current_file, $this->_current_line_no, __FILE__, __LINE__); |
883
|
|
|
} elseif(method_exists($this->_reg_objects[$object][0], $obj_comp)) { |
884
|
|
|
// method |
885
|
|
|
if(in_array($obj_comp, $this->_reg_objects[$object][3])) { |
886
|
|
|
// block method |
887
|
|
|
if ($start_tag) { |
888
|
|
|
$prefix = "\$this->_tag_stack[] = array('$obj_comp', $args); "; |
889
|
|
|
$prefix .= "\$_block_repeat=true; \$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], null, \$this, \$_block_repeat); "; |
890
|
|
|
$prefix .= "while (\$_block_repeat) { ob_start();"; |
891
|
|
|
$return = null; |
892
|
|
|
$postfix = ''; |
893
|
|
|
} else { |
894
|
|
|
$prefix = "\$_obj_block_content = ob_get_contents(); ob_end_clean(); \$_block_repeat=false;"; |
895
|
|
|
$return = "\$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], \$_obj_block_content, \$this, \$_block_repeat)"; |
896
|
|
|
$postfix = "} array_pop(\$this->_tag_stack);"; |
897
|
|
|
} |
898
|
|
|
} else { |
899
|
|
|
// non-block method |
900
|
|
|
$return = "\$this->_reg_objects['$object'][0]->$obj_comp($args)"; |
901
|
|
|
} |
902
|
|
|
} else { |
903
|
|
|
// property |
904
|
|
|
$return = "\$this->_reg_objects['$object'][0]->$obj_comp"; |
905
|
|
|
} |
906
|
|
|
|
907
|
|
|
if($return != null) { |
908
|
|
|
if($tag_modifier != '') { |
909
|
|
|
$this->_parse_modifiers($return, $tag_modifier); |
910
|
|
|
} |
911
|
|
|
|
912
|
|
|
if(!empty($_assign_var)) { |
913
|
|
|
$output = "\$this->assign('" . $this->_dequote($_assign_var) ."', $return);"; |
914
|
|
|
} else { |
915
|
|
|
$output = 'echo ' . $return . ';'; |
916
|
|
|
$newline = $this->_additional_newline; |
917
|
|
|
} |
918
|
|
|
} else { |
919
|
|
|
$output = ''; |
920
|
|
|
} |
921
|
|
|
|
922
|
|
|
return '<?php ' . $prefix . $output . $postfix . "?>" . $newline; |
923
|
|
|
} |
924
|
|
|
|
925
|
|
|
/** |
926
|
|
|
* Compile {insert ...} tag |
927
|
|
|
* |
928
|
|
|
* @param string $tag_args |
929
|
|
|
* @return string |
930
|
|
|
*/ |
931
|
|
|
function _compile_insert_tag($tag_args) |
|
|
|
|
932
|
|
|
{ |
933
|
|
|
$attrs = $this->_parse_attrs($tag_args); |
934
|
|
|
$name = $this->_dequote($attrs['name']); |
935
|
|
|
|
936
|
|
|
if (empty($name)) { |
937
|
|
|
return $this->_syntax_error("missing insert name", E_USER_ERROR, __FILE__, __LINE__); |
|
|
|
|
938
|
|
|
} |
939
|
|
|
|
940
|
|
|
if (!preg_match('~^\w+$~', $name)) { |
941
|
|
|
return $this->_syntax_error("'insert: 'name' must be an insert function name", E_USER_ERROR, __FILE__, __LINE__); |
|
|
|
|
942
|
|
|
} |
943
|
|
|
|
944
|
|
|
if (!empty($attrs['script'])) { |
945
|
|
|
$delayed_loading = true; |
946
|
|
|
} else { |
947
|
|
|
$delayed_loading = false; |
948
|
|
|
} |
949
|
|
|
|
950
|
|
|
foreach ($attrs as $arg_name => $arg_value) { |
951
|
|
|
if (is_bool($arg_value)) |
952
|
|
|
$arg_value = $arg_value ? 'true' : 'false'; |
953
|
|
|
$arg_list[] = "'$arg_name' => $arg_value"; |
954
|
|
|
} |
955
|
|
|
|
956
|
|
|
$this->_add_plugin('insert', $name, $delayed_loading); |
957
|
|
|
|
958
|
|
|
$_params = "array('args' => array(".implode(', ', (array)$arg_list)."))"; |
|
|
|
|
959
|
|
|
|
960
|
|
|
return "<?php require_once(SMARTY_CORE_DIR . 'core.run_insert_handler.php');\necho smarty_core_run_insert_handler($_params, \$this); ?>" . $this->_additional_newline; |
961
|
|
|
} |
962
|
|
|
|
963
|
|
|
/** |
964
|
|
|
* Compile {include ...} tag |
965
|
|
|
* |
966
|
|
|
* @param string $tag_args |
967
|
|
|
* @return string |
968
|
|
|
*/ |
969
|
|
|
function _compile_include_tag($tag_args) |
|
|
|
|
970
|
|
|
{ |
971
|
|
|
$attrs = $this->_parse_attrs($tag_args); |
972
|
|
|
$arg_list = array(); |
973
|
|
|
|
974
|
|
|
if (empty($attrs['file'])) { |
975
|
|
|
$this->_syntax_error("missing 'file' attribute in include tag", E_USER_ERROR, __FILE__, __LINE__); |
976
|
|
|
} |
977
|
|
|
|
978
|
|
|
foreach ($attrs as $arg_name => $arg_value) { |
979
|
|
|
if ($arg_name == 'file') { |
980
|
|
|
$include_file = $arg_value; |
981
|
|
|
continue; |
982
|
|
|
} else if ($arg_name == 'assign') { |
983
|
|
|
$assign_var = $arg_value; |
984
|
|
|
continue; |
985
|
|
|
} |
986
|
|
|
if (is_bool($arg_value)) |
987
|
|
|
$arg_value = $arg_value ? 'true' : 'false'; |
988
|
|
|
$arg_list[] = "'$arg_name' => $arg_value"; |
989
|
|
|
} |
990
|
|
|
|
991
|
|
|
$output = '<?php '; |
992
|
|
|
|
993
|
|
|
if (isset($assign_var)) { |
994
|
|
|
$output .= "ob_start();\n"; |
995
|
|
|
} |
996
|
|
|
|
997
|
|
|