+ $timestamp = microtime(true);
+ // "rawContent" is especially meant for technical endpoints.
+ // This endpoint doesn't need any theme initialization or other comparable stuff.
+ $this->rawContent($request);
+
+ try {
+ $arr = ['content' => ''];
+ Hook::callAll(static::class . '_mod_content', $arr);
+ $this->response->addContent($arr['content']);
+ $this->response->addContent($this->content($request));
+ } catch (HTTPException $e) {
+ $this->response->addContent((new ModuleHTTPException())->content($e));
+ } finally {
+ $this->profiler->set(microtime(true) - $timestamp, 'content');
+ }
+
+ return $this->response->generate();
+ }
+
+ /**
+ * Checks request inputs and sets default parameters
+ *
+ * @param array $defaults Associative array of expected request keys and their default typed value. A null
+ * value will remove the request key from the resulting value array.
+ * @param array $input Custom REQUEST array, superglobal instead
+ *
+ * @return array Request data
+ */
+ protected function checkDefaults(array $defaults, array $input): array
+ {
+ $request = [];
+
+ foreach ($defaults as $parameter => $defaultvalue) {
+ $request[$parameter] = $this->getRequestValue($input, $parameter, $defaultvalue);
+ }
+
+ foreach ($input ?? [] as $parameter => $value) {
+ if ($parameter == 'pagename') {
+ continue;
+ }
+ if (!in_array($parameter, array_keys($defaults))) {
+ $this->logger->notice('Unhandled request field', ['parameter' => $parameter, 'value' => $value, 'command' => $this->args->getCommand()]);
+ }
+ }
+
+ $this->logger->debug('Got request parameters', ['request' => $request, 'command' => $this->args->getCommand()]);
+ return $request;
+ }
+
+ /**
+ * Fetch a request value and apply default values and check against minimal and maximal values
+ *
+ * @param array $input
+ * @param string $parameter
+ * @param mixed $default
+ * @param mixed $minimal_value
+ * @param mixed $maximum_value
+ * @return mixed
+ */
+ public function getRequestValue(array $input, string $parameter, $default = null, $minimal_value = null, $maximum_value = null)
+ {
+ if (is_string($default)) {
+ $value = (string)($input[$parameter] ?? $default);
+ } elseif (is_int($default)) {
+ $value = filter_var($input[$parameter] ?? $default, FILTER_VALIDATE_INT);
+ if (!is_null($minimal_value)) {
+ $value = max(filter_var($minimal_value, FILTER_VALIDATE_INT), $value);
+ }
+ if (!is_null($maximum_value)) {
+ $value = min(filter_var($maximum_value, FILTER_VALIDATE_INT), $value);
+ }
+ } elseif (is_float($default)) {
+ $value = filter_var($input[$parameter] ?? $default, FILTER_VALIDATE_FLOAT);
+ if (!is_null($minimal_value)) {
+ $value = max(filter_var($minimal_value, FILTER_VALIDATE_FLOAT), $value);
+ }
+ if (!is_null($maximum_value)) {
+ $value = min(filter_var($maximum_value, FILTER_VALIDATE_FLOAT), $value);
+ }
+ } elseif (is_array($default)) {
+ $value = filter_var($input[$parameter] ?? $default, FILTER_DEFAULT, ['flags' => FILTER_FORCE_ARRAY]);
+ } elseif (is_bool($default)) {
+ $value = filter_var($input[$parameter] ?? $default, FILTER_VALIDATE_BOOLEAN);
+ } elseif (is_null($default)) {
+ $value = $input[$parameter] ?? null;
+ } else {
+ $this->logger->notice('Unhandled default value type', ['parameter' => $parameter, 'type' => gettype($default)]);
+ $value = null;
+ }
+
+ return $value;