alpinux-portail/web/inc/calendar.php
Cédrix 4f6094c6c6 Afficher les prochains événements du calendrier public sur la page d'accueil
- Ajoute web/inc/calendar.php : récupère le flux iCal via ?p=webcal,
  cache le résultat 1h dans /tmp, parse les VEVENT à venir
- Intègre le bloc "Prochains événements" dans index.php avant les services

Closes #1

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-05 23:45:05 +02:00

47 lines
1.4 KiB
PHP

<?php
function calendar_next_events(int $n = 5): array {
$url = 'https://alpinux.org/public-calendars/n5BWPYsxw7FCYozM?p=webcal';
$cache = sys_get_temp_dir() . '/alpinux_calendar.ics';
$ttl = 3600;
if (!file_exists($cache) || (time() - filemtime($cache)) >= $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);
}