From f387d85cdbbe181964015cfeb85582871f2b5e1c Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 8 Apr 2019 20:41:18 +0000 Subject: [PATCH] Fetch profile data from different contact tables when we won't or can't probe via network --- src/Model/Contact.php | 122 +++++++++++++++++++++++------------ src/Protocol/ActivityPub.php | 7 +- 2 files changed, 85 insertions(+), 44 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index fe373ae94a..2072044b30 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1114,6 +1114,81 @@ class Contact extends BaseObject )", intval($uid), intval($uid)); } + /** + * Have a look at all contact tables for a given profile url. + * This function works as a replacement for probing the contact. + * + * @param string $url Contact URL + * + * @return array Contact array in the "probe" structure + */ + private static function getProbeDataFromDatabase($url) + { + // The link could be provided as http although we stored it as https + $ssl_url = str_replace('http://', 'https://', $url); + + $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick', + 'photo', 'keywords', 'location', 'about', 'network', + 'priority', 'batch', 'request', 'confirm', 'poco']; + $data = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url)]); + + if (!DBA::isResult($contact)) { + $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]]; + $data = DBA::selectFirst('contact', $fields, $condition); + } + + if (DBA::isResult($data)) { + // For security reasons we don't fetch key data from our users + $data["pubkey"] = ''; + return $data; + } + + $fields = ['url', 'addr', 'alias', 'notify', 'name', 'nick', + 'photo', 'keywords', 'location', 'about', 'network']; + $data = DBA::selectFirst('gcontact', $fields, ['nurl' => Strings::normaliseLink($url)]); + + if (!DBA::isResult($contact)) { + $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]]; + $data = DBA::selectFirst('contact', $fields, $condition); + } + + if (DBA::isResult($data)) { + $data["pubkey"] = ''; + $data["poll"] = ''; + $data["priority"] = 0; + $data["batch"] = ''; + $data["request"] = ''; + $data["confirm"] = ''; + $data["poco"] = ''; + return $data; + } + + $data = ActivityPub::probeProfile($url, false); + if (!empty($data)) { + return $data; + } + + $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick', + 'photo', 'network', 'priority', 'batch', 'request', 'confirm']; + $data = DBA::selectFirst('fcontact', $fields, ['url' => $url]); + + if (!DBA::isResult($contact)) { + $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]]; + $data = DBA::selectFirst('contact', $fields, $condition); + } + + if (DBA::isResult($data)) { + $data["pubkey"] = ''; + $data["keywords"] = ''; + $data["location"] = ''; + $data["about"] = ''; + $data["poco"] = ''; + return $data; + } + + return []; + } + /** * @brief Fetch the contact id for a given URL and user * @@ -1187,17 +1262,9 @@ class Contact extends BaseObject return 0; } - // When we don't want to update, we look if some of our users already know this contact - if ($no_update) { - $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick', - 'photo', 'keywords', 'location', 'about', 'network', - 'priority', 'batch', 'request', 'confirm', 'poco']; - $data = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url)]); - - if (DBA::isResult($data)) { - // For security reasons we don't fetch key data from our users - $data["pubkey"] = ''; - } + // When we don't want to update, we look if we know this contact in any way + if ($no_update && empty($default)) { + $data = self::getProbeDataFromDatabase($url); } else { $data = []; } @@ -1217,40 +1284,13 @@ class Contact extends BaseObject return 0; } - // Get data from the gcontact table - $fields = ['name', 'nick', 'url', 'photo', 'addr', 'alias', 'network']; - $contact = DBA::selectFirst('gcontact', $fields, ['nurl' => Strings::normaliseLink($url)]); - if (!DBA::isResult($contact)) { - $contact = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url)]); - } - - if (!DBA::isResult($contact)) { - $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick', - 'photo', 'keywords', 'location', 'about', 'network', - 'priority', 'batch', 'request', 'confirm', 'poco']; - $contact = DBA::selectFirst('contact', $fields, ['addr' => $url]); - } - - // The link could be provided as http although we stored it as https - $ssl_url = str_replace('http://', 'https://', $url); - - if (!DBA::isResult($contact)) { - $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]]; - $contact = DBA::selectFirst('contact', $fields, $condition); - } - - if (!DBA::isResult($contact)) { - $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick', - 'photo', 'network', 'priority', 'batch', 'request', 'confirm']; - $condition = ['url' => [$url, Strings::normaliseLink($url), $ssl_url]]; - $contact = DBA::selectFirst('fcontact', $fields, $condition); - } - if (!empty($default)) { $contact = $default; + } else { + $contact = self::getProbeDataFromDatabase($url); } - if (!DBA::isResult($contact)) { + if (!empty($contact)) { return 0; } else { $data = array_merge($data, $contact); diff --git a/src/Protocol/ActivityPub.php b/src/Protocol/ActivityPub.php index bf8fed94c6..22073a5028 100644 --- a/src/Protocol/ActivityPub.php +++ b/src/Protocol/ActivityPub.php @@ -89,14 +89,15 @@ class ActivityPub /** * Fetches a profile from the given url into an array that is compatible to Probe::uri * - * @param string $url profile url + * @param string $url profile url + * @param boolean $update Update the profile * @return array * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function probeProfile($url) + public static function probeProfile($url, $update = true) { - $apcontact = APContact::getByURL($url, true); + $apcontact = APContact::getByURL($url, $update); if (empty($apcontact)) { return false; } -- 2.39.5