smarty_internal_cacheresource_file.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * Smarty Internal Plugin CacheResource File
  4. *
  5. * @package Smarty
  6. * @subpackage Cacher
  7. * @author Uwe Tews
  8. * @author Rodney Rehm
  9. */
  10. /**
  11. * This class does contain all necessary methods for the HTML cache on file system
  12. *
  13. * Implements the file system as resource for the HTML cache Version ussing nocache inserts.
  14. *
  15. * @package Smarty
  16. * @subpackage Cacher
  17. */
  18. class Smarty_Internal_CacheResource_File extends Smarty_CacheResource {
  19. /**
  20. * populate Cached Object with meta data from Resource
  21. *
  22. * @param Smarty_Template_Cached $cached cached object
  23. * @param Smarty_Internal_Template $_template template object
  24. * @return void
  25. */
  26. public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
  27. {
  28. $_source_file_path = str_replace(':', '.', $_template->source->filepath);
  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->source->uid;
  32. // if use_sub_dirs, break file into directories
  33. if ($_template->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 = $_template->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 = $_template->smarty->getCacheDir();
  51. if ($_template->smarty->cache_locking) {
  52. // create locking file name
  53. // relative file name?
  54. if (!preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_cache_dir)) {
  55. $_lock_dir = rtrim(getcwd(), '/\\') . DS . $_cache_dir;
  56. } else {
  57. $_lock_dir = $_cache_dir;
  58. }
  59. $cached->lock_id = $_lock_dir.sha1($_cache_id.$_compile_id.$_template->source->uid).'.lock';
  60. }
  61. $cached->filepath = $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_source_file_path) . '.php';
  62. $cached->timestamp = @filemtime($cached->filepath);
  63. $cached->exists = !!$cached->timestamp;
  64. }
  65. /**
  66. * populate Cached Object with timestamp and exists from Resource
  67. *
  68. * @param Smarty_Template_Cached $cached cached object
  69. * @return void
  70. */
  71. public function populateTimestamp(Smarty_Template_Cached $cached)
  72. {
  73. $cached->timestamp = @filemtime($cached->filepath);
  74. $cached->exists = !!$cached->timestamp;
  75. }
  76. /**
  77. * Read the cached template and process its header
  78. *
  79. * @param Smarty_Internal_Template $_template template object
  80. * @param Smarty_Template_Cached $cached cached object
  81. * @return booelan true or false if the cached content does not exist
  82. */
  83. public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached=null)
  84. {
  85. $_smarty_tpl = $_template;
  86. return @include $_template->cached->filepath;
  87. }
  88. /**
  89. * Write the rendered template output to cache
  90. *
  91. * @param Smarty_Internal_Template $_template template object
  92. * @param string $content content to cache
  93. * @return boolean success
  94. */
  95. public function writeCachedContent(Smarty_Internal_Template $_template, $content)
  96. {
  97. if (Smarty_Internal_Write_File::writeFile($_template->cached->filepath, $content, $_template->smarty) === true) {
  98. $_template->cached->timestamp = @filemtime($_template->cached->filepath);
  99. $_template->cached->exists = !!$_template->cached->timestamp;
  100. if ($_template->cached->exists) {
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. /**
  107. * Empty cache
  108. *
  109. * @param Smarty_Internal_Template $_template template object
  110. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  111. * @return integer number of cache files deleted
  112. */
  113. public function clearAll(Smarty $smarty, $exp_time = null)
  114. {
  115. return $this->clear($smarty, null, null, null, $exp_time);
  116. }
  117. /**
  118. * Empty cache for a specific template
  119. *
  120. * @param Smarty $_template template object
  121. * @param string $resource_name template name
  122. * @param string $cache_id cache id
  123. * @param string $compile_id compile id
  124. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  125. * @return integer number of cache files deleted
  126. */
  127. public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
  128. {
  129. $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
  130. $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
  131. $_dir_sep = $smarty->use_sub_dirs ? '/' : '^';
  132. $_compile_id_offset = $smarty->use_sub_dirs ? 3 : 0;
  133. $_dir = $smarty->getCacheDir();
  134. $_dir_length = strlen($_dir);
  135. if (isset($_cache_id)) {
  136. $_cache_id_parts = explode('|', $_cache_id);
  137. $_cache_id_parts_count = count($_cache_id_parts);
  138. if ($smarty->use_sub_dirs) {
  139. foreach ($_cache_id_parts as $id_part) {
  140. $_dir .= $id_part . DS;
  141. }
  142. }
  143. }
  144. if (isset($resource_name)) {
  145. $_save_stat = $smarty->caching;
  146. $smarty->caching = true;
  147. $tpl = new $smarty->template_class($resource_name, $smarty);
  148. $smarty->caching = $_save_stat;
  149. // remove from template cache
  150. $tpl->source; // have the template registered before unset()
  151. if ($smarty->allow_ambiguous_resources) {
  152. $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id;
  153. } else {
  154. $_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id;
  155. }
  156. if (isset($_templateId[150])) {
  157. $_templateId = sha1($_templateId);
  158. }
  159. unset($smarty->template_objects[$_templateId]);
  160. if ($tpl->source->exists) {
  161. $_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath));
  162. } else {
  163. return 0;
  164. }
  165. }
  166. $_count = 0;
  167. $_time = time();
  168. if (file_exists($_dir)) {
  169. $_cacheDirs = new RecursiveDirectoryIterator($_dir);
  170. $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);
  171. foreach ($_cache as $_file) {
  172. if (substr($_file->getBasename(),0,1) == '.' || strpos($_file, '.svn') !== false) continue;
  173. // directory ?
  174. if ($_file->isDir()) {
  175. if (!$_cache->isDot()) {
  176. // delete folder if empty
  177. @rmdir($_file->getPathname());
  178. }
  179. } else {
  180. $_parts = explode($_dir_sep, str_replace('\\', '/', substr((string)$_file, $_dir_length)));
  181. $_parts_count = count($_parts);
  182. // check name
  183. if (isset($resource_name)) {
  184. if ($_parts[$_parts_count-1] != $_resourcename_parts) {
  185. continue;
  186. }
  187. }
  188. // check compile id
  189. if (isset($_compile_id) && (!isset($_parts[$_parts_count-2 - $_compile_id_offset]) || $_parts[$_parts_count-2 - $_compile_id_offset] != $_compile_id)) {
  190. continue;
  191. }
  192. // check cache id
  193. if (isset($_cache_id)) {
  194. // count of cache id parts
  195. $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : $_parts_count - 1 - $_compile_id_offset;
  196. if ($_parts_count < $_cache_id_parts_count) {
  197. continue;
  198. }
  199. for ($i = 0; $i < $_cache_id_parts_count; $i++) {
  200. if ($_parts[$i] != $_cache_id_parts[$i]) continue 2;
  201. }
  202. }
  203. // expired ?
  204. if (isset($exp_time) && $_time - @filemtime($_file) < $exp_time) {
  205. continue;
  206. }
  207. $_count += @unlink((string) $_file) ? 1 : 0;
  208. }
  209. }
  210. }
  211. return $_count;
  212. }
  213. /**
  214. * Check is cache is locked for this template
  215. *
  216. * @param Smarty $smarty Smarty object
  217. * @param Smarty_Template_Cached $cached cached object
  218. * @return booelan true or false if cache is locked
  219. */
  220. public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
  221. {
  222. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  223. clearstatcache(true, $cached->lock_id);
  224. } else {
  225. clearstatcache();
  226. }
  227. $t = @filemtime($cached->lock_id);
  228. return $t && (time() - $t < $smarty->locking_timeout);
  229. }
  230. /**
  231. * Lock cache for this template
  232. *
  233. * @param Smarty $smarty Smarty object
  234. * @param Smarty_Template_Cached $cached cached object
  235. */
  236. public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
  237. {
  238. $cached->is_locked = true;
  239. touch($cached->lock_id);
  240. }
  241. /**
  242. * Unlock cache for this template
  243. *
  244. * @param Smarty $smarty Smarty object
  245. * @param Smarty_Template_Cached $cached cached object
  246. */
  247. public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
  248. {
  249. $cached->is_locked = false;
  250. @unlink($cached->lock_id);
  251. }
  252. }
  253. ?>