Back'); Render::output(); exit; } if (!isset($array[$key])) return $default; if ($type !== false) { settype($array[$key], $type); } 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) { if ($type === 'bool') { // An unchecked checkbox will not have its key in the post data, so self::handle would fall back // to the default value. If the default is true, this would mean the parameter would be set to // true, which is incorrect. $default = false; } else { $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 = $limits['default'] ?? array_shift($limits['enum']); } $out[$field] = $value; } } } }