library/smarty/libs/sysplugins/smarty_internal_compile_private_php.php
changeset 0 4869aea77e21
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     1 <?php
       
     2 /**
       
     3  * Smarty Internal Plugin Compile PHP Expression
       
     4  * Compiles any tag which will output an expression or variable
       
     5  *
       
     6  * @package    Smarty
       
     7  * @subpackage Compiler
       
     8  * @author     Uwe Tews
       
     9  */
       
    10 
       
    11 /**
       
    12  * Smarty Internal Plugin Compile PHP Expression Class
       
    13  *
       
    14  * @package    Smarty
       
    15  * @subpackage Compiler
       
    16  */
       
    17 class Smarty_Internal_Compile_Private_Php extends Smarty_Internal_CompileBase {
       
    18 
       
    19     /**
       
    20      * Attribute definition: Overwrites base class.
       
    21      *
       
    22      * @var array
       
    23      * @see Smarty_Internal_CompileBase
       
    24      */
       
    25     public $required_attributes = array('code', 'type');
       
    26 
       
    27     /**
       
    28      * Compiles code for generating output from any expression
       
    29      *
       
    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
       
    33      *
       
    34      * @return string
       
    35      * @throws \SmartyException
       
    36      */
       
    37     public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter) {
       
    38         // check and get attributes
       
    39         $_attr = $this->getAttributes($compiler, $args);
       
    40         $compiler->has_code = false;
       
    41         if ($_attr['type'] == 'xml') {
       
    42             $compiler->tag_nocache = true;
       
    43             $save = $compiler->template->has_nocache_code;
       
    44             $output = addcslashes($_attr['code'], "'\\");
       
    45             $compiler->parser->current_buffer->append_subtree(new Smarty_Internal_ParseTree_Tag($compiler->parser, $compiler->processNocacheCode("<?php echo '" . $output . "';?>", $compiler, true)));
       
    46             $compiler->template->has_nocache_code = $save;
       
    47             return '';
       
    48         }
       
    49         if ($_attr['type'] != 'tag') {
       
    50             if ($compiler->php_handling == Smarty::PHP_REMOVE) {
       
    51                 return '';
       
    52             } elseif ($compiler->php_handling == Smarty::PHP_QUOTE) {
       
    53                 $output = preg_replace_callback('#(<\?(?:php|=)?)|(<%)|(<script\s+language\s*=\s*["\']?\s*php\s*["\']?\s*>)|(\?>)|(%>)|(<\/script>)#i', array($this,
       
    54                     'quote'), $_attr['code']);
       
    55                 $compiler->parser->current_buffer->append_subtree(new Smarty_Internal_ParseTree_Text($compiler->parser, $output));
       
    56                 return '';
       
    57             } elseif ($compiler->php_handling == Smarty::PHP_PASSTHRU || $_attr['type'] == 'unmatched') {
       
    58                 $compiler->tag_nocache = true;
       
    59                 $save = $compiler->template->has_nocache_code;
       
    60                 $output = addcslashes($_attr['code'], "'\\");
       
    61                 $compiler->parser->current_buffer->append_subtree(new Smarty_Internal_ParseTree_Tag($compiler->parser, $compiler->processNocacheCode("<?php echo '" . $output . "';?>", $compiler, true)));
       
    62                 $compiler->template->has_nocache_code = $save;
       
    63                 return '';
       
    64             } elseif ($compiler->php_handling == Smarty::PHP_ALLOW) {
       
    65                 if (!($compiler->smarty instanceof SmartyBC)) {
       
    66                     $compiler->trigger_template_error('$smarty->php_handling PHP_ALLOW not allowed. Use SmartyBC to enable it', $compiler->lex->taglineno);
       
    67                 }
       
    68                 $compiler->has_code = true;
       
    69                 return $_attr['code'];
       
    70             } else {
       
    71                 $compiler->trigger_template_error('Illegal $smarty->php_handling value', $compiler->lex->taglineno);
       
    72             }
       
    73         } else {
       
    74             $compiler->has_code = true;
       
    75             if (!($compiler->smarty instanceof SmartyBC)) {
       
    76                 $compiler->trigger_template_error('{php}[/php} tags not allowed. Use SmartyBC to enable them', $compiler->lex->taglineno);
       
    77             }
       
    78             $ldel = preg_quote($compiler->smarty->left_delimiter, '#');
       
    79             $rdel = preg_quote($compiler->smarty->right_delimiter, '#');
       
    80             preg_match("#^({$ldel}php\\s*)((.)*?)({$rdel})#", $_attr['code'], $match);
       
    81             if (!empty($match[2])) {
       
    82                 if ('nocache' == trim($match[2])) {
       
    83                     $compiler->tag_nocache = true;
       
    84                 } else {
       
    85                     $compiler->trigger_template_error("illegal value of option flag \"{$match[2]}\"", $compiler->lex->taglineno);
       
    86                 }
       
    87             }
       
    88             return preg_replace(array("#^{$ldel}\\s*php\\s*(.)*?{$rdel}#",
       
    89                 "#{$ldel}\\s*/\\s*php\\s*{$rdel}$#"), array('<?php ', '?>'), $_attr['code']);
       
    90         }
       
    91     }
       
    92 
       
    93     /**
       
    94      * Lexer code for PHP tags
       
    95      *
       
    96      * This code has been moved from lexer here fo easier debugging and maintenance
       
    97      *
       
    98      * @param $lex
       
    99      */
       
   100     public function parsePhp($lex) {
       
   101         $lex->token = Smarty_Internal_Templateparser::TP_PHP;
       
   102         $close = 0;
       
   103         $lex->taglineno = $lex->line;
       
   104         $closeTag = '?>';
       
   105         if (strpos($lex->value, '<?xml') === 0) {
       
   106             $lex->is_xml = true;
       
   107             $lex->token = Smarty_Internal_Templateparser::TP_NOCACHE;
       
   108             return;
       
   109         } elseif (strpos($lex->value, '<?') === 0) {
       
   110             $lex->phpType = 'php';
       
   111         } elseif (strpos($lex->value, '<%') === 0) {
       
   112             $lex->phpType = 'asp';
       
   113             $closeTag = '%>';
       
   114         } elseif (strpos($lex->value, '%>') === 0) {
       
   115             $lex->phpType = 'unmatched';
       
   116         } elseif (strpos($lex->value, '?>') === 0) {
       
   117             if ($lex->is_xml) {
       
   118                 $lex->is_xml = false;
       
   119                 $lex->token = Smarty_Internal_Templateparser::TP_NOCACHE;
       
   120                 return;
       
   121             }
       
   122             $lex->phpType = 'unmatched';
       
   123         } elseif (strpos($lex->value, '<s') === 0) {
       
   124             $lex->phpType = 'script';
       
   125             $closeTag = '</script>';
       
   126         } elseif (strpos($lex->value, $lex->smarty->left_delimiter) === 0) {
       
   127             if ($lex->isAutoLiteral()) {
       
   128                 $lex->token = Smarty_Internal_Templateparser::TP_TEXT;
       
   129                 return;
       
   130             }
       
   131             $closeTag = "{$lex->smarty->left_delimiter}/php{$lex->smarty->right_delimiter}";
       
   132             if ($lex->value == $closeTag) {
       
   133                 $lex->compiler->trigger_template_error("unexpected closing tag '{$closeTag}'");
       
   134             }
       
   135             $lex->phpType = 'tag';
       
   136         }
       
   137         if ($lex->phpType == 'unmatched') {
       
   138             return;
       
   139         }
       
   140         if (($lex->phpType == 'php' || $lex->phpType == 'asp') && ($lex->compiler->php_handling == Smarty::PHP_PASSTHRU || $lex->compiler->php_handling == Smarty::PHP_QUOTE)) {
       
   141             return;
       
   142         }
       
   143         $start = $lex->counter + strlen($lex->value);
       
   144         $body = true;
       
   145         if (preg_match('~' . preg_quote($closeTag, '~') . '~i', $lex->data, $match, PREG_OFFSET_CAPTURE, $start)) {
       
   146             $close = $match[0][1];
       
   147         } else {
       
   148             $lex->compiler->trigger_template_error("missing closing tag '{$closeTag}'");
       
   149         }
       
   150         while ($body) {
       
   151             if (preg_match('~([/][*])|([/][/][^\n]*)|(\'[^\'\\\\]*(?:\\.[^\'\\\\]*)*\')|("[^"\\\\]*(?:\\.[^"\\\\]*)*")~', $lex->data, $match, PREG_OFFSET_CAPTURE, $start)) {
       
   152                 $value = $match[0][0];
       
   153                 $from = $pos = $match[0][1];
       
   154                 if ($pos > $close) {
       
   155                     $body = false;
       
   156                 } else {
       
   157                     $start = $pos + strlen($value);
       
   158                     $phpCommentStart = $value == '/*';
       
   159                     if ($phpCommentStart) {
       
   160                         $phpCommentEnd = preg_match('~([*][/])~', $lex->data, $match, PREG_OFFSET_CAPTURE, $start);
       
   161                         if ($phpCommentEnd) {
       
   162                             $pos2 = $match[0][1];
       
   163                             $start = $pos2 + strlen($match[0][0]);
       
   164                         }
       
   165                     }
       
   166                     while ($close > $pos && $close < $start) {
       
   167                         if (preg_match('~' . preg_quote($closeTag, '~') . '~i', $lex->data, $match, PREG_OFFSET_CAPTURE, $from)) {
       
   168                             $close = $match[0][1];
       
   169                             $from = $close + strlen($match[0][0]);
       
   170                         } else {
       
   171                             $lex->compiler->trigger_template_error("missing closing tag '{$closeTag}'");
       
   172                         }
       
   173                     }
       
   174                     if ($phpCommentStart && (!$phpCommentEnd || $pos2 > $close)) {
       
   175                         $lex->taglineno = $lex->line + substr_count(substr($lex->data, $lex->counter, $start), "\n");
       
   176                         $lex->compiler->trigger_template_error("missing PHP comment closing tag '*/'");
       
   177                     }
       
   178                 }
       
   179             } else {
       
   180                 $body = false;
       
   181             }
       
   182         }
       
   183         $lex->value = substr($lex->data, $lex->counter, $close + strlen($closeTag) - $lex->counter);
       
   184     }
       
   185 
       
   186     /*
       
   187      * Call back function for $php_handling = PHP_QUOTE
       
   188      *
       
   189      */
       
   190     private function quote($match) {
       
   191         return htmlspecialchars($match[0], ENT_QUOTES);
       
   192     }
       
   193 }