From 7d6b1cf2d4f3c5f78107f0454a3398e0cb167f69 Mon Sep 17 00:00:00 2001 From: mjansen Date: Wed, 13 Nov 2024 19:49:00 +0100 Subject: [PATCH] Init: Fix URLs in language selection dropdown See: https://mantis.ilias.de/view.php?id=42681 (cherry picked from commit 480920c05addfe1aa5b7d2d2461fef01b06cd985) (cherry picked from commit 2c45e22739211a75882408bbb58949df8a2d26e7) --- .../Provider/StartUpMetaBarProvider.php | 102 +++++++++--------- 1 file changed, 53 insertions(+), 49 deletions(-) diff --git a/Services/Init/classes/Provider/StartUpMetaBarProvider.php b/Services/Init/classes/Provider/StartUpMetaBarProvider.php index d2b9a88c6715..b0b14176a3db 100644 --- a/Services/Init/classes/Provider/StartUpMetaBarProvider.php +++ b/Services/Init/classes/Provider/StartUpMetaBarProvider.php @@ -21,15 +21,10 @@ namespace ILIAS\Init\Provider; use ILIAS\GlobalScreen\Identification\IdentificationInterface; -use ILIAS\GlobalScreen\Scope\MetaBar\Factory\TopParentItem; use ILIAS\GlobalScreen\Scope\MetaBar\Provider\AbstractStaticMetaBarProvider; use Psr\Http\Message\UriInterface; use ILIAS\Data\Factory as DataFactory; -/** - * Class StartUpMetaBarProvider - * @author Fabian Schmid - */ class StartUpMetaBarProvider extends AbstractStaticMetaBarProvider { /** @@ -54,7 +49,7 @@ public function getMetaBarItems(): array $target_str = ''; if (isset($request->getQueryParams()['ref_id']) && $ref_id = $request->getQueryParams()['ref_id']) { $type = \ilObject::_lookupType((int) $ref_id, true); - if ($type != "root") { // see bug #30710 + if ($type !== 'root') { // see bug #30710 $target_str = 'target=' . \ilObject::_lookupType((int) $ref_id, true) . '_' . (int) $ref_id . '&'; } } elseif (isset($request->getQueryParams()['target']) && $target = $request->getQueryParams()['target']) { @@ -63,46 +58,56 @@ public function getMetaBarItems(): array } $login_glyph = $factory->symbol()->glyph()->login(); - $login = $this->meta_bar->topLinkItem($if('login')) - ->withAction("login.php?" . $target_str . "client_id=" . rawurlencode(CLIENT_ID) . "&cmd=force_login&lang=" . $this->dic->user()->getCurrentLanguage()) - ->withSymbol($login_glyph) - ->withPosition(2) - ->withTitle($txt('log_in')) - ->withAvailableCallable(function () { - return !$this->isUserLoggedIn(); - }) - ->withVisibilityCallable(function () use ($request) { - return !$this->isUserOnLoginPage($request->getUri()); - }); + $login = $this->meta_bar + ->topLinkItem($if('login')) + ->withAction( + 'login.php?' . $target_str . 'client_id=' . rawurlencode( + CLIENT_ID + ) . '&cmd=force_login&lang=' . $this->dic->user()->getCurrentLanguage() + ) + ->withSymbol($login_glyph) + ->withPosition(2) + ->withTitle($txt('log_in')) + ->withAvailableCallable(function () { + return !$this->isUserLoggedIn(); + }) + ->withVisibilityCallable(function () use ($request) { + return !$this->isUserOnLoginPage($request->getUri()); + }); // Language-Selection - $language_selection = $this->meta_bar->topParentItem($if('language_selection')) - ->withSymbol($factory->symbol()->glyph()->language()) - ->withPosition(1) - ->withAvailableCallable(function () { - return !$this->isUserLoggedIn(); - }) - ->withVisibilityCallable(function () use ($languages) { - return count($languages) > 1; - }) - ->withTitle($txt('language')); + $language_selection = $this->meta_bar + ->topParentItem($if('language_selection')) + ->withSymbol($factory->symbol()->glyph()->language()) + ->withPosition(1) + ->withAvailableCallable(function () { + return !$this->isUserLoggedIn(); + }) + ->withVisibilityCallable(function () use ($languages) { + return \count($languages) > 1; + }) + ->withTitle($txt('language')); $base = $this->getBaseURL($request->getUri()); $dataFactory = new DataFactory(); foreach ($languages as $lang_key) { - $link = $this->appendUrlParameterString($base, "lang=" . $lang_key); - $language_name = $this->dic->language()->_lookupEntry($lang_key, "meta", "meta_l_" . $lang_key); - - $language_icon = $factory->symbol()->icon()->standard("none", $language_name) - ->withAbbreviation($lang_key); - - $s = $this->meta_bar->linkItem($if($lang_key)) - ->withSymbol($language_icon) - ->withAction($link) - ->withContentLanguage($dataFactory->languageTag($lang_key)) - ->withLanguageForTargetedResource($dataFactory->languageTag($lang_key)) - ->withTitle($language_name); + $link = $this->appendUrlParameterString($base, 'lang=' . $lang_key); + $language_name = $this->dic->language()->_lookupEntry($lang_key, 'meta', 'meta_l_' . $lang_key); + + $language_icon = $factory + ->symbol() + ->icon() + ->standard('none', $language_name) + ->withAbbreviation($lang_key); + + $s = $this->meta_bar + ->linkItem($if($lang_key)) + ->withSymbol($language_icon) + ->withAction($link) + ->withContentLanguage($dataFactory->languageTag($lang_key)) + ->withLanguageForTargetedResource($dataFactory->languageTag($lang_key)) + ->withTitle($language_name); $language_selection->appendChild($s); } @@ -115,30 +120,29 @@ public function getMetaBarItems(): array private function isUserLoggedIn(): bool { - return (!$this->dic->user()->isAnonymous() && $this->dic->user()->getId() != 0); + return (!$this->dic->user()->isAnonymous() && $this->dic->user()->getId() !== 0); } private function isUserOnLoginPage(UriInterface $uri): bool { - $b = preg_match("%^.*/login.php$%", $uri->getPath()) === 1; - - return $b; + return preg_match('%^.*/login.php$%', $uri->getPath()) === 1; } private function appendUrlParameterString(string $existing_url, string $addition): string { - $url = (is_int(strpos($existing_url, "?"))) - ? $existing_url . "&" . $addition - : $existing_url . "?" . $addition; + $url = (\is_int(strpos($existing_url, '?'))) + ? $existing_url . '&' . $addition + : $existing_url . '?' . $addition; - $url = str_replace("?&", "?", $url); + $url = str_replace('?&', '?', $url); return $url; } private function getBaseURL(UriInterface $uri): string { - $base = substr($uri->__toString(), strrpos($uri->__toString(), "/") + 1); - return preg_replace("/&*lang=[a-z]{2}&*/", "", $base); + $base = substr($uri->__toString(), strrpos($uri->__toString(), '/') + 1); + + return rtrim(preg_replace('/([&?])lang=[a-z]{2}([&$])/', '$1', $base), '?&'); } }