PageRenderTime 68ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Smarty/Smarty_Compiler.class.php

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