From f402e0c96a52c73ab390b76f637af3ccde6ac8b2 Mon Sep 17 00:00:00 2001
From: UMTS at Teleco <umts@teleco.ch>
Date: Fri, 14 Feb 2025 03:12:25 +0100
Subject: initial upload ... somewhat cleaned up and "less" swearing included

---
 admin/tools/edit_database.php | 361 ++++++++++++++++++++++++++++++++++++++++++
 admin/tools/edit_row.php      |  80 ++++++++++
 admin/tools/manage_files.php  | 222 ++++++++++++++++++++++++++
 3 files changed, 663 insertions(+)
 create mode 100644 admin/tools/edit_database.php
 create mode 100644 admin/tools/edit_row.php
 create mode 100644 admin/tools/manage_files.php

(limited to 'admin/tools')

diff --git a/admin/tools/edit_database.php b/admin/tools/edit_database.php
new file mode 100644
index 0000000..bc5489a
--- /dev/null
+++ b/admin/tools/edit_database.php
@@ -0,0 +1,361 @@
+<?php
+session_start();
+
+// Config lade
+$config = include(__DIR__ . '/../../config.php');
+
+// WebsiteID us der URL oder session hole 
+$websiteId = isset($_GET['websiteId']) ? (int)$_GET['websiteId'] : (isset($_SESSION['websiteId']) ? (int)$_SESSION['websiteId'] : 1);
+$_SESSION['websiteId'] = $websiteId;
+
+// Website config ider config finde
+$websiteConfig = array_filter($config, function($site) use ($websiteId) {
+    return isset($site['id']) && $site['id'] === $websiteId;
+});
+$websiteConfig = reset($websiteConfig);
+
+// Sqlite datenbank ahfigge
+$db = new SQLite3($websiteConfig['database']);
+
+// Backup erstelle oder ganzi datehbank lôsche *evil laugh* 😈 jk mer propbieret sehr schlecht IDs wieder sekuentiel zmache
+if (isset($_POST['backup']) || isset($_POST['delete_row']) || isset($_POST['reassign_ids'])) {
+    $source = $websiteConfig['database'];
+    $backupDir = __DIR__ . '/../../' . $websiteConfig['backup_folder'];
+    $timestamp = date('Y-m-d-H-i-s');
+    $destination = $backupDir . '/' . $timestamp . '.db';
+
+    // machen das backup directory wenns nid git
+    if (!is_dir($backupDir)) {
+        mkdir($backupDir, 0755, true);
+    }
+
+    // omg file deht ineh kopiere 😱
+    if (copy($source, $destination)) {
+        $backupMessage = "Database backup created successfully: " . htmlspecialchars($destination);
+    } else {
+        $backupMessage = "Failed to create database backup.";
+    }
+}
+
+$selectedTable = $_POST['table'] ?? $_SESSION['selectedTable'] ?? '';
+$columns = [];
+$rows = [];
+$sortOrder = $_GET['sortOrder'] ?? 'ASC';
+
+$tablesResult = $db->query("SELECT name FROM sqlite_master WHERE type='table' AND name != 'sqlite_sequence'");
+$tables = [];
+while ($row = $tablesResult->fetchArray(SQLITE3_ASSOC)) {
+    $tables[] = $row['name'];
+}
+
+if ($selectedTable && in_array($selectedTable, $tables)) {
+    $_SESSION['selectedTable'] = $selectedTable;
+    $columnsResult = $db->query("PRAGMA table_info($selectedTable)");
+    while ($row = $columnsResult->fetchArray(SQLITE3_ASSOC)) {
+        $columns[] = $row['name'];
+    }
+    if ($selectedTable == 'content' && in_array('date', $columns)) {
+        $orderBy = "ORDER BY date DESC"; // das hier machen default sorting nach datum und ziiht de neuschti row zerscht will susch isch das unmanagebar
+    } else {
+        $orderBy = in_array('date', $columns) ? "ORDER BY date $sortOrder" : "ORDER BY id ASC";
+    }
+    $rowsResult = $db->query("SELECT * FROM $selectedTable $orderBy");
+    while ($row = $rowsResult->fetchArray(SQLITE3_ASSOC)) {
+        $rows[] = $row;
+    }
+} else {
+    $selectedTable = '';
+    $_SESSION['selectedTable'] = '';
+}
+
+if (isset($_POST['edit_row'])) {
+    header("Location: edit_row.php?table=$selectedTable&id=" . $_POST['id']);
+    exit();
+}
+
+if (isset($_POST['delete_row'])) {
+    $idsToDelete = $_POST['ids'] ?? [];
+    foreach ($idsToDelete as $id) {
+        $stmt = $db->prepare("DELETE FROM $selectedTable WHERE id = :id");
+        $stmt->bindValue(':id', $id, SQLITE3_INTEGER);
+        $stmt->execute();
+    }
+    $message = "Selected rows deleted successfully (most likely).";
+}
+
+if (isset($_POST['insert_row'])) {
+    $values = $_POST['values'];
+    $highestIdResult = $db->querySingle("SELECT MAX(id) as max_id FROM $selectedTable");
+    $highestId = $highestIdResult ? $highestIdResult : 0;
+    $values['id'] = $highestId + 1;
+    if ($selectedTable == 'content') {
+        if (empty($values['date'])) {
+            $values['date'] = date('Y-m-d H:i:s');
+        }
+    }
+    $columnsString = implode(", ", array_keys($values));
+    $placeholders = implode(", ", array_fill(0, count($values), "?"));
+    $stmt = $db->prepare("INSERT INTO $selectedTable ($columnsString) VALUES ($placeholders)");
+    $index = 1;
+    foreach ($values as $value) {
+        $stmt->bindValue($index, $value ?: null, SQLITE3_TEXT);
+        $index++;
+    }
+    $stmt->execute();
+    $message = "Row inserted successfully. (maybe)";
+}
+
+if (isset($_POST['swap_row'])) {
+    $id = $_POST['id'];
+    $targetId = $_POST['target_id'];
+    $db->exec("BEGIN TRANSACTION");
+    $db->exec("UPDATE $selectedTable SET id = -1 WHERE id = $id");
+    $db->exec("UPDATE $selectedTable SET id = $id WHERE id = $targetId");
+    $db->exec("UPDATE $selectedTable SET id = $targetId WHERE id = -1");
+    $db->exec("COMMIT");
+    $message = "Row swapped successfully for sure.";
+}
+
+if (isset($_POST['push_row'])) {
+    $id = $_POST['id'];
+    $targetId = $_POST['target_id'];
+    $tempTable = $selectedTable . '_temp';
+    $db->exec("BEGIN TRANSACTION");
+    $db->exec("CREATE TEMPORARY TABLE $tempTable AS SELECT * FROM $selectedTable");
+    $db->exec("UPDATE $tempTable SET id = -1 WHERE id = $id");
+    if ($id < $targetId) {
+        $db->exec("UPDATE $tempTable SET id = id - 1 WHERE id > $id AND id <= $targetId");
+    } else {
+        $db->exec("UPDATE $tempTable SET id = id + 1 WHERE id < $id AND id >= $targetId");
+    }
+    $db->exec("UPDATE $tempTable SET id = $targetId WHERE id = -1");
+    $db->exec("DELETE FROM $selectedTable");
+    $db->exec("INSERT INTO $selectedTable SELECT * FROM $tempTable");
+    $db->exec("DROP TABLE $tempTable");
+    $db->exec("COMMIT");
+    $message = "Row pushed successfully (perhaps).";
+}
+
+if (isset($_POST['reassign_ids'])) {
+    $tempBackup = $backupDir . '/' . $timestamp . '_temp.db';
+    copy($source, $tempBackup);
+
+    try {
+        $db->exec("BEGIN TRANSACTION");
+        $tempTable = $selectedTable . '_temp';
+        $db->exec("CREATE TEMPORARY TABLE $tempTable AS SELECT * FROM $selectedTable");
+        $db->exec("DELETE FROM $selectedTable");
+
+        if (in_array('date', $columns)) {
+            $orderBy = "ORDER BY date ASC";
+        } else {
+            $orderBy = "ORDER BY id ASC";
+        }
+
+        $rowsResult = $db->query("SELECT * FROM $tempTable $orderBy");
+        $newId = 1;
+        while ($row = $rowsResult->fetchArray(SQLITE3_ASSOC)) {
+            $row['id'] = $newId++;
+            $columnsString = implode(", ", array_keys($row));
+            $placeholders = implode(", ", array_fill(0, count($row), "?"));
+            $stmt = $db->prepare("INSERT INTO $selectedTable ($columnsString) VALUES ($placeholders)");
+            $index = 1;
+            foreach ($row as $value) {
+                $stmt->bindValue($index, $value ?: null, SQLITE3_TEXT);
+                $index++;
+            }
+            $stmt->execute();
+        }
+
+        $db->exec("DROP TABLE $tempTable");
+        $db->exec("COMMIT");
+        $message = "IDs reassigned successfully.";
+    } catch (Exception $e) {
+        copy($tempBackup, $source);
+        $message = "Failed to reassign IDs. Database restored from backup. (probably)";
+    } finally {
+        unlink($tempBackup);
+    }
+}
+?>
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Edit Database</title>
+</head>
+<body>
+    <form method="post" action="../index.php">
+        <button type="submit">Back to Admin Panel</button>
+    </form>
+    <h1>Edit Database</h1>
+    <form method="get" action="edit_database.php">
+        <label for="websiteId">Select Website:</label>
+        <select name="websiteId" id="websiteId" onchange="this.form.submit()">
+            <?php foreach ($config as $site): ?>
+                <?php if (isset($site['id'])): ?>
+                    <option value="<?php echo $site['id']; ?>" <?php echo ($websiteId == $site['id']) ? 'selected' : ''; ?>>
+                        <?php echo htmlspecialchars($site['name']); ?>
+                    </option>
+                <?php endif; ?>
+            <?php endforeach; ?>
+        </select>
+    </form>
+    <form method="post">
+        <button type="submit" name="backup">Backup das Database</button>
+    </form>
+    <?php if (isset($backupMessage)): ?>
+        <p><?php echo $backupMessage; ?></p>
+    <?php endif; ?>
+    <form method="post">
+        <label for="table">Select das Table:</label>
+        <select id="table" name="table" onchange="this.form.submit()">
+            <option value="">-- Select a table --</option>
+            <?php foreach ($tables as $table): ?>
+                <option value="<?php echo htmlspecialchars($table); ?>" <?php echo ($selectedTable == $table) ? 'selected' : ''; ?>><?php echo htmlspecialchars($table); ?></option>
+            <?php endforeach; ?>
+        </select>
+    </form>
+    <form method="post">
+        <button type="submit" name="refresh">Refresh</button>
+    </form>
+    <?php if ($selectedTable): ?>
+        <h2>Editing Table: <?php echo htmlspecialchars($selectedTable); ?></h2>
+        <?php if ($selectedTable == 'content'): ?>
+            <form method="post">
+                <input type="hidden" name="table" value="<?php echo htmlspecialchars($selectedTable); ?>">
+                <table border="1">
+                    <thead>
+                        <tr>
+                            <th>Select</th>
+                            <?php foreach ($columns as $column): ?>
+                                <th>
+                                    <?php echo htmlspecialchars($column); ?>
+                                    <?php if ($column == 'date'): ?>
+                                        <a href="?table=<?php echo htmlspecialchars($selectedTable); ?>&sortOrder=<?php echo $sortOrder == 'ASC' ? 'DESC' : 'ASC'; ?>">Sort by Date</a>
+                                    <?php endif; ?>
+                                </th>
+                            <?php endforeach; ?>
+                            <th>Actions</th>
+                        </tr>
+                    </thead>
+                    <tbody>
+                        <tr>
+                            <td></td>
+                            <?php foreach ($columns as $column): ?>
+                                <td>
+                                    <?php if ($column == 'id'): ?>
+                                        <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]" disabled>
+                                    <?php elseif ($column == 'date'): ?>
+                                        <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]" value="<?php echo date('Y-m-d H:i:s'); ?>" disabled>
+                                        <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]">
+                                    <?php endif; ?>
+                                </td>
+                            <?php endforeach; ?>
+                            <td>
+                                <button type="submit" name="insert_row">Add Row</button>
+                            </td>
+                        </tr>
+                        <?php foreach ($rows as $row): ?>
+                            <tr>
+                                <td><input type="checkbox" name="ids[]" value="<?php echo $row['id']; ?>"></td>
+                                <?php foreach ($columns as $column): ?>
+                                    <td><?php echo htmlspecialchars($row[$column] ?? ''); ?></td>
+                                <?php endforeach; ?>
+                                <td>
+                                    <form method="post" style="display:inline;">
+                                        <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
+                                        <button type="submit" name="edit_row">Edit</button>
+                                    </form>
+                                    <form method="post" style="display:inline;">
+                                        <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
+                                        <button type="submit" name="delete_row">Delete</button>
+                                    </form>
+                                    <form method="post" style="display:inline;">
+                                        <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
+                                        <input type="text" name="target_id" placeholder="Target ID">
+                                        <button type="submit" name="swap_row">Swap</button>
+                                        <button type="submit" name="push_row">Push</button>
+                                    </form>
+                                </td>
+                            </tr>
+                        <?php endforeach; ?>
+                    </tbody>
+                </table>
+                <button type="submit" name="delete_row">Maybe Delete Selected Rows</button>
+                <button type="submit" name="reassign_ids">Maybe Reassign IDs</button>
+            </form>
+        <?php else: ?>
+            <form method="post">
+                <input type="hidden" name="table" value="<?php echo htmlspecialchars($selectedTable); ?>">
+                <table border="1">
+                    <thead>
+                        <tr>
+                            <th>Select</th>
+                            <?php foreach ($columns as $column): ?>
+                                <th>
+                                    <?php echo htmlspecialchars($column); ?>
+                                    <?php if ($column == 'date'): ?>
+                                        <a href="?table=<?php echo htmlspecialchars($selectedTable); ?>&sortOrder=<?php echo $sortOrder == 'ASC' ? 'DESC' : 'ASC'; ?>">Sort by Datum</a>
+                                    <?php endif; ?>
+                                </th>
+                            <?php endforeach; ?>
+                            <th>Actions</th>
+                        </tr>
+                    </thead>
+                    <tbody>
+                        <?php foreach ($rows as $row): ?>
+                            <tr>
+                                <td><input type="checkbox" name="ids[]" value="<?php echo $row['id']; ?>"></td>
+                                <?php foreach ($columns as $column): ?>
+                                    <td><?php echo htmlspecialchars($row[$column] ?? ''); ?></td>
+                                <?php endforeach; ?>
+                                <td>
+                                    <form method="post" style="display:inline;">
+                                        <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
+                                        <button type="submit" name="edit_row">Edit</button>
+                                    </form>
+                                    <form method="post" style="display:inline;">
+                                        <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
+                                        <button type="submit" name="delete_row">Bye</button>
+                                    </form>
+                                    <form method="post" style="display:inline;">
+                                        <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
+                                        <input type="text" name="target_id" placeholder="Target ID">
+                                        <button type="submit" name="swap_row">Swap</button>
+                                        <button type="submit" name="push_row">Push</button>
+                                    </form>
+                                </td>
+                            </tr>
+                        <?php endforeach; ?>
+                        <tr>
+                            <td></td>
+                            <?php foreach ($columns as $column): ?>
+                                <td>
+                                    <?php if ($column == 'id'): ?>
+                                        <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]" disabled>
+                                    <?php elseif ($column == 'date'): ?>
+                                        <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]" value="<?php echo date('Y-m-d H:i:s'); ?>" disabled>
+                                    <?php elseif ($selectedTable == 'content' && $column == 'custom_html'): ?>
+                                        <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]" value="0">
+                                    <?php else: ?>
+                                        <input type="text" name="values[<?php echo htmlspecialchars($column); ?>]">
+                                    <?php endif; ?>
+                                </td>
+                            <?php endforeach; ?>
+                            <td>
+                                <button type="submit" name="insert_row">Add Row</button>
+                            </td>
+                        </tr>
+                    </tbody>
+                </table>
+                <button type="submit" name="delete_row">Delete Selected Rows</button>
+                <button type="submit" name="reassign_ids">Reassign IDs</button>
+            </form>
+        <?php endif; ?>
+    <?php else: ?>
+        <p>No das table selected. Please select das table to editieren.</p>
+    <?php endif; ?>
+</body>
+</html>
\ No newline at end of file
diff --git a/admin/tools/edit_row.php b/admin/tools/edit_row.php
new file mode 100644
index 0000000..ceb59f3
--- /dev/null
+++ b/admin/tools/edit_row.php
@@ -0,0 +1,80 @@
+<?php
+session_start();
+
+// Load configuration
+$config = include(__DIR__ . '/../../config.php');
+
+// Get the website ID from the session
+$websiteId = $_SESSION['websiteId'] ?? 1;
+
+// Find the website configuration
+$websiteConfig = array_filter($config, function($site) use ($websiteId) {
+    return isset($site['id']) && $site['id'] === $websiteId;
+});
+$websiteConfig = reset($websiteConfig);
+
+// Connect to SQLite database
+$db = new SQLite3($websiteConfig['database']);
+
+// Get the table and ID from the request
+$table = $_GET['table'] ?? '';
+$id = $_GET['id'] ?? 0;
+
+$columns = [];
+$columnsResult = $db->query("PRAGMA table_info($table)");
+while ($row = $columnsResult->fetchArray(SQLITE3_ASSOC)) {
+    $columns[] = $row['name'];
+}
+
+$stmt = $db->prepare("SELECT * FROM $table WHERE id = :id");
+$stmt->bindValue(':id', $id, SQLITE3_INTEGER);
+$result = $stmt->execute();
+
+if ($result) {
+    $row = $result->fetchArray(SQLITE3_ASSOC);
+} else {
+    $row = null;
+}
+
+if (isset($_POST['save_changes'])) {
+    foreach ($columns as $column) {
+        $value = $_POST[$column] ?: null;
+        if ($column == 'date' && empty($value)) {
+            $value = date('Y-m-d H:i:s');
+        }
+        if ($table == 'content' && $column == 'custom_html') {
+            $value = $value ? 1 : 0;
+        }
+        $stmt = $db->prepare("UPDATE $table SET $column = :value WHERE id = :id");
+        $stmt->bindValue(':value', $value, SQLITE3_TEXT);
+        $stmt->bindValue(':id', $id, SQLITE3_INTEGER);
+        $stmt->execute();
+    }
+    header("Location: edit_database.php?table=$table");
+    exit();
+}
+?>
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Edit Row</title>
+</head>
+<body>
+    <h1>Edit Row in Table: <?php echo htmlspecialchars($table); ?></h1>
+    <form method="post">
+        <?php foreach ($columns as $column): ?>
+            <label for="<?php echo htmlspecialchars($column); ?>"><?php echo htmlspecialchars($column); ?>:</label>
+            <?php if ($table == 'content' && $column == 'content'): ?>
+                <textarea id="<?php echo htmlspecialchars($column); ?>" name="<?php echo htmlspecialchars($column); ?>" style="resize: both;"><?php echo htmlspecialchars($row[$column] ?? ''); ?></textarea>
+            <?php else: ?>
+                <input type="text" id="<?php echo htmlspecialchars($column); ?>" name="<?php echo htmlspecialchars($column); ?>" value="<?php echo htmlspecialchars($row[$column] ?? ''); ?>">
+            <?php endif; ?>
+            <br>
+        <?php endforeach; ?>
+        <button type="submit" name="save_changes">Save Changes</button>
+    </form>
+    <a href="edit_database.php?table=<?php echo htmlspecialchars($table); ?>">Back to Table</a>
+</body>
+</html>
diff --git a/admin/tools/manage_files.php b/admin/tools/manage_files.php
new file mode 100644
index 0000000..f7d9592
--- /dev/null
+++ b/admin/tools/manage_files.php
@@ -0,0 +1,222 @@
+<?php
+session_start();
+
+// Load configuration
+$config = include(__DIR__ . '/../../config.php');
+
+// Get the website ID from the request or session
+$websiteId = isset($_GET['websiteId']) ? (int)$_GET['websiteId'] : (isset($_SESSION['websiteId']) ? (int)$_SESSION['websiteId'] : 1);
+$_SESSION['websiteId'] = $websiteId;
+
+// Find the website configuration
+$websiteConfig = array_filter($config, function($site) use ($websiteId) {
+    return isset($site['id']) && $site['id'] === $websiteId;
+});
+$websiteConfig = reset($websiteConfig);
+
+// Enable error reporting
+error_reporting(E_ALL);
+ini_set('display_errors', 1);
+
+$rootDir = realpath(__DIR__ . '/../../' . $websiteConfig['folder']);
+$currentDir = isset($_GET['dir']) ? realpath($rootDir . '/' . $_GET['dir']) : $rootDir;
+
+// Ensure the current directory is within the root directory
+if (strpos($currentDir, $rootDir) !== 0) {
+    $currentDir = $rootDir;
+}
+
+// Handle file upload
+if (isset($_POST['upload'])) {
+    $targetFile = $currentDir . '/' . basename($_FILES['file']['name']);
+    if (move_uploaded_file($_FILES['file']['tmp_name'], $targetFile)) {
+        $message = "File uploaded successfully.";
+    } else {
+        $message = "Failed to upload file.";
+    }
+}
+
+// Handle file deletion
+if (isset($_POST['delete'])) {
+    $fileToDelete = $currentDir . '/' . $_POST['filename'];
+    if (unlink($fileToDelete)) {
+        $message = "File deleted successfully.";
+    } else {
+        $message = "Failed to delete file.";
+    }
+}
+
+// Handle file renaming
+if (isset($_POST['rename'])) {
+    $oldName = $currentDir . '/' . $_POST['oldname'];
+    $newName = $currentDir . '/' . $_POST['newname'];
+    if (rename($oldName, $newName)) {
+        $message = "File renamed successfully.";
+    } else {
+        $message = "Failed to rename file.";
+    }
+}
+
+// Handle folder creation
+if (isset($_POST['create_folder'])) {
+    $newFolder = $currentDir . '/' . $_POST['foldername'];
+    if (mkdir($newFolder, 0755, true)) {
+        $message = "Folder created successfully.";
+    } else {
+        $message = "Failed to create folder.";
+    }
+}
+
+// Handle folder deletion
+if (isset($_POST['delete_folder'])) {
+    $folderToDelete = $currentDir . '/' . $_POST['foldername'];
+    if (rmdir($folderToDelete)) {
+        $message = "Folder deleted successfully.";
+    } else {
+        $message = "Failed to delete folder.";
+    }
+}
+
+// Handle folder backup
+if (isset($_POST['backup_folder'])) {
+    $backupDir = realpath(__DIR__ . '/../../' . $websiteConfig['backup_folder']);
+    $backupFile = $backupDir . '/' . basename($currentDir) . '_' . date('Ymd_His') . '.tar';
+    $command = "tar -cf $backupFile -C " . escapeshellarg($currentDir) . " .";
+    exec($command, $output, $returnVar);
+    if ($returnVar === 0) {
+        $message = "Folder backed up successfully.";
+    } else {
+        $message = "Failed to back up folder.";
+    }
+}
+
+// Get list of files and directories
+$items = scandir($currentDir);
+
+// Get relative path for display
+$relativePath = str_replace($rootDir, '', $currentDir);
+if ($relativePath === '') {
+    $relativePath = '/';
+}
+?>
+
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Manage Files</title>
+</head>
+<body>
+    <div class="container">
+        <form method="post" action="../index.php">
+            <button type="submit">Back to Admin Panel</button>
+        </form>
+        <h1>File Manager</h1>
+        <form method="get" action="manage_files.php">
+            <label for="websiteId">Select Website:</label>
+            <select name="websiteId" id="websiteId" onchange="this.form.submit()">
+                <?php foreach ($config as $site): ?>
+                    <?php if (isset($site['id'])): ?>
+                        <option value="<?php echo $site['id']; ?>" <?php echo ($websiteId == $site['id']) ? 'selected' : ''; ?>>
+                            <?php echo htmlspecialchars($site['name']); ?>
+                        </option>
+                    <?php endif; ?>
+                <?php endforeach; ?>
+            </select>
+        </form>
+        <?php if (isset($message)): ?>
+            <p class="message"><?php echo $message; ?></p>
+        <?php endif; ?>
+        <h2>Current Directory: <?php echo htmlspecialchars($relativePath); ?></h2>
+        <form method="post">
+            <button type="submit" name="refresh">Refresh</button>
+        </form>
+        <table border="1">
+            <thead>
+                <tr>
+                    <th colspan="2"></th>
+                </tr>
+            </thead>
+            <tbody>
+                <?php if ($currentDir !== $rootDir): ?>
+                    <tr>
+                        <td colspan="2"><a href="?dir=<?php echo urlencode(dirname($relativePath)); ?>&websiteId=<?php echo $websiteId; ?>">.. (Parent Directory)</a></td>
+                    </tr>
+                <?php endif; ?>
+                <tr>
+                    <td colspan="2" style="text-align: center; background-color: yellow;"><strong>Folders</strong></td>
+                </tr>
+                <?php foreach ($items as $item): ?>
+                    <?php if ($item === '.' || $item === '..') continue; ?>
+                    <?php if (is_dir($currentDir . '/' . $item)): ?>
+                        <tr>
+                            <td><a href="?dir=<?php echo urlencode(ltrim($relativePath . '/' . $item, '/')); ?>&websiteId=<?php echo $websiteId; ?>"><?php echo htmlspecialchars($item); ?></a></td>
+                            <td class="actions">
+                                <form method="post" style="display:inline;">
+                                    <input type="hidden" name="foldername" value="<?php echo htmlspecialchars($item); ?>">
+                                    <button type="submit" name="delete_folder">Delete</button>
+                                </form>
+                                <form method="post" style="display:inline;">
+                                    <input type="hidden" name="oldname" value="<?php echo htmlspecialchars($item); ?>">
+                                    <input type="text" name="newname" placeholder="New name">
+                                    <button type="submit" name="rename">Rename</button>
+                                </form>
+                            </td>
+                        </tr>
+                    <?php endif; ?>
+                <?php endforeach; ?>
+                <tr>
+                    <td colspan="2" style="text-align: center; background-color: orange;"><strong>Files</strong></td>
+                </tr>
+                <?php foreach ($items as $item): ?>
+                    <?php if ($item === '.' || $item === '..') continue; ?>
+                    <?php if (!is_dir($currentDir . '/' . $item)): ?>
+                        <tr>
+                            <td><?php echo htmlspecialchars($item); ?></td>
+                            <td class="actions">
+                                <form method="post" style="display:inline;">
+                                    <input type="hidden" name="filename" value="<?php echo htmlspecialchars($item); ?>">
+                                    <button type="submit" name="delete">Delete</button>
+                                </form>
+                                <form method="post" style="display:inline;">
+                                    <input type="hidden" name="oldname" value="<?php echo htmlspecialchars($item); ?>">
+                                    <input type="text" name="newname" placeholder="New name">
+                                    <button type="submit" name="rename">Rename</button>
+                                </form>
+                                <a href="../../<?php echo htmlspecialchars($websiteConfig['folder'] . '/' . $relativePath . '/' . $item); ?>" download>Download</a>
+                            </td>
+                        </tr>
+                    <?php endif; ?>
+                <?php endforeach; ?>
+                <tr>
+                    <td colspan="2">
+                        <h2>Upload File</h2>
+                        <form method="post" enctype="multipart/form-data">
+                            <input type="file" name="file">
+                            <button type="submit" name="upload">Upload</button>
+                        </form>
+                    </td>
+                </tr>
+                <tr>
+                    <td colspan="2">
+                        <h2>Create Folder</h2>
+                        <form method="post">
+                            <input type="text" name="foldername" placeholder="Folder name">
+                            <button type="submit" name="create_folder">Create</button>
+                        </form>
+                    </td>
+                </tr>
+                <tr>
+                    <td colspan="2">
+                        <h2>Backup Folder</h2>
+                        <form method="post">
+                            <button type="submit" name="backup_folder">Backup Folder</button>
+                        </form>
+                    </td>
+                </tr>
+            </tbody>
+        </table>
+    </div>
+</body>
+</html>
-- 
cgit v1.2.3-70-g09d2