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

/smarty/Smarty_Compiler.class.php

https://bitbucket.org/itoxable/chiron-gaming
PHP | 2319 lines | 1635 code | 238 blank | 446 comment | 322 complexity | 8ae420814c2aba6b5262765119659b6b MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  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.16
  24. * @copyright 2001-2005 New Digital Group, Inc.
  25. * @package Smarty
  26. */
  27. /* $Id: Smarty_Compiler.class.php,v 1.386 2006/11/30 17:01:28 mohrt Exp $ */
  28. /**
  29. * Template compiling class
  30. * @package Smarty
  31. */
  32. class Smarty_Compiler extends Smarty {
  33. // internal vars
  34. /**#@+
  35. * @access private
  36. */
  37. var $_folded_blocks = array(); // keeps folded template blocks
  38. var $_current_file = null; // the current template being compiled
  39. var $_current_line_no = 1; // line number for error messages
  40. var $_capture_stack = array(); // keeps track of nested capture buffers
  41. var $_plugin_info = array(); // keeps track of plugins to load
  42. var $_init_smarty_vars = false;
  43. var $_permitted_tokens = array('true','false','yes','no','on','off','null');
  44. var $_db_qstr_regexp = null; // regexps are setup in the constructor
  45. var $_si_qstr_regexp = null;
  46. var $_qstr_regexp = null;
  47. var $_func_regexp = null;
  48. var $_reg_obj_regexp = null;
  49. var $_var_bracket_regexp = null;
  50. var $_num_const_regexp = null;
  51. var $_dvar_guts_regexp = null;
  52. var $_dvar_regexp = null;
  53. var $_cvar_regexp = null;
  54. var $_svar_regexp = null;
  55. var $_avar_regexp = null;
  56. var $_mod_regexp = null;
  57. var $_var_regexp = null;
  58. var $_parenth_param_regexp = null;
  59. var $_func_call_regexp = null;
  60. var $_obj_ext_regexp = null;
  61. var $_obj_start_regexp = null;
  62. var $_obj_params_regexp = null;
  63. var $_obj_call_regexp = null;
  64. var $_cacheable_state = 0;
  65. var $_cache_attrs_count = 0;
  66. var $_nocache_count = 0;
  67. var $_cache_serial = null;
  68. var $_cache_include = null;
  69. var $_strip_depth = 0;
  70. var $_additional_newline = "\n";
  71. /**#@-*/
  72. /**
  73. * The class constructor.
  74. */
  75. function Smarty_Compiler()
  76. {
  77. // matches double quoted strings:
  78. // "foobar"
  79. // "foo\"bar"
  80. $this->_db_qstr_regexp = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';
  81. // matches single quoted strings:
  82. // 'foobar'
  83. // 'foo\'bar'
  84. $this->_si_qstr_regexp = '\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'';
  85. // matches single or double quoted strings
  86. $this->_qstr_regexp = '(?:' . $this->_db_qstr_regexp . '|' . $this->_si_qstr_regexp . ')';
  87. // matches bracket portion of vars
  88. // [0]
  89. // [foo]
  90. // [$bar]
  91. $this->_var_bracket_regexp = '\[\$?[\w\.]+\]';
  92. // matches numerical constants
  93. // 30
  94. // -12
  95. // 13.22
  96. $this->_num_const_regexp = '(?:\-?\d+(?:\.\d+)?)';
  97. // matches $ vars (not objects):
  98. // $foo
  99. // $foo.bar
  100. // $foo.bar.foobar
  101. // $foo[0]
  102. // $foo[$bar]
  103. // $foo[5][blah]
  104. // $foo[5].bar[$foobar][4]
  105. $this->_dvar_math_regexp = '(?:[\+\*\/\%]|(?:-(?!>)))';
  106. $this->_dvar_math_var_regexp = '[\$\w\.\+\-\*\/\%\d\>\[\]]';
  107. $this->_dvar_guts_regexp = '\w+(?:' . $this->_var_bracket_regexp
  108. . ')*(?:\.\$?\w+(?:' . $this->_var_bracket_regexp . ')*)*(?:' . $this->_dvar_math_regexp . '(?:' . $this->_num_const_regexp . '|' . $this->_dvar_math_var_regexp . ')*)?';
  109. $this->_dvar_regexp = '\$' . $this->_dvar_guts_regexp;
  110. // matches config vars:
  111. // #foo#
  112. // #foobar123_foo#
  113. $this->_cvar_regexp = '\#\w+\#';
  114. // matches section vars:
  115. // %foo.bar%
  116. $this->_svar_regexp = '\%\w+\.\w+\%';
  117. // matches all valid variables (no quotes, no modifiers)
  118. $this->_avar_regexp = '(?:' . $this->_dvar_regexp . '|'
  119. . $this->_cvar_regexp . '|' . $this->_svar_regexp . ')';
  120. // matches valid variable syntax:
  121. // $foo
  122. // $foo
  123. // #foo#
  124. // #foo#
  125. // "text"
  126. // "text"
  127. $this->_var_regexp = '(?:' . $this->_avar_regexp . '|' . $this->_qstr_regexp . ')';
  128. // matches valid object call (one level of object nesting allowed in parameters):
  129. // $foo->bar
  130. // $foo->bar()
  131. // $foo->bar("text")
  132. // $foo->bar($foo, $bar, "text")
  133. // $foo->bar($foo, "foo")
  134. // $foo->bar->foo()
  135. // $foo->bar->foo->bar()
  136. // $foo->bar($foo->bar)
  137. // $foo->bar($foo->bar())
  138. // $foo->bar($foo->bar($blah,$foo,44,"foo",$foo[0].bar))
  139. $this->_obj_ext_regexp = '\->(?:\$?' . $this->_dvar_guts_regexp . ')';
  140. $this->_obj_restricted_param_regexp = '(?:'
  141. . '(?:' . $this->_var_regexp . '|' . $this->_num_const_regexp . ')(?:' . $this->_obj_ext_regexp . '(?:\((?:(?:' . $this->_var_regexp . '|' . $this->_num_const_regexp . ')'
  142. . '(?:\s*,\s*(?:' . $this->_var_regexp . '|' . $this->_num_const_regexp . '))*)?\))?)*)';
  143. $this->_obj_single_param_regexp = '(?:\w+|' . $this->_obj_restricted_param_regexp . '(?:\s*,\s*(?:(?:\w+|'
  144. . $this->_var_regexp . $this->_obj_restricted_param_regexp . ')))*)';
  145. $this->_obj_params_regexp = '\((?:' . $this->_obj_single_param_regexp
  146. . '(?:\s*,\s*' . $this->_obj_single_param_regexp . ')*)?\)';
  147. $this->_obj_start_regexp = '(?:' . $this->_dvar_regexp . '(?:' . $this->_obj_ext_regexp . ')+)';
  148. $this->_obj_call_regexp = '(?:' . $this->_obj_start_regexp . '(?:' . $this->_obj_params_regexp . ')?(?:' . $this->_dvar_math_regexp . '(?:' . $this->_num_const_regexp . '|' . $this->_dvar_math_var_regexp . ')*)?)';
  149. // matches valid modifier syntax:
  150. // |foo
  151. // |@foo
  152. // |foo:"bar"
  153. // |foo:$bar
  154. // |foo:"bar":$foobar
  155. // |foo|bar
  156. // |foo:$foo->bar
  157. $this->_mod_regexp = '(?:\|@?\w+(?::(?:\w+|' . $this->_num_const_regexp . '|'
  158. . $this->_obj_call_regexp . '|' . $this->_avar_regexp . '|' . $this->_qstr_regexp .'))*)';
  159. // matches valid function name:
  160. // foo123
  161. // _foo_bar
  162. $this->_func_regexp = '[a-zA-Z_]\w*';
  163. // matches valid registered object:
  164. // foo->bar
  165. $this->_reg_obj_regexp = '[a-zA-Z_]\w*->[a-zA-Z_]\w*';
  166. // matches valid parameter values:
  167. // true
  168. // $foo
  169. // $foo|bar
  170. // #foo#
  171. // #foo#|bar
  172. // "text"
  173. // "text"|bar
  174. // $foo->bar
  175. $this->_param_regexp = '(?:\s*(?:' . $this->_obj_call_regexp . '|'
  176. . $this->_var_regexp . '|' . $this->_num_const_regexp . '|\w+)(?>' . $this->_mod_regexp . '*)\s*)';
  177. // matches valid parenthesised function parameters:
  178. //
  179. // "text"
  180. // $foo, $bar, "text"
  181. // $foo|bar, "foo"|bar, $foo->bar($foo)|bar
  182. $this->_parenth_param_regexp = '(?:\((?:\w+|'
  183. . $this->_param_regexp . '(?:\s*,\s*(?:(?:\w+|'
  184. . $this->_param_regexp . ')))*)?\))';
  185. // matches valid function call:
  186. // foo()
  187. // foo_bar($foo)
  188. // _foo_bar($foo,"bar")
  189. // foo123($foo,$foo->bar(),"foo")
  190. $this->_func_call_regexp = '(?:' . $this->_func_regexp . '\s*(?:'
  191. . $this->_parenth_param_regexp . '))';
  192. }
  193. /**
  194. * compile a resource
  195. *
  196. * sets $compiled_content to the compiled source
  197. * @param string $resource_name
  198. * @param string $source_content
  199. * @param string $compiled_content
  200. * @return true
  201. */
  202. function _compile_file($resource_name, $source_content, &$compiled_content)
  203. {
  204. if ($this->security) {
  205. // do not allow php syntax to be executed unless specified
  206. if ($this->php_handling == SMARTY_PHP_ALLOW &&
  207. !$this->security_settings['PHP_HANDLING']) {
  208. $this->php_handling = SMARTY_PHP_PASSTHRU;
  209. }
  210. }
  211. $this->_load_filters();
  212. $this->_current_file = $resource_name;
  213. $this->_current_line_no = 1;
  214. $ldq = preg_quote($this->left_delimiter, '~');
  215. $rdq = preg_quote($this->right_delimiter, '~');
  216. /* un-hide hidden xml open tags */
  217. $source_content = preg_replace("~<({$ldq}(.*?){$rdq})[?]~s", '< \\1', $source_content);
  218. // run template source through prefilter functions
  219. if (count($this->_plugins['prefilter']) > 0) {
  220. foreach ($this->_plugins['prefilter'] as $filter_name => $prefilter) {
  221. if ($prefilter === false) continue;
  222. if ($prefilter[3] || is_callable($prefilter[0])) {
  223. $source_content = call_user_func_array($prefilter[0],
  224. array($source_content, &$this));
  225. $this->_plugins['prefilter'][$filter_name][3] = true;
  226. } else {
  227. $this->_trigger_fatal_error("[plugin] prefilter '$filter_name' is not implemented");
  228. }
  229. }
  230. }
  231. /* fetch all special blocks */
  232. $search = "~{$ldq}\*(.*?)\*{$rdq}|{$ldq}\s*literal\s*{$rdq}(.*?){$ldq}\s*/literal\s*{$rdq}|{$ldq}\s*php\s*{$rdq}(.*?){$ldq}\s*/php\s*{$rdq}~s";
  233. preg_match_all($search, $source_content, $match, PREG_SET_ORDER);
  234. $this->_folded_blocks = $match;
  235. reset($this->_folded_blocks);
  236. /* replace special blocks by "{php}" */
  237. $source_content = preg_replace($search.'e', "'"
  238. . $this->_quote_replace($this->left_delimiter) . 'php'
  239. . "' . str_repeat(\"\n\", substr_count('\\0', \"\n\")) .'"
  240. . $this->_quote_replace($this->right_delimiter)
  241. . "'"
  242. , $source_content);
  243. /* Gather all template tags. */
  244. preg_match_all("~{$ldq}\s*(.*?)\s*{$rdq}~s", $source_content, $_match);
  245. $template_tags = $_match[1];
  246. /* Split content by template tags to obtain non-template content. */
  247. $text_blocks = preg_split("~{$ldq}.*?{$rdq}~s", $source_content);
  248. /* loop through text blocks */
  249. for ($curr_tb = 0, $for_max = count($text_blocks); $curr_tb < $for_max; $curr_tb++) {
  250. /* match anything resembling php tags */
  251. if (preg_match_all('~(<\?(?:\w+|=)?|\?>|language\s*=\s*[\"\']?php[\"\']?)~is', $text_blocks[$curr_tb], $sp_match)) {
  252. /* replace tags with placeholders to prevent recursive replacements */
  253. $sp_match[1] = array_unique($sp_match[1]);
  254. usort($sp_match[1], '_smarty_sort_length');
  255. for ($curr_sp = 0, $for_max2 = count($sp_match[1]); $curr_sp < $for_max2; $curr_sp++) {
  256. $text_blocks[$curr_tb] = str_replace($sp_match[1][$curr_sp],'%%%SMARTYSP'.$curr_sp.'%%%',$text_blocks[$curr_tb]);
  257. }
  258. /* process each one */
  259. for ($curr_sp = 0, $for_max2 = count($sp_match[1]); $curr_sp < $for_max2; $curr_sp++) {
  260. if ($this->php_handling == SMARTY_PHP_PASSTHRU) {
  261. /* echo php contents */
  262. $text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', '<?php echo \''.str_replace("'", "\'", $sp_match[1][$curr_sp]).'\'; ?>'."\n", $text_blocks[$curr_tb]);
  263. } else if ($this->php_handling == SMARTY_PHP_QUOTE) {
  264. /* quote php tags */
  265. $text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', htmlspecialchars($sp_match[1][$curr_sp]), $text_blocks[$curr_tb]);
  266. } else if ($this->php_handling == SMARTY_PHP_REMOVE) {
  267. /* remove php tags */
  268. $text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', '', $text_blocks[$curr_tb]);
  269. } else {
  270. /* SMARTY_PHP_ALLOW, but echo non php starting tags */
  271. $sp_match[1][$curr_sp] = preg_replace('~(<\?(?!php|=|$))~i', '<?php echo \'\\1\'?>'."\n", $sp_match[1][$curr_sp]);
  272. $text_blocks[$curr_tb] = str_replace('%%%SMARTYSP'.$curr_sp.'%%%', $sp_match[1][$curr_sp], $text_blocks[$curr_tb]);
  273. }
  274. }
  275. }
  276. }
  277. /* Compile the template tags into PHP code. */
  278. $compiled_tags = array();
  279. for ($i = 0, $for_max = count($template_tags); $i < $for_max; $i++) {
  280. $this->_current_line_no += substr_count($text_blocks[$i], "\n");
  281. $compiled_tags[] = $this->_compile_tag($template_tags[$i]);
  282. $this->_current_line_no += substr_count($template_tags[$i], "\n");
  283. }
  284. if (count($this->_tag_stack)>0) {
  285. list($_open_tag, $_line_no) = end($this->_tag_stack);
  286. $this->_syntax_error("unclosed tag \{$_open_tag} (opened line $_line_no).", E_USER_ERROR, __FILE__, __LINE__);
  287. return;
  288. }
  289. /* Reformat $text_blocks between 'strip' and '/strip' tags,
  290. removing spaces, tabs and newlines. */
  291. $strip = false;
  292. for ($i = 0, $for_max = count($compiled_tags); $i < $for_max; $i++) {
  293. if ($compiled_tags[$i] == '{strip}') {
  294. $compiled_tags[$i] = '';
  295. $strip = true;
  296. /* remove leading whitespaces */
  297. $text_blocks[$i + 1] = ltrim($text_blocks[$i + 1]);
  298. }
  299. if ($strip) {
  300. /* strip all $text_blocks before the next '/strip' */
  301. for ($j = $i + 1; $j < $for_max; $j++) {
  302. /* remove leading and trailing whitespaces of each line */
  303. $text_blocks[$j] = preg_replace('![\t ]*[\r\n]+[\t ]*!', '', $text_blocks[$j]);
  304. if ($compiled_tags[$j] == '{/strip}') {
  305. /* remove trailing whitespaces from the last text_block */
  306. $text_blocks[$j] = rtrim($text_blocks[$j]);
  307. }
  308. $text_blocks[$j] = "<?php echo '" . strtr($text_blocks[$j], array("'"=>"\'", "\\"=>"\\\\")) . "'; ?>";
  309. if ($compiled_tags[$j] == '{/strip}') {
  310. $compiled_tags[$j] = "\n"; /* slurped by php, but necessary
  311. if a newline is following the closing strip-tag */
  312. $strip = false;
  313. $i = $j;
  314. break;
  315. }
  316. }
  317. }
  318. }
  319. $compiled_content = '';
  320. /* Interleave the compiled contents and text blocks to get the final result. */
  321. for ($i = 0, $for_max = count($compiled_tags); $i < $for_max; $i++) {
  322. if ($compiled_tags[$i] == '') {
  323. // tag result empty, remove first newline from following text block
  324. $text_blocks[$i+1] = preg_replace('~^(\r\n|\r|\n)~', '', $text_blocks[$i+1]);
  325. }
  326. $compiled_content .= $text_blocks[$i].$compiled_tags[$i];
  327. }
  328. $compiled_content .= $text_blocks[$i];
  329. // remove \n from the end of the file, if any
  330. if (strlen($compiled_content) && (substr($compiled_content, -1) == "\n") ) {
  331. $compiled_content = substr($compiled_content, 0, -1);
  332. }
  333. if (!empty($this->_cache_serial)) {
  334. $compiled_content = "<?php \$this->_cache_serials['".$this->_cache_include."'] = '".$this->_cache_serial."'; ?>" . $compiled_content;
  335. }
  336. // remove unnecessary close/open tags
  337. $compiled_content = preg_replace('~\?>\n?<\?php~', '', $compiled_content);
  338. // run compiled template through postfilter functions
  339. if (count($this->_plugins['postfilter']) > 0) {
  340. foreach ($this->_plugins['postfilter'] as $filter_name => $postfilter) {
  341. if ($postfilter === false) continue;
  342. if ($postfilter[3] || is_callable($postfilter[0])) {
  343. $compiled_content = call_user_func_array($postfilter[0],
  344. array($compiled_content, &$this));
  345. $this->_plugins['postfilter'][$filter_name][3] = true;
  346. } else {
  347. $this->_trigger_fatal_error("Smarty plugin error: postfilter '$filter_name' is not implemented");
  348. }
  349. }
  350. }
  351. // put header at the top of the compiled template
  352. $template_header = "<?php /* Smarty version ".$this->_version.", created on ".strftime("%Y-%m-%d %H:%M:%S")."\n";
  353. $template_header .= " compiled from ".strtr(urlencode($resource_name), array('%2F'=>'/', '%3A'=>':'))." */ ?>\n";
  354. /* Emit code to load needed plugins. */
  355. $this->_plugins_code = '';
  356. if (count($this->_plugin_info)) {
  357. $_plugins_params = "array('plugins' => array(";
  358. foreach ($this->_plugin_info as $plugin_type => $plugins) {
  359. foreach ($plugins as $plugin_name => $plugin_info) {
  360. $_plugins_params .= "array('$plugin_type', '$plugin_name', '" . strtr($plugin_info[0], array("'" => "\\'", "\\" => "\\\\")) . "', $plugin_info[1], ";
  361. $_plugins_params .= $plugin_info[2] ? 'true),' : 'false),';
  362. }
  363. }
  364. $_plugins_params .= '))';
  365. $plugins_code = "<?php require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');\nsmarty_core_load_plugins($_plugins_params, \$this); ?>\n";
  366. $template_header .= $plugins_code;
  367. $this->_plugin_info = array();
  368. $this->_plugins_code = $plugins_code;
  369. }
  370. if ($this->_init_smarty_vars) {
  371. $template_header .= "<?php require_once(SMARTY_CORE_DIR . 'core.assign_smarty_interface.php');\nsmarty_core_assign_smarty_interface(null, \$this); ?>\n";
  372. $this->_init_smarty_vars = false;
  373. }
  374. $compiled_content = $template_header . $compiled_content;
  375. return true;
  376. }
  377. /**
  378. * Compile a template tag
  379. *
  380. * @param string $template_tag
  381. * @return string
  382. */
  383. function _compile_tag($template_tag)
  384. {
  385. /* Matched comment. */
  386. if (substr($template_tag, 0, 1) == '*' && substr($template_tag, -1) == '*')
  387. return '';
  388. /* Split tag into two three parts: command, command modifiers and the arguments. */
  389. if(! preg_match('~^(?:(' . $this->_num_const_regexp . '|' . $this->_obj_call_regexp . '|' . $this->_var_regexp
  390. . '|\/?' . $this->_reg_obj_regexp . '|\/?' . $this->_func_regexp . ')(' . $this->_mod_regexp . '*))
  391. (?:\s+(.*))?$
  392. ~xs', $template_tag, $match)) {
  393. $this->_syntax_error("unrecognized tag: $template_tag", E_USER_ERROR, __FILE__, __LINE__);
  394. }
  395. $tag_command = $match[1];
  396. $tag_modifier = isset($match[2]) ? $match[2] : null;
  397. $tag_args = isset($match[3]) ? $match[3] : null;
  398. if (preg_match('~^' . $this->_num_const_regexp . '|' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '$~', $tag_command)) {
  399. /* tag name is a variable or object */
  400. $_return = $this->_parse_var_props($tag_command . $tag_modifier);
  401. return "<?php echo $_return; ?>" . $this->_additional_newline;
  402. }
  403. /* If the tag name is a registered object, we process it. */
  404. if (preg_match('~^\/?' . $this->_reg_obj_regexp . '$~', $tag_command)) {
  405. return $this->_compile_registered_object_tag($tag_command, $this->_parse_attrs($tag_args), $tag_modifier);
  406. }
  407. switch ($tag_command) {
  408. case 'include':
  409. return $this->_compile_include_tag($tag_args);
  410. case 'include_php':
  411. return $this->_compile_include_php_tag($tag_args);
  412. case 'if':
  413. $this->_push_tag('if');
  414. return $this->_compile_if_tag($tag_args);
  415. case 'else':
  416. list($_open_tag) = end($this->_tag_stack);
  417. if ($_open_tag != 'if' && $_open_tag != 'elseif')
  418. $this->_syntax_error('unexpected {else}', E_USER_ERROR, __FILE__, __LINE__);
  419. else
  420. $this->_push_tag('else');
  421. return '<?php else: ?>';
  422. case 'elseif':
  423. list($_open_tag) = end($this->_tag_stack);
  424. if ($_open_tag != 'if' && $_open_tag != 'elseif')
  425. $this->_syntax_error('unexpected {elseif}', E_USER_ERROR, __FILE__, __LINE__);
  426. if ($_open_tag == 'if')
  427. $this->_push_tag('elseif');
  428. return $this->_compile_if_tag($tag_args, true);
  429. case '/if':
  430. $this->_pop_tag('if');
  431. return '<?php endif; ?>';
  432. case 'capture':
  433. return $this->_compile_capture_tag(true, $tag_args);
  434. case '/capture':
  435. return $this->_compile_capture_tag(false);
  436. case 'ldelim':
  437. return $this->left_delimiter;
  438. case 'rdelim':
  439. return $this->right_delimiter;
  440. case 'section':
  441. $this->_push_tag('section');
  442. return $this->_compile_section_start($tag_args);
  443. case 'sectionelse':
  444. $this->_push_tag('sectionelse');
  445. return "<?php endfor; else: ?>";
  446. break;
  447. case '/section':
  448. $_open_tag = $this->_pop_tag('section');
  449. if ($_open_tag == 'sectionelse')
  450. return "<?php endif; ?>";
  451. else
  452. return "<?php endfor; endif; ?>";
  453. case 'foreach':
  454. $this->_push_tag('foreach');
  455. return $this->_compile_foreach_start($tag_args);
  456. break;
  457. case 'foreachelse':
  458. $this->_push_tag('foreachelse');
  459. return "<?php endforeach; else: ?>";
  460. case '/foreach':
  461. $_open_tag = $this->_pop_tag('foreach');
  462. if ($_open_tag == 'foreachelse')
  463. return "<?php endif; unset(\$_from); ?>";
  464. else
  465. return "<?php endforeach; endif; unset(\$_from); ?>";
  466. break;
  467. case 'strip':
  468. case '/strip':
  469. if (substr($tag_command, 0, 1)=='/') {
  470. $this->_pop_tag('strip');
  471. if (--$this->_strip_depth==0) { /* outermost closing {/strip} */
  472. $this->_additional_newline = "\n";
  473. return '{' . $tag_command . '}';
  474. }
  475. } else {
  476. $this->_push_tag('strip');
  477. if ($this->_strip_depth++==0) { /* outermost opening {strip} */
  478. $this->_additional_newline = "";
  479. return '{' . $tag_command . '}';
  480. }
  481. }
  482. return '';
  483. case 'php':
  484. /* handle folded tags replaced by {php} */
  485. list(, $block) = each($this->_folded_blocks);
  486. $this->_current_line_no += substr_count($block[0], "\n");
  487. /* the number of matched elements in the regexp in _compile_file()
  488. determins the type of folded tag that was found */
  489. switch (count($block)) {
  490. case 2: /* comment */
  491. return '';
  492. case 3: /* literal */
  493. return "<?php echo '" . strtr($block[2], array("'"=>"\'", "\\"=>"\\\\")) . "'; ?>" . $this->_additional_newline;
  494. case 4: /* php */
  495. if ($this->security && !$this->security_settings['PHP_TAGS']) {
  496. $this->_syntax_error("(secure mode) php tags not permitted", E_USER_WARNING, __FILE__, __LINE__);
  497. return;
  498. }
  499. return '<?php ' . $block[3] .' ?>';
  500. }
  501. break;
  502. case 'insert':
  503. return $this->_compile_insert_tag($tag_args);
  504. default:
  505. if ($this->_compile_compiler_tag($tag_command, $tag_args, $output)) {
  506. return $output;
  507. } else if ($this->_compile_block_tag($tag_command, $tag_args, $tag_modifier, $output)) {
  508. return $output;
  509. } else if ($this->_compile_custom_tag($tag_command, $tag_args, $tag_modifier, $output)) {
  510. return $output;
  511. } else {
  512. $this->_syntax_error("unrecognized tag '$tag_command'", E_USER_ERROR, __FILE__, __LINE__);
  513. }
  514. }
  515. }
  516. /**
  517. * compile the custom compiler tag
  518. *
  519. * sets $output to the compiled custom compiler tag
  520. * @param string $tag_command
  521. * @param string $tag_args
  522. * @param string $output
  523. * @return boolean
  524. */
  525. function _compile_compiler_tag($tag_command, $tag_args, &$output)
  526. {
  527. $found = false;
  528. $have_function = true;
  529. /*
  530. * First we check if the compiler function has already been registered
  531. * or loaded from a plugin file.
  532. */
  533. if (isset($this->_plugins['compiler'][$tag_command])) {
  534. $found = true;
  535. $plugin_func = $this->_plugins['compiler'][$tag_command][0];
  536. if (!is_callable($plugin_func)) {
  537. $message = "compiler function '$tag_command' is not implemented";
  538. $have_function = false;
  539. }
  540. }
  541. /*
  542. * Otherwise we need to load plugin file and look for the function
  543. * inside it.
  544. */
  545. else if ($plugin_file = $this->_get_plugin_filepath('compiler', $tag_command)) {
  546. $found = true;
  547. include_once $plugin_file;
  548. $plugin_func = 'smarty_compiler_' . $tag_command;
  549. if (!is_callable($plugin_func)) {
  550. $message = "plugin function $plugin_func() not found in $plugin_file\n";
  551. $have_function = false;
  552. } else {
  553. $this->_plugins['compiler'][$tag_command] = array($plugin_func, null, null, null, true);
  554. }
  555. }
  556. /*
  557. * True return value means that we either found a plugin or a
  558. * dynamically registered function. False means that we didn't and the
  559. * compiler should now emit code to load custom function plugin for this
  560. * tag.
  561. */
  562. if ($found) {
  563. if ($have_function) {
  564. $output = call_user_func_array($plugin_func, array($tag_args, &$this));
  565. if($output != '') {
  566. $output = '<?php ' . $this->_push_cacheable_state('compiler', $tag_command)
  567. . $output
  568. . $this->_pop_cacheable_state('compiler', $tag_command) . ' ?>';
  569. }
  570. } else {
  571. $this->_syntax_error($message, E_USER_WARNING, __FILE__, __LINE__);
  572. }
  573. return true;
  574. } else {
  575. return false;
  576. }
  577. }
  578. /**
  579. * compile block function tag
  580. *
  581. * sets $output to compiled block function tag
  582. * @param string $tag_command
  583. * @param string $tag_args
  584. * @param string $tag_modifier
  585. * @param string $output
  586. * @return boolean
  587. */
  588. function _compile_block_tag($tag_command, $tag_args, $tag_modifier, &$output)
  589. {
  590. if (substr($tag_command, 0, 1) == '/') {
  591. $start_tag = false;
  592. $tag_command = substr($tag_command, 1);
  593. } else
  594. $start_tag = true;
  595. $found = false;
  596. $have_function = true;
  597. /*
  598. * First we check if the block function has already been registered
  599. * or loaded from a plugin file.
  600. */
  601. if (isset($this->_plugins['block'][$tag_command])) {
  602. $found = true;
  603. $plugin_func = $this->_plugins['block'][$tag_command][0];
  604. if (!is_callable($plugin_func)) {
  605. $message = "block function '$tag_command' is not implemented";
  606. $have_function = false;
  607. }
  608. }
  609. /*
  610. * Otherwise we need to load plugin file and look for the function
  611. * inside it.
  612. */
  613. else if ($plugin_file = $this->_get_plugin_filepath('block', $tag_command)) {
  614. $found = true;
  615. include_once $plugin_file;
  616. $plugin_func = 'smarty_block_' . $tag_command;
  617. if (!function_exists($plugin_func)) {
  618. $message = "plugin function $plugin_func() not found in $plugin_file\n";
  619. $have_function = false;
  620. } else {
  621. $this->_plugins['block'][$tag_command] = array($plugin_func, null, null, null, true);
  622. }
  623. }
  624. if (!$found) {
  625. return false;
  626. } else if (!$have_function) {
  627. $this->_syntax_error($message, E_USER_WARNING, __FILE__, __LINE__);
  628. return true;
  629. }
  630. /*
  631. * Even though we've located the plugin function, compilation
  632. * happens only once, so the plugin will still need to be loaded
  633. * at runtime for future requests.
  634. */
  635. $this->_add_plugin('block', $tag_command);
  636. if ($start_tag)
  637. $this->_push_tag($tag_command);
  638. else
  639. $this->_pop_tag($tag_command);
  640. if ($start_tag) {
  641. $output = '<?php ' . $this->_push_cacheable_state('block', $tag_command);
  642. $attrs = $this->_parse_attrs($tag_args);
  643. $_cache_attrs='';
  644. $arg_list = $this->_compile_arg_list('block', $tag_command, $attrs, $_cache_attrs);
  645. $output .= "$_cache_attrs\$this->_tag_stack[] = array('$tag_command', array(".implode(',', $arg_list).')); ';
  646. $output .= '$_block_repeat=true;' . $this->_compile_plugin_call('block', $tag_command).'($this->_tag_stack[count($this->_tag_stack)-1][1], null, $this, $_block_repeat);';
  647. $output .= 'while ($_block_repeat) { ob_start(); ?>';
  648. } else {
  649. $output = '<?php $_block_content = ob_get_contents(); ob_end_clean(); ';
  650. $_out_tag_text = $this->_compile_plugin_call('block', $tag_command).'($this->_tag_stack[count($this->_tag_stack)-1][1], $_block_content, $this, $_block_repeat)';
  651. if ($tag_modifier != '') {
  652. $this->_parse_modifiers($_out_tag_text, $tag_modifier);
  653. }
  654. $output .= '$_block_repeat=false;echo ' . $_out_tag_text . '; } ';
  655. $output .= " array_pop(\$this->_tag_stack); " . $this->_pop_cacheable_state('block', $tag_command) . '?>';
  656. }
  657. return true;
  658. }
  659. /**
  660. * compile custom function tag
  661. *
  662. * @param string $tag_command
  663. * @param string $tag_args
  664. * @param string $tag_modifier
  665. * @return string
  666. */
  667. function _compile_custom_tag($tag_command, $tag_args, $tag_modifier, &$output)
  668. {
  669. $found = false;
  670. $have_function = true;
  671. /*
  672. * First we check if the custom function has already been registered
  673. * or loaded from a plugin file.
  674. */
  675. if (isset($this->_plugins['function'][$tag_command])) {
  676. $found = true;
  677. $plugin_func = $this->_plugins['function'][$tag_command][0];
  678. if (!is_callable($plugin_func)) {
  679. $message = "custom function '$tag_command' is not implemented";
  680. $have_function = false;
  681. }
  682. }
  683. /*
  684. * Otherwise we need to load plugin file and look for the function
  685. * inside it.
  686. */
  687. else if ($plugin_file = $this->_get_plugin_filepath('function', $tag_command)) {
  688. $found = true;
  689. include_once $plugin_file;
  690. $plugin_func = 'smarty_function_' . $tag_command;
  691. if (!function_exists($plugin_func)) {
  692. $message = "plugin function $plugin_func() not found in $plugin_file\n";
  693. $have_function = false;
  694. } else {
  695. $this->_plugins['function'][$tag_command] = array($plugin_func, null, null, null, true);
  696. }
  697. }
  698. if (!$found) {
  699. return false;
  700. } else if (!$have_function) {
  701. $this->_syntax_error($message, E_USER_WARNING, __FILE__, __LINE__);
  702. return true;
  703. }
  704. /* declare plugin to be loaded on display of the template that
  705. we compile right now */
  706. $this->_add_plugin('function', $tag_command);
  707. $_cacheable_state = $this->_push_cacheable_state('function', $tag_command);
  708. $attrs = $this->_parse_attrs($tag_args);
  709. $_cache_attrs = '';
  710. $arg_list = $this->_compile_arg_list('function', $tag_command, $attrs, $_cache_attrs);
  711. $output = $this->_compile_plugin_call('function', $tag_command).'(array('.implode(',', $arg_list)."), \$this)";
  712. if($tag_modifier != '') {
  713. $this->_parse_modifiers($output, $tag_modifier);
  714. }
  715. if($output != '') {
  716. $output = '<?php ' . $_cacheable_state . $_cache_attrs . 'echo ' . $output . ';'
  717. . $this->_pop_cacheable_state('function', $tag_command) . "?>" . $this->_additional_newline;
  718. }
  719. return true;
  720. }
  721. /**
  722. * compile a registered object tag
  723. *
  724. * @param string $tag_command
  725. * @param array $attrs
  726. * @param string $tag_modifier
  727. * @return string
  728. */
  729. function _compile_registered_object_tag($tag_command, $attrs, $tag_modifier)
  730. {
  731. if (substr($tag_command, 0, 1) == '/') {
  732. $start_tag = false;
  733. $tag_command = substr($tag_command, 1);
  734. } else {
  735. $start_tag = true;
  736. }
  737. list($object, $obj_comp) = explode('->', $tag_command);
  738. $arg_list = array();
  739. if(count($attrs)) {
  740. $_assign_var = false;
  741. foreach ($attrs as $arg_name => $arg_value) {
  742. if($arg_name == 'assign') {
  743. $_assign_var = $arg_value;
  744. unset($attrs['assign']);
  745. continue;
  746. }
  747. if (is_bool($arg_value))
  748. $arg_value = $arg_value ? 'true' : 'false';
  749. $arg_list[] = "'$arg_name' => $arg_value";
  750. }
  751. }
  752. if($this->_reg_objects[$object][2]) {
  753. // smarty object argument format
  754. $args = "array(".implode(',', (array)$arg_list)."), \$this";
  755. } else {
  756. // traditional argument format
  757. $args = implode(',', array_values($attrs));
  758. if (empty($args)) {
  759. $args = 'null';
  760. }
  761. }
  762. $prefix = '';
  763. $postfix = '';
  764. $newline = '';
  765. if(!is_object($this->_reg_objects[$object][0])) {
  766. $this->_trigger_fatal_error("registered '$object' is not an object" , $this->_current_file, $this->_current_line_no, __FILE__, __LINE__);
  767. } elseif(!empty($this->_reg_objects[$object][1]) && !in_array($obj_comp, $this->_reg_objects[$object][1])) {
  768. $this->_trigger_fatal_error("'$obj_comp' is not a registered component of object '$object'", $this->_current_file, $this->_current_line_no, __FILE__, __LINE__);
  769. } elseif(method_exists($this->_reg_objects[$object][0], $obj_comp)) {
  770. // method
  771. if(in_array($obj_comp, $this->_reg_objects[$object][3])) {
  772. // block method
  773. if ($start_tag) {
  774. $prefix = "\$this->_tag_stack[] = array('$obj_comp', $args); ";
  775. $prefix .= "\$_block_repeat=true; \$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], null, \$this, \$_block_repeat); ";
  776. $prefix .= "while (\$_block_repeat) { ob_start();";
  777. $return = null;
  778. $postfix = '';
  779. } else {
  780. $prefix = "\$_obj_block_content = ob_get_contents(); ob_end_clean(); \$_block_repeat=false;";
  781. $return = "\$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], \$_obj_block_content, \$this, \$_block_repeat)";
  782. $postfix = "} array_pop(\$this->_tag_stack);";
  783. }
  784. } else {
  785. // non-block method
  786. $return = "\$this->_reg_objects['$object'][0]->$obj_comp($args)";
  787. }
  788. } else {
  789. // property
  790. $return = "\$this->_reg_objects['$object'][0]->$obj_comp";
  791. }
  792. if($return != null) {
  793. if($tag_modifier != '') {
  794. $this->_parse_modifiers($return, $tag_modifier);
  795. }
  796. if(!empty($_assign_var)) {
  797. $output = "\$this->assign('" . $this->_dequote($_assign_var) ."', $return);";
  798. } else {
  799. $output = 'echo ' . $return . ';';
  800. $newline = $this->_additional_newline;
  801. }
  802. } else {
  803. $output = '';
  804. }
  805. return '<?php ' . $prefix . $output . $postfix . "?>" . $newline;
  806. }
  807. /**
  808. * Compile {insert ...} tag
  809. *
  810. * @param string $tag_args
  811. * @return string
  812. */
  813. function _compile_insert_tag($tag_args)
  814. {
  815. $attrs = $this->_parse_attrs($tag_args);
  816. $name = $this->_dequote($attrs['name']);
  817. if (empty($name)) {
  818. return $this->_syntax_error("missing insert name", E_USER_ERROR, __FILE__, __LINE__);
  819. }
  820. if (!preg_match('~^\w+$~', $name)) {
  821. return $this->_syntax_error("'insert: 'name' must be an insert function name", E_USER_ERROR, __FILE__, __LINE__);
  822. }
  823. if (!empty($attrs['script'])) {
  824. $delayed_loading = true;
  825. } else {
  826. $delayed_loading = false;
  827. }
  828. foreach ($attrs as $arg_name => $arg_value) {
  829. if (is_bool($arg_value))
  830. $arg_value = $arg_value ? 'true' : 'false';
  831. $arg_list[] = "'$arg_name' => $arg_value";
  832. }
  833. $this->_add_plugin('insert', $name, $delayed_loading);
  834. $_params = "array('args' => array(".implode(', ', (array)$arg_list)."))";
  835. return "<?php require_once(SMARTY_CORE_DIR . 'core.run_insert_handler.php');\necho smarty_core_run_insert_handler($_params, \$this); ?>" . $this->_additional_newline;
  836. }
  837. /**
  838. * Compile {include ...} tag
  839. *
  840. * @param string $tag_args
  841. * @return string
  842. */
  843. function _compile_include_tag($tag_args)
  844. {
  845. $attrs = $this->_parse_attrs($tag_args);
  846. $arg_list = array();
  847. if (empty($attrs['file'])) {
  848. $this->_syntax_error("missing 'file' attribute in include tag", E_USER_ERROR, __FILE__, __LINE__);
  849. }
  850. foreach ($attrs as $arg_name => $arg_value) {
  851. if ($arg_name == 'file') {
  852. $include_file = $arg_value;
  853. continue;
  854. } else if ($arg_name == 'assign') {
  855. $assign_var = $arg_value;
  856. continue;
  857. }
  858. if (is_bool($arg_value))
  859. $arg_value = $arg_value ? 'true' : 'false';
  860. $arg_list[] = "'$arg_name' => $arg_value";
  861. }
  862. $output = '<?php ';
  863. if (isset($assign_var)) {
  864. $output .= "ob_start();\n";
  865. }
  866. $output .=
  867. "\$_smarty_tpl_vars = \$this->_tpl_vars;\n";
  868. $_params = "array('smarty_include_tpl_file' => " . $include_file . ", 'smarty_include_vars' => array(".implode(',', (array)$arg_list)."))";
  869. $output .= "\$this->_smarty_include($_params);\n" .
  870. "\$this->_tpl_vars = \$_smarty_tpl_vars;\n" .
  871. "unset(\$_smarty_tpl_vars);\n";
  872. if (isset($assign_var)) {
  873. $output .= "\$this->assign(" . $assign_var . ", ob_get_contents()); ob_end_clean();\n";
  874. }
  875. $output .= ' ?>';
  876. return $output;
  877. }
  878. /**
  879. * Compile {include ...} tag
  880. *
  881. * @param string $tag_args
  882. * @return string
  883. */
  884. function _compile_include_php_tag($tag_args)
  885. {
  886. $attrs = $this->_parse_attrs($tag_args);
  887. if (empty($attrs['file'])) {
  888. $this->_syntax_error("missing 'file' attribute in include_php tag", E_USER_ERROR, __FILE__, __LINE__);
  889. }
  890. $assign_var = (empty($attrs['assign'])) ? '' : $this->_dequote($attrs['assign']);
  891. $once_var = (empty($attrs['once']) || $attrs['once']=='false') ? 'false' : 'true';
  892. $arg_list = array();
  893. foreach($attrs as $arg_name => $arg_value) {
  894. if($arg_name != 'file' AND $arg_name != 'once' AND $arg_name != 'assign') {
  895. if(is_bool($arg_value))
  896. $arg_value = $arg_value ? 'true' : 'false';
  897. $arg_list[] = "'$arg_name' => $arg_value";
  898. }
  899. }
  900. $_params = "array('smarty_file' => " . $attrs['file'] . ", 'smarty_assign' => '$assign_var', 'smarty_once' => $once_var, 'smarty_include_vars' => array(".implode(',', $arg_list)."))";
  901. return "<?php require_once(SMARTY_CORE_DIR . 'core.smarty_include_php.php');\nsmarty_core_smarty_include_php($_params, \$this); ?>" . $this->_additional_newline;
  902. }
  903. /**
  904. * Compile {section ...} tag
  905. *
  906. * @param string $tag_args
  907. * @return string
  908. */
  909. function _compile_section_start($tag_args)
  910. {
  911. $attrs = $this->_parse_attrs($tag_args);
  912. $arg_list = array();
  913. $output = '<?php ';
  914. $section_name = $attrs['name'];
  915. if (empty($section_name)) {
  916. $this->_syntax_error("missing section name", E_USER_ERROR, __FILE__, __LINE__);
  917. }
  918. $output .= "unset(\$this->_sections[$section_name]);\n";
  919. $section_props = "\$this->_sections[$section_name]";
  920. foreach ($attrs as $attr_name => $attr_value) {
  921. switch ($attr_name) {
  922. case 'loop':
  923. $output .= "{$section_props}['loop'] = is_array(\$_loop=$attr_value) ? count(\$_loop) : max(0, (int)\$_loop); unset(\$_loop);\n";
  924. break;
  925. case 'show':
  926. if (is_bool($attr_value))
  927. $show_attr_value = $attr_value ? 'true' : 'false';
  928. else
  929. $show_attr_value = "(bool)$attr_value";
  930. $output .= "{$section_props}['show'] = $show_attr_value;\n";
  931. break;
  932. case 'name':
  933. $output .= "{$section_props}['$attr_name'] = $attr_value;\n";
  934. break;
  935. case 'max':
  936. case 'start':
  937. $output .= "{$section_props}['$attr_name'] = (int)$attr_value;\n";
  938. break;
  939. case 'step':
  940. $output .= "{$section_props}['$attr_name'] = ((int)$attr_value) == 0 ? 1 : (int)$attr_value;\n";
  941. break;
  942. default:
  943. $this->_syntax_error("unknown section attribute - '$attr_name'", E_USER_ERROR, __FILE__, __LINE__);
  944. break;
  945. }
  946. }
  947. if (!isset($attrs['show']))
  948. $output .= "{$section_props}['show'] = true;\n";
  949. if (!isset($attrs['loop']))
  950. $output .= "{$section_props}['loop'] = 1;\n";
  951. if (!isset($attrs['max']))
  952. $output .= "{$section_props}['max'] = {$section_props}['loop'];\n";
  953. else
  954. $output .= "if ({$section_props}['max'] < 0)\n" .
  955. " {$section_props}['max'] = {$section_props}['loop'];\n";
  956. if (!isset($attrs['step']))
  957. $output .= "{$section_props}['step'] = 1;\n";
  958. if (!isset($attrs['start']))
  959. $output .= "{$section_props}['start'] = {$section_props}['step'] > 0 ? 0 : {$section_props}['loop']-1;\n";
  960. else {
  961. $output .= "if ({$section_props}['start'] < 0)\n" .
  962. " {$section_props}['start'] = max({$section_props}['step'] > 0 ? 0 : -1, {$section_props}['loop'] + {$section_props}['start']);\n" .
  963. "else\n" .
  964. " {$section_props}['start'] = min({$section_props}['start'], {$section_props}['step'] > 0 ? {$section_props}['loop'] : {$section_props}['loop']-1);\n";
  965. }
  966. $output .= "if ({$section_props}['show']) {\n";
  967. if (!isset($attrs['start']) && !isset($attrs['step']) && !isset($attrs['max'])) {
  968. $output .= " {$section_props}['total'] = {$section_props}['loop'];\n";
  969. } else {
  970. $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";
  971. }
  972. $output .= " if ({$section_props}['total'] == 0)\n" .
  973. " {$section_props}['show'] = false;\n" .
  974. "} else\n" .
  975. " {$section_props}['total'] = 0;\n";
  976. $output .= "if ({$section_props}['show']):\n";
  977. $output .= "
  978. for ({$section_props}['index'] = {$section_props}['start'], {$section_props}['iteration'] = 1;
  979. {$section_props}['iteration'] <= {$section_props}['total'];
  980. {$section_props}['index'] += {$section_props}['step'], {$section_props}['iteration']++):\n";
  981. $output .= "{$section_props}['rownum'] = {$section_props}['iteration'];\n";
  982. $output .= "{$section_props}['index_prev'] = {$section_props}['index'] - {$section_props}['step'];\n";
  983. $output .= "{$section_props}['index_next'] = {$section_props}['index'] + {$section_props}['step'];\n";
  984. $output .= "{$section_props}['first'] = ({$section_props}['iteration'] == 1);\n";
  985. $output .= "{$section_props}['last'] = ({$section_props}['iteration'] == {$section_props}['total']);\n";
  986. $output .= "?>";
  987. return $output;
  988. }
  989. /**
  990. * Compile {foreach ...} tag.
  991. *
  992. * @param string $tag_args
  993. * @return string
  994. */
  995. function _compile_foreach_start($tag_args)
  996. {
  997. $attrs = $this->_parse_attrs($tag_args);
  998. $arg_list = array();
  999. if (empty($attrs['from'])) {
  1000. return $this->_syntax_error("foreach: missing 'from' attribute", E_USER_ERROR, __FILE__, __LINE__);
  1001. }
  1002. $from = $attrs['from'];
  1003. if (empty($attrs['item'])) {
  1004. return $this->_syntax_error("foreach: missing 'item' attribute", E_USER_ERROR, __FILE__, __LINE__);
  1005. }
  1006. $item = $this->_dequote($attrs['item']);
  1007. if (!preg_match('~^\w+$~', $item)) {
  1008. return $this->_syntax_error("'foreach: 'item' must be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
  1009. }
  1010. if (isset($attrs['key'])) {
  1011. $key = $this->_dequote($attrs['key']);
  1012. if (!preg_match('~^\w+$~', $key)) {
  1013. return $this->_syntax_error("foreach: 'key' must to be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
  1014. }
  1015. $key_part = "\$this->_tpl_vars['$key'] => ";
  1016. } else {
  1017. $key = null;
  1018. $key_part = '';
  1019. }
  1020. if (isset($attrs['name'])) {
  1021. $name = $attrs['name'];
  1022. } else {
  1023. $name = null;
  1024. }
  1025. $output = '<?php ';
  1026. $output .= "\$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array'); }";
  1027. if (isset($name)) {
  1028. $foreach_props = "\$this->_foreach[$name]";
  1029. $output .= "{$foreach_props} = array('total' => count(\$_from), 'iteration' => 0);\n";
  1030. $output .= "if ({$foreach_props}['total'] > 0):\n";
  1031. $output .= " foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
  1032. $output .= " {$foreach_props}['iteration']++;\n";
  1033. } else {
  1034. $output .= "if (count(\$_from)):\n";
  1035. $output .= " foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
  1036. }
  1037. $output .= '?>';
  1038. return $output;
  1039. }
  1040. /**
  1041. * Compile {capture} .. {/capture} tags
  1042. *
  1043. * @param boolean $start true if this is the {capture} tag
  1044. * @param string $tag_args
  1045. * @return string
  1046. */
  1047. function _compile_capture_tag($start, $tag_args = '')
  1048. {
  1049. $attrs = $this->_parse_attrs($tag_args);
  1050. if ($start) {
  1051. if (isset($attrs['name']))
  1052. $buffer = $attrs['name'];
  1053. else
  1054. $buffer = "'default'";
  1055. if (isset($attrs['assign']))
  1056. $assign = $attrs['assign'];
  1057. else
  1058. $assign = null;
  1059. $output = "<?php ob_start(); ?>";
  1060. $this->_capture_stack[] = array($buffer, $assign);
  1061. } else {
  1062. list($buffer, $assign) = array_pop($this->_capture_stack);
  1063. $output = "<?php \$this->_smarty_vars['capture'][$buffer] = ob_get_contents(); ";
  1064. if (isset($assign)) {
  1065. $output .= " \$this->assign($assign, ob_get_contents());";
  1066. }
  1067. $output .= "ob_end_clean(); ?>";
  1068. }
  1069. return $output;
  1070. }
  1071. /**
  1072. * Compile {if ...} tag
  1073. *
  1074. * @param string $tag_args
  1075. * @param boolean $elseif if true, uses elseif instead of if
  1076. * @return string
  1077. */
  1078. function _compile_if_tag($tag_args, $elseif = false)
  1079. {
  1080. /* Tokenize args for 'if' tag. */
  1081. preg_match_all('~(?>
  1082. ' . $this->_obj_call_regexp . '(?:' . $this->_mod_regexp . '*)? | # valid object call
  1083. ' . $this->_var_regexp . '(?:' . $this->_mod_regexp . '*)? | # var or quoted string
  1084. \-?0[xX][0-9a-fA-F]+|\-?\d+(?:\.\d+)?|\.\d+|!==|===|==|!=|<>|<<|>>|<=|>=|\&\&|\|\||\(|\)|,|\!|\^|=|\&|\~|<|>|\||\%|\+|\-|\/|\*|\@ | # valid non-word token
  1085. \b\w+\b | # valid word token
  1086. \S+ # anything else
  1087. )~x', $tag_args, $match);
  1088. $tokens = $match[0];
  1089. if(empty($tokens)) {
  1090. $_error_msg = $elseif ? "'elseif'" : "'if'";
  1091. $_error_msg .= ' statement requires arguments';
  1092. $this->_syntax_error($_error_msg, E_USER_ERROR, __FILE__, __LINE__);
  1093. }
  1094. // make sure we have balanced parenthesis
  1095. $token_count = array_count_values($tokens);
  1096. if(isset($token_count['(']) && $token_count['('] != $token_count[')']) {
  1097. $this->_syntax_error("unbalanced parenthesis in if statement", E_USER_ERROR, __FILE__, __LINE__);
  1098. }
  1099. $is_arg_stack = array();
  1100. for ($i = 0; $i < count($tokens); $i++) {
  1101. $token = &$tokens[$i];
  1102. switch (strtolower($token)) {
  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. case ')':
  1123. case ',':
  1124. case '+':
  1125. case '-':
  1126. case '*':
  1127. case '/':
  1128. case '@':
  1129. break;
  1130. case 'eq':
  1131. $token = '==';
  1132. break;
  1133. case 'ne':
  1134. case 'neq':
  1135. $token = '!=';
  1136. break;
  1137. case 'lt':
  1138. $token = '<';
  1139. break;
  1140. case 'le':
  1141. case 'lte':
  1142. $token = '<=';
  1143. break;
  1144. case 'gt':
  1145. $token = '>';
  1146. break;
  1147. case 'ge':
  1148. case 'gte':
  1149. $token = '>=';
  1150. break;
  1151. case 'and':
  1152. $token = '&&';
  1153. break;
  1154. case 'or':
  1155. $token = '||';
  1156. break;
  1157. case 'not':
  1158. $token = '!';
  1159. break;
  1160. case 'mod':
  1161. $token = '%';
  1162. break;
  1163. case '(':
  1164. array_push($is_arg_stack, $i);
  1165. break;
  1166. case 'is':
  1167. /* If last token was a ')', we operate on the parenthesized
  1168. expression. The start of the expression is on the stack.
  1169. Otherwise, we operate on the last encountered token. */
  1170. if ($tokens[$i-1] == ')')
  1171. $is_arg_start = array_pop($is_arg_stack);
  1172. else
  1173. $is_arg_start = $i-1;
  1174. /* Construct the argument for 'is' expression, so it knows
  1175. what to operate on. */
  1176. $is_arg = implode(' ', array_slice($tokens, $is_arg_start, $i - $is_arg_start));
  1177. /* Pass all tokens from next one until the end to the
  1178. 'is' expression parsing function. The function will
  1179. return modified tokens, where the first one is the result
  1180. of the 'is' expression and the rest are the tokens it
  1181. didn't touch. */
  1182. $new_tokens = $this->_parse_is_expr($is_arg, array_slice($tokens, $i+1));
  1183. /* Replace the old tokens with the new ones. */
  1184. array_splice($tokens, $is_arg_start, count($tokens), $new_tokens);
  1185. /* Adjust argument start so that it won't change from the
  1186. current position for the next iteration. */
  1187. $i = $is_arg_start;
  1188. break;
  1189. default:
  1190. if(preg_match('~^' . $this->_func_regexp . '$~', $token) ) {
  1191. // function call
  1192. if($this->security &&
  1193. !in_array($token, $this->security_settings['IF_FUNCS'])) {
  1194. $this->_syntax_error("(secure mode) '$token' not allowed in if statement", E_USER_ERROR, __FILE__, __LINE__);
  1195. }
  1196. } elseif(preg_match('~^' . $this->_var_regexp . '$~', $token) && (strpos('+-*/^%&|', substr($token, -1)) === false) && isset($tokens[$i+1]) && $tokens[$i+1] == '(') {
  1197. // variable function call
  1198. $this->_syntax_error("variable function call '$token' not allowed in if statement", E_USER_ERROR, __FILE__, __LINE__);
  1199. } elseif(preg_match('~^' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '(?:' . $this->_mod_regexp . '*)$~', $token)) {
  1200. // object or variable
  1201. $token = $this->_parse_var_props($token);
  1202. } elseif(is_numeric($token)) {
  1203. // number, skip it
  1204. } else {
  1205. $this->_syntax_error("unidentified token '$token'", E_USER_ERROR, __FILE__, __LINE__);
  1206. }
  1207. break;
  1208. }
  1209. }
  1210. if ($elseif)
  1211. return '<?php elseif ('.implode(' ', $tokens).'): ?>';
  1212. else
  1213. return '<?php if ('.implode(' ', $tokens).'): ?>';
  1214. }
  1215. function _compile_arg_list($type, $name, $attrs, &$cache_code) {
  1216. $arg_list = array();
  1217. if (isset($type) && isset($name)
  1218. && isset($this->_plugins[$type])
  1219. && isset($this->_plugins[$type][$name])
  1220. && empty($this->_plugins[$type][$name][4])
  1221. && is_array($this->_plugins[$type][$name][5])
  1222. ) {
  1223. /* we have a list of parameters that should be cached */
  1224. $_cache_attrs = $this->_plugins[$type][$name][5];
  1225. $_count = $this->_cache_attrs_count++;
  1226. $cache_code = "\$_cache_attrs =& \$this->_smarty_cache_attrs('$this->_cache_serial','$_count');";
  1227. } else {
  1228. /* no parameters are cached */
  1229. $_cache_attrs = null;
  1230. }
  1231. foreach ($attrs as $arg_name => $arg_value) {
  1232. if (is_bool($arg_value))
  1233. $arg_value = $arg_value ? 'true' : 'false';
  1234. if (is_null($arg_value))
  1235. $arg_value = 'null';
  1236. if ($_cache_attrs && in_array($arg_name, $_cache_attrs)) {
  1237. $arg_list[] = "'$arg_name' => (\$this->_cache_including) ? \$_cache_attrs['$arg_name'] : (\$_cache_attrs['$arg_name']=$arg_value)";
  1238. } else {
  1239. $arg_list[] = "'$arg_name' => $arg_value";
  1240. }
  1241. }
  1242. return $arg_list;
  1243. }
  1244. /**
  1245. * Parse is expression
  1246. *
  1247. * @param string $is_arg
  1248. * @param array $tokens
  1249. * @return array
  1250. */
  1251. function _parse_is_expr($is_arg, $tokens)
  1252. {
  1253. $expr_end = 0;
  1254. $negate_expr = false;
  1255. if (($first_token = array_shift($tokens)) == 'not') {
  1256. $negate_expr = true;
  1257. $expr_type = array_shift($tokens);
  1258. } else
  1259. $expr_type = $first_token;
  1260. switch ($expr_type) {
  1261. case 'even':
  1262. if (isset($tokens[$expr_end]) && $tokens[$expr_end] == 'by') {
  1263. $expr_end++;
  1264. $expr_arg = $tokens[$expr_end++];
  1265. $expr = "!(1 & ($is_arg / " . $this->_parse_var_props($expr_arg) . "))";
  1266. } else
  1267. $expr = "!(1 & $is_arg)";
  1268. break;
  1269. case 'odd':
  1270. if (isset($tokens[$expr_end]) && $tokens[$expr_end] == 'by') {
  1271. $expr_end++;
  1272. $expr_arg = $tokens[$expr_end++];
  1273. $expr = "(1 & ($is_arg / " . $this->_parse_var_props($expr_arg) . "))";
  1274. } else
  1275. $expr = "(1 & $is_arg)";
  1276. break;
  1277. case 'div':
  1278. if (@$tokens[$expr_end] == 'by') {
  1279. $expr_end++;
  1280. $expr_arg = $tokens[$expr_end++];
  1281. $expr = "!($is_arg % " . $this->_parse_var_props($expr_arg) . ")";
  1282. } else {
  1283. $this->_syntax_error("expecting 'by' after 'div'", E_USER_ERROR, __FILE__, __LINE__);
  1284. }
  1285. break;
  1286. default:
  1287. $this->_syntax_error("unknown 'is' expression - '$expr_type'", E_USER_ERROR, __FILE__, __LINE__);
  1288. break;
  1289. }
  1290. if ($negate_expr) {
  1291. $expr = "!($expr)";
  1292. }
  1293. array_splice($tokens, 0, $expr_end, $expr);
  1294. return $tokens;
  1295. }
  1296. /**
  1297. * Parse attribute string
  1298. *
  1299. * @param string $tag_args
  1300. * @return array
  1301. */
  1302. function _parse_attrs($tag_args)
  1303. {
  1304. /* Tokenize tag attributes. */
  1305. preg_match_all('~(?:' . $this->_obj_call_regexp . '|' . $this->_qstr_regexp . ' | (?>[^"\'=\s]+)
  1306. )+ |
  1307. [=]
  1308. ~x', $tag_args, $match);
  1309. $tokens = $match[0];
  1310. $attrs = array();
  1311. /* Parse state:
  1312. 0 - expecting attribute name
  1313. 1 - expecting '='
  1314. 2 - expecting attribute value (not '=') */
  1315. $state = 0;
  1316. foreach ($tokens as $token) {
  1317. switch ($state) {
  1318. case 0:
  1319. /* If the token is a valid identifier, we set attribute name
  1320. and go to state 1. */
  1321. if (preg_match('~^\w+$~', $token)) {
  1322. $attr_name = $token;
  1323. $state = 1;
  1324. } else
  1325. $this->_syntax_error("invalid attribute name: '$token'", E_USER_ERROR, __FILE__, __LINE__);
  1326. break;
  1327. case 1:
  1328. /* If the token is '=', then we go to state 2. */
  1329. if ($token == '=') {
  1330. $state = 2;
  1331. } else
  1332. $this->_syntax_error("expecting '=' after attribute name '$last_token'", E_USER_ERROR, __FILE__, __LINE__);
  1333. break;
  1334. case 2:
  1335. /* If token is not '=', we set the attribute value and go to
  1336. state 0. */
  1337. if ($token != '=') {
  1338. /* We booleanize the token if it's a non-quoted possible
  1339. boolean value. */
  1340. if (preg_match('~^(on|yes|true)$~', $token)) {
  1341. $token = 'true';
  1342. } else if (preg_match('~^(off|no|false)$~', $token)) {
  1343. $token = 'false';
  1344. } else if ($token == 'null') {
  1345. $token = 'null';
  1346. } else if (preg_match('~^' . $this->_num_const_regexp . '|0[xX][0-9a-fA-F]+$~', $token)) {
  1347. /* treat integer literally */
  1348. } else if (!preg_match('~^' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '(?:' . $this->_mod_regexp . ')*$~', $token)) {
  1349. /* treat as a string, double-quote it escaping quotes */
  1350. $token = '"'.addslashes($token).'"';
  1351. }
  1352. $attrs[$attr_name] = $token;
  1353. $state = 0;
  1354. } else
  1355. $this->_syntax_error("'=' cannot be an attribute value", E_USER_ERROR, __FILE__, __LINE__);
  1356. break;
  1357. }
  1358. $last_token = $token;
  1359. }
  1360. if($state != 0) {
  1361. if($state == 1) {
  1362. $this->_syntax_error("expecting '=' after attribute name '$last_token'", E_USER_ERROR, __FILE__, __LINE__);
  1363. } else {
  1364. $this->_syntax_error("missing attribute value", E_USER_ERROR, __FILE__, __LINE__);
  1365. }
  1366. }
  1367. $this->_parse_vars_props($attrs);
  1368. return $attrs;
  1369. }
  1370. /**
  1371. * compile multiple variables and section properties tokens into
  1372. * PHP code
  1373. *
  1374. * @param array $tokens
  1375. */
  1376. function _parse_vars_props(&$tokens)
  1377. {
  1378. foreach($tokens as $key => $val) {
  1379. $tokens[$key] = $this->_parse_var_props($val);
  1380. }
  1381. }
  1382. /**
  1383. * compile single variable and section properties token into
  1384. * PHP code
  1385. *
  1386. * @param string $val
  1387. * @param string $tag_attrs
  1388. * @return string
  1389. */
  1390. function _parse_var_props($val)
  1391. {
  1392. $val = trim($val);
  1393. if(preg_match('~^(' . $this->_obj_call_regexp . '|' . $this->_dvar_regexp . ')(' . $this->_mod_regexp . '*)$~', $val, $match)) {
  1394. // $ variable or object
  1395. $return = $this->_parse_var($match[1]);
  1396. $modifiers = $match[2];
  1397. if (!empty($this->default_modifiers) && !preg_match('~(^|\|)smarty:nodefaults($|\|)~',$modifiers)) {
  1398. $_default_mod_string = implode('|',(array)$this->default_modifiers);
  1399. $modifiers = empty($modifiers) ? $_default_mod_string : $_default_mod_string . '|' . $modifiers;
  1400. }
  1401. $this->_parse_modifiers($return, $modifiers);
  1402. return $return;
  1403. } elseif (preg_match('~^' . $this->_db_qstr_regexp . '(?:' . $this->_mod_regexp . '*)$~', $val)) {
  1404. // double quoted text
  1405. preg_match('~^(' . $this->_db_qstr_regexp . ')('. $this->_mod_regexp . '*)$~', $val, $match);
  1406. $return = $this->_expand_quoted_text($match[1]);
  1407. if($match[2] != '') {
  1408. $this->_parse_modifiers($return, $match[2]);
  1409. }
  1410. return $return;
  1411. }
  1412. elseif(preg_match('~^' . $this->_num_const_regexp . '(?:' . $this->_mod_regexp . '*)$~', $val)) {
  1413. // numerical constant
  1414. preg_match('~^(' . $this->_num_const_regexp . ')('. $this->_mod_regexp . '*)$~', $val, $match);
  1415. if($match[2] != '') {
  1416. $this->_parse_modifiers($match[1], $match[2]);
  1417. return $match[1];
  1418. }
  1419. }
  1420. elseif(preg_match('~^' . $this->_si_qstr_regexp . '(?:' . $this->_mod_regexp . '*)$~', $val)) {
  1421. // single quoted text
  1422. preg_match('~^(' . $this->_si_qstr_regexp . ')('. $this->_mod_regexp . '*)$~', $val, $match);
  1423. if($match[2] != '') {
  1424. $this->_parse_modifiers($match[1], $match[2]);
  1425. return $match[1];
  1426. }
  1427. }
  1428. elseif(preg_match('~^' . $this->_cvar_regexp . '(?:' . $this->_mod_regexp . '*)$~', $val)) {
  1429. // config var
  1430. return $this->_parse_conf_var($val);
  1431. }
  1432. elseif(preg_match('~^' . $this->_svar_regexp . '(?:' . $this->_mod_regexp . '*)$~', $val)) {
  1433. // section var
  1434. return $this->_parse_section_prop($val);
  1435. }
  1436. elseif(!in_array($val, $this->_permitted_tokens) && !is_numeric($val)) {
  1437. // literal string
  1438. return $this->_expand_quoted_text('"' . strtr($val, array('\\' => '\\\\', '"' => '\\"')) .'"');
  1439. }
  1440. return $val;
  1441. }
  1442. /**
  1443. * expand quoted text with embedded variables
  1444. *
  1445. * @param string $var_expr
  1446. * @return string
  1447. */
  1448. function _expand_quoted_text($var_expr)
  1449. {
  1450. // if contains unescaped $, expand it
  1451. if(preg_match_all('~(?:\`(?<!\\\\)\$' . $this->_dvar_guts_regexp . '(?:' . $this->_obj_ext_regexp . ')*\`)|(?:(?<!\\\\)\$\w+(\[[a-zA-Z0-9]+\])*)~', $var_expr, $_match)) {
  1452. $_match = $_match[0];
  1453. $_replace = array();
  1454. foreach($_match as $_var) {
  1455. $_replace[$_var] = '".(' . $this->_parse_var(str_replace('`','',$_var)) . ')."';
  1456. }
  1457. $var_expr = strtr($var_expr, $_replace);
  1458. $_return = preg_replace('~\.""|(?<!\\\\)""\.~', '', $var_expr);
  1459. } else {
  1460. $_return = $var_expr;
  1461. }
  1462. // replace double quoted literal string with single quotes
  1463. $_return = preg_replace('~^"([\s\w]+)"$~',"'\\1'",$_return);
  1464. return $_return;
  1465. }
  1466. /**
  1467. * parse variable expression into PHP code
  1468. *
  1469. * @param string $var_expr
  1470. * @param string $output
  1471. * @return string
  1472. */
  1473. function _parse_var($var_expr)
  1474. {
  1475. $_has_math = false;
  1476. $_math_vars = preg_split('~('.$this->_dvar_math_regexp.'|'.$this->_qstr_regexp.')~', $var_expr, -1, PREG_SPLIT_DELIM_CAPTURE);
  1477. if(count($_math_vars) > 1) {
  1478. $_first_var = "";
  1479. $_complete_var = "";
  1480. $_output = "";
  1481. // simple check if there is any math, to stop recursion (due to modifiers with "xx % yy" as parameter)
  1482. foreach($_math_vars as $_k => $_math_var) {
  1483. $_math_var = $_math_vars[$_k];
  1484. if(!empty($_math_var) || is_numeric($_math_var)) {
  1485. // hit a math operator, so process the stuff which came before it
  1486. if(preg_match('~^' . $this->_dvar_math_regexp . '$~', $_math_var)) {
  1487. $_has_math = true;
  1488. if(!empty($_complete_var) || is_numeric($_complete_var)) {
  1489. $_output .= $this->_parse_var($_complete_var);
  1490. }
  1491. // just output the math operator to php
  1492. $_output .= $_math_var;
  1493. if(empty($_first_var))
  1494. $_first_var = $_complete_var;
  1495. $_complete_var = "";
  1496. } else {
  1497. $_complete_var .= $_math_var;
  1498. }
  1499. }
  1500. }
  1501. if($_has_math) {
  1502. if(!empty($_complete_var) || is_numeric($_complete_var))
  1503. $_output .= $this->_parse_var($_complete_var);
  1504. // get the modifiers working (only the last var from math + modifier is left)
  1505. $var_expr = $_complete_var;
  1506. }
  1507. }
  1508. // prevent cutting of first digit in the number (we _definitly_ got a number if the first char is a digit)
  1509. if(is_numeric(substr($var_expr, 0, 1)))
  1510. $_var_ref = $var_expr;
  1511. else
  1512. $_var_ref = substr($var_expr, 1);
  1513. if(!$_has_math) {
  1514. // get [foo] and .foo and ->foo and (...) pieces
  1515. preg_match_all('~(?:^\w+)|' . $this->_obj_params_regexp . '|(?:' . $this->_var_bracket_regexp . ')|->\$?\w+|\.\$?\w+|\S+~', $_var_ref, $match);
  1516. $_indexes = $match[0];
  1517. $_var_name = array_shift($_indexes);
  1518. /* Handle $smarty.* variable references as a special case. */
  1519. if ($_var_name == 'smarty') {
  1520. /*
  1521. * If the reference could be compiled, use the compiled output;
  1522. * otherwise, fall back on the $smarty variable generated at
  1523. * run-time.
  1524. */
  1525. if (($smarty_ref = $this->_compile_smarty_ref($_indexes)) !== null) {
  1526. $_output = $smarty_ref;
  1527. } else {
  1528. $_var_name = substr(array_shift($_indexes), 1);
  1529. $_output = "\$this->_smarty_vars['$_var_name']";
  1530. }
  1531. } elseif(is_numeric($_var_name) && is_numeric(substr($var_expr, 0, 1))) {
  1532. // because . is the operator for accessing arrays thru inidizes we need to put it together again for floating point numbers
  1533. if(count($_indexes) > 0)
  1534. {
  1535. $_var_name .= implode("", $_indexes);
  1536. $_indexes = array();
  1537. }
  1538. $_output = $_var_name;
  1539. } else {
  1540. $_output = "\$this->_tpl_vars['$_var_name']";
  1541. }
  1542. foreach ($_indexes as $_index) {
  1543. if (substr($_index, 0, 1) == '[') {
  1544. $_index = substr($_index, 1, -1);
  1545. if (is_numeric($_index)) {
  1546. $_output .= "[$_index]";
  1547. } elseif (substr($_index, 0, 1) == '$') {
  1548. if (strpos($_index, '.') !== false) {
  1549. $_output .= '[' . $this->_parse_var($_index) . ']';
  1550. } else {
  1551. $_output .= "[\$this->_tpl_vars['" . substr($_index, 1) . "']]";
  1552. }
  1553. } else {
  1554. $_var_parts = explode('.', $_index);
  1555. $_var_section = $_var_parts[0];
  1556. $_var_section_prop = isset($_var_parts[1]) ? $_var_parts[1] : 'index';
  1557. $_output .= "[\$this->_sections['$_var_section']['$_var_section_prop']]";
  1558. }
  1559. } else if (substr($_index, 0, 1) == '.') {
  1560. if (substr($_index, 1, 1) == '$')
  1561. $_output .= "[\$this->_tpl_vars['" . substr($_index, 2) . "']]";
  1562. else
  1563. $_output .= "['" . substr($_index, 1) . "']";
  1564. } else if (substr($_index,0,2) == '->') {
  1565. if(substr($_index,2,2) == '__') {
  1566. $this->_syntax_error('call to internal object members is not allowed', E_USER_ERROR, __FILE__, __LINE__);
  1567. } elseif($this->security && substr($_index, 2, 1) == '_') {
  1568. $this->_syntax_error('(secure) call to private object member is not allowed', E_USER_ERROR, __FILE__, __LINE__);
  1569. } elseif (substr($_index, 2, 1) == '$') {
  1570. if ($this->security) {
  1571. $this->_syntax_error('(secure) call to dynamic object member is not allowed', E_USER_ERROR, __FILE__, __LINE__);
  1572. } else {
  1573. $_output .= '->{(($_var=$this->_tpl_vars[\''.substr($_index,3).'\']) && substr($_var,0,2)!=\'__\') ? $_var : $this->trigger_error("cannot access property \\"$_var\\"")}';
  1574. }
  1575. } else {
  1576. $_output .= $_index;
  1577. }
  1578. } elseif (substr($_index, 0, 1) == '(') {
  1579. $_index = $this->_parse_parenth_args($_index);
  1580. $_output .= $_index;
  1581. } else {
  1582. $_output .= $_index;
  1583. }
  1584. }
  1585. }
  1586. return $_output;
  1587. }
  1588. /**
  1589. * parse arguments in function call parenthesis
  1590. *
  1591. * @param string $parenth_args
  1592. * @return string
  1593. */
  1594. function _parse_parenth_args($parenth_args)
  1595. {
  1596. preg_match_all('~' . $this->_param_regexp . '~',$parenth_args, $match);
  1597. $orig_vals = $match = $match[0];
  1598. $this->_parse_vars_props($match);
  1599. $replace = array();
  1600. for ($i = 0, $count = count($match); $i < $count; $i++) {
  1601. $replace[$orig_vals[$i]] = $match[$i];
  1602. }
  1603. return strtr($parenth_args, $replace);
  1604. }
  1605. /**
  1606. * parse configuration variable expression into PHP code
  1607. *
  1608. * @param string $conf_var_expr
  1609. */
  1610. function _parse_conf_var($conf_var_expr)
  1611. {
  1612. $parts = explode('|', $conf_var_expr, 2);
  1613. $var_ref = $parts[0];
  1614. $modifiers = isset($parts[1]) ? $parts[1] : '';
  1615. $var_name = substr($var_ref, 1, -1);
  1616. $output = "\$this->_config[0]['vars']['$var_name']";
  1617. $this->_parse_modifiers($output, $modifiers);
  1618. return $output;
  1619. }
  1620. /**
  1621. * parse section property expression into PHP code
  1622. *
  1623. * @param string $section_prop_expr
  1624. * @return string
  1625. */
  1626. function _parse_section_prop($section_prop_expr)
  1627. {
  1628. $parts = explode('|', $section_prop_expr, 2);
  1629. $var_ref = $parts[0];
  1630. $modifiers = isset($parts[1]) ? $parts[1] : '';
  1631. preg_match('!%(\w+)\.(\w+)%!', $var_ref, $match);
  1632. $section_name = $match[1];
  1633. $prop_name = $match[2];
  1634. $output = "\$this->_sections['$section_name']['$prop_name']";
  1635. $this->_parse_modifiers($output, $modifiers);
  1636. return $output;
  1637. }
  1638. /**
  1639. * parse modifier chain into PHP code
  1640. *
  1641. * sets $output to parsed modified chain
  1642. * @param string $output
  1643. * @param string $modifier_string
  1644. */
  1645. function _parse_modifiers(&$output, $modifier_string)
  1646. {
  1647. preg_match_all('~\|(@?\w+)((?>:(?:'. $this->_qstr_regexp . '|[^|]+))*)~', '|' . $modifier_string, $_match);
  1648. list(, $_modifiers, $modifier_arg_strings) = $_match;
  1649. for ($_i = 0, $_for_max = count($_modifiers); $_i < $_for_max; $_i++) {
  1650. $_modifier_name = $_modifiers[$_i];
  1651. if($_modifier_name == 'smarty') {
  1652. // skip smarty modifier
  1653. continue;
  1654. }
  1655. preg_match_all('~:(' . $this->_qstr_regexp . '|[^:]+)~', $modifier_arg_strings[$_i], $_match);
  1656. $_modifier_args = $_match[1];
  1657. if (substr($_modifier_name, 0, 1) == '@') {
  1658. $_map_array = false;
  1659. $_modifier_name = substr($_modifier_name, 1);
  1660. } else {
  1661. $_map_array = true;
  1662. }
  1663. if (empty($this->_plugins['modifier'][$_modifier_name])
  1664. && !$this->_get_plugin_filepath('modifier', $_modifier_name)
  1665. && function_exists($_modifier_name)) {
  1666. if ($this->security && !in_array($_modifier_name, $this->security_settings['MODIFIER_FUNCS'])) {
  1667. $this->_trigger_fatal_error("[plugin] (secure mode) modifier '$_modifier_name' is not allowed" , $this->_current_file, $this->_current_line_no, __FILE__, __LINE__);
  1668. } else {
  1669. $this->_plugins['modifier'][$_modifier_name] = array($_modifier_name, null, null, false);
  1670. }
  1671. }
  1672. $this->_add_plugin('modifier', $_modifier_name);
  1673. $this->_parse_vars_props($_modifier_args);
  1674. if($_modifier_name == 'default') {
  1675. // supress notifications of default modifier vars and args
  1676. if(substr($output, 0, 1) == '$') {
  1677. $output = '@' . $output;
  1678. }
  1679. if(isset($_modifier_args[0]) && substr($_modifier_args[0], 0, 1) == '$') {
  1680. $_modifier_args[0] = '@' . $_modifier_args[0];
  1681. }
  1682. }
  1683. if (count($_modifier_args) > 0)
  1684. $_modifier_args = ', '.implode(', ', $_modifier_args);
  1685. else
  1686. $_modifier_args = '';
  1687. if ($_map_array) {
  1688. $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))";
  1689. } else {
  1690. $output = $this->_compile_plugin_call('modifier', $_modifier_name)."($output$_modifier_args)";
  1691. }
  1692. }
  1693. }
  1694. /**
  1695. * add plugin
  1696. *
  1697. * @param string $type
  1698. * @param string $name
  1699. * @param boolean? $delayed_loading
  1700. */
  1701. function _add_plugin($type, $name, $delayed_loading = null)
  1702. {
  1703. if (!isset($this->_plugin_info[$type])) {
  1704. $this->_plugin_info[$type] = array();
  1705. }
  1706. if (!isset($this->_plugin_info[$type][$name])) {
  1707. $this->_plugin_info[$type][$name] = array($this->_current_file,
  1708. $this->_current_line_no,
  1709. $delayed_loading);
  1710. }
  1711. }
  1712. /**
  1713. * Compiles references of type $smarty.foo
  1714. *
  1715. * @param string $indexes
  1716. * @return string
  1717. */
  1718. function _compile_smarty_ref(&$indexes)
  1719. {
  1720. /* Extract the reference name. */
  1721. $_ref = substr($indexes[0], 1);
  1722. foreach($indexes as $_index_no=>$_index) {
  1723. if (substr($_index, 0, 1) != '.' && $_index_no<2 || !preg_match('~^(\.|\[|->)~', $_index)) {
  1724. $this->_syntax_error('$smarty' . implode('', array_slice($indexes, 0, 2)) . ' is an invalid reference', E_USER_ERROR, __FILE__, __LINE__);
  1725. }
  1726. }
  1727. switch ($_ref) {
  1728. case 'now':
  1729. $compiled_ref = 'time()';
  1730. $_max_index = 1;
  1731. break;
  1732. case 'foreach':
  1733. array_shift($indexes);
  1734. $_var = $this->_parse_var_props(substr($indexes[0], 1));
  1735. $_propname = substr($indexes[1], 1);
  1736. $_max_index = 1;
  1737. switch ($_propname) {
  1738. case 'index':
  1739. array_shift($indexes);
  1740. $compiled_ref = "(\$this->_foreach[$_var]['iteration']-1)";
  1741. break;
  1742. case 'first':
  1743. array_shift($indexes);
  1744. $compiled_ref = "(\$this->_foreach[$_var]['iteration'] <= 1)";
  1745. break;
  1746. case 'last':
  1747. array_shift($indexes);
  1748. $compiled_ref = "(\$this->_foreach[$_var]['iteration'] == \$this->_foreach[$_var]['total'])";
  1749. break;
  1750. case 'show':
  1751. array_shift($indexes);
  1752. $compiled_ref = "(\$this->_foreach[$_var]['total'] > 0)";
  1753. break;
  1754. default:
  1755. unset($_max_index);
  1756. $compiled_ref = "\$this->_foreach[$_var]";
  1757. }
  1758. break;
  1759. case 'section':
  1760. array_shift($indexes);
  1761. $_var = $this->_parse_var_props(substr($indexes[0], 1));
  1762. $compiled_ref = "\$this->_sections[$_var]";
  1763. break;
  1764. case 'get':
  1765. $compiled_ref = ($this->request_use_auto_globals) ? '$_GET' : "\$GLOBALS['HTTP_GET_VARS']";
  1766. break;
  1767. case 'post':
  1768. $compiled_ref = ($this->request_use_auto_globals) ? '$_POST' : "\$GLOBALS['HTTP_POST_VARS']";
  1769. break;
  1770. case 'cookies':
  1771. $compiled_ref = ($this->request_use_auto_globals) ? '$_COOKIE' : "\$GLOBALS['HTTP_COOKIE_VARS']";
  1772. break;
  1773. case 'env':
  1774. $compiled_ref = ($this->request_use_auto_globals) ? '$_ENV' : "\$GLOBALS['HTTP_ENV_VARS']";
  1775. break;
  1776. case 'server':
  1777. $compiled_ref = ($this->request_use_auto_globals) ? '$_SERVER' : "\$GLOBALS['HTTP_SERVER_VARS']";
  1778. break;
  1779. case 'session':
  1780. $compiled_ref = ($this->request_use_auto_globals) ? '$_SESSION' : "\$GLOBALS['HTTP_SESSION_VARS']";
  1781. break;
  1782. /*
  1783. * These cases are handled either at run-time or elsewhere in the
  1784. * compiler.
  1785. */
  1786. case 'request':
  1787. if ($this->request_use_auto_globals) {
  1788. $compiled_ref = '$_REQUEST';
  1789. break;
  1790. } else {
  1791. $this->_init_smarty_vars = true;
  1792. }
  1793. return null;
  1794. case 'capture':
  1795. return null;
  1796. case 'template':
  1797. $compiled_ref = "'$this->_current_file'";
  1798. $_max_index = 1;
  1799. break;
  1800. case 'version':
  1801. $compiled_ref = "'$this->_version'";
  1802. $_max_index = 1;
  1803. break;
  1804. case 'const':
  1805. if ($this->security && !$this->security_settings['ALLOW_CONSTANTS']) {
  1806. $this->_syntax_error("(secure mode) constants not permitted",
  1807. E_USER_WARNING, __FILE__, __LINE__);
  1808. return;
  1809. }
  1810. array_shift($indexes);
  1811. if (preg_match('!^\.\w+$!', $indexes[0])) {
  1812. $compiled_ref = '@' . substr($indexes[0], 1);
  1813. } else {
  1814. $_val = $this->_parse_var_props(substr($indexes[0], 1));
  1815. $compiled_ref = '@constant(' . $_val . ')';
  1816. }
  1817. $_max_index = 1;
  1818. break;
  1819. case 'config':
  1820. $compiled_ref = "\$this->_config[0]['vars']";
  1821. $_max_index = 3;
  1822. break;
  1823. case 'ldelim':
  1824. $compiled_ref = "'$this->left_delimiter'";
  1825. break;
  1826. case 'rdelim':
  1827. $compiled_ref = "'$this->right_delimiter'";
  1828. break;
  1829. default:
  1830. $this->_syntax_error('$smarty.' . $_ref . ' is an unknown reference', E_USER_ERROR, __FILE__, __LINE__);
  1831. break;
  1832. }
  1833. if (isset($_max_index) && count($indexes) > $_max_index) {
  1834. $this->_syntax_error('$smarty' . implode('', $indexes) .' is an invalid reference', E_USER_ERROR, __FILE__, __LINE__);
  1835. }
  1836. array_shift($indexes);
  1837. return $compiled_ref;
  1838. }
  1839. /**
  1840. * compiles call to plugin of type $type with name $name
  1841. * returns a string containing the function-name or method call
  1842. * without the paramter-list that would have follow to make the
  1843. * call valid php-syntax
  1844. *
  1845. * @param string $type
  1846. * @param string $name
  1847. * @return string
  1848. */
  1849. function _compile_plugin_call($type, $name) {
  1850. if (isset($this->_plugins[$type][$name])) {
  1851. /* plugin loaded */
  1852. if (is_array($this->_plugins[$type][$name][0])) {
  1853. return ((is_object($this->_plugins[$type][$name][0][0])) ?
  1854. "\$this->_plugins['$type']['$name'][0][0]->" /* method callback */
  1855. : (string)($this->_plugins[$type][$name][0][0]).'::' /* class callback */
  1856. ). $this->_plugins[$type][$name][0][1];
  1857. } else {
  1858. /* function callback */
  1859. return $this->_plugins[$type][$name][0];
  1860. }
  1861. } else {
  1862. /* plugin not loaded -> auto-loadable-plugin */
  1863. return 'smarty_'.$type.'_'.$name;
  1864. }
  1865. }
  1866. /**
  1867. * load pre- and post-filters
  1868. */
  1869. function _load_filters()
  1870. {
  1871. if (count($this->_plugins['prefilter']) > 0) {
  1872. foreach ($this->_plugins['prefilter'] as $filter_name => $prefilter) {
  1873. if ($prefilter === false) {
  1874. unset($this->_plugins['prefilter'][$filter_name]);
  1875. $_params = array('plugins' => array(array('prefilter', $filter_name, null, null, false)));
  1876. require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');
  1877. smarty_core_load_plugins($_params, $this);
  1878. }
  1879. }
  1880. }
  1881. if (count($this->_plugins['postfilter']) > 0) {
  1882. foreach ($this->_plugins['postfilter'] as $filter_name => $postfilter) {
  1883. if ($postfilter === false) {
  1884. unset($this->_plugins['postfilter'][$filter_name]);
  1885. $_params = array('plugins' => array(array('postfilter', $filter_name, null, null, false)));
  1886. require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');
  1887. smarty_core_load_plugins($_params, $this);
  1888. }
  1889. }
  1890. }
  1891. }
  1892. /**
  1893. * Quote subpattern references
  1894. *
  1895. * @param string $string
  1896. * @return string
  1897. */
  1898. function _quote_replace($string)
  1899. {
  1900. return strtr($string, array('\\' => '\\\\', '$' => '\\$'));
  1901. }
  1902. /**
  1903. * display Smarty syntax error
  1904. *
  1905. * @param string $error_msg
  1906. * @param integer $error_type
  1907. * @param string $file
  1908. * @param integer $line
  1909. */
  1910. function _syntax_error($error_msg, $error_type = E_USER_ERROR, $file=null, $line=null)
  1911. {
  1912. $this->_trigger_fatal_error("syntax error: $error_msg", $this->_current_file, $this->_current_line_no, $file, $line, $error_type);
  1913. }
  1914. /**
  1915. * check if the compilation changes from cacheable to
  1916. * non-cacheable state with the beginning of the current
  1917. * plugin. return php-code to reflect the transition.
  1918. * @return string
  1919. */
  1920. function _push_cacheable_state($type, $name) {
  1921. $_cacheable = !isset($this->_plugins[$type][$name]) || $this->_plugins[$type][$name][4];
  1922. if ($_cacheable
  1923. || 0<$this->_cacheable_state++) return '';
  1924. if (!isset($this->_cache_serial)) $this->_cache_serial = md5(uniqid('Smarty'));
  1925. $_ret = 'if ($this->caching && !$this->_cache_including) { echo \'{nocache:'
  1926. . $this->_cache_serial . '#' . $this->_nocache_count
  1927. . '}\'; };';
  1928. return $_ret;
  1929. }
  1930. /**
  1931. * check if the compilation changes from non-cacheable to
  1932. * cacheable state with the end of the current plugin return
  1933. * php-code to reflect the transition.
  1934. * @return string
  1935. */
  1936. function _pop_cacheable_state($type, $name) {
  1937. $_cacheable = !isset($this->_plugins[$type][$name]) || $this->_plugins[$type][$name][4];
  1938. if ($_cacheable
  1939. || --$this->_cacheable_state>0) return '';
  1940. return 'if ($this->caching && !$this->_cache_including) { echo \'{/nocache:'
  1941. . $this->_cache_serial . '#' . ($this->_nocache_count++)
  1942. . '}\'; };';
  1943. }
  1944. /**
  1945. * push opening tag-name, file-name and line-number on the tag-stack
  1946. * @param string the opening tag's name
  1947. */
  1948. function _push_tag($open_tag)
  1949. {
  1950. array_push($this->_tag_stack, array($open_tag, $this->_current_line_no));
  1951. }
  1952. /**
  1953. * pop closing tag-name
  1954. * raise an error if this stack-top doesn't match with the closing tag
  1955. * @param string the closing tag's name
  1956. * @return string the opening tag's name
  1957. */
  1958. function _pop_tag($close_tag)
  1959. {
  1960. $message = '';
  1961. if (count($this->_tag_stack)>0) {
  1962. list($_open_tag, $_line_no) = array_pop($this->_tag_stack);
  1963. if ($close_tag == $_open_tag) {
  1964. return $_open_tag;
  1965. }
  1966. if ($close_tag == 'if' && ($_open_tag == 'else' || $_open_tag == 'elseif' )) {
  1967. return $this->_pop_tag($close_tag);
  1968. }
  1969. if ($close_tag == 'section' && $_open_tag == 'sectionelse') {
  1970. $this->_pop_tag($close_tag);
  1971. return $_open_tag;
  1972. }
  1973. if ($close_tag == 'foreach' && $_open_tag == 'foreachelse') {
  1974. $this->_pop_tag($close_tag);
  1975. return $_open_tag;
  1976. }
  1977. if ($_open_tag == 'else' || $_open_tag == 'elseif') {
  1978. $_open_tag = 'if';
  1979. } elseif ($_open_tag == 'sectionelse') {
  1980. $_open_tag = 'section';
  1981. } elseif ($_open_tag == 'foreachelse') {
  1982. $_open_tag = 'foreach';
  1983. }
  1984. $message = " expected {/$_open_tag} (opened line $_line_no).";
  1985. }
  1986. $this->_syntax_error("mismatched tag {/$close_tag}.$message",
  1987. E_USER_ERROR, __FILE__, __LINE__);
  1988. }
  1989. }
  1990. /**
  1991. * compare to values by their string length
  1992. *
  1993. * @access private
  1994. * @param string $a
  1995. * @param string $b
  1996. * @return 0|-1|1
  1997. */
  1998. function _smarty_sort_length($a, $b)
  1999. {
  2000. if($a == $b)
  2001. return 0;
  2002. if(strlen($a) == strlen($b))
  2003. return ($a > $b) ? -1 : 1;
  2004. return (strlen($a) > strlen($b)) ? -1 : 1;
  2005. }
  2006. /* vim: set et: */
  2007. ?>