Smarty_Compiler.class.php 90 KB

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