summaryrefslogtreecommitdiffstats
path: root/inc/request.inc.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/request.inc.php')
-rw-r--r--inc/request.inc.php51
1 files changed, 37 insertions, 14 deletions
diff --git a/inc/request.inc.php b/inc/request.inc.php
index 515f723d..cd782d99 100644
--- a/inc/request.inc.php
+++ b/inc/request.inc.php
@@ -1,5 +1,7 @@
<?php
+declare(strict_types=1);
+
/**
* Wrapper for getting fields from the request (GET, POST, ...)
*/
@@ -7,17 +9,25 @@ class Request
{
/**
+ * Required and not empty
+ */
+ const REQUIRED = "\0\1\2REQ\0\1\2";
+
+ /**
+ * Required, but might be empty
+ */
+ const REQUIRED_EMPTY = "\0\3\4REQ\0\3\4";
+
+ /**
*
* @param string $key Key of field to get from $_GET
* @param string $default Value to return if $_GET does not contain $key
* @param string $type if the parameter exists, cast it to given type
* @return mixed Field from $_GET, or $default if not set
*/
- public static function get($key, $default = false, $type = false)
+ public static function get(string $key, $default = false, $type = false)
{
- if (!isset($_GET[$key])) return $default;
- if ($type !== false) settype($_GET[$key], $type);
- return $_GET[$key];
+ return self::handle($_GET, $key, $default, $type);
}
/**
@@ -26,11 +36,9 @@ class Request
* @param string $default Value to return if $_POST does not contain $key
* @return mixed Field from $_POST, or $default if not set
*/
- public static function post($key, $default = false, $type = false)
+ public static function post(string $key, $default = false, $type = false)
{
- if (!isset($_POST[$key])) return $default;
- if ($type !== false) settype($_POST[$key], $type);
- return $_POST[$key];
+ return self::handle($_POST, $key, $default, $type);
}
/**
@@ -39,17 +47,15 @@ class Request
* @param string $default Value to return if $_REQUEST does not contain $key
* @return mixed Field from $_REQUEST, or $default if not set
*/
- public static function any($key, $default = false, $type = false)
+ public static function any(string $key, $default = false, $type = false)
{
- if (!isset($_REQUEST[$key])) return $default;
- if ($type !== false) settype($_REQUEST[$key], $type);
- return $_REQUEST[$key];
+ return self::handle($_REQUEST, $key, $default, $type);
}
/**
* @return true iff the request is a POST request
*/
- public static function isPost()
+ public static function isPost(): bool
{
return $_SERVER['REQUEST_METHOD'] === 'POST';
}
@@ -57,9 +63,26 @@ class Request
/**
* @return true iff the request is a GET request
*/
- public static function isGet()
+ public static function isGet(): bool
{
return $_SERVER['REQUEST_METHOD'] === 'GET';
}
+ private static function handle(&$array, $key, $default, $type)
+ {
+ if (!array_key_exists($key, $array)) {
+ if ($default === self::REQUIRED || $default === self::REQUIRED_EMPTY) {
+ Message::addError('main.parameter-missing', $key);
+ Util::redirect('?do=' . $_REQUEST['do']);
+ }
+ return $default;
+ }
+ if ($default === self::REQUIRED && $array[$key] === '') {
+ Message::addError('main.parameter-empty', $key);
+ Util::redirect('?do=' . $_REQUEST['do']);
+ }
+ if ($type !== false) settype($array[$key], $type);
+ return $array[$key];
+ }
+
}