From 448927e02085e9f52ee5ecb9657682c9d291f15c Mon Sep 17 00:00:00 2001 From: dimasites Date: Mon, 25 Mar 2024 18:51:06 +0300 Subject: [PATCH] Fix working with non-standard ports (#16541) Fixed In default config, setup process and setup wizard. This is port from 2.x branch, all context here: https://github.com/modxcms/revolution/pull/16455 --- core/docs/config.inc.tpl | 12 +++++------- setup/includes/config/modconfigreader.class.php | 9 +++------ setup/provisioner/bootstrap.php | 11 +++++------ 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/core/docs/config.inc.tpl b/core/docs/config.inc.tpl index 70626d6e5a4..2bc4a5d9d58 100644 --- a/core/docs/config.inc.tpl +++ b/core/docs/config.inc.tpl @@ -49,10 +49,10 @@ if (!defined('MODX_BASE_PATH')) { if(defined('PHP_SAPI') && (PHP_SAPI == "cli" || PHP_SAPI == "embed")) { $isSecureRequest = false; } else { - $isSecureRequest = ((isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') || $_SERVER['SERVER_PORT'] == $https_port); + $isSecureRequest = ((isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') || parse_url('http://' . $_SERVER['HTTP_HOST'], PHP_URL_PORT) == $https_port); } if (!defined('MODX_URL_SCHEME')) { - $url_scheme= $isSecureRequest ? 'https://' : 'http://'; + $url_scheme = $isSecureRequest ? 'https://' : 'http://'; define('MODX_URL_SCHEME', $url_scheme); } if (!defined('MODX_HTTP_HOST')) { @@ -60,11 +60,9 @@ if (!defined('MODX_HTTP_HOST')) { $http_host='{http_host}'; define('MODX_HTTP_HOST', $http_host); } else { - $http_host= array_key_exists('HTTP_HOST', $_SERVER) ? htmlspecialchars($_SERVER['HTTP_HOST'], ENT_QUOTES) : '{http_host}'; - if ($_SERVER['SERVER_PORT'] != 80) { - $http_host = str_replace(':' . $_SERVER['SERVER_PORT'], '', $http_host); // remove port from HTTP_HOST - } - $http_host .= in_array($_SERVER['SERVER_PORT'], [80, 443]) ? '' : ':' . $_SERVER['SERVER_PORT']; + $http_host = array_key_exists('HTTP_HOST', $_SERVER) ? parse_url($url_scheme . $_SERVER['HTTP_HOST'], PHP_URL_HOST) : '{http_host}'; + $http_port = parse_url($url_scheme . $_SERVER['HTTP_HOST'], PHP_URL_PORT); + $http_host .= in_array($http_port, [null, 80, 443]) ? '' : ':' . $http_port; define('MODX_HTTP_HOST', $http_host); } } diff --git a/setup/includes/config/modconfigreader.class.php b/setup/includes/config/modconfigreader.class.php index 7b06c11897c..19155b5a6da 100644 --- a/setup/includes/config/modconfigreader.class.php +++ b/setup/includes/config/modconfigreader.class.php @@ -70,12 +70,9 @@ public function loadDefaults(array $config = []) { public function getHttpHost() { if (php_sapi_name() != 'cli') { $this->config['https_port'] = isset ($_POST['httpsport']) ? $_POST['httpsport'] : '443'; - $isSecureRequest = ((isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') || $_SERVER['SERVER_PORT'] == $this->config['https_port']); - $this->config['http_host']= $_SERVER['HTTP_HOST']; - if ($_SERVER['SERVER_PORT'] != 80) { - $this->config['http_host']= str_replace(':' . $_SERVER['SERVER_PORT'], '', $this->config['http_host']); /* remove port from HTTP_HOST */ - } - $this->config['http_host'] .= in_array($_SERVER['SERVER_PORT'], [80, 443]) ? '' : ':' . $_SERVER['SERVER_PORT']; + $this->config['http_host'] = parse_url('http://' . $_SERVER['HTTP_HOST'], PHP_URL_HOST); + $this->config['http_port'] = parse_url('http://' . $_SERVER['HTTP_HOST'], PHP_URL_PORT); + $this->config['http_host'] .= in_array($this->config['http_port'], [null , 80, 443]) ? '' : ':' . $this->config['http_port']; } else { $this->config['http_host'] = 'localhost'; $this->config['https_port'] = 443; diff --git a/setup/provisioner/bootstrap.php b/setup/provisioner/bootstrap.php index a9485a559bf..cc8f22c92f5 100644 --- a/setup/provisioner/bootstrap.php +++ b/setup/provisioner/bootstrap.php @@ -17,12 +17,11 @@ if (!MODX_SETUP_INTERFACE_IS_CLI) { $https = $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? $_SERVER['HTTPS'] ?? 'off'; $https = in_array(strtolower((string)$https), ['https', 'on', 'ssl', '1'], true); - $installBaseUrl = $https ? 'https://' : 'http://'; - $installBaseUrl .= $_SERVER['HTTP_HOST']; - if (isset($_SERVER['SERVER_PORT']) && (string)$_SERVER['SERVER_PORT'] !== '' && $_SERVER['SERVER_PORT'] !== 80) { - $installBaseUrl = str_replace(':' . $_SERVER['SERVER_PORT'], '', $installBaseUrl); - } - $installBaseUrl .= in_array($_SERVER['SERVER_PORT'], [80, 443]) ? '' : ':' . $_SERVER['SERVER_PORT']; + $url_scheme = $https ? 'https://' : 'http://'; + $installBaseUrl = $url_scheme; + $installBaseUrl .= parse_url($url_scheme . $_SERVER['HTTP_HOST'], PHP_URL_HOST); + $url_port = parse_url($url_scheme . $_SERVER['HTTP_HOST'], PHP_URL_PORT); + $installBaseUrl .= in_array($url_port, [null , 80, 443]) ? '' : ':' . $url_port; $installBaseUrl .= $_SERVER['SCRIPT_NAME']; $installBaseUrl = htmlspecialchars($installBaseUrl, ENT_QUOTES, 'utf-8'); define('MODX_SETUP_URL', $installBaseUrl);