diff options
Diffstat (limited to 'modules-available/locationinfo/inc/coursebackend')
-rw-r--r-- | modules-available/locationinfo/inc/coursebackend/coursebackend_hisinone.inc.php | 109 | ||||
-rw-r--r-- | modules-available/locationinfo/inc/coursebackend/coursebackend_ical.inc.php | 61 |
2 files changed, 68 insertions, 102 deletions
diff --git a/modules-available/locationinfo/inc/coursebackend/coursebackend_hisinone.inc.php b/modules-available/locationinfo/inc/coursebackend/coursebackend_hisinone.inc.php index 82efae87..8bd18169 100644 --- a/modules-available/locationinfo/inc/coursebackend/coursebackend_hisinone.inc.php +++ b/modules-available/locationinfo/inc/coursebackend/coursebackend_hisinone.inc.php @@ -1,15 +1,7 @@ <?php -class CourseBackend_HisInOne extends CourseBackend +class CourseBackend_HisInOne extends ICalCourseBackend { - private $location; - private $verifyHostname = true; - private $verifyCert = true; - /** - * @var bool|resource - */ - private $curlHandle = false; - public function setCredentialsInternal($data) { @@ -18,9 +10,8 @@ class CourseBackend_HisInOne extends CourseBackend return false; } - $this->location = $this->mangleProperty('baseUrl', $data['baseUrl']); - $this->verifyHostname = $data['verifyHostname']; - $this->verifyCert = $data['verifyCert']; + $this->init($this->mangleProperty('baseUrl', $data['baseUrl']), + $data['verifyCert'], $data['verifyHostname']); return true; } @@ -39,14 +30,14 @@ class CourseBackend_HisInOne extends CourseBackend if ($prop === 'baseUrl') { // Update form SOAP to iCal url if (preg_match(',^(http.*?)/qisserver,', $value, $out)) { - $value = $out[1] . '/qisserver/pages/cm/exa/timetable/roomScheduleCalendarExport.faces?roomId='; + $value = $out[1] . '/qisserver/pages/cm/exa/timetable/roomScheduleCalendarExport.faces?roomId=%ID%'; } elseif (preg_match(',(.*[/=])\d*$', $value, $out)) { - $value = $out[1]; + $value = $out[1] . '%ID%'; } elseif (substr_count($value, '/') <= 3) { if (substr($value, -1) !== '/') { $value .= '/'; } - $value .= 'qisserver/pages/cm/exa/timetable/roomScheduleCalendarExport.faces?roomId='; + $value .= 'qisserver/pages/cm/exa/timetable/roomScheduleCalendarExport.faces?roomId=%ID%'; } } return $value; @@ -54,10 +45,8 @@ class CourseBackend_HisInOne extends CourseBackend public function checkConnection() { - if (empty($this->location)) { - $this->addError("Credentials are not set", true); + if (!$this->isOK()) return false; - } // Unfortunately HisInOne returns an internal server error if you pass an invalid roomId. // So we just try a bunch and see if anything works. Even if this fails, using // the backend should work, given the URL is actually correct. @@ -68,43 +57,6 @@ class CourseBackend_HisInOne extends CourseBackend return false; } - /** - * @param int $roomId room id - * @return ICalEvent[]|null all events for this room in the range -7 days to +7 days, or NULL on error - */ - private function downloadIcal($roomId) - { - if ($this->curlHandle === false) { - $this->curlHandle = curl_init(); - } - - $ical = new ICalParser(['filterDaysBefore' => 7, 'filterDaysAfter' => 7]); - $options = array( - CURLOPT_WRITEFUNCTION => function ($ch, $data) use ($ical) { - $ical->feedData($data); - return strlen($data); - }, - CURLOPT_FOLLOWLOCATION => true, - CURLOPT_SSL_VERIFYHOST => $this->verifyHostname ? 2 : 0, - CURLOPT_SSL_VERIFYPEER => $this->verifyCert ? 1 : 0, - CURLOPT_URL => $this->location . $roomId, - CURLOPT_TIMEOUT => 60, - CURLOPT_CONNECTTIMEOUT => 4, - ); - - curl_setopt_array($this->curlHandle, $options); - - if (!curl_exec($this->curlHandle)) { - $this->addError('Curl error: ' . curl_error($this->curlHandle), false); - } - $ical->finish(); - if (!$ical->isValid()) { - $this->addError("Did not find a VCALENDAR in returned data for {$this->location}$roomId", false); - return null; - } - return $ical->events(); - } - public function getCacheTime() { return 30 * 60; @@ -121,51 +73,4 @@ class CourseBackend_HisInOne extends CourseBackend return "HisInOne"; } - public function fetchSchedulesInternal($requestedRoomIds) - { - if (empty($requestedRoomIds)) { - return array(); - } - $tTables = []; - foreach ($requestedRoomIds as $roomId) { - $data = $this->downloadIcal($roomId); - if ($data === null) { - $this->addError("Downloading ical for $roomId failed", false); - continue; - } - foreach ($data as $event) { - $tTables[$roomId][] = array( - 'title' => $this->toTitle($event), - 'start' => $event->dtstart, - 'end' => $event->dtend, - 'cancelled' => false, // ??? How - ); - } - } - return $tTables; - } - - /** - * Get a usable title from either SUMMARY or DESCRIPTION - * @param ICalEvent $event - */ - private function toTitle($event) - { - $title = $event->summary; - if (empty($title)) { - $title = $event->description; - } - if (empty($title)) { - $title = 'Unknown'; - } - return preg_replace([',(\s*<br\s*/?>\s*|\r|\n|\\\r|\\\n)+,', '/\\\\([,;:])/'], ["\n", '$1'], $title); - } - - public function __destruct() - { - if ($this->curlHandle !== false) { - curl_close($this->curlHandle); - } - } - } diff --git a/modules-available/locationinfo/inc/coursebackend/coursebackend_ical.inc.php b/modules-available/locationinfo/inc/coursebackend/coursebackend_ical.inc.php new file mode 100644 index 00000000..98dca1cb --- /dev/null +++ b/modules-available/locationinfo/inc/coursebackend/coursebackend_ical.inc.php @@ -0,0 +1,61 @@ +<?php + +class CourseBackend_ICal extends ICalCourseBackend +{ + + /** @var string room ID for testing connection */ + private $testId; + + public function setCredentialsInternal($data) + { + if (empty($data['baseUrl'])) { + $this->addError("No url is given", true); + return false; + } + + $this->init($data['baseUrl'], $data['verifyCert'], $data['verifyHostname'], $data['authMethod'], + $data['user'], $data['pass']); + $this->testId = $data['testId']; + + return true; + } + + public function getCredentialDefinitions() + { + return [ + new BackendProperty('baseUrl', 'string'), + new BackendProperty('verifyCert', 'bool', true), + new BackendProperty('verifyHostname', 'bool', true), + new BackendProperty('testId', 'string'), + new BackendProperty('authMethod', ['NONE', 'BASIC', 'DIGEST', 'GSSNEGOTIATE', 'NTLM'], 'NONE'), + new BackendProperty('user', 'string'), + new BackendProperty('pass', 'string'), + ]; + } + + public function checkConnection() + { + if (!$this->isOK()) + return false; + if (empty($this->testId)) + return true; + return ($this->downloadIcal($this->testId) !== null); + } + + public function getCacheTime() + { + return 30 * 60; + } + + public function getRefreshTime() + { + return 60 * 60; + } + + + public function getDisplayName() + { + return "iCal"; + } + +} |