Smarty_Compiler.class.php 87 KB

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