dorf.jetzt/index.php

205 lines
5.5 KiB
PHP
Raw Permalink Normal View History

2020-02-13 23:35:11 +01:00
<?php
2023-01-23 12:41:45 +01:00
declare(strict_types=1);
2023-01-26 14:42:32 +01:00
require __DIR__ . '/vendor/autoload.php';
2023-01-20 18:54:40 +01:00
2020-02-13 23:35:11 +01:00
use ICal\ICal;
2022-12-17 00:10:35 +01:00
use Symfony\Component\HttpClient\CachingHttpClient;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpKernel\HttpCache\Store;
2023-01-20 18:54:40 +01:00
use Twig\TwigFilter;
2023-01-23 12:41:45 +01:00
use Twig\NodeVisitor\OptimizerNodeVisitor;
2023-01-26 13:52:00 +01:00
use DorfJetzt\State;
2023-01-23 12:41:45 +01:00
2023-01-26 13:52:00 +01:00
#region Configuration
2023-01-23 12:41:45 +01:00
const VISITORS_FILE = '/opt/dorf.jetzt_visitors';
2023-06-06 15:54:42 +02:00
const ICAL_URL = 'https://chaosdorf.de/~derf/cccd.ics';
2023-01-23 12:41:45 +01:00
const HTTP_CACHE = '/tmp/dorf.jetzt/http_cache';
const TMPL_CACHE = '/tmp/dorf.jetzt/twig_cache';
const ROOM_STATE_URL = 'https://chaosdorf.de/raumstatus/status.png';
2023-01-26 13:52:00 +01:00
const DEFAULT_TZ = 'Europe/Berlin';
#endregion
2023-01-20 18:54:40 +01:00
const INVALID_UAS = [
"AhrefsBot",
"Googlebot",
"Yahoo",
"Go-http-client/",
"bingbot",
"CheckMarkNetwork",
"SemrushBot",
"BingPreview",
"facebookexternalhit",
"hetrix.tools",
];
const HASH_TO_STATE = [
'bff0167ed8aba031c49122ef4046cf1b' => 'closed',
'd8ec899c69283bc775952a767db9d5f5' => 'maybe_open',
'2c2672c641425e5b2acd6ee74f39ae60' => 'open',
'66aece8ae27ffd3a656d42005fa3efbd' => 'private',
'86c75c0ad413b06ff8291673162d0b64' => 'unknown',
'0' => 'error',
];
2023-01-26 13:52:00 +01:00
function stateMap(string $state): State
{
return match ($state) {
'closed' => new State(
'Das Dorf ist gerade <em>geschlossen</em>.',
'lock',
'red',
),
'maybe_open' => new State(
'Das Dorf ist gerade <em>vielleicht geöffnet</em>: </p><p>Der Clubraum ist offen, aber es findet keine Veranstaltung statt.</p><p>
Der Status kann sich also kurzfristig ändern.',
2023-01-26 13:52:00 +01:00
'done',
'brown',
),
'open' => new State(
'Das Dorf ist gerade <em>geöffnet</em>.</p><p>
Komm gerne vorbei.',
2023-01-26 13:52:00 +01:00
'done',
'green',
),
'private' => new State(
'Das Dorf ist gerade <em>privat</em>: </p><p>Es sind Leute da, aber der Clubraum ist nicht geöffnet.</p><p>
Komm gerne vorbei (aber frag lieber vorher, wie lange noch Leute da sind).',
2023-01-26 13:52:00 +01:00
'lock',
'fdd835',
),
'unknown' => new State(
'Der Status vom Dorf ist gerade <em>unbekannt</em>',
2023-08-18 11:11:23 +02:00
'lock-question',
2023-01-26 13:52:00 +01:00
'orange',
),
'error' => new State(
'Der Server konnte den Status vom Dorf nicht abrufen.',
'error',
'blue',
),
};
}
2023-01-23 12:41:45 +01:00
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;
2022-12-17 00:10:35 +01:00
}
2023-01-23 12:41:45 +01:00
return false;
}
2023-01-26 13:52:00 +01:00
function dateTimeFromEvent(ICal $ical, object $event): DateTimeImmutable
{
2023-01-23 12:41:45 +01:00
return DateTimeImmutable::createFromMutable($ical->iCalDateToDateTime($event->dtstart_array[3]));
}
/**
2023-01-26 13:52:00 +01:00
* @return array{summary: string, url: string, start: DateTimeImmutable, end: DateTimeImmutable}[]
2023-01-23 12:41:45 +01:00
*/
2023-01-26 13:52:00 +01:00
function prepareEvents(ICal $ical, array $events): array
2023-01-23 12:41:45 +01:00
{
$returns = [];
2023-10-01 18:46:55 +02:00
foreach ($events as $index => $event) {
2023-01-23 12:41:45 +01:00
$start = dateTimeFromEvent($ical, $event);
$end = $start->add(new DateInterval($event->duration));
2023-01-26 13:52:00 +01:00
$url = strval($event->url);
$summary = strval($event->summary);
2023-01-23 12:41:45 +01:00
$returns[] = [
2023-01-26 13:52:00 +01:00
'summary' => $summary,
'url' => $url,
2023-01-23 12:41:45 +01:00
'start' => $start,
'end' => $end,
];
2022-12-17 00:10:35 +01:00
}
2023-01-23 12:41:45 +01:00
return $returns;
}
2023-01-26 13:52:00 +01:00
function getState(\Symfony\Contracts\HttpClient\HttpClientInterface $http): State
{
try {
$response = $http->request('GET', ROOM_STATE_URL);
$hash = md5($response->getContent());
if (!array_key_exists($hash, HASH_TO_STATE)){
error_log("Encountered unknown state hash $hash");
}
return stateMap(HASH_TO_STATE[$hash]);
} catch (\Exception $e) {
return stateMap('error');
}
}
2023-01-23 12:41:45 +01:00
$store = new Store(HTTP_CACHE);
$client = HttpClient::create();
$client = new CachingHttpClient($client, $store, ['default_ttl' => 60, 'allow_revalidate' => true]);
2023-01-26 13:52:00 +01:00
$state_obj = getState($client);
$ical = new ICal(options: [
2023-01-23 12:41:45 +01:00
'defaultSpan' => 2,
2023-01-26 13:52:00 +01:00
'defaultTimeZone' => DEFAULT_TZ,
2023-01-23 12:41:45 +01:00
'defaultWeekStart' => 'MO',
'filterDaysBefore' => '1',
]);
2023-06-06 15:56:18 +02:00
try {
$ical->initUrl(ICAL_URL, userAgent: 'dorf.jetzt', acceptLanguage: 'de');
$events = $ical->eventsFromInterval('2 week');
} catch (\Exception $e){
$events = [];
}
2023-01-23 12:41:45 +01:00
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);
2023-01-20 18:54:40 +01:00
if (is_string($visitors)) {
$visitors = intval($visitors);
} else {
$visitors = 0;
}
2023-01-26 13:52:00 +01:00
2023-01-20 18:54:40 +01:00
$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader, [
2023-01-23 12:41:45 +01:00
'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,
2023-01-20 18:54:40 +01:00
]);
2023-01-23 12:41:45 +01:00
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) {
2023-01-20 18:54:40 +01:00
return $end->format('H:i');
} else {
return $end->format('d.m.Y H:i');
2020-12-27 19:08:21 +01:00
}
}
2023-01-20 18:54:40 +01:00
$twig->addFilter(new TwigFilter('end_datetime', 'formatEndDt'));
2023-01-26 13:52:00 +01:00
$twig->getExtension(\Twig\Extension\CoreExtension::class)->setTimezone(DEFAULT_TZ);
2023-01-23 12:41:45 +01:00
echo ($twig->render("Main.twig", [
2023-01-20 18:54:40 +01:00
'visitors' => $visitors,
2023-01-26 13:52:00 +01:00
'state_svg' => $state_obj->svg_name,
2023-01-20 18:54:40 +01:00
'state_color' => $state_obj->color,
2023-01-26 13:52:00 +01:00
'state_string' => $state_obj->description,
'events' => prepareEvents($ical, $events),
2023-01-20 18:54:40 +01:00
]));
/* Initialising values */
if (hasValidUa()) {
$visitors++;
2023-01-23 12:41:45 +01:00
file_put_contents(VISITORS_FILE, strval($visitors));
2020-12-27 19:08:21 +01:00
}