From 3703d50a76ed5ec1aea85ab4a4b10ac1ef10c9d2 Mon Sep 17 00:00:00 2001 From: Jaime Perez Crespo <jaime.perez@uninett.no> Date: Tue, 21 Apr 2015 10:52:17 +0200 Subject: [PATCH] Move SimpleSAML_Utilities::getServerHosst(), getServerHTTPS() and getServerPort() to SimpleSAML\Utils\HTTP. --- lib/SimpleSAML/Utils/HTTP.php | 90 +++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 lib/SimpleSAML/Utils/HTTP.php diff --git a/lib/SimpleSAML/Utils/HTTP.php b/lib/SimpleSAML/Utils/HTTP.php new file mode 100644 index 000000000..01a6209fd --- /dev/null +++ b/lib/SimpleSAML/Utils/HTTP.php @@ -0,0 +1,90 @@ +<?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 -- GitLab