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

/php/extlib/smarty/libs/Smarty_Compiler.class.php

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

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