smarty_internal_cacheresource_file.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. $_dir = realpath($smarty->getCacheDir()) . '/';
  143. $_dir_length = strlen($_dir);
  144. if (isset($_cache_id)) {
  145. $_cache_id_parts = explode('|', $_cache_id);
  146. $_cache_id_parts_count = count($_cache_id_parts);
  147. if ($smarty->use_sub_dirs) {
  148. foreach ($_cache_id_parts as $id_part) {
  149. $_dir .= $id_part . DS;
  150. }
  151. }
  152. }
  153. if (isset($resource_name)) {
  154. $_save_stat = $smarty->caching;
  155. $smarty->caching = true;
  156. $tpl = new $smarty->template_class($resource_name, $smarty);
  157. $smarty->caching = $_save_stat;
  158. // remove from template cache
  159. $tpl->source; // have the template registered before unset()
  160. if ($smarty->allow_ambiguous_resources) {
  161. $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id;
  162. } else {
  163. $_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id;
  164. }
  165. if (isset($_templateId[150])) {
  166. $_templateId = sha1($_templateId);
  167. }
  168. unset($smarty->template_objects[$_templateId]);
  169. if ($tpl->source->exists) {
  170. $_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath));
  171. } else {
  172. return 0;
  173. }
  174. }
  175. $_count = 0;
  176. $_time = time();
  177. if (file_exists($_dir)) {
  178. $_cacheDirs = new RecursiveDirectoryIterator($_dir);
  179. $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);
  180. foreach ($_cache as $_file) {
  181. if (substr(basename($_file->getPathname()), 0, 1) == '.' || strpos($_file, '.svn') !== false) {
  182. continue;
  183. }
  184. // directory ?
  185. if ($_file->isDir()) {
  186. if (!$_cache->isDot()) {
  187. // delete folder if empty
  188. @rmdir($_file->getPathname());
  189. }
  190. } else {
  191. $_parts = explode($_dir_sep, str_replace('\\', '/', substr((string) $_file, $_dir_length)));
  192. $_parts_count = count($_parts);
  193. // check name
  194. if (isset($resource_name)) {
  195. if ($_parts[$_parts_count - 1] != $_resourcename_parts) {
  196. continue;
  197. }
  198. }
  199. // check compile id
  200. if (isset($_compile_id) && (!isset($_parts[$_parts_count - 2 - $_compile_id_offset]) || $_parts[$_parts_count - 2 - $_compile_id_offset] != $_compile_id)) {
  201. continue;
  202. }
  203. // check cache id
  204. if (isset($_cache_id)) {
  205. // count of cache id parts
  206. $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : $_parts_count - 1 - $_compile_id_offset;
  207. if ($_parts_count < $_cache_id_parts_count) {
  208. continue;
  209. }
  210. for ($i = 0; $i < $_cache_id_parts_count; $i ++) {
  211. if ($_parts[$i] != $_cache_id_parts[$i]) {
  212. continue 2;
  213. }
  214. }
  215. }
  216. // expired ?
  217. if (isset($exp_time)) {
  218. if ($exp_time < 0) {
  219. preg_match('#\'cache_lifetime\' =>\s*(\d*)#', file_get_contents($_file), $match);
  220. if ($_time < (@filemtime($_file) + $match[1])) {
  221. continue;
  222. }
  223. } else {
  224. if ($_time - @filemtime($_file) < $exp_time) {
  225. continue;
  226. }
  227. }
  228. }
  229. $_count += @unlink((string) $_file) ? 1 : 0;
  230. }
  231. }
  232. }
  233. return $_count;
  234. }
  235. /**
  236. * Check is cache is locked for this template
  237. *
  238. * @param Smarty $smarty Smarty object
  239. * @param Smarty_Template_Cached $cached cached object
  240. *
  241. * @return boolean true or false if cache is locked
  242. */
  243. public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
  244. {
  245. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  246. clearstatcache(true, $cached->lock_id);
  247. } else {
  248. clearstatcache();
  249. }
  250. $t = @filemtime($cached->lock_id);
  251. return $t && (time() - $t < $smarty->locking_timeout);
  252. }
  253. /**
  254. * Lock cache for this template
  255. *
  256. * @param Smarty $smarty Smarty object
  257. * @param Smarty_Template_Cached $cached cached object
  258. *
  259. * @return bool|void
  260. */
  261. public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
  262. {
  263. $cached->is_locked = true;
  264. touch($cached->lock_id);
  265. }
  266. /**
  267. * Unlock cache for this template
  268. *
  269. * @param Smarty $smarty Smarty object
  270. * @param Smarty_Template_Cached $cached cached object
  271. *
  272. * @return bool|void
  273. */
  274. public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
  275. {
  276. $cached->is_locked = false;
  277. @unlink($cached->lock_id);
  278. }
  279. }