From fc246424a907e91fc19a4aa9ac7ef3526523607e Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 7 Dec 2022 22:21:23 -0500 Subject: [PATCH] Provide default host value to hash() in Model\Item::guidFromUri - Address part of https://github.com/friendica/friendica/issues/12011#issuecomment-1338133783 --- src/Contact/Avatar.php | 2 +- src/Model/Contact.php | 4 ++-- src/Model/Item.php | 10 ++++++++-- src/Model/Mail.php | 3 +-- src/Protocol/Feed.php | 4 ++-- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Contact/Avatar.php b/src/Contact/Avatar.php index 0cfc8df345..711a8549f2 100644 --- a/src/Contact/Avatar.php +++ b/src/Contact/Avatar.php @@ -125,7 +125,7 @@ class Avatar private static function getFilename(string $url): string { - $guid = Item::guidFromUri($url, parse_url($url, PHP_URL_HOST)); + $guid = Item::guidFromUri($url); return substr($guid, 0, 2) . '/' . substr($guid, 3, 2) . '/' . substr($guid, 5, 3) . '/' . substr($guid, 9, 2) .'/' . substr($guid, 11, 2) . '/' . substr($guid, 13, 4). '/' . substr($guid, 18) . '-'; diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 873de08906..4a10fd98ea 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -2099,7 +2099,7 @@ class Contact if ($static) { $query_params['static'] = true; } - + return $url . ($guid ?: $cid) . (!empty($query_params) ? '?' . http_build_query($query_params) : ''); } @@ -2675,7 +2675,7 @@ class Contact } $update = false; - $guid = ($ret['guid'] ?? '') ?: Item::guidFromUri($ret['url'], parse_url($ret['url'], PHP_URL_HOST)); + $guid = ($ret['guid'] ?? '') ?: Item::guidFromUri($ret['url']); // make sure to not overwrite existing values with blank entries except some technical fields $keep = ['batch', 'notify', 'poll', 'request', 'confirm', 'poco', 'baseurl']; diff --git a/src/Model/Item.php b/src/Model/Item.php index ff4791a7cc..8b9909f8bf 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2034,9 +2034,10 @@ class Item * Posts that are created on this system are using System::createUUID. * Received ActivityPub posts are using Processor::getGUIDByURL. * - * @param string $uri uri of an item entry + * @param string $uri uri of an item entry * @param string|null $host hostname for the GUID prefix * @return string Unique guid + * @throws \Exception */ public static function guidFromUri(string $uri, string $host = null): string { @@ -2047,11 +2048,16 @@ class Item // Remove the scheme to make sure that "https" and "http" doesn't make a difference unset($parsed['scheme']); + $hostPart = $host ?? $parsed['host'] ?? ''; + if (!$hostPart) { + Logger::warning('Empty host GUID part', ['uri' => $uri, 'host' => $host, 'parsed' => $parsed, 'callstack' => System::callstack(10)]); + } + // Glue it together to be able to make a hash from it $host_id = implode('/', $parsed); // Use a mixture of several hashes to provide some GUID like experience - return hash('crc32', $host) . '-'. hash('joaat', $host_id) . '-'. hash('fnv164', $host_id); + return hash('crc32', $hostPart) . '-' . hash('joaat', $host_id) . '-' . hash('fnv164', $host_id); } /** diff --git a/src/Model/Mail.php b/src/Model/Mail.php index 6ccce24ba7..9469483d9a 100644 --- a/src/Model/Mail.php +++ b/src/Model/Mail.php @@ -59,8 +59,7 @@ class Mail } if (empty($msg['guid'])) { - $host = parse_url($msg['from-url'], PHP_URL_HOST); - $msg['guid'] = Item::guidFromUri($msg['uri'], $host); + $msg['guid'] = Item::guidFromUri($msg['uri'], parse_url($msg['from-url'], PHP_URL_HOST)); } $msg['created'] = (!empty($msg['created']) ? DateTimeFormat::utc($msg['created']) : DateTimeFormat::utcNow()); diff --git a/src/Protocol/Feed.php b/src/Protocol/Feed.php index 29b1923316..dec463820d 100644 --- a/src/Protocol/Feed.php +++ b/src/Protocol/Feed.php @@ -625,8 +625,8 @@ class Feed $notify = Item::isRemoteSelf($contact, $item); - // Distributed items should have a well formatted URI. - // Additionally we have to avoid conflicts with identical URI between imported feeds and these items. + // Distributed items should have a well-formatted URI. + // Additionally, we have to avoid conflicts with identical URI between imported feeds and these items. if ($notify) { $item['guid'] = Item::guidFromUri($orig_plink, DI::baseUrl()->getHostname()); $item['uri'] = Item::newURI($item['guid']);