$type, 'message' => $message]; } 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'] ?? ($_SERVER['HTTP_X_CSRF_TOKEN'] ?? ''); return is_string($sent) && $sent !== '' && hash_equals(csrf_token(), $sent); } function require_post(): void { if (!is_post()) { abort_page(405, 'Diese Aktion ist nur per POST möglich.'); } if (!csrf_valid()) { abort_page(419, 'Die Sitzung ist abgelaufen. Bitte die Seite neu laden und erneut versuchen.'); } } /** Nur lokale Pfade als Rücksprungziel zulassen. */ function safe_next(string $next, string $fallback = '/'): string { $next = trim($next); if ($next === '' || $next[0] !== '/' || str_starts_with($next, '//') || str_contains($next, '\\')) { return $fallback; } return $next; } function abort_page(int $code, string $message): never { http_response_code($code); $titles = [403 => 'Kein Zugriff', 404 => 'Nicht gefunden', 405 => 'Nicht erlaubt', 419 => 'Sitzung abgelaufen']; $title = $titles[$code] ?? 'Fehler'; header('Content-Type: text/html; charset=utf-8'); echo '' . '' . '' . e($title) . ' · Drink it cold' . '' . '
' . '' . '

Drink it cold · Terminplanung

' . '

' . e($title) . '

' . e($message) . '

' . '

Zur Übersicht

' . '

Impressum' . ' · Datenschutz

