mirror of
https://github.com/smarty-php/smarty.git
synced 2025-11-03 05:41:37 +01:00
- optimization move <?php ?> handling from parser to new compiler module
This commit is contained in:
92
libs/sysplugins/smarty_internal_compile_private_php.php
Normal file
92
libs/sysplugins/smarty_internal_compile_private_php.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* Smarty Internal Plugin Compile Print Expression
|
||||
* Compiles any tag which will output an expression or variable
|
||||
*
|
||||
* @package Smarty
|
||||
* @subpackage Compiler
|
||||
* @author Uwe Tews
|
||||
*/
|
||||
|
||||
/**
|
||||
* Smarty Internal Plugin Compile Print Expression Class
|
||||
*
|
||||
* @package Smarty
|
||||
* @subpackage Compiler
|
||||
*/
|
||||
class Smarty_Internal_Compile_Private_Php extends Smarty_Internal_CompileBase
|
||||
{
|
||||
/**
|
||||
* 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;
|
||||
$this->asp_tags = (ini_get('asp_tags') != '0');
|
||||
if ($_attr['type'] == 'tag' && !($compiler->smarty instanceof SmartyBC)) {
|
||||
$compiler->trigger_template_error('{php}[/php} tags not allowed. Use SmartyBC to enable them', $compiler->lex->taglineno);
|
||||
}
|
||||
if ($_attr['type'] != 'tag') {
|
||||
if (isset($compiler->smarty->security_policy)) {
|
||||
$this->php_handling = $compiler->smarty->security_policy->php_handling;
|
||||
} else {
|
||||
$this->php_handling = $compiler->smarty->php_handling;
|
||||
}
|
||||
if ($this->php_handling == Smarty::PHP_REMOVE) {
|
||||
$output = preg_replace(array('#^(<\?(?:php|=)?)|(<%)|(<script\s+language\s*=\s*["\']?\s*php\s*["\']?\s*>)#', '#(\?>)|(%>)|(<\/script>)$#'), '', $_attr['code']);
|
||||
$compiler->parser->current_buffer->append_subtree(new Smarty_Internal_ParseTree_Text($compiler->parser, $output));
|
||||
return '';
|
||||
} elseif ($this->php_handling == Smarty::PHP_QUOTE) {
|
||||
$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']);
|
||||
$compiler->parser->current_buffer->append_subtree(new Smarty_Internal_ParseTree_Text($compiler->parser, $output));
|
||||
return '';
|
||||
} elseif ($this->php_handling == Smarty::PHP_PASSTHRU || ($_attr['type'] == 'asp' && !$this->asp_tags) || $_attr['type'] == 'unmatched') {
|
||||
$compiler->parser->current_buffer->append_subtree(new Smarty_Internal_ParseTree_Text($compiler->parser, $_attr['code']));
|
||||
return '';
|
||||
} elseif ($this->php_handling == Smarty::PHP_ALLOW) {
|
||||
if (!($compiler->smarty instanceof SmartyBC)) {
|
||||
$compiler->trigger_template_error('$smarty->php_handling PHP_ALLOW not allowed. Use SmartyBC to enable it', $compiler->lex->taglineno);
|
||||
}
|
||||
$compiler->has_code = true;
|
||||
return $_attr['code'];
|
||||
} else {
|
||||
$compiler->trigger_template_error('Illegal $smarty->php_handling value', $compiler->lex->taglineno);
|
||||
}
|
||||
} else {
|
||||
$compiler->has_code = true;
|
||||
$ldel = preg_quote($compiler->smarty->left_delimiter, '#');
|
||||
$rdel = preg_quote($compiler->smarty->right_delimiter, '#');
|
||||
if (!preg_match("#{$ldel}\\s*/\\s*php\\s*{$rdel}$#", $_attr['code'], $match)) {
|
||||
$compiler->trigger_template_error('Missing {/php} closing tag', $compiler->lex->taglineno);
|
||||
}
|
||||
if (!preg_match("#^({$ldel}\\s*php\\s*)((.)*?)({$rdel})#", $_attr['code'], $match)) {
|
||||
$compiler->trigger_template_error('Missing {php} open tag', $compiler->lex->taglineno);
|
||||
}
|
||||
if (!empty($match[2])) {
|
||||
if ('nocache' == trim($match[2])) {
|
||||
$compiler->tag_nocache = true;
|
||||
} else {
|
||||
$compiler->trigger_template_error("illegal value of option flag \"{$match[2]}\"", $compiler->lex->taglineno);
|
||||
}
|
||||
}
|
||||
return preg_replace(array("#^{$ldel}\\s*php\\s*(.)*?{$rdel}#", "#{$ldel}\\s*/\\s*php\\s*{$rdel}$#"), array('<?php ', '?>'), $_attr['code']);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,10 +19,11 @@ abstract class Smarty_Internal_TemplateCompilerBase
|
||||
{
|
||||
/**
|
||||
* Smarty object
|
||||
*
|
||||
* @var Smarty
|
||||
*/
|
||||
public $smarty = null;
|
||||
|
||||
|
||||
/**
|
||||
* hash for nocache sections
|
||||
*
|
||||
@@ -234,12 +235,14 @@ abstract class Smarty_Internal_TemplateCompilerBase
|
||||
|
||||
/**
|
||||
* Flag true when tag is compiled as nocache
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $tag_nocache = false;
|
||||
|
||||
/**
|
||||
* Flag to restart parsing
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $abort_and_recompile = false;
|
||||
@@ -253,18 +256,28 @@ abstract class Smarty_Internal_TemplateCompilerBase
|
||||
|
||||
/**
|
||||
* Prefix code stack
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $prefixCodeStack = array();
|
||||
|
||||
/**
|
||||
* Tag has compiled code
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $has_code = false;
|
||||
|
||||
/**
|
||||
* A variable string was compiled
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $has_variable_string = false;
|
||||
|
||||
/**
|
||||
* Tag creates output
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $has_output = false;
|
||||
@@ -296,9 +309,9 @@ abstract class Smarty_Internal_TemplateCompilerBase
|
||||
/**
|
||||
* Method to compile a Smarty template
|
||||
*
|
||||
* @param Smarty_Internal_Template $template template object to compile
|
||||
* @param bool $nocache true is shall be compiled in nocache mode
|
||||
* @param null|Smarty_Internal_TemplateCompilerBase $parent_compiler
|
||||
* @param Smarty_Internal_Template $template template object to compile
|
||||
* @param bool $nocache true is shall be compiled in nocache mode
|
||||
* @param null|Smarty_Internal_TemplateCompilerBase $parent_compiler
|
||||
*
|
||||
* @return bool true if compiling succeeded, false if it failed
|
||||
*/
|
||||
|
||||
@@ -61,6 +61,12 @@ class Smarty_Internal_Templatelexer
|
||||
* @var bool
|
||||
*/
|
||||
public $is_phpScript = false;
|
||||
/**
|
||||
* php code type
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $phpType = '';
|
||||
/**
|
||||
* escaped left delimiter
|
||||
*
|
||||
@@ -265,20 +271,21 @@ class Smarty_Internal_Templatelexer
|
||||
9 => 0,
|
||||
10 => 0,
|
||||
11 => 0,
|
||||
12 => 0,
|
||||
13 => 0,
|
||||
14 => 2,
|
||||
17 => 0,
|
||||
18 => 0,
|
||||
12 => 6,
|
||||
19 => 0,
|
||||
20 => 0,
|
||||
21 => 0,
|
||||
22 => 0,
|
||||
22 => 1,
|
||||
24 => 6,
|
||||
31 => 7,
|
||||
39 => 6,
|
||||
46 => 3,
|
||||
50 => 0,
|
||||
);
|
||||
if ($this->counter >= strlen($this->data)) {
|
||||
return false; // end of input
|
||||
}
|
||||
$yy_global_pattern = "/\G(\\{\\})|\G(" . $this->ldel . "\\*([\S\s]*?)\\*" . $this->rdel . ")|\G(" . $this->ldel . "\\s*strip\\s*" . $this->rdel . ")|\G(" . $this->ldel . "\\s*\/strip\\s*" . $this->rdel . ")|\G(" . $this->ldel . "\\s*literal\\s*" . $this->rdel . ")|\G(" . $this->ldel . "\\s*(if|elseif|else if|while)\\s+)|\G(" . $this->ldel . "\\s*for\\s+)|\G(" . $this->ldel . "\\s*foreach(?![^\s]))|\G(" . $this->ldel . "\\s*setfilter\\s+)|\G(" . $this->ldel . "\\s*\/)|\G(" . $this->ldel . "\\s*)|\G((<script\\s+language\\s*=\\s*[\"']?\\s*php\\s*[\"']?\\s*>)|(<\\?(?:php\\w+|=|[a-zA-Z]+)?))|\G(\\?>)|\G(<\/script>)|\G(\\s*" . $this->rdel . ")|\G(<%)|\G(%>)|\G([\S\s])/iS";
|
||||
$yy_global_pattern = "/\G(\\{\\})|\G(" . $this->ldel . "\\*([\S\s]*?)\\*" . $this->rdel . ")|\G(" . $this->ldel . "\\s*strip\\s*" . $this->rdel . ")|\G(" . $this->ldel . "\\s*\/strip\\s*" . $this->rdel . ")|\G(" . $this->ldel . "\\s*literal\\s*" . $this->rdel . ")|\G(" . $this->ldel . "\\s*(if|elseif|else if|while)\\s+)|\G(" . $this->ldel . "\\s*for\\s+)|\G(" . $this->ldel . "\\s*foreach(?![^\s]))|\G(" . $this->ldel . "\\s*setfilter\\s+)|\G((" . $this->ldel . "\\s*php\\s*(.)*?" . $this->rdel . "((.)*?)" . $this->ldel . "\\s*\/php\\s*" . $this->rdel . ")|(" . $this->ldel . "\\s*[\/]?php\\s*(.)*?" . $this->rdel . "))|\G(" . $this->ldel . "\\s*\/)|\G(" . $this->ldel . "\\s*)|\G(\\s*" . $this->rdel . ")|\G(<\\?xml\\s+([\S\s]*?)\\?>)|\G(<%((('[^'\\\\]*(?:\\\\.[^'\\\\]*)*')|(\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")|(\/\\*(.)*?\\*\/)|.)*?)%>)|\G((<\\?(?:php\\s+|=)?)((('[^'\\\\]*(?:\\\\.[^'\\\\]*)*')|(\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")|(\/\\*(.)*?\\*\/)|.)*?)\\?>)|\G(<script\\s+language\\s*=\\s*[\"']?\\s*php\\s*[\"']?\\s*>((('[^'\\\\]*(?:\\\\.[^'\\\\]*)*')|(\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")|(\/\\*(.)*?\\*\/)|.)*?)<\/script>)|\G((<(\\?(?:php\\s+|=)?|(script\\s+language\\s*=\\s*[\"']?\\s*php\\s*[\"']?\\s*>)|%))|\\?>|%>)|\G([\S\s])/iS";
|
||||
|
||||
do {
|
||||
if (preg_match($yy_global_pattern, $this->data, $yymatches, null, $this->counter)) {
|
||||
@@ -420,6 +427,18 @@ class Smarty_Internal_Templatelexer
|
||||
}
|
||||
|
||||
function yy_r1_12($yy_subpatterns)
|
||||
{
|
||||
|
||||
if ($this->smarty->auto_literal && isset($this->value[$this->ldel_length]) ? strpos(" \n\t\r", $this->value[$this->ldel_length]) !== false : false) {
|
||||
$this->token = Smarty_Internal_Templateparser::TP_TEXT;
|
||||
} else {
|
||||
$this->token = Smarty_Internal_Templateparser::TP_PHP;
|
||||
$this->phpType = 'tag';
|
||||
$this->taglineno = $this->line;
|
||||
}
|
||||
}
|
||||
|
||||
function yy_r1_19($yy_subpatterns)
|
||||
{
|
||||
|
||||
if ($this->smarty->auto_literal && isset($this->value[$this->ldel_length]) ? strpos(" \n\t\r", $this->value[$this->ldel_length]) !== false : false) {
|
||||
@@ -431,7 +450,7 @@ class Smarty_Internal_Templatelexer
|
||||
}
|
||||
}
|
||||
|
||||
function yy_r1_13($yy_subpatterns)
|
||||
function yy_r1_20($yy_subpatterns)
|
||||
{
|
||||
|
||||
if ($this->smarty->auto_literal && isset($this->value[$this->ldel_length]) ? strpos(" \n\t\r", $this->value[$this->ldel_length]) !== false : false) {
|
||||
@@ -443,58 +462,56 @@ class Smarty_Internal_Templatelexer
|
||||
}
|
||||
}
|
||||
|
||||
function yy_r1_14($yy_subpatterns)
|
||||
{
|
||||
|
||||
if (($script = strpos($this->value, '<s') === 0) || in_array($this->value, Array('<?', '<?=', '<?php'))) {
|
||||
if ($script) {
|
||||
$this->is_phpScript = true;
|
||||
}
|
||||
$this->token = Smarty_Internal_Templateparser::TP_PHPSTARTTAG;
|
||||
} elseif ($this->value == '<?xml') {
|
||||
$this->token = Smarty_Internal_Templateparser::TP_XMLTAG;
|
||||
} else {
|
||||
$this->token = Smarty_Internal_Templateparser::TP_TEXT;
|
||||
//$this->value = substr($this->value, 0, 2);
|
||||
}
|
||||
}
|
||||
|
||||
function yy_r1_17($yy_subpatterns)
|
||||
{
|
||||
|
||||
$this->token = Smarty_Internal_Templateparser::TP_PHPENDTAG;
|
||||
}
|
||||
|
||||
function yy_r1_18($yy_subpatterns)
|
||||
{
|
||||
|
||||
$this->token = Smarty_Internal_Templateparser::TP_PHPENDSCRIPT;
|
||||
}
|
||||
|
||||
function yy_r1_19($yy_subpatterns)
|
||||
function yy_r1_21($yy_subpatterns)
|
||||
{
|
||||
|
||||
$this->token = Smarty_Internal_Templateparser::TP_TEXT;
|
||||
}
|
||||
|
||||
function yy_r1_20($yy_subpatterns)
|
||||
{
|
||||
|
||||
$this->token = Smarty_Internal_Templateparser::TP_ASPSTARTTAG;
|
||||
}
|
||||
|
||||
function yy_r1_21($yy_subpatterns)
|
||||
{
|
||||
|
||||
$this->token = Smarty_Internal_Templateparser::TP_ASPENDTAG;
|
||||
}
|
||||
|
||||
function yy_r1_22($yy_subpatterns)
|
||||
{
|
||||
|
||||
$phpEndScript = $this->is_phpScript ? '|<\\/script>' : '';
|
||||
$this->token = Smarty_Internal_Templateparser::TP_XMLTAG;
|
||||
$this->taglineno = $this->line;
|
||||
}
|
||||
|
||||
function yy_r1_24($yy_subpatterns)
|
||||
{
|
||||
|
||||
$this->phpType = 'asp';
|
||||
$this->taglineno = $this->line;
|
||||
$this->token = Smarty_Internal_Templateparser::TP_PHP;
|
||||
}
|
||||
|
||||
function yy_r1_31($yy_subpatterns)
|
||||
{
|
||||
|
||||
$this->phpType = 'php';
|
||||
$this->taglineno = $this->line;
|
||||
$this->token = Smarty_Internal_Templateparser::TP_PHP;
|
||||
}
|
||||
|
||||
function yy_r1_39($yy_subpatterns)
|
||||
{
|
||||
|
||||
$this->phpType = 'script';
|
||||
$this->taglineno = $this->line;
|
||||
$this->token = Smarty_Internal_Templateparser::TP_PHP;
|
||||
}
|
||||
|
||||
function yy_r1_46($yy_subpatterns)
|
||||
{
|
||||
|
||||
$this->phpType = 'unmatched';
|
||||
$this->taglineno = $this->line;
|
||||
$this->token = Smarty_Internal_Templateparser::TP_PHP;
|
||||
}
|
||||
|
||||
function yy_r1_50($yy_subpatterns)
|
||||
{
|
||||
|
||||
$to = strlen($this->data);
|
||||
preg_match("/{$this->ldel}|<\?|<%|\?>|%>|<script\s+language\s*=\s*[\"\']?\s*php\s*[\"\']?\s*>{$phpEndScript}/", $this->data, $match, PREG_OFFSET_CAPTURE, $this->counter);
|
||||
preg_match("/{$this->ldel}|<\?|<%|\?>|%>|<script\s+language\s*=\s*[\"\']?\s*php\s*[\"\']?\s*>/", $this->data, $match, PREG_OFFSET_CAPTURE, $this->counter);
|
||||
if (isset($match[0][1])) {
|
||||
$to = $match[0][1];
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user