PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/smarty/sysplugins/smarty_internal_cacheresource_file.php

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