[feature/request-class] Remove $html_encode arg, force manual decoding

PHPBB3-9716
This commit is contained in:
Igor Wiedler 2011-08-18 23:19:48 +02:00
parent b4ae124084
commit fd08cd8dd0
6 changed files with 21 additions and 45 deletions

View file

@ -61,34 +61,31 @@ interface phpbb_request_interface
* Default is false, causing all bytes outside the ASCII range (0-127) to be replaced with question marks * Default is false, causing all bytes outside the ASCII range (0-127) to be replaced with question marks
* @param phpbb_request_interface::POST|GET|REQUEST|COOKIE $super_global * @param phpbb_request_interface::POST|GET|REQUEST|COOKIE $super_global
* Specifies which super global should be used * Specifies which super global should be used
* @param bool $html_encode When true, html encoding will be applied
* *
* @return mixed The value of $_REQUEST[$var_name] run through {@link set_var set_var} to ensure that the type is the * @return mixed The value of $_REQUEST[$var_name] run through {@link set_var set_var} to ensure that the type is the
* the same as that of $default. If the variable is not set $default is returned. * the same as that of $default. If the variable is not set $default is returned.
*/ */
public function variable($var_name, $default, $multibyte = false, $super_global = phpbb_request_interface::REQUEST, $html_encode = true); public function variable($var_name, $default, $multibyte = false, $super_global = phpbb_request_interface::REQUEST);
/** /**
* Shortcut method to retrieve SERVER variables. * Shortcut method to retrieve SERVER variables.
* *
* @param string|array $var_name See phpbb_request_interface::variable * @param string|array $var_name See phpbb_request_interface::variable
* @param mixed $default See phpbb_request_interface::variable * @param mixed $default See phpbb_request_interface::variable
* @param bool $html_encode See phpbb_request_interface::variable
* *
* @return mixed The server variable value. * @return mixed The server variable value.
*/ */
public function server($var_name, $default = '', $html_encode = false); public function server($var_name, $default = '');
/** /**
* Shortcut method to retrieve the value of client HTTP headers. * Shortcut method to retrieve the value of client HTTP headers.
* *
* @param string|array $header_name The name of the header to retrieve. * @param string|array $header_name The name of the header to retrieve.
* @param mixed $default See phpbb_request_interface::variable * @param mixed $default See phpbb_request_interface::variable
* @param bool $html_encode See phpbb_request_interface::variable
* *
* @return mixed The header value. * @return mixed The header value.
*/ */
public function header($var_name, $default = '', $html_encode = false); public function header($var_name, $default = '');
/** /**
* Checks whether a certain variable was sent via POST. * Checks whether a certain variable was sent via POST.

View file

@ -194,12 +194,11 @@ class phpbb_request implements phpbb_request_interface
* Default is false, causing all bytes outside the ASCII range (0-127) to be replaced with question marks * Default is false, causing all bytes outside the ASCII range (0-127) to be replaced with question marks
* @param phpbb_request_interface::POST|GET|REQUEST|COOKIE $super_global * @param phpbb_request_interface::POST|GET|REQUEST|COOKIE $super_global
* Specifies which super global should be used * Specifies which super global should be used
* @param bool $html_encode When true, html encoding will be applied
* *
* @return mixed The value of $_REQUEST[$var_name] run through {@link set_var set_var} to ensure that the type is the * @return mixed The value of $_REQUEST[$var_name] run through {@link set_var set_var} to ensure that the type is the
* the same as that of $default. If the variable is not set $default is returned. * the same as that of $default. If the variable is not set $default is returned.
*/ */
public function variable($var_name, $default, $multibyte = false, $super_global = phpbb_request_interface::REQUEST, $html_encode = true) public function variable($var_name, $default, $multibyte = false, $super_global = phpbb_request_interface::REQUEST)
{ {
$path = false; $path = false;
@ -238,7 +237,7 @@ class phpbb_request implements phpbb_request_interface
} }
} }
$this->type_cast_helper->recursive_set_var($var, $default, $multibyte, $html_encode); $this->type_cast_helper->recursive_set_var($var, $default, $multibyte);
return $var; return $var;
} }
@ -251,22 +250,21 @@ class phpbb_request implements phpbb_request_interface
* *
* @param string|array $var_name See phpbb_request_interface::variable * @param string|array $var_name See phpbb_request_interface::variable
* @param mixed $Default See phpbb_request_interface::variable * @param mixed $Default See phpbb_request_interface::variable
* @param bool $html_encode See phpbb_request_interface::variable
* *
* @return mixed The server variable value. * @return mixed The server variable value.
*/ */
public function server($var_name, $default = '', $html_encode = false) public function server($var_name, $default = '')
{ {
$multibyte = true; $multibyte = true;
if ($this->is_set($var_name, phpbb_request_interface::SERVER)) if ($this->is_set($var_name, phpbb_request_interface::SERVER))
{ {
return $this->variable($var_name, $default, $multibyte, phpbb_request_interface::SERVER, $html_encode); return $this->variable($var_name, $default, $multibyte, phpbb_request_interface::SERVER);
} }
else else
{ {
$var = getenv($var_name); $var = getenv($var_name);
$this->type_cast_helper->recursive_set_var($var, $default, $multibyte, $html_encode); $this->type_cast_helper->recursive_set_var($var, $default, $multibyte);
return $var; return $var;
} }
} }
@ -276,14 +274,13 @@ class phpbb_request implements phpbb_request_interface
* *
* @param string|array $header_name The name of the header to retrieve. * @param string|array $header_name The name of the header to retrieve.
* @param mixed $default See phpbb_request_interface::variable * @param mixed $default See phpbb_request_interface::variable
* @param bool $html_encode See phpbb_request_interface::variable
* *
* @return mixed The header value. * @return mixed The header value.
*/ */
public function header($header_name, $default = '', $html_encode = true) public function header($header_name, $default = '')
{ {
$var_name = 'HTTP_' . str_replace('-', '_', strtoupper($header_name)); $var_name = 'HTTP_' . str_replace('-', '_', strtoupper($header_name));
return $this->server($var_name, $default, $html_encode); return $this->server($var_name, $default);
} }
/** /**

View file

@ -93,9 +93,8 @@ class phpbb_request_type_cast_helper implements phpbb_request_type_cast_helper_i
* @param mixed $type The variable type. Will be used with {@link settype()} * @param mixed $type The variable type. Will be used with {@link settype()}
* @param bool $multibyte Indicates whether string values may contain UTF-8 characters. * @param bool $multibyte Indicates whether string values may contain UTF-8 characters.
* Default is false, causing all bytes outside the ASCII range (0-127) to be replaced with question marks. * Default is false, causing all bytes outside the ASCII range (0-127) to be replaced with question marks.
* @param bool $html_encode When true, html encoding will be applied
*/ */
public function set_var(&$result, $var, $type, $multibyte = false, $html_encode = true) public function set_var(&$result, $var, $type, $multibyte = false)
{ {
settype($var, $type); settype($var, $type);
$result = $var; $result = $var;
@ -103,11 +102,7 @@ class phpbb_request_type_cast_helper implements phpbb_request_type_cast_helper_i
if ($type == 'string') if ($type == 'string')
{ {
$result = trim(str_replace(array("\r\n", "\r", "\0"), array("\n", "\n", ''), $result)); $result = trim(str_replace(array("\r\n", "\r", "\0"), array("\n", "\n", ''), $result));
$result = htmlspecialchars($result, ENT_COMPAT, 'UTF-8');
if ($html_encode)
{
$result = htmlspecialchars($result, ENT_COMPAT, 'UTF-8');
}
if ($multibyte) if ($multibyte)
{ {
@ -146,9 +141,8 @@ class phpbb_request_type_cast_helper implements phpbb_request_type_cast_helper_i
* @param bool $multibyte Indicates whether string keys and values may contain UTF-8 characters. * @param bool $multibyte Indicates whether string keys and values may contain UTF-8 characters.
* Default is false, causing all bytes outside the ASCII range (0-127) to * Default is false, causing all bytes outside the ASCII range (0-127) to
* be replaced with question marks. * be replaced with question marks.
* @param bool $html_encode When true, html encoding will be applied
*/ */
public function recursive_set_var(&$var, $default, $multibyte, $html_encode = true) public function recursive_set_var(&$var, $default, $multibyte)
{ {
if (is_array($var) !== is_array($default)) if (is_array($var) !== is_array($default))
{ {
@ -159,7 +153,7 @@ class phpbb_request_type_cast_helper implements phpbb_request_type_cast_helper_i
if (!is_array($default)) if (!is_array($default))
{ {
$type = gettype($default); $type = gettype($default);
$this->set_var($var, $var, $type, $multibyte, $html_encode); $this->set_var($var, $var, $type, $multibyte);
} }
else else
{ {
@ -180,9 +174,9 @@ class phpbb_request_type_cast_helper implements phpbb_request_type_cast_helper_i
foreach ($_var as $k => $v) foreach ($_var as $k => $v)
{ {
$this->set_var($k, $k, $key_type, $multibyte, $multibyte, $html_encode); $this->set_var($k, $k, $key_type, $multibyte, $multibyte);
$this->recursive_set_var($v, $default_value, $multibyte, $html_encode); $this->recursive_set_var($v, $default_value, $multibyte);
$var[$k] = $v; $var[$k] = $v;
} }
} }

View file

@ -25,21 +25,21 @@ class phpbb_mock_request implements phpbb_request_interface
$this->data[$super_global][$var_name] = $value; $this->data[$super_global][$var_name] = $value;
} }
public function variable($var_name, $default, $multibyte = false, $super_global = phpbb_request_interface::REQUEST, $html_encode = true) public function variable($var_name, $default, $multibyte = false, $super_global = phpbb_request_interface::REQUEST)
{ {
return isset($this->data[$super_global][$var_name]) ? $this->data[$super_global][$var_name] : $default; return isset($this->data[$super_global][$var_name]) ? $this->data[$super_global][$var_name] : $default;
} }
public function server($var_name, $default = '', $html_encode = false) public function server($var_name, $default = '')
{ {
$super_global = phpbb_request_interface::SERVER; $super_global = phpbb_request_interface::SERVER;
return isset($this->data[$super_global][$var_name]) ? $this->data[$super_global][$var_name] : $default; return isset($this->data[$super_global][$var_name]) ? $this->data[$super_global][$var_name] : $default;
} }
public function header($header_name, $default = '', $html_encode = false) public function header($header_name, $default = '')
{ {
$var_name = 'HTTP_' . str_replace('-', '_', strtoupper($header_name)); $var_name = 'HTTP_' . str_replace('-', '_', strtoupper($header_name));
return $this->server($var_name, $default, $html_encode); return $this->server($var_name, $default);
} }
public function is_set_post($name) public function is_set_post($name)

View file

@ -60,8 +60,7 @@ class phpbb_request_test extends phpbb_test_case
->with( ->with(
$this->anything(), $this->anything(),
'', '',
true, true
false
); );
$this->request->server('HTTP_SOMEVAR'); $this->request->server('HTTP_SOMEVAR');
@ -80,7 +79,6 @@ class phpbb_request_test extends phpbb_test_case
->with( ->with(
$this->anything(), $this->anything(),
'', '',
true,
true true
); );

View file

@ -48,14 +48,4 @@ class phpbb_type_cast_helper_test extends phpbb_test_case
$this->assertEquals($expected, $data); $this->assertEquals($expected, $data);
} }
public function test_simple_set_var_without_html_encoding()
{
$data = 'eviL<3';
$expected = 'eviL<3';
$this->type_cast_helper->recursive_set_var($data, '', true, false);
$this->assertEquals($expected, $data);
}
} }