From 1c5cf718599f698836fef3f8f88748757a7e85b5 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Sun, 6 Feb 2022 14:31:36 +0100 Subject: Fix Fever 32 bit ID issue + more PHP type hints (#4201) * Fix Fever 32 bit ID issue + more PHP type hints #fix https://github.com/FreshRSS/FreshRSS/issues/4200 Follow up and fix regression from https://github.com/FreshRSS/FreshRSS/pull/4110 * More PHP type hints with PHPStan * Fix pull problem * Avoid more nulls --- p/api/fever.php | 123 ++++++++++++++++++++++++-------------------------------- 1 file changed, 53 insertions(+), 70 deletions(-) (limited to 'p/api/fever.php') diff --git a/p/api/fever.php b/p/api/fever.php index ca8d574d4..618df4fbf 100644 --- a/p/api/fever.php +++ b/p/api/fever.php @@ -63,13 +63,7 @@ function debugInfo() { class FeverDAO extends Minz_ModelPdo { - /** - * @param string $prefix - * @param array $values - * @param array $bindArray - * @return string - */ - protected function bindParamArray($prefix, $values, &$bindArray) { + protected function bindParamArray(string $prefix, array $values, array &$bindArray): string { $str = ''; for ($i = 0; $i < count($values); $i++) { $str .= ':' . $prefix . $i . ','; @@ -79,13 +73,9 @@ class FeverDAO extends Minz_ModelPdo } /** - * @param array $feed_ids - * @param array $entry_ids - * @param int|null $max_id - * @param int|null $since_id * @return FreshRSS_Entry[] */ - public function findEntries(array $feed_ids, array $entry_ids, $max_id, $since_id) { + public function findEntries(array $feed_ids, array $entry_ids, string $max_id, string $since_id) { $values = array(); $order = ''; $entryDAO = FreshRSS_Factory::createEntryDao(); @@ -98,11 +88,11 @@ class FeverDAO extends Minz_ModelPdo if (!empty($entry_ids)) { $bindEntryIds = $this->bindParamArray('id', $entry_ids, $values); $sql .= " id IN($bindEntryIds)"; - } elseif ($max_id != null) { + } elseif ($max_id != '') { $sql .= ' id < :id'; $values[':id'] = $max_id; $order = ' ORDER BY id DESC'; - } elseif ($since_id != null) { + } elseif ($since_id != '') { $sql .= ' id > :id'; $values[':id'] = $since_id; $order = ' ORDER BY id ASC'; @@ -149,7 +139,7 @@ class FeverAPI * API Password sent from client is the result of the md5 sum of * your FreshRSS "username:your-api-password" combination */ - private function authenticate() { + private function authenticate(): bool { FreshRSS_Context::$user_conf = null; Minz_Session::_param('currentUser'); $feverKey = empty($_POST['api_key']) ? '' : substr(trim($_POST['api_key']), 0, 128); @@ -176,10 +166,7 @@ class FeverAPI return false; } - /** - * @return bool - */ - public function isAuthenticatedApiUser() { + public function isAuthenticatedApiUser(): bool { $this->authenticate(); if (FreshRSS_Context::$user_conf !== null) { @@ -191,11 +178,9 @@ class FeverAPI /** * This does all the processing, since the fever api does not have a specific variable that specifies the operation - * - * @return array * @throws Exception */ - public function process() { + public function process(): array { $response_arr = array(); if (!$this->isAuthenticatedApiUser()) { @@ -247,7 +232,7 @@ class FeverAPI break; case 'feed': case 'group': - $before = isset($_REQUEST['before']) ? $_REQUEST['before'] : null; + $before = $_REQUEST['before'] ?? ''; $this->{$method_name}($id, $before); break; } @@ -271,12 +256,8 @@ class FeverAPI /** * Returns the complete JSON, with 'api_version' and status as 'auth'. - * - * @param int $status - * @param array $reply - * @return string */ - public function wrap($status, array $reply = array()) { + public function wrap(int $status, array $reply = array()): string { $arr = array('api_version' => self::API_LEVEL, 'auth' => $status); if ($status === self::STATUS_OK) { @@ -289,10 +270,8 @@ class FeverAPI /** * every authenticated method includes last_refreshed_on_time - * - * @return int */ - protected function lastRefreshedOnTime() { + protected function lastRefreshedOnTime(): int { $lastUpdate = 0; $entries = $this->feedDAO->listFeedsOrderUpdate(-1, 1); @@ -305,10 +284,7 @@ class FeverAPI return $lastUpdate; } - /** - * @return array - */ - protected function getFeeds() { + protected function getFeeds(): array { $feeds = array(); $myFeeds = $this->feedDAO->listFeeds(); @@ -328,10 +304,7 @@ class FeverAPI return $feeds; } - /** - * @return array - */ - protected function getGroups() { + protected function getGroups(): array { $groups = array(); $categoryDAO = FreshRSS_Factory::createCategoryDao(); @@ -348,10 +321,7 @@ class FeverAPI return $groups; } - /** - * @return array - */ - protected function getFavicons() { + protected function getFavicons(): array { $favicons = array(); $salt = FreshRSS_Context::$system_conf->salt; $myFeeds = $this->feedDAO->listFeeds(); @@ -381,10 +351,7 @@ class FeverAPI return $this->entryDAO->count(); } - /** - * @return array - */ - protected function getFeedsGroup() { + protected function getFeedsGroup(): array { $groups = array(); $ids = array(); $myFeeds = $this->feedDAO->listFeeds(); @@ -406,24 +373,19 @@ class FeverAPI /** * AFAIK there is no 'hot links' alternative in FreshRSS - * @return array */ - protected function getLinks() { + protected function getLinks(): array { return array(); } /** * @param array $ids - * @return string */ - protected function entriesToIdList($ids = array()) { + protected function entriesToIdList(array $ids = array()): string { return implode(',', array_values($ids)); } - /** - * @return string - */ - protected function getUnreadItemIds() { + protected function getUnreadItemIds(): string { $entries = $this->entryDAO->listIdsWhere('a', '', FreshRSS_Entry::STATE_NOT_READ, 'ASC', 0); return $this->entriesToIdList($entries); } @@ -436,30 +398,39 @@ class FeverAPI return $this->entriesToIdList($entries); } + /** + * @return integer|false + */ protected function setItemAsRead($id) { return $this->entryDAO->markRead($id, true); } + /** + * @return integer|false + */ protected function setItemAsUnread($id) { return $this->entryDAO->markRead($id, false); } + /** + * @return integer|false + */ protected function setItemAsSaved($id) { return $this->entryDAO->markFavorite($id, true); } + /** + * @return integer|false + */ protected function setItemAsUnsaved($id) { return $this->entryDAO->markFavorite($id, false); } - /** - * @return array - */ - protected function getItems() { + protected function getItems(): array { $feed_ids = array(); $entry_ids = array(); - $max_id = null; - $since_id = null; + $max_id = ''; + $since_id = ''; if (isset($_REQUEST['feed_ids']) || isset($_REQUEST['group_ids'])) { if (isset($_REQUEST['feed_ids'])) { @@ -485,12 +456,18 @@ class FeverAPI if (isset($_REQUEST['max_id'])) { // use the max_id argument to request the previous $item_limit items - $max_id = ctype_digit('' . $_REQUEST['max_id']) ? intval($_REQUEST['max_id']) : null; + $max_id = '' . $_REQUEST['max_id']; + if (!ctype_digit($max_id)) { + $max_id = ''; + } } elseif (isset($_REQUEST['with_ids'])) { $entry_ids = explode(',', $_REQUEST['with_ids']); } elseif (isset($_REQUEST['since_id'])) { // use the since_id argument to request the next $item_limit items - $since_id = ctype_digit('' . $_REQUEST['since_id']) ? intval($_REQUEST['since_id']) : null; + $since_id = '' . $_REQUEST['since_id']; + if (!ctype_digit($since_id)) { + $since_id = ''; + } } $items = array(); @@ -526,23 +503,29 @@ class FeverAPI /** * TODO replace by a dynamic fetch for id <= $before timestamp * - * @param int $beforeTimestamp - * @return int + * @param int|string $beforeTimestamp + * @return string */ - protected function convertBeforeToId($beforeTimestamp) { - return $beforeTimestamp == 0 ? 0 : $beforeTimestamp . '000000'; + protected function convertBeforeToId($beforeTimestamp): string { + return $beforeTimestamp == '0' ? '0' : $beforeTimestamp . '000000'; } - protected function setFeedAsRead($id, $before) { + /** + * @return integer|false + */ + protected function setFeedAsRead(string $id, string $before) { $before = $this->convertBeforeToId($before); return $this->entryDAO->markReadFeed($id, $before); } - protected function setGroupAsRead($id, $before) { + /** + * @return integer|false + */ + protected function setGroupAsRead(string $id, string $before) { $before = $this->convertBeforeToId($before); // special case to mark all items as read - if ($id == 0) { + if ($id == '0') { return $this->entryDAO->markReadEntries($before); } -- cgit v1.2.3