1b5652a11cd7ba35cea7f657027c0c45dfa70051
[GitHub/Stricted/Domain-Control-Panel.git] / lib / api / smarty / sysplugins / smarty_internal_compile_private_php.php
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 {
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);
45 }
46 if ($_attr['type'] != 'tag') {
47 if (isset($compiler->smarty->security_policy)) {
48 $this->php_handling = $compiler->smarty->security_policy->php_handling;
49 } else {
50 $this->php_handling = $compiler->smarty->php_handling;
51 }
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));
55 return '';
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));
59 return '';
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']));
62 return '';
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);
66 }
67 $compiler->has_code = true;
68 return $_attr['code'];
69 } else {
70 $compiler->trigger_template_error('Illegal $smarty->php_handling value', $compiler->lex->taglineno);
71 }
72 } else {
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);
78 }
79 if (!preg_match("#^({$ldel}\\s*php\\s*)((.)*?)({$rdel})#", $_attr['code'], $match)) {
80 $compiler->trigger_template_error('Missing {php} open tag', $compiler->lex->taglineno);
81 }
82 if (!empty($match[2])) {
83 if ('nocache' == trim($match[2])) {
84 $compiler->tag_nocache = true;
85 } else {
86 $compiler->trigger_template_error("illegal value of option flag \"{$match[2]}\"", $compiler->lex->taglineno);
87 }
88 }
89 return preg_replace(array("#^{$ldel}\\s*php\\s*(.)*?{$rdel}#", "#{$ldel}\\s*/\\s*php\\s*{$rdel}$#"), array('<?php ', '?>'), $_attr['code']);
90 }
91 }
92 }