From 40f7a876d43a21680888ffdd3a6066bb3a7865f6 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 29 May 2014 23:59:45 +1200 Subject: [PATCH] Support for protocol relative RedirectorPage.ExternalURL --- code/model/RedirectorPage.php | 6 +++++- tests/model/RedirectorPageTest.php | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/code/model/RedirectorPage.php b/code/model/RedirectorPage.php index 55021b23..c9ec3270 100644 --- a/code/model/RedirectorPage.php +++ b/code/model/RedirectorPage.php @@ -107,7 +107,11 @@ class RedirectorPage extends Page { parent::onBeforeWrite(); // Prefix the URL with "http://" if no prefix is found - if($this->ExternalURL && (strpos($this->ExternalURL, '://') === false)) { + if( + $this->ExternalURL + && !parse_url($this->ExternalURL, PHP_URL_SCHEME) + && !preg_match('#^//#', $this->ExternalURL) + ) { $this->ExternalURL = 'http://' . $this->ExternalURL; } } diff --git a/tests/model/RedirectorPageTest.php b/tests/model/RedirectorPageTest.php index 24afdab8..5e036a16 100644 --- a/tests/model/RedirectorPageTest.php +++ b/tests/model/RedirectorPageTest.php @@ -50,4 +50,18 @@ class RedirectorPageTest extends FunctionalTest { $page->write(); $this->assertEquals($page->ExternalURL, 'http://google.com', 'onBeforeWrite will not double prefix if written again!'); } + + public function testAllowsProtocolRelative() { + $noProtocol = new RedirectorPage(array('ExternalURL' => 'mydomain.com')); + $noProtocol->write(); + $this->assertEquals('http://mydomain.com', $noProtocol->ExternalURL); + + $protocolAbsolute = new RedirectorPage(array('ExternalURL' => 'http://mydomain.com')); + $protocolAbsolute->write(); + $this->assertEquals('http://mydomain.com', $protocolAbsolute->ExternalURL); + + $protocolRelative = new RedirectorPage(array('ExternalURL' => '//mydomain.com')); + $protocolRelative->write(); + $this->assertEquals('//mydomain.com', $protocolRelative->ExternalURL); + } }