PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/smarty/Smarty_Compiler.class.php

https://github.com/michaeljoyce/pkp-lib
PHP | 2367 lines | 1742 code | 218 blank | 407 comment | 318 complexity | 503d9a86576cfc9418835f0408a92c28 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Project: Smarty: the PHP compiling template engine
  4. * File: Smarty_Compiler.class.php
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * @link http://smarty.php.net/
  21. * @author Monte Ohrt <monte at ohrt dot com>
  22. * @author Andrei Zmievski <andrei@php.net>
  23. * @version 2.6.26
  24. * @copyright 2001-2005 New Digital Group, Inc.
  25. * @package Smarty
  26. */
  27. /* $Id$ */
  28. /**
  29. * Template compiling class
  30. * @package Smarty
  31. */
  32. class Smarty_Compiler extends Smarty {
  33. // internal vars
  34. /**#@+
  35. * @access private
  36. */
  37. var $_folded_blocks = array(); // keeps folded template blocks
  38. var $_current_file = null; // the current template being compiled
  39. var $_current_line_no = 1; // line number for error messages
  40. var $_capture_stack = array(); // keeps track of nested capture buffers
  41. var $_plugin_info = array(); // keeps track of plugins to load
  42. var $_init_smarty_vars = false;
  43. var $_permitted_tokens = array('true','false','yes','no','on','off','null');
  44. var $_db_qstr_regexp = null; // regexps are setup in the constructor
  45. var $_si_qstr_regexp = null;
  46. var $_qstr_regexp = null;
  47. var $_func_regexp = null;
  48. var $_reg_obj_regexp = null;
  49. var $_var_bracket_regexp = null;
  50. var $_num_const_regexp = null;
  51. var $_dvar_guts_regexp = null;
  52. var $_dvar_regexp = null;
  53. var $_cvar_regexp = null;
  54. var $_svar_regexp = null;
  55. var $_avar_regexp = null;
  56. var $_mod_regexp = null;
  57. var $_var_regexp = null;
  58. var $_parenth_param_regexp = null;
  59. var $_func_call_regexp = null;
  60. var $_obj_ext_regexp = null;
  61. var $_obj_start_regexp = null;
  62. var $_obj_params_regexp = null;
  63. var $_obj_call_regexp = null;
  64. var $_cacheable_state = 0;
  65. var $_cache_attrs_count = 0;
  66. var $_nocache_count = 0;
  67. var $_cache_serial = null;
  68. var $_cache_include = null;
  69. var $_strip_depth = 0;
  70. var $_additional_newline = "\n";
  71. /**#@-*/
  72. /**
  73. * The class constructor.
  74. */
  75. function Smarty_Compiler()
  76. {
  77. // matches double quoted strings:
  78. // "foobar"
  79. // "foo\"bar"
  80. $this->_db_qstr_regexp = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';
  81. // matches single quoted strings:
  82. // 'foobar'
  83. // 'foo\'bar'
  84. $this->_si_qstr_regexp = '\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'';
  85. // matches single or double quoted strings
  86. $this->_qstr_regexp = '(?:' . $this->_db_qstr_regexp . '|' . $this->_si_qstr_regexp . ')';
  87. // matches bracket portion of vars
  88. // [0]
  89. // [foo]
  90. // [$bar]
  91. $this->_var_bracket_regexp = '\[\$?[\w\.]+\]';
  92. // matches numerical constants
  93. // 30
  94. // -12
  95. // 13.22
  96. $this->_num_const_regexp = '(?:\-?\d+(?:\.\d+)?)';
  97. // matches $ vars (not objects):
  98. // $foo
  99. // $foo.bar
  100. // $foo.bar.foobar
  101. // $foo[0]
  102. // $foo[$bar]
  103. // $foo[5][blah]
  104. // $foo[5].bar[$foobar][4]
  105. $this->_dvar_math_regexp = '(?:[\+\*\/\%]|(?:-(?!>)))';
  106. $this->_dvar_math_var_regexp = '[\$\w\.\+\-\*\/\%\d\>\[\]]';
  107. $this->_dvar_guts_regexp = '\w+(?:' . $this->_var_bracket_regexp
  108. . ')*(?:\.\$?\w+(?:' . $this->_var_bracket_regexp . ')*)*(?:' . $this->_dvar_math_regexp . '(?:' . $this->_num_const_regexp . '|' . $this->_dvar_math_var_regexp . ')*)?';
  109. $this->_dvar_regexp = '\$' . $this->_dvar_guts_regexp;
  110. // matches config vars:
  111. // #foo#
  112. // #foobar123_foo#
  113. $this->_cvar_regexp = '\#\w+\#';
  114. // matches section vars:
  115. // %foo.bar%
  116. $this->_svar_regexp = '\%\w+\.\w+\%';
  117. // matches all valid variables (no quotes, no modifiers)
  118. $this->_avar_regexp = '(?:' . $this->_dvar_regexp . '|'
  119. . $this->_cvar_regexp . '|' . $this->_svar_regexp . ')';
  120. // matches valid variable syntax:
  121. // $foo
  122. // $foo
  123. // #foo#
  124. // #foo#
  125. // "text"
  126. // "text"
  127. $this->_var_regexp = '(?:' . $this->_avar_regexp . '|' . $this->_qstr_regexp . ')';
  128. // matches valid object call (one level of object nesting allowed in parameters):
  129. // $foo->bar
  130. // $foo->bar()
  131. // $foo->bar("text")
  132. // $foo->bar($foo, $bar, "text")
  133. // $foo->bar($foo, "foo")
  134. // $foo->bar->foo()
  135. // $foo->bar->foo->bar()
  136. // $foo->bar($foo->bar)
  137. // $foo->bar($foo->bar())
  138. // $foo->bar($foo->bar($blah,$foo,44,"foo",$foo[0].bar))
  139. $this->_obj_ext_regexp = '\->(?:\$?' . $this->_dvar_guts_regexp . ')';
  140. $this->_obj_restricted_param_regexp = '(?:'
  141. . '(?:' . $this->_var_regexp . '|' . $this->_num_const_regexp . ')(?:' . $this->_obj_ext_regexp . '(?:\((?:(?:' . $this->_var_regexp . '|' . $this->_num_const_regexp . ')'
  142. . '(?:\s*,\s*(?:' . $this->_var_regexp . '|' . $this->_num_const_regexp . '))*)?\))?)*)';
  143. $this->_obj_single_param_regexp = '(?:\w+|' . $this->_obj_restricted_param_regexp . '(?:\s*,\s*(?:(?:\w+|'
  144. . $this->_var_regexp . $this->_obj_restricted_param_regexp . ')))*)';
  145. $this->_obj_params_regexp = '\((?:' . $this->_obj_single_param_regexp
  146. . '(?:\s*,\s*' . $this->_obj_single_param_regexp . ')*)?\)';
  147. $this->_obj_start_regexp = '(?:' . $this->_dvar_regexp . '(?:' . $this->_obj_ext_regexp . ')+)';
  148. $this->_obj_call_regexp = '(?:' . $this->_obj_start_regexp . '(?:' . $this->_obj_params_regexp . ')?(?:' . $this->_dvar_math_regexp . '(?:' . $this->_num_const_regexp . '|' . $this->_dvar_math_var_regexp . ')*)?)';
  149. // Added 2004-01-07 Used for expanding object calls in backticks
  150. $this->_obj_callext_regexp = '(?:' . $this->_obj_ext_regexp . ')' . '(?:' . $this->_obj_params_regexp . ')?(?:' . $this->_dvar_math_regexp . '(?:' . $this->_num_const_regexp . '|' . $this->_dvar_math_var_regexp . ')*)?';
  151. // matches valid modifier syntax:
  152. // |foo
  153. // |@foo
  154. // |foo:"bar"
  155. // |foo:$bar
  156. // |foo:"bar":$foobar
  157. // |foo|bar
  158. // |foo:$foo->bar
  159. $this->_mod_regexp = '(?:\|@?\w+(?::(?:\w+|' . $this->_num_const_regexp . '|'
  160. . $this->_obj_call_regexp . '|' . $this->_avar_regexp . '|' . $this->_qstr_regexp .'))*)';
  161. // matches valid function name:
  162. // foo123
  163. // _foo_bar
  164. $this->_func_regexp = '[a-zA-Z_]\w*';
  165. // matches valid registered object:
  166. // foo->bar
  167. $this->_reg_obj_regexp = '[a-zA-Z_]\w*->[a-zA-Z_]\w*';
  168. // matches valid parameter values:
  169. // true
  170. // $foo
  171. // $foo|bar
  172. // #foo#
  173. // #foo#|bar
  174. // "text"
  175. // "text"|bar
  176. // $foo->bar
  177. $this->_param_regexp = '(?:\s*(?:' . $this->_obj_call_regexp . '|'
  178. . $this->_var_regexp . '|' . $this->_num_const_regexp . '|\w+)(?>' . $this->_mod_regexp . '*)\s*)';
  179. // matches valid parenthesised function parameters:
  180. //
  181. // "text"
  182. // $foo, $bar, "text"
  183. // $foo|bar, "foo"|bar, $foo->bar($foo)|bar
  184. $this->_parenth_param_regexp = '(?:\((?:\w+|'
  185. . $this->_param_regexp . '(?:\s*,\s*(?:(?:\w+|'
  186. . $this->_param_regexp . ')))*)?\))';
  187. // matches valid function call:
  188. // foo()
  189. // foo_bar($foo)
  190. // _foo_bar($foo,"bar")
  191. // foo123($foo,$foo->bar(),"foo")
  192. $this->_func_call_regexp = '(?:' . $this->_func_regexp . '\s*(?:'
  193. . $this->_parenth_param_regexp . '))';
  194. }
  195. /**
  196. * compile a resource
  197. *
  198. * sets $compiled_content to the compiled source
  199. * @param string $resource_name
  200. * @param string $source_content
  201. * @param string $compiled_content
  202. * @return true
  203. */
  204. function _compile_file($resource_name, $source_content, &$compiled_content)
  205. {
  206. if ($this->security) {
  207. // do not allow php syntax to be executed unless specified
  208. if ($this->php_handling == SMARTY_PHP_ALLOW &&
  209. !$this->security_settings['PHP_HANDLING']) {
  210. $this->php_handling = SMARTY_PHP_PASSTHRU;
  211. }
  212. }
  213. $this->_load_filters();
  214. $this->_current_file = $resource_name;
  215. $this->_current_line_no = 1;
  216. $ldq = preg_quote($this->left_delimiter, '~');
  217. $rdq = preg_quote($this->right_delimiter, '~');
  218. // run template source through prefilter functions
  219. if (count($this->_plugins['prefilter']) > 0) {
  220. foreach ($this->_plugins['prefilter'] as $filter_name => $prefilter) {
  221. if ($prefilter === false) continue;
  222. if ($prefilter[3] || is_callable($prefilter[0])) {
  223. $source_content = call_user_func_array($prefilter[0],
  224. array($source_content, &$this));
  225. $this->_plugins['prefilter'][$filter_name][3] = true;
  226. } else {
  227. $this->_trigger_fatal_error("[plugin] prefilter '$filter_name' is not implemented");
  228. }
  229. }
  230. }
  231. /* fetch all special blocks */
  232. $search = "~{$ldq}\*(.*?)\*{$rdq}|{$ldq}\s*literal\s*{$rdq}(.*?){$ldq}\s*/literal\s*{$rdq}|{$ldq}\s*php\s*{$rdq}(.*?){$ldq}\s*/php\s*{$rdq}~s";
  233. preg_match_all($search, $source_content, $match, PREG_SET_ORDER);
  234. $this->_folded_blocks = $match;
  235. reset($this->_folded_blocks);
  236. /* replace special blocks by "{php}" */
  237. $source_content = preg_replace($search.'e', "'"
  238. . $this->_quote_replace($this->left_delimiter) . 'php'
  239. . "' . str_repeat(\"\n\", substr_count('\\0', \"\n\")) .'"
  240. . $this->_quote_replace($this->right_delimiter)
  241. . "'"
  242. , $source_content);
  243. /* Gather all template tags. */
  244. preg_match_all("~{$ldq}\s*(.*?)\s*{$rdq}~s", $source_content, $_match);
  245. $template_tags = $_match[1];
  246. /* Split content by template tags to obtain non-template content. */
  247. $text_blocks = preg_split("~{$ldq}.*?{$rdq}~s", $source_content);
  248. /* loop through text blocks */
  249. for ($curr_tb = 0, $for_max = count($text_blocks); $curr_tb < $for_max; $curr_tb++) {
  250. /* match anything resembling php tags */
  251. if (preg_match_all('~(<\?(?:\w+|=)?|\?>|language\s*=\s*[\"\']?\s*php\s*[\"\']?)~is', $text_blocks[$curr_tb], $sp_match)) {
  252. /* replace tags with placeholders to prevent recursive replacements */
  253. $sp_match[1] = array_unique($sp_match[1]);
  254. usort($sp_match[1], '_smarty_sort_length');
  255. for ($curr_sp = 0, $for_max2 = count($sp_match[1]); $curr_sp < $for_max2; $curr_sp++) {
  256. $text_blocks[$curr_tb] = str_replace($sp_match[1][$curr_sp],'%%%SMARTYSP'.$curr_sp.'%%%',$text_blocks[$curr_tb]);
  257. }
  258. /* process each one */
  259. for ($curr_sp = 0, $for_max2 = count($sp_match[1]); $curr_sp < $for_max2; $curr_sp++) {
  260. if ($this->php_handling == SMARTY_PHP_PASSTHRU) {
  261. /* echo php contents */
  262. $text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', '<?php echo \''.str_replace("'", "\'", $sp_match[1][$curr_sp]).'\'; ?>'."\n", $text_blocks[$curr_tb]);
  263. } else if ($this->php_handling == SMARTY_PHP_QUOTE) {
  264. /* quote php tags */
  265. $text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', htmlspecialchars($sp_match[1][$curr_sp]), $text_blocks[$curr_tb]);
  266. } else if ($this->php_handling == SMARTY_PHP_REMOVE) {
  267. /* remove php tags */
  268. $text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', '', $text_blocks[$curr_tb]);
  269. } else {
  270. /* SMARTY_PHP_ALLOW, but echo non php starting tags */
  271. $sp_match[1][$curr_sp] = preg_replace('~(<\?(?!php|=|$))~i', '<?php echo \'\\1\'?>'."\n", $sp_match[1][$curr_sp]);
  272. $text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', $sp_match[1][$curr_sp], $text_blocks[$curr_tb]);
  273. }
  274. }
  275. }
  276. }
  277. /* Compile the template tags into PHP code. */
  278. $compiled_tags = array();
  279. for ($i = 0, $for_max = count($template_tags); $i < $for_max; $i++) {
  280. $this->_current_line_no += substr_count($text_blocks[$i], "\n");
  281. $compiled_tags[] = $this->_compile_tag($template_tags[$i]);
  282. $this->_current_line_no += substr_count($template_tags[$i], "\n");
  283. }
  284. if (count($this->_tag_stack)>0) {
  285. list($_open_tag, $_line_no) = end($this->_tag_stack);
  286. $this->_syntax_error("unclosed tag \{$_open_tag} (opened line $_line_no).", E_USER_ERROR, __FILE__, __LINE__);
  287. return;
  288. }
  289. /* Reformat $text_blocks between 'strip' and '/strip' tags,
  290. removing spaces, tabs and newlines. */
  291. $strip = false;
  292. for ($i = 0, $for_max = count($compiled_tags); $i < $for_max; $i++) {
  293. if ($compiled_tags[$i] == '{strip}') {
  294. $compiled_tags[$i] = '';
  295. $strip = true;
  296. /* remove leading whitespaces */
  297. $text_blocks[$i + 1] = ltrim($text_blocks[$i + 1]);
  298. }
  299. if ($strip) {
  300. /* strip all $text_blocks before the next '/strip' */
  301. for ($j = $i + 1; $j < $for_max; $j++) {
  302. /* remove leading and trailing whitespaces of each line */
  303. $text_blocks[$j] = preg_replace('![\t ]*[\r\n]+[\t ]*!', '', $text_blocks[$j]);
  304. if ($compiled_tags[$j] == '{/strip}') {
  305. /* remove trailing whitespaces from the last text_block */
  306. $text_blocks[$j] = rtrim($text_blocks[$j]);
  307. }
  308. $text_blocks[$j] = "<?php echo '" . strtr($text_blocks[$j], array("'"=>"\'", "\\"=>"\\\\")) . "'; ?>";
  309. if ($compiled_tags[$j] == '{/strip}') {
  310. $compiled_tags[$j] = "\n"; /* slurped by php, but necessary
  311. if a newline is following the closing strip-tag */
  312. $strip = false;
  313. $i = $j;
  314. break;
  315. }
  316. }
  317. }
  318. }
  319. $compiled_content = '';
  320. $tag_guard = '%%%SMARTYOTG' . md5(uniqid(rand(), true)) . '%%%';
  321. /* Interleave the compiled contents and text blocks to get the final result. */
  322. for ($i = 0, $for_max = count($compiled_tags); $i < $for_max; $i++) {
  323. if ($compiled_tags[$i] == '') {
  324. // tag result empty, remove first newline from following text block
  325. $text_blocks[$i+1] = preg_replace('~^(\r\n|\r|\n)~', '', $text_blocks[$i+1]);
  326. }
  327. // replace legit PHP tags with placeholder
  328. $text_blocks[$i] = str_replace('<?', $tag_guard, $text_blocks[$i]);
  329. $compiled_tags[$i] = str_replace('<?', $tag_guard, $compiled_tags[$i]);
  330. $compiled_content .= $text_blocks[$i] . $compiled_tags[$i];
  331. }
  332. $compiled_content .= str_replace('<?', $tag_guard, $text_blocks[$i]);
  333. // escape php tags created by interleaving
  334. $compiled_content = str_replace('<?', "<?php echo '<?' ?>\n", $compiled_content);
  335. $compiled_content = preg_replace("~(?<!')language\s*=\s*[\"\']?\s*php\s*[\"\']?~", "<?php echo 'language=php' ?>\n", $compiled_content);
  336. // recover legit tags
  337. $compiled_content = str_replace($tag_guard, '<?', $compiled_content);
  338. // remove \n from the end of the file, if any
  339. if (strlen($compiled_content) && (substr($compiled_content, -1) == "\n") ) {
  340. $compiled_content = substr($compiled_content, 0, -1);
  341. }
  342. if (!empty($this->_cache_serial)) {
  343. $compiled_content = "<?php \$this->_cache_serials['".$this->_cache_include."'] = '".$this->_cache_serial."'; ?>" . $compiled_content;
  344. }
  345. // run compiled template through postfilter functions
  346. if (count($this->_plugins['postfilter']) > 0) {
  347. foreach ($this->_plugins['postfilter'] as $filter_name => $postfilter) {
  348. if ($postfilter === false) continue;
  349. if ($postfilter[3] || is_callable($postfilter[0])) {
  350. $compiled_content = call_user_func_array($postfilter[0],
  351. array($compiled_content, &$this));
  352. $this->_plugins['postfilter'][$filter_name][3] = true;
  353. } else {
  354. $this->_trigger_fatal_error("Smarty plugin error: postfilter '$filter_name' is not implemented");
  355. }
  356. }
  357. }
  358. // put header at the top of the compiled template
  359. $template_header = "<?php /* Smarty version ".$this->_version.", created on ".strftime("%Y-%m-%d %H:%M:%S")."\n";
  360. $template_header .= " compiled from ".strtr(urlencode($resource_name), array('%2F'=>'/', '%3A'=>':'))." */ ?>\n";
  361. /* Emit code to load needed plugins. */
  362. $this->_plugins_code = '';
  363. if (count($this->_plugin_info)) {
  364. $_plugins_params = "array('plugins' => array(";
  365. foreach ($this->_plugin_info as $plugin_type => $plugins) {
  366. foreach ($plugins as $plugin_name => $plugin_info) {
  367. $_plugins_params .= "array('$plugin_type', '$plugin_name', '" . strtr($plugin_info[0], array("'" => "\\'", "\\" => "\\\\")) . "', $plugin_info[1], ";
  368. $_plugins_params .= $plugin_info[2] ? 'true),' : 'false),';
  369. }
  370. }
  371. $_plugins_params .= '))';
  372. $plugins_code = "<?php require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');\nsmarty_core_load_plugins($_plugins_params, \$this); ?>\n";
  373. $template_header .= $plugins_code;
  374. $this->_plugin_info = array();
  375. $this->_plugins_code = $plugins_code;
  376. }
  377. if ($this->_init_smarty_vars) {
  378. $template_header .= "<?php require_once(SMARTY_CORE_DIR . 'core.assign_smarty_interface.php');\nsmarty_core_assign_smarty_interface(null, \$this); ?>\n";
  379. $this->_init_smarty_vars = false;
  380. }
  381. $compiled_content = $template_header . $compiled_content;
  382. return true;
  383. }
  384. /**
  385. * Compile a template tag
  386. *
  387. * @param string $template_tag
  388. * @return string
  389. */
  390. function _compile_tag($template_tag)
  391. {
  392. /* Matched comment. */
  393. if (substr($template_tag, 0, 1) == '*' && substr($template_tag, -1) == '*')
  394. return '';
  395. /* Split tag into two three parts: command, command modifiers and the arguments. */
  396. if(! preg_match('~^(?:(' . $this->_num_const_regexp . '|' . $this->_obj_call_regexp . '|' . $this->_var_regexp
  397. . '|\/?' . $this->_reg_obj_regexp . '|\/?' . $this->_func_regexp . ')(' . $this->_mod_regexp . '*))
  398. (?:\s+(.*))?$
  399. ~xs', $template_tag, $match)) {
  400. $this->_syntax_error("unrecognized tag: $template_tag", E_USER_ERROR, __FILE__, __LINE__);
  401. }
  402. $tag_command = $match[1];
  403. $tag_modifier = isset($match[2]) ? $match[2] : null;
  404. $tag_args = isset($match[3]) ? $match[3] : null;
  405. if (preg_match('~^' . $this->_num_const_regexp . '|' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '$~', $tag_command)) {
  406. /* tag name is a variable or object */
  407. $_return = $this->_parse_var_props($tag_command . $tag_modifier);
  408. return "<?php echo $_return; ?>" . $this->_additional_newline;
  409. }
  410. /* If the tag name is a registered object, we process it. */
  411. if (preg_match('~^\/?' . $this->_reg_obj_regexp . '$~', $tag_command)) {
  412. return $this->_compile_registered_object_tag($tag_command, $this->_parse_attrs($tag_args), $tag_modifier);
  413. }
  414. switch ($tag_command) {
  415. case 'include':
  416. return $this->_compile_include_tag($tag_args);
  417. case 'include_php':
  418. return $this->_compile_include_php_tag($tag_args);
  419. case 'if':
  420. $this->_push_tag('if');
  421. return $this->_compile_if_tag($tag_args);
  422. case 'else':
  423. list($_open_tag) = end($this->_tag_stack);
  424. if ($_open_tag != 'if' && $_open_tag != 'elseif')
  425. $this->_syntax_error('unexpected {else}', E_USER_ERROR, __FILE__, __LINE__);
  426. else
  427. $this->_push_tag('else');
  428. return '<?php else: ?>';
  429. case 'elseif':
  430. list($_open_tag) = end($this->_tag_stack);
  431. if ($_open_tag != 'if' && $_open_tag != 'elseif')
  432. $this->_syntax_error('unexpected {elseif}', E_USER_ERROR, __FILE__, __LINE__);
  433. if ($_open_tag == 'if')
  434. $this->_push_tag('elseif');
  435. return $this->_compile_if_tag($tag_args, true);
  436. case '/if':
  437. $this->_pop_tag('if');
  438. return '<?php endif; ?>';
  439. case 'capture':
  440. return $this->_compile_capture_tag(true, $tag_args);
  441. case '/capture':
  442. return $this->_compile_capture_tag(false);
  443. case 'ldelim':
  444. return $this->left_delimiter;
  445. case 'rdelim':
  446. return $this->right_delimiter;
  447. case 'section':
  448. $this->_push_tag('section');
  449. return $this->_compile_section_start($tag_args);
  450. case 'sectionelse':
  451. $this->_push_tag('sectionelse');
  452. return "<?php endfor; else: ?>";
  453. break;
  454. case '/section':
  455. $_open_tag = $this->_pop_tag('section');
  456. if ($_open_tag == 'sectionelse')
  457. return "<?php endif; ?>";
  458. else
  459. return "<?php endfor; endif; ?>";
  460. case 'foreach':
  461. $this->_push_tag('foreach');
  462. return $this->_compile_foreach_start($tag_args);
  463. break;
  464. case 'foreachelse':
  465. $this->_push_tag('foreachelse');
  466. return "<?php endforeach; else: ?>";
  467. case '/foreach':
  468. $_open_tag = $this->_pop_tag('foreach');
  469. if ($_open_tag == 'foreachelse')
  470. return "<?php endif; unset(\$_from); ?>";
  471. else
  472. return "<?php endforeach; endif; unset(\$_from); ?>";
  473. break;
  474. case 'strip':
  475. case '/strip':
  476. if (substr($tag_command, 0, 1)=='/') {
  477. $this->_pop_tag('strip');
  478. if (--$this->_strip_depth==0) { /* outermost closing {/strip} */
  479. $this->_additional_newline = "\n";
  480. return '{' . $tag_command . '}';
  481. }
  482. } else {
  483. $this->_push_tag('strip');
  484. if ($this->_strip_depth++==0) { /* outermost opening {strip} */
  485. $this->_additional_newline = "";
  486. return '{' . $tag_command . '}';
  487. }
  488. }
  489. return '';
  490. case 'php':
  491. /* handle folded tags replaced by {php} */
  492. list(, $block) = each($this->_folded_blocks);
  493. $this->_current_line_no += substr_count($block[0], "\n");
  494. /* the number of matched elements in the regexp in _compile_file()
  495. determins the type of folded tag that was found */
  496. switch (count($block)) {
  497. case 2: /* comment */
  498. return '';
  499. case 3: /* literal */
  500. return "<?php echo '" . strtr($block[2], array("'"=>"\'", "\\"=>"\\\\")) . "'; ?>" . $this->_additional_newline;
  501. case 4: /* php */
  502. if ($this->security && !$this->security_settings['PHP_TAGS']) {
  503. $this->_syntax_error("(secure mode) php tags not permitted", E_USER_WARNING, __FILE__, __LINE__);
  504. return;
  505. }
  506. return '<?php ' . $block[3] .' ?>';
  507. }
  508. break;
  509. case 'insert':
  510. return $this->_compile_insert_tag($tag_args);
  511. default:
  512. if ($this->_compile_compiler_tag($tag_command, $tag_args, $output)) {
  513. return $output;
  514. } else if ($this->_compile_block_tag($tag_command, $tag_args, $tag_modifier, $output)) {
  515. return $output;
  516. } else if ($this->_compile_custom_tag($tag_command, $tag_args, $tag_modifier, $output)) {
  517. return $output;
  518. } else {
  519. $this->_syntax_error("unrecognized tag '$tag_command'", E_USER_ERROR, __FILE__, __LINE__);
  520. }
  521. }
  522. }
  523. /**
  524. * compile the custom compiler tag
  525. *
  526. * sets $output to the compiled custom compiler tag
  527. * @param string $tag_command
  528. * @param string $tag_args
  529. * @param string $output
  530. * @return boolean
  531. */
  532. function _compile_compiler_tag($tag_command, $tag_args, &$output)
  533. {
  534. $found = false;
  535. $have_function = true;
  536. /*
  537. * First we check if the compiler function has already been registered
  538. * or loaded from a plugin file.
  539. */
  540. if (isset($this->_plugins['compiler'][$tag_command])) {
  541. $found = true;
  542. $plugin_func = $this->_plugins['compiler'][$tag_command][0];
  543. if (!is_callable($plugin_func)) {
  544. $message = "compiler function '$tag_command' is not implemented";
  545. $have_function = false;
  546. }
  547. }
  548. /*
  549. * Otherwise we need to load plugin file and look for the function
  550. * inside it.
  551. */
  552. else if ($plugin_file = $this->_get_plugin_filepath('compiler', $tag_command)) {
  553. $found = true;
  554. include_once $plugin_file;
  555. $plugin_func = 'smarty_compiler_' . $tag_command;
  556. if (!is_callable($plugin_func)) {
  557. $message = "plugin function $plugin_func() not found in $plugin_file\n";
  558. $have_function = false;
  559. } else {
  560. $this->_plugins['compiler'][$tag_command] = array($plugin_func, null, null, null, true);
  561. }
  562. }
  563. /*
  564. * True return value means that we either found a plugin or a
  565. * dynamically registered function. False means that we didn't and the
  566. * compiler should now emit code to load custom function plugin for this
  567. * tag.
  568. */
  569. if ($found) {
  570. if ($have_function) {
  571. $output = call_user_func_array($plugin_func, array($tag_args, &$this));
  572. if($output != '') {
  573. $output = '<?php ' . $this->_push_cacheable_state('compiler', $tag_command)
  574. . $output
  575. . $this->_pop_cacheable_state('compiler', $tag_command) . ' ?>';
  576. }
  577. } else {
  578. $this->_syntax_error($message, E_USER_WARNING, __FILE__, __LINE__);
  579. }
  580. return true;
  581. } else {
  582. return false;
  583. }
  584. }
  585. /**
  586. * compile block function tag
  587. *
  588. * sets $output to compiled block function tag
  589. * @param string $tag_command
  590. * @param string $tag_args
  591. * @param string $tag_modifier
  592. * @param string $output
  593. * @return boolean
  594. */
  595. function _compile_block_tag($tag_command, $tag_args, $tag_modifier, &$output)
  596. {
  597. if (substr($tag_command, 0, 1) == '/') {
  598. $start_tag = false;
  599. $tag_command = substr($tag_command, 1);
  600. } else
  601. $start_tag = true;
  602. $found = false;
  603. $have_function = true;
  604. /*
  605. * First we check if the block function has already been registered
  606. * or loaded from a plugin file.
  607. */
  608. if (isset($this->_plugins['block'][$tag_command])) {
  609. $found = true;
  610. $plugin_func = $this->_plugins['block'][$tag_command][0];
  611. if (!is_callable($plugin_func)) {
  612. $message = "block function '$tag_command' is not implemented";
  613. $have_function = false;
  614. }
  615. }
  616. /*
  617. * Otherwise we need to load plugin file and look for the function
  618. * inside it.
  619. */
  620. else if ($plugin_file = $this->_get_plugin_filepath('block', $tag_command)) {
  621. $found = true;
  622. include_once $plugin_file;
  623. $plugin_func = 'smarty_block_' . $tag_command;
  624. if (!function_exists($plugin_func)) {
  625. $message = "plugin function $plugin_func() not found in $plugin_file\n";
  626. $have_function = false;
  627. } else {
  628. $this->_plugins['block'][$tag_command] = array($plugin_func, null, null, null, true);
  629. }
  630. }
  631. if (!$found) {
  632. return false;
  633. } else if (!$have_function) {
  634. $this->_syntax_error($message, E_USER_WARNING, __FILE__, __LINE__);
  635. return true;
  636. }
  637. /*
  638. * Even though we've located the plugin function, compilation
  639. * happens only once, so the plugin will still need to be loaded
  640. * at runtime for future requests.
  641. */
  642. $this->_add_plugin('block', $tag_command);
  643. if ($start_tag)
  644. $this->_push_tag($tag_command);
  645. else
  646. $this->_pop_tag($tag_command);
  647. if ($start_tag) {
  648. $output = '<?php ' . $this->_push_cacheable_state('block', $tag_command);
  649. $attrs = $this->_parse_attrs($tag_args);
  650. $_cache_attrs='';
  651. $arg_list = $this->_compile_arg_list('block', $tag_command, $attrs, $_cache_attrs);
  652. $output .= "$_cache_attrs\$this->_tag_stack[] = array('$tag_command', array(".implode(',', $arg_list).')); ';
  653. $output .= '$_block_repeat=true;' . $this->_compile_plugin_call('block', $tag_command).'($this->_tag_stack[count($this->_tag_stack)-1][1], null, $this, $_block_repeat);';
  654. $output .= 'while ($_block_repeat) { ob_start(); ?>';
  655. } else {
  656. $output = '<?php $_block_content = ob_get_contents(); ob_end_clean(); ';
  657. $_out_tag_text = $this->_compile_plugin_call('block', $tag_command).'($this->_tag_stack[count($this->_tag_stack)-1][1], $_block_content, $this, $_block_repeat)';
  658. if ($tag_modifier != '') {
  659. $this->_parse_modifiers($_out_tag_text, $tag_modifier);
  660. }
  661. $output .= '$_block_repeat=false;echo ' . $_out_tag_text . '; } ';
  662. $output .= " array_pop(\$this->_tag_stack); " . $this->_pop_cacheable_state('block', $tag_command) . '?>';
  663. }
  664. return true;
  665. }
  666. /**
  667. * compile custom function tag
  668. *
  669. * @param string $tag_command
  670. * @param string $tag_args
  671. * @param string $tag_modifier
  672. * @return string
  673. */
  674. function _compile_custom_tag($tag_command, $tag_args, $tag_modifier, &$output)
  675. {
  676. $found = false;
  677. $have_function = true;
  678. /*
  679. * First we check if the custom function has already been registered
  680. * or loaded from a plugin file.
  681. */
  682. if (isset($this->_plugins['function'][$tag_command])) {
  683. $found = true;
  684. $plugin_func = $this->_plugins['function'][$tag_command][0];
  685. if (!is_callable($plugin_func)) {
  686. $message = "custom function '$tag_command' is not implemented";
  687. $have_function = false;
  688. }
  689. }
  690. /*
  691. * Otherwise we need to load plugin file and look for the function
  692. * inside it.
  693. */
  694. else if ($plugin_file = $this->_get_plugin_filepath('function', $tag_command)) {
  695. $found = true;
  696. include_once $plugin_file;
  697. $plugin_func = 'smarty_function_' . $tag_command;
  698. if (!function_exists($plugin_func)) {
  699. $message = "plugin function $plugin_func() not found in $plugin_file\n";
  700. $have_function = false;
  701. } else {
  702. $this->_plugins['function'][$tag_command] = array($plugin_func, null, null, null, true);
  703. }
  704. }
  705. if (!$found) {
  706. return false;
  707. } else if (!$have_function) {
  708. $this->_syntax_error($message, E_USER_WARNING, __FILE__, __LINE__);
  709. return true;
  710. }
  711. /* declare plugin to be loaded on display of the template that
  712. we compile right now */
  713. $this->_add_plugin('function', $tag_command);
  714. $_cacheable_state = $this->_push_cacheable_state('function', $tag_command);
  715. $attrs = $this->_parse_attrs($tag_args);
  716. $_cache_attrs = '';
  717. $arg_list = $this->_compile_arg_list('function', $tag_command, $attrs, $_cache_attrs);
  718. $output = $this->_compile_plugin_call('function', $tag_command).'(array('.implode(',', $arg_list)."), \$this)";
  719. if($tag_modifier != '') {
  720. $this->_parse_modifiers($output, $tag_modifier);
  721. }
  722. if($output != '') {
  723. $output = '<?php ' . $_cacheable_state . $_cache_attrs . 'echo ' . $output . ';'
  724. . $this->_pop_cacheable_state('function', $tag_command) . "?>" . $this->_additional_newline;
  725. }
  726. return true;
  727. }
  728. /**
  729. * compile a registered object tag
  730. *
  731. * @param string $tag_command
  732. * @param array $attrs
  733. * @param string $tag_modifier
  734. * @return string
  735. */
  736. function _compile_registered_object_tag($tag_command, $attrs, $tag_modifier)
  737. {
  738. if (substr($tag_command, 0, 1) == '/') {
  739. $start_tag = false;
  740. $tag_command = substr($tag_command, 1);
  741. } else {
  742. $start_tag = true;
  743. }
  744. list($object, $obj_comp) = explode('->', $tag_command);
  745. $arg_list = array();
  746. if(count($attrs)) {
  747. $_assign_var = false;
  748. foreach ($attrs as $arg_name => $arg_value) {
  749. if($arg_name == 'assign') {
  750. $_assign_var = $arg_value;
  751. unset($attrs['assign']);
  752. continue;
  753. }
  754. if (is_bool($arg_value))
  755. $arg_value = $arg_value ? 'true' : 'false';
  756. $arg_list[] = "'$arg_name' => $arg_value";
  757. }
  758. }
  759. if($this->_reg_objects[$object][2]) {
  760. // smarty object argument format
  761. $args = "array(".implode(',', (array)$arg_list)."), \$this";
  762. } else {
  763. // traditional argument format
  764. $args = implode(',', array_values($attrs));
  765. if (empty($args)) {
  766. $args = '';
  767. }
  768. }
  769. $prefix = '';
  770. $postfix = '';
  771. $newline = '';
  772. if(!is_object($this->_reg_objects[$object][0])) {
  773. $this->_trigger_fatal_error("registered '$object' is not an object" , $this->_current_file, $this->_current_line_no, __FILE__, __LINE__);
  774. } elseif(!empty($this->_reg_objects[$object][1]) && !in_array($obj_comp, $this->_reg_objects[$object][1])) {
  775. $this->_trigger_fatal_error("'$obj_comp' is not a registered component of object '$object'", $this->_current_file, $this->_current_line_no, __FILE__, __LINE__);
  776. } elseif(method_exists($this->_reg_objects[$object][0], $obj_comp)) {
  777. // method
  778. if(in_array($obj_comp, $this->_reg_objects[$object][3])) {
  779. // block method
  780. if ($start_tag) {
  781. $prefix = "\$this->_tag_stack[] = array('$obj_comp', $args); ";
  782. $prefix .= "\$_block_repeat=true; \$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], null, \$this, \$_block_repeat); ";
  783. $prefix .= "while (\$_block_repeat) { ob_start();";
  784. $return = null;
  785. $postfix = '';
  786. } else {
  787. $prefix = "\$_obj_block_content = ob_get_contents(); ob_end_clean(); \$_block_repeat=false;";
  788. $return = "\$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], \$_obj_block_content, \$this, \$_block_repeat)";
  789. $postfix = "} array_pop(\$this->_tag_stack);";
  790. }
  791. } else {
  792. // non-block method
  793. $return = "\$this->_reg_objects['$object'][0]->$obj_comp($args)";
  794. }
  795. } else {
  796. // property
  797. $return = "\$this->_reg_objects['$object'][0]->$obj_comp";
  798. }
  799. if($return != null) {
  800. if($tag_modifier != '') {
  801. $this->_parse_modifiers($return, $tag_modifier);
  802. }
  803. if(!empty($_assign_var)) {
  804. $output = "\$this->assign('" . $this->_dequote($_assign_var) ."', $return);";
  805. } else {
  806. $output = 'echo ' . $return . ';';
  807. $newline = $this->_additional_newline;
  808. }
  809. } else {
  810. $output = '';
  811. }
  812. return '<?php ' . $prefix . $output . $postfix . "?>" . $newline;
  813. }
  814. /**
  815. * Compile {insert ...} tag
  816. *
  817. * @param string $tag_args
  818. * @return string
  819. */
  820. function _compile_insert_tag($tag_args)
  821. {
  822. $attrs = $this->_parse_attrs($tag_args);
  823. $name = $this->_dequote($attrs['name']);
  824. if (empty($name)) {
  825. return $this->_syntax_error("missing insert name", E_USER_ERROR, __FILE__, __LINE__);
  826. }
  827. if (!preg_match('~^\w+$~', $name)) {
  828. return $this->_syntax_error("'insert: 'name' must be an insert function name", E_USER_ERROR, __FILE__, __LINE__);
  829. }
  830. if (!empty($attrs['script'])) {
  831. $delayed_loading = true;
  832. } else {
  833. $delayed_loading = false;
  834. }
  835. foreach ($attrs as $arg_name => $arg_value) {
  836. if (is_bool($arg_value))
  837. $arg_value = $arg_value ? 'true' : 'false';
  838. $arg_list[] = "'$arg_name' => $arg_value";
  839. }
  840. $this->_add_plugin('insert', $name, $delayed_loading);
  841. $_params = "array('args' => array(".implode(', ', (array)$arg_list)."))";
  842. return "<?php require_once(SMARTY_CORE_DIR . 'core.run_insert_handler.php');\necho smarty_core_run_insert_handler($_params, \$this); ?>" . $this->_additional_newline;
  843. }
  844. /**
  845. * Compile {include ...} tag
  846. *
  847. * @param string $tag_args
  848. * @return string
  849. */
  850. function _compile_include_tag($tag_args)
  851. {
  852. $attrs = $this->_parse_attrs($tag_args);
  853. $arg_list = array();
  854. if (empty($attrs['file'])) {
  855. $this->_syntax_error("missing 'file' attribute in include tag", E_USER_ERROR, __FILE__, __LINE__);
  856. }
  857. foreach ($attrs as $arg_name => $arg_value) {
  858. if ($arg_name == 'file') {
  859. $include_file = $arg_value;
  860. continue;
  861. } else if ($arg_name == 'assign') {
  862. $assign_var = $arg_value;
  863. continue;
  864. }
  865. if (is_bool($arg_value))
  866. $arg_value = $arg_value ? 'true' : 'false';
  867. $arg_list[] = "'$arg_name' => $arg_value";
  868. }
  869. $output = '<?php ';
  870. if (isset($assign_var)) {
  871. $output .= "ob_start();\n";
  872. }
  873. $output .=
  874. "\$_smarty_tpl_vars = \$this->_tpl_vars;\n";
  875. $_params = "array('smarty_include_tpl_file' => " . $include_file . ", 'smarty_include_vars' => array(".implode(',', (array)$arg_list)."))";
  876. $output .= "\$this->_smarty_include($_params);\n" .
  877. "\$this->_tpl_vars = \$_smarty_tpl_vars;\n" .
  878. "unset(\$_smarty_tpl_vars);\n";
  879. if (isset($assign_var)) {
  880. $output .= "\$this->assign(" . $assign_var . ", ob_get_contents()); ob_end_clean();\n";
  881. }
  882. $output .= ' ?>';
  883. return $output;
  884. }
  885. /**
  886. * Compile {include ...} tag
  887. *
  888. * @param string $tag_args
  889. * @return string
  890. */
  891. function _compile_include_php_tag($tag_args)
  892. {
  893. $attrs = $this->_parse_attrs($tag_args);
  894. if (empty($attrs['file'])) {
  895. $this->_syntax_error("missing 'file' attribute in include_php tag", E_USER_ERROR, __FILE__, __LINE__);
  896. }
  897. $assign_var = (empty($attrs['assign'])) ? '' : $this->_dequote($attrs['assign']);
  898. $once_var = (empty($attrs['once']) || $attrs['once']=='false') ? 'false' : 'true';
  899. $arg_list = array();
  900. foreach($attrs as $arg_name => $arg_value) {
  901. if($arg_name != 'file' AND $arg_name != 'once' AND $arg_name != 'assign') {
  902. if(is_bool($arg_value))
  903. $arg_value = $arg_value ? 'true' : 'false';
  904. $arg_list[] = "'$arg_name' => $arg_value";
  905. }
  906. }
  907. $_params = "array('smarty_file' => " . $attrs['file'] . ", 'smarty_assign' => '$assign_var', 'smarty_once' => $once_var, 'smarty_include_vars' => array(".implode(',', $arg_list)."))";
  908. return "<?php require_once(SMARTY_CORE_DIR . 'core.smarty_include_php.php');\nsmarty_core_smarty_include_php($_params, \$this); ?>" . $this->_additional_newline;
  909. }
  910. /**
  911. * Compile {section ...} tag
  912. *
  913. * @param string $tag_args
  914. * @return string
  915. */
  916. function _compile_section_start($tag_args)
  917. {
  918. $attrs = $this->_parse_attrs($tag_args);
  919. $arg_list = array();
  920. $output = '<?php ';
  921. $section_name = $attrs['name'];
  922. if (empty($section_name)) {
  923. $this->_syntax_error("missing section name", E_USER_ERROR, __FILE__, __LINE__);
  924. }
  925. $output .= "unset(\$this->_sections[$section_name]);\n";
  926. $section_props = "\$this->_sections[$section_name]";
  927. foreach ($attrs as $attr_name => $attr_value) {
  928. switch ($attr_name) {
  929. case 'loop':
  930. $output .= "{$section_props}['loop'] = is_array(\$_loop=$attr_value) ? count(\$_loop) : max(0, (int)\$_loop); unset(\$_loop);\n";
  931. break;
  932. case 'show':
  933. if (is_bool($attr_value))
  934. $show_attr_value = $attr_value ? 'true' : 'false';
  935. else
  936. $show_attr_value = "(bool)$attr_value";
  937. $output .= "{$section_props}['show'] = $show_attr_value;\n";
  938. break;
  939. case 'name':
  940. $output .= "{$section_props}['$attr_name'] = $attr_value;\n";
  941. break;
  942. case 'max':
  943. case 'start':
  944. $output .= "{$section_props}['$attr_name'] = (int)$attr_value;\n";
  945. break;
  946. case 'step':
  947. $output .= "{$section_props}['$attr_name'] = ((int)$attr_value) == 0 ? 1 : (int)$attr_value;\n";
  948. break;
  949. default:
  950. $this->_syntax_error("unknown section attribute - '$attr_name'", E_USER_ERROR, __FILE__, __LINE__);
  951. break;
  952. }
  953. }
  954. if (!isset($attrs['show']))
  955. $output .= "{$section_props}['show'] = true;\n";
  956. if (!isset($attrs['loop']))
  957. $output .= "{$section_props}['loop'] = 1;\n";
  958. if (!isset($attrs['max']))
  959. $output .= "{$section_props}['max'] = {$section_props}['loop'];\n";
  960. else
  961. $output .= "if ({$section_props}['max'] < 0)\n" .
  962. " {$section_props}['max'] = {$section_props}['loop'];\n";
  963. if (!isset($attrs['step']))
  964. $output .= "{$section_props}['step'] = 1;\n";
  965. if (!isset($attrs['start']))
  966. $output .= "{$section_props}['start'] = {$section_props}['step'] > 0 ? 0 : {$section_props}['loop']-1;\n";
  967. else {
  968. $output .= "if ({$section_props}['start'] < 0)\n" .
  969. " {$section_props}['start'] = max({$section_props}['step'] > 0 ? 0 : -1, {$section_props}['loop'] + {$section_props}['start']);\n" .
  970. "else\n" .
  971. " {$section_props}['start'] = min({$section_props}['start'], {$section_props}['step'] > 0 ? {$section_props}['loop'] : {$section_props}['loop']-1);\n";
  972. }
  973. $output .= "if ({$section_props}['show']) {\n";
  974. if (!isset($attrs['start']) && !isset($attrs['step']) && !isset($attrs['max'])) {
  975. $output .= " {$section_props}['total'] = {$section_props}['loop'];\n";
  976. } else {
  977. $output .= " {$section_props}['total'] = min(ceil(({$section_props}['step'] > 0 ? {$section_props}['loop'] - {$section_props}['start'] : {$section_props}['start']+1)/abs({$section_props}['step'])), {$section_props}['max']);\n";
  978. }
  979. $output .= " if ({$section_props}['total'] == 0)\n" .
  980. " {$section_props}['show'] = false;\n" .
  981. "} else\n" .
  982. " {$section_props}['total'] = 0;\n";
  983. $output .= "if ({$section_props}['show']):\n";
  984. $output .= "
  985. for ({$section_props}['index'] = {$section_props}['start'], {$section_props}['iteration'] = 1;
  986. {$section_props}['iteration'] <= {$section_props}['total'];
  987. {$section_props}['index'] += {$section_props}['step'], {$section_props}['iteration']++):\n";
  988. $output .= "{$section_props}['rownum'] = {$section_props}['iteration'];\n";
  989. $output .= "{$section_props}['index_prev'] = {$section_props}['index'] - {$section_props}['step'];\n";
  990. $output .= "{$section_props}['index_next'] = {$section_props}['index'] + {$section_props}['step'];\n";
  991. $output .= "{$section_props}['first'] = ({$section_props}['iteration'] == 1);\n";
  992. $output .= "{$section_props}['last'] = ({$section_props}['iteration'] == {$section_props}['total']);\n";
  993. $output .= "?>";
  994. return $output;
  995. }
  996. /**
  997. * Compile {foreach ...} tag.
  998. *
  999. * @param string $tag_args
  1000. * @return string
  1001. */
  1002. function _compile_foreach_start($tag_args)
  1003. {
  1004. $attrs = $this->_parse_attrs($tag_args);
  1005. $arg_list = array();
  1006. if (empty($attrs['from'])) {
  1007. return $this->_syntax_error("foreach: missing 'from' attribute", E_USER_ERROR, __FILE__, __LINE__);
  1008. }
  1009. $from = $attrs['from'];
  1010. if (empty($attrs['item'])) {
  1011. return $this->_syntax_error("foreach: missing 'item' attribute", E_USER_ERROR, __FILE__, __LINE__);
  1012. }
  1013. $item = $this->_dequote($attrs['item']);
  1014. if (!preg_match('~^\w+$~', $item)) {
  1015. return $this->_syntax_error("foreach: 'item' must be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
  1016. }
  1017. if (isset($attrs['key'])) {
  1018. $key = $this->_dequote($attrs['key']);
  1019. if (!preg_match('~^\w+$~', $key)) {
  1020. return $this->_syntax_error("foreach: 'key' must to be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
  1021. }
  1022. $key_part = "\$this->_tpl_vars['$key'] => ";
  1023. } else {
  1024. $key = null;
  1025. $key_part = '';
  1026. }
  1027. if (isset($attrs['name'])) {
  1028. $name = $attrs['name'];
  1029. } else {
  1030. $name = null;
  1031. }
  1032. $output = '<?php ';
  1033. $output .= "\$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array'); }";
  1034. if (isset($name)) {
  1035. $foreach_props = "\$this->_foreach[$name]";
  1036. $output .= "{$foreach_props} = array('total' => count(\$_from), 'iteration' => 0);\n";
  1037. $output .= "if ({$foreach_props}['total'] > 0):\n";
  1038. $output .= " …

Large files files are truncated, but you can click here to view the full file