setTimezone(new DateTimeZone('Europe/Berlin'))->format('d.m.Y H:i'); } catch (Throwable) { return '–'; } } function human_bytes(int|float|null $bytes): string { $bytes = (float) ($bytes ?? 0); if ($bytes < 1024) { return number_format($bytes, 0, ',', '.') . ' B'; } $units = ['KB', 'MB', 'GB']; $index = -1; do { $bytes /= 1024; $index++; } while ($bytes >= 1024 && $index < count($units) - 1); return number_format($bytes, 1, ',', '.') . ' ' . $units[$index]; } function truncate_text(string $value, int $length): string { if (mb_strlen($value, 'UTF-8') <= $length) { return $value; } return mb_substr($value, 0, max(0, $length - 1), 'UTF-8') . '…'; } function clean_filename(string $name): string { $name = basename(str_replace('\\', '/', $name)); $name = preg_replace('/[\x00-\x1F\x7F]/u', '', $name) ?? ''; $name = trim($name); if ($name === '' || $name === '.' || $name === '..') { return 'datei'; } return mb_substr($name, 0, 190, 'UTF-8'); } /** Dateiname für Content-Disposition: ASCII-Fallback plus UTF-8-Variante. */ function content_disposition(string $filename): string { $filename = clean_filename($filename); $ascii = preg_replace('/[^\x20-\x7E]/', '_', $filename) ?? 'datei'; $ascii = str_replace(['"', '\\', ';'], '_', $ascii); return 'attachment; filename="' . $ascii . '"; filename*=UTF-8\'\'' . rawurlencode($filename); } function flash(string $type, string $message): void { $_SESSION['flash'][] = ['type' => $type, 'message' => $message]; } function flash_secret(string $label, string $value): void { $_SESSION['flash'][] = ['type' => 'secret', 'label' => $label, 'value' => $value]; } function flash_pull(): array { $items = $_SESSION['flash'] ?? []; unset($_SESSION['flash']); return is_array($items) ? $items : []; } function csrf_token(): string { if (empty($_SESSION['csrf'])) { $_SESSION['csrf'] = bin2hex(random_bytes(32)); } return (string) $_SESSION['csrf']; } function csrf_field(): string { return ''; } function csrf_valid(): bool { $sent = $_POST['_csrf'] ?? ''; return is_string($sent) && $sent !== '' && hash_equals(csrf_token(), $sent); } function csrf_require(): void { if (!csrf_valid()) { abort_page(419, 'Die Sitzung ist abgelaufen. Bitte die Seite neu laden.'); } } function wants_json(): bool { $path = (string) parse_url((string) ($_SERVER['REQUEST_URI'] ?? ''), PHP_URL_PATH); return str_starts_with($path, '/api/') || str_contains(strtolower((string) ($_SERVER['HTTP_ACCEPT'] ?? '')), 'application/json'); } function json_response(mixed $data, int $status = 200): never { http_response_code($status); header('Content-Type: application/json; charset=utf-8'); echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); exit; } function json_input(): array { $raw = file_get_contents('php://input'); if (is_string($raw) && $raw !== '') { $decoded = json_decode($raw, true); if (is_array($decoded)) { return $decoded; } } return $_POST; } function render(string $view, array $data = []): string { $file = HUB_ROOT . '/views/' . $view . '.php'; if (!is_file($file)) { throw new RuntimeException('View fehlt: ' . $view); } extract($data, EXTR_SKIP); ob_start(); require $file; return (string) ob_get_clean(); } function render_page(string $view, array $data = [], string $title = 'Kür-Hub'): void { $content = render($view, $data); echo render('layout', [ 'title' => $title, 'content' => $content, 'user' => auth_user(), ]); } function abort_page(int $status, string $message): never { http_response_code($status); $content = render('error', ['status' => $status, 'message' => $message]); echo render('layout', [ 'title' => 'Fehler ' . $status, 'content' => $content, 'user' => auth_user(), ]); exit; } function abort_api(int $status, string $code, string $message): never { json_response(['error' => ['code' => $code, 'message' => $message]], $status); } function password_policy_error(string $password, string $username = ''): ?string { $length = mb_strlen($password, 'UTF-8'); if ($length < 10) { return 'Das Passwort muss mindestens 10 Zeichen lang sein.'; } if ($length > 200) { return 'Das Passwort darf höchstens 200 Zeichen lang sein.'; } if ($username !== '' && mb_strtolower($password) === mb_strtolower($username)) { return 'Das Passwort darf nicht dem Benutzernamen entsprechen.'; } return null; } function hub_log(string $message): void { error_log('Hub: ' . $message); }