Using the request helper without specifying a key should return the entire request body.

This commit is contained in:
Alex Tselegidis 2021-11-03 08:21:53 +01:00
parent 10ee3401e4
commit c390a6552a
2 changed files with 25 additions and 7 deletions

View file

@ -45,12 +45,12 @@ class EA_Input extends CI_Input {
/** /**
* Fetch an item from JSON data. * Fetch an item from JSON data.
* *
* @param string $index Index for item to be fetched from the JSON payload. * @param string|null $index Index for item to be fetched from the JSON payload.
* @param bool|false $xss_clean Whether to apply XSS filtering * @param bool|false $xss_clean Whether to apply XSS filtering
* *
* @return mixed * @return mixed
*/ */
public function json(string $index, bool $xss_clean = FALSE) public function json(string $index = NULL, bool $xss_clean = FALSE)
{ {
/** @var EA_Controller $CI */ /** @var EA_Controller $CI */
$CI = &get_instance(); $CI = &get_instance();
@ -69,8 +69,19 @@ class EA_Input extends CI_Input {
$payload = json_decode($input_stream, TRUE); $payload = json_decode($input_stream, TRUE);
$value = $payload[$index] ?? NULL; if ($xss_clean)
{
foreach ($payload as $name => $value)
{
$payload[$name] = $CI->security->xss_clean($value);
}
}
return $value && $xss_clean ? $CI->security->xss_clean($value) : $value; if (empty($index))
{
return $payload;
}
return $payload[$index] ?? NULL;;
} }
} }

View file

@ -21,20 +21,27 @@ if ( ! function_exists('request'))
* $first_name = request('first_name', 'John'); * $first_name = request('first_name', 'John');
* *
* @param string|null $key Request variable key. * @param string|null $key Request variable key.
* @param mixed $default Default value in case the requested variable has no value. * @param mixed|null $default Default value in case the requested variable has no value.
* *
* @return mixed * @return mixed
* *
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
function request(string $key, $default = NULL) function request(string $key = NULL, $default = NULL)
{ {
/** @var EA_Controller $CI */ /** @var EA_Controller $CI */
$CI = &get_instance(); $CI = &get_instance();
if (empty($key)) if (empty($key))
{ {
throw new InvalidArgumentException('The $key argument cannot be empty.'); $payload = $CI->input->post_get($key);
if (empty($payload))
{
$payload = $CI->input->json($key);
}
return $payload;
} }
return $CI->input->post_get($key) ?? $CI->input->json($key) ?? $default; return $CI->input->post_get($key) ?? $CI->input->json($key) ?? $default;