PageRenderTime 63ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/smarty/Smarty_Compiler.class.php

https://github.com/JackSpratt/silk
PHP | 2351 lines | 1861 code | 161 blank | 329 comment | 255 complexity | 72137324bfeea8b7039036992569e903 MD5 | raw file
Possible License(s): LGPL-2.1

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

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