Skip to content

Commit af53ee6

Browse files
committed
FIX getSelfHost no longer returns a port number
1 parent 7a87f05 commit af53ee6

File tree

2 files changed

+70
-19
lines changed

2 files changed

+70
-19
lines changed

lib/Saml2/Utils.php

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

tests/src/OneLogin/Saml2/UtilsTest.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
/**
44
* Unit tests for Utils class
5+
*
6+
* @backupStaticAttributes enabled
57
*/
68
class OneLogin_Saml2_UtilsTest extends PHPUnit_Framework_TestCase
79
{
@@ -14,7 +16,7 @@ class OneLogin_Saml2_UtilsTest extends PHPUnit_Framework_TestCase
1416
/*
1517
public function testT()
1618
{
17-
setlocale(LC_MESSAGES, 'en_US');
19+
setlocale(LC_MESSAGES, 'en_US');
1820
1921
$msg = 'test';
2022
$translatedMsg = OneLogin_Saml2_Utils::t($msg);
@@ -300,7 +302,7 @@ public function testGetselfhost()
300302
$this->assertEquals('example.org', OneLogin_Saml2_Utils::getSelfHost());
301303

302304
$_SERVER['HTTP_HOST'] = 'example.org:ok';
303-
$this->assertEquals('example.org:ok', OneLogin_Saml2_Utils::getSelfHost());
305+
$this->assertEquals('example.org', OneLogin_Saml2_Utils::getSelfHost());
304306
}
305307

306308
/**
@@ -313,6 +315,29 @@ public function testisHTTPS()
313315
$this->assertFalse(OneLogin_Saml2_Utils::isHTTPS());
314316
}
315317

318+
/**
319+
* @covers OneLogin_Saml2_Utils::getSelfPort()
320+
*/
321+
public function testGetselfPort()
322+
{
323+
$this->assertNull(OneLogin_Saml2_Utils::getSelfPort());
324+
325+
$_SERVER['HTTP_HOST'] = 'example.org:ok';
326+
$this->assertNull(OneLogin_Saml2_Utils::getSelfPort());
327+
328+
$_SERVER['HTTP_HOST'] = 'example.org:8080';
329+
$this->assertEquals(8080, OneLogin_Saml2_Utils::getSelfPort());
330+
331+
$_SERVER["SERVER_PORT"] = 80;
332+
$this->assertEquals(80, OneLogin_Saml2_Utils::getSelfPort());
333+
334+
$_SERVER["HTTP_X_FORWARDED_PORT"] = 443;
335+
$this->assertEquals(80, OneLogin_Saml2_Utils::getSelfPort());
336+
337+
OneLogin_Saml2_Utils::setProxyVars(true);
338+
$this->assertEquals(443, OneLogin_Saml2_Utils::getSelfPort());
339+
}
340+
316341

317342
/**
318343
* Tests the getSelfURLhost method of the OneLogin_Saml2_Utils

0 commit comments

Comments
 (0)