X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FGServer.php;h=b034ded754d2862b4b77dded79abe767a9e06fa4;hb=cdc04bf1470e4b95f6a3c060caedba8166abcdd0;hp=54e4d7220e875ce503f271a6c2e0e26aafea3160;hpb=4b73335defd957e93fd985cab3a15f27fe34d5ce;p=friendica.git diff --git a/src/Model/GServer.php b/src/Model/GServer.php index 54e4d7220e..b034ded754 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -92,7 +92,7 @@ class GServer const DETECT_NODEINFO_210 = 103; /** - * Check for the existance of a server and adds it in the background if not existant + * Check for the existence of a server and adds it in the background if not existant * * @param string $url * @param boolean $only_nodeinfo @@ -327,7 +327,7 @@ class GServer return DateTimeFormat::utc('now +1 month'); } - // The system hadn't been successul contacted for more than a month, so try again in three months + // The system hadn't been successful contacted for more than a month, so try again in three months return DateTimeFormat::utc('now +3 month'); } @@ -455,22 +455,26 @@ class GServer * Set failed server status * * @param string $url + * @return void */ public static function setFailureByUrl(string $url) { - $gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($url)]); + $nurl = Strings::normaliseLink($url); + + $gserver = DBA::selectFirst('gserver', [], ['nurl' => $nurl]); if (DBA::isResult($gserver)) { $next_update = self::getNextUpdateDate(false, $gserver['created'], $gserver['last_contact']); self::update(['url' => $url, 'failed' => true, 'blocked' => Network::isUrlBlocked($url), 'last_failure' => DateTimeFormat::utcNow(), 'next_contact' => $next_update, 'network' => Protocol::PHANTOM, 'detection-method' => null], - ['nurl' => Strings::normaliseLink($url)]); + ['nurl' => $nurl]); Logger::info('Set failed status for existing server', ['url' => $url]); if (self::isDefunct($gserver)) { self::archiveContacts($gserver['id']); } return; } - self::insert(['url' => $url, 'nurl' => Strings::normaliseLink($url), + + self::insert(['url' => $url, 'nurl' => $nurl, 'network' => Protocol::PHANTOM, 'created' => DateTimeFormat::utcNow(), 'failed' => true, 'last_failure' => DateTimeFormat::utcNow()]); Logger::info('Set failed status for new server', ['url' => $url]); @@ -553,10 +557,10 @@ class GServer return false; } - // If the URL missmatches, then we mark the old entry as failure + // If the URL mismatches, then we mark the old entry as failure if (!Strings::compareLink($url, $original_url)) { self::setFailureByUrl($original_url); - if (!self::getID($url, true)) { + if (!self::getID($url, true) && !Network::isUrlBlocked($url)) { self::detect($url, $network, $only_nodeinfo); } return false; @@ -577,7 +581,7 @@ class GServer (((parse_url($url, PHP_URL_HOST) != parse_url($valid_url, PHP_URL_HOST)) || (parse_url($url, PHP_URL_PATH) != parse_url($valid_url, PHP_URL_PATH))) && empty(parse_url($valid_url, PHP_URL_PATH)))) { Logger::debug('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $valid_url]); self::setFailureByUrl($url); - if (!self::getID($valid_url, true)) { + if (!self::getID($valid_url, true) && !Network::isUrlBlocked($valid_url)) { self::detect($valid_url, $network, $only_nodeinfo); } return false; @@ -591,7 +595,7 @@ class GServer $valid_url = (string)Uri::fromParts($parts); self::setFailureByUrl($url); - if (!self::getID($valid_url, true)) { + if (!self::getID($valid_url, true) && !Network::isUrlBlocked($valid_url)) { self::detect($valid_url, $network, $only_nodeinfo); } return false; @@ -751,6 +755,10 @@ class GServer return false; } + if (empty($serverdata['version']) && in_array($serverdata['platform'], ['osada']) && in_array($serverdata['detection-method'], [self::DETECT_CONTACTS, self::DETECT_BODY])) { + $serverdata['version'] = self::getNomadVersion($url); + } + // Detect the directory type $serverdata['directory-type'] = self::DT_NONE; @@ -1056,10 +1064,11 @@ class GServer Logger::info('Invalid nodeinfo format', ['url' => $url]); continue; } + if ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/1.0') { - $nodeinfo1_url = $link['href']; + $nodeinfo1_url = Network::addBasePath($link['href'], $httpResult->getUrl()); } elseif ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/2.0') { - $nodeinfo2_url = $link['href']; + $nodeinfo2_url = Network::addBasePath($link['href'], $httpResult->getUrl()); } } @@ -1412,7 +1421,7 @@ class GServer $serverdata['detection-method'] = self::DETECT_SITEINFO_JSON; } - if (!empty($data['url'])) { + if (!empty($data['platform'])) { $serverdata['platform'] = strtolower($data['platform']); $serverdata['version'] = $data['version'] ?? 'N/A'; } @@ -1494,9 +1503,14 @@ class GServer $serverdata['network'] = Protocol::ACTIVITYPUB; $serverdata['site_name'] = JsonLD::fetchElement($actor, 'as:name', '@value'); $serverdata['info'] = JsonLD::fetchElement($actor, 'as:summary', '@value'); - if (!empty($actor['as:generator'])) { + if (self::isNomad($actor)) { + $serverdata['platform'] = self::getNomadName($actor['@id']); + $serverdata['version'] = self::getNomadVersion($actor['@id']); + $serverdata['detection-method'] = self::DETECT_SYSTEM_ACTOR; + } elseif (!empty($actor['as:generator'])) { $generator = explode(' ', JsonLD::fetchElement($actor['as:generator'], 'as:name', '@value')); $serverdata['platform'] = strtolower(array_shift($generator)); + $serverdata['version'] = self::getNomadVersion($actor['@id']); $serverdata['detection-method'] = self::DETECT_SYSTEM_ACTOR; } else { $serverdata['detection-method'] = self::DETECT_AP_ACTOR; @@ -1520,6 +1534,69 @@ class GServer return ['server' => $serverdata, 'actor' => '']; } + /** + * Detect if the given actor is a nomad account + * + * @param array $actor + * @return boolean + */ + private static function isNomad(array $actor): bool + { + $tags = JsonLD::fetchElementArray($actor, 'as:tag'); + if (empty($tags)) { + return false; + } + + foreach ($tags as $tag) { + if ((($tag['as:name'] ?? '') == 'Protocol') && (($tag['sc:value'] ?? '') == 'nomad')) { + return true; + } + } + return false; + } + + /** + * Fetch the name of Nomad implementation + * + * @param string $url + * @return string + */ + private static function getNomadName(string $url): string + { + $name = 'nomad'; + $curlResult = DI::httpClient()->get($url . '/manifest', 'application/manifest+json'); + if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { + return $name; + } + + $data = json_decode($curlResult->getBody(), true); + if (empty($data)) { + return $name; + } + + return $data['name'] ?? $name; + } + + /** + * Fetch the version of the Nomad installation + * + * @param string $url + * @return string + */ + private static function getNomadVersion(string $url): string + { + $curlResult = DI::httpClient()->get($url . '/api/z/1.0/version', HttpClientAccept::JSON); + if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { + return ''; + } + + $data = json_decode($curlResult->getBody(), true); + if (empty($data)) { + return ''; + } + return $data ?? ''; + } + /** * Checks if the server contains a valid host meta file * @@ -2360,7 +2437,7 @@ class GServer } } - // Disvover Mastodon servers + // Discover Mastodon servers $accesstoken = DI::config()->get('system', 'instances_social_key'); if (!empty($accesstoken)) {