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; if ($_SERVER['REQUEST_METHOD'] === 'POST' && (string)($_POST['action'] ?? '') === 'login') { $host = trim((string)($_POST['host'] ?? '')); $port = (int)($_POST['port'] ?? 3306); $user = trim((string)($_POST['user'] ?? '')); $pass = (string)($_POST['pass'] ?? ''); $db = trim((string)($_POST['db'] ?? '')); $res = admin_try_login($host, $port, $user, $pass, $db); if ($res['ok']) { header('Location: /adminer', true, 302); exit; } $error = (string)($res['error'] ?? 'Login fehlgeschlagen.'); } $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 .= "

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 .= "

Hinweise

"; $body .= "
    "; $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); exit; } // Logged-in area try { $pdo = admin_pdo(); $table = (string)($_GET['t'] ?? ''); $page = max(1, (int)($_GET['p'] ?? 1)); $limit = 50; $offset = ($page - 1) * $limit; $msg = null; $queryResultHtml = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST' && (string)($_POST['action'] ?? '') === 'query') { $sql = trim((string)($_POST['sql'] ?? '')); if ($sql !== '') { // Allow multiple statements? No. Keep minimal & safer. if (preg_match('/;\s*\S/', $sql)) { $msg = ['type' => 'err', 'text' => 'Bitte nur ein Statement ohne zusätzliche Semikolons ausführen.']; } else { try { $stmt = $pdo->query($sql); if ($stmt instanceof PDOStatement) { $rows = $stmt->fetchAll(); $queryResultHtml .= '

Ergebnis

'; $queryResultHtml .= admin_render_table($rows); $msg = ['type' => 'ok', 'text' => 'Query ausgeführt.']; } else { $msg = ['type' => 'ok', 'text' => 'Statement ausgeführt.']; } } catch (Throwable $e) { $msg = ['type' => 'err', 'text' => 'Fehler: ' . $e->getMessage()]; } } } } // Build left nav tables $tables = $pdo->query('SHOW TABLES')->fetchAll(PDO::FETCH_NUM); $body = '
' . '

DB-Verwaltung

eingeloggt
' . '
Logout
' . '
'; $body .= '
'; $body .= '
' . '

Tabellen

' . '
Klick zum Anzeigen
'; if (empty($tables)) { $body .= '
Keine Tabellen gefunden.
'; } else { $body .= ''; } $body .= '
'; $body .= '
'; if ($msg) { $cls = $msg['type'] === 'ok' ? 'ok' : 'err'; $body .= '
' . h($msg['text']) . '

'; } // Browse table if ($table !== '') { // naive identifier quoting for MySQL if (!preg_match('/^[A-Za-z0-9_]+$/', $table)) { $body .= '
Ungültiger Tabellenname.
'; } else { $body .= '

Tabelle: ' . h($table) . '

'; $stmt = $pdo->query('SELECT * FROM `' . $table . '` LIMIT ' . (int)$limit . ' OFFSET ' . (int)$offset); $rows = $stmt->fetchAll(); $body .= admin_render_table($rows); $body .= '
'; if ($page > 1) { $body .= '← Zurück'; } $body .= 'Weiter →'; $body .= '
'; } $body .= '
'; } // Query box $body .= '

SQL Query

'; $body .= '
' . '' . '' . '
' . '
'; $body .= $queryResultHtml; $body .= '
'; // card $body .= '
'; // grid admin_layout('DB-Verwaltung', $body); } catch (Throwable $e) { // Session invalid, force re-login admin_logout(); admin_layout('DB-Verwaltung', '

DB-Verwaltung

' . h($e->getMessage()) . '

Zum Login
'); } function admin_render_table(array $rows): string { if (empty($rows)) { return '
(keine Zeilen)
'; } $cols = array_keys((array)$rows[0]); $html = ''; foreach ($cols as $c) { $html .= ''; } $html .= ''; foreach ($rows as $r) { $html .= ''; foreach ($cols as $c) { $v = $r[$c] ?? null; if ($v === null) { $cell = 'NULL'; } else { $s = (string)$v; $cell = strlen($s) > 500 ? h(substr($s, 0, 500)) . '…' : h($s); } $html .= ''; } $html .= ''; } $html .= '
' . h((string)$c) . '
' . $cell . '
'; return $html; }