engelsystem/includes/pages/user_ical.php

66 lines
1.9 KiB
PHP
Raw Normal View History

2011-10-11 19:47:49 +02:00
<?php
/**
* Controller for ical output of users own shifts or any user_shifts filter.
*/
2011-10-11 19:47:49 +02:00
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 === false) {
engelsystem_error("Unable to find user.");
}
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);
}
2011-10-11 19:47:49 +02:00
/**
* Renders an ical calender from given shifts array.
*
* @param array<Shift> $shifts
*/
function send_ical_from_shifts($shifts) {
2012-12-03 21:00:20 +01:00
header("Content-Type: text/calendar; charset=utf-8");
$output = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//-//Engelsystem//DE\r\nCALSCALE:GREGORIAN\r\n";
2016-08-21 23:00:58 +02:00
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
*/
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";
2012-12-03 21:00:20 +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";
$output .= "END:VEVENT\r\n";
return $output;
2011-10-11 19:47:49 +02:00
}
?>