Skip to content
Browse files

Support for protocol relative RedirectorPage.ExternalURL

  • Loading branch information...
1 parent 6a69134 commit 40f7a876d43a21680888ffdd3a6066bb3a7865f6 @chillu chillu committed May 29, 2014
Showing with 19 additions and 1 deletion.
  1. +5 −1 code/model/RedirectorPage.php
  2. +14 −0 tests/model/RedirectorPageTest.php
View
6 code/model/RedirectorPage.php
@@ -107,7 +107,11 @@ public function onBeforeWrite() {
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;
}
}
View
14 tests/model/RedirectorPageTest.php
@@ -50,4 +50,18 @@ public function testExternalURLGetsPrefixIfNotSet() {
$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);
+ }
}

0 comments on commit 40f7a87

Please sign in to comment.
Something went wrong with that request. Please try again.