generated from kaltura/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd2calendar.php
202 lines (197 loc) · 8.11 KB
/
add2calendar.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
// ----------------------------------------------------------------
// Rewrite of https://github.com/spatie/calendar-links to a single page script
// ----------------------------------------------------------------
// Meeting calendar event data -
require_once('./utils.php');
$title = filter_var($_GET["title"], FILTER_SANITIZE_STRING);
$title = urldecode($title);
$description = filter_var($_GET["desc"], FILTER_SANITIZE_STRING);
$description = urldecode($description);
$address = filter_var($_GET["address"], FILTER_SANITIZE_STRING);
$address = urldecode($address);
$allDay = filter_var($_GET["allday"], FILTER_SANITIZE_STRING); //yes or no (do not use boolean)
$startDateInput = filter_var($_GET["from"], FILTER_SANITIZE_NUMBER_INT); // see: $inputDateFormat
$endDateInput = false;
// see: $inputDateFormat --> MUST be a time AFTER $startDate
if (isset($_GET["to"])) {
$endDateInput = filter_var($_GET["to"], FILTER_SANITIZE_NUMBER_INT);
}
$duration = false;
// DateInterval format, e.g. PT1H
if (isset($_GET["duration"])) {
$duration = filter_var($_GET["duration"], FILTER_SANITIZE_STRING);
}
if (
$title === false || $description === false || $address === false || $startDateInput === false ||
$allDay === false || ($endDateInput === false && $duration === false)
) {
redirect_to_page('index.html');
}
$inputDateFormat = 'U'; // input should be UNIX timestamp formatted. e.g. 1612612219
$durationInterval = null;
try {
$startDate = DateTime::createFromFormat($inputDateFormat, $startDateInput);
} catch (Exception $e) {
echo 'from input date (' . $startDateInput . ') wrong format, please use a unix-timestamp, e.g. 1612612219' . PHP_EOL;
exit(1);
}
if ($endDateInput) {
try {
$endDate = DateTime::createFromFormat($inputDateFormat, $endDateInput);
} catch (Exception $e) {
echo 'to input date (' . $endDateInput . ') wrong format, please use a unix-timestamp, e.g. 1612612219' . PHP_EOL;
exit(1);
}
} else {
if ($duration) {
try {
$durationInterval = new DateInterval($duration);
} catch (Exception $e) {
echo 'duration input (' . $duration . ') wrong format, please use PHP DateInterval format, e.g. PT1H for +1 hour' . PHP_EOL;
exit(1);
}
$endDate = (clone $startDate)->add($durationInterval);
}
}
if ($endDate <= $startDate) {
echo 'the end date/time must be after start date/time' . PHP_EOL;
exit(1);
}
if ($allDay != 'yes' && $allDay != 'no') {
echo 'allday input (' . $allDay . ') wrong format, please use either "yes" or "no" as value' . PHP_EOL;
exit(1);
}
$allDay = $allDay == 'yes' ? true : false;
// ----------------------------------------------------------------
$dateFormat = 'Ymd';
$dateTimeFormat = 'Ymd\THis\Z';
$outlookDateFormat = 'Y-m-d';
$outlookDateTimeFormat = 'Y-m-d\TH:i:s\Z';
$IcsDateTimeFormat = 'e:Ymd\THis';
$dateTimeFormat = $allDay ? $dateFormat : $dateTimeFormat;
// ----------------------------------------------------------------
// Google Calendar
$googleCalUrl = 'https://calendar.google.com/calendar/render?action=TEMPLATE';
$googleCalUrl .= '&dates=' . $startDate->format($dateTimeFormat) . '/' . $endDate->format($dateTimeFormat);
$googleCalUrl .= '&text=' . rawurlencode($title);
if ($description) {
$googleCalUrl .= '&details=' . rawurlencode($description);
}
if ($address) {
$googleCalUrl .= '&location=' . rawurlencode($address);
}
// ----------------------------------------------------------------
// Outlook365 Calendar
$outlook365Url = 'https://outlook.live.com/calendar/deeplink/compose?path=/calendar/action/compose'; //&rru=addevent causes + signs
$outlook365Url .= '&startdt=' . $startDate->format($outlookDateTimeFormat);
$outlook365Url .= '&enddt=' . $endDate->format($outlookDateTimeFormat);
if ($allDay) {
$outlook365Url .= '&allday=true';
}
$outlook365Url .= '&subject=' . rawurlencode($title);
if ($description) {
$outlook365Url .= '&body=' . rawurlencode($description);
}
if ($address) {
$outlook365Url .= '&location=' . rawurlencode($address);
}
// ----------------------------------------------------------------
// Yahoo! Calendar
$yahooUrl = 'https://calendar.yahoo.com/?v=60&view=d&type=20';
if ($allDay && $startDate->diff($endDate)->days === 1) {
$yahooUrl .= '&st=' . $startDate->format($dateTimeFormat);
$yahooUrl .= '&dur=allday';
} else {
$yahooUrl .= '&st=' . $startDate->format($dateTimeFormat);
/**
* Yahoo has a bug on parsing end date parameter: it ignores timezone, assuming
* that it's specified in user's tz. In order to bypass it, we can use duration ("dur")
* parameter instead of "et", but this parameter has a limitation cause by it's format HHmm:
* the max duration is 99hours and 59 minutes (dur=9959).
*/
$maxDurationInSecs = (59 * 60 * 60) + (59 * 60);
$canUseDuration = $maxDurationInSecs > ($endDate->getTimestamp() - $startDate->getTimestamp());
if ($canUseDuration) {
$dateDiff = $startDate->diff($endDate);
$yahooUrl .= '&dur=' . $dateDiff->format('%H%I');
} else {
$yahooUrl .= '&et=' . $endDate->format($dateTimeFormat);
}
}
$yahooUrl .= '&title=' . rawurlencode($title);
if ($description) {
$yahooUrl .= '&desc=' . rawurlencode($description);
}
if ($address) {
$yahooUrl .= '&in_loc=' . rawurlencode($address);
}
// ----------------------------------------------------------------
// ICS Download
// See: https://tools.ietf.org/html/rfc5545#section-3.8.4.7
$eventUuid = md5(sprintf(
'%s%s%s%s',
$startDate->format(\DateTimeInterface::ATOM),
$endDate->format(\DateTimeInterface::ATOM),
$title,
$address
));
// See: https://tools.ietf.org/html/rfc5545.html#section-3.3.11
$ecapedTitle = addcslashes($title, "\r\n,;");
$IcsUrlParts = array(
'BEGIN:VCALENDAR',
'VERSION:2.0',
'BEGIN:VEVENT',
'UID:' . $eventUuid,
'SUMMARY:' . $ecapedTitle,
);
$dateTimeFormat2Use = $allDay ? $dateFormat : $IcsDateTimeFormat;
if ($allDay) {
$IcsUrlParts[] = 'DTSTART:' . $startDate->format($dateTimeFormat2Use);
$IcsUrlParts[] = 'DURATION:P1D';
} else {
$IcsUrlParts[] = 'DTSTART;TZID=' . $startDate->format($dateTimeFormat2Use);
$IcsUrlParts[] = 'DTEND;TZID=' . $endDate->format($dateTimeFormat2Use);
}
if ($description) {
$IcsUrlParts[] = 'DESCRIPTION:' . addcslashes($description, "\r\n,;");
}
if ($address) {
$IcsUrlParts[] = 'LOCATION:' . addcslashes($address, "\r\n,;");
}
$IcsUrlParts[] = 'END:VEVENT';
$IcsUrlParts[] = 'END:VCALENDAR';
$IcsUrl = 'data:text/calendar;charset=utf8;base64,' . base64_encode(implode("\r\n", $IcsUrlParts));
function echoLink($url, $name, $filename = '')
{
echo '<a href="' . $url . '" target="_blank"' . ($filename != '' ? 'download="' . $filename . '"' : '') . '>' . $name . '</a>' . PHP_EOL;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Kaltura Job Application</title>
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:200,300,400,600,700,900&display=swap" rel="stylesheet" />
<link rel="stylesheet" href="./css/theme.css" type="text/css" media="all" />
<link rel="icon" href="./css/images/fav_icon.png" sizes="32x32" />
<link rel="icon" href="./css/images/fav_icon.png" sizes="192x192" />
<link rel="apple-touch-icon" href="./css/images/fav_icon.png" />
<meta name="msapplication-TileImage" content="./css/images/fav_icon.png" />
</head>
<body>
<div id="maincontainer" class="main">
<h1>Add the meeting to your calendar</h1>
<div class="add2calendar">
<ul>
<li><span class="icon-google"></span> <?php echoLink($googleCalUrl, 'Google'); ?></li>
<li><span class="icon-microsoftoutlook"></span> <?php echoLink($outlook365Url, 'Outlook365'); ?></li>
<li><span class="icon-yahoo"></span> <?php echoLink($yahooUrl, 'Yahoo!'); ?></li>
<li><span class="icon-calendar"></span> <?php echoLink($IcsUrl, 'Download ICS file', 'meetingCal.ics'); ?></li>
</ul>
</div>
</div>
</body>
</html>