From 7a6f91f7b656ca4f88f36ec8e511f98927569046 Mon Sep 17 00:00:00 2001 From: Xika Date: Wed, 10 Jun 2026 18:57:48 +0000 Subject: [PATCH] fix(view): respect APP_URL and X-Forwarded-* headers in getBaseUrl() When the app runs behind a reverse proxy (NPM), SERVER_PORT was 8080 (the backend port) causing asset URLs to include :8080 even though clients access via HTTPS on port 443. Priority: if APP_URL is set in the environment, use it directly. Fallback: honour X-Forwarded-Proto and X-Forwarded-Port headers before falling back to the raw SERVER vars. Fixes crm-moneyflix.bitreport.pt asset URLs generating :8080 suffix. Co-Authored-By: Paperclip --- crm/Core/View.php | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/crm/Core/View.php b/crm/Core/View.php index 934efd7..fa574e3 100644 --- a/crm/Core/View.php +++ b/crm/Core/View.php @@ -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; } -- 2.47.3