diff --git a/core/docs/config.inc.tpl b/core/docs/config.inc.tpl index b3ae24f2e7d..a2e54ad8fcc 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); - } - $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 772e870f5ff..a461c4305f0 100644 --- a/setup/includes/config/modconfigreader.class.php +++ b/setup/includes/config/modconfigreader.class.php @@ -70,12 +70,9 @@ public function loadDefaults(array $config = array()) { 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']); - } - $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/index.php b/setup/index.php index c76e3b81a96..05030317c86 100644 --- a/setup/index.php +++ b/setup/index.php @@ -62,12 +62,11 @@ } if (!$isCommandLine) { $https = isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : false; - $installBaseUrl = (!$https || strtolower($https) != 'on') ? 'http://' : 'https://'; - $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 || strtolower($https) != 'on') ? 'http://' : 'https://'; + $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);