From c3bfa9f1c151e12b8854e189140f35aec928e512 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 18 Aug 2020 16:42:01 +0000 Subject: [PATCH 1/4] Don't probe on local profiles via network --- src/Network/Probe.php | 54 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/src/Network/Probe.php b/src/Network/Probe.php index c841b9ce4..467c79a15 100644 --- a/src/Network/Probe.php +++ b/src/Network/Probe.php @@ -23,13 +23,13 @@ namespace Friendica\Network; use DOMDocument; use DomXPath; -use Friendica\Core\Cache\Duration; use Friendica\Core\Hook; use Friendica\Core\Logger; use Friendica\Core\Protocol; use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Model\Contact; use Friendica\Model\GServer; use Friendica\Model\Profile; use Friendica\Model\User; @@ -330,6 +330,14 @@ class Probe */ public static function uri($uri, $network = '', $uid = -1) { + // Local profiles aren't probed via network + if (empty($network) && strpos($uri, DI::baseUrl()->getHostname())) { + $data = self::localProbe($uri); + if (!empty($data)) { + return $data; + } + } + if ($uid == -1) { $uid = local_user(); } @@ -2156,4 +2164,48 @@ class Probe return ''; } + + public static function localProbe($url) + { + $fields = ['uid', 'url', 'name', 'nick', 'addr', 'alias', 'photo', 'contact-type', 'keywords', + 'location', 'about', 'notify', 'poll', 'request', 'confirm', 'poco', 'pubkey', 'gsid']; + $self = Contact::selectFirst($fields, ['self' => true, 'nurl' => Strings::normaliseLink($url)]); + if (empty($self)) { + $self = Contact::selectFirst($fields, ['self' => true, 'addr' => $url]); + } + if (empty($self)) { + $self = Contact::selectFirst($fields, ['self' => true, 'alias' => [$url, Strings::normaliseLink($url)]]); + } + if (empty($self)) { + return []; + } + + $profile = Profile::getByUID($self['uid']); + if (empty($profile)) { + return []; + } + + $approfile = ActivityPub\Transmitter::getProfile($self['uid']); + if (empty($approfile)) { + return []; + } + + if (empty($self['gsid'])) { + $self['gsid'] = GServer::getID($approfile['generator']['url']); + } + + $data = ['name' => $self['name'], 'nick' => $self['nick'], 'guid' => $approfile['diaspora:guid'], + 'url' => $self['url'], 'addr' => $self['addr'], 'alias' => $self['alias'], + 'photo' => $self['photo'], 'account-type' => $self['contact-type'], + 'community' => ($self['contact-type'] == Contact::TYPE_COMMUNITY), + 'keywords' => $self['keywords'], 'location' => $self['location'], 'about' => $self['about'], + 'hide' => !$profile['net-publish'], 'batch' => '', 'notify' => $self['notify'], + 'poll' => $self['poll'], 'request' => $self['request'], 'confirm' => $self['confirm'], + 'subscribe' => $approfile['generator']['url'] . '/follow?url={uri}', 'poco' => $self['poco'], + 'following' => $approfile['following'], 'followers' => $approfile['followers'], + 'inbox' => $approfile['inbox'], 'outbox' => $approfile['outbox'], + 'sharedinbox' => $approfile['endpoints']['sharedInbox'], 'network' => Protocol::DFRN, + 'pubkey' => $self['pubkey'], 'baseurl' => $approfile['generator']['url'], 'gsid' => $self['gsid']]; + return self::rearrangeData($data); + } } From f1a8db4e7638f0d0021cd2a349d86d620e5beaea Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 18 Aug 2020 17:30:48 +0000 Subject: [PATCH 2/4] Use the owner data to collect all data --- src/Network/Probe.php | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/Network/Probe.php b/src/Network/Probe.php index 467c79a15..01cd886fb 100644 --- a/src/Network/Probe.php +++ b/src/Network/Probe.php @@ -2167,20 +2167,18 @@ class Probe public static function localProbe($url) { - $fields = ['uid', 'url', 'name', 'nick', 'addr', 'alias', 'photo', 'contact-type', 'keywords', - 'location', 'about', 'notify', 'poll', 'request', 'confirm', 'poco', 'pubkey', 'gsid']; - $self = Contact::selectFirst($fields, ['self' => true, 'nurl' => Strings::normaliseLink($url)]); - if (empty($self)) { - $self = Contact::selectFirst($fields, ['self' => true, 'addr' => $url]); + $self = Contact::selectFirst(['uid'], ['self' => true, 'nurl' => Strings::normaliseLink($url)]); + if (empty($self['uid'])) { + $self = Contact::selectFirst(['uid'], ['self' => true, 'addr' => $url]); } - if (empty($self)) { - $self = Contact::selectFirst($fields, ['self' => true, 'alias' => [$url, Strings::normaliseLink($url)]]); + if (empty($self['uid'])) { + $self = Contact::selectFirst(['uid'], ['self' => true, 'alias' => [$url, Strings::normaliseLink($url)]]); } - if (empty($self)) { + if (empty($self['uid'])) { return []; } - $profile = Profile::getByUID($self['uid']); + $profile = User::getOwnerDataById($self['uid']); if (empty($profile)) { return []; } @@ -2190,22 +2188,22 @@ class Probe return []; } - if (empty($self['gsid'])) { - $self['gsid'] = GServer::getID($approfile['generator']['url']); + if (empty($profile['gsid'])) { + $profile['gsid'] = GServer::getID($approfile['generator']['url']); } - $data = ['name' => $self['name'], 'nick' => $self['nick'], 'guid' => $approfile['diaspora:guid'], - 'url' => $self['url'], 'addr' => $self['addr'], 'alias' => $self['alias'], - 'photo' => $self['photo'], 'account-type' => $self['contact-type'], - 'community' => ($self['contact-type'] == Contact::TYPE_COMMUNITY), - 'keywords' => $self['keywords'], 'location' => $self['location'], 'about' => $self['about'], - 'hide' => !$profile['net-publish'], 'batch' => '', 'notify' => $self['notify'], - 'poll' => $self['poll'], 'request' => $self['request'], 'confirm' => $self['confirm'], - 'subscribe' => $approfile['generator']['url'] . '/follow?url={uri}', 'poco' => $self['poco'], + $data = ['name' => $profile['name'], 'nick' => $profile['nick'], 'guid' => $approfile['diaspora:guid'], + 'url' => $profile['url'], 'addr' => $profile['addr'], 'alias' => $profile['alias'], + 'photo' => $profile['photo'], 'account-type' => $profile['contact-type'], + 'community' => ($profile['contact-type'] == Contact::TYPE_COMMUNITY), + 'keywords' => $profile['keywords'], 'location' => $profile['location'], 'about' => $profile['about'], + 'hide' => !$profile['net-publish'], 'batch' => '', 'notify' => $profile['notify'], + 'poll' => $profile['poll'], 'request' => $profile['request'], 'confirm' => $profile['confirm'], + 'subscribe' => $approfile['generator']['url'] . '/follow?url={uri}', 'poco' => $profile['poco'], 'following' => $approfile['following'], 'followers' => $approfile['followers'], 'inbox' => $approfile['inbox'], 'outbox' => $approfile['outbox'], 'sharedinbox' => $approfile['endpoints']['sharedInbox'], 'network' => Protocol::DFRN, - 'pubkey' => $self['pubkey'], 'baseurl' => $approfile['generator']['url'], 'gsid' => $self['gsid']]; + 'pubkey' => $profile['upubkey'], 'baseurl' => $approfile['generator']['url'], 'gsid' => $profile['gsid']]; return self::rearrangeData($data); } } From 5aba1df497379db0273249b8de5ca00aa4dc26bb Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 18 Aug 2020 19:45:01 +0000 Subject: [PATCH 3/4] Use "User::getIdForURL" --- src/Model/User.php | 20 +++++++++++++++----- src/Network/Probe.php | 25 ++++++++++++------------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/Model/User.php b/src/Model/User.php index 9f3cadb37..38fe3b0ec 100644 --- a/src/Model/User.php +++ b/src/Model/User.php @@ -162,14 +162,24 @@ class User * @return integer user id * @throws Exception */ - public static function getIdForURL($url) + public static function getIdForURL(string $url) { - $self = DBA::selectFirst('contact', ['uid'], ['nurl' => Strings::normaliseLink($url), 'self' => true]); - if (!DBA::isResult($self)) { - return false; - } else { + $self = Contact::selectFirst(['uid'], ['self' => true, 'nurl' => Strings::normaliseLink($url)]); + if (!empty($self['uid'])) { return $self['uid']; } + + $self = Contact::selectFirst(['uid'], ['self' => true, 'addr' => $url]); + if (!empty($self['uid'])) { + return $self['uid']; + } + + $self = Contact::selectFirst(['uid'], ['self' => true, 'alias' => [$url, Strings::normaliseLink($url)]]); + if (!empty($self['uid'])) { + return $self['uid']; + } + + return 0; } /** diff --git a/src/Network/Probe.php b/src/Network/Probe.php index 01cd886fb..9f6b3a485 100644 --- a/src/Network/Probe.php +++ b/src/Network/Probe.php @@ -29,7 +29,6 @@ use Friendica\Core\Protocol; use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\DI; -use Friendica\Model\Contact; use Friendica\Model\GServer; use Friendica\Model\Profile; use Friendica\Model\User; @@ -2165,25 +2164,25 @@ class Probe return ''; } - public static function localProbe($url) + /** + * Probe data from local profiles without network traffic + * + * @param string $url + * @return array probed data + */ + public static function localProbe(string $url) { - $self = Contact::selectFirst(['uid'], ['self' => true, 'nurl' => Strings::normaliseLink($url)]); - if (empty($self['uid'])) { - $self = Contact::selectFirst(['uid'], ['self' => true, 'addr' => $url]); - } - if (empty($self['uid'])) { - $self = Contact::selectFirst(['uid'], ['self' => true, 'alias' => [$url, Strings::normaliseLink($url)]]); - } - if (empty($self['uid'])) { + $uid = User::getIdForURL($url); + if (empty($uid)) { return []; } - $profile = User::getOwnerDataById($self['uid']); + $profile = User::getOwnerDataById($uid); if (empty($profile)) { return []; } - $approfile = ActivityPub\Transmitter::getProfile($self['uid']); + $approfile = ActivityPub\Transmitter::getProfile($uid); if (empty($approfile)) { return []; } @@ -2195,7 +2194,7 @@ class Probe $data = ['name' => $profile['name'], 'nick' => $profile['nick'], 'guid' => $approfile['diaspora:guid'], 'url' => $profile['url'], 'addr' => $profile['addr'], 'alias' => $profile['alias'], 'photo' => $profile['photo'], 'account-type' => $profile['contact-type'], - 'community' => ($profile['contact-type'] == Contact::TYPE_COMMUNITY), + 'community' => ($profile['contact-type'] == User::ACCOUNT_TYPE_COMMUNITY), 'keywords' => $profile['keywords'], 'location' => $profile['location'], 'about' => $profile['about'], 'hide' => !$profile['net-publish'], 'batch' => '', 'notify' => $profile['notify'], 'poll' => $profile['poll'], 'request' => $profile['request'], 'confirm' => $profile['confirm'], From 76bcf27130437c0d27ea543ac4e1f63c412fdc90 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 18 Aug 2020 19:48:14 +0000 Subject: [PATCH 4/4] "localProbe" is used only locally --- src/Network/Probe.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Network/Probe.php b/src/Network/Probe.php index 9f6b3a485..3524d0072 100644 --- a/src/Network/Probe.php +++ b/src/Network/Probe.php @@ -2170,7 +2170,7 @@ class Probe * @param string $url * @return array probed data */ - public static function localProbe(string $url) + private static function localProbe(string $url) { $uid = User::getIdForURL($url); if (empty($uid)) {