forked from fsi-tue/eei
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendar.php
More file actions
173 lines (149 loc) · 5.11 KB
/
calendar.php
File metadata and controls
173 lines (149 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
require_once 'config.php';
require_once 'utils.php';
require_once 'event_type.php';
require_once 'i18n/i18n.php';
class ICSGenerator
{
private const MIME_TYPE = 'text/calendar';
private const LINE_ENDING = "\r\n";
private array $events;
public function __construct(array $events)
{
$this->events = $events;
}
public function generateICS(): string
{
$eventBlocks = array();
foreach($this->events as $event) {
if ($event->cancelled) {
continue;
}
$eventBlocks[] = $this->generateEventBlock($event);
}
return implode(self::LINE_ENDING, [
'BEGIN:VCALENDAR',
'VERSION:2.0',
'PRODID:-//FSI//Calendar//EN',
'CALSCALE:GREGORIAN',
'METHOD:PUBLISH',
implode(self::LINE_ENDING, $eventBlocks),
'BEGIN:VTIMEZONE',
'TZID:Europe/Berlin',
'BEGIN:DAYLIGHT',
'TZOFFSETFROM:+0100',
'TZOFFSETTO:+0200',
'TZNAME:CEST',
'DTSTART:19700329T020000',
'RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU',
'END:DAYLIGHT',
'BEGIN:STANDARD',
'TZOFFSETFROM:+0200',
'TZOFFSETTO:+0100',
'TZNAME:CET',
'DTSTART:19701025T030000',
'RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU',
'END:STANDARD',
'END:VTIMEZONE',
'END:VCALENDAR'
]);
}
private function generateEventBlock($event): string
{
$eventData = [
'BEGIN:VEVENT',
'UID:' . $this->generateUID($event),
'DTSTAMP:' . $this->formatDateTime(new DateTimeImmutable()),
'DTSTART;TZID=Europe/Berlin:' . $this->formatDateTime((new DateTimeImmutable())->setTimestamp($event->getEventStartUTS())),
];
$eventStartUTS = $event->getEventStartUTS();
$eventEndUTS = $event->getEventEndUTS();
// Check if the event has an end
if ($eventEndUTS > 0) {
$eventData[] = 'DTEND;TZID=Europe/Berlin:' . $this->formatDateTime((new DateTimeImmutable())->setTimestamp($eventEndUTS));
} else {
// If there is no end, instead of using the 0 (which is kind of bad because this represents 1970-01-01)
// we set the end to the end of the day
$endOfDay = (new DateTimeImmutable())->setTimestamp($eventStartUTS)->setTime(23, 59, 00);
$eventData[] = 'DTEND;TZID=Europe/Berlin:' . $this->formatDateTime($endOfDay);
}
$eventData[] = 'SUMMARY:' . $this->escapeString($event->name);
if ( ! empty($event->location)) {
$eventData[] = 'LOCATION:' . $this->escapeString($event->location);
}
if ( ! empty($event->link)) {
$eventData[] = 'URL:' . $this->escapeString(getRemoteAddr() . '/event.php?e=' . $event->link);
}
$eventData[] = 'END:VEVENT';
// To debug the Calendar, uncomment the following lines
// echo '<pre>';
// var_dump($eventData);
// echo '</pre>';
return implode(self::LINE_ENDING, $eventData);
}
private function generateUID($event): string
{
return sprintf(
'%s-%s@%s',
uniqid('event-', true),
hash('crc32', $event->name), /* TODO: rework */
$_SERVER['HTTP_HOST'] ?? 'fsi.uni-tuebingen.de'
);
}
private function formatDateTime(DateTimeInterface $dateTime): string
{
return $dateTime->format('Ymd\THis');
}
private function escapeString(string $text): string
{
$text = str_replace(['\\', ',', ';'], ['\\\\', '\,', '\;'], $text);
return preg_replace('/\R/', '\n', $text);
}
public function sendICSFile(): void
{
if(count($this->events) > 1) {
$filename = "eei.ics";
} else {
$filename = $this->sanitizeFilename($this->events[0]->name) . '.ics';
}
// To debug the Calendar, comment all the "header" lines
header('Access-Control-Allow-Origin: *');
header('Content-Type: ' . self::MIME_TYPE);
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: 0');
echo $this->generateICS();
exit;
}
private function sanitizeFilename(string $filename): string
{
return preg_replace('/[^a-zA-Z0-9-_.]/', '_', $filename);
}
}
/* begin procedural code execution */
if (isset($_GET['e'], $_GET['ics'])) {
/* generate ICS for specific event */
$eventId = filter_input(INPUT_GET, 'e', FILTER_SANITIZE_ENCODED);
if ( ! isset($events[$eventId])) {
header('Location: /', true, 302);
exit;
}
downloadIcsFile($eventId);
exit;
} elseif(isset($_GET["allevents"])) {
/* generate ICS for all events (usable live feed) */
$generator = new ICSGenerator($events);
$generator->sendICSFile();
exit;
}
function downloadIcsFile(string $eventId): void
{
global $events;
$event = $events[$eventId];
if (!$event instanceof Event) {
return;
}
$generator = new ICSGenerator(array($event));
$generator->sendICSFile();
exit;
}