diff options
author | msquare <msquare@notrademark.de> | 2016-08-21 22:58:09 +0200 |
---|---|---|
committer | msquare <msquare@notrademark.de> | 2016-08-21 22:58:09 +0200 |
commit | 28e5a3b978c705e20457b5c71a086e6ba23757d0 (patch) | |
tree | 7f0c37d2ddd3758aaac5645ec4591da0ae08d014 | |
parent | e89727de7d12edfb3f309c485a5effbbfc9e4c2f (diff) |
user_ical rewrite (remove codacy issues)
-rw-r--r-- | includes/helper/error_helper.php | 2 | ||||
-rw-r--r-- | includes/pages/user_ical.php | 108 | ||||
-rw-r--r-- | includes/sys_page.php | 10 |
3 files changed, 79 insertions, 41 deletions
diff --git a/includes/helper/error_helper.php b/includes/helper/error_helper.php index b4b4f248..58d0ac86 100644 --- a/includes/helper/error_helper.php +++ b/includes/helper/error_helper.php @@ -6,7 +6,7 @@ * @param string $message */ function engelsystem_error($message) { - die($message); + raw_output($message); } ?>
\ No newline at end of file diff --git a/includes/pages/user_ical.php b/includes/pages/user_ical.php index 2f3d5cd5..c38a336c 100644 --- a/includes/pages/user_ical.php +++ b/includes/pages/user_ical.php @@ -1,54 +1,82 @@ <?php - -// Öffentlich zugängliche Funktion zum Abrufen von iCal-Exports der eigenen Schichten +/** + * Controller for ical output of users own shifts or any user_shifts filter. + */ function user_ical() { - global $ical_shifts, $user; - - if (isset ($_REQUEST['key']) && preg_match("/^[0-9a-f]{32}$/", $_REQUEST['key'])) - $key = $_REQUEST['key']; - else - die("Missing key."); - + 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) - die("Unable to find user."); - if($user == null) - die("Key invalid."); - if(!in_array('ical', privileges_for_user($user['UID']))) - die("No privilege for ical."); + 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); +} - if (isset ($_REQUEST['export']) && $_REQUEST['export'] == 'user_shifts') { +/** + * Returns shifts to export. + * Users shifts or user_shifts filter based shifts if export=user_shifts is given as param. + */ +function load_ical_shifts() { + global $user, $ical_shifts; + + if (isset($_REQUEST['export']) && $_REQUEST['export'] == 'user_shifts') { require_once realpath(__DIR__ . '/user_shifts.php'); view_user_shifts(); - } else { - $ical_shifts = sql_select(" - SELECT `ShiftTypes`.`name`, `Shifts`.*, `Room`.`Name` as `room_name` - FROM `ShiftEntry` - INNER JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`) - JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`) - INNER JOIN `Room` ON (`Shifts`.`RID` = `Room`.`RID`) - WHERE `UID`='" . sql_escape($user['UID']) . "' - ORDER BY `start`"); + + return $ical_shifts; } + + return Shifts_by_user($user); +} +/** + * Renders an ical calender from given shifts array. + * + * @param array<Shift> $shifts + */ +function send_ical_from_shifts($shifts) { header("Content-Type: text/calendar; charset=utf-8"); - $html = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//-//Engelsystem//DE\r\nCALSCALE:GREGORIAN\r\n"; + $output = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//-//Engelsystem//DE\r\nCALSCALE:GREGORIAN\r\n"; foreach ($ical_shifts as $shift) { - $html .= "BEGIN:VEVENT\r\n"; - $html .= "UID:" . md5($shift['start'] . $shift['end'] . $shift['name']) . "\r\n"; - $html .= "SUMMARY:" . str_replace("\n", "\\n", $shift['name']) . " (" . str_replace("\n", "\\n", $shift['title']) . ")\r\n"; - if(isset($shift['Comment'])) - $html .= "DESCRIPTION:" . str_replace("\n", "\\n", $shift['Comment']) . "\r\n"; - $html .= "DTSTART;TZID=Europe/Berlin:" . date("Ymd\THis", $shift['start']) . "\r\n"; - $html .= "DTEND;TZID=Europe/Berlin:" . date("Ymd\THis", $shift['end']) . "\r\n"; - $html .= "LOCATION:" . $shift['room_name'] . "\r\n"; - $html .= "END:VEVENT\r\n"; + $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"; } - $html .= "END:VCALENDAR\r\n"; - $html = trim($html, "\x0A"); - header("Content-Length: " . strlen($html)); - echo $html; - die(); + $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; } ?> diff --git a/includes/sys_page.php b/includes/sys_page.php index 967a0172..52cffb26 100644 --- a/includes/sys_page.php +++ b/includes/sys_page.php @@ -9,6 +9,16 @@ function redirect($to) { } /** + * Echoes given output and dies. + * + * @param String $output + */ +function raw_output($output) { + echo $output; + die(); +} + +/** * Gibt den gefilterten REQUEST Wert ohne Zeilenumbrüche zurück */ function strip_request_item($name) { |