PageRenderTime 32ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/source/libs/smarty/Smarty_Compiler.class.php

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