4 * Smarty Internal Plugin Smarty Template Compiler Base
5 * This file contains the basic classes and methods for compiling Smarty templates with lexer/parser
13 * Main abstract compiler class
16 * @subpackage Compiler
18 * @property Smarty_Internal_SmartyTemplateCompiler $prefixCompiledCode = ''
19 * @property Smarty_Internal_SmartyTemplateCompiler $postfixCompiledCode = ''
20 * @method registerPostCompileCallback($callback, $parameter = array(), $key = null, $replace = false)
21 * @method unregisterPostCompileCallback($key)
23 abstract class Smarty_Internal_TemplateCompilerBase
31 public $smarty = null;
36 * @var Smarty_Internal_Templateparser
38 public $parser = null;
41 * hash for nocache sections
45 public $nocache_hash = null;
48 * suppress generation of nocache code
52 public $suppressNocacheProcessing = false;
55 * compile tag objects cache
59 static $_tag_objects = array();
66 public $_tag_stack = array();
71 * @var Smarty_Internal_Template
73 public $template = null;
76 * merged included sub template data
80 public $mergedSubTemplatesData = array();
83 * merged sub template code
87 public $mergedSubTemplatesCode = array();
90 * collected template properties during compilation
94 public $templateProperties = array();
97 * source line offset for error messages
101 public $trace_line_offset = 0;
108 public $trace_uid = '';
115 public $trace_filepath = '';
118 * stack for tracing file and line of nested {block} tags
122 public $trace_stack = array();
125 * plugins loaded by default plugin handler
129 public $default_handler_plugins = array();
132 * saved preprocessed modifier list
136 public $default_modifier_list = null;
139 * force compilation of complete template as nocache
143 public $forceNocache = false;
146 * flag if compiled template file shall we written
150 public $write_compiled_code = true;
157 public $tpl_function = array();
160 * called sub functions from template function
164 public $called_functions = array();
167 * compiled template or block function code
171 public $blockOrFunctionCode = '';
174 * php_handling setting either from Smarty or security
178 public $php_handling = 0;
181 * flags for used modifier plugins
185 public $modifier_plugins = array();
188 * type of already compiled modifier
192 public $known_modifier_type = array();
195 * parent compiler object for merged subtemplates and template functions
197 * @var Smarty_Internal_TemplateCompilerBase
199 public $parent_compiler = null;
202 * Flag true when compiling nocache section
206 public $nocache = false;
209 * Flag true when tag is compiled as nocache
213 public $tag_nocache = false;
216 * Compiled tag prefix code
220 public $prefix_code = array();
227 public $prefixCodeStack = array();
230 * Tag has compiled code
234 public $has_code = false;
237 * A variable string was compiled
241 public $has_variable_string = false;
248 public $has_output = false;
251 * Stack for {setfilter} {/setfilter}
255 public $variable_filter_stack = array();
258 * variable filters for {setfilter} {/setfilter}
262 public $variable_filters = array();
265 * Nesting count of looping tags like {foreach}, {for}, {section}, {while}
269 public $loopNesting = 0;
276 public $stripRegEx = '![\t ]*[\r\n]+[\t ]*!';
279 * plugin search order
283 public $plugin_search_order = array('function', 'block', 'compiler', 'class');
286 * General storage area for tag compiler plugins
290 public $_cache = array();
293 * counter for prefix variable number
297 public static $prefixVariableNumber = 0;
300 * method to compile a Smarty template
302 * @param mixed $_content template source
303 * @param bool $isTemplateSource
305 * @return bool true if compiling succeeded, false if it failed
307 abstract protected function doCompile($_content, $isTemplateSource = false);
310 * Initialize compiler
312 * @param Smarty $smarty global instance
314 public function __construct(Smarty $smarty)
316 $this->smarty = $smarty;
317 $this->nocache_hash = str_replace(array('.', ','), '_', uniqid(rand(), true));
321 * Method to compile a Smarty template
323 * @param Smarty_Internal_Template $template template object to compile
324 * @param bool $nocache true is shall be compiled in nocache mode
325 * @param null|Smarty_Internal_TemplateCompilerBase $parent_compiler
327 * @return bool true if compiling succeeded, false if it failed
330 public function compileTemplate(Smarty_Internal_Template $template, $nocache = null,
331 Smarty_Internal_TemplateCompilerBase $parent_compiler = null)
333 // get code frame of compiled template
334 $_compiled_code = $template->smarty->ext->_codeFrame->create($template,
335 $this->compileTemplateSource($template, $nocache,
337 $this->postFilter($this->blockOrFunctionCode) .
338 join('', $this->mergedSubTemplatesCode), false,
340 return $_compiled_code;
344 * Compile template source and run optional post filter
346 * @param \Smarty_Internal_Template $template
347 * @param null|bool $nocache flag if template must be compiled in nocache mode
348 * @param \Smarty_Internal_TemplateCompilerBase $parent_compiler
353 public function compileTemplateSource(Smarty_Internal_Template $template, $nocache = null,
354 Smarty_Internal_TemplateCompilerBase $parent_compiler = null)
357 // save template object in compiler class
358 $this->template = $template;
359 if (property_exists($this->template->smarty, 'plugin_search_order')) {
360 $this->plugin_search_order = $this->template->smarty->plugin_search_order;
362 if ($this->smarty->debugging) {
363 if (!isset($this->smarty->_debug)) {
364 $this->smarty->_debug = new Smarty_Internal_Debug();
366 $this->smarty->_debug->start_compile($this->template);
368 if (isset($this->template->smarty->security_policy)) {
369 $this->php_handling = $this->template->smarty->security_policy->php_handling;
371 $this->php_handling = $this->template->smarty->php_handling;
373 $this->parent_compiler = $parent_compiler ? $parent_compiler : $this;
374 $nocache = isset($nocache) ? $nocache : false;
375 if (empty($template->compiled->nocache_hash)) {
376 $template->compiled->nocache_hash = $this->nocache_hash;
378 $this->nocache_hash = $template->compiled->nocache_hash;
380 // flag for nocache sections
381 $this->nocache = $nocache;
382 $this->tag_nocache = false;
383 // reset has nocache code flag
384 $this->template->compiled->has_nocache_code = false;
385 $this->has_variable_string = false;
386 $this->prefix_code = array();
387 // add file dependency
388 if ($this->smarty->merge_compiled_includes || $this->template->source->handler->checkTimestamps()) {
389 $this->parent_compiler->template->compiled->file_dependency[ $this->template->source->uid ] =
390 array($this->template->source->filepath, $this->template->source->getTimeStamp(),
391 $this->template->source->type,);
393 $this->smarty->_current_file = $this->template->source->filepath;
394 // get template source
395 if (!empty($this->template->source->components)) {
396 // we have array of inheritance templates by extends: resource
397 // generate corresponding source code sequence
399 Smarty_Internal_Compile_Extends::extendsSourceArrayCode($this->template->source->components);
401 // get template source
402 $_content = $this->template->source->getContent();
404 $_compiled_code = $this->postFilter($this->doCompile($this->preFilter($_content), true));
406 catch (Exception $e) {
407 if ($this->smarty->debugging) {
408 $this->smarty->_debug->end_compile($this->template);
410 $this->_tag_stack = array();
412 $this->parent_compiler = null;
413 $this->template = null;
414 $this->parser = null;
417 if ($this->smarty->debugging) {
418 $this->smarty->_debug->end_compile($this->template);
420 $this->parent_compiler = null;
421 $this->parser = null;
422 return $_compiled_code;
426 * Optionally process compiled code by post filter
428 * @param string $code compiled code
431 * @throws \SmartyException
433 public function postFilter($code)
435 // run post filter if on code
437 (isset($this->smarty->autoload_filters[ 'post' ]) || isset($this->smarty->registered_filters[ 'post' ]))
439 return $this->smarty->ext->_filterHandler->runFilter('post', $code, $this->template);
446 * Run optional prefilter
448 * @param string $_content template source
451 * @throws \SmartyException
453 public function preFilter($_content)
455 // run pre filter if required
456 if ($_content != '' &&
457 ((isset($this->smarty->autoload_filters[ 'pre' ]) || isset($this->smarty->registered_filters[ 'pre' ])))
459 return $this->smarty->ext->_filterHandler->runFilter('pre', $_content, $this->template);
467 * This is a call back from the lexer/parser
469 * Save current prefix code
471 * Merge tag prefix code with saved one
472 * (required nested tags in attributes)
474 * @param string $tag tag name
475 * @param array $args array with tag attributes
476 * @param array $parameter array with compilation parameter
478 * @throws SmartyCompilerException
479 * @throws SmartyException
480 * @return string compiled code
482 public function compileTag($tag, $args, $parameter = array())
484 $this->prefixCodeStack[] = $this->prefix_code;
485 $this->prefix_code = array();
486 $result = $this->compileTag2($tag, $args, $parameter);
487 $this->prefix_code = array_merge($this->prefix_code, array_pop($this->prefixCodeStack));
494 * @param string $tag tag name
495 * @param array $args array with tag attributes
496 * @param array $parameter array with compilation parameter
498 * @throws SmartyCompilerException
499 * @throws SmartyException
500 * @return string compiled code
502 private function compileTag2($tag, $args, $parameter)
505 // $args contains the attributes parsed and compiled by the lexer/parser
506 // assume that tag does compile into code, but creates no HTML output
507 $this->has_code = true;
508 $this->has_output = false;
509 // log tag/attributes
510 if (isset($this->smarty->_cache[ 'get_used_tags' ])) {
511 $this->template->_cache[ 'used_tags' ][] = array($tag, $args);
513 // check nocache option flag
514 foreach ($args as $arg) {
515 if (!is_array($arg)) {
516 if ($arg === "'nocache'" || $arg === 'nocache') {
517 $this->tag_nocache = true;
520 foreach ($arg as $k => $v) {
521 if (($k === "'nocache'" || $k === 'nocache') && (trim($v, "'\" ") == 'true')) {
522 $this->tag_nocache = true;
527 // compile the smarty tag (required compile classes to compile the tag are auto loaded)
528 if (($_output = $this->callTagCompiler($tag, $args, $parameter)) === false) {
529 if (isset($this->parent_compiler->tpl_function[ $tag ]) ||
530 (isset ($this->template->smarty->ext->_tplFunction) && $this->template->smarty->ext->_tplFunction->getTplFunction($this->template, $tag) !== false)) {
531 // template defined by {template} tag
532 $args[ '_attr' ][ 'name' ] = "'" . $tag . "'";
533 $_output = $this->callTagCompiler('call', $args, $parameter);
536 if ($_output !== false) {
537 if ($_output !== true) {
538 // did we get compiled code
539 if ($this->has_code) {
540 // Does it create output?
541 if ($this->has_output) {
544 // return compiled code
548 // tag did not produce compiled code
551 // map_named attributes
552 if (isset($args[ '_attr' ])) {
553 foreach ($args[ '_attr' ] as $key => $attribute) {
554 if (is_array($attribute)) {
555 $args = array_merge($args, $attribute);
559 // not an internal compiler tag
560 if (strlen($tag) < 6 || substr($tag, - 5) != 'close') {
561 // check if tag is a registered object
562 if (isset($this->smarty->registered_objects[ $tag ]) && isset($parameter[ 'object_method' ])) {
563 $method = $parameter[ 'object_method' ];
564 if (!in_array($method, $this->smarty->registered_objects[ $tag ][ 3 ]) &&
565 (empty($this->smarty->registered_objects[ $tag ][ 1 ]) ||
566 in_array($method, $this->smarty->registered_objects[ $tag ][ 1 ]))
568 return $this->callTagCompiler('private_object_function', $args, $parameter, $tag, $method);
569 } elseif (in_array($method, $this->smarty->registered_objects[ $tag ][ 3 ])) {
570 return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag,
574 $this->trigger_template_error('not allowed method "' . $method . '" in registered object "' .
575 $tag . '"', null, true);
578 // check if tag is registered
579 foreach (array(Smarty::PLUGIN_COMPILER, Smarty::PLUGIN_FUNCTION, Smarty::PLUGIN_BLOCK,) as $plugin_type)
581 if (isset($this->smarty->registered_plugins[ $plugin_type ][ $tag ])) {
582 // if compiler function plugin call it now
583 if ($plugin_type == Smarty::PLUGIN_COMPILER) {
585 foreach ($args as $key => $mixed) {
586 if (is_array($mixed)) {
587 $new_args = array_merge($new_args, $mixed);
589 $new_args[ $key ] = $mixed;
592 if (!$this->smarty->registered_plugins[ $plugin_type ][ $tag ][ 1 ]) {
593 $this->tag_nocache = true;
595 return call_user_func_array($this->smarty->registered_plugins[ $plugin_type ][ $tag ][ 0 ],
596 array($new_args, $this));
598 // compile registered function or block function
599 if ($plugin_type == Smarty::PLUGIN_FUNCTION || $plugin_type == Smarty::PLUGIN_BLOCK) {
600 return $this->callTagCompiler('private_registered_' . $plugin_type, $args, $parameter,
605 // check plugins from plugins folder
606 foreach ($this->plugin_search_order as $plugin_type) {
607 if ($plugin_type == Smarty::PLUGIN_COMPILER &&
608 $this->smarty->loadPlugin('smarty_compiler_' . $tag) &&
609 (!isset($this->smarty->security_policy) ||
610 $this->smarty->security_policy->isTrustedTag($tag, $this))
612 $plugin = 'smarty_compiler_' . $tag;
613 if (is_callable($plugin)) {
614 // convert arguments format for old compiler plugins
616 foreach ($args as $key => $mixed) {
617 if (is_array($mixed)) {
618 $new_args = array_merge($new_args, $mixed);
620 $new_args[ $key ] = $mixed;
624 return $plugin($new_args, $this->smarty);
626 if (class_exists($plugin, false)) {
627 $plugin_object = new $plugin;
628 if (method_exists($plugin_object, 'compile')) {
629 return $plugin_object->compile($args, $this);
632 throw new SmartyException("Plugin \"{$tag}\" not callable");
634 if ($function = $this->getPlugin($tag, $plugin_type)) {
635 if (!isset($this->smarty->security_policy) ||
636 $this->smarty->security_policy->isTrustedTag($tag, $this)
638 return $this->callTagCompiler('private_' . $plugin_type . '_plugin', $args, $parameter,
644 if (is_callable($this->smarty->default_plugin_handler_func)) {
646 // look for already resolved tags
647 foreach ($this->plugin_search_order as $plugin_type) {
648 if (isset($this->default_handler_plugins[ $plugin_type ][ $tag ])) {
654 // call default handler
655 foreach ($this->plugin_search_order as $plugin_type) {
656 if ($this->getPluginFromDefaultHandler($tag, $plugin_type)) {
663 // if compiler function plugin call it now
664 if ($plugin_type == Smarty::PLUGIN_COMPILER) {
666 foreach ($args as $mixed) {
667 $new_args = array_merge($new_args, $mixed);
669 return call_user_func_array($this->default_handler_plugins[ $plugin_type ][ $tag ][ 0 ],
670 array($new_args, $this));
672 return $this->callTagCompiler('private_registered_' . $plugin_type, $args, $parameter,
678 // compile closing tag of block function
679 $base_tag = substr($tag, 0, - 5);
680 // check if closing tag is a registered object
681 if (isset($this->smarty->registered_objects[ $base_tag ]) && isset($parameter[ 'object_method' ])) {
682 $method = $parameter[ 'object_method' ];
683 if (in_array($method, $this->smarty->registered_objects[ $base_tag ][ 3 ])) {
684 return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag,
688 $this->trigger_template_error('not allowed closing tag method "' . $method .
689 '" in registered object "' . $base_tag . '"', null, true);
692 // registered block tag ?
693 if (isset($this->smarty->registered_plugins[ Smarty::PLUGIN_BLOCK ][ $base_tag ]) ||
694 isset($this->default_handler_plugins[ Smarty::PLUGIN_BLOCK ][ $base_tag ])
696 return $this->callTagCompiler('private_registered_block', $args, $parameter, $tag);
698 // registered function tag ?
699 if (isset($this->smarty->registered_plugins[ Smarty::PLUGIN_FUNCTION ][ $tag ])) {
700 return $this->callTagCompiler('private_registered_function', $args, $parameter, $tag);
703 if ($function = $this->getPlugin($base_tag, Smarty::PLUGIN_BLOCK)) {
704 return $this->callTagCompiler('private_block_plugin', $args, $parameter, $tag, $function);
707 if ($function = $this->getPlugin($tag, Smarty::PLUGIN_FUNCTION)) {
708 if (!isset($this->smarty->security_policy) ||
709 $this->smarty->security_policy->isTrustedTag($tag, $this)
711 return $this->callTagCompiler('private_function_plugin', $args, $parameter, $tag, $function);
714 // registered compiler plugin ?
715 if (isset($this->smarty->registered_plugins[ Smarty::PLUGIN_COMPILER ][ $tag ])) {
716 // if compiler function plugin call it now
718 if (!$this->smarty->registered_plugins[ Smarty::PLUGIN_COMPILER ][ $tag ][ 1 ]) {
719 $this->tag_nocache = true;
721 return call_user_func_array($this->smarty->registered_plugins[ Smarty::PLUGIN_COMPILER ][ $tag ][ 0 ],
722 array($args, $this));
724 if ($this->smarty->loadPlugin('smarty_compiler_' . $tag)) {
725 $plugin = 'smarty_compiler_' . $tag;
726 if (is_callable($plugin)) {
727 return $plugin($args, $this->smarty);
729 if (class_exists($plugin, false)) {
730 $plugin_object = new $plugin;
731 if (method_exists($plugin_object, 'compile')) {
732 return $plugin_object->compile($args, $this);
735 throw new SmartyException("Plugin \"{$tag}\" not callable");
738 $this->trigger_template_error("unknown tag \"" . $tag . "\"", null, true);
745 * @param string $variable
749 public function compileVariable($variable)
751 if (strpos($variable, '(') == 0) {
752 // not a variable variable
753 $var = trim($variable, '\'');
754 $this->tag_nocache = $this->tag_nocache |
755 $this->template->ext->getTemplateVars->_getVariable($this->template, $var, null, true,
757 // todo $this->template->compiled->properties['variables'][$var] = $this->tag_nocache | $this->nocache;
759 return '$_smarty_tpl->tpl_vars[' . $variable . ']->value';
763 * compile config variable
765 * @param string $variable
769 public function compileConfigVariable($variable)
771 // return '$_smarty_tpl->config_vars[' . $variable . ']';
772 return '$_smarty_tpl->smarty->ext->configLoad->_getConfigVariable($_smarty_tpl, ' . $variable . ')';
776 * compile PHP function call
778 * @param string $name
779 * @param array $parameter
783 public function compilePHPFunctionCall($name, $parameter)
785 if (!$this->smarty->security_policy || $this->smarty->security_policy->isTrustedPhpFunction($name, $this)) {
786 if (strcasecmp($name, 'isset') === 0 || strcasecmp($name, 'empty') === 0 ||
787 strcasecmp($name, 'array') === 0 || is_callable($name)
789 $func_name = strtolower($name);
790 $par = implode(',', $parameter);
791 $parHasFuction = strpos($par, '(') !== false;
792 if ($func_name == 'isset') {
793 if (count($parameter) == 0) {
794 $this->trigger_template_error('Illegal number of paramer in "isset()"');
796 if ($parHasFuction) {
797 $prefixVar = $this->getNewPrefixVariable();
798 $this->appendPrefixCode("<?php $prefixVar" . '=' . $par . ';?>');
799 $isset_par = $prefixVar;
801 $isset_par = str_replace("')->value", "',null,true,false)->value", $par);
803 return $name . "(" . $isset_par . ")";
804 } elseif (in_array($func_name, array('empty', 'reset', 'current', 'end', 'prev', 'next'))) {
805 if (count($parameter) != 1) {
806 $this->trigger_template_error('Illegal number of paramer in "empty()"');
808 if ($func_name == 'empty') {
809 if ($parHasFuction) {
810 $prefixVar = $this->getNewPrefixVariable();
811 $this->appendPrefixCode("<?php $prefixVar" . '=' . $par . ';?>');
813 return $func_name . '(' . $prefixVar . ')';
815 return $func_name . '(' .
816 str_replace("')->value", "',null,true,false)->value", $parameter[ 0 ]) . ')';
819 return $func_name . '(' . $parameter[ 0 ] . ')';
822 return $name . "(" . implode(',', $parameter) . ")";
825 $this->trigger_template_error("unknown function \"" . $name . "\"");
831 * This method is called from parser to process a text content section
832 * - remove text from inheritance child templates as they may generate output
833 * - strip text if strip is enabled
835 * @param string $text
837 * @return null|\Smarty_Internal_ParseTree_Text
839 public function processText($text)
841 if ((string) $text != '') {
844 if ($this->parser->strip) {
845 if (strpos($text, '<') !== false) {
846 // capture html elements not to be messed with
848 if (preg_match_all('#(<script[^>]*>.*?</script[^>]*>)|(<textarea[^>]*>.*?</textarea[^>]*>)|(<pre[^>]*>.*?</pre[^>]*>)#is',
849 $text, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
850 foreach ($matches as $match) {
851 $store[] = $match[ 0 ][ 0 ];
852 $_length = strlen($match[ 0 ][ 0 ]);
853 $replace = '@!@SMARTY:' . $_store . ':SMARTY@!@';
854 $text = substr_replace($text, $replace, $match[ 0 ][ 1 ] - $_offset, $_length);
856 $_offset += $_length - strlen($replace);
860 $expressions = array(// replace multiple spaces between tags by a single space
861 '#(:SMARTY@!@|>)[\040\011]+(?=@!@SMARTY:|<)#s' => '\1 \2',
862 // remove newline between tags
863 '#(:SMARTY@!@|>)[\040\011]*[\n]\s*(?=@!@SMARTY:|<)#s' => '\1\2',
864 // remove multiple spaces between attributes (but not in attribute values!)
865 '#(([a-z0-9]\s*=\s*("[^"]*?")|(\'[^\']*?\'))|<[a-z0-9_]+)\s+([a-z/>])#is' => '\1 \5',
866 '#>[\040\011]+$#Ss' => '> ', '#>[\040\011]*[\n]\s*$#Ss' => '>',
867 $this->stripRegEx => '',);
869 $text = preg_replace(array_keys($expressions), array_values($expressions), $text);
871 if (preg_match_all('#@!@SMARTY:([0-9]+):SMARTY@!@#is', $text, $matches,
872 PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
873 foreach ($matches as $match) {
874 $_length = strlen($match[ 0 ][ 0 ]);
875 $replace = $store[ $match[ 1 ][ 0 ] ];
876 $text = substr_replace($text, $replace, $match[ 0 ][ 1 ] + $_offset, $_length);
878 $_offset += strlen($replace) - $_length;
883 $text = preg_replace($this->stripRegEx, '', $text);
886 return new Smarty_Internal_ParseTree_Text($text);
892 * lazy loads internal compile plugin for tag and calls the compile method
893 * compile objects cached for reuse.
894 * class name format: Smarty_Internal_Compile_TagName
895 * plugin filename format: Smarty_Internal_TagName.php
897 * @param string $tag tag name
898 * @param array $args list of tag attributes
899 * @param mixed $param1 optional parameter
900 * @param mixed $param2 optional parameter
901 * @param mixed $param3 optional parameter
903 * @return string|bool compiled code or false
905 public function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null)
907 $tagCompiler = $this->getTagCompiler($tag);
909 return $tagCompiler === false ? false : $tagCompiler->compile($args, $this, $param1, $param2, $param3);
913 * lazy loads internal compile plugin for tag compile objects cached for reuse.
915 * class name format: Smarty_Internal_Compile_TagName
916 * plugin filename format: Smarty_Internal_TagName.php
918 * @param string $tag tag name
920 * @return Smarty_Internal_CompileBase|bool tag compiler object or false if not found
922 public function getTagCompiler($tag)
924 // re-use object if already exists
925 if (!isset(self::$_tag_objects[ $tag ])) {
926 // lazy load internal compiler plugin
927 $_tag = explode('_', $tag);
928 $_tag = array_map('ucfirst', $_tag);
929 $class_name = 'Smarty_Internal_Compile_' . implode('_', $_tag);
930 if (class_exists($class_name) &&
931 (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this))
933 self::$_tag_objects[ $tag ] = new $class_name;
935 self::$_tag_objects[ $tag ] = false;
938 return self::$_tag_objects[ $tag ];
942 * Check for plugins and return function name
944 * @param $plugin_name
945 * @param string $plugin_type type of plugin
947 * @return string call name of function
949 public function getPlugin($plugin_name, $plugin_type)
952 if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
953 if (isset($this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $plugin_name ][ $plugin_type ])) {
955 $this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $plugin_name ][ $plugin_type ][ 'function' ];
956 } elseif (isset($this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ $plugin_type ])) {
957 $this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $plugin_name ][ $plugin_type ] =
958 $this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ $plugin_type ];
960 $this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $plugin_name ][ $plugin_type ][ 'function' ];
963 if (isset($this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ $plugin_type ])) {
965 $this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ $plugin_type ][ 'function' ];
966 } elseif (isset($this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $plugin_name ][ $plugin_type ])) {
967 $this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ $plugin_type ] =
968 $this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $plugin_name ][ $plugin_type ];
970 $this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ $plugin_type ][ 'function' ];
973 if (isset($function)) {
974 if ($plugin_type == 'modifier') {
975 $this->modifier_plugins[ $plugin_name ] = true;
980 // loop through plugin dirs and find the plugin
981 $function = 'smarty_' . $plugin_type . '_' . $plugin_name;
982 $file = $this->smarty->loadPlugin($function, false);
984 if (is_string($file)) {
985 if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
986 $this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $plugin_name ][ $plugin_type ][ 'file' ] =
988 $this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $plugin_name ][ $plugin_type ][ 'function' ] =
991 $this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ $plugin_type ][ 'file' ] =
993 $this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ $plugin_type ][ 'function' ] =
996 if ($plugin_type == 'modifier') {
997 $this->modifier_plugins[ $plugin_name ] = true;
1002 if (is_callable($function)) {
1003 // plugin function is defined in the script
1011 * Check for plugins by default plugin handler
1013 * @param string $tag name of tag
1014 * @param string $plugin_type type of plugin
1016 * @return boolean true if found
1018 public function getPluginFromDefaultHandler($tag, $plugin_type)
1023 $result = call_user_func_array($this->smarty->default_plugin_handler_func,
1024 array($tag, $plugin_type, $this->template, &$callback, &$script, &$cacheable,));
1026 $this->tag_nocache = $this->tag_nocache || !$cacheable;
1027 if ($script !== null) {
1028 if (is_file($script)) {
1029 if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
1030 $this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $tag ][ $plugin_type ][ 'file' ] =
1032 $this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $tag ][ $plugin_type ][ 'function' ] =
1035 $this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $tag ][ $plugin_type ][ 'file' ] =
1037 $this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $tag ][ $plugin_type ][ 'function' ] =
1040 require_once $script;
1042 $this->trigger_template_error("Default plugin handler: Returned script file \"{$script}\" for \"{$tag}\" not found");
1045 if (is_callable($callback)) {
1046 $this->default_handler_plugins[ $plugin_type ][ $tag ] = array($callback, true, array());
1050 $this->trigger_template_error("Default plugin handler: Returned callback for \"{$tag}\" not callable");
1058 * Append code segments and remove unneeded ?> <?php transitions
1060 * @param string $left
1061 * @param string $right
1065 public function appendCode($left, $right)
1067 if (preg_match('/\s*\?>\s*$/', $left) && preg_match('/^\s*<\?php\s+/', $right)) {
1068 $left = preg_replace('/\s*\?>\s*$/', "\n", $left);
1069 $left .= preg_replace('/^\s*<\?php\s+/', '', $right);
1077 * Inject inline code for nocache template sections
1078 * This method gets the content of each template element from the parser.
1079 * If the content is compiled code and it should be not cached the code is injected
1080 * into the rendered output.
1082 * @param string $content content of template element
1083 * @param boolean $is_code true if content is compiled code
1085 * @return string content
1087 public function processNocacheCode($content, $is_code)
1089 // If the template is not evaluated and we have a nocache section and or a nocache tag
1090 if ($is_code && !empty($content)) {
1091 // generate replacement code
1092 if ((!($this->template->source->handler->recompiled) || $this->forceNocache) && $this->template->caching &&
1093 !$this->suppressNocacheProcessing && ($this->nocache || $this->tag_nocache)
1095 $this->template->compiled->has_nocache_code = true;
1096 $_output = addcslashes($content, '\'\\');
1097 $_output = str_replace("^#^", "'", $_output);
1098 $_output = "<?php echo '/*%%SmartyNocache:{$this->nocache_hash}%%*/" . $_output .
1099 "/*/%%SmartyNocache:{$this->nocache_hash}%%*/';?>\n";
1100 // make sure we include modifier plugins for nocache code
1101 foreach ($this->modifier_plugins as $plugin_name => $dummy) {
1102 if (isset($this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ 'modifier' ])) {
1103 $this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $plugin_name ][ 'modifier' ] =
1104 $this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ 'modifier' ];
1108 $_output = $content;
1111 $_output = $content;
1113 $this->modifier_plugins = array();
1114 $this->suppressNocacheProcessing = false;
1115 $this->tag_nocache = false;
1123 * @param string $input
1125 * @return bool|string
1127 public function getId($input)
1129 if (preg_match('~^([\'"]*)([0-9]*[a-zA-Z_]\w*)\1$~', $input, $match)) {
1136 * Get variable name from string
1138 * @param string $input
1140 * @return bool|string
1142 public function getVariableName($input)
1144 if (preg_match('~^[$]_smarty_tpl->tpl_vars\[[\'"]*([0-9]*[a-zA-Z_]\w*)[\'"]*\]->value$~', $input, $match)) {
1151 * Set nocache flag in variable or create new variable
1153 * @param string $varName
1155 public function setNocacheInVariable($varName)
1157 // create nocache var to make it know for further compiling
1158 if ($_var = $this->getId($varName)) {
1159 if (isset($this->template->tpl_vars[ $_var ])) {
1160 $this->template->tpl_vars[ $_var ] = clone $this->template->tpl_vars[ $_var ];
1161 $this->template->tpl_vars[ $_var ]->nocache = true;
1163 $this->template->tpl_vars[ $_var ] = new Smarty_Variable(null, true);
1169 * @param array $_attr tag attributes
1170 * @param array $validScopes
1172 * @return int|string
1173 * @throws \SmartyCompilerException
1175 public function convertScope($_attr, $validScopes)
1178 if (isset($_attr[ 'scope' ])) {
1179 $_scopeName = trim($_attr[ 'scope' ], "'\"");
1180 if (is_numeric($_scopeName) && in_array($_scopeName, $validScopes)) {
1181 $_scope = $_scopeName;
1182 } elseif (is_string($_scopeName)) {
1183 $_scopeName = trim($_scopeName, "'\"");
1184 $_scope = isset($validScopes[ $_scopeName ]) ? $validScopes[ $_scopeName ] : false;
1188 if ($_scope === false) {
1189 $err = var_export($_scopeName, true);
1190 $this->trigger_template_error("illegal value '{$err}' for \"scope\" attribute", null, true);
1197 * Generate nocache code string
1199 * @param string $code PHP code
1203 public function makeNocacheCode($code)
1205 return "echo '/*%%SmartyNocache:{$this->nocache_hash}%%*/<?php " .
1206 str_replace("^#^", "'", addcslashes($code, '\'\\')) .
1207 "?>/*/%%SmartyNocache:{$this->nocache_hash}%%*/';\n";
1211 * display compiler error messages without dying
1212 * If parameter $args is empty it is a parser detected syntax error.
1213 * In this case the parser is called to obtain information about expected tokens.
1214 * If parameter $args contains a string this is used as error message
1216 * @param string $args individual error message or null
1217 * @param string $line line-number
1218 * @param null|bool $tagline if true the line number of last tag
1220 * @throws \SmartyCompilerException when an unexpected token is found
1222 public function trigger_template_error($args = null, $line = null, $tagline = null)
1224 $lex = $this->parser->lex;
1225 if ($tagline === true) {
1226 // get line number of Tag
1227 $line = $lex->taglineno;
1228 } elseif (!isset($line)) {
1229 // get template source line which has error
1232 $line = (int) $line;
1235 if (in_array($this->template->source->type, array('eval', 'string'))) {
1236 $templateName = $this->template->source->type . ':' . trim(preg_replace('![\t\r\n]+!', ' ',
1237 strlen($lex->data) > 40 ?
1238 substr($lex->data, 0, 40) .
1239 '...' : $lex->data));
1241 $templateName = $this->template->source->type . ':' . $this->template->source->filepath;
1244 // $line += $this->trace_line_offset;
1245 $match = preg_split("/\n/", $lex->data);
1247 'Syntax error in template "' . (empty($this->trace_filepath) ? $templateName : $this->trace_filepath) .
1248 '" on line ' . ($line + $this->trace_line_offset) . ' "' .
1249 trim(preg_replace('![\t\r\n]+!', ' ', $match[ $line - 1 ])) . '" ';
1251 // individual error message
1252 $error_text .= $args;
1255 // expected token from parser
1256 $error_text .= ' - Unexpected "' . $lex->value . '"';
1257 if (count($this->parser->yy_get_expected_tokens($this->parser->yymajor)) <= 4) {
1258 foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
1259 $exp_token = $this->parser->yyTokenName[ $token ];
1260 if (isset($lex->smarty_token_names[ $exp_token ])) {
1261 // token type from lexer
1262 $expect[] = '"' . $lex->smarty_token_names[ $exp_token ] . '"';
1264 // otherwise internal token name
1265 $expect[] = $this->parser->yyTokenName[ $token ];
1268 $error_text .= ', expected one of: ' . implode(' , ', $expect);
1271 $e = new SmartyCompilerException($error_text);
1273 $e->source = trim(preg_replace('![\t\r\n]+!', ' ', $match[ $line - 1 ]));
1275 $e->template = $this->template->source->filepath;
1280 * Return var_export() value with all white spaces removed
1282 * @param mixed $value
1286 public function getVarExport($value)
1288 return preg_replace('/\s/', '', var_export($value, true));
1292 * Check if $value contains variable elements
1294 * @param mixed $value
1298 public function isVariable($value)
1300 if (is_string($value)) {
1301 return preg_match('/[$(]/', $value);
1303 if (is_bool($value) || is_numeric($value)) {
1306 if (is_array($value)) {
1307 foreach ($value as $k => $v) {
1308 if ($this->isVariable($k) || $this->isVariable($v)) {
1318 * Get new prefix variable name
1322 public function getNewPrefixVariable()
1324 self::$prefixVariableNumber ++;
1325 return $this->getPrefixVariable();
1329 * Get current prefix variable name
1333 public function getPrefixVariable()
1335 return '$_prefixVariable' . self::$prefixVariableNumber;
1339 * append code to prefix buffer
1341 * @param string $code
1343 public function appendPrefixCode($code)
1345 $this->prefix_code[] = $code;
1349 * get prefix code string
1353 public function getPrefixCode()
1356 $prefixArray = array_merge($this->prefix_code, array_pop($this->prefixCodeStack));
1357 $this->prefixCodeStack[] = array();
1358 foreach ($prefixArray as $c) {
1359 $code = $this->appendCode($code, $c);
1361 $this->prefix_code = array();