From 95e65e37ee0f5c8eaf6daa92f7fd6ffceb82efb4 Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Thu, 9 Jan 2020 21:41:35 +0100 Subject: [PATCH 1/5] Refactor duplicate code for parsing photo URL --- src/Model/Mail.php | 8 +++----- src/Model/Photo.php | 31 +++++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/Model/Mail.php b/src/Model/Mail.php index eeea130a60..da72d7d51f 100644 --- a/src/Model/Mail.php +++ b/src/Model/Mail.php @@ -215,12 +215,10 @@ class Mail $images = $match[1]; if (count($images)) { foreach ($images as $image) { - if (!stristr($image, DI::baseUrl() . '/photo/')) { - continue; + $image_rid = Photo::ridFromURI($image); + if ($image_rid) { + Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_rid, 'album' => 'Wall Photos', 'uid' => local_user()]); } - $image_uri = substr($image, strrpos($image, '/') + 1); - $image_uri = substr($image_uri, 0, strpos($image_uri, '-')); - Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_uri, 'album' => 'Wall Photos', 'uid' => local_user()]); } } } diff --git a/src/Model/Photo.php b/src/Model/Photo.php index c4dbf2b30a..faae9e8b99 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -596,6 +596,25 @@ class Photo return System::createGUID(32, false); } + /** + * Extracts the rid from a local photo URI + * + * @param string $image_uri The URI of the photo + * @return string The rid of the photo, or an empty string if the URI is not local + */ + public static function ridFromURI($image_uri) + { + if (!stristr($image_uri, DI::baseUrl() . '/photo/')) { + return; + } + $image_uri = substr($image_uri, strrpos($image_uri, '/') + 1); + $image_uri = substr($image_uri, 0, strpos($image_uri, '-')); + if (!strlen($image_uri)) { + return; + } + return $image_uri; + } + /** * Changes photo permissions that had been embedded in a post * @@ -622,12 +641,8 @@ class Photo } foreach ($images as $image) { - if (!stristr($image, DI::baseUrl() . '/photo/')) { - continue; - } - $image_uri = substr($image,strrpos($image,'/') + 1); - $image_uri = substr($image_uri,0, strpos($image_uri,'-')); - if (!strlen($image_uri)) { + $image_rid = ridFromURI($image); + if (!$image_rid) { continue; } @@ -636,7 +651,7 @@ class Photo $condition = [ 'allow_cid' => $srch, 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '', - 'resource-id' => $image_uri, 'uid' => $uid + 'resource-id' => $image_rid, 'uid' => $uid ]; if (!Photo::exists($condition)) { continue; @@ -646,7 +661,7 @@ class Photo $fields = ['allow_cid' => $str_contact_allow, 'allow_gid' => $str_group_allow, 'deny_cid' => $str_contact_deny, 'deny_gid' => $str_group_deny]; - $condition = ['resource-id' => $image_uri, 'uid' => $uid]; + $condition = ['resource-id' => $image_rid, 'uid' => $uid]; Logger::info('Set permissions', ['condition' => $condition, 'permissions' => $fields]); Photo::update($fields, $condition); } From ae6748a4650abf3f4066378e1f74a2bbd5d7a1bc Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Thu, 9 Jan 2020 21:41:52 +0100 Subject: [PATCH 2/5] Repair missing contact photos --- src/Model/Contact.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 0540bf7beb..7722b69909 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1896,6 +1896,14 @@ class Contact $data = [$contact["photo"], $contact["thumb"], $contact["micro"]]; } + foreach ($data as $image_uri) { + $image_rid = Photo::ridFromURI($image_uri); + if ($image_rid && !Photo::exists(['resource-id' => $image_rid, 'uid' => $uid])) { + Logger::info('Regenerating avatar for contact uid ' . $uid . ' cid ' . $cid . ' missing photo ' . $image_rid . ' avatar ' . $contact['avatar']); + $force = true; + } + } + if (($contact["avatar"] != $avatar) || $force) { $photos = Photo::importProfilePhoto($avatar, $uid, $cid, true); From dee6a6384120bfefcd37154db1bc5a098cd53062 Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Fri, 10 Jan 2020 20:29:15 +0100 Subject: [PATCH 3/5] Qualify call to ridFromURI Co-Authored-By: Hypolite Petovan --- src/Model/Photo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Photo.php b/src/Model/Photo.php index faae9e8b99..801ef25fba 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -641,7 +641,7 @@ class Photo } foreach ($images as $image) { - $image_rid = ridFromURI($image); + $image_rid = self::ridFromURI($image); if (!$image_rid) { continue; } From 5d0a3be4f698d4861e7aabd9237484ee0e9644e7 Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Sat, 11 Jan 2020 16:00:02 +0100 Subject: [PATCH 4/5] Implement code review from nupplaphil --- src/Model/Contact.php | 2 +- src/Model/Mail.php | 2 +- src/Model/Photo.php | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 7722b69909..e6c00b1e3c 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1899,7 +1899,7 @@ class Contact foreach ($data as $image_uri) { $image_rid = Photo::ridFromURI($image_uri); if ($image_rid && !Photo::exists(['resource-id' => $image_rid, 'uid' => $uid])) { - Logger::info('Regenerating avatar for contact uid ' . $uid . ' cid ' . $cid . ' missing photo ' . $image_rid . ' avatar ' . $contact['avatar']); + Logger::info('Regenerating avatar', ['contact uid' => $uid, 'cid' => $cid, 'missing photo' => $image_rid, 'avatar' => $contact['avatar']]); $force = true; } } diff --git a/src/Model/Mail.php b/src/Model/Mail.php index da72d7d51f..292d797b85 100644 --- a/src/Model/Mail.php +++ b/src/Model/Mail.php @@ -216,7 +216,7 @@ class Mail if (count($images)) { foreach ($images as $image) { $image_rid = Photo::ridFromURI($image); - if ($image_rid) { + if (!empty($image_rid)) { Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_rid, 'album' => 'Wall Photos', 'uid' => local_user()]); } } diff --git a/src/Model/Photo.php b/src/Model/Photo.php index 801ef25fba..8b65c0d0e0 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -605,12 +605,12 @@ class Photo public static function ridFromURI($image_uri) { if (!stristr($image_uri, DI::baseUrl() . '/photo/')) { - return; + return ''; } $image_uri = substr($image_uri, strrpos($image_uri, '/') + 1); $image_uri = substr($image_uri, 0, strpos($image_uri, '-')); if (!strlen($image_uri)) { - return; + return ''; } return $image_uri; } @@ -642,7 +642,7 @@ class Photo foreach ($images as $image) { $image_rid = self::ridFromURI($image); - if (!$image_rid) { + if (empty($image_rid)) { continue; } From e002365d7fe4a157c1ed464151caef92723f876f Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Sat, 11 Jan 2020 16:27:01 +0100 Subject: [PATCH 5/5] Add type declaration to parameter --- src/Model/Photo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Photo.php b/src/Model/Photo.php index 8b65c0d0e0..f235b1b8c4 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -602,7 +602,7 @@ class Photo * @param string $image_uri The URI of the photo * @return string The rid of the photo, or an empty string if the URI is not local */ - public static function ridFromURI($image_uri) + public static function ridFromURI(string $image_uri) { if (!stristr($image_uri, DI::baseUrl() . '/photo/')) { return '';