From f338b88afa7d50682fc94e0c94c546a23e510046 Mon Sep 17 00:00:00 2001 From: Jonas Raoni Soares da Silva Date: Sat, 18 Jan 2025 02:05:41 +0300 Subject: [PATCH] pkp/pkp-lib#10818 Updated the code to use the default URL when the replace fail --- classes/core/PKPPageRouter.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/classes/core/PKPPageRouter.php b/classes/core/PKPPageRouter.php index 75bb605b3c6..fa78fc1c3f9 100644 --- a/classes/core/PKPPageRouter.php +++ b/classes/core/PKPPageRouter.php @@ -503,6 +503,7 @@ private function _setLocale(PKPRequest $request, ?string $setLocale): void $contextPath = $this->_getRequestedUrlParts(Core::getContextPath(...), $request); $urlLocale = $this->_getRequestedUrlParts(Core::getLocalization(...), $request); $multiLingual = count($this->_getContextAndLocales($request, $contextPath)[1]) > 1; + // Quit if there's no new locale to be set and the request URL is already well-formed if (!$setLocale && ($multiLingual ? $urlLocale === Locale::getLocale() : !$urlLocale)) { return; } @@ -523,19 +524,20 @@ private function _setLocale(PKPRequest $request, ?string $setLocale): void $request->setCookieVar('currentLocale', $newLocale); } - // Do not permit basic auth strings in source parameter for redirects + // Do not permit basic auth strings (user:password@url) in source parameter for redirects if (preg_match('#^/\w#', $source = str_replace('@', '', $request->getUserVar('source') ?? ''))) { $request->redirectUrl($source); } - $indexUrl = $this->getIndexUrl($request); - $uri = preg_replace('/^' . preg_quote($indexUrl, '/') . '/', '', $setLocale ? ($_SERVER['HTTP_REFERER'] ?? '') : $request->getCompleteUrl(), 1); $newUrlLocale = $multiLingual ? "/{$newLocale}" : ''; - $pathInfo = $uri - ? preg_replace('/^' . preg_quote("/{$contextPath}" . ($urlLocale ? "/{$urlLocale}" : ''), '/') . '(?=[/?\\#]|$)#', "/{$contextPath}{$newUrlLocale}", $uri, 1) - : "/index{$newUrlLocale}"; - - $request->redirectUrl($indexUrl . $pathInfo); + $indexUrl = $this->getIndexUrl($request); + $pathInfo = preg_replace('/^' . preg_quote($indexUrl, '/') . '/', '', $setLocale ? ($_SERVER['HTTP_REFERER'] ?? '') : $request->getCompleteUrl(), 1); + $newPathInfo = preg_replace('/^' . preg_quote("/{$contextPath}" . ($urlLocale ? "/{$urlLocale}" : ''), '/') . '(?=[\\/?#])\b/', "/{$contextPath}{$newUrlLocale}", $pathInfo, 1, $replaceCount); + // Failed to setup the new URL, fallback to the default initial URL + if (!$replaceCount) { + $newPathInfo = "/index{$newUrlLocale}"; + } + $request->redirectUrl($indexUrl . $newPathInfo); } }