From 38400e62e76c15872746263cc165178309a2e5dc Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 22 Nov 2019 11:19:29 +0100 Subject: [inc/Request] add REQUIRED flag for automatic error message If $default is set to Request::REQUIRED for get() post() or any(), the method will display an error message and halt execution if the given parameter is either missing or empty. --- inc/request.inc.php | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/inc/request.inc.php b/inc/request.inc.php index 515f723d..a2dc9711 100644 --- a/inc/request.inc.php +++ b/inc/request.inc.php @@ -6,6 +6,8 @@ class Request { + const REQUIRED = "\0\1\2REQ\0\1\2"; + /** * * @param string $key Key of field to get from $_GET @@ -15,9 +17,7 @@ class Request */ public static function get($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); } /** @@ -28,9 +28,7 @@ class Request */ public static function post($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); } /** @@ -41,9 +39,7 @@ class Request */ public static function any($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); } /** @@ -62,4 +58,21 @@ class Request return $_SERVER['REQUEST_METHOD'] === 'GET'; } + private static function handle(&$array, $key, $default, $type) + { + if (!isset($array[$key])) { + if ($default === self::REQUIRED) { + Message::addError('main.parameter-missing', $key); + Util::redirect('?do=' . $_REQUEST['do']); + } + return $default; + } + if ($default === self::REQUIRED && empty($array[$key])) { + Message::addError('main.parameter-empty', $key); + Util::redirect('?do=' . $_REQUEST['do']); + } + if ($type !== false) settype($array[$key], $type); + return $array[$key]; + } + } -- cgit v1.2.3-55-g7522