diff --git a/application/Espo/Core/Portal/Utils/Url.php b/application/Espo/Core/Portal/Utils/Url.php index c10bbd0176..6a98e023c3 100644 --- a/application/Espo/Core/Portal/Utils/Url.php +++ b/application/Espo/Core/Portal/Utils/Url.php @@ -33,6 +33,12 @@ class Url { public static function detectPortalId() : ?string { + $portalId = $_SERVER['ESPO_PORTAL_ID'] ?? null; + + if ($portalId) { + return $portalId; + } + $url = $_SERVER['REQUEST_URI']; $portalId = explode('/', $url)[count(explode('/', $_SERVER['SCRIPT_NAME'])) - 1] ?? null; @@ -41,57 +47,61 @@ class Url $portalId = null; } - if (!isset($portalId)) { - $url = $_SERVER['REDIRECT_URL'] ?? $url; - - $portalId = explode('/', $url)[count(explode('/', $_SERVER['SCRIPT_NAME'])) - 1] ?? null; + if ($portalId) { + return $portalId; } + $url = $_SERVER['REDIRECT_URL'] ?? null; + + if (!$url) { + return null; + } + + $portalId = explode('/', $url)[count(explode('/', $_SERVER['SCRIPT_NAME'])) - 1] ?? null; + return $portalId; } - public static function detectUrl() : string + protected static function detectIsCustomUrl() : bool { - $url = $_SERVER['REQUEST_URI']; - - $portalId = explode('/', $url)[count(explode('/', $_SERVER['SCRIPT_NAME'])) - 1] ?? null; - - if (strpos($url, '=') !== false) { - $portalId = null; - } - - if (!isset($portalId)) { - return $_SERVER['REDIRECT_URL'] ?? $url; - } - - return $url; + return (bool) ($_SERVER['ESPO_PORTAL_IS_CUSTOM_URL'] ?? false); } - public static function normalizeUrl(string $url) : string + public static function detectIsInPortalDir() : bool { - $urlParts = explode('?', $url); + $isCustomUrl = self::detectIsCustomUrl(); - if (substr($urlParts[0], -1) === '/') { - return $url; + if ($isCustomUrl) { + return false; } - $url = $urlParts[0] . '/'; - - if (count($urlParts) > 1) { - $url .= '?' . $urlParts[1]; - } - - return $url; - } - - public static function detectIsInDir() : bool - { $url = $_SERVER['REQUEST_URI']; $a = explode('?', $url); $url = rtrim($a[0], '/'); - return strpos($url, '/') !== false; + return strpos($url, '/portal') !== false ; + } + + public static function detectIsInPortalWithId() : bool + { + if (!self::detectIsInPortalDir()) { + return false; + } + + $url = $_SERVER['REQUEST_URI']; + + $a = explode('?', $url); + + $url = rtrim($a[0], '/'); + + $folders = explode('/', $url); + + if (count($folders) > 1 && $folders[count($folders) - 2] === 'portal') { + return true; + } + + return false; } } diff --git a/application/Espo/EntryPoints/Portal.php b/application/Espo/EntryPoints/Portal.php index 75f6cc6b4f..bb244104c6 100644 --- a/application/Espo/EntryPoints/Portal.php +++ b/application/Espo/EntryPoints/Portal.php @@ -41,6 +41,7 @@ use Espo\Core\{ Utils\Config, Portal\Application as PortalApplication, Portal\ApplicationRunners\Client, + Portal\Utils\Url, Api\Request, Api\Response, }; @@ -64,26 +65,14 @@ class Portal implements EntryPoint { $data = $data ?? (object) []; - $id = $request->get('id') ?? $data->id ?? null; + $id = $data->id ?? Url::detectPortalId(); if (!$id) { - $url = $_SERVER['REQUEST_URI']; + $id = $this->config->get('defaultPortalId'); + } - $id = explode('/', $url)[count(explode('/', $_SERVER['SCRIPT_NAME'])) - 1]; - - if (!isset($id)) { - $url = $_SERVER['REDIRECT_URL']; - - $id = explode('/', $url)[count(explode('/', $_SERVER['SCRIPT_NAME'])) - 1]; - } - - if (!$id) { - $id = $this->config->get('defaultPortalId'); - } - - if (!$id) { - throw new NotFound(); - } + if (!$id) { + throw new NotFound("Portal ID not detected."); } $application = new PortalApplication($id); diff --git a/portal/index.php b/portal/index.php index 21bd113156..76bac869d4 100644 --- a/portal/index.php +++ b/portal/index.php @@ -41,19 +41,10 @@ if (!$app->isInstalled()) { exit; } -$url = Url::detectUrl(); -$normalizedUrl = Url::normalizeUrl($url); - -if ($url !== $normalizedUrl) { - header("Location: " . $normalizedUrl); - - exit; -} - -if (Url::detectIsInDir()) { +if (Url::detectIsInPortalDir()) { $basePath = '../'; - if (Url::detectPortalId()) { + if (Url::detectIsInPortalWithId()) { $basePath = '../../'; }