]> git.mxchange.org Git - friendica.git/commitdiff
Unify request value handling
authorMichael <heluecht@pirati.ca>
Sun, 16 Jan 2022 14:04:20 +0000 (14:04 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 16 Jan 2022 14:04:20 +0000 (14:04 +0000)
16 files changed:
src/BaseModule.php
src/Module/Api/Friendica/Group/Create.php
src/Module/Api/Friendica/Group/Show.php
src/Module/Api/Friendica/Group/Update.php
src/Module/Api/Friendica/Photo/Create.php
src/Module/Api/Friendica/Photo/Update.php
src/Module/Api/GNUSocial/Statusnet/Conversation.php
src/Module/Api/Twitter/DirectMessagesEndpoint.php
src/Module/Api/Twitter/Favorites.php
src/Module/Api/Twitter/Lists/Create.php
src/Module/Api/Twitter/Lists/Destroy.php
src/Module/Api/Twitter/Lists/Statuses.php
src/Module/Api/Twitter/Lists/Update.php
src/Module/Api/Twitter/Statuses/HomeTimeline.php
src/Module/Api/Twitter/Statuses/Mentions.php
src/Module/Api/Twitter/Statuses/PublicTimeline.php

index 06d9da66afb457a43816f7be55f9c359c1078e41..d85e895a17c6252d461e769b3d5aa7d5e7efc000 100644 (file)
@@ -260,19 +260,7 @@ abstract class BaseModule implements ICanHandleRequests
                $request = [];
 
                foreach ($defaults as $parameter => $defaultvalue) {
-                       if (is_string($defaultvalue)) {
-                               $request[$parameter] = (string)($input[$parameter] ?? $defaultvalue);
-                       } elseif (is_int($defaultvalue)) {
-                               $request[$parameter] = filter_var($input[$parameter] ?? $defaultvalue, FILTER_VALIDATE_INT);
-                       } elseif (is_float($defaultvalue)) {
-                               $request[$parameter] = filter_var($input[$parameter] ?? $defaultvalue, FILTER_VALIDATE_FLOAT);
-                       } elseif (is_array($defaultvalue)) {
-                               $request[$parameter] = filter_var($input[$parameter] ?? $defaultvalue, FILTER_DEFAULT, ['flags' => FILTER_FORCE_ARRAY]);
-                       } elseif (is_bool($defaultvalue)) {
-                               $request[$parameter] = filter_var($input[$parameter] ?? $defaultvalue, FILTER_VALIDATE_BOOLEAN);
-                       } else {
-                               $this->logger->notice('Unhandled default value type', ['parameter' => $parameter, 'type' => gettype($defaultvalue)]);
-                       }
+                       $request[$parameter] = $this->getRequestValue($input, $parameter, $defaultvalue);
                }
 
                foreach ($input ?? [] as $parameter => $value) {
@@ -288,6 +276,50 @@ abstract class BaseModule implements ICanHandleRequests
                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($minimal_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($minimal_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;
+       }
+
        /*
         * Functions used to protect against Cross-Site Request Forgery
         * The security token has to base on at least one value that an attacker can't know - here it's the session ID and the private key.
index 2742bae8c6a931e6036c77f5c4aa16bffd8d1080..3b0f030135b5f78743d1f760ed80016986e720e7 100644 (file)
@@ -37,7 +37,7 @@ class Create extends BaseApi
                $uid = BaseApi::getCurrentUserID();
 
                // params
-               $name  = $request['name'] ?? '';
+               $name  = $this->getRequestValue($request, 'name', '');
                $json  = json_decode($request['json'], true);
                $users = $json['user'];
 
index f735e7c116813cb718c6dff04cf588b33e4fa834..ec0bdd134874e7cb01cf204a363cd814e3bd2f37 100644 (file)
@@ -39,7 +39,7 @@ class Show extends BaseApi
                $type = $this->parameters['extension'] ?? '';
 
                // params
-               $gid = $request['gid'] ?? 0;
+               $gid = $this->getRequestValue($request, 'gid', 0);
 
                // get data of the specified group id or all groups if not specified
                if ($gid != 0) {
index 5b18af236b5ed34533a5fc285caa992718954bcb..defbeed1c12bdb6c60582dede304e74d5e8a6032 100644 (file)
@@ -38,8 +38,8 @@ class Update extends BaseApi
                $uid = BaseApi::getCurrentUserID();
 
                // params
-               $gid   = $request['gid']  ?? 0;
-               $name  = $request['name'] ?? '';
+               $gid   = $this->getRequestValue($request, 'gid', 0);
+               $name  = $this->getRequestValue($request, 'name', '');
                $json  = json_decode($request['json'], true);
                $users = $json['user'];
 
index 460bff40f93717122548f947277a13c673bbb738..0eeb455140ec8cf10f718016deb0fe578f1111dd 100644 (file)
@@ -54,13 +54,13 @@ class Create extends BaseApi
                $uid  = BaseApi::getCurrentUserID();
                $type = $this->parameters['extension'] ?? '';
 
-               // input params
-               $desc      = $request['desc']      ?? null;
-               $album     = $request['album']     ?? null;
-               $allow_cid = $request['allow_cid'] ?? null;
-               $deny_cid  = $request['deny_cid' ] ?? null;
-               $allow_gid = $request['allow_gid'] ?? null;
-               $deny_gid  = $request['deny_gid' ] ?? null;
+               // input params 
+               $desc      = $this->getRequestValue($request, 'desc');
+               $album     = $this->getRequestValue($request, 'album');
+               $allow_cid = $this->getRequestValue($request, 'allow_cid');
+               $deny_cid  = $this->getRequestValue($request, 'deny_cid');
+               $allow_gid = $this->getRequestValue($request, 'allow_gid');
+               $deny_gid  = $this->getRequestValue($request, 'deny_gid');
 
                // do several checks on input parameters
                // we do not allow calls without album string
index a7ac91460eb8fbd6130b3b4ad6f78042c122cbb3..ccb9f9150385e3316381d8702c15c12e7eeda167 100644 (file)
@@ -55,14 +55,14 @@ class Update extends BaseApi
                $type = $this->parameters['extension'] ?? '';
 
                // input params
-               $photo_id  = $request['photo_id']  ?? null;
-               $desc      = $request['desc']      ?? null;
-               $album     = $request['album']     ?? null;
-               $album_new = $request['album_new'] ?? null;
-               $allow_cid = $request['allow_cid'] ?? null;
-               $deny_cid  = $request['deny_cid' ] ?? null;
-               $allow_gid = $request['allow_gid'] ?? null;
-               $deny_gid  = $request['deny_gid' ] ?? null;
+               $photo_id  = $this->getRequestValue($request, 'photo_id');
+               $desc      = $this->getRequestValue($request, 'desc');
+               $album     = $this->getRequestValue($request, 'album');
+               $album_new = $this->getRequestValue($request, 'album_new');
+               $allow_cid = $this->getRequestValue($request, 'allow_cid');
+               $deny_cid  = $this->getRequestValue($request, 'deny_cid');
+               $allow_gid = $this->getRequestValue($request, 'allow_gid');
+               $deny_gid  = $this->getRequestValue($request, 'deny_gid');
 
                // do several checks on input parameters
                // we do not allow calls without album string
index cd70cb5022d505277fcef8de5f9d7ce30f8ed1ae..77fdc034baaa5483b25286b85e205fac5fa14a0c 100644 (file)
@@ -41,10 +41,10 @@ class Conversation extends BaseApi
 
                // params
                $id       = $this->parameters['id'] ?? 0;
-               $since_id = $request['since_id']    ?? 0;
-               $max_id   = $request['max_id']      ?? 0;
-               $count    = $request['count']       ?? 20;
-               $page     = $request['page']        ?? 1;
+               $since_id = $this->getRequestValue($request, 'since_id', 0);
+               $max_id   = $this->getRequestValue($request, 'max_id', 0);
+               $count    = $this->getRequestValue($request, 'count', 20);
+               $page     = $this->getRequestValue($request, 'page', 1);
 
                $start = max(0, ($page - 1) * $count);
 
index c88e43036a676f995c47af558044f2ee8956a38a..a1f519975c1452fba04f2bf918172da907ec2e1a 100644 (file)
@@ -58,12 +58,12 @@ abstract class DirectMessagesEndpoint extends BaseApi
        protected function getMessages(array $request, int $uid, array $condition)
        {
                // params
-               $count    = filter_var($request['count'] ?? 20,                FILTER_VALIDATE_INT, ['options' => ['max_range' => 100]]);
-               $page     = filter_var($request['page'] ?? 1,                  FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
-               $since_id = filter_var($request['since_id'] ?? 0,              FILTER_VALIDATE_INT);
-               $max_id   = filter_var($request['max_id'] ?? 0,                FILTER_VALIDATE_INT);
-               $min_id   = filter_var($request['min_id'] ?? 0,                FILTER_VALIDATE_INT);
-               $verbose  = filter_var($request['friendica_verbose'] ?? false, FILTER_VALIDATE_BOOLEAN);
+               $count    = $this->getRequestValue($request, 'count', 20, 1, 100);
+               $page     = $this->getRequestValue($request, 'page', 1, 1);
+               $since_id = $this->getRequestValue($request, 'since_id', 0, 1);
+               $max_id   = $this->getRequestValue($request, 'max_id', 0, 1);
+               $min_id   = $this->getRequestValue($request, 'min_id', 0, 1);
+               $verbose  = $this->getRequestValue($request, 'friendica_verbose', false);
 
                // pagination
                $start = max(0, ($page - 1) * $count);
index 9a7149cab7c774afbbdf4dfb8c2b9eb07dd5749d..56aa26cda7170f452b17d4f763ce1568e569e53a 100644 (file)
@@ -45,10 +45,10 @@ class Favorites extends BaseApi
                Logger::info(BaseApi::LOG_PREFIX . 'for {self}', ['module' => 'api', 'action' => 'favorites']);
 
                // params
-               $since_id = $request['since_id'] ?? 0;
-               $max_id   = $request['max_id']   ?? 0;
-               $count    = $request['count']    ?? 20;
-               $page     = $request['page']     ?? 1;
+               $count    = $this->getRequestValue($request, 'count', 20, 1, 100);
+               $page     = $this->getRequestValue($request, 'page', 1, 1);
+               $since_id = $this->getRequestValue($request, 'since_id', 0, 1);
+               $max_id   = $this->getRequestValue($request, 'max_id', 0, 1);
 
                $start = max(0, ($page - 1) * $count);
 
index 799f01a4c7f8d19c34a9fd66d42cfde9c8c913b0..26e94eb1ab52e04ebdbdb449402f7c29b8757cbb 100644 (file)
@@ -60,7 +60,7 @@ class Create extends BaseApi
                $uid = BaseApi::getCurrentUserID();
 
                // params
-               $name = $request['name'] ?? '';
+               $name = $this->getRequestValue($request, 'name', '');
 
                if ($name == '') {
                        throw new HTTPException\BadRequestException('group name not specified');
index 2bb9642df0390b167466313e6b010846e9ea4b3d..8390bab7758d456cfb53551b3c2240af926809bd 100644 (file)
@@ -60,7 +60,7 @@ class Destroy extends BaseApi
                $uid = BaseApi::getCurrentUserID();
 
                // params
-               $gid = $request['list_id'] ?? 0;
+               $gid = $this->getRequestValue($request, 'list_id', 0);
 
                // error if no gid specified
                if ($gid == 0) {
index 301966a6a4e0ec7ea182bbda0924ba905a1851c7..268ad81941f28db70c9abb35b1e63d56c49c7fff 100644 (file)
@@ -65,12 +65,12 @@ class Statuses extends BaseApi
                }
 
                // params
-               $count           = $request['count']    ?? 20;
-               $page            = $request['page']     ?? 1;
-               $since_id        = $request['since_id'] ?? 0;
-               $max_id          = $request['max_id']   ?? 0;
-               $exclude_replies = (!empty($request['exclude_replies']) ? 1 : 0);
-               $conversation_id = $request['conversation_id'] ?? 0;
+               $count           = $this->getRequestValue($request, 'count', 20);
+               $page            = $this->getRequestValue($request, 'page', 1);
+               $since_id        = $this->getRequestValue($request, 'since_id', 0);
+               $max_id          = $this->getRequestValue($request, 'max_id', 0);
+               $exclude_replies = $this->getRequestValue($request, 'exclude_replies', false);
+               $conversation_id = $this->getRequestValue($request, 'conversation_id', 0);
 
                $start = max(0, ($page - 1) * $count);
 
@@ -83,7 +83,7 @@ class Statuses extends BaseApi
                        $condition[0] .= " AND `id` <= ?";
                        $condition[] = $max_id;
                }
-               if ($exclude_replies > 0) {
+               if ($exclude_replies) {
                        $condition[0] .= ' AND `gravity` = ?';
                        $condition[] = GRAVITY_PARENT;
                }
index 34a0b658fff50b43cf669598fd3e3224097af765..e7929e77d22f3b990bec417aa67c631acef0d839 100644 (file)
@@ -60,8 +60,8 @@ class Update extends BaseApi
                $uid = BaseApi::getCurrentUserID();
 
                // params
-               $gid  = $request['list_id'] ?? 0;
-               $name = $request['name'] ?? '';
+               $gid  = $this->getRequestValue($request, 'list_id', 0);
+               $name = $this->getRequestValue($request, 'name', '');
 
                // error if no gid specified
                if ($gid == 0) {
index 8a54aeda63852843b50c305638a2196428801970..c45342a52336bd279bd90586a1c10fca5f49e9c1 100644 (file)
@@ -43,12 +43,12 @@ class HomeTimeline extends BaseApi
                // get last network messages
 
                // params
-               $count           = $_REQUEST['count']    ?? 20;
-               $page            = $_REQUEST['page']     ?? 0;
-               $since_id        = $_REQUEST['since_id'] ?? 0;
-               $max_id          = $_REQUEST['max_id']   ?? 0;
-               $exclude_replies = !empty($_REQUEST['exclude_replies']);
-               $conversation_id = $_REQUEST['conversation_id'] ?? 0;
+               $count           = $this->getRequestValue($request, 'count', 20, 1, 100);
+               $page            = $this->getRequestValue($request, 'page', 1, 1);
+               $since_id        = $this->getRequestValue($request, 'since_id', 0, 1);
+               $max_id          = $this->getRequestValue($request, 'max_id', 0, 1);
+               $exclude_replies = $this->getRequestValue($request, 'exclude_replies', false);
+               $conversation_id = $this->getRequestValue($request, 'conversation_id', 0);
 
                $start = max(0, ($page - 1) * $count);
 
index cb66a49f1b5cef570f98bdfba0fb3efedd648923..954aca57b4c3cec7a417c03e8dd94834f7f46223 100644 (file)
@@ -42,10 +42,10 @@ class Mentions extends BaseApi
                // get last network messages
 
                // params
-               $since_id = $_REQUEST['since_id'] ?? 0;
-               $max_id   = $_REQUEST['max_id']   ?? 0;
-               $count    = $_REQUEST['count']    ?? 20;
-               $page     = $_REQUEST['page']     ?? 1;
+               $count    = $this->getRequestValue($request, 'count', 20, 1, 100);
+               $page     = $this->getRequestValue($request, 'page', 1, 1);
+               $since_id = $this->getRequestValue($request, 'since_id', 0, 1);
+               $max_id   = $this->getRequestValue($request, 'max_id', 0, 1);
 
                $start = max(0, ($page - 1) * $count);
 
index 5508d3b09f4b84ed33d2f60a1648f5b26ab104d6..53950573beeac57bb2a39b8c389ce551d1c58a1e 100644 (file)
@@ -41,12 +41,12 @@ class PublicTimeline extends BaseApi
                // get last network messages
 
                // params
-               $count           = $_REQUEST['count']    ?? 20;
-               $page            = $_REQUEST['page']     ?? 1;
-               $since_id        = $_REQUEST['since_id'] ?? 0;
-               $max_id          = $_REQUEST['max_id']   ?? 0;
-               $exclude_replies = (!empty($_REQUEST['exclude_replies']) ? 1 : 0);
-               $conversation_id = $_REQUEST['conversation_id'] ?? 0;
+               $count           = $this->getRequestValue($request, 'count', 20, 1, 100);
+               $page            = $this->getRequestValue($request, 'page', 1, 1);
+               $since_id        = $this->getRequestValue($request, 'since_id', 0, 1);
+               $max_id          = $this->getRequestValue($request, 'max_id', 0, 1);
+               $exclude_replies = $this->getRequestValue($request, 'exclude_replies', false);
+               $conversation_id = $this->getRequestValue($request, 'conversation_id', 0);
 
                $start = max(0, ($page - 1) * $count);