From 75e738b2351f91e36451261207abca53f0851ff0 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 5 Dec 2017 13:44:20 +0100 Subject: [locationinfo] Fix formatting of calendar starttime/endtime, remove if not possible The weekcalendar plugin chokes on badly formatted timestamps, so either fix or remove dates which don't match the expectation. Fixes #3192 --- .../locationinfo/inc/coursebackend.inc.php | 34 +++++++++++++++++++--- .../inc/coursebackend/coursebackend_dummy.inc.php | 15 ++++++++-- 2 files changed, 43 insertions(+), 6 deletions(-) (limited to 'modules-available/locationinfo') diff --git a/modules-available/locationinfo/inc/coursebackend.inc.php b/modules-available/locationinfo/inc/coursebackend.inc.php index 1fe87202..bf3e2e1f 100644 --- a/modules-available/locationinfo/inc/coursebackend.inc.php +++ b/modules-available/locationinfo/inc/coursebackend.inc.php @@ -131,6 +131,19 @@ abstract class CourseBackend */ protected abstract function fetchSchedulesInternal($roomId); + private static function fixTime(&$start, &$end) + { + if (!preg_match('/^\d+-\d+-\d+T\d+:\d+:\d+$/', $start) || !preg_match('/^\d+-\d+-\d+T\d+:\d+:\d+$/', $start)) + return false; + $start = strtotime($start); + $end = strtotime($end); + if ($start >= $end) + return false; + $start = date('Y-m-d\TH:i:s', $start); + $end = date('Y-m-d\TH:i:s', $end); + return true; + } + /** * Method for fetching the schedule of the given rooms on a server. * @@ -184,18 +197,31 @@ abstract class CourseBackend return false; } - if ($this->getCacheTime() > 0) { - // Caching requested by backend, write to DB - foreach ($backendResponse as $serverRoomId => $calendar) { + foreach ($backendResponse as $serverRoomId => &$calendar) { + $calendar = array_values($calendar); + for ($i = 0; $i < count($calendar); ++$i) { + if (empty($calendar[$i]['title'])) { + $calendar[$i]['title'] = '-'; + } + if (!self::fixTime($calendar[$i]['start'], $calendar[$i]['end'])) { + error_log("Ignoring calendar entry '{$calendar[$i]['title']}' with bad time format"); + unset($calendar[$i]); + } + } + $calendar = array_values($calendar); + if ($this->getCacheTime() > 0) { + // Caching requested by backend, write to DB $value = json_encode($calendar); Database::simpleQuery("UPDATE locationinfo_locationconfig SET calendar = :ttable, lastcalendarupdate = :now - WHERE serverid = :serverid AND serverlocationid = :serverlocationid", array( + WHERE serverid = :serverid AND serverlocationid = :serverlocationid", array( 'serverid' => $this->serverId, 'serverlocationid' => $serverRoomId, 'ttable' => $value, 'now' => $NOW )); } + + unset($calendar); } // Add rooms that were requested to the final return value foreach ($remoteIds as $location => $serverRoomId) { diff --git a/modules-available/locationinfo/inc/coursebackend/coursebackend_dummy.inc.php b/modules-available/locationinfo/inc/coursebackend/coursebackend_dummy.inc.php index e2577284..24e01070 100644 --- a/modules-available/locationinfo/inc/coursebackend/coursebackend_dummy.inc.php +++ b/modules-available/locationinfo/inc/coursebackend/coursebackend_dummy.inc.php @@ -87,13 +87,24 @@ class CourseBackend_Dummy extends CourseBackend * * @param $roomIds array with local ID as key and serverId as value * @return array a recursive array that uses the roomID as key - * and has the schedule array as value. A shedule array contains an array in this format: - * ["start"=>'JJJJ-MM-DD HH:MM:SS',"end"=>'JJJJ-MM-DD HH:MM:SS',"title"=>string] + * and has the schedule array as value. A schedule array contains an array in this format: + * ["start"=>'YYYY-MM-DDHH:MM:SS',"end"=>'YYYY-MM-DDHH:MM:SS',"title"=>string] */ public function fetchSchedulesInternal($roomId) { $a = array(); foreach ($roomId as $id) { + if ($id == 1) { + $now = time(); + return array($id => array( + array( + 'title' => 'Lange', + 'start' => date('Y-m-d', $now) . 'T0:00:00', + 'end' => date('Y-m-d', $now + 86400 * 3) . 'T0:00:00', + ) + )); + } + // Normal $x = array(); $time = strtotime('today'); $end = strtotime('+7 days', $time); -- cgit v1.2.3-55-g7522