PageRenderTime 169ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/ui/smarty/Smarty_Compiler.class.php

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