<?php
/**
 * 炎酷CMS - 缓存清理（清空 cache/ 目录下文件）
 */
define('YKCMS', 1);
require dirname(__DIR__) . '/inc/auth.php';

$page_title = '缓存清理';
$active = 'cache';

$cacheDir = SITE_PATH . '/cache';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    csrf_check();
    $num = 0;
    if (is_dir($cacheDir)) {
        $it = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($cacheDir, FilesystemIterator::SKIP_DOTS),
            RecursiveIteratorIterator::CHILD_FIRST
        );
        foreach ($it as $f) {
            if ($f->isFile()) {
                if (@unlink($f->getPathname())) $num++;
            } elseif ($f->isDir()) {
                @rmdir($f->getPathname());
            }
        }
    }
    admin_log('缓存清理', '清理 cache/ 文件 ' . $num . ' 个');
    flash_set('缓存清理完成，共删除 ' . $num . ' 个文件');
    header('Location: cache.php');
    exit;
}

// 统计当前缓存
$fileNum = 0;
$totalSize = 0;
if (is_dir($cacheDir)) {
    $it = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($cacheDir, FilesystemIterator::SKIP_DOTS)
    );
    foreach ($it as $f) {
        if ($f->isFile()) {
            $fileNum++;
            $totalSize += $f->getSize();
        }
    }
}

require dirname(__DIR__) . '/inc/header.php';
?>
<div class="yk-card" style="max-width:560px">
  <h3>缓存清理</h3>
  <table class="yk-table" style="margin-bottom:16px">
    <tr><th width="160">缓存目录</th><td>cache/</td></tr>
    <tr><th>文件数量</th><td><?php echo $fileNum; ?> 个</td></tr>
    <tr><th>占用空间</th><td><?php echo e(number_format($totalSize / 1024, 1)); ?> KB</td></tr>
  </table>
  <form method="post" action="cache.php" onsubmit="return confirm('确定清空缓存目录？');">
    <?php echo csrf_field(); ?>
    <button class="yk-btn red" type="submit">立即清理缓存</button>
  </form>
</div>
<?php require dirname(__DIR__) . '/inc/footer.php'; ?>
