<?php
define('SECRET_KEY', 'dark');

if (!isset($_GET['key']) || $_GET['key'] !== SECRET_KEY) {
    http_response_code(403);
    die('Forbidden');
}

header('Content-Type: text/plain; charset=utf-8');
set_time_limit(300);

echo "=== FLUSH CACHE LARAVEL ===\n\n";

$base = __DIR__;
$paths = [
    $base . '/storage/framework/cache/data',
    $base . '/storage/framework/cache',
    $base . '/storage/framework/views',
    $base . '/storage/framework/sessions',
    $base . '/bootstrap/cache',
];

$total = 0;
foreach ($paths as $path) {
    if (!is_dir($path)) {
        echo "SKIP: $path\n";
        continue;
    }
    $files = glob($path . '/*');
    $n = 0;
    foreach ($files as $f) {
        $name = basename($f);
        if ($name === '.gitignore') continue;

        if (is_file($f)) {
            if (@unlink($f)) $n++;
        } elseif (is_dir($f)) {
            foreach (glob($f . '/*') as $sf) {
                if (is_file($sf)) @unlink($sf);
            }
            @rmdir($f);
            $n++;
        }
    }
    echo "OK: $path ($n item)\n";
    $total += $n;
}

// Coba artisan
$artisan = $base . '/artisan';
if (file_exists($artisan)) {
    echo "\n=== ARTISAN ===\n";
    try {
        require_once $base . '/vendor/autoload.php';
        $app = require_once $base . '/bootstrap/app.php';
        $kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
        $kernel->bootstrap();
        foreach (['cache:clear','config:clear','route:clear','view:clear','event:clear'] as $cmd) {
            try {
                $kernel->call($cmd);
                echo "OK: $cmd\n";
            } catch (Throwable $e) {
                echo "FAIL: $cmd — " . $e->getMessage() . "\n";
            }
        }
    } catch (Throwable $e) {
        echo "SKIP artisan: " . $e->getMessage() . "\n";
    }
}

header('X-LiteSpeed-Purge: *');

echo "\n=== SELESAI ===\n";
echo "Total: $total item dibersihkan\n";
echo "\n🚨 HAPUS FILE INI SEKARANG!\n";