@@ -312,11 +312,7 @@ public static function getSelfURLhost()
312312 $ protocol = 'http ' ;
313313 }
314314
315- if (self ::getProxyVars () && isset ($ _SERVER ["HTTP_X_FORWARDED_PORT " ])) {
316- $ portnumber = $ _SERVER ["HTTP_X_FORWARDED_PORT " ];
317- } else if (isset ($ _SERVER ["SERVER_PORT " ])) {
318- $ portnumber = $ _SERVER ["SERVER_PORT " ];
319- }
315+ $ portnumber = self ::getSelfPort ();
320316
321317 if (isset ($ portnumber ) && ($ portnumber != '80 ' ) && ($ portnumber != '443 ' )) {
322318 $ port = ': ' . $ portnumber ;
@@ -326,13 +322,10 @@ public static function getSelfURLhost()
326322 }
327323
328324 /**
329- * Returns the current host.
330- *
331- * @return string $currentHost The current host
325+ * @return string The raw host name
332326 */
333- public static function getSelfHost ()
327+ protected static function getRawHost ()
334328 {
335-
336329 if (array_key_exists ('HTTP_HOST ' , $ _SERVER )) {
337330 $ currentHost = $ _SERVER ['HTTP_HOST ' ];
338331 } elseif (array_key_exists ('SERVER_NAME ' , $ _SERVER )) {
@@ -344,17 +337,50 @@ public static function getSelfHost()
344337 $ currentHost = php_uname ("n " );
345338 }
346339 }
340+ return $ currentHost ;
341+ }
347342
348- if (strstr ($ currentHost , ": " )) {
349- $ currentHostData = explode (": " , $ currentHost );
350- $ possiblePort = array_pop ($ currentHostData );
351- if (is_numeric ($ possiblePort )) {
352- $ currentHost = implode (': ' , $ currentHostData );
353- }
343+ /**
344+ * Returns the current host.
345+ *
346+ * @return string $currentHost The current host
347+ */
348+ public static function getSelfHost ()
349+ {
350+ $ currentHost = self ::getRawHost ();
351+
352+ // strip the port
353+ if (false !== strpos ($ currentHost , ': ' )) {
354+ list ($ currentHost , $ port ) = explode (': ' , $ currentHost , 2 );
354355 }
356+
355357 return $ currentHost ;
356358 }
357359
360+ /**
361+ * @return null|string The port number used for the request
362+ */
363+ public static function getSelfPort ()
364+ {
365+ $ portnumber = null ;
366+ if (self ::getProxyVars () && isset ($ _SERVER ["HTTP_X_FORWARDED_PORT " ])) {
367+ $ portnumber = $ _SERVER ["HTTP_X_FORWARDED_PORT " ];
368+ } else if (isset ($ _SERVER ["SERVER_PORT " ])) {
369+ $ portnumber = $ _SERVER ["SERVER_PORT " ];
370+ } else {
371+ $ currentHost = self ::getRawHost ();
372+
373+ // strip the port
374+ if (false !== strpos ($ currentHost , ': ' )) {
375+ list ($ currentHost , $ port ) = explode (': ' , $ currentHost , 2 );
376+ if (is_numeric ($ port )) {
377+ $ portnumber = $ port ;
378+ }
379+ }
380+ }
381+ return $ portnumber ;
382+ }
383+
358384 /**
359385 * Checks if https or http.
360386 *
@@ -363,7 +389,7 @@ public static function getSelfHost()
363389 public static function isHTTPS ()
364390 {
365391 $ isHttps = (!empty ($ _SERVER ['HTTPS ' ]) && $ _SERVER ['HTTPS ' ] !== 'off ' )
366- || (isset ( $ _SERVER [ ' SERVER_PORT ' ]) && $ _SERVER [ ' SERVER_PORT ' ] == 443 )
392+ || (self :: getSelfPort () == 443 )
367393 || (self ::getProxyVars () && isset ($ _SERVER ['HTTP_X_FORWARDED_PROTO ' ]) && $ _SERVER ['HTTP_X_FORWARDED_PROTO ' ] == 'https ' );
368394 return $ isHttps ;
369395 }
0 commit comments