From a77cf5b328705c83ec0ad4b591eaea60fb60bc0c Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Thu, 25 Sep 2025 11:37:54 +0300 Subject: [PATCH] Adds simple admin-tools page --- app/pages/admin-tools.php | 94 +++++++++++++++++++++++++++++++++++ app/templates/admin-tools.php | 69 +++++++++++++++++++++++++ app/templates/page-menu.php | 9 +++- public_html/index.php | 1 + 4 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 app/pages/admin-tools.php create mode 100644 app/templates/admin-tools.php diff --git a/app/pages/admin-tools.php b/app/pages/admin-tools.php new file mode 100644 index 0000000..6d93ca6 --- /dev/null +++ b/app/pages/admin-tools.php @@ -0,0 +1,94 @@ +hasRight($userId, 'superuser'); +} +if (!$canAdmin) { + Feedback::flash('SECURITY', 'PERMISSION_DENIED'); + header('Location: ' . $app_root); + exit; +} + +// Handle actions +$action = $_POST['action'] ?? ''; +if ($action !== '') { + if (!$security->verifyCsrfToken($_POST['csrf_token'] ?? '')) { + Feedback::flash('SECURITY', 'CSRF_INVALID'); + header('Location: ' . $app_root . '?page=admin-tools'); + exit; + } + + try { + if ($action === 'maintenance_on') { + require_once __DIR__ . '/../core/Maintenance.php'; + $msg = trim($_POST['maintenance_message'] ?? ''); + \App\Core\Maintenance::enable($msg); + Feedback::flash('NOTICE', 'DEFAULT', 'Maintenance mode enabled.', true); + } elseif ($action === 'maintenance_off') { + require_once __DIR__ . '/../core/Maintenance.php'; + \App\Core\Maintenance::disable(); + Feedback::flash('NOTICE', 'DEFAULT', 'Maintenance mode disabled.', true); + } elseif ($action === 'migrate_up') { + require_once __DIR__ . '/../core/MigrationRunner.php'; + $migrationsDir = __DIR__ . '/../../doc/database/migrations'; + $runner = new \App\Core\MigrationRunner($db, $migrationsDir); + $applied = $runner->applyPendingMigrations(); + if (empty($applied)) { + Feedback::flash('NOTICE', 'DEFAULT', 'No pending migrations.', true); + } else { + Feedback::flash('NOTICE', 'DEFAULT', 'Applied migrations: ' . implode(', ', $applied), true); + } + } + } catch (Throwable $e) { + Feedback::flash('ERROR', 'DEFAULT', 'Action failed: ' . $e->getMessage(), false); + } + + header('Location: ' . $app_root . '?page=admin-tools'); + exit; +} + +// Prepare data for view +require_once __DIR__ . '/../core/Maintenance.php'; +$maintenance_enabled = \App\Core\Maintenance::isEnabled(); +$maintenance_message = \App\Core\Maintenance::getMessage(); + +require_once __DIR__ . '/../core/MigrationRunner.php'; +$migrationsDir = __DIR__ . '/../../doc/database/migrations'; +$pending = []; +$applied = []; +try { + $runner = new \App\Core\MigrationRunner($db, $migrationsDir); + $pending = $runner->listPendingMigrations(); + $applied = $runner->listAppliedMigrations(); +} catch (Throwable $e) { + // show error in the page + $migration_error = $e->getMessage(); +} + +// CSRF token +$csrf_token = $security->generateCsrfToken(); + +// Get any new feedback messages +include __DIR__ . '/../helpers/feedback.php'; + +// Load the template +include __DIR__ . '/../templates/admin-tools.php'; diff --git a/app/templates/admin-tools.php b/app/templates/admin-tools.php new file mode 100644 index 0000000..7d092b1 --- /dev/null +++ b/app/templates/admin-tools.php @@ -0,0 +1,69 @@ + +
+

Admin tools

+

System maintenance and database utilities.

+ +
+
+
+
Maintenance mode
+
+

Status: +

+
+ + +
+ + +
+ +
+
+ + + +
+
+
+
+ +
+
+
Database migrations
+
+ +
Error:
+ +

+ Pending (): + + None + + + +

+

+ Applied (): + + None + + + +

+
+ + + +
+
+
+
+
+
diff --git a/app/templates/page-menu.php b/app/templates/page-menu.php index 0785f13..9cd30b3 100644 --- a/app/templates/page-menu.php +++ b/app/templates/page-menu.php @@ -69,8 +69,13 @@