From fd4926b0f387dd9720f3c0415af1f663ce9538fd Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 16 Jan 2022 18:04:05 +0000 Subject: [PATCH] More parameter handling improved --- src/Module/Api/Friendica/Group/Show.php | 2 +- src/Module/Api/Friendica/Photo.php | 2 +- src/Module/Api/Friendica/Photo/Create.php | 2 +- src/Module/Api/Friendica/Photo/Lists.php | 2 +- src/Module/Api/Friendica/Photo/Update.php | 2 +- src/Module/Api/GNUSocial/Statusnet/Conversation.php | 2 +- src/Module/Api/Twitter/DirectMessages/Destroy.php | 12 ++++++------ src/Module/Api/Twitter/Favorites.php | 11 +++++------ src/Module/Api/Twitter/Statuses/Destroy.php | 8 +++----- src/Module/Api/Twitter/Statuses/Retweet.php | 8 +++----- src/Module/Api/Twitter/Statuses/Show.php | 8 ++++---- 11 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/Module/Api/Friendica/Group/Show.php b/src/Module/Api/Friendica/Group/Show.php index ec0bdd134..1a0e7b6be 100644 --- a/src/Module/Api/Friendica/Group/Show.php +++ b/src/Module/Api/Friendica/Group/Show.php @@ -36,7 +36,7 @@ class Show extends BaseApi { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); $uid = BaseApi::getCurrentUserID(); - $type = $this->parameters['extension'] ?? ''; + $type = $this->getRequestValue($this->parameters, 'extension', 'json'); // params $gid = $this->getRequestValue($request, 'gid', 0); diff --git a/src/Module/Api/Friendica/Photo.php b/src/Module/Api/Friendica/Photo.php index d0ea25b30..ba87081d4 100644 --- a/src/Module/Api/Friendica/Photo.php +++ b/src/Module/Api/Friendica/Photo.php @@ -48,7 +48,7 @@ class Photo extends BaseApi { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); $uid = BaseApi::getCurrentUserID(); - $type = $this->parameters['extension'] ?? ''; + $type = $this->getRequestValue($this->parameters, 'extension', 'json'); if (empty($request['photo_id'])) { throw new HTTPException\BadRequestException('No photo id.'); diff --git a/src/Module/Api/Friendica/Photo/Create.php b/src/Module/Api/Friendica/Photo/Create.php index 0eeb45514..c8ea2fbd7 100644 --- a/src/Module/Api/Friendica/Photo/Create.php +++ b/src/Module/Api/Friendica/Photo/Create.php @@ -52,7 +52,7 @@ class Create extends BaseApi { BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); $uid = BaseApi::getCurrentUserID(); - $type = $this->parameters['extension'] ?? ''; + $type = $this->getRequestValue($this->parameters, 'extension', 'json'); // input params $desc = $this->getRequestValue($request, 'desc'); diff --git a/src/Module/Api/Friendica/Photo/Lists.php b/src/Module/Api/Friendica/Photo/Lists.php index 6d2eb1716..350d98a1d 100644 --- a/src/Module/Api/Friendica/Photo/Lists.php +++ b/src/Module/Api/Friendica/Photo/Lists.php @@ -54,7 +54,7 @@ class Lists extends BaseApi { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); $uid = BaseApi::getCurrentUserID(); - $type = $this->parameters['extension'] ?? ''; + $type = $this->getRequestValue($this->parameters, 'extension', 'json'); $photos = Photo::selectToArray(['resource-id'], ["`uid` = ? AND NOT `photo-type` IN (?, ?)", $uid, Photo::CONTACT_AVATAR, Photo::CONTACT_BANNER], ['order' => ['id'], 'group_by' => ['resource-id']]); diff --git a/src/Module/Api/Friendica/Photo/Update.php b/src/Module/Api/Friendica/Photo/Update.php index ccb9f9150..6723cad53 100644 --- a/src/Module/Api/Friendica/Photo/Update.php +++ b/src/Module/Api/Friendica/Photo/Update.php @@ -52,7 +52,7 @@ class Update extends BaseApi { BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); $uid = BaseApi::getCurrentUserID(); - $type = $this->parameters['extension'] ?? ''; + $type = $this->getRequestValue($this->parameters, 'extension', 'json'); // input params $photo_id = $this->getRequestValue($request, 'photo_id'); diff --git a/src/Module/Api/GNUSocial/Statusnet/Conversation.php b/src/Module/Api/GNUSocial/Statusnet/Conversation.php index f21f4c311..08cfe82fb 100644 --- a/src/Module/Api/GNUSocial/Statusnet/Conversation.php +++ b/src/Module/Api/GNUSocial/Statusnet/Conversation.php @@ -40,7 +40,7 @@ class Conversation extends BaseApi $uid = BaseApi::getCurrentUserID(); // params - $id = $this->parameters['id'] ?? 0; + $id = $this->getRequestValue($this->parameters, 'id', 0); $since_id = $this->getRequestValue($request, 'since_id', 0, 0); $max_id = $this->getRequestValue($request, 'max_id', 0, 0); $count = $this->getRequestValue($request, 'count', 20, 1, 100); diff --git a/src/Module/Api/Twitter/DirectMessages/Destroy.php b/src/Module/Api/Twitter/DirectMessages/Destroy.php index 3100ff345..ed0b1ed29 100644 --- a/src/Module/Api/Twitter/DirectMessages/Destroy.php +++ b/src/Module/Api/Twitter/DirectMessages/Destroy.php @@ -52,7 +52,12 @@ class Destroy extends BaseApi BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); $uid = BaseApi::getCurrentUserID(); - $id = $this->getRequestValue($request, 'id', 0); + $id = $this->getRequestValue($request, 'id', 0); + $id = $this->getRequestValue($this->parameters, 'id', $id); + if (empty($id)) { + throw new BadRequestException('Message id not specified'); + } + $verbose = $this->getRequestValue($request, 'friendica_verbose', false); $parenturi = $request['friendica_parenturi'] ?? ''; @@ -64,11 +69,6 @@ class Destroy extends BaseApi return; } - // BadRequestException if no id specified (for clients using Twitter API) - if ($id == 0) { - throw new BadRequestException('Message id not specified'); - } - // add parent-uri to sql command if specified by calling app $sql_extra = ($parenturi != "" ? " AND `parent-uri` = '" . DBA::escape($parenturi) . "'" : ""); diff --git a/src/Module/Api/Twitter/Favorites.php b/src/Module/Api/Twitter/Favorites.php index e88922af7..828741a19 100644 --- a/src/Module/Api/Twitter/Favorites.php +++ b/src/Module/Api/Twitter/Favorites.php @@ -45,10 +45,11 @@ class Favorites extends BaseApi Logger::info(BaseApi::LOG_PREFIX . 'for {self}', ['module' => 'api', 'action' => 'favorites']); // params - $count = $this->getRequestValue($request, 'count', 20, 1, 100); - $page = $this->getRequestValue($request, 'page', 1, 1); - $since_id = $this->getRequestValue($request, 'since_id', 0, 0); - $max_id = $this->getRequestValue($request, 'max_id', 0, 0); + $count = $this->getRequestValue($request, 'count', 20, 1, 100); + $page = $this->getRequestValue($request, 'page', 1, 1); + $since_id = $this->getRequestValue($request, 'since_id', 0, 0); + $max_id = $this->getRequestValue($request, 'max_id', 0, 0); + $include_entities = $this->getRequestValue($request, 'include_entities', false); $start = max(0, ($page - 1) * $count); @@ -64,8 +65,6 @@ class Favorites extends BaseApi $statuses = Post::selectForUser($uid, [], $condition, $params); - $include_entities = strtolower(($request['include_entities'] ?? 'false') == 'true'); - $ret = []; while ($status = DBA::fetch($statuses)) { $ret[] = DI::twitterStatus()->createFromUriId($status['uri-id'], $status['uid'], $include_entities)->toArray(); diff --git a/src/Module/Api/Twitter/Statuses/Destroy.php b/src/Module/Api/Twitter/Statuses/Destroy.php index 7f4a6c6dc..be0848353 100644 --- a/src/Module/Api/Twitter/Statuses/Destroy.php +++ b/src/Module/Api/Twitter/Statuses/Destroy.php @@ -39,11 +39,9 @@ class Destroy extends BaseApi BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); $uid = BaseApi::getCurrentUserID(); - if (!empty($this->parameters['id'])) { - $id = (int)$this->parameters['id']; - } elseif (!empty($request['id'])) { - $id = (int)$request['id']; - } else { + $id = $this->getRequestValue($request, 'id', 0); + $id = $this->getRequestValue($this->parameters, 'id', $id); + if (empty($id)) { throw new BadRequestException('An id is missing.'); } diff --git a/src/Module/Api/Twitter/Statuses/Retweet.php b/src/Module/Api/Twitter/Statuses/Retweet.php index 0c2401cf3..1d67443b1 100644 --- a/src/Module/Api/Twitter/Statuses/Retweet.php +++ b/src/Module/Api/Twitter/Statuses/Retweet.php @@ -44,11 +44,9 @@ class Retweet extends BaseApi self::checkAllowedScope(self::SCOPE_WRITE); $uid = self::getCurrentUserID(); - if (!empty($this->parameters['id'])) { - $id = (int)$this->parameters['id']; - } elseif (!empty($request['id'])) { - $id = (int)$request['id']; - } else { + $id = $this->getRequestValue($request, 'id', 0); + $id = $this->getRequestValue($this->parameters, 'id', $id); + if (empty($id)) { throw new BadRequestException('An id is missing.'); } diff --git a/src/Module/Api/Twitter/Statuses/Show.php b/src/Module/Api/Twitter/Statuses/Show.php index a85483790..64533d0bc 100644 --- a/src/Module/Api/Twitter/Statuses/Show.php +++ b/src/Module/Api/Twitter/Statuses/Show.php @@ -41,10 +41,10 @@ class Show extends BaseApi BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); $uid = BaseApi::getCurrentUserID(); - if (empty($this->parameters['id'])) { - $id = intval($request['id'] ?? 0); - } else { - $id = (int)$this->parameters['id']; + $id = $this->getRequestValue($request, 'id', 0); + $id = $this->getRequestValue($this->parameters, 'id', $id); + if (empty($id)) { + throw new BadRequestException('An id is missing.'); } Logger::notice('API: api_statuses_show: ' . $id);