Loheide-eu-mitarbeiterplatt.../plugins-core/events/src/Calendar.php
Friederich Loheide f1202bea08
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m50s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m45s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m30s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m5s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Sicherheitsupdate nach Audit (0.29.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 11:34:54 +00:00

86 lines
2.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Inhaus\Plugins\Events;
use Illuminate\Support\Str;
use Inhaus\Plugins\Events\Models\Event;
/**
* iCalendar-Export (RFC 5545) für einzelne Termine – zum Übernehmen in Outlook & Co.
*/
class Calendar
{
public function toIcs(Event $event, string $organizer): string
{
$end = $event->ends_at ?? ($event->all_day ? $event->starts_at : $event->starts_at->copy()->addHour());
$lines = [
'BEGIN:VCALENDAR',
'VERSION:2.0',
'PRODID:-//'.$this->escape($organizer).'//Inhaus//DE',
'CALSCALE:GREGORIAN',
'METHOD:PUBLISH',
'BEGIN:VEVENT',
'UID:event-'.$event->id.'@'.(parse_url(config('app.url'), PHP_URL_HOST) ?: 'inhaus'),
'DTSTAMP:'.now()->utc()->format('Ymd\THis\Z'),
];
if ($event->all_day) {
$lines[] = 'DTSTART;VALUE=DATE:'.$event->starts_at->format('Ymd');
// DTEND ist bei ganztägigen Terminen exklusiv: der Tag nach dem letzten Tag.
$lines[] = 'DTEND;VALUE=DATE:'.$end->copy()->startOfDay()->addDay()->format('Ymd');
} else {
$lines[] = 'DTSTART:'.$event->starts_at->copy()->utc()->format('Ymd\THis\Z');
$lines[] = 'DTEND:'.$end->copy()->utc()->format('Ymd\THis\Z');
}
$lines[] = 'SUMMARY:'.$this->escape($event->title);
if ($event->location) {
$lines[] = 'LOCATION:'.$this->escape($event->location);
}
$description = trim(html_entity_decode(strip_tags(str_replace(['</p>', '<br>'], "\n", (string) $event->description_html))));
if ($description !== '') {
$lines[] = 'DESCRIPTION:'.$this->escape(Str::limit($description, 2000));
}
if ($event->online_url) {
$lines[] = 'URL:'.$this->escape($event->online_url);
}
$lines[] = 'END:VEVENT';
$lines[] = 'END:VCALENDAR';
return implode("\r\n", array_map($this->fold(...), $lines))."\r\n";
}
private function escape(string $value): string
{
return str_replace(['\\', ';', ',', "\r\n", "\n", "\r"], ['\\\\', '\;', '\\,', '\\n', '\\n', '\\n'], $value);
}
/**
* Zeilen nach 75 Oktetts umbrechen (Folgezeilen beginnen mit einem Leerzeichen,
* das mitzählt), ohne UTF-8-Zeichen zu zerschneiden.
*/
private function fold(string $line): string
{
$folded = '';
$current = '';
$limit = 75;
foreach (mb_str_split($line) as $char) {
if (strlen($current.$char) > $limit) {
$folded .= $current."\r\n ";
$current = '';
$limit = 74;
}
$current .= $char;
}
return $folded.$current;
}
}