PageRenderTime 67ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/smarty/libs/Smarty_Compiler.class.php

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

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