-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcalendar.php
142 lines (115 loc) · 4 KB
/
calendar.php
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
<?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 Event $event;
public function __construct(Event $event)
{
$this->event = $event;
}
public function generateICS(): string
{
return implode(self::LINE_ENDING, [
'BEGIN:VCALENDAR',
'VERSION:2.0',
'PRODID:-//FSI//Calendar//EN',
'CALSCALE:GREGORIAN',
'METHOD:PUBLISH',
$this->generateEventBlock(),
'END:VCALENDAR'
]);
}
private function generateEventBlock(): string
{
$eventData = [
'BEGIN:VEVENT',
'UID:' . $this->generateUID(),
'DTSTAMP:' . $this->formatDateTime(new DateTimeImmutable()),
'DTSTART:' . $this->formatDateTime((new DateTimeImmutable())->setTimestamp($this->event->getEventStartUTS())),
];
$eventStartUTS = $this->event->getEventStartUTS();
$eventEndUTS = $this->event->getEventEndUTS();
// Check if the event has an end
if ($eventEndUTS > 0) {
$eventData[] = 'DTEND:' . $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, 59);
$eventData[] = 'DTEND:' . $this->formatDateTime($endOfDay);
}
$eventData[] = 'SUMMARY:' . $this->escapeString($this->event->name);
if (!empty($this->event->description)) {
$eventData[] = 'DESCRIPTION:' . $this->escapeString($this->event->text);
}
if (!empty($this->event->location)) {
$eventData[] = 'LOCATION:' . $this->escapeString($this->event->location);
}
if (!empty($this->event->link)) {
$eventData[] = 'URL:' . $this->escapeString(getRemoteAddr() . '/event.php?e=' . $this->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(): string
{
return sprintf(
'%s-%s@%s',
uniqid('event-', true),
hash('crc32', $this->event->name),
$_SERVER['HTTP_HOST'] ?? 'fsi.uni-tuebingen.de'
);
}
private function formatDateTime(DateTimeInterface $dateTime): string
{
return $dateTime->format('Ymd\THis\Z');
}
private function escapeString(string $text): string
{
$text = str_replace(['\\', ',', ';'], ['\\\\', '\,', '\;'], $text);
return preg_replace('/\R/', '\n', $text);
}
public function sendICSFile(): void
{
$filename = $this->sanitizeFilename($this->event->name) . '.ics';
// To debug the Calendar, comment all the "header" lines
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);
}
}
if (isset($_GET['e'], $_GET['ics'])) {
$eventId = filter_input(INPUT_GET, 'e', FILTER_SANITIZE_ENCODED);
if (!isset($events[$eventId])) {
header('Location: /', true, 302);
exit;
}
downloadIcsFile($eventId);
}
function downloadIcsFile(string $eventId): void
{
global $events;
$event = $events[$eventId];
if (!$event instanceof Event) {
return;
}
$generator = new ICSGenerator($event);
$generator->sendICSFile();
exit;
}