Merge pull request 'fix(view): respect APP_URL and X-Forwarded-* in getBaseUrl() — BIT-24927' (#2) from fix/bit-24925-crm-admin-pathmanager into master

fix(view): respect APP_URL and X-Forwarded-* in getBaseUrl() — BIT-24927

Co-Authored-By: Paperclip <[email protected]>
This commit was merged in pull request #2.
This commit is contained in:
sparkandPaperclip committed 2026-06-10 19:59:30 +01:00
commit 872ad2d1c6
1 file changed
+19 -6
+19 -6
View File
@@ -278,18 +278,31 @@ class View
*/
public static function getBaseUrl(): string
{
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
// Use explicit APP_URL when set (required for reverse-proxy deployments)
$appUrl = $_ENV['APP_URL'] ?? '';
if ($appUrl && filter_var($appUrl, FILTER_VALIDATE_URL)) {
return rtrim($appUrl, '/');
}
// Detect scheme: check X-Forwarded-Proto first (set by reverse proxy)
$forwardedProto = $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '';
if ($forwardedProto === 'https') {
$protocol = 'https';
} else {
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? 'https' : 'http';
}
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
// If HTTP_HOST already includes the port, use it as is
if (strpos($host, ':') !== false) {
return $protocol . '://' . $host;
}
// Otherwise, add port if needed
$port = $_SERVER['SERVER_PORT'] ?? '';
// Use X-Forwarded-Port if available, otherwise fall back to SERVER_PORT
$port = $_SERVER['HTTP_X_FORWARDED_PORT'] ?? $_SERVER['SERVER_PORT'] ?? '';
$port = ($port === '80' || $port === '443') ? '' : ':' . $port;
return $protocol . '://' . $host . $port;
}