PageRenderTime 36ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/_friendly/vendor/smarty/Smarty_Compiler.class.php

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