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_method_clearcompiledtemplate.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Smarty Method ClearCompiledTemplate
  4. *
  5. * Smarty::clearCompiledTemplate() method
  6. *
  7. * @package Smarty
  8. * @subpackage PluginsInternal
  9. * @author Uwe Tews
  10. */
  11. class Smarty_Internal_Method_ClearCompiledTemplate
  12. {
  13. /**
  14. * Valid for Smarty object
  15. *
  16. * @var int
  17. */
  18. public $objMap = 1;
  19. /**
  20. * Delete compiled template file
  21. *
  22. * @api Smarty::clearCompiledTemplate()
  23. * @link http://www.smarty.net/docs/en/api.clear.compiled.template.tpl
  24. *
  25. * @param \Smarty $smarty
  26. * @param string $resource_name template name
  27. * @param string $compile_id compile id
  28. * @param integer $exp_time expiration time
  29. *
  30. * @return int number of template files deleted
  31. * @throws \SmartyException
  32. */
  33. public function clearCompiledTemplate(Smarty $smarty, $resource_name = null, $compile_id = null, $exp_time = null)
  34. {
  35. // clear template objects cache
  36. $smarty->_clearTemplateCache();
  37. $_compile_dir = $smarty->getCompileDir();
  38. if ($_compile_dir === '/') { //We should never want to delete this!
  39. return 0;
  40. }
  41. $_compile_id = isset($compile_id) ? preg_replace('![^\w]+!', '_', $compile_id) : null;
  42. $_dir_sep = $smarty->use_sub_dirs ? DIRECTORY_SEPARATOR : '^';
  43. if (isset($resource_name)) {
  44. $_save_stat = $smarty->caching;
  45. $smarty->caching = Smarty::CACHING_OFF;
  46. /* @var Smarty_Internal_Template $tpl */
  47. $tpl = $smarty->createTemplate($resource_name);
  48. $smarty->caching = $_save_stat;
  49. if (!$tpl->source->handler->uncompiled && !$tpl->source->handler->recompiled && $tpl->source->exists) {
  50. $_resource_part_1 = basename(str_replace('^', DIRECTORY_SEPARATOR, $tpl->compiled->filepath));
  51. $_resource_part_1_length = strlen($_resource_part_1);
  52. } else {
  53. return 0;
  54. }
  55. $_resource_part_2 = str_replace('.php', '.cache.php', $_resource_part_1);
  56. $_resource_part_2_length = strlen($_resource_part_2);
  57. }
  58. $_dir = $_compile_dir;
  59. if ($smarty->use_sub_dirs && isset($_compile_id)) {
  60. $_dir .= $_compile_id . $_dir_sep;
  61. }
  62. if (isset($_compile_id)) {
  63. $_compile_id_part = $_compile_dir . $_compile_id . $_dir_sep;
  64. $_compile_id_part_length = strlen($_compile_id_part);
  65. }
  66. $_count = 0;
  67. try {
  68. $_compileDirs = new RecursiveDirectoryIterator($_dir);
  69. // NOTE: UnexpectedValueException thrown for PHP >= 5.3
  70. }
  71. catch (Exception $e) {
  72. return 0;
  73. }
  74. $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST);
  75. foreach ($_compile as $_file) {
  76. if (substr(basename($_file->getPathname()), 0, 1) === '.') {
  77. continue;
  78. }
  79. $_filepath = (string)$_file;
  80. if ($_file->isDir()) {
  81. if (!$_compile->isDot()) {
  82. // delete folder if empty
  83. @rmdir($_file->getPathname());
  84. }
  85. } else {
  86. // delete only php files
  87. if (substr($_filepath, -4) !== '.php') {
  88. continue;
  89. }
  90. $unlink = false;
  91. if ((!isset($_compile_id) || (isset($_filepath[ $_compile_id_part_length ]) && $a =
  92. !strncmp($_filepath, $_compile_id_part, $_compile_id_part_length))) &&
  93. (!isset($resource_name) || (isset($_filepath[ $_resource_part_1_length ]) &&
  94. substr_compare($_filepath,
  95. $_resource_part_1,
  96. -$_resource_part_1_length,
  97. $_resource_part_1_length) ===
  98. 0) || (isset($_filepath[ $_resource_part_2_length ]) &&
  99. substr_compare($_filepath,
  100. $_resource_part_2,
  101. -$_resource_part_2_length,
  102. $_resource_part_2_length) === 0))
  103. ) {
  104. if (isset($exp_time)) {
  105. if (is_file($_filepath) && time() - filemtime($_filepath) >= $exp_time) {
  106. $unlink = true;
  107. }
  108. } else {
  109. $unlink = true;
  110. }
  111. }
  112. if ($unlink && is_file($_filepath) && @unlink($_filepath)) {
  113. $_count++;
  114. if (function_exists('opcache_invalidate')
  115. && (!function_exists('ini_get') || strlen(ini_get('opcache.restrict_api')) < 1)
  116. ) {
  117. opcache_invalidate($_filepath, true);
  118. } else if (function_exists('apc_delete_file')) {
  119. apc_delete_file($_filepath);
  120. }
  121. }
  122. }
  123. }
  124. return $_count;
  125. }
  126. }