Skip to content

Commit

Permalink
BUG Fix HTTP url rewriting
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Mooyman committed Jun 17, 2016
1 parent deb8163 commit 9b9bef7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 5 additions & 4 deletions control/HTTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static function absoluteURLs($html) {
* @param string|callable $code Either a string that can evaluate to an expression to rewrite links
* (depreciated), or a callable that takes a single parameter and returns the rewritten URL.
*
* @return The content with all links rewritten as per the logic specified in $code.
* @return string The content with all links rewritten as per the logic specified in $code.
*/
public static function urlRewriter($content, $code) {
if(!is_callable($code)) {
Expand Down Expand Up @@ -132,14 +132,15 @@ public static function urlRewriter($content, $code) {

// Callback for regexp replacement
$callback = function($matches) use($code) {
// Decode HTML attribute
$URL = Convert::xml2raw($matches[2]);
if(is_callable($code)) {
$rewritten = $code($matches[2]);
$rewritten = $code($URL);
} else {
// Expose the $URL variable to be used by the $code expression
$URL = $matches[2];
$rewritten = eval("return ($code);");
}
return $matches[1] . $rewritten . $matches[3];
return $matches[1] . Convert::raw2xml($rewritten) . $matches[3];
};

// Execute each expression
Expand Down
6 changes: 6 additions & 0 deletions tests/control/HTTPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ public function testAbsoluteURLsAttributes() {
'<link href=http://www.silverstripe.org/base.css />',
HTTP::absoluteURLs('<link href=base.css />')
);

// Test special characters are retained
$test->assertEquals(
'<a href="http://www.silverstripe.org/Security/changepassword?m=3&amp;t=7214fdfde">password reset link</a>',
HTTP::absoluteURLs('<a href="/Security/changepassword?m=3&amp;t=7214fdfde">password reset link</a>')
);
});
}

Expand Down

0 comments on commit 9b9bef7

Please sign in to comment.