Skip to content

Commit 0bb4336

Browse files
committed
RequestFactory: refactoring of removing magic quotes
1 parent ca5f760 commit 0bb4336

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

src/Http/Helpers.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,13 @@ public static function removeDuplicateCookies()
8383
/**
8484
* @internal
8585
*/
86-
public static function stripSlashes($arr)
86+
public static function stripSlashes($arr, $onlyKeys = FALSE)
8787
{
8888
$res = array();
8989
foreach ($arr as $k => $v) {
90-
$res[stripslashes($k)] = is_array($v) ? self::stripSlashes($v) : stripslashes($v);
90+
$res[stripslashes($k)] = is_array($v)
91+
? self::stripSlashes($v, $onlyKeys)
92+
: ($onlyKeys ? $v : stripslashes($v));
9193
}
9294
return $res;
9395
}

src/Http/RequestFactory.php

+12-21
Original file line numberDiff line numberDiff line change
@@ -100,38 +100,29 @@ public function createHttpRequest()
100100
// GET, POST, COOKIE
101101
$useFilter = (!in_array(ini_get('filter.default'), array('', 'unsafe_raw')) || ini_get('filter.default_flags'));
102102

103-
parse_str($url->getQuery(), $query);
103+
$query = $url->getQueryParameters();
104104
$post = $useFilter ? filter_input_array(INPUT_POST, FILTER_UNSAFE_RAW) : (empty($_POST) ? array() : $_POST);
105105
$cookies = $useFilter ? filter_input_array(INPUT_COOKIE, FILTER_UNSAFE_RAW) : (empty($_COOKIE) ? array() : $_COOKIE);
106106

107-
$gpc = (bool) get_magic_quotes_gpc();
107+
if (get_magic_quotes_gpc()) {
108+
$post = Helpers::stripslashes($post, $useFilter);
109+
$cookies = Helpers::stripslashes($cookies, $useFilter);
110+
}
108111

109-
// remove fucking quotes, control characters and check encoding
110-
if ($gpc || !$this->binary) {
112+
// remove invalid characters
113+
if (!$this->binary) {
111114
$list = array(& $query, & $post, & $cookies);
112115
while (list($key, $val) = each($list)) {
113116
foreach ($val as $k => $v) {
114-
unset($list[$key][$k]);
115-
116-
if ($gpc) {
117-
$k = stripslashes($k);
118-
}
119-
120-
if (!$this->binary && is_string($k) && (!preg_match(self::CHARS, $k) || preg_last_error())) {
121-
// invalid key -> ignore
117+
if (is_string($k) && (!preg_match(self::CHARS, $k) || preg_last_error())) {
118+
unset($list[$key][$k]);
122119

123120
} elseif (is_array($v)) {
124121
$list[$key][$k] = $v;
125122
$list[] = & $list[$key][$k];
126123

127-
} else {
128-
if ($gpc && !$useFilter) {
129-
$v = stripSlashes($v);
130-
}
131-
if (!$this->binary && (!preg_match(self::CHARS, $v) || preg_last_error())) {
132-
$v = '';
133-
}
134-
$list[$key][$k] = $v;
124+
} elseif (!preg_match(self::CHARS, $v) || preg_last_error()) {
125+
$list[$key][$k] = '';
135126
}
136127
}
137128
}
@@ -158,7 +149,7 @@ public function createHttpRequest()
158149
continue;
159150

160151
} elseif (!is_array($v['name'])) {
161-
if ($gpc) {
152+
if (get_magic_quotes_gpc()) {
162153
$v['name'] = stripSlashes($v['name']);
163154
}
164155
if (!$this->binary && (!preg_match(self::CHARS, $v['name']) || preg_last_error())) {

tests/Http/Request.request.phpt

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $_SERVER = array(
1818
'QUERY_STRING' => 'x param=val.&pa%%72am=val2&param3=v%20a%26l%3Du%2Be)',
1919
'REMOTE_ADDR' => '192.168.188.66',
2020
'REQUEST_METHOD' => 'GET',
21-
'REQUEST_URI' => '/file.php?x param=val.&pa%%72am=val2&param3=v%20a%26l%3Du%2Be)',
21+
'REQUEST_URI' => '/file.php?x param=val.&pa%%72am=val2&quotes\\"=\\"&param3=v%20a%26l%3Du%2Be)',
2222
'SCRIPT_NAME' => '/file.php',
2323
);
2424

@@ -39,16 +39,16 @@ test(function() {
3939
Assert::same( 'nette.org', $request->getUrl()->host );
4040
Assert::same( 8080, $request->getUrl()->port );
4141
Assert::same( '/file.php', $request->getUrl()->path );
42-
Assert::same( 'x_param=val.&pa%25ram=val2&param3=v%20a%26l%3Du%2Be', $request->getUrl()->query );
42+
Assert::same( 'x_param=val.&pa%25ram=val2&quotes%5C%22=%5C%22&param3=v%20a%26l%3Du%2Be', $request->getUrl()->query );
4343
Assert::same( '', $request->getUrl()->fragment );
4444
Assert::same( 'val.', $request->getQuery('x_param') );
4545
Assert::same( 'val2', $request->getQuery('pa%ram') );
4646
Assert::same( 'nette.org:8080', $request->getUrl()->authority );
4747
Assert::same( 'https://nette.org:8080', $request->getUrl()->hostUrl );
4848
Assert::same( 'https://nette.org:8080/', $request->getUrl()->baseUrl );
4949
Assert::same( '/', $request->getUrl()->basePath );
50-
Assert::same( 'file.php?x_param=val.&pa%25ram=val2&param3=v%20a%26l%3Du%2Be', $request->getUrl()->relativeUrl );
51-
Assert::same( 'https://nette.org:8080/file.php?x_param=val.&pa%25ram=val2&param3=v%20a%26l%3Du%2Be', $request->getUrl()->absoluteUrl );
50+
Assert::same( 'file.php?x_param=val.&pa%25ram=val2&quotes%5C%22=%5C%22&param3=v%20a%26l%3Du%2Be', $request->getUrl()->relativeUrl );
51+
Assert::same( 'https://nette.org:8080/file.php?x_param=val.&pa%25ram=val2&quotes%5C%22=%5C%22&param3=v%20a%26l%3Du%2Be', $request->getUrl()->absoluteUrl );
5252
Assert::same( '', $request->getUrl()->pathInfo );
5353
});
5454

@@ -65,7 +65,7 @@ test(function() {
6565
Assert::same( 'nette.org', $request->getUrl()->host );
6666
Assert::same( 8080, $request->getUrl()->port );
6767
Assert::same( '/file.php', $request->getUrl()->path );
68-
Assert::same( 'x_param=val.&pa%25ram=val2&param3=v%20a%26l%3Du%2Be%29', $request->getUrl()->query );
68+
Assert::same( 'x_param=val.&pa%25ram=val2&quotes%5C%22=%5C%22&param3=v%20a%26l%3Du%2Be%29', $request->getUrl()->query );
6969
Assert::same( '', $request->getUrl()->fragment );
7070
Assert::same( 'val.', $request->getQuery('x_param') );
7171
Assert::same( 'val2', $request->getQuery('pa%ram') );

0 commit comments

Comments
 (0)