diff options
Diffstat (limited to 'modules-available/locationinfo/page.inc.php')
-rw-r--r-- | modules-available/locationinfo/page.inc.php | 167 |
1 files changed, 89 insertions, 78 deletions
diff --git a/modules-available/locationinfo/page.inc.php b/modules-available/locationinfo/page.inc.php index 9e7a704e..63a02ba2 100644 --- a/modules-available/locationinfo/page.inc.php +++ b/modules-available/locationinfo/page.inc.php @@ -81,7 +81,7 @@ class Page_LocationInfo extends Page $this->showLocationsTable(); break; case 'backends': - $this->showBackendsTable($backends); + $this->showBackendsTable($backends ?? []); break; case 'edit-panel': $this->showPanelConfig(); @@ -100,7 +100,7 @@ class Page_LocationInfo extends Page /** * Deletes the server from the db. */ - private function deleteServer($id) + private function deleteServer($id): void { User::assertPermission('backend.edit'); if ($id === 0) { @@ -113,7 +113,7 @@ class Page_LocationInfo extends Page } } - private function deletePanel() + private function deletePanel(): void { $id = Request::post('uuid', false, 'string'); if ($id === false) { @@ -130,25 +130,27 @@ class Page_LocationInfo extends Page } } - private function getTime($str) + private static function getTime(string $str): ?int { $str = explode(':', $str); - if (count($str) !== 2) return false; - if ($str[0] < 0 || $str[0] > 23 || $str[1] < 0 || $str[1] > 59) return false; + if (count($str) !== 2) + return null; + if ($str[0] < 0 || $str[0] > 23 || $str[1] < 0 || $str[1] > 59) + return null; return $str[0] * 60 + $str[1]; } - private function writeLocationConfig() + private function writeLocationConfig(): void { // Check locations $locationid = Request::post('locationid', false, 'int'); if ($locationid === false) { Message::addError('main.parameter-missing', 'locationid'); - return false; + return; } if (Location::get($locationid) === false) { Message::addError('location.invalid-location-id', $locationid); - return false; + return; } User::assertPermission('location.edit', $locationid); @@ -198,29 +200,23 @@ class Page_LocationInfo extends Page )); } } - - return true; } /** * Get all location ids from the locationids parameter, which is comma separated, then split - * and remove any ids that don't exist. The cleaned list will be returned + * and remove any ids that don't exist. The cleaned list will be returned. + * Will show error and redirect to main page if parameter is missing + * * @param bool $failIfEmpty Show error and redirect to main page if parameter is missing or list is empty * @return array list of locations from parameter */ - private function getLocationIdsFromRequest($failIfEmpty) + private function getLocationIdsFromRequest(): array { - $locationids = Request::post('locationids', false, 'string'); - if ($locationids === false) { - if (!$failIfEmpty) - return array(); - Message::addError('main.parameter-missing', 'locationids'); - Util::redirect('?do=locationinfo'); - } + $locationids = Request::post('locationids', Request::REQUIRED_EMPTY, 'string'); $locationids = explode(',', $locationids); $all = array_map(function ($item) { return $item['locationid']; }, Location::queryLocations()); $locationids = array_filter($locationids, function ($item) use ($all) { return in_array($item, $all); }); - if ($failIfEmpty && empty($locationids)) { + if (empty($locationids)) { Message::addError('main.parameter-empty', 'locationids'); Util::redirect('?do=locationinfo'); } @@ -230,7 +226,7 @@ class Page_LocationInfo extends Page /** * Updated the config in the db. */ - private function writePanelConfig() + private function writePanelConfig(): void { // UUID - existing or new $paneluuid = Request::post('uuid', false, 'string'); @@ -253,7 +249,7 @@ class Page_LocationInfo extends Page } // Permission - $this->assertPanelPermission($paneluuid, 'panel.edit', $params['locationids']); + $this->assertPanelPermission($paneluuid, 'panel.edit', $params['locationids'] ?? []); if ($paneluuid === 'new') { $paneluuid = Util::randomUuid(); @@ -276,10 +272,13 @@ class Page_LocationInfo extends Page Util::redirect('?do=locationinfo'); } - private function preparePanelConfigDefault() + /** + * @return array{config: array, locationids: array} + */ + private function preparePanelConfigDefault(): array { // Check locations - $locationids = self::getLocationIdsFromRequest(true); + $locationids = self::getLocationIdsFromRequest(); if (count($locationids) > 4) { $locationids = array_slice($locationids, 0, 4); } @@ -331,7 +330,10 @@ class Page_LocationInfo extends Page return array('config' => $conf, 'locationids' => $locationids); } - private function preparePanelConfigUrl() + /** + * @return array{config: array, locationids: array} + */ + private function preparePanelConfigUrl(): array { $bookmarkNames = Request::post('bookmarkNames', [], 'array'); $bookmarkUrls = Request::post('bookmarkUrls', [], 'array'); @@ -349,18 +351,22 @@ class Page_LocationInfo extends Page 'url' => Request::post('url', 'https://www.bwlehrpool.de/', 'string'), 'insecure-ssl' => Request::post('insecure-ssl', 0, 'int'), 'reload-minutes' => max(0, Request::post('reloadminutes', 0, 'int')), - 'whitelist' => preg_replace("/[\r\n]+/ms", "\n", Request::post('whitelist', '', 'string')), - 'blacklist' => preg_replace("/[\r\n]+/ms", "\n", Request::post('blacklist', '', 'string')), + 'whitelist' => preg_replace("/[\r\n]+/m", "\n", Request::post('whitelist', '', 'string')), + 'blacklist' => preg_replace("/[\r\n]+/m", "\n", Request::post('blacklist', '', 'string')), 'split-login' => Request::post('split-login', 0, 'bool'), 'browser' => Request::post('browser', 'slx-browser', 'string'), 'interactive' => Request::post('interactive', '0', 'bool'), - 'bookmarks' => $bookmarkString ? $bookmarkString : '', + 'bookmarks' => $bookmarkString ?: '', 'allow-tty' => Request::post('allow-tty', '', 'string'), + 'zoom-factor' => Request::post('zoom-factor', 100, 'int'), ); return array('config' => $conf, 'locationids' => []); } - private function preparePanelConfigSummary() + /** + * @return array{config: array, locationids: array} + */ + private function preparePanelConfigSummary(): array { // Build json structure $conf = array( @@ -373,14 +379,14 @@ class Page_LocationInfo extends Page $conf['panelupdate'] = 15; } // Check locations - $locationids = self::getLocationIdsFromRequest(true); + $locationids = self::getLocationIdsFromRequest(); return array('config' => $conf, 'locationids' => $locationids); } /** * Updates the server settings in the db. */ - private function updateServerSettings() + private function updateServerSettings(): void { User::assertPermission('backend.edit'); $serverid = Request::post('id', -1, 'int'); @@ -422,10 +428,10 @@ class Page_LocationInfo extends Page * * @param int $id Server id which connection should be checked. */ - private function checkConnection($serverid = 0) + private function checkConnection(int $serverid = 0): void { if ($serverid === 0) { - Util::traceError('checkConnection called with no server id'); + ErrorHandler::traceError('checkConnection called with no server id'); } User::assertPermission('backend.check'); @@ -438,7 +444,8 @@ class Page_LocationInfo extends Page LocationInfo::setServerError($serverid, 'Unknown backend type: ' . $dbresult['servertype']); return; } - $credentialsOk = $serverInstance->setCredentials($serverid, json_decode($dbresult['credentials'], true)); + $credentialsOk = $serverInstance->setCredentials($serverid, + (array)json_decode($dbresult['credentials'], true)); if ($credentialsOk) { $serverInstance->checkConnection(); @@ -447,7 +454,7 @@ class Page_LocationInfo extends Page LocationInfo::setServerError($serverid, $serverInstance->getErrors()); } - private function loadBackends() + private function loadBackends(): array { // Get a list of all the backend types. $servertypes = array(); @@ -459,7 +466,7 @@ class Page_LocationInfo extends Page // Build list of defined backends $serverlist = array(); $dbquery2 = Database::simpleQuery("SELECT * FROM `locationinfo_coursebackend` ORDER BY servername ASC"); - while ($row = $dbquery2->fetch(PDO::FETCH_ASSOC)) { + foreach ($dbquery2 as $row) { if (isset($servertypes[$row['servertype']])) { $row['typename'] = $servertypes[$row['servertype']]; } else { @@ -485,7 +492,7 @@ class Page_LocationInfo extends Page /** * Show the list of backends */ - private function showBackendsTable($serverlist) + private function showBackendsTable(array $serverlist): void { User::assertPermission('backend.*'); $data = array( @@ -496,7 +503,7 @@ class Page_LocationInfo extends Page Render::addTemplate('page-servers', $data); } - private function showBackendLog() + private function showBackendLog(): void { $id = Request::get('serverid', false, 'int'); if ($id === false) { @@ -512,7 +519,7 @@ class Page_LocationInfo extends Page $server['list'] = []; $res = Database::simpleQuery('SELECT dateline, message FROM locationinfo_backendlog WHERE serverid = :id ORDER BY logid DESC LIMIT 100', ['id' => $id]); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { $row['dateline_s'] = Util::prettyTime($row['dateline']); $row['class'] = substr($row['message'], 0, 3) === '[F]' ? 'text-danger' : 'text-warning'; $row['message'] = Substr($row['message'], 3); @@ -521,7 +528,7 @@ class Page_LocationInfo extends Page Render::addTemplate('page-server-log', $server); } - private function showLocationsTable() + private function showLocationsTable(): void { $allowedLocations = User::getAllowedLocations('location.edit'); if (empty($allowedLocations)) { @@ -536,7 +543,7 @@ class Page_LocationInfo extends Page LEFT JOIN `locationinfo_coursebackend` AS cb USING (serverid) LEFT JOIN `location` AS loc USING (locationid)"); - while ($row = $dbquery->fetch(PDO::FETCH_ASSOC)) { + foreach ($dbquery as $row) { $locid = (int)$row['locationid']; if (!isset($locations[$locid]) || !in_array($locid, $allowedLocations)) continue; @@ -547,6 +554,7 @@ class Page_LocationInfo extends Page } $locations[$locid] += array( 'openingGlyph' => $glyph, + 'strong' => $glyph === 'ok', 'backend' => $backend, 'lastCalendarUpdate' => Util::prettyTime($row['lastcalendarupdate']), // TODO 'backendMissing' => !CourseBackend::exists($row['servertype']), @@ -575,11 +583,20 @@ class Page_LocationInfo extends Page )); } - private function showPanelsTable() + private function showPanelsTable(): void { $visibleLocations = User::getAllowedLocations('panel.list'); + if (in_array(0, $visibleLocations)) { + $visibleLocations = true; + } $editLocations = User::getAllowedLocations('panel.edit'); + if (in_array(0, $editLocations)) { + $editLocations = true; + } $assignLocations = USer::getAllowedLocations('panel.assign-client'); + if (in_array(0, $assignLocations)) { + $assignLocations = true; + } if (empty($visibleLocations)) { Message::addError('main.no-permission'); return; @@ -593,7 +610,7 @@ class Page_LocationInfo extends Page } $panels = array(); $locations = Location::getLocationsAssoc(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { if ($row['paneltype'] === 'URL') { $url = json_decode($row['panelconfig'], true)['url']; $row['locations'] = $row['locationurl'] = $url; @@ -602,14 +619,16 @@ class Page_LocationInfo extends Page } else { $lids = explode(',', $row['locationids']); // Permissions - if (!empty(array_diff($lids, $visibleLocations))) { + if ($visibleLocations !== true && !empty(array_diff($lids, $visibleLocations))) { continue; } - $row['edit_disabled'] = !empty(array_diff($lids, $editLocations)) ? 'disabled' : ''; - $row['runmode_disabled'] = !empty(array_diff($lids, $assignLocations)) ? 'disabled' : ''; + $row['edit_disabled'] = $editLocations !== true && !empty(array_diff($lids, $editLocations)) + ? 'disabled' : ''; + $row['runmode_disabled'] = $assignLocations !== true && !empty(array_diff($lids, $assignLocations)) + ? 'disabled' : ''; // Locations $locs = array_map(function ($id) use ($locations) { - return isset($locations[$id]) ? $locations[$id]['locationname'] : $id; + return isset($locations[$id]) ? $locations[$id]['locationname'] : "<<deleted=$id>>"; }, $lids); $row['locations'] = implode(', ', $locs); } @@ -648,7 +667,7 @@ class Page_LocationInfo extends Page * * @param int $id Serverid */ - private function ajaxServerSettings($id) + private function ajaxServerSettings(int $id): void { User::assertPermission('backend.edit'); $oldConfig = Database::queryFirst('SELECT servername, servertype, credentials @@ -679,7 +698,7 @@ class Page_LocationInfo extends Page } else { $cred->initForRender(); } - $cred->title = Dictionary::translateFile('backend-' . $s, $cred->property, true); + $cred->title = Dictionary::translateFile('backend-' . $s, $cred->property); $cred->helptext = Dictionary::translateFile('backend-' . $s, $cred->property . "_helptext"); $cred->credentialsHtml = Render::parse('server-prop-' . $cred->template, (array)$cred); } @@ -697,7 +716,7 @@ class Page_LocationInfo extends Page * * @param int $id id of the location */ - private function ajaxConfigLocation($id) + private function ajaxConfigLocation(int $id): void { User::assertPermission('location.edit', $id); $locConfig = Database::queryFirst("SELECT info.serverid, info.serverlocationid, loc.openingtime @@ -721,7 +740,7 @@ class Page_LocationInfo extends Page WHERE locationid IN (:locations) AND serverid IS NOT NULL", array('locations' => $chain)); $chain = array_flip($chain); $best = false; - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { if ($best === false || $chain[$row['locationid']] < $chain[$best['locationid']]) { $best = $row; } @@ -735,7 +754,7 @@ class Page_LocationInfo extends Page // get Server / ID list $res = Database::simpleQuery("SELECT serverid, servername FROM locationinfo_coursebackend ORDER BY servername ASC"); $serverList = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { if ($row['serverid'] == $locConfig['serverid']) { $row['selected'] = 'selected'; } @@ -752,12 +771,6 @@ class Page_LocationInfo extends Page echo Render::parse('ajax-config-location', $data); } - private function fmtTime($time) - { - $t = explode(':', $time); - return sprintf('%02d:%02d', $t[0], $t[1]); - } - /** * Checks if simple mode or expert mode is active. * Tries to merge/compact the opening times schedule, and @@ -766,7 +779,7 @@ class Page_LocationInfo extends Page * * @return array new optimized openingtimes */ - private function compressTimes(&$array) + private function compressTimes(array $array): array { if (empty($array)) return []; @@ -774,9 +787,9 @@ class Page_LocationInfo extends Page $DAYLIST = array_flip(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']); $new = []; foreach ($array as $row) { - $s = $this->getTime($row['openingtime']); - $e = $this->getTime($row['closingtime']); - if ($s === false || $e === false || $e <= $s) + $s = Page_LocationInfo::getTime($row['openingtime']); + $e = Page_LocationInfo::getTime($row['closingtime']); + if ($s === null || $e === null || $e <= $s) continue; foreach ($row['days'] as $day) { $day = $DAYLIST[$day] ?? -1; @@ -811,9 +824,9 @@ class Page_LocationInfo extends Page /** * @param array $daysArray List of days, "Monday", "Tuesday" etc. Must not contain duplicates. - * @return string Human readable representation of list of days + * @return string Human-readable representation of list of days */ - private function buildDaysString(array $daysArray) + private function buildDaysString(array $daysArray): string { /* Dictionary::translate('monday') Dictionary::translate('tuesday') Dictionary::translate('wednesday') * Dictionary::translate('thursday') Dictionary::translate('friday') Dictionary::translate('saturday') @@ -831,10 +844,10 @@ class Page_LocationInfo extends Page // Chain $last++; } else { - $string = Dictionary::translate($DAYLIST[$first], true); + $string = Dictionary::translate($DAYLIST[$first]); if ($first !== $last) { $string .= ($first + 1 === $last ? ",\xe2\x80\x89" : "\xe2\x80\x89-\xe2\x80\x89") - . Dictionary::translate($DAYLIST[$last], true); + . Dictionary::translate($DAYLIST[$last]); } $output[] = $string; $first = $last = $day; @@ -870,7 +883,7 @@ class Page_LocationInfo extends Page // $start must lie before range start, otherwise we'd have hit the case above $e = $current[1]; unset($array[$day][$key]); - continue; + //continue; } } $array[$day][] = array($s, $e); @@ -878,10 +891,8 @@ class Page_LocationInfo extends Page /** * Ajax the config of a panel. - * - * @param $id Location ID */ - private function showPanelConfig() + private function showPanelConfig(): void { $id = Request::get('uuid', false, 'string'); if ($id === false) { @@ -1000,6 +1011,7 @@ class Page_LocationInfo extends Page 'uuid' => $id, 'panelname' => $panel['panelname'], 'url' => $config['url'], + 'zoom-factor' => $config['zoom-factor'], 'ssl_checked' => $config['insecure-ssl'] ? 'checked' : '', 'reloadminutes' => (int)$config['reload-minutes'], 'whitelist' => str_replace("\n", "\r\n", $config['whitelist']), @@ -1025,7 +1037,7 @@ class Page_LocationInfo extends Page } } - private function showPanel() + private function showPanel(): void { $uuid = Request::get('uuid', false, 'string'); if ($uuid === false) { @@ -1033,7 +1045,7 @@ class Page_LocationInfo extends Page die('Missing parameter uuid'); } $type = InfoPanel::getConfig($uuid, $config); - if ($type === false) { + if ($type === null) { http_response_code(404); die('Panel with given uuid not found'); } @@ -1060,7 +1072,7 @@ class Page_LocationInfo extends Page 'language' => $config['language'], ); - die(Render::parse('frontend-default', $data, $module = false, $lang = $config['language'])); + die(Render::parse('frontend-default', $data, null, $config['language'])); } if ($type === 'SUMMARY') { @@ -1072,7 +1084,7 @@ class Page_LocationInfo extends Page 'language' => $config['language'], ); - die(Render::parse('frontend-summary', $data, $module = false, $lang = $config['language'])); + die(Render::parse('frontend-summary', $data, null, $config['language'])); } http_response_code(500); @@ -1081,10 +1093,9 @@ class Page_LocationInfo extends Page /** * @param string|array $panelOrUuid UUID of panel, or array with keys paneltype and locationds - * @param string $permission - * @param null|int[] $additionalLocations + * @param int[] $additionalLocations */ - private function assertPanelPermission($panelOrUuid, $permission, $additionalLocations = null) + private function assertPanelPermission($panelOrUuid, string $permission, array $additionalLocations = null): void { if (is_array($panelOrUuid)) { $panel = $panelOrUuid; |