smarty_internal_parsetree.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Templateparser Parsetrees
  4. *
  5. * These are classes to build parsetrees in the template parser
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Thue Kristensen
  10. * @author Uwe Tews
  11. */
  12. /**
  13. * @package Smarty
  14. * @subpackage Compiler
  15. * @ignore
  16. */
  17. abstract class _smarty_parsetree {
  18. /**
  19. * Parser object
  20. * @var object
  21. */
  22. public $parser;
  23. /**
  24. * Buffer content
  25. * @var mixed
  26. */
  27. public $data;
  28. /**
  29. * Return buffer
  30. *
  31. * @return string buffer content
  32. */
  33. abstract public function to_smarty_php();
  34. }
  35. /**
  36. * A complete smarty tag.
  37. *
  38. * @package Smarty
  39. * @subpackage Compiler
  40. * @ignore
  41. */
  42. class _smarty_tag extends _smarty_parsetree {
  43. /**
  44. * Saved block nesting level
  45. * @var int
  46. */
  47. public $saved_block_nesting;
  48. /**
  49. * Create parse tree buffer for Smarty tag
  50. *
  51. * @param object $parser parser object
  52. * @param string $data content
  53. */
  54. public function __construct($parser, $data)
  55. {
  56. $this->parser = $parser;
  57. $this->data = $data;
  58. $this->saved_block_nesting = $parser->block_nesting_level;
  59. }
  60. /**
  61. * Return buffer content
  62. *
  63. * @return string content
  64. */
  65. public function to_smarty_php()
  66. {
  67. return $this->data;
  68. }
  69. /**
  70. * Return complied code that loads the evaluated outout of buffer content into a temporary variable
  71. *
  72. * @return string template code
  73. */
  74. public function assign_to_var()
  75. {
  76. $var = sprintf('$_tmp%d', ++$this->parser->prefix_number);
  77. $this->parser->compiler->prefix_code[] = sprintf('<?php ob_start();?>%s<?php %s=ob_get_clean();?>', $this->data, $var);
  78. return $var;
  79. }
  80. }
  81. /**
  82. * Code fragment inside a tag.
  83. *
  84. * @package Smarty
  85. * @subpackage Compiler
  86. * @ignore
  87. */
  88. class _smarty_code extends _smarty_parsetree {
  89. /**
  90. * Create parse tree buffer for code fragment
  91. *
  92. * @param object $parser parser object
  93. * @param string $data content
  94. */
  95. public function __construct($parser, $data)
  96. {
  97. $this->parser = $parser;
  98. $this->data = $data;
  99. }
  100. /**
  101. * Return buffer content in parentheses
  102. *
  103. * @return string content
  104. */
  105. public function to_smarty_php()
  106. {
  107. return sprintf("(%s)", $this->data);
  108. }
  109. }
  110. /**
  111. * Double quoted string inside a tag.
  112. *
  113. * @package Smarty
  114. * @subpackage Compiler
  115. * @ignore
  116. */
  117. class _smarty_doublequoted extends _smarty_parsetree {
  118. /**
  119. * Create parse tree buffer for double quoted string subtrees
  120. *
  121. * @param object $parser parser object
  122. * @param _smarty_parsetree $subtree parsetree buffer
  123. */
  124. public function __construct($parser, _smarty_parsetree $subtree)
  125. {
  126. $this->parser = $parser;
  127. $this->subtrees[] = $subtree;
  128. if ($subtree instanceof _smarty_tag) {
  129. $this->parser->block_nesting_level = count($this->parser->compiler->_tag_stack);
  130. }
  131. }
  132. /**
  133. * Append buffer to subtree
  134. *
  135. * @param _smarty_parsetree $subtree parsetree buffer
  136. */
  137. public function append_subtree(_smarty_parsetree $subtree)
  138. {
  139. $last_subtree = count($this->subtrees) - 1;
  140. if ($last_subtree >= 0 && $this->subtrees[$last_subtree] instanceof _smarty_tag && $this->subtrees[$last_subtree]->saved_block_nesting < $this->parser->block_nesting_level) {
  141. if ($subtree instanceof _smarty_code) {
  142. $this->subtrees[$last_subtree]->data .= '<?php echo ' . $subtree->data . ';?>';
  143. } elseif ($subtree instanceof _smarty_dq_content) {
  144. $this->subtrees[$last_subtree]->data .= '<?php echo "' . $subtree->data . '";?>';
  145. } else {
  146. $this->subtrees[$last_subtree]->data .= $subtree->data;
  147. }
  148. } else {
  149. $this->subtrees[] = $subtree;
  150. }
  151. if ($subtree instanceof _smarty_tag) {
  152. $this->parser->block_nesting_level = count($this->parser->compiler->_tag_stack);
  153. }
  154. }
  155. /**
  156. * Merge subtree buffer content together
  157. *
  158. * @return string compiled template code
  159. */
  160. public function to_smarty_php()
  161. {
  162. $code = '';
  163. foreach ($this->subtrees as $subtree) {
  164. if ($code !== "") {
  165. $code .= ".";
  166. }
  167. if ($subtree instanceof _smarty_tag) {
  168. $more_php = $subtree->assign_to_var();
  169. } else {
  170. $more_php = $subtree->to_smarty_php();
  171. }
  172. $code .= $more_php;
  173. if (!$subtree instanceof _smarty_dq_content) {
  174. $this->parser->compiler->has_variable_string = true;
  175. }
  176. }
  177. return $code;
  178. }
  179. }
  180. /**
  181. * Raw chars as part of a double quoted string.
  182. *
  183. * @package Smarty
  184. * @subpackage Compiler
  185. * @ignore
  186. */
  187. class _smarty_dq_content extends _smarty_parsetree {
  188. /**
  189. * Create parse tree buffer with string content
  190. *
  191. * @param object $parser parser object
  192. * @param string $data string section
  193. */
  194. public function __construct($parser, $data)
  195. {
  196. $this->parser = $parser;
  197. $this->data = $data;
  198. }
  199. /**
  200. * Return content as double quoted string
  201. *
  202. * @return string doubled quoted string
  203. */
  204. public function to_smarty_php()
  205. {
  206. return '"' . $this->data . '"';
  207. }
  208. }
  209. /**
  210. * Template element
  211. *
  212. * @package Smarty
  213. * @subpackage Compiler
  214. * @ignore
  215. */
  216. class _smarty_template_buffer extends _smarty_parsetree {
  217. /**
  218. * Array of template elements
  219. *
  220. * @var array
  221. */
  222. public $subtrees = Array();
  223. /**
  224. * Create root of parse tree for template elements
  225. *
  226. * @param object $parser parse object
  227. */
  228. public function __construct($parser)
  229. {
  230. $this->parser = $parser;
  231. }
  232. /**
  233. * Append buffer to subtree
  234. *
  235. * @param _smarty_parsetree $subtree
  236. */
  237. public function append_subtree(_smarty_parsetree $subtree)
  238. {
  239. $this->subtrees[] = $subtree;
  240. }
  241. /**
  242. * Sanitize and merge subtree buffers together
  243. *
  244. * @return string template code content
  245. */
  246. public function to_smarty_php()
  247. {
  248. $code = '';
  249. for ($key = 0, $cnt = count($this->subtrees); $key < $cnt; $key++) {
  250. if ($key + 2 < $cnt) {
  251. if ($this->subtrees[$key] instanceof _smarty_linebreak && $this->subtrees[$key + 1] instanceof _smarty_tag && $this->subtrees[$key + 1]->data == '' && $this->subtrees[$key + 2] instanceof _smarty_linebreak) {
  252. $key = $key + 1;
  253. continue;
  254. }
  255. if (substr($this->subtrees[$key]->data, -1) == '<' && $this->subtrees[$key + 1]->data == '' && substr($this->subtrees[$key + 2]->data, -1) == '?') {
  256. $key = $key + 2;
  257. continue;
  258. }
  259. }
  260. if (substr($code, -1) == '<') {
  261. $subtree = $this->subtrees[$key]->to_smarty_php();
  262. if (substr($subtree, 0, 1) == '?') {
  263. $code = substr($code, 0, strlen($code) - 1) . '<<?php ?>?' . substr($subtree, 1);
  264. } elseif ($this->parser->asp_tags && substr($subtree, 0, 1) == '%') {
  265. $code = substr($code, 0, strlen($code) - 1) . '<<?php ?>%' . substr($subtree, 1);
  266. } else {
  267. $code .= $subtree;
  268. }
  269. continue;
  270. }
  271. if ($this->parser->asp_tags && substr($code, -1) == '%') {
  272. $subtree = $this->subtrees[$key]->to_smarty_php();
  273. if (substr($subtree, 0, 1) == '>') {
  274. $code = substr($code, 0, strlen($code) - 1) . '%<?php ?>>' . substr($subtree, 1);
  275. } else {
  276. $code .= $subtree;
  277. }
  278. continue;
  279. }
  280. if (substr($code, -1) == '?') {
  281. $subtree = $this->subtrees[$key]->to_smarty_php();
  282. if (substr($subtree, 0, 1) == '>') {
  283. $code = substr($code, 0, strlen($code) - 1) . '?<?php ?>>' . substr($subtree, 1);
  284. } else {
  285. $code .= $subtree;
  286. }
  287. continue;
  288. }
  289. $code .= $this->subtrees[$key]->to_smarty_php();
  290. }
  291. return $code;
  292. }
  293. }
  294. /**
  295. * template text
  296. *
  297. * @package Smarty
  298. * @subpackage Compiler
  299. * @ignore
  300. */
  301. class _smarty_text extends _smarty_parsetree {
  302. /**
  303. * Create template text buffer
  304. *
  305. * @param object $parser parser object
  306. * @param string $data text
  307. */
  308. public function __construct($parser, $data)
  309. {
  310. $this->parser = $parser;
  311. $this->data = $data;
  312. }
  313. /**
  314. * Return buffer content
  315. *
  316. * @return strint text
  317. */
  318. public function to_smarty_php()
  319. {
  320. return $this->data;
  321. }
  322. }
  323. /**
  324. * template linebreaks
  325. *
  326. * @package Smarty
  327. * @subpackage Compiler
  328. * @ignore
  329. */
  330. class _smarty_linebreak extends _smarty_parsetree {
  331. /**
  332. * Create buffer with linebreak content
  333. *
  334. * @param object $parser parser object
  335. * @param string $data linebreak string
  336. */
  337. public function __construct($parser, $data)
  338. {
  339. $this->parser = $parser;
  340. $this->data = $data;
  341. }
  342. /**
  343. * Return linebrak
  344. *
  345. * @return string linebreak
  346. */
  347. public function to_smarty_php()
  348. {
  349. return $this->data;
  350. }
  351. }
  352. ?>