PageRenderTime 53ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/libs/module/smarty/Smarty_Compiler.class.php

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