Merge pull request #1957 from jedateach/uri-rewriting-patch

FIX: Don't rewrite urls to be absolute, if they are a URI with a protocol
This commit is contained in:
Sean Harvey 2013-05-20 00:31:13 -07:00
commit 881a41cc30
2 changed files with 34 additions and 6 deletions

View File

@ -62,7 +62,10 @@ 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(preg_match('/^\w+:/', $url)){
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'
);
});
}
/**