From: Roland Haeder Date: Mon, 28 Mar 2016 12:55:01 +0000 (+0200) Subject: Introduced detectServerProtocol() to fully support HTTPS and more use of isFullQualif... X-Git-Url: https://git.mxchange.org/?p=mailer.git;a=commitdiff_plain;h=eac9440acf7d8f00feb31a27dea7585c248ca974 Introduced detectServerProtocol() to fully support HTTPS and more use of isFullQualifiedUrl() Signed-off-by: Roland Häder --- diff --git a/inc/config-global.php b/inc/config-global.php index 9880b8f9a8..e34c4cfb9d 100644 --- a/inc/config-global.php +++ b/inc/config-global.php @@ -66,7 +66,7 @@ foreach (array('config', 'wrapper', 'template', 'module', 'inc', 'http') as $inc include($path . 'inc/functions.php'); // Auto-detection of URL -$URL = 'http://' . detectServerName() . str_replace(chr(92), '/', dirname($_SERVER['PHP_SELF'])); +$URL = detectServerProtocol() . '://' . detectServerName() . str_replace(chr(92), '/', dirname($_SERVER['PHP_SELF'])); while (substr($URL, -1, 1) == '/') { $URL = substr($URL, 0, -1); } // Initialize the configuration diff --git a/inc/functions.php b/inc/functions.php index 4469fb25e0..a240205c28 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -2083,7 +2083,7 @@ function encodeUrl ($url, $outputMode = '0') { } // END - if // Add {?URL?} ? - if ((substr($url, 0, strlen(getUrl())) != getUrl()) && (substr($url, 0, 7) != '{?URL?}') && (substr($url, 0, 7) != 'http://') && (substr($url, 0, 8) != 'https://')) { + if ((substr($url, 0, strlen(getUrl())) != getUrl()) && (substr($url, 0, 7) != '{?URL?}') && (!isFullQualifiedUrl($url))) { // Add it $url = '{?URL?}/' . $url; } // END - if diff --git a/inc/http-functions.php b/inc/http-functions.php index 042e4b72e8..654c05e290 100644 --- a/inc/http-functions.php +++ b/inc/http-functions.php @@ -644,11 +644,13 @@ function extractHostnameFromUrl (&$script) { // Is this URL valid? if (substr($script, 0, 7) == 'http://') { // Use the hostname from script URL as new hostname - $url = substr($script, 7); - $extract = explode('/', $url); + $extract = explode('/', substr($script, 7)); $url = $extract[0]; - // Done extracting the URL :) - } // END - if + } elseif (substr($script, 0, 8) == 'https://') { + // Use the hostname from script URL as new hostname + $extract = explode('/', substr($script, 8)); + $url = $extract[0]; + } // Extract host name $host = str_replace(array('http://', 'https://'), array('', ''), $url); diff --git a/inc/install-functions.php b/inc/install-functions.php index 7c205e2481..a0ac6850a6 100644 --- a/inc/install-functions.php +++ b/inc/install-functions.php @@ -474,7 +474,7 @@ function isInstallerBaseUrlValid ($value) { ($value == getUrl()) || ( // Starts with http:// or https:// ? - ((substr($value, 0, 7) == 'http://') || (substr($value, 0, 8) == 'https://')) + (isFullQualifiedUrl($value)) && // Has no trailing slash? (substr($value, -1, 1) != '/') diff --git a/inc/wrapper-functions.php b/inc/wrapper-functions.php index 4520489da1..b3a6329262 100644 --- a/inc/wrapper-functions.php +++ b/inc/wrapper-functions.php @@ -293,6 +293,24 @@ function detectServerName () { return (getenv('SERVER_NAME')); } +// Detects server protocol (http/s) +function detectServerProtocol () { + // Is cache there? + if (!isset($GLOBALS[__FUNCTION__])) { + // Default is HTTP + $GLOBALS[__FUNCTION__] = 'http'; + + // Are some specific fields set? + if (((isset($_SERVER['HTTPS'])) && (strtolower($_SERVER['HTTPS']) == 'on')) || ((isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) && (strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https'))) { + // Switch to HTTPS + $GLOBALS[__FUNCTION__] = 'https'; + } // END - if + } // END - if + + // Return cached value + return $GLOBALS[__FUNCTION__]; +} + // Removes any existing www. from SERVER_NAME. This is very silly but enough // for our purpose here. function detectDomainName () {