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
21 * Attribute definition: Overwrites base class.
24 * @see Smarty_Internal_CompileBase
26 public $required_attributes = array('code', 'type');
29 * Compiles code for generating output from any expression
31 * @param array $args array with attributes from parser
32 * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
33 * @param array $parameter array with compilation parameter
36 * @throws \SmartyException
38 public function compile($args, Smarty_Internal_TemplateCompilerBase
$compiler, $parameter)
40 // check and get attributes
41 $_attr = $this->getAttributes($compiler, $args);
42 $compiler->has_code
= false;
43 if ($_attr['type'] == 'xml') {
44 $compiler->tag_nocache
= true;
45 $save = $compiler->template
->has_nocache_code
;
46 $compiler->template
->has_nocache_code
= $save;
47 $output = addcslashes($_attr['code'], "'\\");
48 $compiler->parser
->current_buffer
->append_subtree(new Smarty_Internal_ParseTree_Tag($compiler->parser
, $compiler->processNocacheCode("<?php echo '" . $output . "';?>", $compiler, true)));
51 if ($_attr['type'] != 'tag') {
52 if ($compiler->php_handling
== Smarty
::PHP_REMOVE
) {
54 } elseif ($compiler->php_handling
== Smarty
::PHP_QUOTE
) {
55 $output = preg_replace_callback('#(<\?(?:php|=)?)|(<%)|(<script\s+language\s*=\s*["\']?\s*php\s*["\']?\s*>)|(\?>)|(%>)|(<\/script>)#i', array($this,
56 'quote'), $_attr['code']);
57 $compiler->parser
->current_buffer
->append_subtree(new Smarty_Internal_ParseTree_Text($compiler->parser
, $output));
59 } elseif ($compiler->php_handling
== Smarty
::PHP_PASSTHRU ||
$_attr['type'] == 'unmatched') {
60 $compiler->tag_nocache
= true;
61 $save = $compiler->template
->has_nocache_code
;
62 $compiler->template
->has_nocache_code
= $save;
63 $output = addcslashes($_attr['code'], "'\\");
64 $compiler->parser
->current_buffer
->append_subtree(new Smarty_Internal_ParseTree_Tag($compiler->parser
, $compiler->processNocacheCode("<?php echo '" . $output . "';?>", $compiler, true)));
66 } elseif ($compiler->php_handling
== Smarty
::PHP_ALLOW
) {
67 if (!($compiler->smarty
instanceof SmartyBC
)) {
68 $compiler->trigger_template_error('$smarty->php_handling PHP_ALLOW not allowed. Use SmartyBC to enable it', $compiler->lex
->taglineno
);
70 $compiler->has_code
= true;
71 return $_attr['code'];
73 $compiler->trigger_template_error('Illegal $smarty->php_handling value', $compiler->lex
->taglineno
);
76 $compiler->has_code
= true;
77 if (!($compiler->smarty
instanceof SmartyBC
)) {
78 $compiler->trigger_template_error('{php}[/php} tags not allowed. Use SmartyBC to enable them', $compiler->lex
->taglineno
);
80 $ldel = preg_quote($compiler->smarty
->left_delimiter
, '#');
81 $rdel = preg_quote($compiler->smarty
->right_delimiter
, '#');
82 preg_match("#^({$ldel}php\\s*)((.)*?)({$rdel})#", $_attr['code'], $match);
83 if (!empty($match[2])) {
84 if ('nocache' == trim($match[2])) {
85 $compiler->tag_nocache
= true;
87 $compiler->trigger_template_error("illegal value of option flag \"{$match[2]}\"", $compiler->lex
->taglineno
);
90 return preg_replace(array("#^{$ldel}\\s*php\\s*(.)*?{$rdel}#",
91 "#{$ldel}\\s*/\\s*php\\s*{$rdel}$#"), array('<?php ', '?>'), $_attr['code']);
96 * Lexer code for PHP tags
98 * This code has been moved from lexer here fo easier debugging and maintenance
102 public function parsePhp($lex)
105 $lex->taglineno
= $lex->line
;
107 if (strpos($lex->value
, '<?xml') === 0) {
108 $lex->phpType
= 'xml';
109 } elseif (strpos($lex->value
, '<?') === 0) {
110 $lex->phpType
= 'php';
111 } elseif (strpos($lex->value
, '<%') === 0) {
112 $lex->phpType
= 'asp';
114 } elseif (strpos($lex->value
, '%>') === 0) {
115 $lex->phpType
= 'unmatched';
116 } elseif (strpos($lex->value
, '?>') === 0) {
117 $lex->phpType
= 'unmatched';
118 } elseif (strpos($lex->value
, '<s') === 0) {
119 $lex->phpType
= 'script';
120 $closeTag = '</script>';
121 } elseif (strpos($lex->value
, $lex->smarty
->left_delimiter
) === 0) {
122 if ($lex->isAutoLiteral()) {
123 $lex->token
= Smarty_Internal_Templateparser
::TP_TEXT
;
126 $closeTag = "{$lex->smarty->left_delimiter}/php{$lex->smarty->right_delimiter}";
127 if ($lex->value
== $closeTag) {
128 $lex->compiler
->trigger_template_error("unexpected closing tag '{$closeTag}'");
130 $lex->phpType
= 'tag';
132 if ($lex->phpType
== 'unmatched') {
135 if (($lex->phpType
== 'php' ||
$lex->phpType
== 'asp') && ($lex->compiler
->php_handling
== Smarty
::PHP_PASSTHRU ||
$lex->compiler
->php_handling
== Smarty
::PHP_QUOTE
)) {
138 $start = $lex->counter +
strlen($lex->value
);
140 if (preg_match('~' . preg_quote($closeTag, '~') . '~i', $lex->data
, $match, PREG_OFFSET_CAPTURE
, $start)) {
141 $close = $match[0][1];
143 $lex->compiler
->trigger_template_error("missing closing tag '{$closeTag}'");
146 if (preg_match('~([/][*])|([/][/][^\n]*)|(\'[^\'\\\\]*(?:\\.[^\'\\\\]*)*\')|("[^"\\\\]*(?:\\.[^"\\\\]*)*")~', $lex->data
, $match, PREG_OFFSET_CAPTURE
, $start)) {
147 $value = $match[0][0];
148 $from = $pos = $match[0][1];
152 $start = $pos +
strlen($value);
153 $phpCommentStart = $value == '/*';
154 if ($phpCommentStart) {
155 $phpCommentEnd = preg_match('~([*][/])~', $lex->data
, $match, PREG_OFFSET_CAPTURE
, $start);
156 if ($phpCommentEnd) {
157 $pos2 = $match[0][1];
158 $start = $pos2 +
strlen($match[0][0]);
161 while ($close > $pos && $close < $start) {
162 if (preg_match('~' . preg_quote($closeTag, '~') . '~i', $lex->data
, $match, PREG_OFFSET_CAPTURE
, $from)) {
163 $close = $match[0][1];
164 $from = $close +
strlen($match[0][0]);
166 $lex->compiler
->trigger_template_error("missing closing tag '{$closeTag}'");
169 if ($phpCommentStart && (!$phpCommentEnd ||
$pos2 > $close)) {
170 $lex->taglineno
= $lex->line +
substr_count(substr($lex->data
, $lex->counter
, $start), "\n");
171 $lex->compiler
->trigger_template_error("missing PHP comment closing tag '*/'");
178 $lex->value
= substr($lex->data
, $lex->counter
, $close +
strlen($closeTag) - $lex->counter
);
182 * Call back function for $php_handling = PHP_QUOTE
185 private function quote($match)
187 return htmlspecialchars($match[0], ENT_QUOTES
);