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 .= "
";
// Step 2: Login
$body .= "
";
$body .= "
2) Login in Datenbank
";
$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 = '';
$body .= '';
$body .= '
'
. '
Tabellen
'
. '
Klick zum Anzeigen
';
if (empty($tables)) {
$body .= '
Keine Tabellen gefunden.
';
} else {
$body .= '
';
foreach ($tables as $row) {
$tname = (string)$row[0];
$active = ($tname === $table) ? ' style="font-weight:700"' : '';
$body .= '- ' . h($tname) . '
';
}
$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 .= '
';
}
$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
');
}
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 .= '| ' . h((string)$c) . ' | ';
}
$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 .= '| ' . $cell . ' | ';
}
$html .= '
';
}
$html .= '
';
return $html;
}