You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_runtime_updatecache.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Inline Runtime Methods render, setSourceByUid, setupSubTemplate
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsInternal
  7. * @author Uwe Tews
  8. *
  9. **/
  10. class Smarty_Internal_Runtime_UpdateCache
  11. {
  12. /**
  13. * check client side cache
  14. *
  15. * @param \Smarty_Template_Cached $cached
  16. * @param Smarty_Internal_Template $_template
  17. * @param string $content
  18. */
  19. public function cacheModifiedCheck(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $content)
  20. {
  21. }
  22. /**
  23. * Sanitize content and write it to cache resource
  24. *
  25. * @param \Smarty_Template_Cached $cached
  26. * @param Smarty_Internal_Template $_template
  27. * @param bool $no_output_filter
  28. *
  29. * @throws \SmartyException
  30. */
  31. public function removeNoCacheHash(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template,
  32. $no_output_filter)
  33. {
  34. $content = ob_get_clean();
  35. unset($cached->hashes[$_template->compiled->nocache_hash]);
  36. if (!empty($cached->hashes)) {
  37. $hash_array = array();
  38. foreach ($cached->hashes as $hash => $foo) {
  39. $hash_array[] = "/{$hash}/";
  40. }
  41. $content = preg_replace($hash_array, $_template->compiled->nocache_hash, $content);
  42. }
  43. $_template->cached->has_nocache_code = false;
  44. // get text between non-cached items
  45. $cache_split =
  46. preg_split("!/\*%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*/!s",
  47. $content);
  48. // get non-cached items
  49. preg_match_all("!/\*%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*/!s",
  50. $content, $cache_parts);
  51. $content = '';
  52. // loop over items, stitch back together
  53. foreach ($cache_split as $curr_idx => $curr_split) {
  54. // escape PHP tags in template content
  55. $content .= preg_replace('/(<%|%>|<\?php|<\?|\?>|<script\s+language\s*=\s*[\"\']?\s*php\s*[\"\']?\s*>)/',
  56. "<?php echo '\$1'; ?>\n", $curr_split);
  57. if (isset($cache_parts[0][$curr_idx])) {
  58. $_template->cached->has_nocache_code = true;
  59. $content .= $cache_parts[1][$curr_idx];
  60. }
  61. }
  62. if (!$no_output_filter && !$_template->compiled->has_nocache_code &&
  63. (isset($_template->smarty->autoload_filters['output']) ||
  64. isset($_template->smarty->registered_filters['output']))
  65. ) {
  66. $content = $_template->smarty->ext->_filterHandler->runFilter('output', $content, $_template);
  67. }
  68. // write cache file content
  69. $this->writeCachedContent($cached, $_template, $content);
  70. }
  71. /**
  72. * Cache was invalid , so render from compiled and write to cache
  73. *
  74. * @param \Smarty_Template_Cached $cached
  75. * @param \Smarty_Internal_Template $_template
  76. * @param $no_output_filter
  77. *
  78. * @throws \Exception
  79. */
  80. public function updateCache(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $no_output_filter)
  81. {
  82. if ($_template->source->handler->uncompiled) {
  83. ob_start();
  84. $_template->source->render($_template);
  85. } else {
  86. ob_start();
  87. if (!isset($_template->compiled)) {
  88. $_template->loadCompiled();
  89. }
  90. $_template->compiled->render($_template);
  91. }
  92. if ($_template->smarty->debugging) {
  93. $_template->smarty->_debug->start_cache($_template);
  94. }
  95. $this->removeNoCacheHash($cached, $_template, $no_output_filter);
  96. $compile_check = $_template->smarty->compile_check;
  97. $_template->smarty->compile_check = false;
  98. if (isset($_template->parent) && $_template->parent->_objType == 2) {
  99. $_template->compiled->unifunc = $_template->parent->compiled->unifunc;
  100. }
  101. if (!$_template->cached->processed) {
  102. $_template->cached->process($_template, true);
  103. }
  104. $_template->smarty->compile_check = $compile_check;
  105. $cached->getRenderedTemplateCode($_template);
  106. if ($_template->smarty->debugging) {
  107. $_template->smarty->_debug->end_cache($_template);
  108. }
  109. }
  110. /**
  111. * Writes the content to cache resource
  112. *
  113. * @param \Smarty_Template_Cached $cached
  114. * @param Smarty_Internal_Template $_template
  115. * @param string $content
  116. *
  117. * @return bool
  118. */
  119. public function writeCachedContent(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $content)
  120. {
  121. if ($_template->source->handler->recompiled || !($_template->caching == Smarty::CACHING_LIFETIME_CURRENT ||
  122. $_template->caching == Smarty::CACHING_LIFETIME_SAVED)
  123. ) {
  124. // don't write cache file
  125. return false;
  126. }
  127. $content = $_template->smarty->ext->_codeFrame->create($_template, $content, '', true);
  128. return $this->write($cached, $_template, $content);
  129. }
  130. /**
  131. * Write this cache object to handler
  132. *
  133. * @param \Smarty_Template_Cached $cached
  134. * @param Smarty_Internal_Template $_template template object
  135. * @param string $content content to cache
  136. *
  137. * @return bool success
  138. */
  139. public function write(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $content)
  140. {
  141. if (!$_template->source->handler->recompiled) {
  142. if ($cached->handler->writeCachedContent($_template, $content)) {
  143. $cached->content = null;
  144. $cached->timestamp = time();
  145. $cached->exists = true;
  146. $cached->valid = true;
  147. $cached->cache_lifetime = $_template->cache_lifetime;
  148. $cached->processed = false;
  149. if ($_template->smarty->cache_locking) {
  150. $cached->handler->releaseLock($_template->smarty, $cached);
  151. }
  152. return true;
  153. }
  154. $cached->content = null;
  155. $cached->timestamp = false;
  156. $cached->exists = false;
  157. $cached->valid = false;
  158. $cached->processed = false;
  159. }
  160. return false;
  161. }
  162. }