PageRenderTime 65ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/src/_includes/Smarty/Smarty_Compiler.class.php

https://bitbucket.org/dereckson/icedeck
PHP | 2316 lines | 1562 code | 284 blank | 470 comment | 362 complexity | 348841a029ab7a7e5f51ca3b2cd3b3b1 MD5 | raw file
Possible License(s): GPL-2.0

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.14
  24. * @copyright 2001-2005 New Digital Group, Inc.
  25. * @package Smarty
  26. */
  27. /* $Id: Smarty_Compiler.class.php,v 1.381 2006/05/25 14:46:18 boots Exp $ */
  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. // matches valid modifier syntax:
  150. // |foo
  151. // |@foo
  152. // |foo:"bar"
  153. // |foo:$bar
  154. // |foo:"bar":$foobar
  155. // |foo|bar
  156. // |foo:$foo->bar
  157. $this->_mod_regexp = '(?:\|@?\w+(?::(?:\w+|' . $this->_num_const_regexp . '|'
  158. . $this->_obj_call_regexp . '|' . $this->_avar_regexp . '|' . $this->_qstr_regexp .'))*)';
  159. // matches valid function name:
  160. // foo123
  161. // _foo_bar
  162. $this->_func_regexp = '[a-zA-Z_]\w*';
  163. // matches valid registered object:
  164. // foo->bar
  165. $this->_reg_obj_regexp = '[a-zA-Z_]\w*->[a-zA-Z_]\w*';
  166. // matches valid parameter values:
  167. // true
  168. // $foo
  169. // $foo|bar
  170. // #foo#
  171. // #foo#|bar
  172. // "text"
  173. // "text"|bar
  174. // $foo->bar
  175. $this->_param_regexp = '(?:\s*(?:' . $this->_obj_call_regexp . '|'
  176. . $this->_var_regexp . '|' . $this->_num_const_regexp . '|\w+)(?>' . $this->_mod_regexp . '*)\s*)';
  177. // matches valid parenthesised function parameters:
  178. //
  179. // "text"
  180. // $foo, $bar, "text"
  181. // $foo|bar, "foo"|bar, $foo->bar($foo)|bar
  182. $this->_parenth_param_regexp = '(?:\((?:\w+|'
  183. . $this->_param_regexp . '(?:\s*,\s*(?:(?:\w+|'
  184. . $this->_param_regexp . ')))*)?\))';
  185. // matches valid function call:
  186. // foo()
  187. // foo_bar($foo)
  188. // _foo_bar($foo,"bar")
  189. // foo123($foo,$foo->bar(),"foo")
  190. $this->_func_call_regexp = '(?:' . $this->_func_regexp . '\s*(?:'
  191. . $this->_parenth_param_regexp . '))';
  192. }
  193. /**
  194. * compile a resource
  195. *
  196. * sets $compiled_content to the compiled source
  197. * @param string $resource_name
  198. * @param string $source_content
  199. * @param string $compiled_content
  200. * @return true
  201. */
  202. function _compile_file($resource_name, $source_content, &$compiled_content)
  203. {
  204. if ($this->security) {
  205. // do not allow php syntax to be executed unless specified
  206. if ($this->php_handling == SMARTY_PHP_ALLOW &&
  207. !$this->security_settings['PHP_HANDLING']) {
  208. $this->php_handling = SMARTY_PHP_PASSTHRU;
  209. }
  210. }
  211. $this->_load_filters();
  212. $this->_current_file = $resource_name;
  213. $this->_current_line_no = 1;
  214. $ldq = preg_quote($this->left_delimiter, '~');
  215. $rdq = preg_quote($this->right_delimiter, '~');
  216. /* un-hide hidden xml open tags */
  217. $source_content = preg_replace("~<({$ldq}(.*?){$rdq})[?]~s", '< \\1', $source_content);
  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*[\"\']?php[\"\']?)~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. /* Interleave the compiled contents and text blocks to get the final result. */
  321. for ($i = 0, $for_max = count($compiled_tags); $i < $for_max; $i++) {
  322. if ($compiled_tags[$i] == '') {
  323. // tag result empty, remove first newline from following text block
  324. $text_blocks[$i+1] = preg_replace('~^(\r\n|\r|\n)~', '', $text_blocks[$i+1]);
  325. }
  326. $compiled_content .= $text_blocks[$i].$compiled_tags[$i];
  327. }
  328. $compiled_content .= $text_blocks[$i];
  329. // remove \n from the end of the file, if any
  330. if (strlen($compiled_content) && (substr($compiled_content, -1) == "\n") ) {
  331. $compiled_content = substr($compiled_content, 0, -1);
  332. }
  333. if (!empty($this->_cache_serial)) {
  334. $compiled_content = "<?php \$this->_cache_serials['".$this->_cache_include."'] = '".$this->_cache_serial."'; ?>" . $compiled_content;
  335. }
  336. // remove unnecessary close/open tags
  337. $compiled_content = preg_replace('~\?>\n?<\?php~', '', $compiled_content);
  338. // run compiled template through postfilter functions
  339. if (count($this->_plugins['postfilter']) > 0) {
  340. foreach ($this->_plugins['postfilter'] as $filter_name => $postfilter) {
  341. if ($postfilter === false) continue;
  342. if ($postfilter[3] || is_callable($postfilter[0])) {
  343. $compiled_content = call_user_func_array($postfilter[0],
  344. array($compiled_content, &$this));
  345. $this->_plugins['postfilter'][$filter_name][3] = true;
  346. } else {
  347. $this->_trigger_fatal_error("Smarty plugin error: postfilter '$filter_name' is not implemented");
  348. }
  349. }
  350. }
  351. // put header at the top of the compiled template
  352. $template_header = "<?php /* Smarty version ".$this->_version.", created on ".strftime("%Y-%m-%d %H:%M:%S")."\n";
  353. $template_header .= " compiled from ".strtr(urlencode($resource_name), array('%2F'=>'/', '%3A'=>':'))." */ ?>\n";
  354. /* Emit code to load needed plugins. */
  355. $this->_plugins_code = '';
  356. if (count($this->_plugin_info)) {
  357. $_plugins_params = "array('plugins' => array(";
  358. foreach ($this->_plugin_info as $plugin_type => $plugins) {
  359. foreach ($plugins as $plugin_name => $plugin_info) {
  360. $_plugins_params .= "array('$plugin_type', '$plugin_name', '" . strtr($plugin_info[0], array("'" => "\\'", "\\" => "\\\\")) . "', $plugin_info[1], ";
  361. $_plugins_params .= $plugin_info[2] ? 'true),' : 'false),';
  362. }
  363. }
  364. $_plugins_params .= '))';
  365. $plugins_code = "<?php require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');\nsmarty_core_load_plugins($_plugins_params, \$this); ?>\n";
  366. $template_header .= $plugins_code;
  367. $this->_plugin_info = array();
  368. $this->_plugins_code = $plugins_code;
  369. }
  370. if ($this->_init_smarty_vars) {
  371. $template_header .= "<?php require_once(SMARTY_CORE_DIR . 'core.assign_smarty_interface.php');\nsmarty_core_assign_smarty_interface(null, \$this); ?>\n";
  372. $this->_init_smarty_vars = false;
  373. }
  374. $compiled_content = $template_header . $compiled_content;
  375. return true;
  376. }
  377. /**
  378. * Compile a template tag
  379. *
  380. * @param string $template_tag
  381. * @return string
  382. */
  383. function _compile_tag($template_tag)
  384. {
  385. /* Matched comment. */
  386. if (substr($template_tag, 0, 1) == '*' && substr($template_tag, -1) == '*')
  387. return '';
  388. /* Split tag into two three parts: command, command modifiers and the arguments. */
  389. if(! preg_match('~^(?:(' . $this->_num_const_regexp . '|' . $this->_obj_call_regexp . '|' . $this->_var_regexp
  390. . '|\/?' . $this->_reg_obj_regexp . '|\/?' . $this->_func_regexp . ')(' . $this->_mod_regexp . '*))
  391. (?:\s+(.*))?$
  392. ~xs', $template_tag, $match)) {
  393. $this->_syntax_error("unrecognized tag: $template_tag", E_USER_ERROR, __FILE__, __LINE__);
  394. }
  395. $tag_command = $match[1];
  396. $tag_modifier = isset($match[2]) ? $match[2] : null;
  397. $tag_args = isset($match[3]) ? $match[3] : null;
  398. if (preg_match('~^' . $this->_num_const_regexp . '|' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '$~', $tag_command)) {
  399. /* tag name is a variable or object */
  400. $_return = $this->_parse_var_props($tag_command . $tag_modifier);
  401. return "<?php echo $_return; ?>" . $this->_additional_newline;
  402. }
  403. /* If the tag name is a registered object, we process it. */
  404. if (preg_match('~^\/?' . $this->_reg_obj_regexp . '$~', $tag_command)) {
  405. return $this->_compile_registered_object_tag($tag_command, $this->_parse_attrs($tag_args), $tag_modifier);
  406. }
  407. switch ($tag_command) {
  408. case 'include':
  409. return $this->_compile_include_tag($tag_args);
  410. case 'include_php':
  411. return $this->_compile_include_php_tag($tag_args);
  412. case 'if':
  413. $this->_push_tag('if');
  414. return $this->_compile_if_tag($tag_args);
  415. case 'else':
  416. list($_open_tag) = end($this->_tag_stack);
  417. if ($_open_tag != 'if' && $_open_tag != 'elseif')
  418. $this->_syntax_error('unexpected {else}', E_USER_ERROR, __FILE__, __LINE__);
  419. else
  420. $this->_push_tag('else');
  421. return '<?php else: ?>';
  422. case 'elseif':
  423. list($_open_tag) = end($this->_tag_stack);
  424. if ($_open_tag != 'if' && $_open_tag != 'elseif')
  425. $this->_syntax_error('unexpected {elseif}', E_USER_ERROR, __FILE__, __LINE__);
  426. if ($_open_tag == 'if')
  427. $this->_push_tag('elseif');
  428. return $this->_compile_if_tag($tag_args, true);
  429. case '/if':
  430. $this->_pop_tag('if');
  431. return '<?php endif; ?>';
  432. case 'capture':
  433. return $this->_compile_capture_tag(true, $tag_args);
  434. case '/capture':
  435. return $this->_compile_capture_tag(false);
  436. case 'ldelim':
  437. return $this->left_delimiter;
  438. case 'rdelim':
  439. return $this->right_delimiter;
  440. case 'section':
  441. $this->_push_tag('section');
  442. return $this->_compile_section_start($tag_args);
  443. case 'sectionelse':
  444. $this->_push_tag('sectionelse');
  445. return "<?php endfor; else: ?>";
  446. break;
  447. case '/section':
  448. $_open_tag = $this->_pop_tag('section');
  449. if ($_open_tag == 'sectionelse')
  450. return "<?php endif; ?>";
  451. else
  452. return "<?php endfor; endif; ?>";
  453. case 'foreach':
  454. $this->_push_tag('foreach');
  455. return $this->_compile_foreach_start($tag_args);
  456. break;
  457. case 'foreachelse':
  458. $this->_push_tag('foreachelse');
  459. return "<?php endforeach; else: ?>";
  460. case '/foreach':
  461. $_open_tag = $this->_pop_tag('foreach');
  462. if ($_open_tag == 'foreachelse')
  463. return "<?php endif; unset(\$_from); ?>";
  464. else
  465. return "<?php endforeach; endif; unset(\$_from); ?>";
  466. break;
  467. case 'strip':
  468. case '/strip':
  469. if (substr($tag_command, 0, 1)=='/') {
  470. $this->_pop_tag('strip');
  471. if (--$this->_strip_depth==0) { /* outermost closing {/strip} */
  472. $this->_additional_newline = "\n";
  473. return '{' . $tag_command . '}';
  474. }
  475. } else {
  476. $this->_push_tag('strip');
  477. if ($this->_strip_depth++==0) { /* outermost opening {strip} */
  478. $this->_additional_newline = "";
  479. return '{' . $tag_command . '}';
  480. }
  481. }
  482. return '';
  483. case 'php':
  484. /* handle folded tags replaced by {php} */
  485. list(, $block) = each($this->_folded_blocks);
  486. $this->_current_line_no += substr_count($block[0], "\n");
  487. /* the number of matched elements in the regexp in _compile_file()
  488. determins the type of folded tag that was found */
  489. switch (count($block)) {
  490. case 2: /* comment */
  491. return '';
  492. case 3: /* literal */
  493. return "<?php echo '" . strtr($block[2], array("'"=>"\'", "\\"=>"\\\\")) . "'; ?>" . $this->_additional_newline;
  494. case 4: /* php */
  495. if ($this->security && !$this->security_settings['PHP_TAGS']) {
  496. $this->_syntax_error("(secure mode) php tags not permitted", E_USER_WARNING, __FILE__, __LINE__);
  497. return;
  498. }
  499. return '<?php ' . $block[3] .' ?>';
  500. }
  501. break;
  502. case 'insert':
  503. return $this->_compile_insert_tag($tag_args);
  504. default:
  505. if ($this->_compile_compiler_tag($tag_command, $tag_args, $output)) {
  506. return $output;
  507. } else if ($this->_compile_block_tag($tag_command, $tag_args, $tag_modifier, $output)) {
  508. return $output;
  509. } else if ($this->_compile_custom_tag($tag_command, $tag_args, $tag_modifier, $output)) {
  510. return $output;
  511. } else {
  512. $this->_syntax_error("unrecognized tag '$tag_command'", E_USER_ERROR, __FILE__, __LINE__);
  513. }
  514. }
  515. }
  516. /**
  517. * compile the custom compiler tag
  518. *
  519. * sets $output to the compiled custom compiler tag
  520. * @param string $tag_command
  521. * @param string $tag_args
  522. * @param string $output
  523. * @return boolean
  524. */
  525. function _compile_compiler_tag($tag_command, $tag_args, &$output)
  526. {
  527. $found = false;
  528. $have_function = true;
  529. /*
  530. * First we check if the compiler function has already been registered
  531. * or loaded from a plugin file.
  532. */
  533. if (isset($this->_plugins['compiler'][$tag_command])) {
  534. $found = true;
  535. $plugin_func = $this->_plugins['compiler'][$tag_command][0];
  536. if (!is_callable($plugin_func)) {
  537. $message = "compiler function '$tag_command' is not implemented";
  538. $have_function = false;
  539. }
  540. }
  541. /*
  542. * Otherwise we need to load plugin file and look for the function
  543. * inside it.
  544. */
  545. else if ($plugin_file = $this->_get_plugin_filepath('compiler', $tag_command)) {
  546. $found = true;
  547. include_once $plugin_file;
  548. $plugin_func = 'smarty_compiler_' . $tag_command;
  549. if (!is_callable($plugin_func)) {
  550. $message = "plugin function $plugin_func() not found in $plugin_file\n";
  551. $have_function = false;
  552. } else {
  553. $this->_plugins['compiler'][$tag_command] = array($plugin_func, null, null, null, true);
  554. }
  555. }
  556. /*
  557. * True return value means that we either found a plugin or a
  558. * dynamically registered function. False means that we didn't and the
  559. * compiler should now emit code to load custom function plugin for this
  560. * tag.
  561. */
  562. if ($found) {
  563. if ($have_function) {
  564. $output = call_user_func_array($plugin_func, array($tag_args, &$this));
  565. if($output != '') {
  566. $output = '<?php ' . $this->_push_cacheable_state('compiler', $tag_command)
  567. . $output
  568. . $this->_pop_cacheable_state('compiler', $tag_command) . ' ?>';
  569. }
  570. } else {
  571. $this->_syntax_error($message, E_USER_WARNING, __FILE__, __LINE__);
  572. }
  573. return true;
  574. } else {
  575. return false;
  576. }
  577. }
  578. /**
  579. * compile block function tag
  580. *
  581. * sets $output to compiled block function tag
  582. * @param string $tag_command
  583. * @param string $tag_args
  584. * @param string $tag_modifier
  585. * @param string $output
  586. * @return boolean
  587. */
  588. function _compile_block_tag($tag_command, $tag_args, $tag_modifier, &$output)
  589. {
  590. if (substr($tag_command, 0, 1) == '/') {
  591. $start_tag = false;
  592. $tag_command = substr($tag_command, 1);
  593. } else
  594. $start_tag = true;
  595. $found = false;
  596. $have_function = true;
  597. /*
  598. * First we check if the block function has already been registered
  599. * or loaded from a plugin file.
  600. */
  601. if (isset($this->_plugins['block'][$tag_command])) {
  602. $found = true;
  603. $plugin_func = $this->_plugins['block'][$tag_command][0];
  604. if (!is_callable($plugin_func)) {
  605. $message = "block function '$tag_command' is not implemented";
  606. $have_function = false;
  607. }
  608. }
  609. /*
  610. * Otherwise we need to load plugin file and look for the function
  611. * inside it.
  612. */
  613. else if ($plugin_file = $this->_get_plugin_filepath('block', $tag_command)) {
  614. $found = true;
  615. include_once $plugin_file;
  616. $plugin_func = 'smarty_block_' . $tag_command;
  617. if (!function_exists($plugin_func)) {
  618. $message = "plugin function $plugin_func() not found in $plugin_file\n";
  619. $have_function = false;
  620. } else {
  621. $this->_plugins['block'][$tag_command] = array($plugin_func, null, null, null, true);
  622. }
  623. }
  624. if (!$found) {
  625. return false;
  626. } else if (!$have_function) {
  627. $this->_syntax_error($message, E_USER_WARNING, __FILE__, __LINE__);
  628. return true;
  629. }
  630. /*
  631. * Even though we've located the plugin function, compilation
  632. * happens only once, so the plugin will still need to be loaded
  633. * at runtime for future requests.
  634. */
  635. $this->_add_plugin('block', $tag_command);
  636. if ($start_tag)
  637. $this->_push_tag($tag_command);
  638. else
  639. $this->_pop_tag($tag_command);
  640. if ($start_tag) {
  641. $output = '<?php ' . $this->_push_cacheable_state('block', $tag_command);
  642. $attrs = $this->_parse_attrs($tag_args);
  643. $_cache_attrs='';
  644. $arg_list = $this->_compile_arg_list('block', $tag_command, $attrs, $_cache_attrs);
  645. $output .= "$_cache_attrs\$this->_tag_stack[] = array('$tag_command', array(".implode(',', $arg_list).')); ';
  646. $output .= '$_block_repeat=true;' . $this->_compile_plugin_call('block', $tag_command).'($this->_tag_stack[count($this->_tag_stack)-1][1], null, $this, $_block_repeat);';
  647. $output .= 'while ($_block_repeat) { ob_start(); ?>';
  648. } else {
  649. $output = '<?php $_block_content = ob_get_contents(); ob_end_clean(); ';
  650. $_out_tag_text = $this->_compile_plugin_call('block', $tag_command).'($this->_tag_stack[count($this->_tag_stack)-1][1], $_block_content, $this, $_block_repeat)';
  651. if ($tag_modifier != '') {
  652. $this->_parse_modifiers($_out_tag_text, $tag_modifier);
  653. }
  654. $output .= '$_block_repeat=false;echo ' . $_out_tag_text . '; } ';
  655. $output .= " array_pop(\$this->_tag_stack); " . $this->_pop_cacheable_state('block', $tag_command) . '?>';
  656. }
  657. return true;
  658. }
  659. /**
  660. * compile custom function tag
  661. *
  662. * @param string $tag_command
  663. * @param string $tag_args
  664. * @param string $tag_modifier
  665. * @return string
  666. */
  667. function _compile_custom_tag($tag_command, $tag_args, $tag_modifier, &$output)
  668. {
  669. $found = false;
  670. $have_function = true;
  671. /*
  672. * First we check if the custom function has already been registered
  673. * or loaded from a plugin file.
  674. */
  675. if (isset($this->_plugins['function'][$tag_command])) {
  676. $found = true;
  677. $plugin_func = $this->_plugins['function'][$tag_command][0];
  678. if (!is_callable($plugin_func)) {
  679. $message = "custom function '$tag_command' is not implemented";
  680. $have_function = false;
  681. }
  682. }
  683. /*
  684. * Otherwise we need to load plugin file and look for the function
  685. * inside it.
  686. */
  687. else if ($plugin_file = $this->_get_plugin_filepath('function', $tag_command)) {
  688. $found = true;
  689. include_once $plugin_file;
  690. $plugin_func = 'smarty_function_' . $tag_command;
  691. if (!function_exists($plugin_func)) {
  692. $message = "plugin function $plugin_func() not found in $plugin_file\n";
  693. $have_function = false;
  694. } else {
  695. $this->_plugins['function'][$tag_command] = array($plugin_func, null, null, null, true);
  696. }
  697. }
  698. if (!$found) {
  699. return false;
  700. } else if (!$have_function) {
  701. $this->_syntax_error($message, E_USER_WARNING, __FILE__, __LINE__);
  702. return true;
  703. }
  704. /* declare plugin to be loaded on display of the template that
  705. we compile right now */
  706. $this->_add_plugin('function', $tag_command);
  707. $_cacheable_state = $this->_push_cacheable_state('function', $tag_command);
  708. $attrs = $this->_parse_attrs($tag_args);
  709. $_cache_attrs = '';
  710. $arg_list = $this->_compile_arg_list('function', $tag_command, $attrs, $_cache_attrs);
  711. $output = $this->_compile_plugin_call('function', $tag_command).'(array('.implode(',', $arg_list)."), \$this)";
  712. if($tag_modifier != '') {
  713. $this->_parse_modifiers($output, $tag_modifier);
  714. }
  715. if($output != '') {
  716. $output = '<?php ' . $_cacheable_state . $_cache_attrs . 'echo ' . $output . ';'
  717. . $this->_pop_cacheable_state('function', $tag_command) . "?>" . $this->_additional_newline;
  718. }
  719. return true;
  720. }
  721. /**
  722. * compile a registered object tag
  723. *
  724. * @param string $tag_command
  725. * @param array $attrs
  726. * @param string $tag_modifier
  727. * @return string
  728. */
  729. function _compile_registered_object_tag($tag_command, $attrs, $tag_modifier)
  730. {
  731. if (substr($tag_command, 0, 1) == '/') {
  732. $start_tag = false;
  733. $tag_command = substr($tag_command, 1);
  734. } else {
  735. $start_tag = true;
  736. }
  737. list($object, $obj_comp) = explode('->', $tag_command);
  738. $arg_list = array();
  739. if(count($attrs)) {
  740. $_assign_var = false;
  741. foreach ($attrs as $arg_name => $arg_value) {
  742. if($arg_name == 'assign') {
  743. $_assign_var = $arg_value;
  744. unset($attrs['assign']);
  745. continue;
  746. }
  747. if (is_bool($arg_value))
  748. $arg_value = $arg_value ? 'true' : 'false';
  749. $arg_list[] = "'$arg_name' => $arg_value";
  750. }
  751. }
  752. if($this->_reg_objects[$object][2]) {
  753. // smarty object argument format
  754. $args = "array(".implode(',', (array)$arg_list)."), \$this";
  755. } else {
  756. // traditional argument format
  757. $args = implode(',', array_values($attrs));
  758. if (empty($args)) {
  759. $args = 'null';
  760. }
  761. }
  762. $prefix = '';
  763. $postfix = '';
  764. $newline = '';
  765. if(!is_object($this->_reg_objects[$object][0])) {
  766. $this->_trigger_fatal_error("registered '$object' is not an object" , $this->_current_file, $this->_current_line_no, __FILE__, __LINE__);
  767. } elseif(!empty($this->_reg_objects[$object][1]) && !in_array($obj_comp, $this->_reg_objects[$object][1])) {
  768. $this->_trigger_fatal_error("'$obj_comp' is not a registered component of object '$object'", $this->_current_file, $this->_current_line_no, __FILE__, __LINE__);
  769. } elseif(method_exists($this->_reg_objects[$object][0], $obj_comp)) {
  770. // method
  771. if(in_array($obj_comp, $this->_reg_objects[$object][3])) {
  772. // block method
  773. if ($start_tag) {
  774. $prefix = "\$this->_tag_stack[] = array('$obj_comp', $args); ";
  775. $prefix .= "\$_block_repeat=true; \$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], null, \$this, \$_block_repeat); ";
  776. $prefix .= "while (\$_block_repeat) { ob_start();";
  777. $return = null;
  778. $postfix = '';
  779. } else {
  780. $prefix = "\$_obj_block_content = ob_get_contents(); ob_end_clean(); \$_block_repeat=false;";
  781. $return = "\$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], \$_obj_block_content, \$this, \$_block_repeat)";
  782. $postfix = "} array_pop(\$this->_tag_stack);";
  783. }
  784. } else {
  785. // non-block method
  786. $return = "\$this->_reg_objects['$object'][0]->$obj_comp($args)";
  787. }
  788. } else {
  789. // property
  790. $return = "\$this->_reg_objects['$object'][0]->$obj_comp";
  791. }
  792. if($return != null) {
  793. if($tag_modifier != '') {
  794. $this->_parse_modifiers($return, $tag_modifier);
  795. }
  796. if(!empty($_assign_var)) {
  797. $output = "\$this->assign('" . $this->_dequote($_assign_var) ."', $return);";
  798. } else {
  799. $output = 'echo ' . $return . ';';
  800. $newline = $this->_additional_newline;
  801. }
  802. } else {
  803. $output = '';
  804. }
  805. return '<?php ' . $prefix . $output . $postfix . "?>" . $newline;
  806. }
  807. /**
  808. * Compile {insert ...} tag
  809. *
  810. * @param string $tag_args
  811. * @return string
  812. */
  813. function _compile_insert_tag($tag_args)
  814. {
  815. $attrs = $this->_parse_attrs($tag_args);
  816. $name = $this->_dequote($attrs['name']);
  817. if (empty($name)) {
  818. $this->_syntax_error("missing insert name", E_USER_ERROR, __FILE__, __LINE__);
  819. }
  820. if (!empty($attrs['script'])) {
  821. $delayed_loading = true;
  822. } else {
  823. $delayed_loading = false;
  824. }
  825. foreach ($attrs as $arg_name => $arg_value) {
  826. if (is_bool($arg_value))
  827. $arg_value = $arg_value ? 'true' : 'false';
  828. $arg_list[] = "'$arg_name' => $arg_value";
  829. }
  830. $this->_add_plugin('insert', $name, $delayed_loading);
  831. $_params = "array('args' => array(".implode(', ', (array)$arg_list)."))";
  832. return "<?php require_once(SMARTY_CORE_DIR . 'core.run_insert_handler.php');\necho smarty_core_run_insert_handler($_params, \$this); ?>" . $this->_additional_newline;
  833. }
  834. /**
  835. * Compile {include ...} tag
  836. *
  837. * @param string $tag_args
  838. * @return string
  839. */
  840. function _compile_include_tag($tag_args)
  841. {
  842. $attrs = $this->_parse_attrs($tag_args);
  843. $arg_list = array();
  844. if (empty($attrs['file'])) {
  845. $this->_syntax_error("missing 'file' attribute in include tag", E_USER_ERROR, __FILE__, __LINE__);
  846. }
  847. foreach ($attrs as $arg_name => $arg_value) {
  848. if ($arg_name == 'file') {
  849. $include_file = $arg_value;
  850. continue;
  851. } else if ($arg_name == 'assign') {
  852. $assign_var = $arg_value;
  853. continue;
  854. }
  855. if (is_bool($arg_value))
  856. $arg_value = $arg_value ? 'true' : 'false';
  857. $arg_list[] = "'$arg_name' => $arg_value";
  858. }
  859. $output = '<?php ';
  860. if (isset($assign_var)) {
  861. $output .= "ob_start();\n";
  862. }
  863. $output .=
  864. "\$_smarty_tpl_vars = \$this->_tpl_vars;\n";
  865. $_params = "array('smarty_include_tpl_file' => " . $include_file . ", 'smarty_include_vars' => array(".implode(',', (array)$arg_list)."))";
  866. $output .= "\$this->_smarty_include($_params);\n" .
  867. "\$this->_tpl_vars = \$_smarty_tpl_vars;\n" .
  868. "unset(\$_smarty_tpl_vars);\n";
  869. if (isset($assign_var)) {
  870. $output .= "\$this->assign(" . $assign_var . ", ob_get_contents()); ob_end_clean();\n";
  871. }
  872. $output .= ' ?>';
  873. return $output;
  874. }
  875. /**
  876. * Compile {include ...} tag
  877. *
  878. * @param string $tag_args
  879. * @return string
  880. */
  881. function _compile_include_php_tag($tag_args)
  882. {
  883. $attrs = $this->_parse_attrs($tag_args);
  884. if (empty($attrs['file'])) {
  885. $this->_syntax_error("missing 'file' attribute in include_php tag", E_USER_ERROR, __FILE__, __LINE__);
  886. }
  887. $assign_var = (empty($attrs['assign'])) ? '' : $this->_dequote($attrs['assign']);
  888. $once_var = (empty($attrs['once']) || $attrs['once']=='false') ? 'false' : 'true';
  889. $arg_list = array();
  890. foreach($attrs as $arg_name => $arg_value) {
  891. if($arg_name != 'file' AND $arg_name != 'once' AND $arg_name != 'assign') {
  892. if(is_bool($arg_value))
  893. $arg_value = $arg_value ? 'true' : 'false';
  894. $arg_list[] = "'$arg_name' => $arg_value";
  895. }
  896. }
  897. $_params = "array('smarty_file' => " . $attrs['file'] . ", 'smarty_assign' => '$assign_var', 'smarty_once' => $once_var, 'smarty_include_vars' => array(".implode(',', $arg_list)."))";
  898. return "<?php require_once(SMARTY_CORE_DIR . 'core.smarty_include_php.php');\nsmarty_core_smarty_include_php($_params, \$this); ?>" . $this->_additional_newline;
  899. }
  900. /**
  901. * Compile {section ...} tag
  902. *
  903. * @param string $tag_args
  904. * @return string
  905. */
  906. function _compile_section_start($tag_args)
  907. {
  908. $attrs = $this->_parse_attrs($tag_args);
  909. $arg_list = array();
  910. $output = '<?php ';
  911. $section_name = $attrs['name'];
  912. if (empty($section_name)) {
  913. $this->_syntax_error("missing section name", E_USER_ERROR, __FILE__, __LINE__);
  914. }
  915. $output .= "unset(\$this->_sections[$section_name]);\n";
  916. $section_props = "\$this->_sections[$section_name]";
  917. foreach ($attrs as $attr_name => $attr_value) {
  918. switch ($attr_name) {
  919. case 'loop':
  920. $output .= "{$section_props}['loop'] = is_array(\$_loop=$attr_value) ? count(\$_loop) : max(0, (int)\$_loop); unset(\$_loop);\n";
  921. break;
  922. case 'show':
  923. if (is_bool($attr_value))
  924. $show_attr_value = $attr_value ? 'true' : 'false';
  925. else
  926. $show_attr_value = "(bool)$attr_value";
  927. $output .= "{$section_props}['show'] = $show_attr_value;\n";
  928. break;
  929. case 'name':
  930. $output .= "{$section_props}['$attr_name'] = $attr_value;\n";
  931. break;
  932. case 'max':
  933. case 'start':
  934. $output .= "{$section_props}['$attr_name'] = (int)$attr_value;\n";
  935. break;
  936. case 'step':
  937. $output .= "{$section_props}['$attr_name'] = ((int)$attr_value) == 0 ? 1 : (int)$attr_value;\n";
  938. break;
  939. default:
  940. $this->_syntax_error("unknown section attribute - '$attr_name'", E_USER_ERROR, __FILE__, __LINE__);
  941. break;
  942. }
  943. }
  944. if (!isset($attrs['show']))
  945. $output .= "{$section_props}['show'] = true;\n";
  946. if (!isset($attrs['loop']))
  947. $output .= "{$section_props}['loop'] = 1;\n";
  948. if (!isset($attrs['max']))
  949. $output .= "{$section_props}['max'] = {$section_props}['loop'];\n";
  950. else
  951. $output .= "if ({$section_props}['max'] < 0)\n" .
  952. " {$section_props}['max'] = {$section_props}['loop'];\n";
  953. if (!isset($attrs['step']))
  954. $output .= "{$section_props}['step'] = 1;\n";
  955. if (!isset($attrs['start']))
  956. $output .= "{$section_props}['start'] = {$section_props}['step'] > 0 ? 0 : {$section_props}['loop']-1;\n";
  957. else {
  958. $output .= "if ({$section_props}['start'] < 0)\n" .
  959. " {$section_props}['start'] = max({$section_props}['step'] > 0 ? 0 : -1, {$section_props}['loop'] + {$section_props}['start']);\n" .
  960. "else\n" .
  961. " {$section_props}['start'] = min({$section_props}['start'], {$section_props}['step'] > 0 ? {$section_props}['loop'] : {$section_props}['loop']-1);\n";
  962. }
  963. $output .= "if ({$section_props}['show']) {\n";
  964. if (!isset($attrs['start']) && !isset($attrs['step']) && !isset($attrs['max'])) {
  965. $output .= " {$section_props}['total'] = {$section_props}['loop'];\n";
  966. } else {
  967. $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";
  968. }
  969. $output .= " if ({$section_props}['total'] == 0)\n" .
  970. " {$section_props}['show'] = false;\n" .
  971. "} else\n" .
  972. " {$section_props}['total'] = 0;\n";
  973. $output .= "if ({$section_props}['show']):\n";
  974. $output .= "
  975. for ({$section_props}['index'] = {$section_props}['start'], {$section_props}['iteration'] = 1;
  976. {$section_props}['iteration'] <= {$section_props}['total'];
  977. {$section_props}['index'] += {$section_props}['step'], {$section_props}['iteration']++):\n";
  978. $output .= "{$section_props}['rownum'] = {$section_props}['iteration'];\n";
  979. $output .= "{$section_props}['index_prev'] = {$section_props}['index'] - {$section_props}['step'];\n";
  980. $output .= "{$section_props}['index_next'] = {$section_props}['index'] + {$section_props}['step'];\n";
  981. $output .= "{$section_props}['first'] = ({$section_props}['iteration'] == 1);\n";
  982. $output .= "{$section_props}['last'] = ({$section_props}['iteration'] == {$section_props}['total']);\n";
  983. $output .= "?>";
  984. return $output;
  985. }
  986. /**
  987. * Compile {foreach ...} tag.
  988. *
  989. * @param string $tag_args
  990. * @return string
  991. */
  992. function _compile_foreach_start($tag_args)
  993. {
  994. $attrs = $this->_parse_attrs($tag_args);
  995. $arg_list = array();
  996. if (empty($attrs['from'])) {
  997. return $this->_syntax_error("foreach: missing 'from' attribute", E_USER_ERROR, __FILE__, __LINE__);
  998. }
  999. $from = $attrs['from'];
  1000. if (empty($attrs['item'])) {
  1001. return $this->_syntax_error("foreach: missing 'item' attribute", E_USER_ERROR, __FILE__, __LINE__);
  1002. }
  1003. $item = $this->_dequote($attrs['item']);
  1004. if (!preg_match('~^\w+$~', $item)) {
  1005. return $this->_syntax_error("'foreach: item' must be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
  1006. }
  1007. if (isset($attrs['key'])) {
  1008. $key = $this->_dequote($attrs['key']);
  1009. if (!preg_match('~^\w+$~', $key)) {
  1010. return $this->_syntax_error("foreach: 'key' must to be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
  1011. }
  1012. $key_part = "\$this->_tpl_vars['$key'] => ";
  1013. } else {
  1014. $key = null;
  1015. $key_part = '';
  1016. }
  1017. if (isset($attrs['name'])) {
  1018. $name = $attrs['name'];
  1019. } else {
  1020. $name = null;
  1021. }
  1022. $output = '<?php ';
  1023. $output .= "\$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array'); }";
  1024. if (isset($name)) {
  1025. $foreach_props = "\$this->_foreach[$name]";
  1026. $output .= "{$foreach_props} = array('total' => count(\$_from), 'iteration' => 0);\n";
  1027. $output .= "if ({$foreach_props}['total'] > 0):\n";
  1028. $output .= " foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
  1029. $output .= " {$foreach_props}['iteration']++;\n";
  1030. } else {
  1031. $output .= "if (count(\$_from)):\n";
  1032. $output .= " foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
  1033. }
  1034. $output .= '?>';
  1035. return $output;
  1036. }
  1037. /**
  1038. * Compile {capture} .. {/capture} tags
  1039. *
  1040. * @param boolean $start true if this is the {capture} tag
  1041. * @param string $tag_args
  1042. * @return string
  1043. */
  1044. function _compile_capture_tag($start, $tag_args = '')
  1045. {
  1046. $attrs = $this->_parse_attrs($tag_args);
  1047. if ($start) {
  1048. if (isset($attrs['name']))
  1049. $buffer = $attrs['name'];
  1050. else
  1051. $buffer = "'default'";
  1052. if (isset($attrs['assign']))
  1053. $assign = $attrs['assign'];
  1054. else

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