diff --git a/web/inc/calendar.php b/web/inc/calendar.php new file mode 100644 index 0000000..05c1913 --- /dev/null +++ b/web/inc/calendar.php @@ -0,0 +1,47 @@ += $ttl) { + $data = @file_get_contents($url); + if ($data !== false) file_put_contents($cache, $data); + } + + if (!file_exists($cache)) return []; + + return _ical_upcoming(file_get_contents($cache), $n); +} + +function _ical_upcoming(string $ical, int $n): array { + $events = []; + $now = time(); + + preg_match_all('/BEGIN:VEVENT(.+?)END:VEVENT/s', $ical, $blocks); + + foreach ($blocks[1] as $b) { + $e = []; + + if (preg_match('/^SUMMARY[^:]*:(.+)$/m', $b, $m)) + $e['title'] = trim($m[1]); + + if (preg_match('/^LOCATION[^:]*:(.+)$/m', $b, $m)) + $e['location'] = trim($m[1]); + + if (preg_match('/^DTSTART[^:]*:(\d+)/m', $b, $m)) { + $r = $m[1]; + $e['start'] = strlen($r) === 8 + ? mktime(0, 0, 0, substr($r, 4, 2), substr($r, 6, 2), substr($r, 0, 4)) + : strtotime($r); + } + + if (isset($e['start'], $e['title']) && $e['start'] >= $now) + $events[] = $e; + } + + usort($events, fn($a, $b) => $a['start'] - $b['start']); + + return array_slice($events, 0, $n); +} diff --git a/web/index.php b/web/index.php index e063a59..89781d0 100644 --- a/web/index.php +++ b/web/index.php @@ -2,6 +2,7 @@ require_once __DIR__ . '/inc/config.php'; require_once __DIR__ . '/inc/auth.php'; require_once __DIR__ . '/inc/services.php'; +require_once __DIR__ . '/inc/calendar.php'; session_start_safe(); @@ -64,6 +65,20 @@ require __DIR__ . '/views/layout.php'; + + + Prochains événements + + + + = htmlspecialchars($ev['title']) ?> + = date('d/m/Y', $ev['start']) ?>= !empty($ev['location']) ? ' — ' . htmlspecialchars($ev['location']) : '' ?> + + + + + + Nos services
= date('d/m/Y', $ev['start']) ?>= !empty($ev['location']) ? ' — ' . htmlspecialchars($ev['location']) : '' ?>