PageRenderTime 33ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/core/sm3/sysplugins/smarty_internal_cacheresource_file.php

https://gitlab.com/ilya.webcity/anna
PHP | 259 lines | 203 code | 6 blank | 50 comment | 24 complexity | 7e2aef4d6f139e718cfc40558f616c8c 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. Smarty::muteExpectedErrors();
  63. $cached->timestamp = @filemtime($cached->filepath);
  64. Smarty::unmuteExpectedErrors();
  65. $cached->exists = !!$cached->timestamp;
  66. }
  67. /**
  68. * populate Cached Object with timestamp and exists from Resource
  69. *
  70. * @param Smarty_Template_Cached $cached cached object
  71. * @return void
  72. */
  73. public function populateTimestamp(Smarty_Template_Cached $cached)
  74. {
  75. Smarty::muteExpectedErrors();
  76. $cached->timestamp = @filemtime($cached->filepath);
  77. Smarty::unmuteExpectedErrors();
  78. $cached->exists = !!$cached->timestamp;
  79. }
  80. /**
  81. * Read the cached template and process its header
  82. *
  83. * @param Smarty_Internal_Template $_template template object
  84. * @param Smarty_Template_Cached $cached cached object
  85. * @return booelan true or false if the cached content does not exist
  86. */
  87. public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached=null)
  88. {
  89. $_smarty_tpl = $_template;
  90. return @include $_template->cached->filepath;
  91. }
  92. /**
  93. * Write the rendered template output to cache
  94. *
  95. * @param Smarty_Internal_Template $_template template object
  96. * @param string $content content to cache
  97. * @return boolean success
  98. */
  99. public function writeCachedContent(Smarty_Internal_Template $_template, $content)
  100. {
  101. if (Smarty_Internal_Write_File::writeFile($_template->cached->filepath, $content, $_template->smarty) === true) {
  102. $_template->cached->timestamp = filemtime($_template->cached->filepath);
  103. $_template->cached->exists = !!$_template->cached->timestamp;
  104. return true;
  105. }
  106. return false;
  107. }
  108. /**
  109. * Empty cache
  110. *
  111. * @param Smarty_Internal_Template $_template template object
  112. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  113. * @return integer number of cache files deleted
  114. */
  115. public function clearAll(Smarty $smarty, $exp_time = null)
  116. {
  117. return $this->clear($smarty, null, null, null, $exp_time);
  118. }
  119. /**
  120. * Empty cache for a specific template
  121. *
  122. * @param Smarty $_template template object
  123. * @param string $resource_name template name
  124. * @param string $cache_id cache id
  125. * @param string $compile_id compile id
  126. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  127. * @return integer number of cache files deleted
  128. */
  129. public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
  130. {
  131. $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
  132. $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
  133. $_dir_sep = $smarty->use_sub_dirs ? '/' : '^';
  134. $_compile_id_offset = $smarty->use_sub_dirs ? 3 : 0;
  135. $_dir = $smarty->getCacheDir();
  136. $_dir_length = strlen($_dir);
  137. if (isset($_cache_id)) {
  138. $_cache_id_parts = explode('|', $_cache_id);
  139. $_cache_id_parts_count = count($_cache_id_parts);
  140. if ($smarty->use_sub_dirs) {
  141. foreach ($_cache_id_parts as $id_part) {
  142. $_dir .= $id_part . DS;
  143. }
  144. }
  145. }
  146. if (isset($resource_name)) {
  147. $_save_stat = $smarty->caching;
  148. $smarty->caching = true;
  149. $tpl = new $smarty->template_class($resource_name, $smarty);
  150. $smarty->caching = $_save_stat;
  151. if ($tpl->source->exists) {
  152. $_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath));
  153. // remove from template cache
  154. unset($smarty->template_objects[sha1(join(DIRECTORY_SEPARATOR, $smarty->getTemplateDir()).$tpl->template_resource . $tpl->cache_id . $tpl->compile_id)]);
  155. } else {
  156. // remove from template cache
  157. unset($smarty->template_objects[sha1(join(DIRECTORY_SEPARATOR, $smarty->getTemplateDir()).$tpl->template_resource . $tpl->cache_id . $tpl->compile_id)]);
  158. return 0;
  159. }
  160. }
  161. $_count = 0;
  162. $_time = time();
  163. if (file_exists($_dir)) {
  164. $_cacheDirs = new RecursiveDirectoryIterator($_dir);
  165. $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);
  166. foreach ($_cache as $_file) {
  167. if (substr($_file->getBasename(),0,1) == '.' || strpos($_file, '.svn') !== false) continue;
  168. // directory ?
  169. if ($_file->isDir()) {
  170. if (!$_cache->isDot()) {
  171. // delete folder if empty
  172. @rmdir($_file->getPathname());
  173. }
  174. } else {
  175. $_parts = explode($_dir_sep, str_replace('\\', '/', substr((string)$_file, $_dir_length)));
  176. $_parts_count = count($_parts);
  177. // check name
  178. if (isset($resource_name)) {
  179. if ($_parts[$_parts_count-1] != $_resourcename_parts) {
  180. continue;
  181. }
  182. }
  183. // check compile id
  184. if (isset($_compile_id) && (!isset($_parts[$_parts_count-2 - $_compile_id_offset]) || $_parts[$_parts_count-2 - $_compile_id_offset] != $_compile_id)) {
  185. continue;
  186. }
  187. // check cache id
  188. if (isset($_cache_id)) {
  189. // count of cache id parts
  190. $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : $_parts_count - 1 - $_compile_id_offset;
  191. if ($_parts_count < $_cache_id_parts_count) {
  192. continue;
  193. }
  194. for ($i = 0; $i < $_cache_id_parts_count; $i++) {
  195. if ($_parts[$i] != $_cache_id_parts[$i]) continue 2;
  196. }
  197. }
  198. // expired ?
  199. if (isset($exp_time) && $_time - @filemtime($_file) < $exp_time) {
  200. continue;
  201. }
  202. $_count += @unlink((string) $_file) ? 1 : 0;
  203. }
  204. }
  205. }
  206. return $_count;
  207. }
  208. /**
  209. * Check is cache is locked for this template
  210. *
  211. * @param Smarty $smarty Smarty object
  212. * @param Smarty_Template_Cached $cached cached object
  213. * @return booelan true or false if cache is locked
  214. */
  215. public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
  216. {
  217. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  218. clearstatcache(true, $cached->lock_id);
  219. } else {
  220. clearstatcache();
  221. }
  222. $t = @filemtime($cached->lock_id);
  223. return $t && (time() - $t < $smarty->locking_timeout);
  224. }
  225. /**
  226. * Lock cache for this template
  227. *
  228. * @param Smarty $smarty Smarty object
  229. * @param Smarty_Template_Cached $cached cached object
  230. */
  231. public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
  232. {
  233. $cached->is_locked = true;
  234. touch($cached->lock_id);
  235. }
  236. /**
  237. * Unlock cache for this template
  238. *
  239. * @param Smarty $smarty Smarty object
  240. * @param Smarty_Template_Cached $cached cached object
  241. */
  242. public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
  243. {
  244. $cached->is_locked = false;
  245. @unlink($cached->lock_id);
  246. }
  247. }
  248. ?>