alpinux-portail/web/inc/calendar.php
Cédrix 8ce9c3ad3b Passer par le proxy public-calendars plutôt que Nextcloud directement
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-05 23:49:20 +02:00

52 lines
1.7 KiB
PHP

<?php
function calendar_next_events(int $n = 5): array {
$url = 'https://alpinux.org/public-calendars/n5BWPYsxw7FCYozM';
$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]);
// Handles: DTSTART:YYYYMMDD, DTSTART:YYYYMMDDTHHmmss[Z], DTSTART;TZID=...:YYYYMMDDTHHmmss
if (preg_match('/^DTSTART(?:;[^:]+)?:([\dTZ]+)/m', $b, $m)) {
$raw = $m[1];
if (strlen($raw) === 8) {
$e['start'] = mktime(0, 0, 0, (int)substr($raw, 4, 2), (int)substr($raw, 6, 2), (int)substr($raw, 0, 4));
} else {
$dt = DateTime::createFromFormat('Ymd\THis\Z', $raw, new DateTimeZone('UTC'))
?: DateTime::createFromFormat('Ymd\THis', $raw, new DateTimeZone('Europe/Paris'));
if ($dt) $e['start'] = $dt->getTimestamp();
}
}
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);
}