' . '
'; exit; } /* ---------------------------------------------------------------- Datum */ function today_iso(): string { return date('Y-m-d'); } function valid_date(?string $iso): bool { if ($iso === null || !preg_match('/^\d{4}-\d{2}-\d{2}$/', $iso)) { return false; } [$y, $m, $d] = array_map('intval', explode('-', $iso)); return checkdate($m, $d, $y); } function date_obj(string $iso): DateTimeImmutable { return new DateTimeImmutable($iso . ' 12:00:00', new DateTimeZone('Europe/Berlin')); } /** Uhrzeit als "HH:MM" normalisieren; leer bleibt leer (optional). */ function normalize_time(mixed $value): string { $text = trim((string) $value); if ($text === '') { return ''; } if (preg_match('/^(\d{1,2}):(\d{2})(?::\d{2})?$/', $text, $match) === 1) { return sprintf('%02d:%02d', (int) $match[1], (int) $match[2]); } return $text; } /** Leere Uhrzeit wird in der Datenbank als NULL gespeichert. */ function null_if_empty(string $value): ?string { return $value === '' ? null : $value; } /** Leere Uhrzeit ist erlaubt – sie ist ein optionales Zusatzfeld. */ function valid_time(?string $time): bool { if ($time === null || $time === '') { return true; } return preg_match('/^([01]\d|2[0-3]):[0-5]\d$/', $time) === 1; } function fmt_time(?string $time): string { $value = normalize_time($time); return $value !== '' && valid_time($value) ? $value : ''; } /** Datum mit optionaler Uhrzeit, z. B. "30.11.2026 14:00". */ function fmt_date_with_time(string $iso, ?string $time): string { $clock = fmt_time($time); return $clock === '' ? fmt_date($iso) : fmt_date($iso) . ' ' . $clock; } /** Kurzform für Listen/Kopiertext: "30.11.26 14:00". */ function fmt_date_short_with_time(string $iso, ?string $time): string { $clock = fmt_time($time); return $clock === '' ? fmt_date_short($iso) : fmt_date_short($iso) . ' ' . $clock; } function add_days(string $iso, int $days): string { return date_obj($iso)->modify(($days >= 0 ? '+' : '') . $days . ' days')->format('Y-m-d'); } function days_between(string $from, string $to): int { return (int) date_obj($from)->diff(date_obj($to))->format('%r%a'); } function week_start(string $iso): string { $date = date_obj($iso); $weekday = (int) $date->format('N'); return $date->modify('-' . ($weekday - 1) . ' days')->format('Y-m-d'); } function month_start(string $iso): string { return date_obj($iso)->modify('first day of this month')->format('Y-m-d'); } function month_end(string $iso): string { return date_obj($iso)->modify('last day of this month')->format('Y-m-d'); } function fmt_date(string $iso): string { return valid_date($iso) ? date_obj($iso)->format('d.m.Y') : '–'; } function fmt_date_short(string $iso): string { return valid_date($iso) ? date_obj($iso)->format('d.m.y') : '–'; } function fmt_date_long(string $iso): string { if (!valid_date($iso)) { return '–'; } $days = ['', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag']; $months = ['', 'Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember']; $date = date_obj($iso); return $days[(int) $date->format('N')] . ', ' . (int) $date->format('j') . '. ' . $months[(int) $date->format('n')] . ' ' . $date->format('Y'); } function fmt_month(string $iso): string { $months = ['', 'Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember']; return $months[(int) date_obj($iso)->format('n')] . ' ' . date_obj($iso)->format('Y'); } function fmt_month_short(string $iso): string { $months = ['', 'Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez']; return $months[(int) date_obj($iso)->format('n')]; } function fmt_weekday_short(string $iso): string { $days = ['', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So']; return $days[(int) date_obj($iso)->format('N')]; } function fmt_date_range(string $from, string $to): string { if ($from === $to) { return fmt_date($from); } if (date_obj($from)->format('Y-m') === date_obj($to)->format('Y-m')) { return (int) date_obj($from)->format('j') . '.–' . fmt_date($to); } return fmt_date($from) . ' – ' . fmt_date($to); } function fmt_datetime(?string $utc): string { if ($utc === null || $utc === '') { return '–'; } try { $date = new DateTimeImmutable($utc, new DateTimeZone('UTC')); return $date->setTimezone(new DateTimeZone('Europe/Berlin'))->format('d.m.Y H:i'); } catch (Throwable) { return '–'; } } function relative_day(string $iso): string { $diff = days_between(today_iso(), $iso); return match (true) { $diff === 0 => 'Heute', $diff === 1 => 'Morgen', $diff === -1 => 'Gestern', $diff < 0 => abs($diff) . ' Tage überfällig', default => 'in ' . $diff . ' Tagen', }; } function greeting(): string { $hour = (int) date('G'); if ($hour < 11) { return 'Guten Morgen'; } if ($hour < 18) { return 'Guten Tag'; } return 'Guten Abend'; } /* ---------------------------------------------------------------- Geld */ function fmt_money(int $cents): string { return number_format($cents / 100, 2, ',', '.') . ' €'; } function parse_money(string $value): int { $value = trim($value); $value = str_replace(["\u{00A0}", ' ', '€', 'EUR'], '', $value); $value = preg_replace('/[^0-9,.\-]/u', '', $value) ?? ''; if ($value === '' || $value === '-') { return 0; } if (str_contains($value, ',')) { $value = str_replace('.', '', $value); $value = str_replace(',', '.', $value); } elseif (preg_match('/^\d{1,3}(\.\d{3})+$/', $value) === 1) { $value = str_replace('.', '', $value); } return (int) round((float) $value * 100); } function due_cents(int $total, int $deposit): int { return $total - $deposit; } /** Lesbare Textfarbe für eine Anhängerfarbe. */ function contrast_color(string $hex): string { $hex = ltrim($hex, '#'); if (strlen($hex) !== 6) { return '#ffffff'; } $r = hexdec(substr($hex, 0, 2)) / 255; $g = hexdec(substr($hex, 2, 2)) / 255; $b = hexdec(substr($hex, 4, 2)) / 255; $channel = static function (float $c): float { return $c <= 0.03928 ? $c / 12.92 : (($c + 0.055) / 1.055) ** 2.4; }; $luminance = 0.2126 * $channel($r) + 0.7152 * $channel($g) + 0.0722 * $channel($b); return $luminance > 0.46 ? '#10202c' : '#ffffff'; } function initials(string $name): string { $parts = preg_split('/\s+/', trim($name)) ?: []; $out = ''; foreach (array_slice($parts, 0, 2) as $part) { if ($part !== '') { $out .= mb_strtoupper(mb_substr($part, 0, 1)); } } return $out !== '' ? $out : '?'; } function truncate(string $value, int $length): string { if (mb_strlen($value) <= $length) { return $value; } return mb_substr($value, 0, max(0, $length - 1)) . '…'; }