'closed', 'd8ec899c69283bc775952a767db9d5f5' => 'maybe_open', '2c2672c641425e5b2acd6ee74f39ae60' => 'open', '66aece8ae27ffd3a656d42005fa3efbd' => 'private', '86c75c0ad413b06ff8291673162d0b64' => 'unknown', '0' => 'error', ]; $STATE_MAP = [ 'closed' => (object) [ 'state_string' => 'Das Dorf ist gerade geschlossen.', 'svg' => 'lock', 'color' => 'red', ], 'maybe_open' => (object) [ 'state_string' => 'Das Dorf ist gerade vielleicht geöffnet:

Der Clubraum ist offen, aber es findet keine Veranstaltung statt.

Der Status kann sich also kurzfristig ändern.', 'svg' => 'done', 'color' => 'brown', ], 'open' => (object) [ 'state_string' => 'Das Dorf ist gerade geöffnet.

Komm gerne vorbei.', 'svg' => 'done', 'color' => 'green', ], 'private' => (object) [ 'state_string' => 'Das Dorf ist gerade privat:

Es sind Leute da, aber der Clubraum ist nicht geöffnet.

Komm gerne vorbei (aber frag lieber vorher, wie lange noch Leute da sind).', 'svg' => 'lock', 'color' => 'fdd835', ], 'unknown' => (object) [ 'state_string' => 'Der Status vom Dorf ist gerade unbekannt', 'svg' => 'warning', 'color' => 'orange', ], 'error' => (object) [ 'state_string' => 'Der Server konnte den Status vom Dorf nicht abrufen.', 'svg' => 'error', 'color' => 'blue', ], ]; function hasValidUa(): bool { if (isset($_SERVER['HTTP_USER_AGENT'])) { if (in_array(true, array_map(fn ($ua) => str_contains($_SERVER['HTTP_USER_AGENT'], $ua), INVALID_UAS))) { return false; } return true; } return false; } function dateTimeFromEvent(ICal $ical, object $event): DateTimeImmutable { return DateTimeImmutable::createFromMutable($ical->iCalDateToDateTime($event->dtstart_array[3])); } /** * @return array> */ function prepare_events(ICal $ical, array $events): array { $returns = []; foreach ($events as $event) { $start = dateTimeFromEvent($ical, $event); $end = $start->add(new DateInterval($event->duration)); $returns[] = [ 'summary' => $event->summary, 'url' => $event->url, 'start' => $start, 'end' => $end, ]; } return $returns; } $store = new Store(HTTP_CACHE); $client = HttpClient::create(); $client = new CachingHttpClient($client, $store, ['default_ttl' => 60, 'allow_revalidate' => true]); $state = 'error'; try { $response = $client->request('GET', ROOM_STATE_URL); $hash = md5($response->getContent()); } catch (\Exception $e) { $hash = 0; } $state = HASH_TO_STATE[$hash]; $ical = new ICal(false, [ 'defaultSpan' => 2, 'defaultTimeZone' => 'Europe/Berlin', 'defaultWeekStart' => 'MO', 'filterDaysBefore' => '1', ]); $ical->initUrl(ICAL_URL, $acceptLanguage = 'de'); $events = $ical->eventsFromInterval('2 week'); if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { $locale = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']); setlocale(LC_TIME, $locale); } $visitors = file_get_contents(VISITORS_FILE); if (is_string($visitors)) { $visitors = intval($visitors); } else { $visitors = 0; } $state_obj = $STATE_MAP[$state]; $loader = new \Twig\Loader\FilesystemLoader('templates'); $twig = new \Twig\Environment($loader, [ 'cache' => TMPL_CACHE, 'auto_reload' => true, // The 'raw' optimizer sometimes eats the only 'raw' that is used 'optimizations' => OptimizerNodeVisitor::OPTIMIZE_ALL ^ OptimizerNodeVisitor::OPTIMIZE_RAW_FILTER, ]); function formatEndDt(?DateTimeImmutable $end, ?DateTimeImmutable $start, string $timezone = 'Europe/Berlin'): string { if ($end == null || $start == null) return "ERROR"; $tz = new DateTimeZone($timezone); $daySame = $end->setTimeZone($tz)->format('d.m.Y') == $start->setTimeZone($tz)->format('d.m.Y'); $endIsMidnight = $end->setTimeZone($tz)->format('H:i') == '00:00' && $start->setTimeZone($tz)->format('H:i') != '00:00'; if ($daySame || $endIsMidnight) { return $end->format('H:i'); } else { return $end->format('d.m.Y H:i'); } } $twig->addFilter(new TwigFilter('end_datetime', 'formatEndDt')); //$twig->addExtension(new \Twig\Extra\Intl\IntlExtension()); $twig->getExtension(\Twig\Extension\CoreExtension::class)->setTimezone('Europe/Berlin'); $render_evts = prepare_events($ical, $events); echo ($twig->render("Main.twig", [ 'visitors' => $visitors, 'state_svg' => $state_obj->svg, 'state_color' => $state_obj->color, 'state_string' => $state_obj->state_string, 'events' => $render_evts, ])); /* Initialising values */ if (hasValidUa()) { $visitors++; file_put_contents(VISITORS_FILE, strval($visitors)); }