Skip to content
Snippets Groups Projects
Commit 3703d50a authored by Jaime Perez Crespo's avatar Jaime Perez Crespo
Browse files

Move SimpleSAML_Utilities::getServerHosst(), getServerHTTPS() and...

Move SimpleSAML_Utilities::getServerHosst(), getServerHTTPS() and getServerPort() to SimpleSAML\Utils\HTTP.
parent 720f08bb
No related branches found
No related tags found
No related merge requests found
<?php
namespace SimpleSAML\Utils;
/**
* HTTP-related utility methods.
*
* @package SimpleSAMLphp
*/
class HTTP
{
/**
* Retrieve Host value from $_SERVER environment variables.
*
* @return string The current host name, including the port if needed. It will use localhost when unable to
* determine the current host.
*
* @author Olav Morken, UNINETT AS <olav.morken@uninett.no>
*/
private static function getServerHost()
{
if (array_key_exists('HTTP_HOST', $_SERVER)) {
$current = $_SERVER['HTTP_HOST'];
} elseif (array_key_exists('SERVER_NAME', $_SERVER)) {
$current = $_SERVER['SERVER_NAME'];
} else {
// almost certainly not what you want, but...
$current = 'localhost';
}
if (strstr($current, ":")) {
$decomposed = explode(":", $current);
$port = array_pop($decomposed);
if (!is_numeric($port)) {
array_push($decomposed, $port);
}
$current = implode($decomposed, ":");
}
return $current;
}
/**
* Retrieve HTTPS status from $_SERVER environment variables.
*
* @return boolean True if the request was performed through HTTPS, false otherwise.
*
* @author Olav Morken, UNINETT AS <olav.morken@uninett.no>
*/
private static function getServerHTTPS()
{
if (!array_key_exists('HTTPS', $_SERVER)) {
// not an https-request
return false;
}
if ($_SERVER['HTTPS'] === 'off') {
// IIS with HTTPS off
return false;
}
// otherwise, HTTPS will be a non-empty string
return $_SERVER['HTTPS'] !== '';
}
/**
* Retrieve the port number from $_SERVER environment variables.
*
* @return string The port number prepended by a colon, if it is different than the default port for the protocol
* (80 for HTTP, 443 for HTTPS), or an empty string otherwise.
*
* @author Olav Morken, UNINETT AS <olav.morken@uninett.no>
*/
private static function getServerPort()
{
$port = (isset($_SERVER['SERVER_PORT'])) ? $_SERVER['SERVER_PORT'] : '80';
if (self::getServerHTTPS()) {
if ($port !== '443') {
$port = ':'.$port;
}
} else {
if ($port !== '80') {
$port = ':'.$port;
}
}
return $port;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment