summaryrefslogtreecommitdiff
path: root/includes/pages/user_ical.php
diff options
context:
space:
mode:
authormsquare <msquare@notrademark.de>2017-11-28 15:43:51 +0100
committerGitHub <noreply@github.com>2017-11-28 15:43:51 +0100
commit599f2fd264bfc7b1b6826fe206442806e317340f (patch)
tree50cf84d7d07d11bd65b45c2c17f37632f6cd8eff /includes/pages/user_ical.php
parenta5fc5bd0979e8de1fce8a8addd351a6e7bd6aeb8 (diff)
parenteda7f7788ea8012bd8be46405c56a666c11f3fa5 (diff)
Merge pull request #365 from engelsystem/feature-igel-rewrite
Feature igel rewrite
Diffstat (limited to 'includes/pages/user_ical.php')
-rw-r--r--includes/pages/user_ical.php96
1 files changed, 51 insertions, 45 deletions
diff --git a/includes/pages/user_ical.php b/includes/pages/user_ical.php
index 34860b70..8d22c4eb 100644
--- a/includes/pages/user_ical.php
+++ b/includes/pages/user_ical.php
@@ -3,61 +3,67 @@
/**
* Controller for ical output of users own shifts or any user_shifts filter.
*/
-function user_ical() {
- global $user;
-
- if (! isset($_REQUEST['key']) || ! preg_match("/^[0-9a-f]{32}$/", $_REQUEST['key'])) {
- engelsystem_error("Missing key.");
- }
- $key = $_REQUEST['key'];
-
- $user = User_by_api_key($key);
- if ($user == null) {
- engelsystem_error("Key invalid.");
- }
-
- if (! in_array('ical', privileges_for_user($user['UID']))) {
- engelsystem_error("No privilege for ical.");
- }
-
- $ical_shifts = load_ical_shifts();
-
- send_ical_from_shifts($ical_shifts);
+function user_ical()
+{
+ global $user;
+ $request = request();
+
+ if (!$request->has('key') || !preg_match('/^[\da-f]{32}$/', $request->input('key'))) {
+ engelsystem_error('Missing key.');
+ }
+ $key = $request->input('key');
+
+ $user = User_by_api_key($key);
+ if ($user == null) {
+ engelsystem_error('Key invalid.');
+ }
+
+ if (!in_array('ical', privileges_for_user($user['UID']))) {
+ engelsystem_error('No privilege for ical.');
+ }
+
+ $ical_shifts = load_ical_shifts();
+
+ send_ical_from_shifts($ical_shifts);
}
/**
* Renders an ical calender from given shifts array.
*
- * @param array<Shift> $shifts
+ * @param array <Shift> $shifts
*/
-function send_ical_from_shifts($shifts) {
- header("Content-Type: text/calendar; charset=utf-8");
- $output = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//-//Engelsystem//DE\r\nCALSCALE:GREGORIAN\r\n";
- foreach ($shifts as $shift) {
- $output .= make_ical_entry_from_shift($shift);
- }
- $output .= "END:VCALENDAR\r\n";
- $output = trim($output, "\x0A");
- header("Content-Length: " . strlen($output));
- raw_output($output);
+function send_ical_from_shifts($shifts)
+{
+ header('Content-Type: text/calendar; charset=utf-8');
+ header('Content-Disposition: attachment; filename=shifts.ics');
+ $output = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//-//Engelsystem//DE\r\nCALSCALE:GREGORIAN\r\n";
+ foreach ($shifts as $shift) {
+ $output .= make_ical_entry_from_shift($shift);
+ }
+ $output .= "END:VCALENDAR\r\n";
+ $output = trim($output, "\x0A");
+ header('Content-Length: ' . strlen($output));
+ raw_output($output);
}
/**
* Renders an ical vevent from given shift.
*
- * @param Shift $shift
+ * @param array $shift
+ * @return string
*/
-function make_ical_entry_from_shift($shift) {
- $output = "BEGIN:VEVENT\r\n";
- $output .= "UID:" . md5($shift['start'] . $shift['end'] . $shift['name']) . "\r\n";
- $output .= "SUMMARY:" . str_replace("\n", "\\n", $shift['name']) . " (" . str_replace("\n", "\\n", $shift['title']) . ")\r\n";
- if (isset($shift['Comment'])) {
- $output .= "DESCRIPTION:" . str_replace("\n", "\\n", $shift['Comment']) . "\r\n";
- }
- $output .= "DTSTART;TZID=Europe/Berlin:" . date("Ymd\THis", $shift['start']) . "\r\n";
- $output .= "DTEND;TZID=Europe/Berlin:" . date("Ymd\THis", $shift['end']) . "\r\n";
- $output .= "LOCATION:" . $shift['Name'] . "\r\n";
- $output .= "END:VEVENT\r\n";
- return $output;
+function make_ical_entry_from_shift($shift)
+{
+ $output = "BEGIN:VEVENT\r\n";
+ $output .= 'UID:' . md5($shift['start'] . $shift['end'] . $shift['name']) . "\r\n";
+ $output .= 'SUMMARY:' . str_replace("\n", "\\n", $shift['name'])
+ . ' (' . str_replace("\n", "\\n", $shift['title']) . ")\r\n";
+ if (isset($shift['Comment'])) {
+ $output .= 'DESCRIPTION:' . str_replace("\n", "\\n", $shift['Comment']) . "\r\n";
+ }
+ $output .= 'DTSTART;TZID=Europe/Berlin:' . date("Ymd\THis", $shift['start']) . "\r\n";
+ $output .= 'DTEND;TZID=Europe/Berlin:' . date("Ymd\THis", $shift['end']) . "\r\n";
+ $output .= 'LOCATION:' . $shift['Name'] . "\r\n";
+ $output .= "END:VEVENT\r\n";
+ return $output;
}
-?>