/vendors/smarty/sysplugins/smarty_internal_cacheresource_file.php

https://github.com/kiang/olc_baker · PHP · 208 lines · 136 code · 10 blank · 62 comment · 36 complexity · 8e9992711c50b74d5897a2c7110a6d4f MD5 · raw file

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