Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

smarty_internal_runtime_cacheresourcefile.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Smarty cache resource file clear method
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsInternal
  7. * @author Uwe Tews
  8. */
  9. /**
  10. * Smarty Internal Runtime Cache Resource File Class
  11. *
  12. * @package Smarty
  13. * @subpackage PluginsInternal
  14. */
  15. class Smarty_Internal_Runtime_CacheResourceFile
  16. {
  17. /**
  18. * Empty cache for a specific template
  19. *
  20. * @param Smarty $smarty
  21. * @param string $resource_name template name
  22. * @param string $cache_id cache id
  23. * @param string $compile_id compile id
  24. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  25. *
  26. * @return integer number of cache files deleted
  27. */
  28. public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
  29. {
  30. $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
  31. $_compile_id = isset($compile_id) ? preg_replace('![^\w]+!', '_', $compile_id) : null;
  32. $_dir_sep = $smarty->use_sub_dirs ? '/' : '^';
  33. $_compile_id_offset = $smarty->use_sub_dirs ? 3 : 0;
  34. $_dir = $smarty->getCacheDir();
  35. if ($_dir === '/') { //We should never want to delete this!
  36. return 0;
  37. }
  38. $_dir_length = strlen($_dir);
  39. if (isset($_cache_id)) {
  40. $_cache_id_parts = explode('|', $_cache_id);
  41. $_cache_id_parts_count = count($_cache_id_parts);
  42. if ($smarty->use_sub_dirs) {
  43. foreach ($_cache_id_parts as $id_part) {
  44. $_dir .= $id_part . DIRECTORY_SEPARATOR;
  45. }
  46. }
  47. }
  48. if (isset($resource_name)) {
  49. $_save_stat = $smarty->caching;
  50. $smarty->caching = Smarty::CACHING_LIFETIME_CURRENT;
  51. $tpl = new $smarty->template_class($resource_name, $smarty);
  52. $smarty->caching = $_save_stat;
  53. // remove from template cache
  54. $tpl->source; // have the template registered before unset()
  55. if ($tpl->source->exists) {
  56. $_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath));
  57. } else {
  58. return 0;
  59. }
  60. }
  61. $_count = 0;
  62. $_time = time();
  63. if (file_exists($_dir)) {
  64. $_cacheDirs = new RecursiveDirectoryIterator($_dir);
  65. $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);
  66. foreach ($_cache as $_file) {
  67. if (substr(basename($_file->getPathname()), 0, 1) === '.') {
  68. continue;
  69. }
  70. $_filepath = (string)$_file;
  71. // directory ?
  72. if ($_file->isDir()) {
  73. if (!$_cache->isDot()) {
  74. // delete folder if empty
  75. @rmdir($_file->getPathname());
  76. }
  77. } else {
  78. // delete only php files
  79. if (substr($_filepath, -4) !== '.php') {
  80. continue;
  81. }
  82. $_parts = explode($_dir_sep, str_replace('\\', '/', substr($_filepath, $_dir_length)));
  83. $_parts_count = count($_parts);
  84. // check name
  85. if (isset($resource_name)) {
  86. if ($_parts[ $_parts_count - 1 ] !== $_resourcename_parts) {
  87. continue;
  88. }
  89. }
  90. // check compile id
  91. if (isset($_compile_id) && (!isset($_parts[ $_parts_count - 2 - $_compile_id_offset ]) ||
  92. $_parts[ $_parts_count - 2 - $_compile_id_offset ] !== $_compile_id)
  93. ) {
  94. continue;
  95. }
  96. // check cache id
  97. if (isset($_cache_id)) {
  98. // count of cache id parts
  99. $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset :
  100. $_parts_count - 1 - $_compile_id_offset;
  101. if ($_parts_count < $_cache_id_parts_count) {
  102. continue;
  103. }
  104. for ($i = 0; $i < $_cache_id_parts_count; $i++) {
  105. if ($_parts[ $i ] !== $_cache_id_parts[ $i ]) {
  106. continue 2;
  107. }
  108. }
  109. }
  110. if (is_file($_filepath)) {
  111. // expired ?
  112. if (isset($exp_time)) {
  113. if ($exp_time < 0) {
  114. preg_match('#\'cache_lifetime\' =>\s*(\d*)#', file_get_contents($_filepath), $match);
  115. if ($_time < (filemtime($_filepath) + $match[ 1 ])) {
  116. continue;
  117. }
  118. } else {
  119. if ($_time - filemtime($_filepath) < $exp_time) {
  120. continue;
  121. }
  122. }
  123. }
  124. $_count += @unlink($_filepath) ? 1 : 0;
  125. if (function_exists('opcache_invalidate')
  126. && (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1)
  127. ) {
  128. opcache_invalidate($_filepath, true);
  129. } else if (function_exists('apc_delete_file')) {
  130. apc_delete_file($_filepath);
  131. }
  132. }
  133. }
  134. }
  135. }
  136. return $_count;
  137. }
  138. }