diff --git a/src/BaseModule.php b/src/BaseModule.php index 86dd5e7c0..a74c02713 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 070dc452c..6826eb378 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 a6a4875b8..5fcb87b32 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 6e3daa770..fed7cc54d 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 f0d94ff3e..9b63cd7e6 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 d4288b88b..5861723b6 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 8a45de2d3..8cb719cd1 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 2c1e5e878..d9fc760d6 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 ba24625c4..3e06528bd 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 a80af6ea9..2aace2683 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 7135cb930..eb97a8856 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 b56fd5a94..6fce26fbb 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 53569b79a..536c68850 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 609bcdb20..0ab5a2a50 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 5811e3873..3f24810bb 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 0a1e9506f..a299185ae 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 d56ad5a88..a3d00dcd8 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 5ff51deb1..125e13e60 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 740d5d92a..67104dc28 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 c1ac2a89c..243d119f9 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 739c53c49..eead8f96c 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 53a9ac012..3ab41329c 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 151da9cc6..9ab676af0 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 65663882d..24d2a3bf8 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 9d612a455..96a0c7eac 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 f57c0268e..7527286e0 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 b4b152e5c..4978e980a 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 644aea874..f6da23aa1 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 1730db68c..dcc9969f7 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 54e85d2a6..05851a983 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 4193b8fa9..f463f46b4 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 d8d89070a..9f8773dc8 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 f6b55a093..ea3a296dc 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 f717ef119..bc53e9bcf 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 a1ecd61f6..c3e525f76 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 9d3b7f836..c3ebe7af0 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 d6915a68f..441de039d 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 4e99b2c74..f87294d0c 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 c67671954..7c0c77372 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 973f31a0b..56a11ba0a 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 536d603c7..745ab0673 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 8bbb272c3..c8ef9ee14 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.