PageRenderTime 70ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/library/smarty/Smarty_Compiler.class.php

https://github.com/mtodd/canvas
PHP | 2311 lines | 1559 code | 283 blank | 469 comment | 361 complexity | 440774ee9dbe1a34b4509c64c2d863a2 MD5 | raw 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.11
  24. * @copyright 2001-2005 New Digital Group, Inc.
  25. * @package Smarty
  26. */
  27. /* $Id: Smarty_Compiler.class.php,v 1.1.1.1 2006/04/26 20:43:08 bsimpson 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. $arg_list = $this->_compile_arg_list('block', $tag_command, $attrs, $_cache_attrs='');
  642. $output .= "$_cache_attrs\$this->_tag_stack[] = array('$tag_command', array(".implode(',', $arg_list).')); ';
  643. $output .= $this->_compile_plugin_call('block', $tag_command).'($this->_tag_stack[count($this->_tag_stack)-1][1], null, $this, $_block_repeat=true);';
  644. $output .= 'while ($_block_repeat) { ob_start(); ?>';
  645. } else {
  646. $output = '<?php $_block_content = ob_get_contents(); ob_end_clean(); ';
  647. $_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)';
  648. if ($tag_modifier != '') {
  649. $this->_parse_modifiers($_out_tag_text, $tag_modifier);
  650. }
  651. $output .= 'echo '.$_out_tag_text.'; } ';
  652. $output .= " array_pop(\$this->_tag_stack); " . $this->_pop_cacheable_state('block', $tag_command) . '?>';
  653. }
  654. return true;
  655. }
  656. /**
  657. * compile custom function tag
  658. *
  659. * @param string $tag_command
  660. * @param string $tag_args
  661. * @param string $tag_modifier
  662. * @return string
  663. */
  664. function _compile_custom_tag($tag_command, $tag_args, $tag_modifier, &$output)
  665. {
  666. $found = false;
  667. $have_function = true;
  668. /*
  669. * First we check if the custom function has already been registered
  670. * or loaded from a plugin file.
  671. */
  672. if (isset($this->_plugins['function'][$tag_command])) {
  673. $found = true;
  674. $plugin_func = $this->_plugins['function'][$tag_command][0];
  675. if (!is_callable($plugin_func)) {
  676. $message = "custom function '$tag_command' is not implemented";
  677. $have_function = false;
  678. }
  679. }
  680. /*
  681. * Otherwise we need to load plugin file and look for the function
  682. * inside it.
  683. */
  684. else if ($plugin_file = $this->_get_plugin_filepath('function', $tag_command)) {
  685. $found = true;
  686. include_once $plugin_file;
  687. $plugin_func = 'smarty_function_' . $tag_command;
  688. if (!function_exists($plugin_func)) {
  689. $message = "plugin function $plugin_func() not found in $plugin_file\n";
  690. $have_function = false;
  691. } else {
  692. $this->_plugins['function'][$tag_command] = array($plugin_func, null, null, null, true);
  693. }
  694. }
  695. if (!$found) {
  696. return false;
  697. } else if (!$have_function) {
  698. $this->_syntax_error($message, E_USER_WARNING, __FILE__, __LINE__);
  699. return true;
  700. }
  701. /* declare plugin to be loaded on display of the template that
  702. we compile right now */
  703. $this->_add_plugin('function', $tag_command);
  704. $_cacheable_state = $this->_push_cacheable_state('function', $tag_command);
  705. $attrs = $this->_parse_attrs($tag_args);
  706. $arg_list = $this->_compile_arg_list('function', $tag_command, $attrs, $_cache_attrs='');
  707. $output = $this->_compile_plugin_call('function', $tag_command).'(array('.implode(',', $arg_list)."), \$this)";
  708. if($tag_modifier != '') {
  709. $this->_parse_modifiers($output, $tag_modifier);
  710. }
  711. if($output != '') {
  712. $output = '<?php ' . $_cacheable_state . $_cache_attrs . 'echo ' . $output . ';'
  713. . $this->_pop_cacheable_state('function', $tag_command) . "?>" . $this->_additional_newline;
  714. }
  715. return true;
  716. }
  717. /**
  718. * compile a registered object tag
  719. *
  720. * @param string $tag_command
  721. * @param array $attrs
  722. * @param string $tag_modifier
  723. * @return string
  724. */
  725. function _compile_registered_object_tag($tag_command, $attrs, $tag_modifier)
  726. {
  727. if (substr($tag_command, 0, 1) == '/') {
  728. $start_tag = false;
  729. $tag_command = substr($tag_command, 1);
  730. } else {
  731. $start_tag = true;
  732. }
  733. list($object, $obj_comp) = explode('->', $tag_command);
  734. $arg_list = array();
  735. if(count($attrs)) {
  736. $_assign_var = false;
  737. foreach ($attrs as $arg_name => $arg_value) {
  738. if($arg_name == 'assign') {
  739. $_assign_var = $arg_value;
  740. unset($attrs['assign']);
  741. continue;
  742. }
  743. if (is_bool($arg_value))
  744. $arg_value = $arg_value ? 'true' : 'false';
  745. $arg_list[] = "'$arg_name' => $arg_value";
  746. }
  747. }
  748. if($this->_reg_objects[$object][2]) {
  749. // smarty object argument format
  750. $args = "array(".implode(',', (array)$arg_list)."), \$this";
  751. } else {
  752. // traditional argument format
  753. $args = implode(',', array_values($attrs));
  754. if (empty($args)) {
  755. $args = 'null';
  756. }
  757. }
  758. $prefix = '';
  759. $postfix = '';
  760. $newline = '';
  761. if(!is_object($this->_reg_objects[$object][0])) {
  762. $this->_trigger_fatal_error("registered '$object' is not an object" , $this->_current_file, $this->_current_line_no, __FILE__, __LINE__);
  763. } elseif(!empty($this->_reg_objects[$object][1]) && !in_array($obj_comp, $this->_reg_objects[$object][1])) {
  764. $this->_trigger_fatal_error("'$obj_comp' is not a registered component of object '$object'", $this->_current_file, $this->_current_line_no, __FILE__, __LINE__);
  765. } elseif(method_exists($this->_reg_objects[$object][0], $obj_comp)) {
  766. // method
  767. if(in_array($obj_comp, $this->_reg_objects[$object][3])) {
  768. // block method
  769. if ($start_tag) {
  770. $prefix = "\$this->_tag_stack[] = array('$obj_comp', $args); ";
  771. $prefix .= "\$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], null, \$this, \$_block_repeat=true); ";
  772. $prefix .= "while (\$_block_repeat) { ob_start();";
  773. $return = null;
  774. $postfix = '';
  775. } else {
  776. $prefix = "\$_obj_block_content = ob_get_contents(); ob_end_clean(); ";
  777. $return = "\$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], \$_obj_block_content, \$this, \$_block_repeat=false)";
  778. $postfix = "} array_pop(\$this->_tag_stack);";
  779. }
  780. } else {
  781. // non-block method
  782. $return = "\$this->_reg_objects['$object'][0]->$obj_comp($args)";
  783. }
  784. } else {
  785. // property
  786. $return = "\$this->_reg_objects['$object'][0]->$obj_comp";
  787. }
  788. if($return != null) {
  789. if($tag_modifier != '') {
  790. $this->_parse_modifiers($return, $tag_modifier);
  791. }
  792. if(!empty($_assign_var)) {
  793. $output = "\$this->assign('" . $this->_dequote($_assign_var) ."', $return);";
  794. } else {
  795. $output = 'echo ' . $return . ';';
  796. $newline = $this->_additional_newline;
  797. }
  798. } else {
  799. $output = '';
  800. }
  801. return '<?php ' . $prefix . $output . $postfix . "?>" . $newline;
  802. }
  803. /**
  804. * Compile {insert ...} tag
  805. *
  806. * @param string $tag_args
  807. * @return string
  808. */
  809. function _compile_insert_tag($tag_args)
  810. {
  811. $attrs = $this->_parse_attrs($tag_args);
  812. $name = $this->_dequote($attrs['name']);
  813. if (empty($name)) {
  814. $this->_syntax_error("missing insert name", E_USER_ERROR, __FILE__, __LINE__);
  815. }
  816. if (!empty($attrs['script'])) {
  817. $delayed_loading = true;
  818. } else {
  819. $delayed_loading = false;
  820. }
  821. foreach ($attrs as $arg_name => $arg_value) {
  822. if (is_bool($arg_value))
  823. $arg_value = $arg_value ? 'true' : 'false';
  824. $arg_list[] = "'$arg_name' => $arg_value";
  825. }
  826. $this->_add_plugin('insert', $name, $delayed_loading);
  827. $_params = "array('args' => array(".implode(', ', (array)$arg_list)."))";
  828. return "<?php require_once(SMARTY_CORE_DIR . 'core.run_insert_handler.php');\necho smarty_core_run_insert_handler($_params, \$this); ?>" . $this->_additional_newline;
  829. }
  830. /**
  831. * Compile {include ...} tag
  832. *
  833. * @param string $tag_args
  834. * @return string
  835. */
  836. function _compile_include_tag($tag_args)
  837. {
  838. $attrs = $this->_parse_attrs($tag_args);
  839. $arg_list = array();
  840. if (empty($attrs['file'])) {
  841. $this->_syntax_error("missing 'file' attribute in include tag", E_USER_ERROR, __FILE__, __LINE__);
  842. }
  843. foreach ($attrs as $arg_name => $arg_value) {
  844. if ($arg_name == 'file') {
  845. $include_file = $arg_value;
  846. continue;
  847. } else if ($arg_name == 'assign') {
  848. $assign_var = $arg_value;
  849. continue;
  850. }
  851. if (is_bool($arg_value))
  852. $arg_value = $arg_value ? 'true' : 'false';
  853. $arg_list[] = "'$arg_name' => $arg_value";
  854. }
  855. $output = '<?php ';
  856. if (isset($assign_var)) {
  857. $output .= "ob_start();\n";
  858. }
  859. $output .=
  860. "\$_smarty_tpl_vars = \$this->_tpl_vars;\n";
  861. $_params = "array('smarty_include_tpl_file' => " . $include_file . ", 'smarty_include_vars' => array(".implode(',', (array)$arg_list)."))";
  862. $output .= "\$this->_smarty_include($_params);\n" .
  863. "\$this->_tpl_vars = \$_smarty_tpl_vars;\n" .
  864. "unset(\$_smarty_tpl_vars);\n";
  865. if (isset($assign_var)) {
  866. $output .= "\$this->assign(" . $assign_var . ", ob_get_contents()); ob_end_clean();\n";
  867. }
  868. $output .= ' ?>';
  869. return $output;
  870. }
  871. /**
  872. * Compile {include ...} tag
  873. *
  874. * @param string $tag_args
  875. * @return string
  876. */
  877. function _compile_include_php_tag($tag_args)
  878. {
  879. $attrs = $this->_parse_attrs($tag_args);
  880. if (empty($attrs['file'])) {
  881. $this->_syntax_error("missing 'file' attribute in include_php tag", E_USER_ERROR, __FILE__, __LINE__);
  882. }
  883. $assign_var = (empty($attrs['assign'])) ? '' : $this->_dequote($attrs['assign']);
  884. $once_var = (empty($attrs['once']) || $attrs['once']=='false') ? 'false' : 'true';
  885. $arg_list = array();
  886. foreach($attrs as $arg_name => $arg_value) {
  887. if($arg_name != 'file' AND $arg_name != 'once' AND $arg_name != 'assign') {
  888. if(is_bool($arg_value))
  889. $arg_value = $arg_value ? 'true' : 'false';
  890. $arg_list[] = "'$arg_name' => $arg_value";
  891. }
  892. }
  893. $_params = "array('smarty_file' => " . $attrs['file'] . ", 'smarty_assign' => '$assign_var', 'smarty_once' => $once_var, 'smarty_include_vars' => array(".implode(',', $arg_list)."))";
  894. return "<?php require_once(SMARTY_CORE_DIR . 'core.smarty_include_php.php');\nsmarty_core_smarty_include_php($_params, \$this); ?>" . $this->_additional_newline;
  895. }
  896. /**
  897. * Compile {section ...} tag
  898. *
  899. * @param string $tag_args
  900. * @return string
  901. */
  902. function _compile_section_start($tag_args)
  903. {
  904. $attrs = $this->_parse_attrs($tag_args);
  905. $arg_list = array();
  906. $output = '<?php ';
  907. $section_name = $attrs['name'];
  908. if (empty($section_name)) {
  909. $this->_syntax_error("missing section name", E_USER_ERROR, __FILE__, __LINE__);
  910. }
  911. $output .= "unset(\$this->_sections[$section_name]);\n";
  912. $section_props = "\$this->_sections[$section_name]";
  913. foreach ($attrs as $attr_name => $attr_value) {
  914. switch ($attr_name) {
  915. case 'loop':
  916. $output .= "{$section_props}['loop'] = is_array(\$_loop=$attr_value) ? count(\$_loop) : max(0, (int)\$_loop); unset(\$_loop);\n";
  917. break;
  918. case 'show':
  919. if (is_bool($attr_value))
  920. $show_attr_value = $attr_value ? 'true' : 'false';
  921. else
  922. $show_attr_value = "(bool)$attr_value";
  923. $output .= "{$section_props}['show'] = $show_attr_value;\n";
  924. break;
  925. case 'name':
  926. $output .= "{$section_props}['$attr_name'] = $attr_value;\n";
  927. break;
  928. case 'max':
  929. case 'start':
  930. $output .= "{$section_props}['$attr_name'] = (int)$attr_value;\n";
  931. break;
  932. case 'step':
  933. $output .= "{$section_props}['$attr_name'] = ((int)$attr_value) == 0 ? 1 : (int)$attr_value;\n";
  934. break;
  935. default:
  936. $this->_syntax_error("unknown section attribute - '$attr_name'", E_USER_ERROR, __FILE__, __LINE__);
  937. break;
  938. }
  939. }
  940. if (!isset($attrs['show']))
  941. $output .= "{$section_props}['show'] = true;\n";
  942. if (!isset($attrs['loop']))
  943. $output .= "{$section_props}['loop'] = 1;\n";
  944. if (!isset($attrs['max']))
  945. $output .= "{$section_props}['max'] = {$section_props}['loop'];\n";
  946. else
  947. $output .= "if ({$section_props}['max'] < 0)\n" .
  948. " {$section_props}['max'] = {$section_props}['loop'];\n";
  949. if (!isset($attrs['step']))
  950. $output .= "{$section_props}['step'] = 1;\n";
  951. if (!isset($attrs['start']))
  952. $output .= "{$section_props}['start'] = {$section_props}['step'] > 0 ? 0 : {$section_props}['loop']-1;\n";
  953. else {
  954. $output .= "if ({$section_props}['start'] < 0)\n" .
  955. " {$section_props}['start'] = max({$section_props}['step'] > 0 ? 0 : -1, {$section_props}['loop'] + {$section_props}['start']);\n" .
  956. "else\n" .
  957. " {$section_props}['start'] = min({$section_props}['start'], {$section_props}['step'] > 0 ? {$section_props}['loop'] : {$section_props}['loop']-1);\n";
  958. }
  959. $output .= "if ({$section_props}['show']) {\n";
  960. if (!isset($attrs['start']) && !isset($attrs['step']) && !isset($attrs['max'])) {
  961. $output .= " {$section_props}['total'] = {$section_props}['loop'];\n";
  962. } else {
  963. $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";
  964. }
  965. $output .= " if ({$section_props}['total'] == 0)\n" .
  966. " {$section_props}['show'] = false;\n" .
  967. "} else\n" .
  968. " {$section_props}['total'] = 0;\n";
  969. $output .= "if ({$section_props}['show']):\n";
  970. $output .= "
  971. for ({$section_props}['index'] = {$section_props}['start'], {$section_props}['iteration'] = 1;
  972. {$section_props}['iteration'] <= {$section_props}['total'];
  973. {$section_props}['index'] += {$section_props}['step'], {$section_props}['iteration']++):\n";
  974. $output .= "{$section_props}['rownum'] = {$section_props}['iteration'];\n";
  975. $output .= "{$section_props}['index_prev'] = {$section_props}['index'] - {$section_props}['step'];\n";
  976. $output .= "{$section_props}['index_next'] = {$section_props}['index'] + {$section_props}['step'];\n";
  977. $output .= "{$section_props}['first'] = ({$section_props}['iteration'] == 1);\n";
  978. $output .= "{$section_props}['last'] = ({$section_props}['iteration'] == {$section_props}['total']);\n";
  979. $output .= "?>";
  980. return $output;
  981. }
  982. /**
  983. * Compile {foreach ...} tag.
  984. *
  985. * @param string $tag_args
  986. * @return string
  987. */
  988. function _compile_foreach_start($tag_args)
  989. {
  990. $attrs = $this->_parse_attrs($tag_args);
  991. $arg_list = array();
  992. if (empty($attrs['from'])) {
  993. return $this->_syntax_error("foreach: missing 'from' attribute", E_USER_ERROR, __FILE__, __LINE__);
  994. }
  995. $from = $attrs['from'];
  996. if (empty($attrs['item'])) {
  997. return $this->_syntax_error("foreach: missing 'item' attribute", E_USER_ERROR, __FILE__, __LINE__);
  998. }
  999. $item = $this->_dequote($attrs['item']);
  1000. if (!preg_match('~^\w+$~', $item)) {
  1001. return $this->_syntax_error("'foreach: item' must be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
  1002. }
  1003. if (isset($attrs['key'])) {
  1004. $key = $this->_dequote($attrs['key']);
  1005. if (!preg_match('~^\w+$~', $key)) {
  1006. return $this->_syntax_error("foreach: 'key' must to be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
  1007. }
  1008. $key_part = "\$this->_tpl_vars['$key'] => ";
  1009. } else {
  1010. $key = null;
  1011. $key_part = '';
  1012. }
  1013. if (isset($attrs['name'])) {
  1014. $name = $attrs['name'];
  1015. } else {
  1016. $name = null;
  1017. }
  1018. $output = '<?php ';
  1019. $output .= "\$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array'); }";
  1020. if (isset($name)) {
  1021. $foreach_props = "\$this->_foreach[$name]";
  1022. $output .= "{$foreach_props} = array('total' => count(\$_from), 'iteration' => 0);\n";
  1023. $output .= "if ({$foreach_props}['total'] > 0):\n";
  1024. $output .= " foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
  1025. $output .= " {$foreach_props}['iteration']++;\n";
  1026. } else {
  1027. $output .= "if (count(\$_from)):\n";
  1028. $output .= " foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
  1029. }
  1030. $output .= '?>';
  1031. return $output;
  1032. }
  1033. /**
  1034. * Compile {capture} .. {/capture} tags
  1035. *
  1036. * @param boolean $start true if this is the {capture} tag
  1037. * @param string $tag_args
  1038. * @return string
  1039. */
  1040. function _compile_capture_tag($start, $tag_args = '')
  1041. {
  1042. $attrs = $this->_parse_attrs($tag_args);
  1043. if ($start) {
  1044. if (isset($attrs['name']))
  1045. $buffer = $attrs['name'];
  1046. else
  1047. $buffer = "'default'";
  1048. if (isset($attrs['assign']))
  1049. $assign = $attrs['assign'];
  1050. else
  1051. $assign = null;
  1052. $output = "<?php ob_start(); ?>";
  1053. $this->_capture_stack[] = array($buffer, $assign);
  1054. } else {
  1055. list($buffer, $assign) = array_pop($this->_capture_stack);
  1056. $output = "<?php \$this->_smarty_vars['capture'][$buffer] = ob_get_contents(); ";
  1057. if (isset($assign)) {
  1058. $output .= " \$this->assign($assign, ob_get_contents());";
  1059. }
  1060. $output .= "ob_end_clean(); ?>";
  1061. }
  1062. return $output;
  1063. }
  1064. /**
  1065. * Compile {if ...} tag
  1066. *
  1067. * @param string $tag_args
  1068. * @param boolean $elseif if true, uses elseif instead of if
  1069. * @return string
  1070. */
  1071. function _compile_if_tag($tag_args, $elseif = false)
  1072. {
  1073. /* Tokenize args for 'if' tag. */
  1074. preg_match_all('~(?>
  1075. ' . $this->_obj_call_regexp . '(?:' . $this->_mod_regexp . '*)? | # valid object call
  1076. ' . $this->_var_regexp . '(?:' . $this->_mod_regexp . '*)? | # var or quoted string
  1077. \-?0[xX][0-9a-fA-F]+|\-?\d+(?:\.\d+)?|\.\d+|!==|===|==|!=|<>|<<|>>|<=|>=|\&\&|\|\||\(|\)|,|\!|\^|=|\&|\~|<|>|\||\%|\+|\-|\/|\*|\@ | # valid non-word token
  1078. \b\w+\b | # valid word token
  1079. \S+ # anything else
  1080. )~x', $tag_args, $match);
  1081. $tokens = $match[0];
  1082. if(empty($tokens)) {
  1083. $_error_msg .= $elseif ? "'elseif'" : "'if'";
  1084. $_error_msg .= ' statement requires arguments';
  1085. $this->_syntax_error($_error_msg, E_USER_ERROR, __FILE__, __LINE__);
  1086. }
  1087. // make sure we have balanced parenthesis
  1088. $token_count = array_count_values($tokens);
  1089. if(isset($token_count['(']) && $token_count['('] != $token_count[')']) {
  1090. $this->_syntax_error("unbalanced parenthesis in if statement", E_USER_ERROR, __FILE__, __LINE__);
  1091. }
  1092. $is_arg_stack = array();
  1093. for ($i = 0; $i < count($tokens); $i++) {
  1094. $token = &$tokens[$i];
  1095. switch (strtolower($token)) {
  1096. case '!':
  1097. case '%':
  1098. case '!==':
  1099. case '==':
  1100. case '===':
  1101. case '>':
  1102. case '<':
  1103. case '!=':
  1104. case '<>':
  1105. case '<<':
  1106. case '>>':
  1107. case '<=':
  1108. case '>=':
  1109. case '&&':
  1110. case '||':
  1111. case '|':
  1112. case '^':
  1113. case '&':
  1114. case '~':
  1115. case ')':
  1116. case ',':
  1117. case '+':
  1118. case '-':
  1119. case '*':
  1120. case '/':
  1121. case '@':
  1122. break;
  1123. case 'eq':
  1124. $token = '==';
  1125. break;
  1126. case 'ne':
  1127. case 'neq':
  1128. $token = '!=';
  1129. break;
  1130. case 'lt':
  1131. $token = '<';
  1132. break;
  1133. case 'le':
  1134. case 'lte':
  1135. $token = '<=';
  1136. break;
  1137. case 'gt':
  1138. $token = '>';
  1139. break;
  1140. case 'ge':
  1141. case 'gte':
  1142. $token = '>=';
  1143. break;
  1144. case 'and':
  1145. $token = '&&';
  1146. break;
  1147. case 'or':
  1148. $token = '||';
  1149. break;
  1150. case 'not':
  1151. $token = '!';
  1152. break;
  1153. case 'mod':
  1154. $token = '%';
  1155. break;
  1156. case '(':
  1157. array_push($is_arg_stack, $i);
  1158. break;
  1159. case 'is':
  1160. /* If last token was a ')', we operate on the parenthesized
  1161. expression. The start of the expression is on the stack.
  1162. Otherwise, we operate on the last encountered token. */
  1163. if ($tokens[$i-1] == ')')
  1164. $is_arg_start = array_pop($is_arg_stack);
  1165. else
  1166. $is_arg_start = $i-1;
  1167. /* Construct the argument for 'is' expression, so it knows
  1168. what to operate on. */
  1169. $is_arg = implode(' ', array_slice($tokens, $is_arg_start, $i - $is_arg_start));
  1170. /* Pass all tokens from next one until the end to the
  1171. 'is' expression parsing function. The function will
  1172. return modified tokens, where the first one is the result
  1173. of the 'is' expression and the rest are the tokens it
  1174. didn't touch. */
  1175. $new_tokens = $this->_parse_is_expr($is_arg, array_slice($tokens, $i+1));
  1176. /* Replace the old tokens with the new ones. */
  1177. array_splice($tokens, $is_arg_start, count($tokens), $new_tokens);
  1178. /* Adjust argument start so that it won't change from the
  1179. current position for the next iteration. */
  1180. $i = $is_arg_start;
  1181. break;
  1182. default:
  1183. if(preg_match('~^' . $this->_func_regexp . '$~', $token) ) {
  1184. // function call
  1185. if($this->security &&
  1186. !in_array($token, $this->security_settings['IF_FUNCS'])) {
  1187. $this->_syntax_error("(secure mode) '$token' not allowed in if statement", E_USER_ERROR, __FILE__, __LINE__);
  1188. }
  1189. } elseif(preg_match('~^' . $this->_var_regexp . '$~', $token) && isset($tokens[$i+1]) && $tokens[$i+1] == '(') {
  1190. // variable function call
  1191. $this->_syntax_error("variable function call '$token' not allowed in if statement", E_USER_ERROR, __FILE__, __LINE__);
  1192. } elseif(preg_match('~^' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '(?:' . $this->_mod_regexp . '*)$~', $token)) {
  1193. // object or variable
  1194. $token = $this->_parse_var_props($token);
  1195. } elseif(is_numeric($token)) {
  1196. // number, skip it
  1197. } else {
  1198. $this->_syntax_error("unidentified token '$token'", E_USER_ERROR, __FILE__, __LINE__);
  1199. }
  1200. break;
  1201. }
  1202. }
  1203. if ($elseif)
  1204. return '<?php elseif ('.implode(' ', $tokens).'): ?>';
  1205. else
  1206. return '<?php if ('.implode(' ', $tokens).'): ?>';
  1207. }
  1208. function _compile_arg_list($type, $name, $attrs, &$cache_code) {
  1209. $arg_list = array();
  1210. if (isset($type) && isset($name)
  1211. && isset($this->_plugins[$type])
  1212. && isset($this->_plugins[$type][$name])
  1213. && empty($this->_plugins[$type][$name][4])
  1214. && is_array($this->_plugins[$type][$name][5])
  1215. ) {
  1216. /* we have a list of parameters that should be cached */
  1217. $_cache_attrs = $this->_plugins[$type][$name][5];
  1218. $_count = $this->_cache_attrs_count++;
  1219. $cache_code = "\$_cache_attrs =& \$this->_smarty_cache_attrs('$this->_cache_serial','$_count');";
  1220. } else {
  1221. /* no parameters are cached */
  1222. $_cache_attrs = null;
  1223. }
  1224. foreach ($attrs as $arg_name => $arg_value) {
  1225. if (is_bool($arg_value))
  1226. $arg_value = $arg_value ? 'true' : 'false';
  1227. if (is_null($arg_value))
  1228. $arg_value = 'null';
  1229. if ($_cache_attrs && in_array($arg_name, $_cache_attrs)) {
  1230. $arg_list[] = "'$arg_name' => (\$this->_cache_including) ? \$_cache_attrs['$arg_name'] : (\$_cache_attrs['$arg_name']=$arg_value)";
  1231. } else {
  1232. $arg_list[] = "'$arg_name' => $arg_value";
  1233. }
  1234. }
  1235. return $arg_list;
  1236. }
  1237. /**
  1238. * Parse is expression
  1239. *
  1240. * @param string $is_arg
  1241. * @param array $tokens
  1242. * @return array
  1243. */
  1244. function _parse_is_expr($is_arg, $tokens)
  1245. {
  1246. $expr_end = 0;
  1247. $negate_expr = false;
  1248. if (($first_token = array_shift($tokens)) == 'not') {
  1249. $negate_expr = true;
  1250. $expr_type = array_shift($tokens);
  1251. } else
  1252. $expr_type = $first_token;
  1253. switch ($expr_type) {
  1254. case 'even':
  1255. if (isset($tokens[$expr_end]) && $tokens[$expr_end] == 'by') {
  1256. $expr_end++;
  1257. $expr_arg = $tokens[$expr_end++];
  1258. $expr = "!(1 & ($is_arg / " . $this->_parse_var_props($expr_arg) . "))";
  1259. } else
  1260. $expr = "!(1 & $is_arg)";
  1261. break;
  1262. case 'odd':
  1263. if (isset($tokens[$expr_end]) && $tokens[$expr_end] == 'by') {
  1264. $expr_end++;
  1265. $expr_arg = $tokens[$expr_end++];
  1266. $expr = "(1 & ($is_arg / " . $this->_parse_var_props($expr_arg) . "))";
  1267. } else
  1268. $expr = "(1 & $is_arg)";
  1269. break;
  1270. case 'div':
  1271. if (@$tokens[$expr_end] == 'by') {
  1272. $expr_end++;
  1273. $expr_arg = $tokens[$expr_end++];
  1274. $expr = "!($is_arg % " . $this->_parse_var_props($expr_arg) . ")";
  1275. } else {
  1276. $this->_syntax_error("expecting 'by' after 'div'", E_USER_ERROR, __FILE__, __LINE__);
  1277. }
  1278. break;
  1279. default:
  1280. $this->_syntax_error("unknown 'is' expression - '$expr_type'", E_USER_ERROR, __FILE__, __LINE__);
  1281. break;
  1282. }
  1283. if ($negate_expr) {
  1284. $expr = "!($expr)";
  1285. }
  1286. array_splice($tokens, 0, $expr_end, $expr);
  1287. return $tokens;
  1288. }
  1289. /**
  1290. * Parse attribute string
  1291. *
  1292. * @param string $tag_args
  1293. * @return array
  1294. */
  1295. function _parse_attrs($tag_args)
  1296. {
  1297. /* Tokenize tag attributes. */
  1298. preg_match_all('~(?:' . $this->_obj_call_regexp . '|' . $this->_qstr_regexp . ' | (?>[^"\'=\s]+)
  1299. )+ |
  1300. [=]
  1301. ~x', $tag_args, $match);
  1302. $tokens = $match[0];
  1303. $attrs = array();
  1304. /* Parse state:
  1305. 0 - expecting attribute name
  1306. 1 - expecting '='
  1307. 2 - expecting attribute value (not '=') */
  1308. $state = 0;
  1309. foreach ($tokens as $token) {
  1310. switch ($state) {
  1311. case 0:
  1312. /* If the token is a valid identifier, we set attribute name
  1313. and go to state 1. */
  1314. if (preg_match('~^\w+$~', $token)) {
  1315. $attr_name = $token;
  1316. $state = 1;
  1317. } else
  1318. $this->_syntax_error("invalid attribute name: '$token'", E_USER_ERROR, __FILE__, __LINE__);
  1319. break;
  1320. case 1:
  1321. /* If the token is '=', then we go to state 2. */
  1322. if ($token == '=') {
  1323. $state = 2;
  1324. } else
  1325. $this->_syntax_error("expecting '=' after attribute name '$last_token'", E_USER_ERROR, __FILE__, __LINE__);
  1326. break;
  1327. case 2:
  1328. /* If token is not '=', we set the attribute value and go to
  1329. state 0. */
  1330. if ($token != '=') {
  1331. /* We booleanize the token if it's a non-quoted possible
  1332. boolean value. */
  1333. if (preg_match('~^(on|yes|true)$~', $token)) {
  1334. $token = 'true';
  1335. } else if (preg_match('~^(off|no|false)$~', $token)) {
  1336. $token = 'false';
  1337. } else if ($token == 'null') {
  1338. $token = 'null';
  1339. } else if (preg_match('~^' . $this->_num_const_regexp . '|0[xX][0-9a-fA-F]+$~', $token)) {
  1340. /* treat integer literally */
  1341. } else if (!preg_match('~^' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '(?:' . $this->_mod_regexp . ')*$~', $token)) {
  1342. /* treat as a string, double-quote it escaping quotes */
  1343. $token = '"'.addslashes($token).'"';
  1344. }
  1345. $attrs[$attr_name] = $token;
  1346. $state = 0;
  1347. } else
  1348. $this->_syntax_error("'=' cannot be an attribute value", E_USER_ERROR, __FILE__, __LINE__);
  1349. break;
  1350. }
  1351. $last_token = $token;
  1352. }
  1353. if($state != 0) {
  1354. if($state == 1) {
  1355. $this->_syntax_error("expecting '=' after attribute name '$last_token'", E_USER_ERROR, __FILE__, __LINE__);
  1356. } else {
  1357. $this->_syntax_error("missing attribute value", E_USER_ERROR, __FILE__, __LINE__);
  1358. }
  1359. }
  1360. $this->_parse_vars_props($attrs);
  1361. return $attrs;
  1362. }
  1363. /**
  1364. * compile multiple variables and section properties tokens into
  1365. * PHP code
  1366. *
  1367. * @param array $tokens
  1368. */
  1369. function _parse_vars_props(&$tokens)
  1370. {
  1371. foreach($tokens as $key => $val) {
  1372. $tokens[$key] = $this->_parse_var_props($val);
  1373. }
  1374. }
  1375. /**
  1376. * compile single variable and section properties token into
  1377. * PHP code
  1378. *
  1379. * @param string $val
  1380. * @param string $tag_attrs
  1381. * @return string
  1382. */
  1383. function _parse_var_props($val)
  1384. {
  1385. $val = trim($val);
  1386. if(preg_match('~^(' . $this->_obj_call_regexp . '|' . $this->_dvar_regexp . ')(' . $this->_mod_regexp . '*)$~', $val, $match)) {
  1387. // $ variable or object
  1388. $return = $this->_parse_var($match[1]);
  1389. $modifiers = $match[2];
  1390. if (!empty($this->default_modifiers) && !preg_match('~(^|\|)smarty:nodefaults($|\|)~',$modifiers)) {
  1391. $_default_mod_string = implode('|',(array)$this->default_modifiers);
  1392. $modifiers = empty($modifiers) ? $_default_mod_string : $_default_mod_string . '|' . $modifiers;
  1393. }
  1394. $this->_parse_modifiers($return, $modifiers);
  1395. return $return;
  1396. } elseif (preg_match('~^' . $this->_db_qstr_regexp . '(?:' . $this->_mod_regexp . '*)$~', $val)) {
  1397. // double quoted text
  1398. preg_match('~^(' . $this->_db_qstr_regexp . ')('. $this->_mod_regexp . '*)$~', $val, $match);
  1399. $return = $this->_expand_quoted_text($match[1]);
  1400. if($match[2] != '') {
  1401. $this->_parse_modifiers($return, $match[2]);
  1402. }
  1403. return $return;
  1404. }
  1405. elseif(preg_match('~^' . $this->_num_const_regexp . '(?:' . $this->_mod_regexp . '*)$~', $val)) {
  1406. // numerical constant
  1407. preg_match('~^(' . $this->_num_const_regexp . ')('. $this->_mod_regexp . '*)$~', $val, $match);
  1408. if($match[2] != '') {
  1409. $this->_parse_modifiers($match[1], $match[2]);
  1410. return $match[1];
  1411. }
  1412. }
  1413. elseif(preg_match('~^' . $this->_si_qstr_regexp . '(?:' . $this->_mod_regexp . '*)$~', $val)) {
  1414. // single quoted text
  1415. preg_match('~^(' . $this->_si_qstr_regexp . ')('. $this->_mod_regexp . '*)$~', $val, $match);
  1416. if($match[2] != '') {
  1417. $this->_parse_modifiers($match[1], $match[2]);
  1418. return $match[1];
  1419. }
  1420. }
  1421. elseif(preg_match('~^' . $this->_cvar_regexp . '(?:' . $this->_mod_regexp . '*)$~', $val)) {
  1422. // config var
  1423. return $this->_parse_conf_var($val);
  1424. }
  1425. elseif(preg_match('~^' . $this->_svar_regexp . '(?:' . $this->_mod_regexp . '*)$~', $val)) {
  1426. // section var
  1427. return $this->_parse_section_prop($val);
  1428. }
  1429. elseif(!in_array($val, $this->_permitted_tokens) && !is_numeric($val)) {
  1430. // literal string
  1431. return $this->_expand_quoted_text('"' . strtr($val, array('\\' => '\\\\', '"' => '\\"')) .'"');
  1432. }
  1433. return $val;
  1434. }
  1435. /**
  1436. * expand quoted text with embedded variables
  1437. *
  1438. * @param string $var_expr
  1439. * @return string
  1440. */
  1441. function _expand_quoted_text($var_expr)
  1442. {
  1443. // if contains unescaped $, expand it
  1444. if(preg_match_all('~(?:\`(?<!\\\\)\$' . $this->_dvar_guts_regexp . '(?:' . $this->_obj_ext_regexp . ')*\`)|(?:(?<!\\\\)\$\w+(\[[a-zA-Z0-9]+\])*)~', $var_expr, $_match)) {
  1445. $_match = $_match[0];
  1446. rsort($_match);
  1447. reset($_match);
  1448. foreach($_match as $_var) {
  1449. $var_expr = str_replace ($_var, '".(' . $this->_parse_var(str_replace('`','',$_var)) . ')."', $var_expr);
  1450. }
  1451. $_return = preg_replace('~\.""|(?<!\\\\)""\.~', '', $var_expr);
  1452. } else {
  1453. $_return = $var_expr;
  1454. }
  1455. // replace double quoted literal string with single quotes
  1456. $_return = preg_replace('~^"([\s\w]+)"$~',"'\\1'",$_return);
  1457. return $_return;
  1458. }
  1459. /**
  1460. * parse variable expression into PHP code
  1461. *
  1462. * @param string $var_expr
  1463. * @param string $output
  1464. * @return string
  1465. */
  1466. function _parse_var($var_expr)
  1467. {
  1468. $_has_math = false;
  1469. $_math_vars = preg_split('~('.$this->_dvar_math_regexp.'|'.$this->_qstr_regexp.')~', $var_expr, -1, PREG_SPLIT_DELIM_CAPTURE);
  1470. if(count($_math_vars) > 1) {
  1471. $_first_var = "";
  1472. $_complete_var = "";
  1473. $_output = "";
  1474. // simple check if there is any math, to stop recursion (due to modifiers with "xx % yy" as parameter)
  1475. foreach($_math_vars as $_k => $_math_var) {
  1476. $_math_var = $_math_vars[$_k];
  1477. if(!empty($_math_var) || is_numeric($_math_var)) {
  1478. // hit a math operator, so process the stuff which came before it
  1479. if(preg_match('~^' . $this->_dvar_math_regexp . '$~', $_math_var)) {
  1480. $_has_math = true;
  1481. if(!empty($_complete_var) || is_numeric($_complete_var)) {
  1482. $_output .= $this->_parse_var($_complete_var);
  1483. }
  1484. // just output the math operator to php
  1485. $_output .= $_math_var;
  1486. if(empty($_first_var))
  1487. $_first_var = $_complete_var;
  1488. $_complete_var = "";
  1489. } else {
  1490. $_complete_var .= $_math_var;
  1491. }
  1492. }
  1493. }
  1494. if($_has_math) {
  1495. if(!empty($_complete_var) || is_numeric($_complete_var))
  1496. $_output .= $this->_parse_var($_complete_var);
  1497. // get the modifiers working (only the last var from math + modifier is left)
  1498. $var_expr = $_complete_var;
  1499. }
  1500. }
  1501. // prevent cutting of first digit in the number (we _definitly_ got a number if the first char is a digit)
  1502. if(is_numeric(substr($var_expr, 0, 1)))
  1503. $_var_ref = $var_expr;
  1504. else
  1505. $_var_ref = substr($var_expr, 1);
  1506. if(!$_has_math) {
  1507. // get [foo] and .foo and ->foo and (...) pieces
  1508. preg_match_all('~(?:^\w+)|' . $this->_obj_params_regexp . '|(?:' . $this->_var_bracket_regexp . ')|->\$?\w+|\.\$?\w+|\S+~', $_var_ref, $match);
  1509. $_indexes = $match[0];
  1510. $_var_name = array_shift($_indexes);
  1511. /* Handle $smarty.* variable references as a special case. */
  1512. if ($_var_name == 'smarty') {
  1513. /*
  1514. * If the reference could be compiled, use the compiled output;
  1515. * otherwise, fall back on the $smarty variable generated at
  1516. * run-time.
  1517. */
  1518. if (($smarty_ref = $this->_compile_smarty_ref($_indexes)) !== null) {
  1519. $_output = $smarty_ref;
  1520. } else {
  1521. $_var_name = substr(array_shift($_indexes), 1);
  1522. $_output = "\$this->_smarty_vars['$_var_name']";
  1523. }
  1524. } elseif(is_numeric($_var_name) && is_numeric(substr($var_expr, 0, 1))) {
  1525. // because . is the operator for accessing arrays thru inidizes we need to put it together again for floating point numbers
  1526. if(count($_indexes) > 0)
  1527. {
  1528. $_var_name .= implode("", $_indexes);
  1529. $_indexes = array();
  1530. }
  1531. $_output = $_var_name;
  1532. } else {
  1533. $_output = "\$this->_tpl_vars['$_var_name']";
  1534. }
  1535. foreach ($_indexes as $_index) {
  1536. if (substr($_index, 0, 1) == '[') {
  1537. $_index = substr($_index, 1, -1);
  1538. if (is_numeric($_index)) {
  1539. $_output .= "[$_index]";
  1540. } elseif (substr($_index, 0, 1) == '$') {
  1541. if (strpos($_index, '.') !== false) {
  1542. $_output .= '[' . $this->_parse_var($_index) . ']';
  1543. } else {
  1544. $_output .= "[\$this->_tpl_vars['" . substr($_index, 1) . "']]";
  1545. }
  1546. } else {
  1547. $_var_parts = explode('.', $_index);
  1548. $_var_section = $_var_parts[0];
  1549. $_var_section_prop = isset($_var_parts[1]) ? $_var_parts[1] : 'index';
  1550. $_output .= "[\$this->_sections['$_var_section']['$_var_section_prop']]";
  1551. }
  1552. } else if (substr($_index, 0, 1) == '.') {
  1553. if (substr($_index, 1, 1) == '$')
  1554. $_output .= "[\$this->_tpl_vars['" . substr($_index, 2) . "']]";
  1555. else
  1556. $_output .= "['" . substr($_index, 1) . "']";
  1557. } else if (substr($_index,0,2) == '->') {
  1558. if(substr($_index,2,2) == '__') {
  1559. $this->_syntax_error('call to internal object members is not allowed', E_USER_ERROR, __FILE__, __LINE__);
  1560. } elseif($this->security && substr($_index, 2, 1) == '_') {
  1561. $this->_syntax_error('(secure) call to private object member is not allowed', E_USER_ERROR, __FILE__, __LINE__);
  1562. } elseif (substr($_index, 2, 1) == '$') {
  1563. if ($this->security) {
  1564. $this->_syntax_error('(secure) call to dynamic object member is not allowed', E_USER_ERROR, __FILE__, __LINE__);
  1565. } else {
  1566. $_output .= '->{(($_var=$this->_tpl_vars[\''.substr($_index,3).'\']) && substr($_var,0,2)!=\'__\') ? $_var : $this->trigger_error("cannot access property \\"$_var\\"")}';
  1567. }
  1568. } else {
  1569. $_output .= $_index;
  1570. }
  1571. } elseif (substr($_index, 0, 1) == '(') {
  1572. $_index = $this->_parse_parenth_args($_index);
  1573. $_output .= $_index;
  1574. } else {
  1575. $_output .= $_index;
  1576. }
  1577. }
  1578. }
  1579. return $_output;
  1580. }
  1581. /**
  1582. * parse arguments in function call parenthesis
  1583. *
  1584. * @param string $parenth_args
  1585. * @return string
  1586. */
  1587. function _parse_parenth_args($parenth_args)
  1588. {
  1589. preg_match_all('~' . $this->_param_regexp . '~',$parenth_args, $match);
  1590. $orig_vals = $match = $match[0];
  1591. $this->_parse_vars_props($match);
  1592. $replace = array();
  1593. for ($i = 0, $count = count($match); $i < $count; $i++) {
  1594. $replace[$orig_vals[$i]] = $match[$i];
  1595. }
  1596. return strtr($parenth_args, $replace);
  1597. }
  1598. /**
  1599. * parse configuration variable expression into PHP code
  1600. *
  1601. * @param string $conf_var_expr
  1602. */
  1603. function _parse_conf_var($conf_var_expr)
  1604. {
  1605. $parts = explode('|', $conf_var_expr, 2);
  1606. $var_ref = $parts[0];
  1607. $modifiers = isset($parts[1]) ? $parts[1] : '';
  1608. $var_name = substr($var_ref, 1, -1);
  1609. $output = "\$this->_config[0]['vars']['$var_name']";
  1610. $this->_parse_modifiers($output, $modifiers);
  1611. return $output;
  1612. }
  1613. /**
  1614. * parse section property expression into PHP code
  1615. *
  1616. * @param string $section_prop_expr
  1617. * @return string
  1618. */
  1619. function _parse_section_prop($section_prop_expr)
  1620. {
  1621. $parts = explode('|', $section_prop_expr, 2);
  1622. $var_ref = $parts[0];
  1623. $modifiers = isset($parts[1]) ? $parts[1] : '';
  1624. preg_match('!%(\w+)\.(\w+)%!', $var_ref, $match);
  1625. $section_name = $match[1];
  1626. $prop_name = $match[2];
  1627. $output = "\$this->_sections['$section_name']['$prop_name']";
  1628. $this->_parse_modifiers($output, $modifiers);
  1629. return $output;
  1630. }
  1631. /**
  1632. * parse modifier chain into PHP code
  1633. *
  1634. * sets $output to parsed modified chain
  1635. * @param string $output
  1636. * @param string $modifier_string
  1637. */
  1638. function _parse_modifiers(&$output, $modifier_string)
  1639. {
  1640. preg_match_all('~\|(@?\w+)((?>:(?:'. $this->_qstr_regexp . '|[^|]+))*)~', '|' . $modifier_string, $_match);
  1641. list(, $_modifiers, $modifier_arg_strings) = $_match;
  1642. for ($_i = 0, $_for_max = count($_modifiers); $_i < $_for_max; $_i++) {
  1643. $_modifier_name = $_modifiers[$_i];
  1644. if($_modifier_name == 'smarty') {
  1645. // skip smarty modifier
  1646. continue;
  1647. }
  1648. preg_match_all('~:(' . $this->_qstr_regexp . '|[^:]+)~', $modifier_arg_strings[$_i], $_match);
  1649. $_modifier_args = $_match[1];
  1650. if (substr($_modifier_name, 0, 1) == '@') {
  1651. $_map_array = false;
  1652. $_modifier_name = substr($_modifier_name, 1);
  1653. } else {
  1654. $_map_array = true;
  1655. }
  1656. if (empty($this->_plugins['modifier'][$_modifier_name])
  1657. && !$this->_get_plugin_filepath('modifier', $_modifier_name)
  1658. && function_exists($_modifier_name)) {
  1659. if ($this->security && !in_array($_modifier_name, $this->security_settings['MODIFIER_FUNCS'])) {
  1660. $this->_trigger_fatal_error("[plugin] (secure mode) modifier '$_modifier_name' is not allowed" , $this->_current_file, $this->_current_line_no, __FILE__, __LINE__);
  1661. } else {
  1662. $this->_plugins['modifier'][$_modifier_name] = array($_modifier_name, null, null, false);
  1663. }
  1664. }
  1665. $this->_add_plugin('modifier', $_modifier_name);
  1666. $this->_parse_vars_props($_modifier_args);
  1667. if($_modifier_name == 'default') {
  1668. // supress notifications of default modifier vars and args
  1669. if(substr($output, 0, 1) == '$') {
  1670. $output = '@' . $output;
  1671. }
  1672. if(isset($_modifier_args[0]) && substr($_modifier_args[0], 0, 1) == '$') {
  1673. $_modifier_args[0] = '@' . $_modifier_args[0];
  1674. }
  1675. }
  1676. if (count($_modifier_args) > 0)
  1677. $_modifier_args = ', '.implode(', ', $_modifier_args);
  1678. else
  1679. $_modifier_args = '';
  1680. if ($_map_array) {
  1681. $output = "((is_array(\$_tmp=$output)) ? \$this->_run_mod_handler('$_modifier_name', true, \$_tmp$_modifier_args) : " . $this->_compile_plugin_call('modifier', $_modifier_name) . "(\$_tmp$_modifier_args))";
  1682. } else {
  1683. $output = $this->_compile_plugin_call('modifier', $_modifier_name)."($output$_modifier_args)";
  1684. }
  1685. }
  1686. }
  1687. /**
  1688. * add plugin
  1689. *
  1690. * @param string $type
  1691. * @param string $name
  1692. * @param boolean? $delayed_loading
  1693. */
  1694. function _add_plugin($type, $name, $delayed_loading = null)
  1695. {
  1696. if (!isset($this->_plugin_info[$type])) {
  1697. $this->_plugin_info[$type] = array();
  1698. }
  1699. if (!isset($this->_plugin_info[$type][$name])) {
  1700. $this->_plugin_info[$type][$name] = array($this->_current_file,
  1701. $this->_current_line_no,
  1702. $delayed_loading);
  1703. }
  1704. }
  1705. /**
  1706. * Compiles references of type $smarty.foo
  1707. *
  1708. * @param string $indexes
  1709. * @return string
  1710. */
  1711. function _compile_smarty_ref(&$indexes)
  1712. {
  1713. /* Extract the reference name. */
  1714. $_ref = substr($indexes[0], 1);
  1715. foreach($indexes as $_index_no=>$_index) {
  1716. if (substr($_index, 0, 1) != '.' && $_index_no<2 || !preg_match('~^(\.|\[|->)~', $_index)) {
  1717. $this->_syntax_error('$smarty' . implode('', array_slice($indexes, 0, 2)) . ' is an invalid reference', E_USER_ERROR, __FILE__, __LINE__);
  1718. }
  1719. }
  1720. switch ($_ref) {
  1721. case 'now':
  1722. $compiled_ref = 'time()';
  1723. $_max_index = 1;
  1724. break;
  1725. case 'foreach':
  1726. array_shift($indexes);
  1727. $_var = $this->_parse_var_props(substr($indexes[0], 1));
  1728. $_propname = substr($indexes[1], 1);
  1729. $_max_index = 1;
  1730. switch ($_propname) {
  1731. case 'index':
  1732. array_shift($indexes);
  1733. $compiled_ref = "(\$this->_foreach[$_var]['iteration']-1)";
  1734. break;
  1735. case 'first':
  1736. array_shift($indexes);
  1737. $compiled_ref = "(\$this->_foreach[$_var]['iteration'] <= 1)";
  1738. break;
  1739. case 'last':
  1740. array_shift($indexes);
  1741. $compiled_ref = "(\$this->_foreach[$_var]['iteration'] == \$this->_foreach[$_var]['total'])";
  1742. break;
  1743. case 'show':
  1744. array_shift($indexes);
  1745. $compiled_ref = "(\$this->_foreach[$_var]['total'] > 0)";
  1746. break;
  1747. default:
  1748. unset($_max_index);
  1749. $compiled_ref = "\$this->_foreach[$_var]";
  1750. }
  1751. break;
  1752. case 'section':
  1753. array_shift($indexes);
  1754. $_var = $this->_parse_var_props(substr($indexes[0], 1));
  1755. $compiled_ref = "\$this->_sections[$_var]";
  1756. break;
  1757. case 'get':
  1758. $compiled_ref = ($this->request_use_auto_globals) ? '$_GET' : "\$GLOBALS['HTTP_GET_VARS']";
  1759. break;
  1760. case 'post':
  1761. $compiled_ref = ($this->request_use_auto_globals) ? '$_POST' : "\$GLOBALS['HTTP_POST_VARS']";
  1762. break;
  1763. case 'cookies':
  1764. $compiled_ref = ($this->request_use_auto_globals) ? '$_COOKIE' : "\$GLOBALS['HTTP_COOKIE_VARS']";
  1765. break;
  1766. case 'env':
  1767. $compiled_ref = ($this->request_use_auto_globals) ? '$_ENV' : "\$GLOBALS['HTTP_ENV_VARS']";
  1768. break;
  1769. case 'server':
  1770. $compiled_ref = ($this->request_use_auto_globals) ? '$_SERVER' : "\$GLOBALS['HTTP_SERVER_VARS']";
  1771. break;
  1772. case 'session':
  1773. $compiled_ref = ($this->request_use_auto_globals) ? '$_SESSION' : "\$GLOBALS['HTTP_SESSION_VARS']";
  1774. break;
  1775. /*
  1776. * These cases are handled either at run-time or elsewhere in the
  1777. * compiler.
  1778. */
  1779. case 'request':
  1780. if ($this->request_use_auto_globals) {
  1781. $compiled_ref = '$_REQUEST';
  1782. break;
  1783. } else {
  1784. $this->_init_smarty_vars = true;
  1785. }
  1786. return null;
  1787. case 'capture':
  1788. return null;
  1789. case 'template':
  1790. $compiled_ref = "'$this->_current_file'";
  1791. $_max_index = 1;
  1792. break;
  1793. case 'version':
  1794. $compiled_ref = "'$this->_version'";
  1795. $_max_index = 1;
  1796. break;
  1797. case 'const':
  1798. if ($this->security && !$this->security_settings['ALLOW_CONSTANTS']) {
  1799. $this->_syntax_error("(secure mode) constants not permitted",
  1800. E_USER_WARNING, __FILE__, __LINE__);
  1801. return;
  1802. }
  1803. array_shift($indexes);
  1804. if (preg_match('!^\.\w+$!', $indexes[0])) {
  1805. $compiled_ref = '@' . substr($indexes[0], 1);
  1806. } else {
  1807. $_val = $this->_parse_var_props(substr($indexes[0], 1));
  1808. $compiled_ref = '@constant(' . $_val . ')';
  1809. }
  1810. $_max_index = 1;
  1811. break;
  1812. case 'config':
  1813. $compiled_ref = "\$this->_config[0]['vars']";
  1814. $_max_index = 3;
  1815. break;
  1816. case 'ldelim':
  1817. $compiled_ref = "'$this->left_delimiter'";
  1818. break;
  1819. case 'rdelim':
  1820. $compiled_ref = "'$this->right_delimiter'";
  1821. break;
  1822. default:
  1823. $this->_syntax_error('$smarty.' . $_ref . ' is an unknown reference', E_USER_ERROR, __FILE__, __LINE__);
  1824. break;
  1825. }
  1826. if (isset($_max_index) && count($indexes) > $_max_index) {
  1827. $this->_syntax_error('$smarty' . implode('', $indexes) .' is an invalid reference', E_USER_ERROR, __FILE__, __LINE__);
  1828. }
  1829. array_shift($indexes);
  1830. return $compiled_ref;
  1831. }
  1832. /**
  1833. * compiles call to plugin of type $type with name $name
  1834. * returns a string containing the function-name or method call
  1835. * without the paramter-list that would have follow to make the
  1836. * call valid php-syntax
  1837. *
  1838. * @param string $type
  1839. * @param string $name
  1840. * @return string
  1841. */
  1842. function _compile_plugin_call($type, $name) {
  1843. if (isset($this->_plugins[$type][$name])) {
  1844. /* plugin loaded */
  1845. if (is_array($this->_plugins[$type][$name][0])) {
  1846. return ((is_object($this->_plugins[$type][$name][0][0])) ?
  1847. "\$this->_plugins['$type']['$name'][0][0]->" /* method callback */
  1848. : (string)($this->_plugins[$type][$name][0][0]).'::' /* class callback */
  1849. ). $this->_plugins[$type][$name][0][1];
  1850. } else {
  1851. /* function callback */
  1852. return $this->_plugins[$type][$name][0];
  1853. }
  1854. } else {
  1855. /* plugin not loaded -> auto-loadable-plugin */
  1856. return 'smarty_'.$type.'_'.$name;
  1857. }
  1858. }
  1859. /**
  1860. * load pre- and post-filters
  1861. */
  1862. function _load_filters()
  1863. {
  1864. if (count($this->_plugins['prefilter']) > 0) {
  1865. foreach ($this->_plugins['prefilter'] as $filter_name => $prefilter) {
  1866. if ($prefilter === false) {
  1867. unset($this->_plugins['prefilter'][$filter_name]);
  1868. $_params = array('plugins' => array(array('prefilter', $filter_name, null, null, false)));
  1869. require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');
  1870. smarty_core_load_plugins($_params, $this);
  1871. }
  1872. }
  1873. }
  1874. if (count($this->_plugins['postfilter']) > 0) {
  1875. foreach ($this->_plugins['postfilter'] as $filter_name => $postfilter) {
  1876. if ($postfilter === false) {
  1877. unset($this->_plugins['postfilter'][$filter_name]);
  1878. $_params = array('plugins' => array(array('postfilter', $filter_name, null, null, false)));
  1879. require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');
  1880. smarty_core_load_plugins($_params, $this);
  1881. }
  1882. }
  1883. }
  1884. }
  1885. /**
  1886. * Quote subpattern references
  1887. *
  1888. * @param string $string
  1889. * @return string
  1890. */
  1891. function _quote_replace($string)
  1892. {
  1893. return strtr($string, array('\\' => '\\\\', '$' => '\\$'));
  1894. }
  1895. /**
  1896. * display Smarty syntax error
  1897. *
  1898. * @param string $error_msg
  1899. * @param integer $error_type
  1900. * @param string $file
  1901. * @param integer $line
  1902. */
  1903. function _syntax_error($error_msg, $error_type = E_USER_ERROR, $file=null, $line=null)
  1904. {
  1905. $this->_trigger_fatal_error("syntax error: $error_msg", $this->_current_file, $this->_current_line_no, $file, $line, $error_type);
  1906. }
  1907. /**
  1908. * check if the compilation changes from cacheable to
  1909. * non-cacheable state with the beginning of the current
  1910. * plugin. return php-code to reflect the transition.
  1911. * @return string
  1912. */
  1913. function _push_cacheable_state($type, $name) {
  1914. $_cacheable = !isset($this->_plugins[$type][$name]) || $this->_plugins[$type][$name][4];
  1915. if ($_cacheable
  1916. || 0<$this->_cacheable_state++) return '';
  1917. if (!isset($this->_cache_serial)) $this->_cache_serial = md5(uniqid('Smarty'));
  1918. $_ret = 'if ($this->caching && !$this->_cache_including) { echo \'{nocache:'
  1919. . $this->_cache_serial . '#' . $this->_nocache_count
  1920. . '}\'; };';
  1921. return $_ret;
  1922. }
  1923. /**
  1924. * check if the compilation changes from non-cacheable to
  1925. * cacheable state with the end of the current plugin return
  1926. * php-code to reflect the transition.
  1927. * @return string
  1928. */
  1929. function _pop_cacheable_state($type, $name) {
  1930. $_cacheable = !isset($this->_plugins[$type][$name]) || $this->_plugins[$type][$name][4];
  1931. if ($_cacheable
  1932. || --$this->_cacheable_state>0) return '';
  1933. return 'if ($this->caching && !$this->_cache_including) { echo \'{/nocache:'
  1934. . $this->_cache_serial . '#' . ($this->_nocache_count++)
  1935. . '}\'; };';
  1936. }
  1937. /**
  1938. * push opening tag-name, file-name and line-number on the tag-stack
  1939. * @param string the opening tag's name
  1940. */
  1941. function _push_tag($open_tag)
  1942. {
  1943. array_push($this->_tag_stack, array($open_tag, $this->_current_line_no));
  1944. }
  1945. /**
  1946. * pop closing tag-name
  1947. * raise an error if this stack-top doesn't match with the closing tag
  1948. * @param string the closing tag's name
  1949. * @return string the opening tag's name
  1950. */
  1951. function _pop_tag($close_tag)
  1952. {
  1953. $message = '';
  1954. if (count($this->_tag_stack)>0) {
  1955. list($_open_tag, $_line_no) = array_pop($this->_tag_stack);
  1956. if ($close_tag == $_open_tag) {
  1957. return $_open_tag;
  1958. }
  1959. if ($close_tag == 'if' && ($_open_tag == 'else' || $_open_tag == 'elseif' )) {
  1960. return $this->_pop_tag($close_tag);
  1961. }
  1962. if ($close_tag == 'section' && $_open_tag == 'sectionelse') {
  1963. $this->_pop_tag($close_tag);
  1964. return $_open_tag;
  1965. }
  1966. if ($close_tag == 'foreach' && $_open_tag == 'foreachelse') {
  1967. $this->_pop_tag($close_tag);
  1968. return $_open_tag;
  1969. }
  1970. if ($_open_tag == 'else' || $_open_tag == 'elseif') {
  1971. $_open_tag = 'if';
  1972. } elseif ($_open_tag == 'sectionelse') {
  1973. $_open_tag = 'section';
  1974. } elseif ($_open_tag == 'foreachelse') {
  1975. $_open_tag = 'foreach';
  1976. }
  1977. $message = " expected {/$_open_tag} (opened line $_line_no).";
  1978. }
  1979. $this->_syntax_error("mismatched tag {/$close_tag}.$message",
  1980. E_USER_ERROR, __FILE__, __LINE__);
  1981. }
  1982. }
  1983. /**
  1984. * compare to values by their string length
  1985. *
  1986. * @access private
  1987. * @param string $a
  1988. * @param string $b
  1989. * @return 0|-1|1
  1990. */
  1991. function _smarty_sort_length($a, $b)
  1992. {
  1993. if($a == $b)
  1994. return 0;
  1995. if(strlen($a) == strlen($b))
  1996. return ($a > $b) ? -1 : 1;
  1997. return (strlen($a) > strlen($b)) ? -1 : 1;
  1998. }
  1999. /* vim: set et: */
  2000. ?>