Files
fewo-stommer/app/Channels/ICal/ICalParser.php
T
Lars StommerandClaude Opus 5.5 0b7205e050 Portal-Synchronisation: unser System führt
- Connector-System (app/Channels, config/channels.php) mit iCal-Connector, robustem Parser und Export-Feed je Portal (ohne eigene Belegungen, ohne Gästedaten)
- Abgleich alle 5 Min. mit Protokoll, Echo-Erkennung, Konflikt-Mail bei möglicher Doppelbuchung
- Überwachung: Störungs-Mail, Status-Endpunkt /health/sync für n8n
- Portal-Aufgaben für Sofortbuchungsportale (FeWo-direkt): automatisch aus Buchungen/Sperren/anderen Portalen, Mail + Erinnerung, Abhaken im Admin
- Optionaler signierter Automatisierungs-Webhook mit Rückmeldung
- Herkunftsmessung: Abfrage im Formular + UTM-Parameter
- Portal-Analyse: AGB-Prüfung Automatisierung, Umsetzungsbeschreibung

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 11:13:55 +02:00

122 lines
3.6 KiB
PHP

<?php
namespace App\Channels\ICal;
use App\Channels\ChannelSyncException;
use App\Channels\ExternalPeriod;
use Carbon\CarbonImmutable;
/**
* Minimal RFC 5545 reader for availability feeds: VEVENTs with DTSTART/DTEND (date or date-time),
* UID and SUMMARY. Cancelled events and events without a positive length are skipped.
*/
class ICalParser
{
/**
* @return list<ExternalPeriod>
*
* @throws ChannelSyncException
*/
public function parse(string $ics): array
{
if (! str_contains($ics, 'BEGIN:VCALENDAR')) {
throw new ChannelSyncException('Die Antwort ist kein iCal-Kalender.');
}
$lines = explode("\n", preg_replace("/\r?\n[ \t]/", '', str_replace("\r\n", "\n", $ics)));
$periods = [];
$event = null;
foreach ($lines as $line) {
$line = rtrim($line, "\r");
if ($line === 'BEGIN:VEVENT') {
$event = [];
continue;
}
if ($line === 'END:VEVENT') {
if ($event !== null && ($period = $this->toPeriod($event))) {
$periods[] = $period;
}
$event = null;
continue;
}
if ($event !== null && str_contains($line, ':')) {
[$nameWithParams, $value] = explode(':', $line, 2);
$parts = explode(';', $nameWithParams);
$event[strtoupper($parts[0])] = ['value' => $value, 'params' => array_slice($parts, 1)];
}
}
return $periods;
}
/**
* @param array<string, array{value: string, params: list<string>}> $event
*/
private function toPeriod(array $event): ?ExternalPeriod
{
if (! isset($event['DTSTART']) || strtoupper($event['STATUS']['value'] ?? '') === 'CANCELLED') {
return null;
}
$start = $this->toDate($event['DTSTART']);
$end = isset($event['DTEND']) ? $this->toDate($event['DTEND']) : $start?->addDay();
if ($start === null || $end === null || $end->lessThanOrEqualTo($start)) {
return null;
}
$uid = $event['UID']['value'] ?? sha1($start->toDateString().'|'.$end->toDateString());
return new ExternalPeriod($uid, $start, $end, $this->unescape($event['SUMMARY']['value'] ?? null));
}
/**
* @param array{value: string, params: list<string>} $property
*/
private function toDate(array $property): ?CarbonImmutable
{
$value = trim($property['value']);
if (preg_match('/^\d{8}$/', $value)) {
return CarbonImmutable::createFromFormat('!Ymd', $value);
}
if (preg_match('/^(\d{8})T(\d{6})(Z?)$/', $value, $match)) {
$timezone = $match[3] === 'Z' ? 'UTC' : $this->timezone($property['params']);
return CarbonImmutable::createFromFormat('YmdHis', $match[1].$match[2], $timezone)
->setTimezone(config('app.timezone'))
->startOfDay();
}
return null;
}
/**
* @param list<string> $params
*/
private function timezone(array $params): string
{
foreach ($params as $param) {
if (str_starts_with(strtoupper($param), 'TZID=')) {
$tz = trim(substr($param, 5), '"');
return in_array($tz, timezone_identifiers_list(), true) ? $tz : config('app.timezone');
}
}
return config('app.timezone');
}
private function unescape(?string $text): ?string
{
return $text === null ? null : str_replace(['\\n', '\\N', '\\,', '\\;', '\\\\'], ["\n", "\n", ',', ';', '\\'], $text);
}
}