smarty_internal_cacheresource_file.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Smarty Internal Plugin CacheResource File
  4. *
  5. * Implements the file system as resource for the HTML cache
  6. * Version ussing nocache inserts
  7. *
  8. * @package Smarty
  9. * @subpackage Cacher
  10. * @author Uwe Tews
  11. */
  12. /**
  13. * This class does contain all necessary methods for the HTML cache on file system
  14. */
  15. class Smarty_Internal_CacheResource_File {
  16. function __construct($smarty)
  17. {
  18. $this->smarty = $smarty;
  19. }
  20. /**
  21. * Returns the filepath of the cached template output
  22. *
  23. * @param object $_template current template
  24. * @return string the cache filepath
  25. */
  26. public function getCachedFilepath($_template)
  27. {
  28. $_source_file_path = str_replace(':', '.', $_template->getTemplateFilepath());
  29. $_cache_id = isset($_template->cache_id) ? preg_replace('![^\w\|]+!', '_', $_template->cache_id) : null;
  30. $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null;
  31. $_filepath = $_template->templateUid;
  32. // if use_sub_dirs, break file into directories
  33. if ($this->smarty->use_sub_dirs) {
  34. $_filepath = substr($_filepath, 0, 2) . DS
  35. . substr($_filepath, 2, 2) . DS
  36. . substr($_filepath, 4, 2) . DS
  37. . $_filepath;
  38. }
  39. $_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
  40. if (isset($_cache_id)) {
  41. $_cache_id = str_replace('|', $_compile_dir_sep, $_cache_id) . $_compile_dir_sep;
  42. } else {
  43. $_cache_id = '';
  44. }
  45. if (isset($_compile_id)) {
  46. $_compile_id = $_compile_id . $_compile_dir_sep;
  47. } else {
  48. $_compile_id = '';
  49. }
  50. $_cache_dir = $this->smarty->cache_dir;
  51. if (strpos('/\\', substr($_cache_dir, -1)) === false) {
  52. $_cache_dir .= DS;
  53. }
  54. return $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_source_file_path) . '.php';
  55. }
  56. /**
  57. * Returns the timpestamp of the cached template output
  58. *
  59. * @param object $_template current template
  60. * @return integer |booelan the template timestamp or false if the file does not exist
  61. */
  62. public function getCachedTimestamp($_template)
  63. {
  64. // return @filemtime ($_template->getCachedFilepath());
  65. return ($_template->getCachedFilepath() && file_exists($_template->getCachedFilepath())) ? filemtime($_template->getCachedFilepath()) : false ;
  66. }
  67. /**
  68. * Returns the cached template output
  69. *
  70. * @param object $_template current template
  71. * @return string |booelan the template content or false if the file does not exist
  72. */
  73. public function getCachedContents($_template)
  74. {
  75. ob_start();
  76. $_smarty_tpl = $_template;
  77. include $_template->getCachedFilepath();
  78. return ob_get_clean();
  79. }
  80. /**
  81. * Writes the rendered template output to cache file
  82. *
  83. * @param object $_template current template
  84. * @return boolean status
  85. */
  86. public function writeCachedContent($_template, $content)
  87. {
  88. if (!$_template->resource_object->isEvaluated) {
  89. if (Smarty_Internal_Write_File::writeFile($_template->getCachedFilepath(), $content, $this->smarty) === true) {
  90. $_template->cached_timestamp = filemtime($_template->getCachedFilepath());
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. /**
  97. * Empty cache folder
  98. *
  99. * @param integer $exp_time expiration time
  100. * @return integer number of cache files deleted
  101. */
  102. public function clearAll($exp_time = null)
  103. {
  104. return $this->clear(null, null, null, $exp_time);
  105. }
  106. /**
  107. * Empty cache for a specific template
  108. *
  109. * @param string $resource_name template name
  110. * @param string $cache_id cache id
  111. * @param string $compile_id compile id
  112. * @param integer $exp_time expiration time
  113. * @return integer number of cache files deleted
  114. */
  115. public function clear($resource_name, $cache_id, $compile_id, $exp_time)
  116. {
  117. $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
  118. $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
  119. $_dir_sep = $this->smarty->use_sub_dirs ? '/' : '^';
  120. $_compile_id_offset = $this->smarty->use_sub_dirs ? 3 : 0;
  121. $_dir = rtrim($this->smarty->cache_dir, '/\\') . DS;
  122. $_dir_length = strlen($_dir);
  123. if (isset($_cache_id)) {
  124. $_cache_id_parts = explode('|', $_cache_id);
  125. $_cache_id_parts_count = count($_cache_id_parts);
  126. if ($this->smarty->use_sub_dirs) {
  127. foreach ($_cache_id_parts as $id_part) {
  128. $_dir .= $id_part . DS;
  129. }
  130. }
  131. }
  132. if (isset($resource_name)) {
  133. $_save_stat = $this->smarty->caching;
  134. $this->smarty->caching = true;
  135. $tpl = new $this->smarty->template_class($resource_name, $this->smarty);
  136. // remove from template cache
  137. unset($this->smarty->template_objects[crc32($tpl->template_resource . $tpl->cache_id . $tpl->compile_id)]);
  138. $this->smarty->caching = $_save_stat;
  139. if ($tpl->isExisting()) {
  140. $_resourcename_parts = basename(str_replace('^', '/', $tpl->getCachedFilepath()));
  141. } else {
  142. return 0;
  143. }
  144. }
  145. $_count = 0;
  146. if (file_exists($_dir)) {
  147. $_cacheDirs = new RecursiveDirectoryIterator($_dir);
  148. $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);
  149. foreach ($_cache as $_file) {
  150. if (strpos($_file, '.svn') !== false) continue;
  151. // directory ?
  152. if ($_file->isDir()) {
  153. if (!$_cache->isDot()) {
  154. // delete folder if empty
  155. @rmdir($_file->getPathname());
  156. }
  157. } else {
  158. $_parts = explode($_dir_sep, str_replace('\\', '/', substr((string)$_file, $_dir_length)));
  159. $_parts_count = count($_parts);
  160. // check name
  161. if (isset($resource_name)) {
  162. if ($_parts[$_parts_count-1] != $_resourcename_parts) {
  163. continue;
  164. }
  165. }
  166. // check compile id
  167. if (isset($_compile_id) && (!isset($_parts[$_parts_count-2 - $_compile_id_offset]) || $_parts[$_parts_count-2 - $_compile_id_offset] != $_compile_id)) {
  168. continue;
  169. }
  170. // check cache id
  171. if (isset($_cache_id)) {
  172. // count of cache id parts
  173. $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : $_parts_count - 1 - $_compile_id_offset;
  174. if ($_parts_count < $_cache_id_parts_count) {
  175. continue;
  176. }
  177. for ($i = 0; $i < $_cache_id_parts_count; $i++) {
  178. if ($_parts[$i] != $_cache_id_parts[$i]) continue 2;
  179. }
  180. }
  181. // expired ?
  182. if (isset($exp_time) && time() - @filemtime($_file) < $exp_time) {
  183. continue;
  184. }
  185. $_count += @unlink((string) $_file) ? 1 : 0;
  186. }
  187. }
  188. }
  189. return $_count;
  190. }
  191. }
  192. ?>