Upload via GUI (340 Dateien)

This commit is contained in:
2026-10-26 21:19:07 +00:00
parent 0df18c6747
commit ecabba060c
6 changed files with 545 additions and 19 deletions
+176 -15
View File
@@ -51,7 +51,10 @@ function status_component_monitor_target(PDO $pdo, array $component): ?array
if ($type === 'manual') return null;
if ($type === 'auto') {
if ($target !== '') return ['type' => 'http', 'target' => $target, 'label' => 'Webadresse'];
if ($target !== '') {
if (str_contains($name, 'minecraft')) return ['type' => 'minecraft', 'target' => $target, 'label' => 'Minecraft-Server'];
return ['type' => 'http', 'target' => $target, 'label' => 'Webadresse'];
}
if (str_contains($name, 'android') || (str_contains($category, 'app') && str_contains($name, 'mobil'))) return ['type' => 'android_app', 'target' => '', 'label' => 'Android-App-Verbindung'];
if (str_contains($name, 'windows') || str_contains($name, 'desktop') || str_contains($name, 'windows-programm')) return ['type' => 'windows_app', 'target' => '', 'label' => 'Windows-Programm-Verbindung'];
if (str_contains($name, 'datenbank') || str_contains($name, 'database')) return ['type' => 'database', 'target' => '', 'label' => 'Datenbank'];
@@ -70,6 +73,7 @@ function status_component_monitor_target(PDO $pdo, array $component): ?array
if ($target === '') $target = trim((string) get_setting('smtp_host', '')) . ':' . ((int) get_setting('smtp_port', '587') ?: 587);
return $target !== ':' ? ['type' => 'tcp', 'target' => $target, 'label' => 'E-Mail-Server'] : null;
}
if ($type === 'tcp' && $target !== '' && str_contains($name, 'minecraft')) return ['type' => 'minecraft', 'target' => $target, 'label' => 'Minecraft-Server'];
if (in_array($type, ['http', 'tcp'], true) && $target !== '') return ['type' => $type, 'target' => $target, 'label' => $type === 'http' ? 'Webadresse' : 'Serververbindung'];
return null;
}
@@ -117,9 +121,13 @@ function status_execute_component_check(PDO $pdo, array $check): array
if ($ok) fclose($socket); else $error = 'Push-Dienst nicht erreichbar';
} else $error = 'Push-Dienst nicht eingerichtet';
} else $error = 'Push-Dienst nicht eingerichtet';
} elseif ($check['type'] === 'tcp') {
if (preg_match('/^([a-zA-Z0-9.-]+):(\\d{1,5})$/', (string) $check['target'], $m) && (int) $m[2] > 0 && (int) $m[2] <= 65535) {
$socket = @stream_socket_client('tcp://' . $m[1] . ':' . $m[2], $errno, $errstr, 4, STREAM_CLIENT_CONNECT);
} elseif (in_array($check['type'], ['tcp', 'minecraft'], true)) {
$endpoint = $check['type'] === 'minecraft'
? status_resolve_minecraft_target((string) $check['target'])
: status_parse_host_port((string) $check['target']);
if ($endpoint !== null) {
$host = strpos($endpoint['host'], ':') !== false ? '[' . $endpoint['host'] . ']' : $endpoint['host'];
$socket = @stream_socket_client('tcp://' . $host . ':' . $endpoint['port'], $errno, $errstr, 4, STREAM_CLIENT_CONNECT);
$ok = is_resource($socket);
if ($ok) fclose($socket); else $error = 'Server oder Port nicht erreichbar';
} else $error = 'Ungültiges Prüfziel';
@@ -147,7 +155,7 @@ function status_execute_component_check(PDO $pdo, array $check): array
return ['ok' => $ok, 'error' => mb_substr($error, 0, 255), 'ms' => (int) round((microtime(true) - $started) * 1000)];
}
function status_run_component_checks(PDO $pdo): int
function status_run_component_checks(PDO $pdo, bool $force = false): int
{
$components = status_components($pdo);
$update = $pdo->prepare('UPDATE status_components SET last_checked_at = ?, last_check_ms = ?, last_check_error = ?, consecutive_failures = ? WHERE id = ?');
@@ -156,6 +164,9 @@ function status_run_component_checks(PDO $pdo): int
foreach ($components as $component) {
$check = status_component_monitor_target($pdo, $component);
if ($check === null) continue;
$intervalMinutes = max(1, (int) ($component['check_interval_minutes'] ?? 1));
$lastChecked = trim((string) ($component['last_checked_at'] ?? ''));
if (!$force && $lastChecked !== '' && strtotime($lastChecked) > time() - $intervalMinutes * 60) continue;
$result = status_execute_component_check($pdo, $check);
$failures = $result['ok'] ? 0 : (int) ($component['consecutive_failures'] ?? 0) + 1;
$update->execute([db_now(), $result['ms'], $result['ok'] ? null : $result['error'], $failures, (int) $component['id']]);
@@ -186,7 +197,7 @@ function status_discover_and_check(PDO $pdo): array
if (status_component_monitor_target($pdo, $component) !== null) $recognized[] = (string) $component['name'];
elseif (($component['monitor_type'] ?? 'auto') === 'auto') $pending[] = (string) $component['name'];
}
$checked = status_run_component_checks($pdo);
$checked = status_run_component_checks($pdo, true);
return ['recognized' => $recognized, 'pending' => $pending, 'checked' => $checked];
}
@@ -201,9 +212,10 @@ function status_incident_states(): array
];
}
function status_components(PDO $pdo): array
function status_components(PDO $pdo, bool $includeDisabled = false): array
{
return $pdo->query("SELECT c.*, COALESCE(cat.name, NULLIF(c.category, '')) AS category_name FROM status_components c LEFT JOIN status_categories cat ON cat.id = c.category_id ORDER BY COALESCE(cat.sort_order, 0), COALESCE(cat.name, c.category, ''), c.sort_order, c.name")->fetchAll();
$where = $includeDisabled ? '' : 'WHERE COALESCE(c.is_enabled, 1) = 1 ';
return $pdo->query("SELECT c.*, COALESCE(cat.name, NULLIF(c.category, '')) AS category_name FROM status_components c LEFT JOIN status_categories cat ON cat.id = c.category_id {$where}ORDER BY COALESCE(cat.sort_order, 0), COALESCE(cat.name, c.category, ''), c.sort_order, c.name")->fetchAll();
}
function status_categories(PDO $pdo): array
@@ -227,6 +239,69 @@ function status_monitor_types(): array
];
}
/** Parses hostname:port, IPv4:port, or [IPv6]:port for TCP service checks. */
function status_parse_host_port(string $target): ?array
{
$target = trim($target);
if (preg_match('/^\[([0-9a-fA-F:.]+)\]:(\d{1,5})$/D', $target, $m)) {
if (filter_var($m[1], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) return null;
$host = $m[1];
$port = (int) $m[2];
} elseif (preg_match('/^([A-Za-z0-9.-]+):(\d{1,5})$/D', $target, $m)) {
$host = $m[1];
$port = (int) $m[2];
if (strlen($host) > 253) return null;
foreach (explode('.', rtrim($host, '.')) as $label) {
if ($label === '' || strlen($label) > 63 || $label[0] === '-' || substr($label, -1) === '-') return null;
}
} else {
return null;
}
if ($port < 1 || $port > 65535) return null;
return ['host' => $host, 'port' => $port];
}
/** Minecraft accepts a bare host and resolves its Java port through DNS SRV. */
function status_parse_minecraft_target(string $target): ?array
{
$target = trim($target);
$explicit = status_parse_host_port($target);
if ($explicit !== null) return $explicit;
if (preg_match('/^\[([0-9a-fA-F:.]+)\]$/D', $target, $m)) {
return filter_var($m[1], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false
? ['host' => $m[1], 'port' => null]
: null;
}
if (filter_var($target, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) return ['host' => $target, 'port' => null];
if (strlen($target) > 253 || $target === '') return null;
foreach (explode('.', rtrim($target, '.')) as $label) {
if ($label === '' || strlen($label) > 63 || $label[0] === '-' || substr($label, -1) === '-' || !preg_match('/^[A-Za-z0-9-]+$/D', $label)) return null;
}
return ['host' => rtrim($target, '.'), 'port' => null];
}
function status_resolve_minecraft_target(string $target): ?array
{
$endpoint = status_parse_minecraft_target($target);
if ($endpoint === null || $endpoint['port'] !== null) return $endpoint;
$host = $endpoint['host'];
if (filter_var($host, FILTER_VALIDATE_IP) !== false) return ['host' => $host, 'port' => 25565];
if (function_exists('dns_get_record') && defined('DNS_SRV')) {
$records = @dns_get_record('_minecraft._tcp.' . $host, DNS_SRV);
if (is_array($records) && $records) {
usort($records, static fn(array $a, array $b): int => ((int) ($a['pri'] ?? 0)) <=> ((int) ($b['pri'] ?? 0)));
foreach ($records as $record) {
$srvHost = rtrim((string) ($record['target'] ?? ''), '.');
$port = (int) ($record['port'] ?? 0);
if ($srvHost !== '' && $port >= 1 && $port <= 65535 && status_parse_minecraft_target($srvHost) !== null) return ['host' => $srvHost, 'port' => $port];
}
}
}
return ['host' => $host, 'port' => 25565];
}
/** SMTP- und IMAP-Ziele aus den eingerichteten Konten der Installation. */
function status_configured_mail_endpoints(PDO $pdo): array
{
@@ -291,14 +366,23 @@ function status_component_detected_targets(PDO $pdo, array $check): array
return [(string) ($check['target'] ?? '')];
}
function status_set_component_monitor(PDO $pdo, int $id, string $type, string $target): bool
function status_set_component_monitor(PDO $pdo, int $id, string $type, string $target, int $intervalMinutes = 1): bool
{
if (!isset(status_monitor_types()[$type])) return false;
if (!in_array($intervalMinutes, [1, 2, 5, 10, 15, 30, 60], true)) return false;
$target = trim(mb_substr($target, 0, 500));
if ($type === 'http' && ($target === '' || !filter_var($target, FILTER_VALIDATE_URL) || !in_array(strtolower((string) parse_url($target, PHP_URL_SCHEME)), ['http', 'https'], true))) return false;
if ($type === 'tcp' && $target !== '' && !preg_match('/^[a-zA-Z0-9.-]+:\\d{1,5}$/', $target)) return false;
$stmt = $pdo->prepare('UPDATE status_components SET monitor_type = ?, monitor_target = ?, consecutive_failures = 0, last_check_error = NULL WHERE id = ?');
$stmt->execute([$type, $target !== '' ? $target : null, $id]);
if ($type === 'tcp' && $target !== '') {
$componentStmt = $pdo->prepare('SELECT name FROM status_components WHERE id = ?');
$componentStmt->execute([$id]);
$componentName = mb_strtolower((string) $componentStmt->fetchColumn());
$validTarget = str_contains($componentName, 'minecraft')
? status_parse_minecraft_target($target) !== null
: status_parse_host_port($target) !== null;
if (!$validTarget) return false;
}
$stmt = $pdo->prepare('UPDATE status_components SET monitor_type = ?, monitor_target = ?, check_interval_minutes = ?, consecutive_failures = 0, last_check_error = NULL WHERE id = ?');
$stmt->execute([$type, $target !== '' ? $target : null, $intervalMinutes, $id]);
return $stmt->rowCount() > 0 || (bool) $pdo->query('SELECT 1 FROM status_components WHERE id = ' . (int) $id)->fetchColumn();
}
@@ -335,17 +419,26 @@ function status_overall(PDO $pdo): array
if (($states[$c['status']][2] ?? 0) > $states[$worst][2]) $worst = $c['status'];
}
// Eine offene Störung ohne zugeordnete Komponente zählt mindestens als „eingeschränkt“
$open = (int) $pdo->query("SELECT COUNT(*) FROM status_incidents WHERE status <> 'resolved'")->fetchColumn();
$open = (int) $pdo->query("SELECT COUNT(*) FROM status_incidents i LEFT JOIN status_components c ON c.id = i.component_id WHERE i.status <> 'resolved' AND (i.component_id IS NULL OR COALESCE(c.is_enabled, 1) = 1)")->fetchColumn();
if ($open > 0 && $states[$worst][2] < $states['degraded'][2] && $worst !== 'maintenance') $worst = 'degraded';
$activeMaintenance = status_active_maintenance_map($pdo);
$hasActiveMaintenance = isset($activeMaintenance[0]);
if (!$hasActiveMaintenance) {
foreach (status_components($pdo) as $component) {
if (isset($activeMaintenance[(int) $component['id']])) { $hasActiveMaintenance = true; break; }
}
}
if ($hasActiveMaintenance && $states[$worst][2] < $states['maintenance'][2]) $worst = 'maintenance';
return ['key' => $worst, 'label' => $worst === 'operational' ? 'Alle Systeme laufen' : ($worst === 'maintenance' ? 'Wartungsarbeiten' : ($worst === 'outage' ? 'Ausfall' : 'Teilweise eingeschränkt')), 'color' => $states[$worst][1]];
}
/** Offene Störungen und die der letzten Tage (neueste zuerst), jeweils mit ihren Updates. */
function status_incidents(PDO $pdo, int $days = 14, bool $onlyOpen = false): array
function status_incidents(PDO $pdo, int $days = 14, bool $onlyOpen = false, bool $includeDisabledComponents = false): array
{
$since = date('Y-m-d H:i:s', time() - max(1, $days) * 86400);
$componentFilter = $includeDisabledComponents ? '' : ' AND (i.component_id IS NULL OR COALESCE(c.is_enabled, 1) = 1)';
$sql = "SELECT i.*, c.name AS component_name FROM status_incidents i LEFT JOIN status_components c ON c.id = i.component_id WHERE "
. ($onlyOpen ? "i.status <> 'resolved'" : "(i.status <> 'resolved' OR i.created_at >= ?)") . ' ORDER BY i.created_at DESC, i.id DESC LIMIT 50';
. ($onlyOpen ? "i.status <> 'resolved'" : "(i.status <> 'resolved' OR i.created_at >= ?)") . $componentFilter . ' ORDER BY i.created_at DESC, i.id DESC LIMIT 50';
$stmt = $pdo->prepare($sql);
$stmt->execute($onlyOpen ? [] : [$since]);
$rows = $stmt->fetchAll();
@@ -357,6 +450,59 @@ function status_incidents(PDO $pdo, int $days = 14, bool $onlyOpen = false): arr
return $rows;
}
/** Liefert geplante und aktuell laufende Wartungsfenster (oder die Admin-Historie). */
function status_maintenance_windows(PDO $pdo, bool $includeHistory = false): array
{
$now = date('Y-m-d H:i:s');
$since = date('Y-m-d H:i:s', time() - 60 * 86400);
$where = $includeHistory
? 'WHERE (w.ends_at >= ? OR w.cancelled_at >= ?)'
: 'WHERE w.cancelled_at IS NULL AND w.ends_at >= ? AND (w.component_id IS NULL OR COALESCE(c.is_enabled, 1) = 1)';
$stmt = $pdo->prepare("SELECT w.*, c.name AS component_name FROM status_maintenance_windows w LEFT JOIN status_components c ON c.id = w.component_id $where ORDER BY w.starts_at ASC, w.id ASC LIMIT 100");
$stmt->execute($includeHistory ? [$since, $since] : [$now]);
return $stmt->fetchAll();
}
function status_create_maintenance_window(PDO $pdo, string $title, string $body, string $startsAt, string $endsAt, ?int $componentId, ?int $staffId): array
{
$title = trim(mb_substr($title, 0, 200));
$body = trim(mb_substr($body, 0, 4000));
$start = DateTimeImmutable::createFromFormat('!Y-m-d\\TH:i', $startsAt);
$end = DateTimeImmutable::createFromFormat('!Y-m-d\\TH:i', $endsAt);
$startErrors = DateTimeImmutable::getLastErrors();
$endErrors = DateTimeImmutable::getLastErrors();
if ($title === '') return ['ok' => false, 'error' => 'Bitte einen Titel für die Wartung angeben.'];
if (!$start || !$end || ($startErrors && ($startErrors['warning_count'] || $startErrors['error_count'])) || ($endErrors && ($endErrors['warning_count'] || $endErrors['error_count']))) return ['ok' => false, 'error' => 'Bitte gültige Start- und Endzeiten angeben.'];
if ($end <= $start || $end <= new DateTimeImmutable()) return ['ok' => false, 'error' => 'Die Endzeit muss nach der Startzeit und in der Zukunft liegen.'];
if ($componentId !== null) {
$component = $pdo->prepare('SELECT COUNT(*) FROM status_components WHERE id = ? AND COALESCE(is_enabled, 1) = 1');
$component->execute([$componentId]);
if (!(int) $component->fetchColumn()) return ['ok' => false, 'error' => 'Der ausgewählte Dienst ist nicht verfügbar.'];
}
$now = db_now();
$pdo->prepare('INSERT INTO status_maintenance_windows (title, body, component_id, starts_at, ends_at, created_by, created_at) VALUES (?,?,?,?,?,?,?)')
->execute([$title, $body !== '' ? $body : null, $componentId, $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s'), $staffId, $now]);
return ['ok' => true, 'id' => (int) $pdo->lastInsertId()];
}
function status_cancel_maintenance_window(PDO $pdo, int $id): bool
{
$stmt = $pdo->prepare('UPDATE status_maintenance_windows SET cancelled_at = ? WHERE id = ? AND cancelled_at IS NULL');
$stmt->execute([db_now(), $id]);
return $stmt->rowCount() > 0;
}
/** Aktive Fenster nach Dienst-ID; Schlüssel 0 steht für eine globale Wartung. */
function status_active_maintenance_map(PDO $pdo): array
{
$now = date('Y-m-d H:i:s');
$stmt = $pdo->prepare('SELECT id, component_id FROM status_maintenance_windows WHERE cancelled_at IS NULL AND starts_at <= ? AND ends_at >= ?');
$stmt->execute([$now, $now]);
$active = [];
foreach ($stmt->fetchAll() as $window) $active[(int) ($window['component_id'] ?? 0)] = true;
return $active;
}
function status_set_component(PDO $pdo, int $id, string $status): bool
{
if (!isset(status_component_states()[$status])) return false;
@@ -381,6 +527,13 @@ function status_toggle_component_maintenance(PDO $pdo, int $id): bool
return status_set_component($pdo, $id, $current === 'maintenance' ? 'operational' : 'maintenance');
}
function status_set_component_enabled(PDO $pdo, int $id, bool $enabled): bool
{
$stmt = $pdo->prepare('UPDATE status_components SET is_enabled = ?, consecutive_failures = 0, last_check_error = NULL WHERE id = ?');
$stmt->execute([$enabled ? 1 : 0, $id]);
return $stmt->rowCount() > 0 || (bool) $pdo->query('SELECT 1 FROM status_components WHERE id = ' . (int) $id)->fetchColumn();
}
/** Letzte einzelne Prüfungen sowie Verfügbarkeit der letzten 24 Stunden. */
function status_component_timeline(PDO $pdo, int $componentId, int $checks = 30): array
{
@@ -463,6 +616,7 @@ function status_save_component(PDO $pdo, ?int $id, string $name, string $descrip
function status_delete_component(PDO $pdo, int $id): void
{
$pdo->prepare('UPDATE status_incidents SET component_id = NULL WHERE component_id = ?')->execute([$id]);
$pdo->prepare('UPDATE status_maintenance_windows SET cancelled_at = ? WHERE component_id = ? AND cancelled_at IS NULL')->execute([db_now(), $id]);
$pdo->prepare('DELETE FROM status_component_events WHERE component_id = ?')->execute([$id]);
$pdo->prepare('DELETE FROM status_components WHERE id = ?')->execute([$id]);
}
@@ -474,8 +628,10 @@ function status_restore_core_components(PDO $pdo): int
['DeskHub-Supportportal', 'Webseite', 'auto', ['DeskHub Supportportal']],
['DeskHub-API', 'API-Schnittstellen', 'auto', ['DeskHub API']],
['Datenbank', 'Server & Datenbank', 'database', ['Datenbank Server', 'Database']],
['Backup-Server', 'Server & Datenbank', 'tcp', ['Backup Server', 'FTP-Backup-Server']],
['Maildienst', 'E-Mail', 'mail', ['Mail Dienst', 'Mail Server']],
['Push-Dienst', 'Benachrichtigungen', 'fcm', ['Push Dienst']],
['Minecraft-Server', 'Apps', 'tcp', ['Minecraft Server']],
['Android-App', 'Apps', 'android_app', ['Windelgeschichten-Android-App', 'DeskHub-Mitarbeiter-App für Android']],
['Windows-Programm', 'Apps', 'windows_app', ['DeskHub-Mitarbeiter-App für Windows', 'Windows-App']],
];
@@ -512,6 +668,7 @@ function status_restore_core_components(PDO $pdo): int
$id = status_save_component($pdo, null, $name, '', $maxOrder + 10, $categoryId);
if ($id) {
status_set_component_monitor($pdo, $id, $monitorType, '');
if (in_array($name, ['Minecraft-Server', 'Backup-Server'], true)) status_set_component_enabled($pdo, $id, false);
$maxOrder += 10;
$restored++;
}
@@ -594,5 +751,9 @@ function status_summary(PDO $pdo): array
'createdAt' => (string) $i['created_at'], 'updatedAt' => (string) $i['updated_at'], 'resolvedAt' => $i['resolved_at'],
'updates' => array_map(fn($u) => ['status' => (string) $u['status'], 'statusLabel' => status_incident_states()[$u['status']] ?? $u['status'], 'body' => (string) ($u['body'] ?? ''), 'createdAt' => (string) $u['created_at']], $i['updates']),
], status_incidents($pdo, 14)),
'maintenanceWindows' => array_map(static fn(array $w): array => [
'id' => (int) $w['id'], 'title' => (string) $w['title'], 'body' => (string) ($w['body'] ?? ''),
'component' => (string) ($w['component_name'] ?? ''), 'startsAt' => (string) $w['starts_at'], 'endsAt' => (string) $w['ends_at'],
], status_maintenance_windows($pdo)),
];
}