| 
									
										
										
										
											2015-05-06 00:03:26 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-05-11 23:09:27 +02:00
										 |  |  |  * Smarty Internal Plugin Compile PHP Expression | 
					
						
							| 
									
										
										
										
											2015-05-06 00:03:26 +02:00
										 |  |  |  * Compiles any tag which will output an expression or variable | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package    Smarty | 
					
						
							|  |  |  |  * @subpackage Compiler | 
					
						
							|  |  |  |  * @author     Uwe Tews | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-05-11 23:09:27 +02:00
										 |  |  |  * Smarty Internal Plugin Compile PHP Expression Class | 
					
						
							| 
									
										
										
										
											2015-05-06 00:03:26 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @package    Smarty | 
					
						
							|  |  |  |  * @subpackage Compiler | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class Smarty_Internal_Compile_Private_Php extends Smarty_Internal_CompileBase | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-21 00:05:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-06 00:03:26 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Attribute definition: Overwrites base class. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var array | 
					
						
							|  |  |  |      * @see Smarty_Internal_CompileBase | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public $required_attributes = array('code', 'type'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Compiles code for generating output from any expression | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param array                                 $args      array with attributes from parser | 
					
						
							|  |  |  |      * @param \Smarty_Internal_TemplateCompilerBase $compiler  compiler object | 
					
						
							|  |  |  |      * @param array                                 $parameter array with compilation parameter | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      * @throws \SmartyException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // check and get attributes
 | 
					
						
							|  |  |  |         $_attr = $this->getAttributes($compiler, $args); | 
					
						
							|  |  |  |         $compiler->has_code = false; | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |         if ($_attr[ 'type' ] === 'xml') { | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |             $compiler->tag_nocache = true; | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |             $output = addcslashes($_attr[ 'code' ], "'\\"); | 
					
						
							|  |  |  |             $compiler->parser->current_buffer->append_subtree($compiler->parser, | 
					
						
							|  |  |  |                                                               new Smarty_Internal_ParseTree_Tag($compiler->parser, | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |                                                                                                 $compiler->processNocacheCode("<?php echo '{$output}';?>", | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |                                                                                                                               true))); | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |             return ''; | 
					
						
							| 
									
										
										
										
											2015-05-06 00:03:26 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |         if ($_attr[ 'type' ] !== 'tag') { | 
					
						
							|  |  |  |             if ($compiler->php_handling === Smarty::PHP_REMOVE) { | 
					
						
							| 
									
										
										
										
											2015-05-06 00:03:26 +02:00
										 |  |  |                 return ''; | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |             } elseif ($compiler->php_handling === Smarty::PHP_QUOTE) { | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |                 $output = | 
					
						
							|  |  |  |                     preg_replace_callback('#(<\?(?:php|=)?)|(<%)|(<script\s+language\s*=\s*["\']?\s*php\s*["\']?\s*>)|(\?>)|(%>)|(<\/script>)#i', | 
					
						
							|  |  |  |                                           array($this, 'quote'), $_attr[ 'code' ]); | 
					
						
							|  |  |  |                 $compiler->parser->current_buffer->append_subtree($compiler->parser, | 
					
						
							|  |  |  |                                                                   new Smarty_Internal_ParseTree_Text($output)); | 
					
						
							| 
									
										
										
										
											2015-05-06 00:03:26 +02:00
										 |  |  |                 return ''; | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |             } elseif ($compiler->php_handling === Smarty::PHP_PASSTHRU || $_attr[ 'type' ] === 'unmatched') { | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |                 $compiler->tag_nocache = true; | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |                 $output = addcslashes($_attr[ 'code' ], "'\\"); | 
					
						
							|  |  |  |                 $compiler->parser->current_buffer->append_subtree($compiler->parser, | 
					
						
							|  |  |  |                                                                   new Smarty_Internal_ParseTree_Tag($compiler->parser, | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |                                                                                                     $compiler->processNocacheCode("<?php echo '{$output}';?>", | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |                                                                                                                                   true))); | 
					
						
							| 
									
										
										
										
											2015-05-06 00:03:26 +02:00
										 |  |  |                 return ''; | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |             } elseif ($compiler->php_handling === Smarty::PHP_ALLOW) { | 
					
						
							| 
									
										
										
										
											2015-05-06 00:03:26 +02:00
										 |  |  |                 if (!($compiler->smarty instanceof SmartyBC)) { | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |                     $compiler->trigger_template_error('$smarty->php_handling PHP_ALLOW not allowed. Use SmartyBC to enable it', | 
					
						
							|  |  |  |                                                       null, true); | 
					
						
							| 
									
										
										
										
											2015-05-06 00:03:26 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 $compiler->has_code = true; | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |                 return $_attr[ 'code' ]; | 
					
						
							| 
									
										
										
										
											2015-05-06 00:03:26 +02:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2015-08-06 01:19:11 +02:00
										 |  |  |                 $compiler->trigger_template_error('Illegal $smarty->php_handling value', null, true); | 
					
						
							| 
									
										
										
										
											2015-05-06 00:03:26 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $compiler->has_code = true; | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |             if (!($compiler->smarty instanceof SmartyBC)) { | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |                 $compiler->trigger_template_error('{php}{/php} tags not allowed. Use SmartyBC to enable them', null, | 
					
						
							|  |  |  |                                                   true); | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-05-06 00:03:26 +02:00
										 |  |  |             $ldel = preg_quote($compiler->smarty->left_delimiter, '#'); | 
					
						
							|  |  |  |             $rdel = preg_quote($compiler->smarty->right_delimiter, '#'); | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |             preg_match("#^({$ldel}php\\s*)((.)*?)({$rdel})#", $_attr[ 'code' ], $match); | 
					
						
							|  |  |  |             if (!empty($match[ 2 ])) { | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |                 if ('nocache' === trim($match[ 2 ])) { | 
					
						
							| 
									
										
										
										
											2015-05-06 00:03:26 +02:00
										 |  |  |                     $compiler->tag_nocache = true; | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |                     $compiler->trigger_template_error("illegal value of option flag '{$match[2]}'", null, true); | 
					
						
							| 
									
										
										
										
											2015-05-06 00:03:26 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |             return preg_replace(array("#^{$ldel}\\s*php\\s*(.)*?{$rdel}#", "#{$ldel}\\s*/\\s*php\\s*{$rdel}$#"), | 
					
						
							|  |  |  |                                 array('<?php ', '?>'), $_attr[ 'code' ]); | 
					
						
							| 
									
										
										
										
											2015-05-06 00:03:26 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-21 00:05:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Lexer code for PHP tags | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * This code has been moved from lexer here fo easier debugging and maintenance | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2017-10-26 10:25:41 +02:00
										 |  |  |      * @param Smarty_Internal_Templatelexer $lex | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @throws \SmartyCompilerException | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-10-26 10:25:41 +02:00
										 |  |  |     public function parsePhp(Smarty_Internal_Templatelexer $lex) | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-05-23 23:40:02 +02:00
										 |  |  |         $lex->token = Smarty_Internal_Templateparser::TP_PHP; | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |         $close = 0; | 
					
						
							|  |  |  |         $lex->taglineno = $lex->line; | 
					
						
							|  |  |  |         $closeTag = '?>'; | 
					
						
							|  |  |  |         if (strpos($lex->value, '<?xml') === 0) { | 
					
						
							| 
									
										
										
										
											2015-06-01 22:26:45 +02:00
										 |  |  |             $lex->is_xml = true; | 
					
						
							| 
									
										
										
										
											2016-09-28 23:39:05 +02:00
										 |  |  |             $lex->phpType = 'xml'; | 
					
						
							| 
									
										
										
										
											2015-06-01 22:26:45 +02:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |         } elseif (strpos($lex->value, '<?') === 0) { | 
					
						
							|  |  |  |             $lex->phpType = 'php'; | 
					
						
							|  |  |  |         } elseif (strpos($lex->value, '<%') === 0) { | 
					
						
							|  |  |  |             $lex->phpType = 'asp'; | 
					
						
							|  |  |  |             $closeTag = '%>'; | 
					
						
							|  |  |  |         } elseif (strpos($lex->value, '%>') === 0) { | 
					
						
							|  |  |  |             $lex->phpType = 'unmatched'; | 
					
						
							|  |  |  |         } elseif (strpos($lex->value, '?>') === 0) { | 
					
						
							| 
									
										
										
										
											2015-06-01 22:26:45 +02:00
										 |  |  |             if ($lex->is_xml) { | 
					
						
							|  |  |  |                 $lex->is_xml = false; | 
					
						
							| 
									
										
										
										
											2016-09-28 23:39:05 +02:00
										 |  |  |                 $lex->phpType = 'xml'; | 
					
						
							| 
									
										
										
										
											2015-06-01 22:26:45 +02:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |             $lex->phpType = 'unmatched'; | 
					
						
							|  |  |  |         } elseif (strpos($lex->value, '<s') === 0) { | 
					
						
							|  |  |  |             $lex->phpType = 'script'; | 
					
						
							|  |  |  |             $closeTag = '</script>'; | 
					
						
							|  |  |  |         } elseif (strpos($lex->value, $lex->smarty->left_delimiter) === 0) { | 
					
						
							|  |  |  |             if ($lex->isAutoLiteral()) { | 
					
						
							|  |  |  |                 $lex->token = Smarty_Internal_Templateparser::TP_TEXT; | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $closeTag = "{$lex->smarty->left_delimiter}/php{$lex->smarty->right_delimiter}"; | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |             if ($lex->value === $closeTag) { | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |                 $lex->compiler->trigger_template_error("unexpected closing tag '{$closeTag}'"); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $lex->phpType = 'tag'; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |         if ($lex->phpType === 'unmatched') { | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |         if (($lex->phpType === 'php' || $lex->phpType === 'asp') && | 
					
						
							|  |  |  |             ($lex->compiler->php_handling === Smarty::PHP_PASSTHRU || $lex->compiler->php_handling === Smarty::PHP_QUOTE) | 
					
						
							| 
									
										
										
										
											2015-08-06 01:19:11 +02:00
										 |  |  |         ) { | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $start = $lex->counter + strlen($lex->value); | 
					
						
							|  |  |  |         $body = true; | 
					
						
							|  |  |  |         if (preg_match('~' . preg_quote($closeTag, '~') . '~i', $lex->data, $match, PREG_OFFSET_CAPTURE, $start)) { | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |             $close = $match[ 0 ][ 1 ]; | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             $lex->compiler->trigger_template_error("missing closing tag '{$closeTag}'"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         while ($body) { | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |             if (preg_match('~([/][*])|([/][/][^\n]*)|(\'[^\'\\\\]*(?:\\.[^\'\\\\]*)*\')|("[^"\\\\]*(?:\\.[^"\\\\]*)*")~', | 
					
						
							|  |  |  |                            $lex->data, $match, PREG_OFFSET_CAPTURE, $start)) { | 
					
						
							|  |  |  |                 $value = $match[ 0 ][ 0 ]; | 
					
						
							|  |  |  |                 $from = $pos = $match[ 0 ][ 1 ]; | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |                 if ($pos > $close) { | 
					
						
							|  |  |  |                     $body = false; | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     $start = $pos + strlen($value); | 
					
						
							| 
									
										
										
										
											2017-11-06 01:02:56 +01:00
										 |  |  |                     $phpCommentStart = $value === '/*'; | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |                     if ($phpCommentStart) { | 
					
						
							|  |  |  |                         $phpCommentEnd = preg_match('~([*][/])~', $lex->data, $match, PREG_OFFSET_CAPTURE, $start); | 
					
						
							|  |  |  |                         if ($phpCommentEnd) { | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |                             $pos2 = $match[ 0 ][ 1 ]; | 
					
						
							|  |  |  |                             $start = $pos2 + strlen($match[ 0 ][ 0 ]); | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     while ($close > $pos && $close < $start) { | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |                         if (preg_match('~' . preg_quote($closeTag, '~') . '~i', $lex->data, $match, PREG_OFFSET_CAPTURE, | 
					
						
							|  |  |  |                                        $from)) { | 
					
						
							|  |  |  |                             $close = $match[ 0 ][ 1 ]; | 
					
						
							|  |  |  |                             $from = $close + strlen($match[ 0 ][ 0 ]); | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |                         } else { | 
					
						
							|  |  |  |                             $lex->compiler->trigger_template_error("missing closing tag '{$closeTag}'"); | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     if ($phpCommentStart && (!$phpCommentEnd || $pos2 > $close)) { | 
					
						
							|  |  |  |                         $lex->taglineno = $lex->line + substr_count(substr($lex->data, $lex->counter, $start), "\n"); | 
					
						
							|  |  |  |                         $lex->compiler->trigger_template_error("missing PHP comment closing tag '*/'"); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $body = false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $lex->value = substr($lex->data, $lex->counter, $close + strlen($closeTag) - $lex->counter); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-21 00:05:16 +02:00
										 |  |  |     /* | 
					
						
							| 
									
										
										
										
											2015-05-23 18:56:00 +02:00
										 |  |  |      * Call back function for $php_handling = PHP_QUOTE | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-08-06 01:19:11 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param $match | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-05-21 00:05:16 +02:00
										 |  |  |     private function quote($match) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-02-09 01:27:15 +01:00
										 |  |  |         return htmlspecialchars($match[ 0 ], ENT_QUOTES); | 
					
						
							| 
									
										
										
										
											2015-05-21 00:05:16 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-06 00:03:26 +02:00
										 |  |  | } |