FIX: Don't rewrite urls to be absolute, if they are a URI with a protocol.

This is determined in this fix by the existence of a colon ':', to show the uri has a protocol.
This commit is contained in:
Jeremy Shipman 2013-05-20 11:59:04 +12:00
parent e0f4bd6113
commit d21fd1f0bb
2 changed files with 35 additions and 6 deletions

View File

@ -62,7 +62,11 @@ class HTTP {
public static function absoluteURLs($html) {
$html = str_replace('$CurrentPageURL', $_SERVER['REQUEST_URI'], $html);
return HTTP::urlRewriter($html, function($url) {
if(stripos($url, 'mailto:') === 0) return $url;
//no need to rewrite, if uri has a protocol
//(determined here by existence of reserved URI character ":")
if(stripos($url, ":") !== false){
return $url;
}
return Director::absoluteURL($url, true);
});
}

View File

@ -173,6 +173,14 @@ class HTTPTest extends SapphireTest {
HTTP::absoluteURLs('<div background="./themes/silverstripe/images/nav-bg-repeat-2.png">SS Blog</div>')
);
//check dot segments
// Assumption: dots are not removed
//if they were, the url should be: http://www.silverstripe.org/abc
$test->assertEquals(
'<a href="http://www.silverstripe.org/test/page/../../abc">Test</a>',
HTTP::absoluteURLs('<a href="test/page/../../abc">Test</a>')
);
// image
$test->assertEquals(
'<img src=\'http://www.silverstripe.org/themes/silverstripe/images/logo-org.png\' />',
@ -187,16 +195,33 @@ class HTTPTest extends SapphireTest {
});
}
public function testEmailLinks() {
/**
* Make sure URI schemes are not rewritten
*/
public function testURISchemes() {
$this->withBaseURL('http://www.silverstripe.org/', function($test){
// links
// mailto
$test->assertEquals(
'<a href=\'mailto:admin@silverstripe.org\'>Email Us</a>',
HTTP::absoluteURLs('<a href=\'mailto:admin@silverstripe.org\'>Email Us</a>')
HTTP::absoluteURLs('<a href=\'mailto:admin@silverstripe.org\'>Email Us</a>'),
'Email links are not rewritten'
);
// data uri
$test->assertEquals(
'<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />',
HTTP::absoluteURLs('<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />'),
'Data URI links are not rewritten'
);
// call
$test->assertEquals(
'<a href="callto:12345678" />',
HTTP::absoluteURLs('<a href="callto:12345678" />'),
'Call to links are not rewritten'
);
});
}
/**