dorf.jetzt/index.php

193 lines
5.9 KiB
PHP
Raw Normal View History

2020-02-13 23:35:11 +01:00
<?php
require_once '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;
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',
];
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;
}
/**
* @param object $event
*/
function format_event($event): string
{
global $DATE_FORMAT, $ical;
$startdate_loop = $ical->iCalDateToDateTime($event->dtstart_array[3]);
$startdate_str = $startdate_loop->format($DATE_FORMAT);
$interval_loop = new DateInterval($event->duration);
if ($interval_loop->d != 0 || $interval_loop->h >= 24) {
$enddate_str = $startdate_loop->add($interval_loop)->format($DATE_FORMAT);
} else {
$enddate_str = $startdate_loop->add($interval_loop)->format('H:i');
}
return $startdate_str . ' &ndash; ' . $enddate_str;
}
/**
* @return array<int, array<string, DateTime|string|bool>>
*/
function prepare_events(array $events): array{
global $ical;
$returns = [];
foreach ($events as $event){
$start = DateTimeImmutable::createFromMutable($ical->iCalDateToDateTime($event->dtstart_array[3]));
$end = $start->add(new DateInterval($event->duration));
$returns[] = [
'summary' => $event->summary,
'url' => $event->url,
'start' => $start,
'end' => $end,
];
}
return $returns;
}
2022-12-17 00:10:35 +01:00
2023-01-20 18:54:40 +01:00
$store = new Store('/tmp/dorf.jetzt/http_cache');
2022-12-17 00:10:35 +01:00
$client = HttpClient::create();
2023-01-20 18:54:40 +01:00
$client = new CachingHttpClient($client, $store, ['default_ttl' => 60, 'allow_revalidate' => true]);
2022-12-17 00:10:35 +01:00
static $DATE_FORMAT = 'd.m.Y H:i';
2021-08-01 20:13:52 +02:00
static $VISITORS_FILE = '/opt/dorf.jetzt_visitors';
2022-12-17 00:10:35 +01:00
static $DORF_IN_LOCKDOWN = false;
2020-03-16 20:26:47 +01:00
static $DORF_VIRTUAL_EVENTS = true;
2020-02-21 22:29:50 +01:00
static $ICAL_URL = 'https://chaosdorf.de/~derf/cccd_all.ics';
2023-01-20 18:54:40 +01:00
$state_map = [
'closed' => (object) [
'state_string' => 'Das Dorf ist gerade <em>geschlossen</em>.',
2020-02-13 23:35:11 +01:00
'svg' => 'lock',
'color' => 'red',
2023-01-20 18:54:40 +01:00
],
'maybe_open' => (object) [
2020-02-21 21:56:04 +01:00
'state_string' => '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.',
2020-02-13 23:35:11 +01:00
'svg' => 'done',
'color' => 'brown',
2023-01-20 18:54:40 +01:00
],
'open' => (object) [
'state_string' => 'Das Dorf ist gerade <em>geöffnet</em>.</p><p>
Komm gerne vorbei.',
2020-02-13 23:35:11 +01:00
'svg' => 'done',
'color' => 'green',
2023-01-20 18:54:40 +01:00
],
'private' => (object) [
2020-02-21 21:56:04 +01:00
'state_string' => '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).',
2020-02-13 23:35:11 +01:00
'svg' => 'lock',
2020-04-17 23:49:25 +02:00
'color' => 'fdd835',
2023-01-20 18:54:40 +01:00
],
'unknown' => (object) [
2020-02-13 23:35:11 +01:00
'state_string' => 'Der Status vom Dorf ist gerade <em>unbekannt</em>',
'svg' => 'warning',
'color' => 'orange',
2023-01-20 18:54:40 +01:00
],
'error' => (object) [
2020-02-13 23:35:11 +01:00
'state_string' => 'Der Server konnte den Status vom Dorf nicht abrufen.',
'svg' => 'error',
'color' => 'blue',
2023-01-20 18:54:40 +01:00
],
];
$hash_to_state = [
2020-02-13 23:35:11 +01:00
'bff0167ed8aba031c49122ef4046cf1b' => 'closed',
'd8ec899c69283bc775952a767db9d5f5' => 'maybe_open',
'2c2672c641425e5b2acd6ee74f39ae60' => 'open',
'66aece8ae27ffd3a656d42005fa3efbd' => 'private',
'86c75c0ad413b06ff8291673162d0b64' => 'unknown',
'0' => 'error',
2023-01-20 18:54:40 +01:00
];
$state = 'error';
if ($DORF_VIRTUAL_EVENTS || !$DORF_IN_LOCKDOWN) {
2022-12-17 00:10:35 +01:00
try {
$response = $client->request('GET', 'https://chaosdorf.de/raumstatus/status.png');
2023-01-20 18:54:40 +01:00
$hash = md5($response->getContent());
} catch (\Exception $e) {
2022-12-17 00:10:35 +01:00
$hash = 0;
}
$state = $hash_to_state[$hash];
2023-01-20 18:54:40 +01:00
$ical = new ICal(false, [
'defaultSpan' => 2,
'defaultTimeZone' => 'Europe/Berlin',
'defaultWeekStart' => 'MO',
'filterDaysBefore' => '1',
]);
2022-12-17 00:10:35 +01:00
$ical->initUrl($ICAL_URL, $acceptLanguage = 'de');
$events = $ical->eventsFromInterval('2 week');
2023-01-20 18:54:40 +01:00
// $first_event = $events[0];
// $events = array_slice($events, 1);
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
2022-12-17 00:10:35 +01:00
$locale = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
setlocale(LC_TIME, $locale);
}
}
2023-01-20 18:54:40 +01:00
$visitors = file_get_contents($VISITORS_FILE);
if (is_string($visitors)) {
$visitors = intval($visitors);
} else {
$visitors = 0;
}
2023-01-20 18:54:40 +01:00
$state_obj = $state_map[$state];
$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader, [
//'cache' => '/tmp/dorf.jetzt/twig_cache'
]);
function formatEndDt(DateTime|DateTimeImmutable $end, DateTime|DateTimeImmutable $start, string $tz = 'Europe/Berlin'): string{
$daySame = $end->setTimeZone(new DateTimeZone($tz))->format('d.m.Y') == $start->setTimeZone(new DateTimeZone($tz))->format('d.m.Y');
$endIsMidnight = $end->setTimeZone(new DateTimeZone($tz))->format('H:i') == '00:00' && $start->setTimeZone(new DateTimeZone($tz))->format('H:i') != '00:00';
if ($daySame || $endIsMidnight){
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'));
//$twig->addExtension(new \Twig\Extra\Intl\IntlExtension());
$twig->getExtension(\Twig\Extension\CoreExtension::class)->setTimezone('Europe/Berlin');
$render_evts = prepare_events($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));
2020-12-27 19:08:21 +01:00
}