X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FNetwork%2FProbe.php;h=a0887d5cba877cb5187acccdf6c09017c261b1a6;hb=e1863951986ba5be173758324a00652bc5af870c;hp=5c188f2522bf95c77f914f73ce96393125dab5b5;hpb=55640eec873e7f274824c6ed904672ebd75a7691;p=friendica.git diff --git a/src/Network/Probe.php b/src/Network/Probe.php index 5c188f2522..a0887d5cba 100644 --- a/src/Network/Probe.php +++ b/src/Network/Probe.php @@ -1,6 +1,6 @@ $data['url'], 'photo' => $data['photo']]); + unset($data['photo']); + } + } + $newdata = []; foreach ($fields as $field) { if (isset($data[$field])) { if (in_array($field, $numeric_fields)) { $newdata[$field] = (int)$data[$field]; } else { - $newdata[$field] = $data[$field]; + $newdata[$field] = trim($data[$field]); } } elseif (!in_array($field, $numeric_fields)) { $newdata[$field] = ''; @@ -133,6 +144,26 @@ class Probe } } + $newdata['networks'] = []; + foreach ([Protocol::DIASPORA, Protocol::OSTATUS] as $network) { + if (!empty($data['networks'][$network])) { + $data['networks'][$network]['subscribe'] = $newdata['subscribe'] ?? ''; + if (empty($data['networks'][$network]['baseurl'])) { + $data['networks'][$network]['baseurl'] = $newdata['baseurl'] ?? ''; + } else { + $newdata['baseurl'] = $data['networks'][$network]['baseurl']; + } + if (!empty($newdata['baseurl'])) { + $newdata['gsid'] = $data['networks'][$network]['gsid'] = GServer::getID($newdata['baseurl']); + } else { + $newdata['gsid'] = $data['networks'][$network]['gsid'] = null; + } + + $newdata['networks'][$network] = self::rearrangeData($data['networks'][$network]); + unset($newdata['networks'][$network]['networks']); + } + } + // We don't use the "priority" field anymore and replace it with a dummy. $newdata['priority'] = 0; @@ -147,7 +178,7 @@ class Probe */ private static function ownHost(string $host): bool { - $own_host = DI::baseUrl()->getHostname(); + $own_host = DI::baseUrl()->getHost(); $parts = parse_url($host); @@ -310,7 +341,6 @@ class Probe * @param string $uri Address that should be probed * @param string $network Test for this specific network * @param integer $uid User ID for the probe (only used for mails) - * @param boolean $cache Use cached values? * * @return array uri data * @throws HTTPException\InternalServerErrorException @@ -344,7 +374,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); @@ -379,14 +415,19 @@ class Probe } } - if (!empty($data['baseurl']) && empty($data['gsid'])) { - $data['gsid'] = GServer::getID($data['baseurl']); - } - if (empty($data['network'])) { $data['network'] = Protocol::PHANTOM; } + $baseurl = parse_url($data['url'], PHP_URL_SCHEME) . '://' . parse_url($data['url'], PHP_URL_HOST); + if (empty($data['baseurl']) && ($data['network'] == Protocol::ACTIVITYPUB) && (rtrim($data['url'], '/') == $baseurl)) { + $data['baseurl'] = $baseurl; + } + + if (!empty($data['baseurl']) && empty($data['gsid'])) { + $data['gsid'] = GServer::getID($data['baseurl']); + } + // Ensure that local connections always are DFRN if (($network == '') && ($data['network'] != Protocol::PHANTOM) && (self::ownHost($data['baseurl'] ?? '') || self::ownHost($data['url']))) { $data['network'] = Protocol::DFRN; @@ -491,7 +532,7 @@ class Probe * @return array Webfinger data * @throws HTTPException\InternalServerErrorException */ - private static function getWebfingerArray(string $uri): array + public static function getWebfingerArray(string $uri): array { $parts = parse_url($uri); @@ -668,7 +709,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 []; } @@ -715,21 +756,25 @@ 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); } if ((!$result && ($network == '')) || ($network == Protocol::PUMPIO)) { - $result = self::pumpio($webfinger, $addr); + $result = self::pumpio($webfinger, $addr, $baseurl); } if (empty($result['network']) && empty($ap_profile['network']) || ($network == Protocol::FEED)) { $result = self::feed($uri); } else { - // We overwrite the detected nick with our try if the previois routines hadn't detected it. - // Additionally it is overwritten when the nickname doesn't make sense (contains spaces). + // We overwrite the detected nick with our try if the previous routines hadn't detected it. + // Additionally, it is overwritten when the nickname doesn't make sense (contains spaces). if ((empty($result['nick']) || (strstr($result['nick'], ' '))) && ($nick != '')) { $result['nick'] = $nick; } @@ -911,9 +956,17 @@ class Probe */ public static function webfinger(string $url, string $type): array { - $xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20); + try { + $curlResult = DI::httpClient()->get( + $url, + $type, + [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout', 20)] + ); + } catch (\Throwable $e) { + Logger::notice($e->getMessage(), ['url' => $url, 'type' => $type, 'class' => get_class($e)]); + return []; + } - $curlResult = DI::httpClient()->get($url, $type, [HttpClientOptions::TIMEOUT => $xrd_timeout]); if ($curlResult->isTimeout()) { self::$isTimeout = true; return []; @@ -1132,7 +1185,6 @@ class Probe $data = self::pollHcard($profile_link, $data, true); } - $prof_data = []; if (empty($data['addr']) || empty($data['nick'])) { $probe_data = self::uri($profile_link); @@ -1140,15 +1192,17 @@ class Probe $data['nick'] = ($data['nick'] ?? '') ?: $probe_data['nick']; } - $prof_data['addr'] = $data['addr']; - $prof_data['nick'] = $data['nick']; - $prof_data['dfrn-request'] = $data['request'] ?? null; - $prof_data['dfrn-confirm'] = $data['confirm'] ?? null; - $prof_data['dfrn-notify'] = $data['notify'] ?? null; - $prof_data['dfrn-poll'] = $data['poll'] ?? null; - $prof_data['photo'] = $data['photo'] ?? null; - $prof_data['fn'] = $data['name'] ?? null; - $prof_data['key'] = $data['pubkey'] ?? null; + $prof_data = [ + 'addr' => $data['addr'], + 'nick' => $data['nick'], + 'dfrn-request' => $data['request'] ?? null, + 'dfrn-confirm' => $data['confirm'] ?? null, + 'dfrn-notify' => $data['notify'] ?? null, + 'dfrn-poll' => $data['poll'] ?? null, + 'photo' => $data['photo'] ?? null, + 'fn' => $data['name'] ?? null, + 'key' => $data['pubkey'] ?? null, + ]; Logger::debug('Result', ['link' => $profile_link, 'data' => $prof_data]); @@ -1290,9 +1344,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); // */ @@ -1309,7 +1373,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) { @@ -1319,20 +1383,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) { @@ -1356,7 +1427,6 @@ class Probe } } - return $data; } @@ -1566,7 +1636,7 @@ class Probe if (!empty($feed_data['header']['author-about'])) { $data['about'] = $feed_data['header']['author-about']; } - // OStatus has serious issues when the the url doesn't fit (ssl vs. non ssl) + // OStatus has serious issues when the url doesn't fit (ssl vs. non ssl) // So we take the value that we just fetched, although the other one worked as well if (!empty($feed_data['header']['author-link'])) { $data['url'] = $feed_data['header']['author-link']; @@ -1587,7 +1657,7 @@ class Probe * * @return array Profile data */ - private static function pumpioProfileData(string $profile_link): array + private static function pumpioProfileData(string $profile_link, string $baseurl): array { $curlResult = DI::httpClient()->get($profile_link, HttpClientAccept::HTML); if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { @@ -1631,8 +1701,8 @@ class Probe } if ($avatar) { foreach ($avatar->attributes as $attribute) { - if ($attribute->name == 'src') { - $data['photo'] = trim($attribute->value); + if (($attribute->name == 'src') && !empty($attribute->value)) { + $data['photo'] = Network::addBasePath($attribute->value, $baseurl); } } } @@ -1648,7 +1718,7 @@ class Probe * * @return array pump.io data */ - private static function pumpio(array $webfinger, string $addr): array + private static function pumpio(array $webfinger, string $addr, string $baseurl): array { $data = []; // The array is reversed to take into account the order of preference for same-rel links @@ -1680,7 +1750,7 @@ class Probe return []; } - $profile_data = self::pumpioProfileData($data['url']); + $profile_data = self::pumpioProfileData($data['url'], $baseurl); if (!$profile_data) { return []; @@ -1805,11 +1875,18 @@ class Probe */ private static function feed(string $url, bool $probe = true): array { - $curlResult = DI::httpClient()->get($url, HttpClientAccept::FEED_XML); + try { + $curlResult = DI::httpClient()->get($url, HttpClientAccept::FEED_XML); + } catch(\Throwable $e) { + DI::logger()->info('Error requesting feed URL', ['url' => $url, 'exception' => $e]); + return []; + } + if ($curlResult->isTimeout()) { self::$isTimeout = true; return []; } + $feed = $curlResult->getBody(); $feed_data = Feed::import($feed); @@ -2171,6 +2248,8 @@ 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']); } @@ -2190,10 +2269,31 @@ class Probe '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]) + '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 + // Default values for nonexistent targets $data = [ 'name' => $url, 'nick' => $url, 'url' => $url, 'network' => Protocol::PHANTOM, 'photo' => DI::baseUrl() . Contact::DEFAULT_AVATAR_PHOTO