PageRenderTime 55ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/smarty/libs/Smarty_Compiler.class.php

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