X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FNetwork%2FProbe.php;h=d35490d0d366f7e4ab2b91c10b957e56649345b6;hb=0ab5fed64cdd737817489434f0f008646a28a61d;hp=d5d87ebf955057f9cc47da804f5374f742b9c862;hpb=a145712f042f54c1e4ce657be1ab8c649ea2f6f8;p=friendica.git diff --git a/src/Network/Probe.php b/src/Network/Probe.php index d5d87ebf95..d35490d0d3 100644 --- a/src/Network/Probe.php +++ b/src/Network/Probe.php @@ -1,6 +1,6 @@ isTimeout()) { Logger::info('Probing timeout', ['url' => $ssl_url]); - self::$istimeout = true; + self::$isTimeout = true; return []; } @@ -190,10 +234,10 @@ class Probe $connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0); if ($curlResult->isTimeout()) { Logger::info('Probing timeout', ['url' => $url]); - self::$istimeout = true; + self::$isTimeout = true; return []; } elseif ($connection_error && $ssl_connection_error) { - self::$istimeout = true; + self::$isTimeout = true; return []; } @@ -294,7 +338,7 @@ class Probe * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function uri($uri, $network = '', $uid = -1) + public static function uri(string $uri, string $network = '', int $uid = -1): array { // Local profiles aren't probed via network if (empty($network) && Contact::isLocal($uri)) { @@ -305,7 +349,7 @@ class Probe } if ($uid == -1) { - $uid = local_user(); + $uid = DI::userSession()->getLocalUserId(); } if (empty($network) || ($network == Protocol::ACTIVITYPUB)) { @@ -314,7 +358,7 @@ class Probe $ap_profile = []; } - self::$istimeout = false; + self::$isTimeout = false; if ($network != Protocol::ACTIVITYPUB) { $data = self::detect($uri, $network, $uid, $ap_profile); @@ -322,7 +366,13 @@ class Probe $data = []; } if (empty($data) || (!empty($ap_profile) && empty($network) && (($data['network'] ?? '') != Protocol::DFRN))) { + $networks = $data['networks'] ?? []; + unset($data['networks']); + if (!empty($data['network'])) { + $networks[$data['network']] = $data; + } $data = $ap_profile; + $data['networks'] = $networks; } elseif (!empty($ap_profile)) { $ap_profile['batch'] = ''; $data = array_merge($ap_profile, $data); @@ -443,6 +493,7 @@ class Probe * * @param array $result Result array * @param array $webfinger Webfinger data + * * @return array result Altered/unaltered result array */ private static function getSubscribeLink(array $result, array $webfinger): array @@ -464,6 +515,7 @@ class Probe * Get webfinger data from a given URI * * @param string $uri URI + * * @return array Webfinger data * @throws HTTPException\InternalServerErrorException */ @@ -521,13 +573,13 @@ class Probe $addr = $uri; $webfinger = self::getWebfinger('https://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr); - if (self::$istimeout) { + if (self::$isTimeout) { return []; } if (empty($webfinger)) { $webfinger = self::getWebfinger('http://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr); - if (self::$istimeout) { + if (self::$isTimeout) { return []; } } else { @@ -536,7 +588,7 @@ class Probe if (empty($webfinger)) { $lrdd = self::hostMeta($host); - if (self::$istimeout) { + if (self::$isTimeout) { return []; } $baseurl = self::$baseurl; @@ -579,6 +631,7 @@ class Probe * @param string $type * @param string $uri * @param string $addr + * * @return array webfinger results */ private static function getWebfinger(string $template, string $type, string $uri, string $addr): array @@ -593,7 +646,7 @@ class Probe $detected = $addr; $path = str_replace('{uri}', urlencode('acct:' . $addr), $template); $webfinger = self::webfinger($path, $type); - if (self::$istimeout) { + if (self::$isTimeout) { return []; } } @@ -603,7 +656,7 @@ class Probe $detected = $uri; $path = str_replace('{uri}', urlencode($uri), $template); $webfinger = self::webfinger($path, $type); - if (self::$istimeout) { + if (self::$isTimeout) { return []; } } @@ -643,7 +696,7 @@ class Probe } $parts = parse_url($uri); - if (empty($parts['scheme']) && empty($parts['host']) && !strstr($parts['path'], '@')) { + if (empty($parts['scheme']) && empty($parts['host']) && (empty($parts['path']) || strpos($parts['path'], '@') === false)) { Logger::info('URI was not detectable', ['uri' => $uri]); return []; } @@ -690,9 +743,13 @@ class Probe } if ((!$result && ($network == '')) || ($network == Protocol::DIASPORA)) { $result = self::diaspora($webfinger); + } else { + $result['networks'][Protocol::DIASPORA] = self::diaspora($webfinger); } if ((!$result && ($network == '')) || ($network == Protocol::OSTATUS)) { $result = self::ostatus($webfinger); + } else { + $result['networks'][Protocol::OSTATUS] = self::ostatus($webfinger); } if (in_array($network, ['', Protocol::ZOT])) { $result = self::zot($webfinger, $result, $baseurl); @@ -739,6 +796,7 @@ class Probe * @param array $webfinger Webfinger data * @param array $data previously probed data * @param string $baseUrl Base URL + * * @return array Zot data * @throws HTTPException\InternalServerErrorException */ @@ -879,6 +937,7 @@ class Probe * * @param string $url Address that should be probed * @param string $type type + * * @return array webfinger data * @throws HTTPException\InternalServerErrorException */ @@ -888,7 +947,7 @@ class Probe $curlResult = DI::httpClient()->get($url, $type, [HttpClientOptions::TIMEOUT => $xrd_timeout]); if ($curlResult->isTimeout()) { - self::$istimeout = true; + self::$isTimeout = true; return []; } $data = $curlResult->getBody(); @@ -949,6 +1008,7 @@ class Probe * * @param string $noscrape_url Link to the noscrape page * @param array $data The already fetched data + * * @return array noscrape data * @throws HTTPException\InternalServerErrorException */ @@ -956,7 +1016,7 @@ class Probe { $curlResult = DI::httpClient()->get($noscrape_url, HttpClientAccept::JSON); if ($curlResult->isTimeout()) { - self::$istimeout = true; + self::$isTimeout = true; return $data; } $content = $curlResult->getBody(); @@ -1055,7 +1115,7 @@ class Probe * * @return int Number of errors */ - public static function validDfrn($data) + public static function validDfrn(array $data): int { $errors = 0; if (!isset($data['key'])) { @@ -1218,7 +1278,7 @@ class Probe { $curlResult = DI::httpClient()->get($hcard_url, HttpClientAccept::HTML); if ($curlResult->isTimeout()) { - self::$istimeout = true; + self::$isTimeout = true; return []; } $content = $curlResult->getBody(); @@ -1262,9 +1322,19 @@ class Probe $data['name'] = $search->item(0)->nodeValue; } + $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' given_name ')]", $vcard); // */ + if ($search->length > 0) { + $data["given_name"] = $search->item(0)->nodeValue; + } + + $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' family_name ')]", $vcard); // */ + if ($search->length > 0) { + $data["family_name"] = $search->item(0)->nodeValue; + } + $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' searchable ')]", $vcard); // */ if ($search->length > 0) { - $data['searchable'] = $search->item(0)->nodeValue; + $data['hide'] = (strtolower($search->item(0)->nodeValue) != 'true'); } $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' key ')]", $vcard); // */ @@ -1281,7 +1351,7 @@ class Probe } } - $avatar = []; + $avatars = []; if (!empty($vcard)) { $photos = $xpath->query("//*[contains(concat(' ', @class, ' '), ' photo ') or contains(concat(' ', @class, ' '), ' avatar ')]", $vcard); // */ foreach ($photos as $photo) { @@ -1291,20 +1361,27 @@ class Probe } if (isset($attr['src']) && isset($attr['width'])) { - $avatar[$attr['width']] = $attr['src']; + $avatars[$attr['width']] = self::fixAvatar($attr['src'], $data['baseurl']); } // We don't have a width. So we just take everything that we got. // This is a Hubzilla workaround which doesn't send a width. - if ((sizeof($avatar) == 0) && !empty($attr['src'])) { - $avatar[] = $attr['src']; + if (!$avatars && !empty($attr['src'])) { + $avatars[] = self::fixAvatar($attr['src'], $data['baseurl']); } } } - if (sizeof($avatar)) { - ksort($avatar); - $data['photo'] = self::fixAvatar(array_pop($avatar), $data['baseurl']); + if ($avatars) { + ksort($avatars); + $data['photo'] = array_pop($avatars); + if ($avatars) { + $data['photo_medium'] = array_pop($avatars); + } + + if ($avatars) { + $data['photo_small'] = array_pop($avatars); + } } if ($dfrn) { @@ -1328,7 +1405,6 @@ class Probe } } - return $data; } @@ -1336,6 +1412,7 @@ class Probe * Check for Diaspora contact * * @param array $webfinger Webfinger data + * * @return array Diaspora data * @throws HTTPException\InternalServerErrorException */ @@ -1430,6 +1507,7 @@ class Probe * * @param array $webfinger Webfinger data * @param bool $short Short detection mode + * * @return array|bool OStatus data or "false" on error or "true" on short mode * @throws HTTPException\InternalServerErrorException */ @@ -1476,19 +1554,17 @@ class Probe } elseif (Strings::normaliseLink($pubkey) == 'http://') { $curlResult = DI::httpClient()->get($pubkey, HttpClientAccept::MAGIC_KEY); if ($curlResult->isTimeout()) { - self::$istimeout = true; + self::$isTimeout = true; return $short ? false : []; } Logger::debug('Fetched public key', ['Content-Type' => $curlResult->getHeader('Content-Type'), 'url' => $pubkey]); $pubkey = $curlResult->getBody(); } - $key = explode('.', $pubkey); + try { + $data['pubkey'] = Salmon::magicKeyToPem($pubkey); + } catch (\Throwable $e) { - if (sizeof($key) >= 3) { - $m = Strings::base64UrlDecode($key[1]); - $e = Strings::base64UrlDecode($key[2]); - $data['pubkey'] = Crypto::meToPem($m, $e); } } } @@ -1511,7 +1587,7 @@ class Probe // Fetch all additional data from the feed $curlResult = DI::httpClient()->get($data['poll'], HttpClientAccept::FEED_XML); if ($curlResult->isTimeout()) { - self::$istimeout = true; + self::$isTimeout = true; return []; } $feed = $curlResult->getBody(); @@ -1556,6 +1632,7 @@ class Probe * Fetch data from a pump.io profile page * * @param string $profile_link Link to the profile page + * * @return array Profile data */ private static function pumpioProfileData(string $profile_link): array @@ -1616,6 +1693,7 @@ class Probe * * @param array $webfinger Webfinger data * @param string $addr + * * @return array pump.io data */ private static function pumpio(array $webfinger, string $addr): array @@ -1673,6 +1751,7 @@ class Probe * * @param string $url Page link * @param string $body Page body string + * * @return string|false Feed link or false if body was invalid HTML document */ public static function getFeedLink(string $url, string $body) @@ -1689,6 +1768,7 @@ class Probe $xpath = new DOMXPath($doc); $feedUrl = $xpath->evaluate('string(/html/head/link[@type="application/rss+xml" and @rel="alternate"]/@href)'); + $feedUrl = $feedUrl ?: $xpath->evaluate('string(/html/head/link[@type="application/atom+xml" and @rel="alternate"]/@href)'); $feedUrl = $feedUrl ? self::ensureAbsoluteLinkFromHTMLDoc($feedUrl, $url, $xpath) : ''; @@ -1705,6 +1785,7 @@ class Probe * @param string $href The potential relative href found in the HTML document * @param string $base The HTML document URL * @param DOMXPath $xpath The HTML document XPath + * * @return string Absolute URL */ private static function ensureAbsoluteLinkFromHTMLDoc(string $href, string $base, DOMXPath $xpath): string @@ -1766,6 +1847,7 @@ class Probe * * @param string $url Profile link * @param boolean $probe Do a probe if the page contains a feed link + * * @return array feed data * @throws HTTPException\InternalServerErrorException */ @@ -1773,7 +1855,7 @@ class Probe { $curlResult = DI::httpClient()->get($url, HttpClientAccept::FEED_XML); if ($curlResult->isTimeout()) { - self::$istimeout = true; + self::$isTimeout = true; return []; } $feed = $curlResult->getBody(); @@ -1822,6 +1904,7 @@ class Probe * * @param string $uri Profile link * @param integer $uid User ID + * * @return array mail data * @throws \Exception */ @@ -1862,22 +1945,26 @@ class Probe $phost = substr($uri, strpos($uri, '@') + 1); - $data = []; - $data['addr'] = $uri; - $data['network'] = Protocol::MAIL; - $data['name'] = substr($uri, 0, strpos($uri, '@')); + $data = [ + 'addr' => $uri, + 'network' => Protocol::MAIL, + 'name' => substr($uri, 0, strpos($uri, '@')), + 'photo' => Network::lookupAvatarByEmail($uri), + 'url' => 'mailto:' . $uri, + 'notify' => 'smtp ' . Strings::getRandomHex(), + 'poll' => 'email ' . Strings::getRandomHex(), + ]; + $data['nick'] = $data['name']; - $data['photo'] = Network::lookupAvatarByEmail($uri); - $data['url'] = 'mailto:'.$uri; - $data['notify'] = 'smtp ' . Strings::getRandomHex(); - $data['poll'] = 'email ' . Strings::getRandomHex(); $x = Email::messageMeta($mbox, $msgs[0]); + if (stristr($x[0]->from, $uri)) { $adr = imap_rfc822_parse_adrlist($x[0]->from, ''); } elseif (stristr($x[0]->to, $uri)) { $adr = imap_rfc822_parse_adrlist($x[0]->to, ''); } + if (isset($adr)) { foreach ($adr as $feadr) { if ((strcasecmp($feadr->mailbox, $data['name']) == 0) @@ -1896,9 +1983,11 @@ class Probe } } } + if (!empty($mbox)) { imap_close($mbox); } + return $data; } @@ -1907,6 +1996,7 @@ class Probe * * @param string $avatar Path to the avatar * @param string $base Another path that is hopefully complete + * * @return string fixed avatar path * @throws \Exception */ @@ -1943,6 +2033,7 @@ class Probe * Fetch the last date that the contact had posted something (publically) * * @param array $data probing result + * * @return string last activity */ public static function getLastUpdate(array $data): string @@ -1974,6 +2065,7 @@ class Probe * Fetch the last activity date from the "noscrape" endpoint * * @param array $data Probing result + * * @return string last activity or true if update was successful or the server was unreachable */ private static function updateFromNoScrape(array $data): string @@ -2006,6 +2098,7 @@ class Probe * * @param string $feed * @param array $data Probing result + * * @return string last activity * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ @@ -2110,6 +2203,7 @@ class Probe * Probe data from local profiles without network traffic * * @param string $url + * * @return array probed data * @throws HTTPException\InternalServerErrorException * @throws HTTPException\NotFoundException @@ -2125,26 +2219,49 @@ class Probe $owner = User::getOwnerDataById($uid); $approfile = ActivityPub\Transmitter::getProfile($uid); + $split_name = Diaspora::splitName($owner['name']); + if (empty($owner['gsid'])) { $owner['gsid'] = GServer::getID($approfile['generator']['url']); } $data = [ - 'name' => $owner['name'], 'nick' => $owner['nick'], 'guid' => $approfile['diaspora:guid'] ?? '', - 'url' => $owner['url'], 'addr' => $owner['addr'], 'alias' => $owner['alias'], - 'photo' => User::getAvatarUrl($owner), - 'header' => $owner['header'] ? Contact::getHeaderUrlForId($owner['id'], $owner['updated']) : '', - 'account-type' => $owner['contact-type'], 'community' => ($owner['contact-type'] == User::ACCOUNT_TYPE_COMMUNITY), - 'keywords' => $owner['keywords'], 'location' => $owner['location'], 'about' => $owner['about'], - 'xmpp' => $owner['xmpp'], 'matrix' => $owner['matrix'], - 'hide' => !$owner['net-publish'], 'batch' => '', 'notify' => $owner['notify'], - 'poll' => $owner['poll'], 'request' => $owner['request'], 'confirm' => $owner['confirm'], - 'subscribe' => $approfile['generator']['url'] . '/follow?url={uri}', 'poco' => $owner['poco'], - 'following' => $approfile['following'], 'followers' => $approfile['followers'], - 'inbox' => $approfile['inbox'], 'outbox' => $approfile['outbox'], - 'sharedinbox' => $approfile['endpoints']['sharedInbox'], 'network' => Protocol::DFRN, - 'pubkey' => $owner['upubkey'], 'baseurl' => $approfile['generator']['url'], 'gsid' => $owner['gsid'], - 'manually-approve' => in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]) + 'name' => $owner['name'], 'nick' => $owner['nick'], 'guid' => $approfile['diaspora:guid'] ?? '', + 'url' => $owner['url'], 'addr' => $owner['addr'], 'alias' => $owner['alias'], + 'photo' => User::getAvatarUrl($owner), + 'header' => $owner['header'] ? Contact::getHeaderUrlForId($owner['id'], $owner['updated']) : '', + 'account-type' => $owner['contact-type'], 'community' => ($owner['contact-type'] == User::ACCOUNT_TYPE_COMMUNITY), + 'keywords' => $owner['keywords'], 'location' => $owner['location'], 'about' => $owner['about'], + 'xmpp' => $owner['xmpp'], 'matrix' => $owner['matrix'], + 'hide' => !$owner['net-publish'], 'batch' => '', 'notify' => $owner['notify'], + 'poll' => $owner['poll'], 'request' => $owner['request'], 'confirm' => $owner['confirm'], + 'subscribe' => $approfile['generator']['url'] . '/contact/follow?url={uri}', 'poco' => $owner['poco'], + 'following' => $approfile['following'], 'followers' => $approfile['followers'], + 'inbox' => $approfile['inbox'], 'outbox' => $approfile['outbox'], + 'sharedinbox' => $approfile['endpoints']['sharedInbox'], 'network' => Protocol::DFRN, + 'pubkey' => $owner['upubkey'], 'baseurl' => $approfile['generator']['url'], 'gsid' => $owner['gsid'], + 'manually-approve' => in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]), + 'networks' => [ + Protocol::DIASPORA => [ + 'name' => $owner['name'], + 'given_name' => $split_name['first'], + 'family_name' => $split_name['last'], + 'nick' => $owner['nick'], + 'guid' => $approfile['diaspora:guid'], + 'url' => $owner['url'], + 'addr' => $owner['addr'], + 'alias' => $owner['alias'], + 'photo' => $owner['photo'], + 'photo_medium' => $owner['thumb'], + 'photo_small' => $owner['micro'], + 'batch' => $approfile['generator']['url'] . '/receive/public', + 'notify' => $owner['notify'], + 'poll' => $owner['poll'], + 'poco' => $owner['poco'], + 'network' => Protocol::DIASPORA, + 'pubkey' => $owner['upubkey'], + ] + ] ]; } catch (Exception $e) { // Default values for non existing targets