You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_extension_clear.php 5.0KB

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