Skip to content

Commit

Permalink
Fix working with non-standard ports (#16541)
Browse files Browse the repository at this point in the history
Fixed In default config, setup process and setup wizard.

This is port from 2.x branch, all context here:
#16455
  • Loading branch information
dimasites authored Mar 25, 2024
1 parent 3bf35e8 commit 90e7a46
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
12 changes: 5 additions & 7 deletions core/docs/config.inc.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,21 @@ if (!defined('MODX_PROCESSORS_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')) {
if(defined('PHP_SAPI') && (PHP_SAPI == "cli" || PHP_SAPI == "embed")) {
$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);
}
}
Expand Down
9 changes: 3 additions & 6 deletions setup/includes/config/modconfigreader.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 5 additions & 6 deletions setup/provisioner/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 90e7a46

Please sign in to comment.