diff --git a/lam/lib/account.inc b/lam/lib/account.inc index d5247f83..7c940304 100644 --- a/lam/lib/account.inc +++ b/lam/lib/account.inc @@ -1465,11 +1465,15 @@ function getDefaultLDAPErrorString($server) { * Returns the URL under which the page was loaded. * This includes any GET parameters set. * + * @param $baseUrl base URL (e.g. http://www.example.com) * @return String URL */ -function getCallingURL() { +function getCallingURL($baseUrl = '') { $url = null; - if (!empty($_SERVER['HTTP_HOST']) && !empty($_SERVER['REQUEST_URI'])) { + if (!empty($baseUrl) && !empty($_SERVER['REQUEST_URI'])) { + $url = $baseUrl . $_SERVER['REQUEST_URI']; + } + elseif (!empty($_SERVER['REQUEST_URI']) && !empty($_SERVER['HTTP_HOST'])) { $proto = 'http://'; if (!empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] != 'off')) { $proto = 'https://'; diff --git a/lam/tests/lib/accountTest.php b/lam/tests/lib/accountTest.php index 5c6e02d5..3bef321e 100644 --- a/lam/tests/lib/accountTest.php +++ b/lam/tests/lib/accountTest.php @@ -19,7 +19,8 @@ */ -include_once 'lam/lib/account.inc'; +include_once __DIR__ . '/../../lib/account.inc'; +include_once __DIR__ . '/../../lib/security.inc'; /** * LAMConfig test case. @@ -88,4 +89,43 @@ class AccountTest extends PHPUnit_Framework_TestCase { $this->assertEquals('', formatSecondsToShortFormat('')); } + /** + * Tests getCallingURL(). + */ + function testGetCallingURL_noBaseUrl_noHost() { + $_SERVER['REQUEST_URI'] = '/test.php'; + $_SERVER['HTTP_HOST'] = null; + $_SERVER['HTTP_REFERER'] = 'http://referrer/test.php'; + $_SERVER['HTTPS'] = 'on'; + $this->assertEquals('http://referrer/test.php', getCallingURL()); + $_SERVER['HTTP_REFERER'] = null; + $this->assertNull(getCallingURL()); + } + + /** + * Tests getCallingURL(). + */ + function testGetCallingURL_noBaseUrl_host() { + $_SERVER['REQUEST_URI'] = '/test.php'; + $_SERVER['HTTP_HOST'] = 'host'; + $_SERVER['HTTP_REFERER'] = 'http://referrer/test.php'; + $_SERVER['HTTPS'] = 'on'; + $this->assertEquals('https://host/test.php', getCallingURL()); + $_SERVER['HTTP_REFERER'] = null; + $this->assertEquals('https://host/test.php', getCallingURL()); + } + + /** + * Tests getCallingURL(). + */ + function testGetCallingURL_baseUrl_host() { + $_SERVER['REQUEST_URI'] = '/test.php'; + $_SERVER['HTTP_HOST'] = 'host'; + $_SERVER['HTTP_REFERER'] = 'http://referrer/test.php'; + $_SERVER['HTTPS'] = 'on'; + $this->assertEquals('http://base/test.php', getCallingURL('http://base')); + $_SERVER['HTTP_REFERER'] = null; + $this->assertEquals('http://base/test.php', getCallingURL('http://base')); + } + }