PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/system/thirdparty/smarty3/sysplugins/smarty_internal_cacheresource_file.php

https://github.com/nisheeth-barthwal/maveric
PHP | 275 lines | 221 code | 7 blank | 47 comment | 23 complexity | 58a1ffbe08752477c453a03e90227233 MD5 | raw file
  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(basename($_file->getPathname()),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)) {
  205. if ($exp_time < 0) {
  206. preg_match('#\'cache_lifetime\' =>\s*(\d*)#', file_get_contents($_file), $match);
  207. if ($_time < (@filemtime($_file) + $match[1])) {
  208. continue;
  209. }
  210. } else {
  211. if ($_time - @filemtime($_file) < $exp_time) {
  212. continue;
  213. }
  214. }
  215. }
  216. $_count += @unlink((string) $_file) ? 1 : 0;
  217. }
  218. }
  219. }
  220. return $_count;
  221. }
  222. /**
  223. * Check is cache is locked for this template
  224. *
  225. * @param Smarty $smarty Smarty object
  226. * @param Smarty_Template_Cached $cached cached object
  227. * @return booelan true or false if cache is locked
  228. */
  229. public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
  230. {
  231. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  232. clearstatcache(true, $cached->lock_id);
  233. } else {
  234. clearstatcache();
  235. }
  236. $t = @filemtime($cached->lock_id);
  237. return $t && (time() - $t < $smarty->locking_timeout);
  238. }
  239. /**
  240. * Lock cache for this template
  241. *
  242. * @param Smarty $smarty Smarty object
  243. * @param Smarty_Template_Cached $cached cached object
  244. */
  245. public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
  246. {
  247. $cached->is_locked = true;
  248. touch($cached->lock_id);
  249. }
  250. /**
  251. * Unlock cache for this template
  252. *
  253. * @param Smarty $smarty Smarty object
  254. * @param Smarty_Template_Cached $cached cached object
  255. */
  256. public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
  257. {
  258. $cached->is_locked = false;
  259. @unlink($cached->lock_id);
  260. }
  261. }
  262. ?>