diff --git a/core/UrlHelper.php b/core/UrlHelper.php index b1dfa46d12b..5efba999960 100644 --- a/core/UrlHelper.php +++ b/core/UrlHelper.php @@ -101,8 +101,8 @@ public static function getLossyUrl($url) */ public static function isLookLikeUrl($url) { - return preg_match('~^(ftp|news|http|https)?://(.*)$~D', $url, $matches) !== 0 - && strlen($matches[2]) > 0; + return preg_match('~^((ftp|news|http|https)?:)?//(.*)$~D', $url, $matches) !== 0 + && strlen($matches[3]) > 0; } /** diff --git a/tests/PHPUnit/Unit/UrlHelperTest.php b/tests/PHPUnit/Unit/UrlHelperTest.php index 9a006c7a40a..dc4a97047b7 100644 --- a/tests/PHPUnit/Unit/UrlHelperTest.php +++ b/tests/PHPUnit/Unit/UrlHelperTest.php @@ -28,9 +28,17 @@ public function getUrls() array('news://www.pi-wik.org', true), array('https://www.tëteâ.org', true), array('http://汉语/漢語.cn', true), //chinese + + // valid network-path reference RFC3986 + array('//piwik.org', true), + array('//piwik/hello?world=test&test', true), + array('//piwik.org/hello?world=test&test', true), + // invalid urls array('it doesnt look like url', false), array('/index?page=test', false), + array('http:/index?page=test', false), + array('http/index?page=test', false), array('test.html', false), array('/\/\/\/\/\/\\\http://test.com////', false), array('jmleslangues.php', false), @@ -46,7 +54,7 @@ public function getUrls() */ public function testIsUrl($url, $isValid) { - $this->assertEquals($isValid, UrlHelper::isLookLikeUrl($url)); + $this->assertEquals($isValid, UrlHelper::isLookLikeUrl($url), "$url failed test"); } /** @@ -196,6 +204,8 @@ public function test_getHostFromUrl() $this->assertEquals('', UrlHelper::getHostFromUrl(null)); $this->assertEquals('localhost', UrlHelper::getHostFromUrl('http://localhost')); $this->assertEquals('localhost', UrlHelper::getHostFromUrl('http://localhost/path')); + $this->assertEquals('localhost', UrlHelper::getHostFromUrl('//localhost/path')); + $this->assertEquals('localhost', UrlHelper::getHostFromUrl('//localhost/path?test=test2')); $this->assertEquals('localhost', UrlHelper::getHostFromUrl('localhost/path')); $this->assertEquals('sub.localhost', UrlHelper::getHostFromUrl('sub.localhost/path')); $this->assertEquals('sub.localhost', UrlHelper::getHostFromUrl('http://sub.localhost/path/?query=test'));