Skip to content
Snippets Groups Projects
Commit 38743404 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

introduce js encodeURI() equivalent in php and use it for X-Location header, fixes #684

parent 17c877e0
No related branches found
No related tags found
No related merge requests found
...@@ -349,7 +349,7 @@ abstract class StudipController extends Trails_Controller ...@@ -349,7 +349,7 @@ abstract class StudipController extends Trails_Controller
$to = $this->adjustToArguments(...func_get_args()); $to = $this->adjustToArguments(...func_get_args());
if (Request::isDialog()) { if (Request::isDialog()) {
$this->response->add_header('X-Location', rawurlencode($to)); $this->response->add_header('X-Location', encodeURI($to));
$this->render_nothing(); $this->render_nothing();
} else { } else {
parent::redirect($to); parent::redirect($to);
......
...@@ -1827,3 +1827,37 @@ function get_default_http_stream_context($url = '') ...@@ -1827,3 +1827,37 @@ function get_default_http_stream_context($url = '')
} }
return stream_context_get_default($opts); return stream_context_get_default($opts);
} }
/**
* Encodes an uri just like encodeURI() in Javascript would do.
*
* encodeURI() escapes all characters except:
*
* A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #
*
* @param string $uri
* @return string
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI
*/
function encodeURI(string $uri): string
{
$replacements = [
'%21' => '!',
'%23' => '#',
'%24' => '$',
'%26' => '&',
'%27' => "'",
'%28' => '(',
'%29' => ')',
'%2A' => '*',
'%2B' => '+',
'%2C' => ',',
'%3B' => ';',
'%2F' => '/',
'%3A' => ':',
'%3D' => '=',
'%3F' => '?',
'%40' => '@',
];
return strtr(rawurlencode($uri), $replacements);
}
...@@ -103,7 +103,7 @@ const Dialog = { ...@@ -103,7 +103,7 @@ const Dialog = {
// Handler for HTTP header X-Location: Relocate to another location // Handler for HTTP header X-Location: Relocate to another location
Dialog.handlers.header['X-Location'] = function(location, options) { Dialog.handlers.header['X-Location'] = function(location, options) {
location = decodeURIComponent(location); location = decodeURI(location);
if (document.location.href === location) { if (document.location.href === location) {
document.location.reload(true); document.location.reload(true);
......
...@@ -42,7 +42,7 @@ class FunctionsTest extends \Codeception\Test\Unit ...@@ -42,7 +42,7 @@ class FunctionsTest extends \Codeception\Test\Unit
$this->assertEquals(range(1, 7), array_flatten($array)); $this->assertEquals(range(1, 7), array_flatten($array));
} }
function testRelsize() function testRelsize()
{ {
// Test basic sizes and suffixed 's' if value is <> 1 // Test basic sizes and suffixed 's' if value is <> 1
...@@ -62,7 +62,7 @@ class FunctionsTest extends \Codeception\Test\Unit ...@@ -62,7 +62,7 @@ class FunctionsTest extends \Codeception\Test\Unit
$this->assertEquals('1 Exabyte', relsize(pow(1024, 6))); $this->assertEquals('1 Exabyte', relsize(pow(1024, 6)));
$this->assertEquals('1 Zettabyte', relsize(pow(1024, 7))); $this->assertEquals('1 Zettabyte', relsize(pow(1024, 7)));
$this->assertEquals('1 Yottabyte', relsize(pow(1024, 8))); $this->assertEquals('1 Yottabyte', relsize(pow(1024, 8)));
// Test displayed levels // Test displayed levels
$this->assertEquals('1 Megabyte', relsize(1024 * 1024 + 2 * 1024 + 3, true, 1)); $this->assertEquals('1 Megabyte', relsize(1024 * 1024 + 2 * 1024 + 3, true, 1));
$this->assertEquals('1.5 Megabytes', relsize(1024 * 1024 + 512 * 1024 + 3, true, 1)); $this->assertEquals('1.5 Megabytes', relsize(1024 * 1024 + 512 * 1024 + 3, true, 1));
...@@ -70,4 +70,18 @@ class FunctionsTest extends \Codeception\Test\Unit ...@@ -70,4 +70,18 @@ class FunctionsTest extends \Codeception\Test\Unit
$this->assertEquals('1 Megabyte, 2 Kilobytes, 3 Bytes', relsize(1024 * 1024 + 2 * 1024 + 3, true, 3)); $this->assertEquals('1 Megabyte, 2 Kilobytes, 3 Bytes', relsize(1024 * 1024 + 2 * 1024 + 3, true, 3));
$this->assertEquals('1 Megabyte, 2 Kilobytes, 3 Bytes', relsize(1024 * 1024 + 2 * 1024 + 3, true, 0)); $this->assertEquals('1 Megabyte, 2 Kilobytes, 3 Bytes', relsize(1024 * 1024 + 2 * 1024 + 3, true, 0));
} }
public function testEncodeURI()
{
$input = 'A-Za-z0-9;,/?:@&=+$-_.!~*\'()#';
$this->assertEquals($input, encodeURI($input));
$input = 'https://example.org/?x=шеллы';
$output = 'https://example.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B';
$this->assertEquals($output, encodeURI($input));
$input = 'https://mäuschen-hüpft.de/öffnungszeiten?menu=Spaß&page=23';
$output = 'https://m%C3%A4uschen-h%C3%BCpft.de/%C3%B6ffnungszeiten?menu=Spa%C3%9F&page=23';
$this->assertEquals($output, encodeURI($input));
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment