Browse files

Merge pull request #1022 from silverstripe-iterators/pulls/redirector…

…-proto-relative

Pulls/redirector proto relative
  • Loading branch information...
2 parents cba0061 + 40f7a87 commit 1a85c7a1ec04d6e905fa4688644d01bcea293d84 @tractorcow tractorcow committed Jun 13, 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 1a85c7a

Please sign in to comment.