diff --git a/.env.example b/.env.example index 91aeebb..b942ea9 100644 --- a/.env.example +++ b/.env.example @@ -8,3 +8,6 @@ DB_DATABASE=FSS_T # Optional: Basis-URL (wenn du was dynamisch bauen willst) APP_URL=https://fabianschieder.com +# Basic Auth für /adminer (zusätzlicher Schutz) +ADMINER_BASIC_USER=admin +ADMINER_BASIC_PASS=change-me-too diff --git a/adminer/basic_auth.php b/adminer/basic_auth.php new file mode 100644 index 0000000..5d3bf76 --- /dev/null +++ b/adminer/basic_auth.php @@ -0,0 +1,49 @@ + PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + ]); + $dbs = $pdo->query('SHOW DATABASES')->fetchAll(PDO::FETCH_COLUMN, 0); + + $_SESSION['db_admin_select'] = [ + 'host' => $host, + 'port' => $port, + 'user' => $user, + 'pass' => $pass, + 'dbs' => $dbs, + ]; + $selectMsg = 'Datenbanken geladen.'; + } catch (Throwable $e) { + $selectError = 'Konnte Datenbanken nicht laden: ' . $e->getMessage(); + } + } +} + +// Login (Step 2) $error = null; -$success = null; if ($_SERVER['REQUEST_METHOD'] === 'POST' && (string)($_POST['action'] ?? '') === 'login') { $host = trim((string)($_POST['host'] ?? '')); $port = (int)($_POST['port'] ?? 3306); @@ -25,7 +67,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (string)($_POST['action'] ?? '') == $res = admin_try_login($host, $port, $user, $pass, $db); if ($res['ok']) { - $success = 'Login erfolgreich.'; header('Location: /adminer', true, 302); exit; } @@ -33,38 +74,80 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (string)($_POST['action'] ?? '') == } $defaults = admin_default_creds(); +$selectState = is_array($_SESSION['db_admin_select']) ? $_SESSION['db_admin_select'] : []; // UI when not logged in if (!admin_is_logged_in()) { + $prefHost = isset($selectState['host']) ? (string)$selectState['host'] : (string)$defaults['host']; + $prefPort = isset($selectState['port']) ? (int)$selectState['port'] : (int)$defaults['port']; + $prefUser = isset($selectState['user']) ? (string)$selectState['user'] : (string)$defaults['user']; + $prefPass = isset($selectState['pass']) ? (string)$selectState['pass'] : ''; + + $dbList = isset($selectState['dbs']) && is_array($selectState['dbs']) ? $selectState['dbs'] : []; + $body = "

DB-Verwaltung

Mini-Admin
"; $body .= "
"; - $body .= "

Login-Daten werden nur in der Session gespeichert. Für Defaults wird .env aus dem Projekt-Root gelesen.

"; + $body .= "

Zuerst Verbindungsdaten prüfen (ohne DB), dann bekommst du eine Datenbank-Auswahl. Defaults kommen aus .env.

"; + if ($selectMsg) $body .= '
' . h($selectMsg) . '

'; + if ($selectError) $body .= '
' . h($selectError) . '

'; if ($error) $body .= '
' . h($error) . '

'; + // Step 1: Probe + $body .= "

1) Verbindung testen & Datenbanken laden

"; $body .= "
"; - $body .= ""; + $body .= ""; $body .= "
"; - $body .= "
"; - $body .= "

"; - $body .= "

"; - $body .= "

"; - $body .= "

"; - $body .= "

"; - $body .= ""; + $body .= "

"; + $body .= "

"; + $body .= "

"; + $body .= "

"; + $body .= ""; $body .= "
"; $body .= "
"; $body .= "

Hinweise

"; $body .= "
    "; - $body .= "
  • Dieses Tool ist bewusst minimal (Tabellenliste, Browse, SQL Query).
  • "; - $body .= "
  • Für produktive Nutzung bitte zusätzlich absichern (Basic Auth / IP-Allowlist).
  • "; + $body .= "
  • Basic Auth ist aktiv (Credentials in .env).
  • "; + $body .= "
  • Für produktive Nutzung zusätzlich mit IP-Allowlist kombinieren.
  • "; $body .= "
"; $body .= "
"; - $body .= "
"; $body .= "
"; + + // Step 2: Login + $body .= "
"; + $body .= "

2) Login in Datenbank

"; + + $body .= "
"; + $body .= ""; + $body .= "
"; + $body .= "
"; + $body .= "

"; + $body .= "

"; + $body .= "

"; + $body .= "

"; + + if (!empty($dbList)) { + $body .= "

"; + } else { + $body .= "

"; + $body .= "
Tipp: Erst oben \"Datenbanken laden\" klicken für Vorschläge.
"; + } + + $body .= ""; + $body .= "
"; + $body .= "
"; + $body .= "
"; + $body .= "
"; admin_layout('DB-Verwaltung', $body);