smarty_internal_runtime_updatecache.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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->cached->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. ob_start();
  83. if (!isset($_template->compiled)) {
  84. $_template->loadCompiled();
  85. }
  86. $_template->compiled->render($_template);
  87. if ($_template->smarty->debugging) {
  88. $_template->smarty->_debug->start_cache($_template);
  89. }
  90. $this->removeNoCacheHash($cached, $_template, $no_output_filter);
  91. $compile_check = $_template->smarty->compile_check;
  92. $_template->smarty->compile_check = false;
  93. if ($_template->_isSubTpl()) {
  94. $_template->compiled->unifunc = $_template->parent->compiled->unifunc;
  95. }
  96. if (!$_template->cached->processed) {
  97. $_template->cached->process($_template, true);
  98. }
  99. $_template->smarty->compile_check = $compile_check;
  100. $cached->getRenderedTemplateCode($_template);
  101. if ($_template->smarty->debugging) {
  102. $_template->smarty->_debug->end_cache($_template);
  103. }
  104. }
  105. /**
  106. * Writes the content to cache resource
  107. *
  108. * @param \Smarty_Template_Cached $cached
  109. * @param Smarty_Internal_Template $_template
  110. * @param string $content
  111. *
  112. * @return bool
  113. */
  114. public function writeCachedContent(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $content)
  115. {
  116. if ($_template->source->handler->recompiled || !($_template->caching == Smarty::CACHING_LIFETIME_CURRENT ||
  117. $_template->caching == Smarty::CACHING_LIFETIME_SAVED)
  118. ) {
  119. // don't write cache file
  120. return false;
  121. }
  122. $content = $_template->smarty->ext->_codeFrame->create($_template, $content, '', true);
  123. return $this->write($cached, $_template, $content);
  124. }
  125. /**
  126. * Write this cache object to handler
  127. *
  128. * @param \Smarty_Template_Cached $cached
  129. * @param Smarty_Internal_Template $_template template object
  130. * @param string $content content to cache
  131. *
  132. * @return bool success
  133. */
  134. public function write(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $content)
  135. {
  136. if (!$_template->source->handler->recompiled) {
  137. if ($cached->handler->writeCachedContent($_template, $content)) {
  138. $cached->content = null;
  139. $cached->timestamp = time();
  140. $cached->exists = true;
  141. $cached->valid = true;
  142. $cached->cache_lifetime = $_template->cache_lifetime;
  143. $cached->processed = false;
  144. if ($_template->smarty->cache_locking) {
  145. $cached->handler->releaseLock($_template->smarty, $cached);
  146. }
  147. return true;
  148. }
  149. $cached->content = null;
  150. $cached->timestamp = false;
  151. $cached->exists = false;
  152. $cached->valid = false;
  153. $cached->processed = false;
  154. }
  155. return false;
  156. }
  157. }