@@ -14,6 +14,22 @@ class OneLogin_Saml2_Utils
1414 */
1515 private static $ _proxyVars = false ;
1616
17+
18+ /**
19+ * @var string
20+ */
21+ private static $ _host ;
22+
23+ /**
24+ * @var string
25+ */
26+ private static $ _protocol ;
27+
28+ /**
29+ * @var int
30+ */
31+ private static $ _port ;
32+
1733 /**
1834 * Translates any string. Accepts args
1935 *
@@ -321,12 +337,24 @@ public static function getSelfURLhost()
321337 return $ protocol .":// " . $ currenthost . $ port ;
322338 }
323339
340+ /**
341+ * @param $host string The host to use when constructing URLs
342+ */
343+ public static function setSelfHost ($ host )
344+ {
345+ self ::$ _host = $ host ;
346+ }
347+
324348 /**
325349 * @return string The raw host name
326350 */
327351 protected static function getRawHost ()
328352 {
329- if (array_key_exists ('HTTP_HOST ' , $ _SERVER )) {
353+ if (self ::$ _host ) {
354+ $ currentHost = self ::$ _host ;
355+ } elseif (self ::getProxyVars () && array_key_exists ('HTTP_X_FORWARDED_HOST ' , $ _SERVER )) {
356+ $ currentHost = $ _SERVER ['HTTP_X_FORWARDED_HOST ' ];
357+ } elseif (array_key_exists ('HTTP_HOST ' , $ _SERVER )) {
330358 $ currentHost = $ _SERVER ['HTTP_HOST ' ];
331359 } elseif (array_key_exists ('SERVER_NAME ' , $ _SERVER )) {
332360 $ currentHost = $ _SERVER ['SERVER_NAME ' ];
@@ -340,6 +368,40 @@ protected static function getRawHost()
340368 return $ currentHost ;
341369 }
342370
371+ /**
372+ * @param $port int The port number to use when constructing URLs
373+ */
374+ public static function setSelfPort ($ port )
375+ {
376+ self ::$ _port = $ port ;
377+ }
378+
379+ /**
380+ * @param $protocol string The protocol to identify as using, usually http or https
381+ */
382+ public static function setSelfProtocol ($ protocol )
383+ {
384+ self ::$ _protocol = $ protocol ;
385+ }
386+
387+ /**
388+ * @return string http|https
389+ */
390+ public static function getSelfProtocol ()
391+ {
392+ $ protocol = 'http ' ;
393+ if (self ::$ _protocol ) {
394+ $ protocol = self ::$ _protocol ;
395+ } elseif (self ::getSelfPort () == 443 ) {
396+ $ protocol = 'https ' ;
397+ } elseif (self ::getProxyVars () && isset ($ _SERVER ['HTTP_X_FORWARDED_PROTO ' ])) {
398+ $ protocol = $ _SERVER ['HTTP_X_FORWARDED_PROTO ' ];
399+ } elseif (!empty ($ _SERVER ['HTTPS ' ]) && $ _SERVER ['HTTPS ' ] !== 'off ' ) {
400+ $ protocol = 'https ' ;
401+ }
402+ return $ protocol ;
403+ }
404+
343405 /**
344406 * Returns the current host.
345407 *
@@ -363,7 +425,9 @@ public static function getSelfHost()
363425 public static function getSelfPort ()
364426 {
365427 $ portnumber = null ;
366- if (self ::getProxyVars () && isset ($ _SERVER ["HTTP_X_FORWARDED_PORT " ])) {
428+ if (self ::$ _port ) {
429+ $ portnumber = self ::$ _port ;
430+ } else if (self ::getProxyVars () && isset ($ _SERVER ["HTTP_X_FORWARDED_PORT " ])) {
367431 $ portnumber = $ _SERVER ["HTTP_X_FORWARDED_PORT " ];
368432 } else if (isset ($ _SERVER ["SERVER_PORT " ])) {
369433 $ portnumber = $ _SERVER ["SERVER_PORT " ];
@@ -388,10 +452,7 @@ public static function getSelfPort()
388452 */
389453 public static function isHTTPS ()
390454 {
391- $ isHttps = (!empty ($ _SERVER ['HTTPS ' ]) && $ _SERVER ['HTTPS ' ] !== 'off ' )
392- || (self ::getSelfPort () == 443 )
393- || (self ::getProxyVars () && isset ($ _SERVER ['HTTP_X_FORWARDED_PROTO ' ]) && $ _SERVER ['HTTP_X_FORWARDED_PROTO ' ] == 'https ' );
394- return $ isHttps ;
455+ return self ::getSelfProtocol () == 'https ' ;
395456 }
396457
397458 /**
0 commit comments