BUG SS has problems handling + in URLs. Filter them out.

+ has a special meaning in the URLs so overall it's a good idea to
strip them out. Otherwise they would need to appear in their ugly url
encoded form "%2B".

Refer: http://open.silverstripe.org/ticket/7929
This commit is contained in:
Mateusz Uzdowski 2012-12-18 15:43:05 +13:00 committed by Ingo Schommer
parent 8f239d6373
commit d5a1c3d99a
2 changed files with 12 additions and 4 deletions

View File

@ -29,7 +29,7 @@ class URLSegmentFilter extends Object {
'/&/u' => '-and-', '/&/u' => '-and-',
'/\s/u' => '-', // remove whitespace '/\s/u' => '-', // remove whitespace
'/_/u' => '-', // underscores to dashes '/_/u' => '-', // underscores to dashes
'/[^A-Za-z0-9+.-]+/u' => '', // remove non-ASCII chars, only allow alphanumeric plus dash and dot '/[^A-Za-z0-9.-]+/u' => '', // remove non-ASCII chars, only allow alphanumeric, dashes and dots.
'/[\-]{2,}/u' => '-', // remove duplicate dashes '/[\-]{2,}/u' => '-', // remove duplicate dashes
'/^[\.\-_]/u' => '', // Remove all leading dots, dashes or underscores '/^[\.\-_]/u' => '', // Remove all leading dots, dashes or underscores
); );
@ -66,8 +66,8 @@ class URLSegmentFilter extends Object {
$replacements = $this->getReplacements(); $replacements = $this->getReplacements();
// Unset automated removal of non-ASCII characters, and don't try to transliterate // Unset automated removal of non-ASCII characters, and don't try to transliterate
if($this->getAllowMultibyte() && isset($replacements['/[^A-Za-z0-9+.-]+/u'])) { if($this->getAllowMultibyte() && isset($replacements['/[^A-Za-z0-9.-]+/u'])) {
unset($replacements['/[^A-Za-z0-9+.-]+/u']); unset($replacements['/[^A-Za-z0-9.-]+/u']);
} }
foreach($replacements as $regex => $replace) { foreach($replacements as $regex => $replace) {

View File

@ -23,6 +23,14 @@ class URLSegmentFilterTest extends SapphireTest {
); );
} }
public function testReplacesCommonNonAsciiCharacters() {
$f = new URLSegmentFilter();
$this->assertEquals(
urlencode('aa1-.'),
$f->filter('Aa1~!@#$%^*()_+`-=;\':"[]\{}|,./<>?')
);
}
public function testRetainsNonAsciiUrlsWithAllowMultiByteOption() { public function testRetainsNonAsciiUrlsWithAllowMultiByteOption() {
$f = new URLSegmentFilter(); $f = new URLSegmentFilter();
$f->setAllowMultibyte(true); $f->setAllowMultibyte(true);