From f580d8e5c022f42f7dbe8465dff668d240c1da09 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 28 Nov 2021 13:22:27 +0100 Subject: [PATCH] extract "BaseApi::checkDefaults()" method for later usage --- src/BaseModule.php | 42 +++++++++++++++++++ src/Module/Api/Friendica/Activity.php | 2 +- .../Api/Friendica/DirectMessages/Setseen.php | 2 +- src/Module/Api/Friendica/Events/Index.php | 2 +- src/Module/Api/Friendica/Group/Delete.php | 2 +- src/Module/Api/Friendica/Photo/Delete.php | 2 +- .../Api/Friendica/Photoalbum/Delete.php | 2 +- .../Api/Friendica/Photoalbum/Update.php | 2 +- .../Api/Mastodon/Accounts/Followers.php | 2 +- .../Api/Mastodon/Accounts/Following.php | 2 +- src/Module/Api/Mastodon/Accounts/Note.php | 2 +- .../Api/Mastodon/Accounts/Relationships.php | 2 +- src/Module/Api/Mastodon/Accounts/Search.php | 2 +- src/Module/Api/Mastodon/Accounts/Statuses.php | 2 +- src/Module/Api/Mastodon/Apps.php | 2 +- src/Module/Api/Mastodon/Blocks.php | 2 +- src/Module/Api/Mastodon/Bookmarks.php | 2 +- src/Module/Api/Mastodon/Conversations.php | 2 +- src/Module/Api/Mastodon/Directory.php | 2 +- src/Module/Api/Mastodon/Favourited.php | 2 +- src/Module/Api/Mastodon/FollowRequests.php | 2 +- src/Module/Api/Mastodon/Lists.php | 4 +- src/Module/Api/Mastodon/Lists/Accounts.php | 2 +- src/Module/Api/Mastodon/Media.php | 2 +- src/Module/Api/Mastodon/Mutes.php | 2 +- src/Module/Api/Mastodon/Notifications.php | 2 +- src/Module/Api/Mastodon/PushSubscription.php | 4 +- src/Module/Api/Mastodon/ScheduledStatuses.php | 2 +- src/Module/Api/Mastodon/Search.php | 2 +- src/Module/Api/Mastodon/Statuses.php | 2 +- src/Module/Api/Mastodon/Statuses/Context.php | 2 +- src/Module/Api/Mastodon/Suggestions.php | 2 +- src/Module/Api/Mastodon/Timelines/Direct.php | 2 +- src/Module/Api/Mastodon/Timelines/Home.php | 2 +- .../Api/Mastodon/Timelines/ListTimeline.php | 2 +- .../Api/Mastodon/Timelines/PublicTimeline.php | 2 +- src/Module/Api/Mastodon/Timelines/Tag.php | 2 +- src/Module/Api/Mastodon/Trends.php | 2 +- src/Module/BaseApi.php | 32 +------------- src/Module/OAuth/Authorize.php | 2 +- src/Module/OAuth/Revoke.php | 2 +- src/Module/OAuth/Token.php | 2 +- 42 files changed, 86 insertions(+), 72 deletions(-) diff --git a/src/BaseModule.php b/src/BaseModule.php index 86dd5e7c06..a74c027131 100644 --- a/src/BaseModule.php +++ b/src/BaseModule.php @@ -241,6 +241,48 @@ abstract class BaseModule implements ICanHandleRequests 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) { + if (is_string($defaultvalue)) { + $request[$parameter] = $input[$parameter] ?? $defaultvalue; + } elseif (is_int($defaultvalue)) { + $request[$parameter] = (int)($input[$parameter] ?? $defaultvalue); + } elseif (is_float($defaultvalue)) { + $request[$parameter] = (float)($input[$parameter] ?? $defaultvalue); + } elseif (is_array($defaultvalue)) { + $request[$parameter] = $input[$parameter] ?? []; + } elseif (is_bool($defaultvalue)) { + $request[$parameter] = in_array(strtolower($input[$parameter] ?? ''), ['true', '1']); + } else { + $this->logger->notice('Unhandled default value type', ['parameter' => $parameter, 'type' => gettype($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; + } + /* * 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. diff --git a/src/Module/Api/Friendica/Activity.php b/src/Module/Api/Friendica/Activity.php index 070dc452ca..6826eb3784 100644 --- a/src/Module/Api/Friendica/Activity.php +++ b/src/Module/Api/Friendica/Activity.php @@ -45,7 +45,7 @@ class Activity extends BaseApi self::checkAllowedScope(self::SCOPE_WRITE); $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'id' => 0, // Id of the post ], $request); diff --git a/src/Module/Api/Friendica/DirectMessages/Setseen.php b/src/Module/Api/Friendica/DirectMessages/Setseen.php index a6a4875b83..5fcb87b326 100644 --- a/src/Module/Api/Friendica/DirectMessages/Setseen.php +++ b/src/Module/Api/Friendica/DirectMessages/Setseen.php @@ -35,7 +35,7 @@ class Setseen extends BaseApi self::checkAllowedScope(self::SCOPE_WRITE); $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'id' => 0, // Id of the direct message ], $request); diff --git a/src/Module/Api/Friendica/Events/Index.php b/src/Module/Api/Friendica/Events/Index.php index 6e3daa7706..fed7cc54d5 100644 --- a/src/Module/Api/Friendica/Events/Index.php +++ b/src/Module/Api/Friendica/Events/Index.php @@ -38,7 +38,7 @@ class Index extends BaseApi self::checkAllowedScope(self::SCOPE_READ); $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'since_id' => 0, 'count' => 0, ], $request); diff --git a/src/Module/Api/Friendica/Group/Delete.php b/src/Module/Api/Friendica/Group/Delete.php index f0d94ff3e0..9b63cd7e6d 100644 --- a/src/Module/Api/Friendica/Group/Delete.php +++ b/src/Module/Api/Friendica/Group/Delete.php @@ -37,7 +37,7 @@ class Delete extends BaseApi self::checkAllowedScope(self::SCOPE_WRITE); $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'gid' => 0, 'name' => '' ], $request); diff --git a/src/Module/Api/Friendica/Photo/Delete.php b/src/Module/Api/Friendica/Photo/Delete.php index d4288b88be..5861723b62 100644 --- a/src/Module/Api/Friendica/Photo/Delete.php +++ b/src/Module/Api/Friendica/Photo/Delete.php @@ -36,7 +36,7 @@ class Delete extends BaseApi { $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'photo_id' => '', // Photo id ], $request); diff --git a/src/Module/Api/Friendica/Photoalbum/Delete.php b/src/Module/Api/Friendica/Photoalbum/Delete.php index 8a45de2d36..8cb719cd1e 100644 --- a/src/Module/Api/Friendica/Photoalbum/Delete.php +++ b/src/Module/Api/Friendica/Photoalbum/Delete.php @@ -39,7 +39,7 @@ class Delete extends BaseApi self::checkAllowedScope(self::SCOPE_WRITE); $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'album' => '', // Album name ], $request); diff --git a/src/Module/Api/Friendica/Photoalbum/Update.php b/src/Module/Api/Friendica/Photoalbum/Update.php index 2c1e5e8786..d9fc760d63 100644 --- a/src/Module/Api/Friendica/Photoalbum/Update.php +++ b/src/Module/Api/Friendica/Photoalbum/Update.php @@ -37,7 +37,7 @@ class Update extends BaseApi self::checkAllowedScope(self::SCOPE_WRITE); $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'album' => '', // Current album name 'album_new' => '', // New album name ], $request); diff --git a/src/Module/Api/Mastodon/Accounts/Followers.php b/src/Module/Api/Mastodon/Accounts/Followers.php index ba24625c47..3e06528bd7 100644 --- a/src/Module/Api/Mastodon/Accounts/Followers.php +++ b/src/Module/Api/Mastodon/Accounts/Followers.php @@ -48,7 +48,7 @@ class Followers extends BaseApi DI::mstdnError()->RecordNotFound(); } - $request = self::getRequest([ + $request = $this->getRequest([ 'max_id' => 0, // Return results older than this id 'since_id' => 0, // Return results newer than this id 'min_id' => 0, // Return results immediately newer than id diff --git a/src/Module/Api/Mastodon/Accounts/Following.php b/src/Module/Api/Mastodon/Accounts/Following.php index a80af6ea99..2aace2683e 100644 --- a/src/Module/Api/Mastodon/Accounts/Following.php +++ b/src/Module/Api/Mastodon/Accounts/Following.php @@ -48,7 +48,7 @@ class Following extends BaseApi DI::mstdnError()->RecordNotFound(); } - $request = self::getRequest([ + $request = $this->getRequest([ 'max_id' => 0, // Return results older than this id 'since_id' => 0, // Return results newer than this id 'min_id' => 0, // Return results immediately newer than id diff --git a/src/Module/Api/Mastodon/Accounts/Note.php b/src/Module/Api/Mastodon/Accounts/Note.php index 7135cb930e..eb97a8856d 100644 --- a/src/Module/Api/Mastodon/Accounts/Note.php +++ b/src/Module/Api/Mastodon/Accounts/Note.php @@ -41,7 +41,7 @@ class Note extends BaseApi DI::mstdnError()->UnprocessableEntity(); } - $request = self::getRequest([ + $request = $this->getRequest([ 'comment' => '', ], $request); diff --git a/src/Module/Api/Mastodon/Accounts/Relationships.php b/src/Module/Api/Mastodon/Accounts/Relationships.php index b56fd5a946..6fce26fbb8 100644 --- a/src/Module/Api/Mastodon/Accounts/Relationships.php +++ b/src/Module/Api/Mastodon/Accounts/Relationships.php @@ -39,7 +39,7 @@ class Relationships extends BaseApi self::checkAllowedScope(self::SCOPE_READ); $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'id' => [], ], $request); diff --git a/src/Module/Api/Mastodon/Accounts/Search.php b/src/Module/Api/Mastodon/Accounts/Search.php index 53569b79ac..536c68850b 100644 --- a/src/Module/Api/Mastodon/Accounts/Search.php +++ b/src/Module/Api/Mastodon/Accounts/Search.php @@ -42,7 +42,7 @@ class Search extends BaseApi self::checkAllowedScope(self::SCOPE_READ); $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'q' => '', // What to search for 'limit' => 40, // Maximum number of results. Defaults to 40. 'resolve' => false, // Attempt WebFinger lookup. Defaults to false. Use this when q is an exact address. diff --git a/src/Module/Api/Mastodon/Accounts/Statuses.php b/src/Module/Api/Mastodon/Accounts/Statuses.php index 609bcdb202..0ab5a2a50a 100644 --- a/src/Module/Api/Mastodon/Accounts/Statuses.php +++ b/src/Module/Api/Mastodon/Accounts/Statuses.php @@ -52,7 +52,7 @@ class Statuses extends BaseApi DI::mstdnError()->RecordNotFound(); } - $request = self::getRequest([ + $request = $this->getRequest([ 'only_media' => false, // Show only statuses with media attached? Defaults to false. 'max_id' => 0, // Return results older than this id 'since_id' => 0, // Return results newer than this id diff --git a/src/Module/Api/Mastodon/Apps.php b/src/Module/Api/Mastodon/Apps.php index 5811e38733..3f24810bbf 100644 --- a/src/Module/Api/Mastodon/Apps.php +++ b/src/Module/Api/Mastodon/Apps.php @@ -37,7 +37,7 @@ class Apps extends BaseApi */ protected function post(array $request = [], array $post = []) { - $request = self::getRequest([ + $request = $this->getRequest([ 'client_name' => '', 'redirect_uris' => '', 'scopes' => 'read', diff --git a/src/Module/Api/Mastodon/Blocks.php b/src/Module/Api/Mastodon/Blocks.php index 0a1e9506ff..a299185aeb 100644 --- a/src/Module/Api/Mastodon/Blocks.php +++ b/src/Module/Api/Mastodon/Blocks.php @@ -48,7 +48,7 @@ class Blocks extends BaseApi DI::mstdnError()->RecordNotFound(); } - $request = self::getRequest([ + $request = $this->getRequest([ 'max_id' => 0, // Return results older than this id 'since_id' => 0, // Return results newer than this id 'min_id' => 0, // Return results immediately newer than id diff --git a/src/Module/Api/Mastodon/Bookmarks.php b/src/Module/Api/Mastodon/Bookmarks.php index d56ad5a883..a3d00dcd82 100644 --- a/src/Module/Api/Mastodon/Bookmarks.php +++ b/src/Module/Api/Mastodon/Bookmarks.php @@ -41,7 +41,7 @@ class Bookmarks extends BaseApi self::checkAllowedScope(self::SCOPE_READ); $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'limit' => 20, // Maximum number of results to return. Defaults to 20. 'max_id' => 0, // Return results older than id 'since_id' => 0, // Return results newer than id diff --git a/src/Module/Api/Mastodon/Conversations.php b/src/Module/Api/Mastodon/Conversations.php index 5ff51deb14..125e13e609 100644 --- a/src/Module/Api/Mastodon/Conversations.php +++ b/src/Module/Api/Mastodon/Conversations.php @@ -54,7 +54,7 @@ class Conversations extends BaseApi self::checkAllowedScope(self::SCOPE_READ); $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'limit' => 20, // Maximum number of results. Defaults to 20. Max 40. 'max_id' => 0, // Return results older than this ID. Use HTTP Link header to paginate. 'since_id' => 0, // Return results newer than this ID. Use HTTP Link header to paginate. diff --git a/src/Module/Api/Mastodon/Directory.php b/src/Module/Api/Mastodon/Directory.php index 740d5d92ae..67104dc28f 100644 --- a/src/Module/Api/Mastodon/Directory.php +++ b/src/Module/Api/Mastodon/Directory.php @@ -41,7 +41,7 @@ class Directory extends BaseApi */ protected function rawContent(array $request = []) { - $request = self::getRequest([ + $request = $this->getRequest([ 'offset' => 0, // How many accounts to skip before returning results. Default 0. 'limit' => 40, // How many accounts to load. Default 40. 'order' => 'active', // active to sort by most recently posted statuses (default) or new to sort by most recently created profiles. diff --git a/src/Module/Api/Mastodon/Favourited.php b/src/Module/Api/Mastodon/Favourited.php index c1ac2a89cc..243d119f96 100644 --- a/src/Module/Api/Mastodon/Favourited.php +++ b/src/Module/Api/Mastodon/Favourited.php @@ -42,7 +42,7 @@ class Favourited extends BaseApi self::checkAllowedScope(self::SCOPE_READ); $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'limit' => 20, // Maximum number of results to return. Defaults to 20. 'min_id' => 0, // Return results immediately newer than id 'max_id' => 0, // Return results older than id diff --git a/src/Module/Api/Mastodon/FollowRequests.php b/src/Module/Api/Mastodon/FollowRequests.php index 739c53c491..eead8f96c1 100644 --- a/src/Module/Api/Mastodon/FollowRequests.php +++ b/src/Module/Api/Mastodon/FollowRequests.php @@ -87,7 +87,7 @@ class FollowRequests extends BaseApi self::checkAllowedScope(self::SCOPE_READ); $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'min_id' => 0, 'max_id' => 0, 'limit' => 40, // Maximum number of results to return. Defaults to 40. Paginate using the HTTP Link header. diff --git a/src/Module/Api/Mastodon/Lists.php b/src/Module/Api/Mastodon/Lists.php index 53a9ac0128..3ab41329cd 100644 --- a/src/Module/Api/Mastodon/Lists.php +++ b/src/Module/Api/Mastodon/Lists.php @@ -56,7 +56,7 @@ class Lists extends BaseApi self::checkAllowedScope(self::SCOPE_WRITE); $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'title' => '', ], $request); @@ -76,7 +76,7 @@ class Lists extends BaseApi public function put() { - $request = self::getRequest([ + $request = $this->getRequest([ 'title' => '', // The title of the list to be updated. 'replies_policy' => '', // One of: "followed", "list", or "none". ]); diff --git a/src/Module/Api/Mastodon/Lists/Accounts.php b/src/Module/Api/Mastodon/Lists/Accounts.php index 151da9cc62..9ab676af02 100644 --- a/src/Module/Api/Mastodon/Lists/Accounts.php +++ b/src/Module/Api/Mastodon/Lists/Accounts.php @@ -61,7 +61,7 @@ class Accounts extends BaseApi DI::mstdnError()->RecordNotFound(); } - $request = self::getRequest([ + $request = $this->getRequest([ 'max_id' => 0, // Return results older than this id 'since_id' => 0, // Return results newer than this id 'min_id' => 0, // Return results immediately newer than id diff --git a/src/Module/Api/Mastodon/Media.php b/src/Module/Api/Mastodon/Media.php index 65663882d4..24d2a3bf8c 100644 --- a/src/Module/Api/Mastodon/Media.php +++ b/src/Module/Api/Mastodon/Media.php @@ -58,7 +58,7 @@ class Media extends BaseApi self::checkAllowedScope(self::SCOPE_WRITE); $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'file' => [], // The file to be attached, using multipart form data. 'thumbnail' => [], // The custom thumbnail of the media to be attached, using multipart form data. 'description' => '', // A plain-text description of the media, for accessibility purposes. diff --git a/src/Module/Api/Mastodon/Mutes.php b/src/Module/Api/Mastodon/Mutes.php index 9d612a455f..96a0c7eac4 100644 --- a/src/Module/Api/Mastodon/Mutes.php +++ b/src/Module/Api/Mastodon/Mutes.php @@ -48,7 +48,7 @@ class Mutes extends BaseApi DI::mstdnError()->RecordNotFound(); } - $request = self::getRequest([ + $request = $this->getRequest([ 'max_id' => 0, // Return results older than this id 'since_id' => 0, // Return results newer than this id 'min_id' => 0, // Return results immediately newer than id diff --git a/src/Module/Api/Mastodon/Notifications.php b/src/Module/Api/Mastodon/Notifications.php index f57c0268e3..7527286e09 100644 --- a/src/Module/Api/Mastodon/Notifications.php +++ b/src/Module/Api/Mastodon/Notifications.php @@ -55,7 +55,7 @@ class Notifications extends BaseApi } } - $request = self::getRequest([ + $request = $this->getRequest([ 'max_id' => 0, // Return results older than this ID 'since_id' => 0, // Return results newer than this ID 'min_id' => 0, // Return results immediately newer than this ID diff --git a/src/Module/Api/Mastodon/PushSubscription.php b/src/Module/Api/Mastodon/PushSubscription.php index b4b152e5c5..4978e980a8 100644 --- a/src/Module/Api/Mastodon/PushSubscription.php +++ b/src/Module/Api/Mastodon/PushSubscription.php @@ -39,7 +39,7 @@ class PushSubscription extends BaseApi $uid = self::getCurrentUserID(); $application = self::getCurrentApplication(); - $request = self::getRequest([ + $request = $this->getRequest([ 'subscription' => [], 'data' => [], ], $request); @@ -72,7 +72,7 @@ class PushSubscription extends BaseApi $uid = self::getCurrentUserID(); $application = self::getCurrentApplication(); - $request = self::getRequest([ + $request = $this->getRequest([ 'data' => [], ]); diff --git a/src/Module/Api/Mastodon/ScheduledStatuses.php b/src/Module/Api/Mastodon/ScheduledStatuses.php index 644aea874c..f6da23aa1a 100644 --- a/src/Module/Api/Mastodon/ScheduledStatuses.php +++ b/src/Module/Api/Mastodon/ScheduledStatuses.php @@ -71,7 +71,7 @@ class ScheduledStatuses extends BaseApi System::jsonExit(DI::mstdnScheduledStatus()->createFromDelayedPostId($this->parameters['id'], $uid)->toArray()); } - $request = self::getRequest([ + $request = $this->getRequest([ 'limit' => 20, // Max number of results to return. Defaults to 20. 'max_id' => 0, // Return results older than ID 'since_id' => 0, // Return results newer than ID diff --git a/src/Module/Api/Mastodon/Search.php b/src/Module/Api/Mastodon/Search.php index 1730db68c3..dcc9969f79 100644 --- a/src/Module/Api/Mastodon/Search.php +++ b/src/Module/Api/Mastodon/Search.php @@ -45,7 +45,7 @@ class Search extends BaseApi self::checkAllowedScope(self::SCOPE_READ); $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'account_id' => 0, // If provided, statuses returned will be authored only by this account 'max_id' => 0, // Return results older than this id 'min_id' => 0, // Return results immediately newer than this id diff --git a/src/Module/Api/Mastodon/Statuses.php b/src/Module/Api/Mastodon/Statuses.php index 54e85d2a6d..05851a983a 100644 --- a/src/Module/Api/Mastodon/Statuses.php +++ b/src/Module/Api/Mastodon/Statuses.php @@ -46,7 +46,7 @@ class Statuses extends BaseApi self::checkAllowedScope(self::SCOPE_WRITE); $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'status' => '', // Text content of the status. If media_ids is provided, this becomes optional. Attaching a poll is optional while status is provided. 'media_ids' => [], // Array of Attachment ids to be attached as media. If provided, status becomes optional, and poll cannot be used. 'poll' => [], // Poll data. If provided, media_ids cannot be used, and poll[expires_in] must be provided. diff --git a/src/Module/Api/Mastodon/Statuses/Context.php b/src/Module/Api/Mastodon/Statuses/Context.php index 4193b8fa92..f463f46b4b 100644 --- a/src/Module/Api/Mastodon/Statuses/Context.php +++ b/src/Module/Api/Mastodon/Statuses/Context.php @@ -43,7 +43,7 @@ class Context extends BaseApi DI::mstdnError()->UnprocessableEntity(); } - $request = self::getRequest([ + $request = $this->getRequest([ 'limit' => 40, // Maximum number of results to return. Defaults to 40. ], $request); diff --git a/src/Module/Api/Mastodon/Suggestions.php b/src/Module/Api/Mastodon/Suggestions.php index d8d89070a0..9f8773dc87 100644 --- a/src/Module/Api/Mastodon/Suggestions.php +++ b/src/Module/Api/Mastodon/Suggestions.php @@ -39,7 +39,7 @@ class Suggestions extends BaseApi self::checkAllowedScope(self::SCOPE_READ); $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'limit' => 40, // Maximum number of results to return. Defaults to 40. ], $request); diff --git a/src/Module/Api/Mastodon/Timelines/Direct.php b/src/Module/Api/Mastodon/Timelines/Direct.php index f6b55a093d..ea3a296dcd 100644 --- a/src/Module/Api/Mastodon/Timelines/Direct.php +++ b/src/Module/Api/Mastodon/Timelines/Direct.php @@ -40,7 +40,7 @@ class Direct extends BaseApi self::checkAllowedScope(self::SCOPE_READ); $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'max_id' => 0, // Return results older than id 'since_id' => 0, // Return results newer than id 'min_id' => 0, // Return results immediately newer than id diff --git a/src/Module/Api/Mastodon/Timelines/Home.php b/src/Module/Api/Mastodon/Timelines/Home.php index f717ef119d..bc53e9bcf8 100644 --- a/src/Module/Api/Mastodon/Timelines/Home.php +++ b/src/Module/Api/Mastodon/Timelines/Home.php @@ -41,7 +41,7 @@ class Home extends BaseApi self::checkAllowedScope(self::SCOPE_READ); $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'max_id' => 0, // Return results older than id 'since_id' => 0, // Return results newer than id 'min_id' => 0, // Return results immediately newer than id diff --git a/src/Module/Api/Mastodon/Timelines/ListTimeline.php b/src/Module/Api/Mastodon/Timelines/ListTimeline.php index a1ecd61f6f..c3e525f764 100644 --- a/src/Module/Api/Mastodon/Timelines/ListTimeline.php +++ b/src/Module/Api/Mastodon/Timelines/ListTimeline.php @@ -45,7 +45,7 @@ class ListTimeline extends BaseApi DI::mstdnError()->UnprocessableEntity(); } - $request = self::getRequest([ + $request = $this->getRequest([ 'max_id' => 0, // Return results older than id 'since_id' => 0, // Return results newer than id 'min_id' => 0, // Return results immediately newer than id diff --git a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php index 9d3b7f8365..c3ebe7af0f 100644 --- a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php +++ b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php @@ -43,7 +43,7 @@ class PublicTimeline extends BaseApi { $uid = self::getCurrentUserID(); - $request = self::getRequest([ + $request = $this->getRequest([ 'local' => false, // Show only local statuses? Defaults to false. 'remote' => false, // Show only remote statuses? Defaults to false. 'only_media' => false, // Show only statuses with media attached? Defaults to false. diff --git a/src/Module/Api/Mastodon/Timelines/Tag.php b/src/Module/Api/Mastodon/Timelines/Tag.php index d6915a68fd..441de039d8 100644 --- a/src/Module/Api/Mastodon/Timelines/Tag.php +++ b/src/Module/Api/Mastodon/Timelines/Tag.php @@ -53,7 +53,7 @@ class Tag extends BaseApi * There seem to be the parameters "any", "all", and "none". */ - $request = self::getRequest([ + $request = $this->getRequest([ 'local' => false, // If true, return only local statuses. Defaults to false. 'remote' => false, // Show only remote statuses? Defaults to false. 'only_media' => false, // If true, return only statuses with media attachments. Defaults to false. diff --git a/src/Module/Api/Mastodon/Trends.php b/src/Module/Api/Mastodon/Trends.php index 4e99b2c743..f87294d0c4 100644 --- a/src/Module/Api/Mastodon/Trends.php +++ b/src/Module/Api/Mastodon/Trends.php @@ -36,7 +36,7 @@ class Trends extends BaseApi */ protected function rawContent(array $request = []) { - $request = self::getRequest([ + $request = $this->getRequest([ 'limit' => 20, // Maximum number of results to return. Defaults to 10. ], $request); diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index c67671954f..7c0c77372e 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -116,7 +116,7 @@ class BaseApi extends BaseModule * @return array request data * @throws \Exception */ - public static function getRequest(array $defaults, array $request = null): array + public function getRequest(array $defaults, array $request = null): array { $httpinput = HTTPInputData::process(); $input = array_merge($httpinput['variables'], $httpinput['files'], $request ?? $_REQUEST); @@ -126,35 +126,7 @@ class BaseApi extends BaseModule unset(self::$request['pagename']); - $request = []; - - foreach ($defaults as $parameter => $defaultvalue) { - if (is_string($defaultvalue)) { - $request[$parameter] = $input[$parameter] ?? $defaultvalue; - } elseif (is_int($defaultvalue)) { - $request[$parameter] = (int)($input[$parameter] ?? $defaultvalue); - } elseif (is_float($defaultvalue)) { - $request[$parameter] = (float)($input[$parameter] ?? $defaultvalue); - } elseif (is_array($defaultvalue)) { - $request[$parameter] = $input[$parameter] ?? []; - } elseif (is_bool($defaultvalue)) { - $request[$parameter] = in_array(strtolower($input[$parameter] ?? ''), ['true', '1']); - } else { - Logger::notice('Unhandled default value type', ['parameter' => $parameter, 'type' => gettype($defaultvalue)]); - } - } - - foreach ($input ?? [] as $parameter => $value) { - if ($parameter == 'pagename') { - continue; - } - if (!in_array($parameter, array_keys($defaults))) { - Logger::notice('Unhandled request field', ['parameter' => $parameter, 'value' => $value, 'command' => DI::args()->getCommand()]); - } - } - - Logger::debug('Got request parameters', ['request' => $request, 'command' => DI::args()->getCommand()]); - return $request; + return $this->checkDefaults($defaults, $input); } /** diff --git a/src/Module/OAuth/Authorize.php b/src/Module/OAuth/Authorize.php index 973f31a0b1..56a11ba0ac 100644 --- a/src/Module/OAuth/Authorize.php +++ b/src/Module/OAuth/Authorize.php @@ -39,7 +39,7 @@ class Authorize extends BaseApi */ protected function rawContent(array $request = []) { - $request = self::getRequest([ + $request = $this->getRequest([ 'force_login' => '', // Forces the user to re-login, which is necessary for authorizing with multiple accounts from the same instance. 'response_type' => '', // Should be set equal to "code". 'client_id' => '', // Client ID, obtained during app registration. diff --git a/src/Module/OAuth/Revoke.php b/src/Module/OAuth/Revoke.php index 536d603c79..745ab0673c 100644 --- a/src/Module/OAuth/Revoke.php +++ b/src/Module/OAuth/Revoke.php @@ -34,7 +34,7 @@ class Revoke extends BaseApi { protected function post(array $request = [], array $post = []) { - $request = self::getRequest([ + $request = $this->getRequest([ 'client_id' => '', // Client ID, obtained during app registration 'client_secret' => '', // Client secret, obtained during app registration 'token' => '', // The previously obtained token, to be invalidated diff --git a/src/Module/OAuth/Token.php b/src/Module/OAuth/Token.php index 8bbb272c31..c8ef9ee14d 100644 --- a/src/Module/OAuth/Token.php +++ b/src/Module/OAuth/Token.php @@ -36,7 +36,7 @@ class Token extends BaseApi { protected function post(array $request = [], array $post = []) { - $request = self::getRequest([ + $request = $this->getRequest([ 'client_id' => '', // Client ID, obtained during app registration 'client_secret' => '', // Client secret, obtained during app registration 'redirect_uri' => '', // Set a URI to redirect the user to. If this parameter is set to "urn:ietf:wg:oauth:2.0:oob" then the token will be shown instead. Must match one of the redirect URIs declared during app registration.