From 644f31289625065d0a492b59fc3d0c5dce4ca478 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 13 Sep 2021 20:22:55 +0200 Subject: [PATCH 1/6] Fix various Notices --- src/Model/Contact.php | 8 +++++--- src/Model/Item.php | 4 ++-- src/Module/Contact.php | 2 +- src/Protocol/ActivityPub/Processor.php | 2 +- src/Protocol/DFRN.php | 2 +- src/Protocol/Diaspora.php | 2 +- src/Protocol/OStatus.php | 2 +- 7 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 93e745456..51d162ae2 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -2757,12 +2757,14 @@ class Contact return null; } - public static function removeFollower($importer, $contact) + public static function removeFollower(array $contact) { - if (($contact['rel'] == self::FRIEND) || ($contact['rel'] == self::SHARING)) { + if (!empty($contact['rel']) && (($contact['rel'] == self::FRIEND) || ($contact['rel'] == self::SHARING))) { DBA::update('contact', ['rel' => self::SHARING], ['id' => $contact['id']]); - } else { + } elseif (!empty($contact['id'])) { self::remove($contact['id']); + } else { + DI::logger()->info('Couldn\'t remove follower because of invalid contact array', ['contact' => $contact]); } } diff --git a/src/Model/Item.php b/src/Model/Item.php index 8327252ab..992c8d471 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2292,9 +2292,9 @@ class Item continue; } elseif (!$expire_starred && intval($item['starred'])) { continue; - } elseif (!$expire_notes && ($item['post-type'] == self::PT_PERSONAL_NOTE)) { + } elseif (!$expire_notes && (($item['post-type'] ?? null) == self::PT_PERSONAL_NOTE)) { continue; - } elseif (!$expire_items && ($item['post-type'] != self::PT_PERSONAL_NOTE)) { + } elseif (!$expire_items && (($item['post-type'] ?? null) != self::PT_PERSONAL_NOTE)) { continue; } diff --git a/src/Module/Contact.php b/src/Module/Contact.php index ceb2b6757..0260e90f1 100644 --- a/src/Module/Contact.php +++ b/src/Module/Contact.php @@ -293,7 +293,7 @@ class Contact extends BaseModule $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => [0, local_user()], 'deleted' => false]); // Don't display contacts that are about to be deleted - if ($contact['network'] == Protocol::PHANTOM) { + if (DBA::isResult($contact) && !empty($contact['network']) && $contact['network'] == Protocol::PHANTOM) { $contact = false; } } diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 47a530035..1fe527f40 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -1102,7 +1102,7 @@ class Processor return; } - Contact::removeFollower($owner, $contact); + Contact::removeFollower($contact); Logger::info('Undo following request', ['contact' => $cid, 'user' => $uid]); } diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 4ccd259f2..6797608e6 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -1584,7 +1584,7 @@ class DFRN } if ($activity->match($item["verb"], Activity::UNFOLLOW)) { Logger::log("Lost follower"); - Contact::removeFollower($importer, $contact, $item); + Contact::removeFollower($contact); return false; } if ($activity->match($item["verb"], Activity::REQ_FRIEND)) { diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 48d7a3371..8ab89d950 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -2211,7 +2211,7 @@ class Diaspora return true; } else { Logger::log("Author ".$author." doesn't want to follow us anymore.", Logger::DEBUG); - Contact::removeFollower($importer, $contact); + Contact::removeFollower($contact); return true; } } diff --git a/src/Protocol/OStatus.php b/src/Protocol/OStatus.php index 2404db2df..4f16f40f1 100644 --- a/src/Protocol/OStatus.php +++ b/src/Protocol/OStatus.php @@ -471,7 +471,7 @@ class OStatus if ($item["verb"] == Activity::O_UNFOLLOW) { $dummy = null; - Contact::removeFollower($importer, $contact, $item, $dummy); + Contact::removeFollower($contact); continue; } From 89a085a12c031721de1a5ab4f757d94ada590f63 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 13 Sep 2021 20:31:14 +0200 Subject: [PATCH 2/6] Check DBA::count() result --- src/Database/Database.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 361a3248d..c11e04936 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1461,8 +1461,13 @@ class Database $row = $this->fetchFirst($sql, $condition); - // Ensure to always return either a "null" or a numeric value - return is_numeric($row['count']) ? (int)$row['count'] : $row['count']; + if (empty($row['count'])) { + $this->logger->notice('Invalid count.', ['table' => $table, 'expression' => $expression, 'condition' => $condition_string]); + return 0; + } else { + // Ensure to always return either a "null" or a numeric value + return is_numeric($row['count']) ? (int)$row['count'] : $row['count']; + } } /** From 462f76352ef017a30c76a397a7e15c621d1c2e3b Mon Sep 17 00:00:00 2001 From: Philipp Date: Wed, 15 Sep 2021 00:31:33 +0200 Subject: [PATCH 3/6] Add feedback :-) --- src/Database/Database.php | 4 ++-- src/Model/Contact.php | 2 +- src/Model/Item.php | 6 +++--- src/Module/Contact.php | 14 ++++++++++---- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index c11e04936..e4a4a0f57 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1461,8 +1461,8 @@ class Database $row = $this->fetchFirst($sql, $condition); - if (empty($row['count'])) { - $this->logger->notice('Invalid count.', ['table' => $table, 'expression' => $expression, 'condition' => $condition_string]); + if (!isset($row['count'])) { + $this->logger->notice('Invalid count.', ['table' => $table, 'expression' => $expression, 'condition' => $condition_string, 'callstack' => System::callstack()]); return 0; } else { // Ensure to always return either a "null" or a numeric value diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 51d162ae2..7f72b38d6 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -2759,7 +2759,7 @@ class Contact public static function removeFollower(array $contact) { - if (!empty($contact['rel']) && (($contact['rel'] == self::FRIEND) || ($contact['rel'] == self::SHARING))) { + if (in_array($contact['rel'] ?? [], [self::FRIEND, self::SHARING])) { DBA::update('contact', ['rel' => self::SHARING], ['id' => $contact['id']]); } elseif (!empty($contact['id'])) { self::remove($contact['id']); diff --git a/src/Model/Item.php b/src/Model/Item.php index 992c8d471..83cbcbb01 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2259,7 +2259,7 @@ class Item $condition[0] .= " AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY"; $condition[] = $days; - $items = Post::select(['resource-id', 'starred', 'id', 'post-type', 'uid', 'uri-id'], $condition); + $items = Post::select(['resource-id', 'starred', 'id', 'post-type', 'uid', 'uri-id', 'post-type'], $condition); if (!DBA::isResult($items)) { return; @@ -2292,9 +2292,9 @@ class Item continue; } elseif (!$expire_starred && intval($item['starred'])) { continue; - } elseif (!$expire_notes && (($item['post-type'] ?? null) == self::PT_PERSONAL_NOTE)) { + } elseif (!$expire_notes && ($item['post-type'] == self::PT_PERSONAL_NOTE)) { continue; - } elseif (!$expire_items && (($item['post-type'] ?? null) != self::PT_PERSONAL_NOTE)) { + } elseif (!$expire_items && ($item['post-type'] != self::PT_PERSONAL_NOTE)) { continue; } diff --git a/src/Module/Contact.php b/src/Module/Contact.php index 0260e90f1..7d323c7a3 100644 --- a/src/Module/Contact.php +++ b/src/Module/Contact.php @@ -290,11 +290,17 @@ class Contact extends BaseModule $contact_id = $data['user']; } - $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => [0, local_user()], 'deleted' => false]); + if (empty($data)) { + $contact = DBA::selectFirst('contact', [], [ + 'id' => $contact_id, + 'uid' => [0, local_user()], + 'deleted' => false + ]); - // Don't display contacts that are about to be deleted - if (DBA::isResult($contact) && !empty($contact['network']) && $contact['network'] == Protocol::PHANTOM) { - $contact = false; + // Don't display contacts that are about to be deleted + if (DBA::isResult($contact) && !empty($contact['network']) && $contact['network'] == Protocol::PHANTOM) { + $contact = false; + } } } From 6893e2c3f7f80e13b7f7126dc6e65d073b4652bb Mon Sep 17 00:00:00 2001 From: Philipp Date: Wed, 15 Sep 2021 08:48:41 +0200 Subject: [PATCH 4/6] Add feedback / revert fix for $item['post-type'] --- src/Database/Database.php | 3 +-- src/Model/Item.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index e4a4a0f57..a93d4e610 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1465,8 +1465,7 @@ class Database $this->logger->notice('Invalid count.', ['table' => $table, 'expression' => $expression, 'condition' => $condition_string, 'callstack' => System::callstack()]); return 0; } else { - // Ensure to always return either a "null" or a numeric value - return is_numeric($row['count']) ? (int)$row['count'] : $row['count']; + return (int)$row['count']; } } diff --git a/src/Model/Item.php b/src/Model/Item.php index 83cbcbb01..8327252ab 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2259,7 +2259,7 @@ class Item $condition[0] .= " AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY"; $condition[] = $days; - $items = Post::select(['resource-id', 'starred', 'id', 'post-type', 'uid', 'uri-id', 'post-type'], $condition); + $items = Post::select(['resource-id', 'starred', 'id', 'post-type', 'uid', 'uri-id'], $condition); if (!DBA::isResult($items)) { return; From fd22906d70e409f724eda03e6b44a92c492d3b5e Mon Sep 17 00:00:00 2001 From: Philipp Date: Wed, 15 Sep 2021 08:51:26 +0200 Subject: [PATCH 5/6] Add feedback --- src/Model/Contact.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 7f72b38d6..3a214d00b 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -2764,7 +2764,7 @@ class Contact } elseif (!empty($contact['id'])) { self::remove($contact['id']); } else { - DI::logger()->info('Couldn\'t remove follower because of invalid contact array', ['contact' => $contact]); + DI::logger()->info('Couldn\'t remove follower because of invalid contact array', ['contact' => $contact, 'callstack' => System::callstack()]); } } From c00da77c677c0b5d3d9c3391365530e41a4dad7c Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 18 Sep 2021 12:55:17 +0200 Subject: [PATCH 6/6] Add feedback --- src/Database/Database.php | 2 +- src/Module/Contact.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index a93d4e610..28283b1c8 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1462,7 +1462,7 @@ class Database $row = $this->fetchFirst($sql, $condition); if (!isset($row['count'])) { - $this->logger->notice('Invalid count.', ['table' => $table, 'expression' => $expression, 'condition' => $condition_string, 'callstack' => System::callstack()]); + $this->logger->notice('Invalid count.', ['table' => $table, 'row' => $row, 'expression' => $expression, 'condition' => $condition_string, 'callstack' => System::callstack()]); return 0; } else { return (int)$row['count']; diff --git a/src/Module/Contact.php b/src/Module/Contact.php index 7d323c7a3..281e69008 100644 --- a/src/Module/Contact.php +++ b/src/Module/Contact.php @@ -290,7 +290,7 @@ class Contact extends BaseModule $contact_id = $data['user']; } - if (empty($data)) { + if (!empty($data)) { $contact = DBA::selectFirst('contact', [], [ 'id' => $contact_id, 'uid' => [0, local_user()],