Files
nexus/partials/landingpages/modules/setup.php
Lars Gebhardt-Kusche e7a1878c72
All checks were successful
Deploy / deploy-staging (push) Successful in 5s
Deploy / deploy-production (push) Has been skipped
cron
2026-04-27 00:24:30 +02:00

586 lines
21 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
$moduleName = (string)($_GET['module'] ?? '');
$module = modules()->get($moduleName);
$error = null;
$notice = null;
$testGroup = null;
$dbTestMessages = [];
require_admin();
if (!$module) {
http_response_code(404);
echo '<div class="card">Modul nicht gefunden.</div>';
return;
}
$fields = (array)($module['setup']['fields'] ?? []);
$fieldTypes = [];
$fieldMeta = [];
foreach ($fields as $field) {
$fname = (string)($field['name'] ?? '');
if ($fname === '') {
continue;
}
$fieldTypes[$fname] = (string)($field['type'] ?? 'text');
$fieldMeta[$fname] = $field;
}
$current = modules()->settings($moduleName);
$intervalTaskStatuses = modules()->intervalTaskStatuses($moduleName);
$defaults = $module['db_defaults'] ?? [];
if (empty($current['db']) && is_array($defaults)) {
$current['db'] = $defaults;
}
$metadataDefaults = $module['metadata_db_defaults'] ?? [];
if (empty($current['metadata_db']) && is_array($metadataDefaults)) {
$current['metadata_db'] = $metadataDefaults;
}
$dbDefaultsByGroup = [
'db' => is_array($defaults) ? $defaults : [],
'metadata_db' => is_array($metadataDefaults) ? $metadataDefaults : [],
];
$setNested = function (array &$target, string $path, mixed $value): void {
$parts = explode('.', $path);
$last = array_pop($parts);
$node = &$target;
foreach ($parts as $part) {
if (!isset($node[$part]) || !is_array($node[$part])) {
$node[$part] = [];
}
$node = &$node[$part];
}
if ($last !== null && $last !== '') {
$node[$last] = $value;
}
};
$getNested = function (array $source, string $path): mixed {
$node = $source;
foreach (explode('.', $path) as $part) {
if (!is_array($node) || !array_key_exists($part, $node)) {
return null;
}
$node = $node[$part];
}
return $node;
};
$dbGroups = [];
$fieldsByDbGroup = [];
$generalFields = [];
foreach ($fields as $field) {
$name = (string)($field['name'] ?? '');
if (!str_contains($name, '.')) {
continue;
}
[$group, $key] = explode('.', $name, 2);
if ($key !== 'driver') {
continue;
}
$label = (string)($field['label'] ?? $group);
$label = trim(preg_replace('/\s+DB\s+Driver$/i', ' DB', $label) ?? $label);
$label = $label !== '' ? $label : $group;
$dbGroups[$group] = $label;
}
foreach ($fields as $field) {
$name = (string)($field['name'] ?? '');
if (str_contains($name, '.')) {
[$group] = explode('.', $name, 2);
if (array_key_exists($group, $dbGroups)) {
$fieldsByDbGroup[$group][] = $field;
continue;
}
}
$generalFields[] = $field;
}
$driverOptions = [
'pgsql' => 'PostgreSQL',
'mysql' => 'MySQL / MariaDB',
'sqlite' => 'SQLite',
];
$describeDbConfig = static function (array $dbConfig): string {
$driver = (string)($dbConfig['driver'] ?? '');
$host = (string)($dbConfig['host'] ?? '');
$port = (string)($dbConfig['port'] ?? '');
return 'Aktive Einstellung: Treiber ' . ($driver !== '' ? $driver : 'nicht gesetzt')
. ', Host ' . ($host !== '' ? $host : 'nicht gesetzt')
. ', Port ' . ($port !== '' ? $port : 'nicht gesetzt') . '.';
};
$describeDbDefaults = static function (array $dbDefaults): string {
$driver = (string)($dbDefaults['driver'] ?? '');
$host = (string)($dbDefaults['host'] ?? '');
$port = (string)($dbDefaults['port'] ?? '');
if ($driver === '' && $host === '' && $port === '') {
return '';
}
return 'Standard: Treiber ' . ($driver !== '' ? $driver : 'nicht gesetzt')
. ', Host ' . ($host !== '' ? $host : 'nicht gesetzt')
. ', Port ' . ($port !== '' ? $port : 'nicht gesetzt') . '.';
};
$dbConfigWarning = static function (array $dbConfig): ?string {
$driver = (string)($dbConfig['driver'] ?? '');
$port = (string)($dbConfig['port'] ?? '');
if ($driver === 'pgsql' && $port === '3306') {
return 'Port 3306 ist typisch fuer MySQL/MariaDB, aber als Treiber ist PostgreSQL ausgewaehlt. Bitte den Treiber auf MySQL / MariaDB stellen.';
}
if ($driver === 'mysql' && $port === '5432') {
return 'Port 5432 ist typisch fuer PostgreSQL, aber als Treiber ist MySQL / MariaDB ausgewaehlt. Bitte Port oder Treiber pruefen.';
}
return null;
};
$dbConfigHint = static function (string $group, array $dbConfig, array $dbDefaults): ?string {
if ($group !== 'metadata_db') {
return null;
}
$driver = (string)($dbConfig['driver'] ?? '');
$host = (string)($dbConfig['host'] ?? '');
$port = (string)($dbConfig['port'] ?? '');
$defaultDriver = (string)($dbDefaults['driver'] ?? '');
$defaultHost = (string)($dbDefaults['host'] ?? '');
$defaultPort = (string)($dbDefaults['port'] ?? '');
if ($driver !== $defaultDriver || $host !== $defaultHost || $port !== $defaultPort) {
return 'Diese Verbindung ist fuer Nexus-eigene DHCP-Zusatzinfos gedacht, nicht fuer die Nexus-App/Base-DB. Erwartet wird normalerweise: '
. ($defaultDriver !== '' ? $defaultDriver : 'Treiber offen')
. ' auf '
. ($defaultHost !== '' ? $defaultHost : 'Host offen')
. ':'
. ($defaultPort !== '' ? $defaultPort : 'Port offen')
. '.';
}
return null;
};
$fetchJsonWithApiKey = static function (string $url, string $apiKey, int $timeout = 10): array {
$headers = [
'Accept: application/json',
'x-api-key: ' . $apiKey,
];
$responseBody = null;
$httpCode = 0;
$curlError = '';
if (function_exists('curl_init')) {
$ch = curl_init($url);
if ($ch !== false) {
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_CONNECTTIMEOUT => min(5, $timeout),
CURLOPT_HTTPHEADER => $headers,
]);
$responseBody = curl_exec($ch);
$curlError = curl_error($ch);
$httpCode = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
}
}
if (!is_string($responseBody) || $responseBody === '') {
$context = stream_context_create([
'http' => [
'method' => 'GET',
'timeout' => $timeout,
'header' => implode("\r\n", $headers) . "\r\n",
],
]);
$responseBody = @file_get_contents($url, false, $context);
}
if (!is_string($responseBody) || $responseBody === '') {
return [
'ok' => false,
'message' => 'Abruf fehlgeschlagen.'
. ($curlError !== '' ? ' ' . $curlError : '')
. ($httpCode > 0 ? ' HTTP ' . $httpCode : ''),
];
}
$decoded = json_decode($responseBody, true);
if (!is_array($decoded)) {
return [
'ok' => false,
'message' => 'Antwort ist kein gueltiges JSON.',
];
}
foreach (['error', 'message', 'detail'] as $errorKey) {
if (isset($decoded[$errorKey]) && is_string($decoded[$errorKey]) && trim($decoded[$errorKey]) !== '') {
return [
'ok' => false,
'message' => trim((string) $decoded[$errorKey]),
];
}
}
return [
'ok' => true,
'data' => $decoded,
];
};
$normalizeDriver = static function (mixed $value): mixed {
if (!is_string($value)) {
return $value;
}
$normalized = strtolower(trim($value));
return match ($normalized) {
'postgres', 'postgresql' => 'pgsql',
'mariadb', 'mysql/mariadb', 'mysql / mariadb' => 'mysql',
default => $normalized,
};
};
$formatRunTimestamp = static function (?string $value): string {
$value = trim((string) $value);
if ($value === '') {
return '-';
}
$ts = strtotime($value);
if ($ts === false) {
return $value;
}
return date('Y-m-d H:i:s', $ts);
};
$renderField = function (array $field) use (&$current, $getNested, $driverOptions): void {
$name = (string)($field['name'] ?? '');
if ($name === '') {
return;
}
$label = (string)($field['label'] ?? $name);
$type = (string)($field['type'] ?? 'text');
$required = !empty($field['required']);
$help = (string)($field['help'] ?? $field['description'] ?? '');
$postKey = str_replace('.', '_', $name);
$value = '';
if ($name === 'kea_auto_init') {
$value = !empty($current[$name]) ? '1' : '0';
} elseif (str_contains($name, '.')) {
$value = (string)($getNested($current, $name) ?? '');
} else {
$value = (string)($current[$name] ?? '');
}
?>
<label class="setup-field muted">
<span><?= e($label) ?></span>
<?php if (str_ends_with($name, '.driver')): ?>
<select name="<?= e($postKey) ?>" <?= $required ? 'required' : '' ?>>
<option value="">Bitte waehlen</option>
<?php foreach ($driverOptions as $driver => $driverLabel): ?>
<option value="<?= e($driver) ?>" <?= $value === $driver ? 'selected' : '' ?>><?= e($driverLabel) ?></option>
<?php endforeach; ?>
</select>
<?php elseif ($type === 'textarea'): ?>
<textarea name="<?= e($postKey) ?>" rows="3" <?= $required ? 'required' : '' ?>><?= e($value) ?></textarea>
<?php elseif ($type === 'checkbox'): ?>
<input type="checkbox" name="<?= e($postKey) ?>" value="1" <?= $value === '1' ? 'checked' : '' ?>>
<?php else: ?>
<input type="<?= e($type) ?>" name="<?= e($postKey) ?>" value="<?= e($value) ?>" <?= $required ? 'required' : '' ?>>
<?php endif; ?>
<?php if ($help !== ''): ?>
<small class="muted"><?= e($help) ?></small>
<?php endif; ?>
</label>
<?php
};
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$payload = [];
foreach ($fields as $field) {
$name = (string)($field['name'] ?? '');
if ($name === '') {
continue;
}
$type = (string)($field['type'] ?? 'text');
$postKey = str_replace('.', '_', $name);
$value = $_POST[$postKey] ?? null;
if ($type === 'checkbox') {
$value = isset($_POST[$postKey]) ? '1' : '0';
}
if (is_array($value)) {
continue;
}
$value = is_string($value) ? trim($value) : $value;
if (str_ends_with($name, '.driver')) {
$value = $normalizeDriver($value);
}
if ($name === 'kea_auto_init') {
$payload[$name] = $value === '1';
continue;
}
if (str_contains($name, '.')) {
$setNested($payload, $name, $value);
continue;
}
$payload[$name] = $value;
}
$current = array_replace_recursive($current, $payload);
$postedTestGroup = (string)($_POST['test_db'] ?? '');
$postedResetGroup = (string)($_POST['reset_db'] ?? '');
if ($postedResetGroup !== '') {
$testGroup = $postedResetGroup;
if (!array_key_exists($postedResetGroup, $dbGroups)) {
$error = 'Unbekannte Datenbank-Konfiguration.';
} elseif (($dbDefaultsByGroup[$postedResetGroup] ?? []) === []) {
$dbTestMessages[$postedResetGroup] = [
'type' => 'error',
'text' => 'Fuer diese Datenbank sind keine Standardwerte hinterlegt.',
];
} else {
$current[$postedResetGroup] = $dbDefaultsByGroup[$postedResetGroup];
$dbTestMessages[$postedResetGroup] = [
'type' => 'success',
'text' => 'Standardwerte geladen. Bitte pruefen und speichern.',
];
}
} elseif ($postedTestGroup !== '') {
$testGroup = $postedTestGroup;
if (!array_key_exists($postedTestGroup, $dbGroups)) {
$error = 'Unbekannte Datenbank-Konfiguration.';
} else {
$dbConfig = $getNested($current, $postedTestGroup);
if (!is_array($dbConfig)) {
$dbTestMessages[$postedTestGroup] = [
'type' => 'error',
'text' => 'Datenbank-Konfiguration ist unvollstaendig.',
];
} else {
$warning = $dbConfigWarning($dbConfig);
if ($warning !== null) {
$dbTestMessages[$postedTestGroup] = [
'type' => 'error',
'text' => $warning . ' ' . $describeDbConfig($dbConfig),
];
} else {
try {
$dbConfig['options'] = array_replace([
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
], (array)($dbConfig['options'] ?? []));
$testPdo = \App\Database::createFromArray($dbConfig);
$testPdo->query('SELECT 1')->fetchColumn();
$dbTestMessages[$postedTestGroup] = [
'type' => 'success',
'text' => 'Verbindung erfolgreich. ' . $describeDbConfig($dbConfig),
];
} catch (\Throwable $e) {
$dbTestMessages[$postedTestGroup] = [
'type' => 'error',
'text' => 'Verbindung fehlgeschlagen. ' . $describeDbConfig($dbConfig) . ' ' . $e->getMessage(),
];
}
}
}
}
} else {
modules()->saveSettings($moduleName, $payload);
$notice = 'Setup gespeichert.';
$module = modules()->get($moduleName) ?: $module;
}
}
$moduleStatusPanel = null;
$activeDbGroup = $testGroup !== null && array_key_exists($testGroup, $dbGroups)
? $testGroup
: (array_key_first($dbGroups) ?? '');
?>
<div class="setup-shell">
<div class="pill">Setup</div>
<h1 class="setup-title"><?= e($module['title']) ?> Einrichtung</h1>
<p class="muted">Trage die benötigten Informationen für das Modul ein.</p>
<?php if ($error): ?>
<div class="bg-red-900 border-l-4 border-red-500 text-red-100 p-4 mb-6" role="alert">
<?= e($error) ?>
</div>
<?php elseif ($notice): ?>
<div class="setup-notice">
<?= e($notice) ?>
</div>
<?php endif; ?>
<form method="post" class="setup-form">
<?php if ($generalFields !== []): ?>
<section class="setup-panel">
<div class="setup-panel__head">
<div>
<span class="pill">Allgemein</span>
<h2>Moduleinstellungen</h2>
</div>
</div>
<div class="setup-grid">
<?php foreach ($generalFields as $field): ?>
<?php $renderField($field); ?>
<?php endforeach; ?>
</div>
</section>
<?php endif; ?>
<?php if ($intervalTaskStatuses !== []): ?>
<section class="setup-panel">
<div class="setup-panel__head">
<div>
<span class="pill">Automationen</span>
<h2>Intervall-Aufgaben</h2>
<p class="muted">Diese Aufgaben werden beim ersten gueltigen Modulaufruf nach Ablauf des Intervalls automatisch ausgefuehrt.</p>
</div>
</div>
<div class="setup-grid">
<?php foreach ($intervalTaskStatuses as $task): ?>
<?php $state = is_array($task['state'] ?? null) ? $task['state'] : []; ?>
<div class="setup-field muted">
<span><?= e((string) ($task['label'] ?? $task['name'] ?? 'Intervall-Aufgabe')) ?></span>
<input type="text" readonly value="<?= e(!empty($task['enabled']) ? 'Aktiv' : 'Deaktiviert') ?>">
<small class="muted">Intervall: <?= e(number_format((float) ($task['interval_hours'] ?? 0), 2, ',', '')) ?> Stunden</small>
<small class="muted">Letzter Start: <?= e($formatRunTimestamp((string) ($state['last_started_at'] ?? ''))) ?></small>
<small class="muted">Letzter Erfolg: <?= e($formatRunTimestamp((string) ($state['last_success_at'] ?? ''))) ?></small>
<small class="muted">Naechster Lauf: <?= e($formatRunTimestamp((string) ($task['next_due_at'] ?? ''))) ?></small>
<small class="muted">Status: <?= e((string) (($state['last_status'] ?? '') !== '' ? $state['last_status'] : '-')) ?></small>
<?php if (trim((string) ($state['last_message'] ?? '')) !== ''): ?>
<small class="muted">Meldung: <?= e((string) $state['last_message']) ?></small>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
</section>
<?php endif; ?>
<?php if (is_array($moduleStatusPanel)): ?>
<section class="setup-panel setup-panel--flat">
<div class="setup-panel__head">
<div>
<span class="pill">Status</span>
<h2><?= e((string) $moduleStatusPanel['title']) ?></h2>
<p class="muted">Der Status wird beim Aufruf der Setup-Seite automatisch geprueft.</p>
</div>
</div>
<div class="setup-db-message setup-db-message--<?= e((string) $moduleStatusPanel['type']) ?>">
<?= e((string) $moduleStatusPanel['text']) ?>
</div>
<?php if (!empty($moduleStatusPanel['stats']) && is_array($moduleStatusPanel['stats'])): ?>
<div class="setup-grid" style="margin-top:16px;">
<?php foreach ($moduleStatusPanel['stats'] as $stat): ?>
<label class="setup-field muted">
<span><?= e((string) ($stat['label'] ?? '')) ?></span>
<input type="text" value="<?= e((string) ($stat['value'] ?? '')) ?>" readonly>
</label>
<?php endforeach; ?>
</div>
<?php endif; ?>
</section>
<?php endif; ?>
<?php if ($dbGroups !== []): ?>
<section class="setup-panel">
<div class="setup-panel__head">
<div>
<span class="pill">Datenbanken</span>
<h2>Verbindungen</h2>
<p class="muted">Jede Verbindung kann getrennt konfiguriert und getestet werden.</p>
</div>
</div>
<div class="setup-tabs" aria-label="Datenbankbereiche">
<?php foreach ($dbGroups as $group => $label): ?>
<button
class="nav-link setup-tab<?= $group === $activeDbGroup ? ' is-active' : '' ?>"
type="button"
data-setup-tab-target="setup-db-<?= e($group) ?>"
aria-controls="setup-db-<?= e($group) ?>"
aria-selected="<?= $group === $activeDbGroup ? 'true' : 'false' ?>"
>
<?= e($label) ?>
</button>
<?php endforeach; ?>
</div>
<div class="setup-db-panels">
<?php foreach ($dbGroups as $group => $label): ?>
<?php
$panelConfig = $getNested($current, $group);
$panelConfig = is_array($panelConfig) ? $panelConfig : [];
$panelDefaults = $dbDefaultsByGroup[$group] ?? [];
$panelDefaults = is_array($panelDefaults) ? $panelDefaults : [];
$panelHint = $dbConfigHint($group, $panelConfig, $panelDefaults);
$defaultDescription = $describeDbDefaults($panelDefaults);
?>
<section class="setup-db-panel" id="setup-db-<?= e($group) ?>" <?= $group === $activeDbGroup ? '' : 'hidden' ?>>
<div class="setup-panel__head">
<div>
<span class="pill"><?= e($label) ?></span>
<h3><?= e($label) ?> konfigurieren</h3>
<?php if ($defaultDescription !== ''): ?>
<p class="muted"><?= e($defaultDescription) ?></p>
<?php endif; ?>
</div>
<div class="setup-db-actions">
<?php if ($panelDefaults !== []): ?>
<button class="nav-link" type="submit" name="reset_db" value="<?= e($group) ?>" formnovalidate>
Standardwerte laden
</button>
<?php endif; ?>
<button class="nav-link" type="submit" name="test_db" value="<?= e($group) ?>" formnovalidate>
Verbindung testen
</button>
</div>
</div>
<?php if (isset($dbTestMessages[$group])): ?>
<div class="setup-db-message setup-db-message--<?= e($dbTestMessages[$group]['type']) ?>">
<?= e($dbTestMessages[$group]['text']) ?>
</div>
<?php elseif ($panelHint !== null): ?>
<div class="setup-db-message setup-db-message--hint">
<?= e($panelHint) ?>
</div>
<?php endif; ?>
<div class="setup-grid">
<?php foreach (($fieldsByDbGroup[$group] ?? []) as $field): ?>
<?php $renderField($field); ?>
<?php endforeach; ?>
</div>
</section>
<?php endforeach; ?>
</div>
</section>
<?php endif; ?>
<div class="setup-actions">
<button class="cta-button" type="submit">Speichern</button>
<a class="nav-link" href="/modules/access/<?= e($moduleName) ?>">Zugriff verwalten</a>
<a class="nav-link" href="/modules">Zurück</a>
</div>
</form>
</div>