smarty_internal_cacheresource_file.php 11 KB

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