3 * Smarty Internal Plugin Compile PHP Expression
4 * Compiles any tag which will output an expression or variable
12 * Smarty Internal Plugin Compile PHP Expression Class
15 * @subpackage Compiler
17 class Smarty_Internal_Compile_Private_Php
extends Smarty_Internal_CompileBase
20 * Attribute definition: Overwrites base class.
23 * @see Smarty_Internal_CompileBase
25 public $required_attributes = array('code', 'type');
28 * Compiles code for generating output from any expression
30 * @param array $args array with attributes from parser
31 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
32 * @param array $parameter array with compilation parameter
35 * @throws \SmartyException
37 public function compile($args, Smarty_Internal_TemplateCompilerBase
$compiler, $parameter)
39 // check and get attributes
40 $_attr = $this->getAttributes($compiler, $args);
41 $compiler->has_code
= false;
42 $this->asp_tags
= (ini_get('asp_tags') != '0');
43 if ($_attr['type'] == 'tag' && !($compiler->smarty
instanceof SmartyBC
)) {
44 $compiler->trigger_template_error('{php}[/php} tags not allowed. Use SmartyBC to enable them', $compiler->lex
->taglineno
);
46 if ($_attr['type'] != 'tag') {
47 if (isset($compiler->smarty
->security_policy
)) {
48 $this->php_handling
= $compiler->smarty
->security_policy
->php_handling
;
50 $this->php_handling
= $compiler->smarty
->php_handling
;
52 if ($this->php_handling
== Smarty
::PHP_REMOVE
) {
53 $output = preg_replace(array('#^(<\?(?:php|=)?)|(<%)|(<script\s+language\s*=\s*["\']?\s*php\s*["\']?\s*>)#', '#(\?>)|(%>)|(<\/script>)$#'), '', $_attr['code']);
54 $compiler->parser
->current_buffer
->append_subtree(new Smarty_Internal_ParseTree_Text($compiler->parser
, $output));
56 } elseif ($this->php_handling
== Smarty
::PHP_QUOTE
) {
57 $output = preg_replace_callback(array('#^(<\?(?:php|=)?)|(<%)|(<script\s+language\s*=\s*["\']?\s*php\s*["\']?\s*>)#', '#(\?>)|(%>)|(<\/script>)$#'), function ($match) {return htmlspecialchars($match[0], ENT_QUOTES
);}, $_attr['code']);
58 $compiler->parser
->current_buffer
->append_subtree(new Smarty_Internal_ParseTree_Text($compiler->parser
, $output));
60 } elseif ($this->php_handling
== Smarty
::PHP_PASSTHRU ||
($_attr['type'] == 'asp' && !$this->asp_tags
) ||
$_attr['type'] == 'unmatched') {
61 $compiler->parser
->current_buffer
->append_subtree(new Smarty_Internal_ParseTree_Text($compiler->parser
, $_attr['code']));
63 } elseif ($this->php_handling
== Smarty
::PHP_ALLOW
) {
64 if (!($compiler->smarty
instanceof SmartyBC
)) {
65 $compiler->trigger_template_error('$smarty->php_handling PHP_ALLOW not allowed. Use SmartyBC to enable it', $compiler->lex
->taglineno
);
67 $compiler->has_code
= true;
68 return $_attr['code'];
70 $compiler->trigger_template_error('Illegal $smarty->php_handling value', $compiler->lex
->taglineno
);
73 $compiler->has_code
= true;
74 $ldel = preg_quote($compiler->smarty
->left_delimiter
, '#');
75 $rdel = preg_quote($compiler->smarty
->right_delimiter
, '#');
76 if (!preg_match("#{$ldel}\\s*/\\s*php\\s*{$rdel}$#", $_attr['code'], $match)) {
77 $compiler->trigger_template_error('Missing {/php} closing tag', $compiler->lex
->taglineno
);
79 if (!preg_match("#^({$ldel}\\s*php\\s*)((.)*?)({$rdel})#", $_attr['code'], $match)) {
80 $compiler->trigger_template_error('Missing {php} open tag', $compiler->lex
->taglineno
);
82 if (!empty($match[2])) {
83 if ('nocache' == trim($match[2])) {
84 $compiler->tag_nocache
= true;
86 $compiler->trigger_template_error("illegal value of option flag \"{$match[2]}\"", $compiler->lex
->taglineno
);
89 return preg_replace(array("#^{$ldel}\\s*php\\s*(.)*?{$rdel}#", "#{$ldel}\\s*/\\s*php\\s*{$rdel}$#"), array('<?php ', '?>'), $_attr['code']);