Support for protocol relative RedirectorPage.ExternalURL

This commit is contained in:
Ingo Schommer 2014-05-29 23:59:45 +12:00
parent 6a69134fb0
commit 40f7a876d4
2 changed files with 19 additions and 1 deletions

View File

@ -107,7 +107,11 @@ class RedirectorPage extends Page {
parent::onBeforeWrite(); parent::onBeforeWrite();
// Prefix the URL with "http://" if no prefix is found // 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; $this->ExternalURL = 'http://' . $this->ExternalURL;
} }
} }

View File

@ -50,4 +50,18 @@ class RedirectorPageTest extends FunctionalTest {
$page->write(); $page->write();
$this->assertEquals($page->ExternalURL, 'http://google.com', 'onBeforeWrite will not double prefix if written again!'); $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);
}
} }