2011-10-11 19:47:49 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-21 22:58:09 +02:00
|
|
|
/**
|
|
|
|
* Controller for ical output of users own shifts or any user_shifts filter.
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function user_ical()
|
|
|
|
{
|
|
|
|
global $user;
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-07-18 21:38:53 +02:00
|
|
|
if (!$request->has('key') || !preg_match('/^[\da-f]{32}$/', $request->input('key'))) {
|
2017-01-03 14:12:17 +01:00
|
|
|
engelsystem_error('Missing key.');
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-07-18 21:38:53 +02:00
|
|
|
$key = $request->input('key');
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$user = User_by_api_key($key);
|
2018-01-14 17:47:26 +01:00
|
|
|
if (empty($user)) {
|
2017-01-03 14:12:17 +01:00
|
|
|
engelsystem_error('Key invalid.');
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
|
|
|
if (!in_array('ical', privileges_for_user($user['UID']))) {
|
2017-01-03 14:12:17 +01:00
|
|
|
engelsystem_error('No privilege for ical.');
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$ical_shifts = load_ical_shifts();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
send_ical_from_shifts($ical_shifts);
|
2016-08-21 22:58:09 +02:00
|
|
|
}
|
2011-10-11 19:47:49 +02:00
|
|
|
|
2016-08-21 22:58:09 +02:00
|
|
|
/**
|
2017-12-25 21:29:00 +01:00
|
|
|
* Renders an ical calendar from given shifts array.
|
2016-08-21 22:58:09 +02:00
|
|
|
*
|
2017-12-25 23:12:52 +01:00
|
|
|
* @param array $shifts Shift
|
2016-08-21 22:58:09 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function send_ical_from_shifts($shifts)
|
|
|
|
{
|
2017-01-03 14:12:17 +01:00
|
|
|
header('Content-Type: text/calendar; charset=utf-8');
|
2017-01-03 15:32:12 +01:00
|
|
|
header('Content-Disposition: attachment; filename=shifts.ics');
|
2018-10-05 15:35:14 +02:00
|
|
|
$output = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//-//" . config('app_name') . "//DE\r\nCALSCALE:GREGORIAN\r\n";
|
2017-01-02 03:57:23 +01:00
|
|
|
foreach ($shifts as $shift) {
|
|
|
|
$output .= make_ical_entry_from_shift($shift);
|
|
|
|
}
|
|
|
|
$output .= "END:VCALENDAR\r\n";
|
|
|
|
$output = trim($output, "\x0A");
|
2017-01-03 14:12:17 +01:00
|
|
|
header('Content-Length: ' . strlen($output));
|
2017-01-02 03:57:23 +01:00
|
|
|
raw_output($output);
|
2016-08-21 22:58:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders an ical vevent from given shift.
|
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @param array $shift
|
|
|
|
* @return string
|
2016-08-21 22:58:09 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function make_ical_entry_from_shift($shift)
|
|
|
|
{
|
|
|
|
$output = "BEGIN:VEVENT\r\n";
|
2017-01-03 14:12:17 +01:00
|
|
|
$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";
|
2017-01-02 03:57:23 +01:00
|
|
|
if (isset($shift['Comment'])) {
|
2017-01-03 14:12:17 +01:00
|
|
|
$output .= 'DESCRIPTION:' . str_replace("\n", "\\n", $shift['Comment']) . "\r\n";
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-03 14:12:17 +01:00
|
|
|
$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";
|
2017-01-02 03:57:23 +01:00
|
|
|
$output .= "END:VEVENT\r\n";
|
|
|
|
return $output;
|
2011-10-11 19:47:49 +02:00
|
|
|
}
|