Просмотр файла mch-lib/sys/libs/Smarty_Compiler.class.php

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