From 49d7154464e2bfdae939af06d922879662253438 Mon Sep 17 00:00:00 2001 From: Welling Guzman Date: Tue, 13 Sep 2016 12:33:59 -0400 Subject: [PATCH] add default host in get_url helper --- api/core/functions.php | 7 +++++-- tests/api/functionsTest.php | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/api/core/functions.php b/api/core/functions.php index 06901a71d33a7..cff58e0d2e2d4 100644 --- a/api/core/functions.php +++ b/api/core/functions.php @@ -143,13 +143,16 @@ function is_ssl() * Get Directus URL * * @param $path - Extra path to add to the url + * @param $defaultHost + * * @return string */ - function get_url($path = '') + function get_url($path = '', $defaultHost = 'localhost') { $schema = is_ssl() ? 'https://' : 'http://'; + $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $defaultHost; $directusPath = defined('DIRECTUS_PATH') ? DIRECTUS_PATH : '/'; - $directusHost = rtrim($_SERVER['HTTP_HOST'] . $directusPath, '/') . '/'; + $directusHost = rtrim($host . $directusPath, '/') . '/'; return $schema . $directusHost . ltrim($path, '/'); } diff --git a/tests/api/functionsTest.php b/tests/api/functionsTest.php index ddb3df1f063f1..e5f3a67d75d47 100644 --- a/tests/api/functionsTest.php +++ b/tests/api/functionsTest.php @@ -22,6 +22,12 @@ public function testSSL() public function testGetURL() { + $url = get_url(); + $this->assertSame('http://localhost/', $url); + + $url = get_url('/users', 'example.local'); + $this->assertSame('http://example.local/users', $url); + $_SERVER['HTTP_HOST'] = 'localhost'; $url = get_url();