portal fixes

This commit is contained in:
Yuri Kuznetsov
2020-09-29 10:05:54 +03:00
parent d3466e201d
commit 061b237032
3 changed files with 52 additions and 62 deletions
+44 -34
View File
@@ -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;
}
}
+6 -17
View File
@@ -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);
+2 -11
View File
@@ -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 = '../../';
}