Smarty_Compiler.class.php 90 KB

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