Skip to content

Commit 93b35c9

Browse files
committed
NEW Allow overriding of host, port and protocol for URL building
- Added ability to set custom host/port/protocol - Updated test coverage to include testing HTTP_X_FORWARDED_PORT - Split port detection into its own method
1 parent 48f98cf commit 93b35c9

File tree

2 files changed

+99
-7
lines changed

2 files changed

+99
-7
lines changed

lib/Saml2/Utils.php

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

tests/src/OneLogin/Saml2/UtilsTest.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class OneLogin_Saml2_UtilsTest extends PHPUnit_Framework_TestCase
1616
/*
1717
public function testT()
1818
{
19-
setlocale(LC_MESSAGES, 'en_US');
19+
setlocale(LC_MESSAGES, 'en_US');
2020
2121
$msg = 'test';
2222
$translatedMsg = OneLogin_Saml2_Utils::t($msg);
@@ -277,6 +277,18 @@ public function testRedirect()
277277
$this->assertEquals("http://$hostname/example?alphavalue=a&numvaluelist[]=", $targetUrl8);
278278
}
279279

280+
/**
281+
* @covers OneLogin_Saml2_Utils::setSelfHost
282+
*/
283+
public function testSetselfhost()
284+
{
285+
$_SERVER['HTTP_HOST'] = 'example.org';
286+
$this->assertEquals('example.org', OneLogin_Saml2_Utils::getSelfHost());
287+
288+
OneLogin_Saml2_Utils::setSelfHost('example.com');
289+
$this->assertEquals('example.com', OneLogin_Saml2_Utils::getSelfHost());
290+
}
291+
280292
/**
281293
* @covers OneLogin_Saml2_Utils::setProxyVars()
282294
* @covers OneLogin_Saml2_Utils::getProxyVars()
@@ -323,6 +335,12 @@ public function testGetselfhost()
323335

324336
$_SERVER['HTTP_HOST'] = 'example.org:ok';
325337
$this->assertEquals('example.org', OneLogin_Saml2_Utils::getSelfHost());
338+
339+
$_SERVER['HTTP_X_FORWARDED_HOST'] = 'example.net';
340+
$this->assertNotEquals('example.net', OneLogin_Saml2_Utils::getSelfHost());
341+
342+
OneLogin_Saml2_Utils::setProxyVars(true);
343+
$this->assertEquals('example.net', OneLogin_Saml2_Utils::getSelfHost());
326344
}
327345

328346
/**
@@ -356,8 +374,21 @@ public function testGetselfPort()
356374

357375
OneLogin_Saml2_Utils::setProxyVars(true);
358376
$this->assertEquals(443, OneLogin_Saml2_Utils::getSelfPort());
377+
378+
OneLogin_Saml2_Utils::setSelfPort(8080);
379+
$this->assertEquals(8080, OneLogin_Saml2_Utils::getSelfPort());
359380
}
360381

382+
/**
383+
* @covers OneLogin_Saml2_Utils::setSelfProtocol()
384+
*/
385+
public function testSetselfprotocol()
386+
{
387+
$this->assertFalse(OneLogin_Saml2_Utils::isHTTPS());
388+
389+
OneLogin_Saml2_Utils::setSelfProtocol('https');
390+
$this->assertTrue(OneLogin_Saml2_Utils::isHTTPS());
391+
}
361392

362393
/**
363394
* Tests the getSelfURLhost method of the OneLogin_Saml2_Utils

0 commit comments

Comments
 (0)