PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/resources/libs/smarty/sysplugins/smarty_internal_cacheresource_file.php

https://gitlab.com/adamlwalker/generatedata
PHP | 264 lines | 233 code | 3 blank | 28 comment | 17 complexity | 14d61f4c108f3969af138333358e2ba4 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. return true;
  101. }
  102. return false;
  103. }
  104. /**
  105. * Empty cache
  106. *
  107. * @param Smarty_Internal_Template $_template template object
  108. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  109. * @return integer number of cache files deleted
  110. */
  111. public function clearAll(Smarty $smarty, $exp_time = null)
  112. {
  113. return $this->clear($smarty, null, null, null, $exp_time);
  114. }
  115. /**
  116. * Empty cache for a specific template
  117. *
  118. * @param Smarty $_template template object
  119. * @param string $resource_name template name
  120. * @param string $cache_id cache id
  121. * @param string $compile_id compile id
  122. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  123. * @return integer number of cache files deleted
  124. */
  125. public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
  126. {
  127. $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
  128. $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
  129. $_dir_sep = $smarty->use_sub_dirs ? '/' : '^';
  130. $_compile_id_offset = $smarty->use_sub_dirs ? 3 : 0;
  131. $_dir = $smarty->getCacheDir();
  132. $_dir_length = strlen($_dir);
  133. if (isset($_cache_id)) {
  134. $_cache_id_parts = explode('|', $_cache_id);
  135. $_cache_id_parts_count = count($_cache_id_parts);
  136. if ($smarty->use_sub_dirs) {
  137. foreach ($_cache_id_parts as $id_part) {
  138. $_dir .= $id_part . DS;
  139. }
  140. }
  141. }
  142. if (isset($resource_name)) {
  143. $_save_stat = $smarty->caching;
  144. $smarty->caching = true;
  145. $tpl = new $smarty->template_class($resource_name, $smarty);
  146. $smarty->caching = $_save_stat;
  147. // remove from template cache
  148. $tpl->source; // have the template registered before unset()
  149. if ($smarty->allow_ambiguous_resources) {
  150. $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id;
  151. } else {
  152. $_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id;
  153. }
  154. if (isset($_templateId[150])) {
  155. $_templateId = sha1($_templateId);
  156. }
  157. unset($smarty->template_objects[$_templateId]);
  158. if ($tpl->source->exists) {
  159. $_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath));
  160. } else {
  161. return 0;
  162. }
  163. }
  164. $_count = 0;
  165. $_time = time();
  166. if (file_exists($_dir)) {
  167. $_cacheDirs = new RecursiveDirectoryIterator($_dir);
  168. $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);
  169. foreach ($_cache as $_file) {
  170. if (substr($_file->getBasename(),0,1) == '.' || strpos($_file, '.svn') !== false) continue;
  171. // directory ?
  172. if ($_file->isDir()) {
  173. if (!$_cache->isDot()) {
  174. // delete folder if empty
  175. @rmdir($_file->getPathname());
  176. }
  177. } else {
  178. $_parts = explode($_dir_sep, str_replace('\\', '/', substr((string)$_file, $_dir_length)));
  179. $_parts_count = count($_parts);
  180. // check name
  181. if (isset($resource_name)) {
  182. if ($_parts[$_parts_count-1] != $_resourcename_parts) {
  183. continue;
  184. }
  185. }
  186. // check compile id
  187. if (isset($_compile_id) && (!isset($_parts[$_parts_count-2 - $_compile_id_offset]) || $_parts[$_parts_count-2 - $_compile_id_offset] != $_compile_id)) {
  188. continue;
  189. }
  190. // check cache id
  191. if (isset($_cache_id)) {
  192. // count of cache id parts
  193. $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : $_parts_count - 1 - $_compile_id_offset;
  194. if ($_parts_count < $_cache_id_parts_count) {
  195. continue;
  196. }
  197. for ($i = 0; $i < $_cache_id_parts_count; $i++) {
  198. if ($_parts[$i] != $_cache_id_parts[$i]) continue 2;
  199. }
  200. }
  201. // expired ?
  202. if (isset($exp_time) && $_time - @filemtime($_file) < $exp_time) {
  203. continue;
  204. }
  205. $_count += @unlink((string) $_file) ? 1 : 0;
  206. }
  207. }
  208. }
  209. return $_count;
  210. }
  211. /**
  212. * Check is cache is locked for this template
  213. *
  214. * @param Smarty $smarty Smarty object
  215. * @param Smarty_Template_Cached $cached cached object
  216. * @return booelan true or false if cache is locked
  217. */
  218. public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
  219. {
  220. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  221. clearstatcache(true, $cached->lock_id);
  222. } else {
  223. clearstatcache();
  224. }
  225. $t = @filemtime($cached->lock_id);
  226. return $t && (time() - $t < $smarty->locking_timeout);
  227. }
  228. /**
  229. * Lock cache for this template
  230. *
  231. * @param Smarty $smarty Smarty object
  232. * @param Smarty_Template_Cached $cached cached object
  233. */
  234. public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
  235. {
  236. $cached->is_locked = true;
  237. touch($cached->lock_id);
  238. }
  239. /**
  240. * Unlock cache for this template
  241. *
  242. * @param Smarty $smarty Smarty object
  243. * @param Smarty_Template_Cached $cached cached object
  244. */
  245. public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
  246. {
  247. $cached->is_locked = false;
  248. @unlink($cached->lock_id);
  249. }
  250. }
  251. ?>