summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2019-11-22 11:19:29 +0100
committerSimon Rettberg2019-11-22 11:19:29 +0100
commit38400e62e76c15872746263cc165178309a2e5dc (patch)
tree59e4fb341ac4e7c16342aa3e4ac2c4c57d409217
parent[statistics] api: Update DB on client ipaddr change (diff)
downloadslx-admin-38400e62e76c15872746263cc165178309a2e5dc.tar.gz
slx-admin-38400e62e76c15872746263cc165178309a2e5dc.tar.xz
slx-admin-38400e62e76c15872746263cc165178309a2e5dc.zip
[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.
-rw-r--r--inc/request.inc.php31
1 files 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];
+ }
+
}