1 : <?php
2 : /**
3 : * Smarty Internal Plugin Smarty Template Compiler Base
4 : *
5 : * This file contains the basic classes and methodes for compiling Smarty templates with lexer/parser
6 : *
7 : * @package Smarty
8 : * @subpackage Compiler
9 : * @author Uwe Tews
10 : */
11 :
12 : /**
13 : * Main abstract compiler class
14 : *
15 : * @package Smarty
16 : * @subpackage Compiler
17 : */
18 : abstract class Smarty_Internal_TemplateCompilerBase {
19 :
20 : /**
21 : * hash for nocache sections
22 : *
23 : * @var mixed
24 : */
25 : private $nocache_hash = null;
26 : /**
27 : * suppress generation of nocache code
28 : *
29 : * @var bool
30 : */
31 : public $suppressNocacheProcessing = false;
32 : /**
33 : * suppress generation of merged template code
34 : *
35 : * @var bool
36 : */
37 : public $suppressMergedTemplates = false;
38 : /**
39 : * compile tag objects
40 : *
41 : * @var array
42 : */
43 : public static $_tag_objects = array();
44 : /**
45 : * tag stack
46 : *
47 : * @var array
48 : */
49 : public $_tag_stack = array();
50 : /**
51 : * current template
52 : *
53 : * @var Smarty_Internal_Template
54 : */
55 : public $template = null;
56 : /**
57 : * merged templates
58 : *
59 : * @var array
60 : */
61 : public $merged_templates = array();
62 : /**
63 : * flag when compiling {block}
64 : *
65 : * @var bool
66 : */
67 : public $inheritance = false;
68 : /**
69 : * plugins loaded by default plugin handler
70 : *
71 : * @var array
72 : */
73 : public $default_handler_plugins = array();
74 : /**
75 : * saved preprocessed modifier list
76 : *
77 : * @var mixed
78 : */
79 : public $default_modifier_list = null;
80 : /**
81 : * force compilation of complete template as nocache
82 : * @var boolean
83 : */
84 : public $forceNocache = false;
85 : /**
86 : * suppress Smarty header code in compiled template
87 : * @var bool
88 : */
89 : public $suppressHeader = false;
90 : /**
91 : * suppress template property header code in compiled template
92 : * @var bool
93 : */
94 : public $suppressTemplatePropertyHeader = false;
95 : /**
96 : * flag if compiled template file shall we written
97 : * @var bool
98 : */
99 : public $write_compiled_code = true;
100 : /**
101 : * flag if currently a template function is compiled
102 : * @var bool
103 : */
104 : public $compiles_template_function = false;
105 : /**
106 : * called subfuntions from template function
107 : * @var array
108 : */
109 : public $called_functions = array();
110 : /**
111 : * flags for used modifier plugins
112 : * @var array
113 : */
114 : public $modifier_plugins = array();
115 :
116 : /**
117 : * Initialize compiler
118 : */
119 : public function __construct()
120 : {
121 0 : $this->nocache_hash = str_replace('.', '-', uniqid(rand(), true));
122 0 : }
123 :
124 : /**
125 : * Method to compile a Smarty template
126 : *
127 : * @param Smarty_Internal_Template $template template object to compile
128 : * @return bool true if compiling succeeded, false if it failed
129 : */
130 : public function compileTemplate(Smarty_Internal_Template $template)
131 : {
132 0 : if (empty($template->properties['nocache_hash'])) {
133 0 : $template->properties['nocache_hash'] = $this->nocache_hash;
134 0 : } else {
135 0 : $this->nocache_hash = $template->properties['nocache_hash'];
136 : }
137 : // flag for nochache sections
138 0 : $this->nocache = false;
139 0 : $this->tag_nocache = false;
140 : // save template object in compiler class
141 0 : $this->template = $template;
142 : // reset has noche code flag
143 0 : $this->template->has_nocache_code = false;
144 0 : $this->smarty->_current_file = $saved_filepath = $this->template->source->filepath;
145 : // template header code
146 0 : $template_header = '';
147 0 : if (!$this->suppressHeader) {
148 0 : $template_header .= "<?php /* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") . "\n";
149 0 : $template_header .= " compiled from \"" . $this->template->source->filepath . "\" */ ?>\n";
150 0 : }
151 :
152 : do {
153 : // flag for aborting current and start recompile
154 0 : $this->abort_and_recompile = false;
155 : // get template source
156 0 : $_content = $template->source->content;
157 : // run prefilter if required
158 0 : if (isset($this->smarty->autoload_filters['pre']) || isset($this->smarty->registered_filters['pre'])) {
159 0 : $template->source->content = $_content = Smarty_Internal_Filter_Handler::runFilter('pre', $_content, $template);
160 0 : }
161 : // on empty template just return header
162 0 : if ($_content == '') {
163 0 : if ($this->suppressTemplatePropertyHeader) {
164 0 : $code = '';
165 0 : } else {
166 0 : $code = $template_header . $template->createTemplateCodeFrame();
167 : }
168 0 : return $code;
169 : }
170 : // call compiler
171 0 : $_compiled_code = $this->doCompile($_content);
172 0 : } while ($this->abort_and_recompile);
173 0 : $this->template->source->filepath = $saved_filepath;
174 : // free memory
175 0 : unset($this->parser->root_buffer, $this->parser->current_buffer, $this->parser, $this->lex, $this->template);
176 0 : self::$_tag_objects = array();
177 : // return compiled code to template object
178 0 : $merged_code = '';
179 0 : if (!$this->suppressMergedTemplates) {
180 0 : foreach ($this->merged_templates as $code) {
181 0 : $merged_code .= $code;
182 0 : }
183 0 : }
184 0 : if ($this->suppressTemplatePropertyHeader) {
185 0 : $code = $_compiled_code . $merged_code;
186 0 : } else {
187 0 : $code = $template_header . $template->createTemplateCodeFrame($_compiled_code) . $merged_code;
188 : }
189 : // run postfilter if required
190 0 : if (isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post'])) {
191 0 : $code = Smarty_Internal_Filter_Handler::runFilter('post', $code, $template);
192 0 : }
193 0 : return $code;
194 : }
195 :
196 : /**
197 : * Compile Tag
198 : *
199 : * This is a call back from the lexer/parser
200 : * It executes the required compile plugin for the Smarty tag
201 : *
202 : * @param string $tag tag name
203 : * @param array $args array with tag attributes
204 : * @param array $parameter array with compilation parameter
205 : * @return string compiled code
206 : */
207 : public function compileTag($tag, $args, $parameter = array())
208 : {
209 : // $args contains the attributes parsed and compiled by the lexer/parser
210 : // assume that tag does compile into code, but creates no HTML output
211 0 : $this->has_code = true;
212 0 : $this->has_output = false;
213 : // log tag/attributes
214 0 : if (isset($this->smarty->get_used_tags) && $this->smarty->get_used_tags) {
215 0 : $this->template->used_tags[] = array($tag, $args);
216 0 : }
217 : // check nocache option flag
218 0 : if (in_array("'nocache'",$args) || in_array(array('nocache'=>'true'),$args)
219 0 : || in_array(array('nocache'=>'"true"'),$args) || in_array(array('nocache'=>"'true'"),$args)) {
220 0 : $this->tag_nocache = true;
221 0 : }
222 : // compile the smarty tag (required compile classes to compile the tag are autoloaded)
223 0 : if (($_output = $this->callTagCompiler($tag, $args, $parameter)) === false) {
224 0 : if (isset($this->smarty->template_functions[$tag])) {
225 : // template defined by {template} tag
226 0 : $args['_attr']['name'] = "'" . $tag . "'";
227 0 : $_output = $this->callTagCompiler('call', $args, $parameter);
228 0 : }
229 0 : }
230 0 : if ($_output !== false) {
231 0 : if ($_output !== true) {
232 : // did we get compiled code
233 0 : if ($this->has_code) {
234 : // Does it create output?
235 0 : if ($this->has_output) {
236 0 : $_output .= "\n";
237 0 : }
238 : // return compiled code
239 0 : return $_output;
240 : }
241 0 : }
242 : // tag did not produce compiled code
243 0 : return '';
244 : } else {
245 : // map_named attributes
246 0 : if (isset($args['_attr'])) {
247 0 : foreach ($args['_attr'] as $key => $attribute) {
248 0 : if (is_array($attribute)) {
249 0 : $args = array_merge($args, $attribute);
250 0 : }
251 0 : }
252 0 : }
253 : // not an internal compiler tag
254 0 : if (strlen($tag) < 6 || substr($tag, -5) != 'close') {
255 : // check if tag is a registered object
256 0 : if (isset($this->smarty->registered_objects[$tag]) && isset($parameter['object_methode'])) {
257 0 : $methode = $parameter['object_methode'];
258 0 : if (!in_array($methode, $this->smarty->registered_objects[$tag][3]) &&
259 0 : (empty($this->smarty->registered_objects[$tag][1]) || in_array($methode, $this->smarty->registered_objects[$tag][1]))) {
260 0 : return $this->callTagCompiler('private_object_function', $args, $parameter, $tag, $methode);
261 0 : } elseif (in_array($methode, $this->smarty->registered_objects[$tag][3])) {
262 0 : return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag, $methode);
263 : } else {
264 0 : return $this->trigger_template_error ('unallowed methode "' . $methode . '" in registered object "' . $tag . '"', $this->lex->taglineno);
265 : }
266 : }
267 : // check if tag is registered
268 0 : foreach (array(Smarty::PLUGIN_COMPILER, Smarty::PLUGIN_FUNCTION, Smarty::PLUGIN_BLOCK) as $plugin_type) {
269 0 : if (isset($this->smarty->registered_plugins[$plugin_type][$tag])) {
270 : // if compiler function plugin call it now
271 0 : if ($plugin_type == Smarty::PLUGIN_COMPILER) {
272 0 : $new_args = array();
273 0 : foreach ($args as $key => $mixed) {
274 0 : if (is_array($mixed)) {
275 0 : $new_args = array_merge($new_args, $mixed);
276 0 : } else {
277 0 : $new_args[$key] = $mixed;
278 : }
279 0 : }
280 0 : if (!$this->smarty->registered_plugins[$plugin_type][$tag][1]) {
281 0 : $this->tag_nocache = true;
282 0 : }
283 0 : $function = $this->smarty->registered_plugins[$plugin_type][$tag][0];
284 0 : if (!is_array($function)) {
285 0 : return $function($new_args, $this);
286 0 : } else if (is_object($function[0])) {
287 0 : return $this->smarty->registered_plugins[$plugin_type][$tag][0][0]->$function[1]($new_args, $this);
288 : } else {
289 0 : return call_user_func_array($function, array($new_args, $this));
290 : }
291 : }
292 : // compile registered function or block function
293 0 : if ($plugin_type == Smarty::PLUGIN_FUNCTION || $plugin_type == Smarty::PLUGIN_BLOCK) {
294 0 : return $this->callTagCompiler('private_registered_' . $plugin_type, $args, $parameter, $tag);
295 : }
296 :
297 0 : }
298 0 : }
299 : // check plugins from plugins folder
300 0 : foreach ($this->smarty->plugin_search_order as $plugin_type) {
301 0 : if ($plugin_type == Smarty::PLUGIN_BLOCK && $this->smarty->loadPlugin('smarty_compiler_' . $tag) && (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this))) {
302 0 : $plugin = 'smarty_compiler_' . $tag;
303 0 : if (is_callable($plugin)) {
304 : // convert arguments format for old compiler plugins
305 0 : $new_args = array();
306 0 : foreach ($args as $key => $mixed) {
307 0 : if (is_array($mixed)) {
308 0 : $new_args = array_merge($new_args, $mixed);
309 0 : } else {
310 0 : $new_args[$key] = $mixed;
311 : }
312 0 : }
313 0 : return $plugin($new_args, $this->smarty);
314 : }
315 0 : if (class_exists($plugin, false)) {
316 0 : $plugin_object = new $plugin;
317 0 : if (method_exists($plugin_object, 'compile')) {
318 0 : return $plugin_object->compile($args, $this);
319 : }
320 0 : }
321 0 : throw new SmartyException("Plugin \"{$tag}\" not callable");
322 : } else {
323 0 : if ($function = $this->getPlugin($tag, $plugin_type)) {
324 0 : if(!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this)) {
325 0 : return $this->callTagCompiler('private_' . $plugin_type . '_plugin', $args, $parameter, $tag, $function);
326 : }
327 0 : }
328 : }
329 0 : }
330 0 : if (is_callable($this->smarty->default_plugin_handler_func)) {
331 0 : $found = false;
332 : // look for already resolved tags
333 0 : foreach ($this->smarty->plugin_search_order as $plugin_type) {
334 0 : if (isset($this->default_handler_plugins[$plugin_type][$tag])) {
335 0 : $found = true;
336 0 : break;
337 : }
338 0 : }
339 0 : if (!$found) {
340 : // call default handler
341 0 : foreach ($this->smarty->plugin_search_order as $plugin_type) {
342 0 : if ($this->getPluginFromDefaultHandler($tag, $plugin_type)) {
343 0 : $found = true;
344 0 : break;
345 : }
346 0 : }
347 0 : }
348 0 : if ($found) {
349 : // if compiler function plugin call it now
350 0 : if ($plugin_type == Smarty::PLUGIN_COMPILER) {
351 0 : $new_args = array();
352 0 : foreach ($args as $mixed) {
353 0 : $new_args = array_merge($new_args, $mixed);
354 0 : }
355 0 : $function = $this->default_handler_plugins[$plugin_type][$tag][0];
356 0 : if (!is_array($function)) {
357 0 : return $function($new_args, $this);
358 0 : } else if (is_object($function[0])) {
359 0 : return $this->default_handler_plugins[$plugin_type][$tag][0][0]->$function[1]($new_args, $this);
360 : } else {
361 0 : return call_user_func_array($function, array($new_args, $this));
362 : }
363 : } else {
364 0 : return $this->callTagCompiler('private_registered_' . $plugin_type, $args, $parameter, $tag);
365 : }
366 : }
367 0 : }
368 0 : } else {
369 : // compile closing tag of block function
370 0 : $base_tag = substr($tag, 0, -5);
371 : // check if closing tag is a registered object
372 0 : if (isset($this->smarty->registered_objects[$base_tag]) && isset($parameter['object_methode'])) {
373 0 : $methode = $parameter['object_methode'];
374 0 : if (in_array($methode, $this->smarty->registered_objects[$base_tag][3])) {
375 0 : return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag, $methode);
376 : } else {
377 0 : return $this->trigger_template_error ('unallowed closing tag methode "' . $methode . '" in registered object "' . $base_tag . '"', $this->lex->taglineno);
378 : }
379 : }
380 : // registered block tag ?
381 0 : if (isset($this->smarty->registered_plugins[Smarty::PLUGIN_BLOCK][$base_tag]) || isset($this->default_handler_plugins[Smarty::PLUGIN_BLOCK][$base_tag])) {
382 0 : return $this->callTagCompiler('private_registered_block', $args, $parameter, $tag);
383 : }
384 : // block plugin?
385 0 : if ($function = $this->getPlugin($base_tag, Smarty::PLUGIN_BLOCK)) {
386 0 : return $this->callTagCompiler('private_block_plugin', $args, $parameter, $tag, $function);
387 : }
388 0 : if ($this->smarty->loadPlugin('smarty_compiler_' . $tag)) {
389 0 : $plugin = 'smarty_compiler_' . $tag;
390 0 : if (is_callable($plugin)) {
391 0 : return $plugin($args, $this->smarty);
392 : }
393 0 : if (class_exists($plugin, false)) {
394 0 : $plugin_object = new $plugin;
395 0 : if (method_exists($plugin_object, 'compile')) {
396 0 : return $plugin_object->compile($args, $this);
397 : }
398 0 : }
399 0 : throw new SmartyException("Plugin \"{$tag}\" not callable");
400 : }
401 : }
402 0 : $this->trigger_template_error ("unknown tag \"" . $tag . "\"", $this->lex->taglineno);
403 : }
404 0 : }
405 :
406 : /**
407 : * lazy loads internal compile plugin for tag and calls the compile methode
408 : *
409 : * compile objects cached for reuse.
410 : * class name format: Smarty_Internal_Compile_TagName
411 : * plugin filename format: Smarty_Internal_Tagname.php
412 : *
413 : * @param string $tag tag name
414 : * @param array $args list of tag attributes
415 : * @param mixed $param1 optional parameter
416 : * @param mixed $param2 optional parameter
417 : * @param mixed $param3 optional parameter
418 : * @return string compiled code
419 : */
420 : public function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null)
421 : {
422 : // re-use object if already exists
423 0 : if (isset(self::$_tag_objects[$tag])) {
424 : // compile this tag
425 0 : return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3);
426 : }
427 : // lazy load internal compiler plugin
428 0 : $class_name = 'Smarty_Internal_Compile_' . $tag;
429 0 : if ($this->smarty->loadPlugin($class_name)) {
430 : // check if tag allowed by security
431 0 : if (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this)) {
432 : // use plugin if found
433 0 : self::$_tag_objects[$tag] = new $class_name;
434 : // compile this tag
435 0 : return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3);
436 : }
437 0 : }
438 : // no internal compile plugin for this tag
439 0 : return false;
440 : }
441 :
442 : /**
443 : * Check for plugins and return function name
444 : *
445 : * @param string $pugin_name name of plugin or function
446 : * @param string $plugin_type type of plugin
447 : * @return string call name of function
448 : */
449 : public function getPlugin($plugin_name, $plugin_type)
450 : {
451 0 : $function = null;
452 0 : if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
453 0 : if (isset($this->template->required_plugins['nocache'][$plugin_name][$plugin_type])) {
454 0 : $function = $this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['function'];
455 0 : } else if (isset($this->template->required_plugins['compiled'][$plugin_name][$plugin_type])) {
456 0 : $this->template->required_plugins['nocache'][$plugin_name][$plugin_type] = $this->template->required_plugins['compiled'][$plugin_name][$plugin_type];
457 0 : $function = $this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['function'];
458 0 : }
459 0 : } else {
460 0 : if (isset($this->template->required_plugins['compiled'][$plugin_name][$plugin_type])) {
461 0 : $function = $this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['function'];
462 0 : } else if (isset($this->template->required_plugins['nocache'][$plugin_name][$plugin_type])) {
463 0 : $this->template->required_plugins['compiled'][$plugin_name][$plugin_type] = $this->template->required_plugins['nocache'][$plugin_name][$plugin_type];
464 0 : $function = $this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['function'];
465 0 : }
466 : }
467 0 : if (isset($function)) {
468 0 : if ($plugin_type == 'modifier') {
469 0 : $this->modifier_plugins[$plugin_name] = true;
470 0 : }
471 0 : return $function;
472 : }
473 : // loop through plugin dirs and find the plugin
474 0 : $function = 'smarty_' . $plugin_type . '_' . $plugin_name;
475 0 : $file = $this->smarty->loadPlugin($function, false);
476 :
477 0 : if (is_string($file)) {
478 0 : if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
479 0 : $this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['file'] = $file;
480 0 : $this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['function'] = $function;
481 0 : } else {
482 0 : $this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['file'] = $file;
483 0 : $this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['function'] = $function;
484 : }
485 0 : if ($plugin_type == 'modifier') {
486 0 : $this->modifier_plugins[$plugin_name] = true;
487 0 : }
488 0 : return $function;
489 : }
490 0 : if (is_callable($function)) {
491 : // plugin function is defined in the script
492 0 : return $function;
493 : }
494 0 : return false;
495 : }
496 :
497 : /**
498 : * Check for plugins by default plugin handler
499 : *
500 : * @param string $tag name of tag
501 : * @param string $plugin_type type of plugin
502 : * @return boolean true if found
503 : */
504 : public function getPluginFromDefaultHandler($tag, $plugin_type)
505 : {
506 0 : $callback = null;
507 0 : $script = null;
508 0 : $result = call_user_func_array(
509 0 : $this->smarty->default_plugin_handler_func,
510 0 : array($tag, $plugin_type, $this->template, &$callback, &$script)
511 0 : );
512 0 : if ($result) {
513 0 : if ($script !== null) {
514 0 : if (is_file($script)) {
515 0 : if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
516 0 : $this->template->required_plugins['nocache'][$tag][$plugin_type]['file'] = $script;
517 0 : $this->template->required_plugins['nocache'][$tag][$plugin_type]['function'] = $callback;
518 0 : } else {
519 0 : $this->template->required_plugins['compiled'][$tag][$plugin_type]['file'] = $script;
520 0 : $this->template->required_plugins['compiled'][$tag][$plugin_type]['function'] = $callback;
521 : }
522 0 : include_once $script;
523 0 : } else {
524 0 : throw new SmartyCompilerException("Plugin or modifer script file $script not found");
525 : }
526 0 : }
527 0 : if (is_callable($callback)) {
528 0 : $this->default_handler_plugins[$plugin_type][$tag] = array($callback, true, array());
529 0 : return true;
530 : } else {
531 0 : throw new SmartyCompilerException("Function for plugin or modifier $tag not callable");
532 : }
533 : }
534 0 : return false;
535 : }
536 :
537 : /**
538 : * Inject inline code for nocache template sections
539 : *
540 : * This method gets the content of each template element from the parser.
541 : * If the content is compiled code and it should be not cached the code is injected
542 : * into the rendered output.
543 : *
544 : * @param string $content content of template element
545 : * @param boolean $is_code true if content is compiled code
546 : * @return string content
547 : */
548 : public function processNocacheCode($content, $is_code)
549 : {
550 : // If the template is not evaluated and we have a nocache section and or a nocache tag
551 0 : if ($is_code && !empty($content)) {
552 : // generate replacement code
553 0 : if ((!($this->template->source->recompiled) || $this->forceNocache) && $this->template->caching && !$this->suppressNocacheProcessing &&
554 0 : ($this->nocache || $this->tag_nocache || $this->forceNocache == 2)) {
555 0 : $this->template->has_nocache_code = true;
556 0 : $_output = str_replace("'", "\'", $content);
557 0 : $_output = str_replace('\\\\', '\\\\\\\\', $_output);
558 0 : $_output = str_replace("^#^", "'", $_output);
559 0 : $_output = "<?php echo '/*%%SmartyNocache:{$this->nocache_hash}%%*/" . $_output . "/*/%%SmartyNocache:{$this->nocache_hash}%%*/';?>\n";
560 : // make sure we include modifer plugins for nocache code
561 0 : foreach ($this->modifier_plugins as $plugin_name => $dummy) {
562 0 : if (isset($this->template->required_plugins['compiled'][$plugin_name]['modifier'])) {
563 0 : $this->template->required_plugins['nocache'][$plugin_name]['modifier'] = $this->template->required_plugins['compiled'][$plugin_name]['modifier'];
564 0 : }
565 0 : }
566 0 : } else {
567 0 : $_output = $content;
568 : }
569 0 : } else {
570 0 : $_output = $content;
571 : }
572 0 : $this->suppressNocacheProcessing = false;
573 0 : $this->tag_nocache = false;
574 0 : return $_output;
575 : }
576 :
577 : /**
578 : * display compiler error messages without dying
579 : *
580 : * If parameter $args is empty it is a parser detected syntax error.
581 : * In this case the parser is called to obtain information about expected tokens.
582 : *
583 : * If parameter $args contains a string this is used as error message
584 : *
585 : * @param string $args individual error message or null
586 : * @param string $line line-number
587 : * @throws SmartyCompilerException when an unexpected token is found
588 : */
589 : public function trigger_template_error($args = null, $line = null)
590 : {
591 : // get template source line which has error
592 0 : if (!isset($line)) {
593 0 : $line = $this->lex->line;
594 0 : }
595 0 : $match = preg_split("/\n/", $this->lex->data);
596 0 : $error_text = 'Syntax Error in template "' . $this->template->source->filepath . '" on line ' . $line . ' "' . htmlspecialchars(trim(preg_replace('![\t\r\n]+!',' ',$match[$line-1]))) . '" ';
597 0 : if (isset($args)) {
598 : // individual error message
599 0 : $error_text .= $args;
600 0 : } else {
601 : // expected token from parser
602 0 : $error_text .= ' - Unexpected "' . $this->lex->value.'"';
603 0 : if (count($this->parser->yy_get_expected_tokens($this->parser->yymajor)) <= 4 ) {
604 0 : foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
605 0 : $exp_token = $this->parser->yyTokenName[$token];
606 0 : if (isset($this->lex->smarty_token_names[$exp_token])) {
607 : // token type from lexer
608 0 : $expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"';
609 0 : } else {
610 : // otherwise internal token name
611 0 : $expect[] = $this->parser->yyTokenName[$token];
612 : }
613 0 : }
614 0 : $error_text .= ', expected one of: ' . implode(' , ', $expect);
615 0 : }
616 : }
617 0 : throw new SmartyCompilerException($error_text);
618 : }
619 :
620 : }
621 :
|