summaryrefslogtreecommitdiffstats
path: root/lib/SimpleSAML/Utils/HTTP.php
diff options
context:
space:
mode:
authorJaime Perez Crespo <jaime.perez@uninett.no>2015-04-21 10:52:17 +0200
committerJaime Perez Crespo <jaime.perez@uninett.no>2015-04-21 10:52:17 +0200
commit3703d50a76ed5ec1aea85ab4a4b10ac1ef10c9d2 (patch)
tree2004432abc41475c150f220067216bc7c3672804 /lib/SimpleSAML/Utils/HTTP.php
parent720f08bbeb4bb94e61bd8c651f8f3a39f241d3fd (diff)
downloadsimplesamlphp-3703d50a76ed5ec1aea85ab4a4b10ac1ef10c9d2.zip
simplesamlphp-3703d50a76ed5ec1aea85ab4a4b10ac1ef10c9d2.tar.gz
simplesamlphp-3703d50a76ed5ec1aea85ab4a4b10ac1ef10c9d2.tar.bz2
Move SimpleSAML_Utilities::getServerHosst(), getServerHTTPS() and getServerPort() to SimpleSAML\Utils\HTTP.
Diffstat (limited to 'lib/SimpleSAML/Utils/HTTP.php')
-rw-r--r--lib/SimpleSAML/Utils/HTTP.php90
1 files changed, 90 insertions, 0 deletions
diff --git a/lib/SimpleSAML/Utils/HTTP.php b/lib/SimpleSAML/Utils/HTTP.php
new file mode 100644
index 0000000..01a6209
--- /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