smarty_internal_templatecompilerbase.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Smarty Template Compiler Base
  4. *
  5. * This file contains the basic classes and methodes for compiling Smarty templates with lexer/parser
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Main abstract compiler class
  13. *
  14. * @package Smarty
  15. * @subpackage Compiler
  16. */
  17. abstract class Smarty_Internal_TemplateCompilerBase {
  18. /**
  19. * hash for nocache sections
  20. *
  21. * @var mixed
  22. */
  23. private $nocache_hash = null;
  24. /**
  25. * suppress generation of nocache code
  26. *
  27. * @var bool
  28. */
  29. public $suppressNocacheProcessing = false;
  30. /**
  31. * suppress generation of merged template code
  32. *
  33. * @var bool
  34. */
  35. public $suppressMergedTemplates = false;
  36. /**
  37. * compile tag objects
  38. *
  39. * @var array
  40. */
  41. public static $_tag_objects = array();
  42. /**
  43. * tag stack
  44. *
  45. * @var array
  46. */
  47. public $_tag_stack = array();
  48. /**
  49. * current template
  50. *
  51. * @var Smarty_Internal_Template
  52. */
  53. public $template = null;
  54. /**
  55. * merged templates
  56. *
  57. * @var array
  58. */
  59. public $merged_templates = array();
  60. /**
  61. * flag when compiling {block}
  62. *
  63. * @var bool
  64. */
  65. public $inheritance = false;
  66. /**
  67. * plugins loaded by default plugin handler
  68. *
  69. * @var array
  70. */
  71. public $default_handler_plugins = array();
  72. /**
  73. * saved preprocessed modifier list
  74. *
  75. * @var mixed
  76. */
  77. public $default_modifier_list = null;
  78. /**
  79. * force compilation of complete template as nocache
  80. * @var boolean
  81. */
  82. public $forceNocache = false;
  83. /**
  84. * suppress Smarty header code in compiled template
  85. * @var bool
  86. */
  87. public $suppressHeader = false;
  88. /**
  89. * suppress template property header code in compiled template
  90. * @var bool
  91. */
  92. public $suppressTemplatePropertyHeader = false;
  93. /**
  94. * flag if compiled template file shall we written
  95. * @var bool
  96. */
  97. public $write_compiled_code = true;
  98. /**
  99. * flag if currently a template function is compiled
  100. * @var bool
  101. */
  102. public $compiles_template_function = false;
  103. /**
  104. * called subfuntions from template function
  105. * @var array
  106. */
  107. public $called_functions = array();
  108. /**
  109. * flags for used modifier plugins
  110. * @var array
  111. */
  112. public $modifier_plugins = array();
  113. /**
  114. * type of already compiled modifier
  115. * @var array
  116. */
  117. public $known_modifier_type = array();
  118. /**
  119. * Initialize compiler
  120. */
  121. public function __construct() {
  122. $this->nocache_hash = str_replace('.', '-', uniqid(rand(), true));
  123. }
  124. /**
  125. * Method to compile a Smarty template
  126. *
  127. * @param Smarty_Internal_Template $template template object to compile
  128. * @return bool true if compiling succeeded, false if it failed
  129. */
  130. public function compileTemplate(Smarty_Internal_Template $template) {
  131. if (empty($template->properties['nocache_hash'])) {
  132. $template->properties['nocache_hash'] = $this->nocache_hash;
  133. } else {
  134. $this->nocache_hash = $template->properties['nocache_hash'];
  135. }
  136. // flag for nochache sections
  137. $this->nocache = false;
  138. $this->tag_nocache = false;
  139. // save template object in compiler class
  140. $this->template = $template;
  141. // reset has noche code flag
  142. $this->template->has_nocache_code = false;
  143. $this->smarty->_current_file = $saved_filepath = $this->template->source->filepath;
  144. // template header code
  145. $template_header = '';
  146. if (!$this->suppressHeader) {
  147. $template_header .= "<?php /* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") . "\n";
  148. $template_header .= " compiled from \"" . $this->template->source->filepath . "\" */ ?>\n";
  149. }
  150. do {
  151. // flag for aborting current and start recompile
  152. $this->abort_and_recompile = false;
  153. // get template source
  154. $_content = $template->source->content;
  155. // run prefilter if required
  156. if (isset($this->smarty->autoload_filters['pre']) || isset($this->smarty->registered_filters['pre'])) {
  157. $_content = Smarty_Internal_Filter_Handler::runFilter('pre', $_content, $template);
  158. }
  159. // on empty template just return header
  160. if ($_content == '') {
  161. if ($this->suppressTemplatePropertyHeader) {
  162. $code = '';
  163. } else {
  164. $code = $template_header . $template->createTemplateCodeFrame();
  165. }
  166. return $code;
  167. }
  168. // call compiler
  169. $_compiled_code = $this->doCompile($_content);
  170. } while ($this->abort_and_recompile);
  171. $this->template->source->filepath = $saved_filepath;
  172. // free memory
  173. unset($this->parser->root_buffer, $this->parser->current_buffer, $this->parser, $this->lex, $this->template);
  174. self::$_tag_objects = array();
  175. // return compiled code to template object
  176. $merged_code = '';
  177. if (!$this->suppressMergedTemplates && !empty($this->merged_templates)) {
  178. foreach ($this->merged_templates as $code) {
  179. $merged_code .= $code;
  180. }
  181. // run postfilter if required on merged code
  182. if (isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post'])) {
  183. $merged_code = Smarty_Internal_Filter_Handler::runFilter('post', $merged_code, $template);
  184. }
  185. }
  186. // run postfilter if required on compiled template code
  187. if (isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post'])) {
  188. $_compiled_code = Smarty_Internal_Filter_Handler::runFilter('post', $_compiled_code, $template);
  189. }
  190. if ($this->suppressTemplatePropertyHeader) {
  191. $code = $_compiled_code . $merged_code;
  192. } else {
  193. $code = $template_header . $template->createTemplateCodeFrame($_compiled_code) . $merged_code;
  194. }
  195. return $code;
  196. }
  197. /**
  198. * Compile Tag
  199. *
  200. * This is a call back from the lexer/parser
  201. * It executes the required compile plugin for the Smarty tag
  202. *
  203. * @param string $tag tag name
  204. * @param array $args array with tag attributes
  205. * @param array $parameter array with compilation parameter
  206. * @return string compiled code
  207. */
  208. public function compileTag($tag, $args, $parameter = array()) {
  209. // $args contains the attributes parsed and compiled by the lexer/parser
  210. // assume that tag does compile into code, but creates no HTML output
  211. $this->has_code = true;
  212. $this->has_output = false;
  213. // log tag/attributes
  214. if (isset($this->smarty->get_used_tags) && $this->smarty->get_used_tags) {
  215. $this->template->used_tags[] = array($tag, $args);
  216. }
  217. // check nocache option flag
  218. if (in_array("'nocache'", $args) || in_array(array('nocache' => 'true'), $args)
  219. || in_array(array('nocache' => '"true"'), $args) || in_array(array('nocache' => "'true'"), $args)) {
  220. $this->tag_nocache = true;
  221. }
  222. // compile the smarty tag (required compile classes to compile the tag are autoloaded)
  223. if (($_output = $this->callTagCompiler($tag, $args, $parameter)) === false) {
  224. if (isset($this->smarty->template_functions[$tag])) {
  225. // template defined by {template} tag
  226. $args['_attr']['name'] = "'" . $tag . "'";
  227. $_output = $this->callTagCompiler('call', $args, $parameter);
  228. }
  229. }
  230. if ($_output !== false) {
  231. if ($_output !== true) {
  232. // did we get compiled code
  233. if ($this->has_code) {
  234. // Does it create output?
  235. if ($this->has_output) {
  236. $_output .= "\n";
  237. }
  238. // return compiled code
  239. return $_output;
  240. }
  241. }
  242. // tag did not produce compiled code
  243. return '';
  244. } else {
  245. // map_named attributes
  246. if (isset($args['_attr'])) {
  247. foreach ($args['_attr'] as $key => $attribute) {
  248. if (is_array($attribute)) {
  249. $args = array_merge($args, $attribute);
  250. }
  251. }
  252. }
  253. // not an internal compiler tag
  254. if (strlen($tag) < 6 || substr($tag, -5) != 'close') {
  255. // check if tag is a registered object
  256. if (isset($this->smarty->registered_objects[$tag]) && isset($parameter['object_methode'])) {
  257. $methode = $parameter['object_methode'];
  258. if (!in_array($methode, $this->smarty->registered_objects[$tag][3]) &&
  259. (empty($this->smarty->registered_objects[$tag][1]) || in_array($methode, $this->smarty->registered_objects[$tag][1]))) {
  260. return $this->callTagCompiler('private_object_function', $args, $parameter, $tag, $methode);
  261. } elseif (in_array($methode, $this->smarty->registered_objects[$tag][3])) {
  262. return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag, $methode);
  263. } else {
  264. return $this->trigger_template_error('unallowed methode "' . $methode . '" in registered object "' . $tag . '"', $this->lex->taglineno);
  265. }
  266. }
  267. // check if tag is registered
  268. foreach (array(Smarty::PLUGIN_COMPILER, Smarty::PLUGIN_FUNCTION, Smarty::PLUGIN_BLOCK) as $plugin_type) {
  269. if (isset($this->smarty->registered_plugins[$plugin_type][$tag])) {
  270. // if compiler function plugin call it now
  271. if ($plugin_type == Smarty::PLUGIN_COMPILER) {
  272. $new_args = array();
  273. foreach ($args as $key => $mixed) {
  274. if (is_array($mixed)) {
  275. $new_args = array_merge($new_args, $mixed);
  276. } else {
  277. $new_args[$key] = $mixed;
  278. }
  279. }
  280. if (!$this->smarty->registered_plugins[$plugin_type][$tag][1]) {
  281. $this->tag_nocache = true;
  282. }
  283. $function = $this->smarty->registered_plugins[$plugin_type][$tag][0];
  284. if (!is_array($function)) {
  285. return $function($new_args, $this);
  286. } else if (is_object($function[0])) {
  287. return $this->smarty->registered_plugins[$plugin_type][$tag][0][0]->$function[1]($new_args, $this);
  288. } else {
  289. return call_user_func_array($function, array($new_args, $this));
  290. }
  291. }
  292. // compile registered function or block function
  293. if ($plugin_type == Smarty::PLUGIN_FUNCTION || $plugin_type == Smarty::PLUGIN_BLOCK) {
  294. return $this->callTagCompiler('private_registered_' . $plugin_type, $args, $parameter, $tag);
  295. }
  296. }
  297. }
  298. // check plugins from plugins folder
  299. foreach ($this->smarty->plugin_search_order as $plugin_type) {
  300. if ($plugin_type == Smarty::PLUGIN_BLOCK && $this->smarty->loadPlugin('smarty_compiler_' . $tag) && (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this))) {
  301. $plugin = 'smarty_compiler_' . $tag;
  302. if (is_callable($plugin)) {
  303. // convert arguments format for old compiler plugins
  304. $new_args = array();
  305. foreach ($args as $key => $mixed) {
  306. if (is_array($mixed)) {
  307. $new_args = array_merge($new_args, $mixed);
  308. } else {
  309. $new_args[$key] = $mixed;
  310. }
  311. }
  312. return $plugin($new_args, $this->smarty);
  313. }
  314. if (class_exists($plugin, false)) {
  315. $plugin_object = new $plugin;
  316. if (method_exists($plugin_object, 'compile')) {
  317. return $plugin_object->compile($args, $this);
  318. }
  319. }
  320. throw new SmartyException("Plugin \"{$tag}\" not callable");
  321. } else {
  322. if ($function = $this->getPlugin($tag, $plugin_type)) {
  323. if (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this)) {
  324. return $this->callTagCompiler('private_' . $plugin_type . '_plugin', $args, $parameter, $tag, $function);
  325. }
  326. }
  327. }
  328. }
  329. if (is_callable($this->smarty->default_plugin_handler_func)) {
  330. $found = false;
  331. // look for already resolved tags
  332. foreach ($this->smarty->plugin_search_order as $plugin_type) {
  333. if (isset($this->default_handler_plugins[$plugin_type][$tag])) {
  334. $found = true;
  335. break;
  336. }
  337. }
  338. if (!$found) {
  339. // call default handler
  340. foreach ($this->smarty->plugin_search_order as $plugin_type) {
  341. if ($this->getPluginFromDefaultHandler($tag, $plugin_type)) {
  342. $found = true;
  343. break;
  344. }
  345. }
  346. }
  347. if ($found) {
  348. // if compiler function plugin call it now
  349. if ($plugin_type == Smarty::PLUGIN_COMPILER) {
  350. $new_args = array();
  351. foreach ($args as $mixed) {
  352. $new_args = array_merge($new_args, $mixed);
  353. }
  354. $function = $this->default_handler_plugins[$plugin_type][$tag][0];
  355. if (!is_array($function)) {
  356. return $function($new_args, $this);
  357. } else if (is_object($function[0])) {
  358. return $this->default_handler_plugins[$plugin_type][$tag][0][0]->$function[1]($new_args, $this);
  359. } else {
  360. return call_user_func_array($function, array($new_args, $this));
  361. }
  362. } else {
  363. return $this->callTagCompiler('private_registered_' . $plugin_type, $args, $parameter, $tag);
  364. }
  365. }
  366. }
  367. } else {
  368. // compile closing tag of block function
  369. $base_tag = substr($tag, 0, -5);
  370. // check if closing tag is a registered object
  371. if (isset($this->smarty->registered_objects[$base_tag]) && isset($parameter['object_methode'])) {
  372. $methode = $parameter['object_methode'];
  373. if (in_array($methode, $this->smarty->registered_objects[$base_tag][3])) {
  374. return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag, $methode);
  375. } else {
  376. return $this->trigger_template_error('unallowed closing tag methode "' . $methode . '" in registered object "' . $base_tag . '"', $this->lex->taglineno);
  377. }
  378. }
  379. // registered block tag ?
  380. if (isset($this->smarty->registered_plugins[Smarty::PLUGIN_BLOCK][$base_tag]) || isset($this->default_handler_plugins[Smarty::PLUGIN_BLOCK][$base_tag])) {
  381. return $this->callTagCompiler('private_registered_block', $args, $parameter, $tag);
  382. }
  383. // block plugin?
  384. if ($function = $this->getPlugin($base_tag, Smarty::PLUGIN_BLOCK)) {
  385. return $this->callTagCompiler('private_block_plugin', $args, $parameter, $tag, $function);
  386. }
  387. // registered compiler plugin ?
  388. if (isset($this->smarty->registered_plugins[Smarty::PLUGIN_COMPILER][$tag])) {
  389. // if compiler function plugin call it now
  390. $args = array();
  391. if (!$this->smarty->registered_plugins[Smarty::PLUGIN_COMPILER][$tag][1]) {
  392. $this->tag_nocache = true;
  393. }
  394. $function = $this->smarty->registered_plugins[Smarty::PLUGIN_COMPILER][$tag][0];
  395. if (!is_array($function)) {
  396. return $function($args, $this);
  397. } else if (is_object($function[0])) {
  398. return $this->smarty->registered_plugins[Smarty::PLUGIN_COMPILER][$tag][0][0]->$function[1]($args, $this);
  399. } else {
  400. return call_user_func_array($function, array($args, $this));
  401. }
  402. }
  403. if ($this->smarty->loadPlugin('smarty_compiler_' . $tag)) {
  404. $plugin = 'smarty_compiler_' . $tag;
  405. if (is_callable($plugin)) {
  406. return $plugin($args, $this->smarty);
  407. }
  408. if (class_exists($plugin, false)) {
  409. $plugin_object = new $plugin;
  410. if (method_exists($plugin_object, 'compile')) {
  411. return $plugin_object->compile($args, $this);
  412. }
  413. }
  414. throw new SmartyException("Plugin \"{$tag}\" not callable");
  415. }
  416. }
  417. $this->trigger_template_error("unknown tag \"" . $tag . "\"", $this->lex->taglineno);
  418. }
  419. }
  420. /**
  421. * lazy loads internal compile plugin for tag and calls the compile methode
  422. *
  423. * compile objects cached for reuse.
  424. * class name format: Smarty_Internal_Compile_TagName
  425. * plugin filename format: Smarty_Internal_Tagname.php
  426. *
  427. * @param string $tag tag name
  428. * @param array $args list of tag attributes
  429. * @param mixed $param1 optional parameter
  430. * @param mixed $param2 optional parameter
  431. * @param mixed $param3 optional parameter
  432. * @return string compiled code
  433. */
  434. public function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null) {
  435. // re-use object if already exists
  436. if (isset(self::$_tag_objects[$tag])) {
  437. // compile this tag
  438. return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3);
  439. }
  440. // lazy load internal compiler plugin
  441. $class_name = 'Smarty_Internal_Compile_' . $tag;
  442. if ($this->smarty->loadPlugin($class_name)) {
  443. // check if tag allowed by security
  444. if (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this)) {
  445. // use plugin if found
  446. self::$_tag_objects[$tag] = new $class_name;
  447. // compile this tag
  448. return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3);
  449. }
  450. }
  451. // no internal compile plugin for this tag
  452. return false;
  453. }
  454. /**
  455. * Check for plugins and return function name
  456. *
  457. * @param string $pugin_name name of plugin or function
  458. * @param string $plugin_type type of plugin
  459. * @return string call name of function
  460. */
  461. public function getPlugin($plugin_name, $plugin_type) {
  462. $function = null;
  463. if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
  464. if (isset($this->template->required_plugins['nocache'][$plugin_name][$plugin_type])) {
  465. $function = $this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['function'];
  466. } else if (isset($this->template->required_plugins['compiled'][$plugin_name][$plugin_type])) {
  467. $this->template->required_plugins['nocache'][$plugin_name][$plugin_type] = $this->template->required_plugins['compiled'][$plugin_name][$plugin_type];
  468. $function = $this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['function'];
  469. }
  470. } else {
  471. if (isset($this->template->required_plugins['compiled'][$plugin_name][$plugin_type])) {
  472. $function = $this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['function'];
  473. } else if (isset($this->template->required_plugins['nocache'][$plugin_name][$plugin_type])) {
  474. $this->template->required_plugins['compiled'][$plugin_name][$plugin_type] = $this->template->required_plugins['nocache'][$plugin_name][$plugin_type];
  475. $function = $this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['function'];
  476. }
  477. }
  478. if (isset($function)) {
  479. if ($plugin_type == 'modifier') {
  480. $this->modifier_plugins[$plugin_name] = true;
  481. }
  482. return $function;
  483. }
  484. // loop through plugin dirs and find the plugin
  485. $function = 'smarty_' . $plugin_type . '_' . $plugin_name;
  486. $file = $this->smarty->loadPlugin($function, false);
  487. if (is_string($file)) {
  488. if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
  489. $this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['file'] = $file;
  490. $this->template->required_plugins['nocache'][$plugin_name][$plugin_type]['function'] = $function;
  491. } else {
  492. $this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['file'] = $file;
  493. $this->template->required_plugins['compiled'][$plugin_name][$plugin_type]['function'] = $function;
  494. }
  495. if ($plugin_type == 'modifier') {
  496. $this->modifier_plugins[$plugin_name] = true;
  497. }
  498. return $function;
  499. }
  500. if (is_callable($function)) {
  501. // plugin function is defined in the script
  502. return $function;
  503. }
  504. return false;
  505. }
  506. /**
  507. * Check for plugins by default plugin handler
  508. *
  509. * @param string $tag name of tag
  510. * @param string $plugin_type type of plugin
  511. * @return boolean true if found
  512. */
  513. public function getPluginFromDefaultHandler($tag, $plugin_type) {
  514. $callback = null;
  515. $script = null;
  516. $cacheable = true;
  517. $result = call_user_func_array(
  518. $this->smarty->default_plugin_handler_func, array($tag, $plugin_type, $this->template, &$callback, &$script, &$cacheable)
  519. );
  520. if ($result) {
  521. $this->tag_nocache = $this->tag_nocache || !$cacheable;
  522. if ($script !== null) {
  523. if (is_file($script)) {
  524. if ($this->template->caching && ($this->nocache || $this->tag_nocache)) {
  525. $this->template->required_plugins['nocache'][$tag][$plugin_type]['file'] = $script;
  526. $this->template->required_plugins['nocache'][$tag][$plugin_type]['function'] = $callback;
  527. } else {
  528. $this->template->required_plugins['compiled'][$tag][$plugin_type]['file'] = $script;
  529. $this->template->required_plugins['compiled'][$tag][$plugin_type]['function'] = $callback;
  530. }
  531. include_once $script;
  532. } else {
  533. $this->trigger_template_error("Default plugin handler: Returned script file \"{$script}\" for \"{$tag}\" not found");
  534. }
  535. }
  536. if (!is_string($callback) && !(is_array($callback) && is_string($callback[0]) && is_string($callback[1]))) {
  537. $this->trigger_template_error("Default plugin handler: Returned callback for \"{$tag}\" must be a static function name or array of class and function name");
  538. }
  539. if (is_callable($callback)) {
  540. $this->default_handler_plugins[$plugin_type][$tag] = array($callback, true, array());
  541. return true;
  542. } else {
  543. $this->trigger_template_error("Default plugin handler: Returned callback for \"{$tag}\" not callable");
  544. }
  545. }
  546. return false;
  547. }
  548. /**
  549. * Inject inline code for nocache template sections
  550. *
  551. * This method gets the content of each template element from the parser.
  552. * If the content is compiled code and it should be not cached the code is injected
  553. * into the rendered output.
  554. *
  555. * @param string $content content of template element
  556. * @param boolean $is_code true if content is compiled code
  557. * @return string content
  558. */
  559. public function processNocacheCode($content, $is_code) {
  560. // If the template is not evaluated and we have a nocache section and or a nocache tag
  561. if ($is_code && !empty($content)) {
  562. // generate replacement code
  563. if ((!($this->template->source->recompiled) || $this->forceNocache) && $this->template->caching && !$this->suppressNocacheProcessing &&
  564. ($this->nocache || $this->tag_nocache || $this->forceNocache == 2)) {
  565. $this->template->has_nocache_code = true;
  566. $_output = str_replace("'", "\'", $content);
  567. $_output = str_replace('\\\\', '\\\\\\\\', $_output);
  568. $_output = str_replace("^#^", "'", $_output);
  569. $_output = "<?php echo '/*%%SmartyNocache:{$this->nocache_hash}%%*/" . $_output . "/*/%%SmartyNocache:{$this->nocache_hash}%%*/';?>\n";
  570. // make sure we include modifer plugins for nocache code
  571. foreach ($this->modifier_plugins as $plugin_name => $dummy) {
  572. if (isset($this->template->required_plugins['compiled'][$plugin_name]['modifier'])) {
  573. $this->template->required_plugins['nocache'][$plugin_name]['modifier'] = $this->template->required_plugins['compiled'][$plugin_name]['modifier'];
  574. }
  575. }
  576. } else {
  577. $_output = $content;
  578. }
  579. } else {
  580. $_output = $content;
  581. }
  582. $this->modifier_plugins = array();
  583. $this->suppressNocacheProcessing = false;
  584. $this->tag_nocache = false;
  585. return $_output;
  586. }
  587. /**
  588. * display compiler error messages without dying
  589. *
  590. * If parameter $args is empty it is a parser detected syntax error.
  591. * In this case the parser is called to obtain information about expected tokens.
  592. *
  593. * If parameter $args contains a string this is used as error message
  594. *
  595. * @param string $args individual error message or null
  596. * @param string $line line-number
  597. * @throws SmartyCompilerException when an unexpected token is found
  598. */
  599. public function trigger_template_error($args = null, $line = null) {
  600. // get template source line which has error
  601. if (!isset($line)) {
  602. $line = $this->lex->line;
  603. }
  604. $match = preg_split("/\n/", $this->lex->data);
  605. $error_text = 'Syntax Error in template "' . $this->template->source->filepath . '" on line ' . $line . ' "' . htmlspecialchars(trim(preg_replace('![\t\r\n]+!', ' ', $match[$line - 1]))) . '" ';
  606. if (isset($args)) {
  607. // individual error message
  608. $error_text .= $args;
  609. } else {
  610. // expected token from parser
  611. $error_text .= ' - Unexpected "' . $this->lex->value . '"';
  612. if (count($this->parser->yy_get_expected_tokens($this->parser->yymajor)) <= 4) {
  613. foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) {
  614. $exp_token = $this->parser->yyTokenName[$token];
  615. if (isset($this->lex->smarty_token_names[$exp_token])) {
  616. // token type from lexer
  617. $expect[] = '"' . $this->lex->smarty_token_names[$exp_token] . '"';
  618. } else {
  619. // otherwise internal token name
  620. $expect[] = $this->parser->yyTokenName[$token];
  621. }
  622. }
  623. $error_text .= ', expected one of: ' . implode(' , ', $expect);
  624. }
  625. }
  626. throw new SmartyCompilerException($error_text);
  627. }
  628. }
  629. ?>