PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/PhPVersion/application/third_party/Smarty/libs/sysplugins/smarty_internal_compile_private_php.php

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