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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. * Cache was invalid , so render from compiled and write to cache
  24. *
  25. * @param \Smarty_Template_Cached $cached
  26. * @param \Smarty_Internal_Template $_template
  27. * @param $no_output_filter
  28. *
  29. * @throws \Exception
  30. */
  31. public function updateCache(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $no_output_filter)
  32. {
  33. ob_start();
  34. if (!isset($_template->compiled)) {
  35. $_template->loadCompiled();
  36. }
  37. $_template->compiled->render($_template);
  38. if ($_template->smarty->debugging) {
  39. $_template->smarty->_debug->start_cache($_template);
  40. }
  41. $this->removeNoCacheHash($cached, $_template, $no_output_filter);
  42. $compile_check = (int)$_template->compile_check;
  43. $_template->compile_check = Smarty::COMPILECHECK_OFF;
  44. if ($_template->_isSubTpl()) {
  45. $_template->compiled->unifunc = $_template->parent->compiled->unifunc;
  46. }
  47. if (!$_template->cached->processed) {
  48. $_template->cached->process($_template, true);
  49. }
  50. $_template->compile_check = $compile_check;
  51. $cached->getRenderedTemplateCode($_template);
  52. if ($_template->smarty->debugging) {
  53. $_template->smarty->_debug->end_cache($_template);
  54. }
  55. }
  56. /**
  57. * Sanitize content and write it to cache resource
  58. *
  59. * @param \Smarty_Template_Cached $cached
  60. * @param Smarty_Internal_Template $_template
  61. * @param bool $no_output_filter
  62. *
  63. * @throws \SmartyException
  64. */
  65. public function removeNoCacheHash(Smarty_Template_Cached $cached,
  66. Smarty_Internal_Template $_template,
  67. $no_output_filter)
  68. {
  69. $php_pattern = '/(<%|%>|<\?php|<\?|\?>|<script\s+language\s*=\s*[\"\']?\s*php\s*[\"\']?\s*>)/';
  70. $content = ob_get_clean();
  71. $hash_array = $cached->hashes;
  72. $hash_array[$_template->compiled->nocache_hash] = true;
  73. $hash_array = array_keys($hash_array);
  74. $nocache_hash = '(' . implode('|', $hash_array) . ')';
  75. $_template->cached->has_nocache_code = false;
  76. // get text between non-cached items
  77. $cache_split =
  78. preg_split("!/\*%%SmartyNocache:{$nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$nocache_hash}%%\*/!s",
  79. $content);
  80. // get non-cached items
  81. preg_match_all("!/\*%%SmartyNocache:{$nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$nocache_hash}%%\*/!s",
  82. $content,
  83. $cache_parts);
  84. $content = '';
  85. // loop over items, stitch back together
  86. foreach ($cache_split as $curr_idx => $curr_split) {
  87. if (preg_match($php_pattern, $curr_split)) {
  88. // escape PHP tags in template content
  89. $php_split = preg_split($php_pattern,
  90. $curr_split);
  91. preg_match_all($php_pattern,
  92. $curr_split,
  93. $php_parts);
  94. foreach ($php_split as $idx_php => $curr_php) {
  95. $content .= $curr_php;
  96. if (isset($php_parts[ 0 ][ $idx_php ])) {
  97. $content .= "<?php echo '{$php_parts[ 1 ][ $idx_php ]}'; ?>\n";
  98. }
  99. }
  100. } else {
  101. $content .= $curr_split;
  102. }
  103. if (isset($cache_parts[ 0 ][ $curr_idx ])) {
  104. $_template->cached->has_nocache_code = true;
  105. $content .= $cache_parts[ 2 ][ $curr_idx ];
  106. }
  107. }
  108. if (!$no_output_filter && !$_template->cached->has_nocache_code &&
  109. (isset($_template->smarty->autoload_filters[ 'output' ]) ||
  110. isset($_template->smarty->registered_filters[ 'output' ]))
  111. ) {
  112. $content = $_template->smarty->ext->_filterHandler->runFilter('output', $content, $_template);
  113. }
  114. // write cache file content
  115. $this->writeCachedContent($_template, $content);
  116. }
  117. /**
  118. * Writes the content to cache resource
  119. *
  120. * @param Smarty_Internal_Template $_template
  121. * @param string $content
  122. *
  123. * @return bool
  124. */
  125. public function writeCachedContent(Smarty_Internal_Template $_template, $content)
  126. {
  127. if ($_template->source->handler->recompiled || !$_template->caching
  128. ) {
  129. // don't write cache file
  130. return false;
  131. }
  132. if (!isset($_template->cached)) {
  133. $_template->loadCached();
  134. }
  135. $content = $_template->smarty->ext->_codeFrame->create($_template, $content, '', true);
  136. return $this->write($_template, $content);
  137. }
  138. /**
  139. * Write this cache object to handler
  140. *
  141. * @param Smarty_Internal_Template $_template template object
  142. * @param string $content content to cache
  143. *
  144. * @return bool success
  145. */
  146. public function write(Smarty_Internal_Template $_template, $content)
  147. {
  148. if (!$_template->source->handler->recompiled) {
  149. $cached = $_template->cached;
  150. if ($cached->handler->writeCachedContent($_template, $content)) {
  151. $cached->content = null;
  152. $cached->timestamp = time();
  153. $cached->exists = true;
  154. $cached->valid = true;
  155. $cached->cache_lifetime = $_template->cache_lifetime;
  156. $cached->processed = false;
  157. if ($_template->smarty->cache_locking) {
  158. $cached->handler->releaseLock($_template->smarty, $cached);
  159. }
  160. return true;
  161. }
  162. $cached->content = null;
  163. $cached->timestamp = false;
  164. $cached->exists = false;
  165. $cached->valid = false;
  166. $cached->processed = false;
  167. }
  168. return false;
  169. }
  170. }