diff options
Diffstat (limited to 'inc/request.inc.php')
| -rw-r--r-- | inc/request.inc.php | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/inc/request.inc.php b/inc/request.inc.php index 3f1d96c6..0b0600da 100644 --- a/inc/request.inc.php +++ b/inc/request.inc.php @@ -99,4 +99,44 @@ class Request return $array[$key]; } + /** + * Processes post parameters based on specified limits and assigns the processed values to the output array. + * $params is expected to have one or more keys specifying a type (int, string, ...), and each of them + * an array with field name as key, and then an array as value that can have optional + * keys 'default', 'min' and 'max', as well as 'enum' (array of allowed values). + * + * @param array $out Reference to an array where the processed parameter values will be stored. + * @param array $params An array containing the parameters to be processed, their types, and limits. + */ + public static function processPostParameters(array &$out, array $params, ?string $source = 'POST'): void + { + if ($source === 'GET') { + $input =& $_GET; + } elseif ($source === 'POST') { + $input =& $_POST; + } elseif ($source === 'REQUEST') { + $input =& $_REQUEST; + } elseif ($source === null) { + $input = []; + } else { + ErrorHandler::traceError("Invalid parameter source: '$source'"); + } + foreach ($params as $type => $list) { + foreach ($list as $field => $limits) { + $default = $limits['default'] ?? false; + $value = self::handle($input, $field, $default, $type); + if (isset($limits['min']) && $value < $limits['min']) { + $value = $limits['min']; + } + if (isset($limits['max']) && $value > $limits['max']) { + $value = $limits['max']; + } + if (isset($limits['enum']) && !in_array($value, $limits['enum'])) { + $value = array_shift($limits['enum']); + } + $out[$field] = $value; + } + } + } + } |
