New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding of host, port, protocol nsdr url path for URL building #175

Merged
merged 6 commits into from Nov 15, 2016
View
@@ -16,6 +16,22 @@ class OneLogin_Saml2_Utils
*/
private static $_proxyVars = false;
/**
* @var string
*/
private static $_host;
/**
* @var string
*/
private static $_protocol;
/**
* @var int
*/
private static $_port;
/**
* Translates any string. Accepts args
*
@@ -323,12 +339,24 @@ public static function getSelfURLhost()
return $protocol."://" . $currenthost . $port;
}
/**
* @param $host string The host to use when constructing URLs
*/
public static function setSelfHost($host)
{
self::$_host = $host;
}
/**
* @return string The raw host name
*/
protected static function getRawHost()
{
if (array_key_exists('HTTP_HOST', $_SERVER)) {
if (self::$_host) {
$currentHost = self::$_host;
} elseif (self::getProxyVars() && array_key_exists('HTTP_X_FORWARDED_HOST', $_SERVER)) {
$currentHost = $_SERVER['HTTP_X_FORWARDED_HOST'];
} elseif (array_key_exists('HTTP_HOST', $_SERVER)) {
$currentHost = $_SERVER['HTTP_HOST'];
} elseif (array_key_exists('SERVER_NAME', $_SERVER)) {
$currentHost = $_SERVER['SERVER_NAME'];
@@ -342,6 +370,40 @@ protected static function getRawHost()
return $currentHost;
}
/**
* @param $port int The port number to use when constructing URLs
*/
public static function setSelfPort($port)
{
self::$_port = $port;
}
/**
* @param $protocol string The protocol to identify as using, usually http or https
*/
public static function setSelfProtocol($protocol)
{
self::$_protocol = $protocol;
}
/**
* @return string http|https
*/
public static function getSelfProtocol()
{
$protocol = 'http';
if (self::$_protocol) {
$protocol = self::$_protocol;
} elseif (self::getSelfPort() == 443) {
$protocol = 'https';
} elseif (self::getProxyVars() && isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
$protocol = $_SERVER['HTTP_X_FORWARDED_PROTO'];
} elseif (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') {
$protocol = 'https';
}
return $protocol;
}
/**
* Returns the current host.
*
@@ -365,7 +427,9 @@ public static function getSelfHost()
public static function getSelfPort()
{
$portnumber = null;
if (self::getProxyVars() && isset($_SERVER["HTTP_X_FORWARDED_PORT"])) {
if (self::$_port) {
$portnumber = self::$_port;
} else if (self::getProxyVars() && isset($_SERVER["HTTP_X_FORWARDED_PORT"])) {
$portnumber = $_SERVER["HTTP_X_FORWARDED_PORT"];
} else if (isset($_SERVER["SERVER_PORT"])) {
$portnumber = $_SERVER["SERVER_PORT"];
@@ -390,10 +454,7 @@ public static function getSelfPort()
*/
public static function isHTTPS()
{
$isHttps = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|| (self::getSelfPort() == 443)
|| (self::getProxyVars() && isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https');
return $isHttps;
return self::getSelfProtocol() == 'https';
}
/**
@@ -16,7 +16,7 @@ class OneLogin_Saml2_UtilsTest extends PHPUnit_Framework_TestCase
/*
public function testT()
{
setlocale(LC_MESSAGES, 'en_US');
setlocale(LC_MESSAGES, 'en_US');
$msg = 'test';
$translatedMsg = OneLogin_Saml2_Utils::t($msg);
@@ -277,6 +277,18 @@ public function testRedirect()
$this->assertEquals("http://$hostname/example?alphavalue=a&numvaluelist[]=", $targetUrl8);
}
/**
* @covers OneLogin_Saml2_Utils::setSelfHost
*/
public function testSetselfhost()
{
$_SERVER['HTTP_HOST'] = 'example.org';
$this->assertEquals('example.org', OneLogin_Saml2_Utils::getSelfHost());
OneLogin_Saml2_Utils::setSelfHost('example.com');
$this->assertEquals('example.com', OneLogin_Saml2_Utils::getSelfHost());
}
/**
* @covers OneLogin_Saml2_Utils::setProxyVars()
* @covers OneLogin_Saml2_Utils::getProxyVars()
@@ -323,6 +335,12 @@ public function testGetselfhost()
$_SERVER['HTTP_HOST'] = 'example.org:ok';
$this->assertEquals('example.org', OneLogin_Saml2_Utils::getSelfHost());
$_SERVER['HTTP_X_FORWARDED_HOST'] = 'example.net';
$this->assertNotEquals('example.net', OneLogin_Saml2_Utils::getSelfHost());
OneLogin_Saml2_Utils::setProxyVars(true);
$this->assertEquals('example.net', OneLogin_Saml2_Utils::getSelfHost());
}
/**
@@ -371,8 +389,21 @@ public function testGetselfPort()
OneLogin_Saml2_Utils::setProxyVars(true);
$this->assertEquals(443, OneLogin_Saml2_Utils::getSelfPort());
OneLogin_Saml2_Utils::setSelfPort(8080);
$this->assertEquals(8080, OneLogin_Saml2_Utils::getSelfPort());
}
/**
* @covers OneLogin_Saml2_Utils::setSelfProtocol()
*/
public function testSetselfprotocol()
{
$this->assertFalse(OneLogin_Saml2_Utils::isHTTPS());
OneLogin_Saml2_Utils::setSelfProtocol('https');
$this->assertTrue(OneLogin_Saml2_Utils::isHTTPS());
}
/**
* Tests the getSelfURLhost method of the OneLogin_Saml2_Utils
ProTip! Use n and p to navigate between commits in a pull request.