]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/GServer.php
Changes:
[friendica.git] / src / Model / GServer.php
index ec7a4b080ec8bd160fdb8cf4af8837c895c6f7ac..a3f0c26077ecfd5718dc655f751d428988aa0ed6 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2024, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -26,7 +26,6 @@ use DOMXPath;
 use Exception;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
-use Friendica\Core\System;
 use Friendica\Core\Worker;
 use Friendica\Database\Database;
 use Friendica\Database\DBA;
@@ -44,7 +43,9 @@ use Friendica\Util\Network;
 use Friendica\Util\Strings;
 use Friendica\Util\XML;
 use Friendica\Network\HTTPException;
+use Friendica\Worker\UpdateGServer;
 use GuzzleHttp\Psr7\Uri;
+use Psr\Http\Message\UriInterface;
 
 /**
  * This class handles GServer related functions
@@ -80,8 +81,8 @@ class GServer
        const DETECT_MASTODON_API = 16;
        const DETECT_STATUS_PHP = 17; // Nextcloud
        const DETECT_V1_CONFIG = 18;
-       const DETECT_PUMPIO = 19; // Deprecated
        const DETECT_SYSTEM_ACTOR = 20; // Mistpark, Osada, Roadhouse, Zap
+       const DETECT_THREADS = 21;
 
        // Standardized endpoints
        const DETECT_STATISTICS_JSON = 100;
@@ -90,7 +91,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
@@ -99,11 +100,11 @@ class GServer
         */
        public static function add(string $url, bool $only_nodeinfo = false)
        {
-               if (self::getID($url, false)) {
+               if (self::getID($url)) {
                        return;
                }
 
-               Worker::add(Worker::PRIORITY_LOW, 'UpdateGServer', $url, $only_nodeinfo);
+               UpdateGServer::add(Worker::PRIORITY_LOW, $url, $only_nodeinfo);
        }
 
        /**
@@ -124,7 +125,14 @@ class GServer
 
                $gserver = DBA::selectFirst('gserver', ['id'], ['nurl' => Strings::normaliseLink($url)]);
                if (DBA::isResult($gserver)) {
-                       Logger::debug('Got ID for URL', ['id' => $gserver['id'], 'url' => $url, 'callstack' => System::callstack(20)]);
+                       Logger::debug('Got ID for URL', ['id' => $gserver['id'], 'url' => $url]);
+
+                       if (Network::isUrlBlocked($url)) {
+                               self::setBlockedById($gserver['id']);
+                       } else {
+                               self::setUnblockedById($gserver['id']);
+                       }
+
                        return $gserver['id'];
                }
 
@@ -143,7 +151,6 @@ class GServer
         * @param string $pattern
         *
         * @return array
-        *
         * @throws Exception
         */
        public static function listByDomainPattern(string $pattern): array
@@ -165,33 +172,99 @@ class GServer
        }
 
        /**
-        * Checks if the given server is reachable
+        * Checks if the given server array is unreachable for a long time now
         *
-        * @param string  $profile URL of the given profile
-        * @param string  $server  URL of the given server (If empty, taken from profile)
-        * @param string  $network Network value that is used, when detection failed
-        * @param boolean $force   Force an update.
+        * @param integer $gsid
+        * @return boolean
+        */
+       private static function isDefunct(array $gserver): bool
+       {
+               return ($gserver['failed'] || in_array($gserver['network'], Protocol::FEDERATED)) &&
+                       ($gserver['last_contact'] >= $gserver['created']) &&
+                       ($gserver['last_contact'] < $gserver['last_failure']) &&
+                       ($gserver['last_contact'] < DateTimeFormat::utc('now - 90 days'));
+       }
+
+       /**
+        * Checks if the given server id is unreachable for a long time now
         *
-        * @return boolean 'true' if server seems vital
+        * @param integer $gsid
+        * @return boolean
         */
-       public static function reachable(string $profile, string $server = '', string $network = '', bool $force = false): bool
+       public static function isDefunctById(int $gsid): bool
        {
-               if ($server == '') {
-                       $contact = Contact::getByURL($profile, null, ['baseurl', 'network']);
-                       if (!empty($contact['baseurl'])) {
-                               $server = $contact['baseurl'];
-                       } elseif ($contact['network'] == Protocol::DIASPORA) {
-                               $parts = parse_url($profile);
-                               unset($parts['path']);
-                               $server = (string)Uri::fromParts($parts);
+               $gserver = DBA::selectFirst('gserver', ['url', 'next_contact', 'last_contact', 'last_failure', 'created', 'failed', 'network'], ['id' => $gsid]);
+               if (empty($gserver)) {
+                       return false;
+               } else {
+                       if (strtotime($gserver['next_contact']) < time()) {
+                               UpdateGServer::add(Worker::PRIORITY_LOW, $gserver['url']);
+                       }
+
+                       return self::isDefunct($gserver);
+               }
+       }
+
+       /**
+        * Checks if the given server id is reachable
+        *
+        * @param integer $gsid
+        * @return boolean
+        */
+       public static function isReachableById(int $gsid): bool
+       {
+               $gserver = DBA::selectFirst('gserver', ['url', 'next_contact', 'failed', 'network'], ['id' => $gsid]);
+               if (empty($gserver)) {
+                       return true;
+               } else {
+                       if (strtotime($gserver['next_contact']) < time()) {
+                               UpdateGServer::add(Worker::PRIORITY_LOW, $gserver['url']);
                        }
+
+                       return !$gserver['failed'] && in_array($gserver['network'], Protocol::FEDERATED);
                }
+       }
 
-               if ($server == '') {
+       /**
+        * Checks if the given server is reachable
+        *
+        * @param array $contact Contact that should be checked
+        *
+        * @return boolean 'true' if server seems vital
+        */
+       public static function reachable(array $contact): bool
+       {
+               if (!empty($contact['gsid'])) {
+                       $gsid = $contact['gsid'];
+               } elseif (!empty($contact['baseurl'])) {
+                       $server = $contact['baseurl'];
+               } elseif ($contact['network'] == Protocol::DIASPORA) {
+                       $parts = (array)parse_url($contact['url']);
+                       unset($parts['path']);
+                       $server = (string)Uri::fromParts($parts);
+               } else {
                        return true;
                }
 
-               return self::check($server, $network, $force);
+               if (!empty($gsid)) {
+                       $condition = ['id' => $gsid];
+               } else {
+                       $condition = ['nurl' => Strings::normaliseLink($server)];
+               }
+
+               $gserver = DBA::selectFirst('gserver', ['url', 'next_contact', 'failed', 'network'], $condition);
+               if (empty($gserver)) {
+                       $reachable = true;
+               } else {
+                       $reachable = !$gserver['failed'] && in_array($gserver['network'], Protocol::FEDERATED);
+                       $server    = $gserver['url'];
+               }
+
+               if (!empty($server) && (empty($gserver) || strtotime($gserver['next_contact']) < time())) {
+                       UpdateGServer::add(Worker::PRIORITY_LOW, $server);
+               }
+
+               return $reachable;
        }
 
        /**
@@ -203,7 +276,6 @@ class GServer
         * @param bool $undetected
         *
         * @return string
-        *
         * @throws Exception
         */
        public static function getNextUpdateDate(bool $success, string $created = '', string $last_contact = '', bool $undetected = false): string
@@ -252,7 +324,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');
        }
 
@@ -273,6 +345,12 @@ class GServer
                        return false;
                }
 
+               if (Network::isUrlBlocked($server_url)) {
+                       Logger::info('Server is blocked', ['url' => $server_url]);
+                       self::setBlockedByUrl($server_url);
+                       return false;
+               }
+
                $gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($server_url)]);
                if (DBA::isResult($gserver)) {
                        if ($gserver['created'] <= DBA::NULL_DATETIME) {
@@ -293,28 +371,124 @@ class GServer
                return self::detect($server_url, $network, $only_nodeinfo);
        }
 
+       /**
+        * Reset failed server status by gserver id
+        *
+        * @param int    $gsid
+        * @param string $network
+        */
+       public static function setReachableById(int $gsid, string $network)
+       {
+               $gserver = DBA::selectFirst('gserver', ['url', 'failed', 'next_contact', 'network'], ['id' => $gsid]);
+               if (!DBA::isResult($gserver)) {
+                       return;
+               }
+
+               $blocked = Network::isUrlBlocked($gserver['url']);
+               if ($gserver['failed']) {
+                       $fields = ['failed' => false, 'blocked' => $blocked, 'last_contact' => DateTimeFormat::utcNow()];
+                       if (!empty($network) && !in_array($gserver['network'], Protocol::FEDERATED)) {
+                               $fields['network'] = $network;
+                       }
+                       self::update($fields, ['id' => $gsid]);
+                       Logger::info('Reset failed status for server', ['url' => $gserver['url']]);
+
+                       if (strtotime($gserver['next_contact']) < time()) {
+                               UpdateGServer::add(Worker::PRIORITY_LOW, $gserver['url']);
+                       }
+               } elseif ($blocked) {
+                       self::setBlockedById($gsid);
+               } else {
+                       self::setUnblockedById($gsid);
+               }
+       }
+
+       /**
+        * Set failed server status by gserver id
+        *
+        * @param int $gsid
+        */
+       public static function setFailureById(int $gsid)
+       {
+               $gserver = DBA::selectFirst('gserver', ['url', 'failed', 'next_contact'], ['id' => $gsid]);
+               if (DBA::isResult($gserver) && !$gserver['failed']) {
+                       self::update(['failed' => true, 'blocked' => Network::isUrlBlocked($gserver['url']), 'last_failure' => DateTimeFormat::utcNow()], ['id' => $gsid]);
+                       Logger::info('Set failed status for server', ['url' => $gserver['url']]);
+
+                       if (strtotime($gserver['next_contact']) < time()) {
+                               UpdateGServer::add(Worker::PRIORITY_LOW, $gserver['url']);
+                       }
+               }
+       }
+
+       public static function setUnblockedById(int $gsid)
+       {
+               $gserver = DBA::selectFirst('gserver', ['url'], ["(`blocked` OR `blocked` IS NULL) AND `id` = ?", $gsid]);
+               if (DBA::isResult($gserver)) {
+                       self::update(['blocked' => false], ['id' => $gsid]);
+                       Logger::info('Set unblocked status for server', ['url' => $gserver['url']]);
+               }
+       }
+
+       public static function setBlockedById(int $gsid)
+       {
+               $gserver = DBA::selectFirst('gserver', ['url'], ["(NOT `blocked` OR `blocked` IS NULL) AND `id` = ?", $gsid]);
+               if (DBA::isResult($gserver)) {
+                       self::update(['blocked' => true, 'failed' => true], ['id' => $gsid]);
+                       Logger::info('Set blocked status for server', ['url' => $gserver['url']]);
+               }
+       }
+
+       public static function setBlockedByUrl(string $url)
+       {
+               $gserver = DBA::selectFirst('gserver', ['url', 'id'], ["(NOT `blocked` OR `blocked` IS NULL) AND `nurl` = ?", Strings::normaliseLink($url)]);
+               if (DBA::isResult($gserver)) {
+                       self::update(['blocked' => true, 'failed' => true], ['id' => $gserver['id']]);
+                       Logger::info('Set blocked status for server', ['url' => $gserver['url']]);
+               }
+       }
+
        /**
         * Set failed server status
         *
         * @param string $url
+        * @return void
         */
-       public static function setFailure(string $url)
+       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, 'last_failure' => DateTimeFormat::utcNow(),
+                       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]);
        }
 
+       /**
+        * Archive server related contacts and inboxes
+        *
+        * @param integer $gsid
+        * @return void
+        */
+       private static function archiveContacts(int $gsid)
+       {
+               Contact::update(['archive' => true], ['gsid' => $gsid]);
+               DBA::update('inbox-status', ['archive' => true], ['gsid' => $gsid]);
+       }
+
        /**
         * Remove unwanted content from the given URL
         *
@@ -322,18 +496,41 @@ class GServer
         *
         * @return string cleaned URL
         * @throws Exception
+        * @deprecated since 2023.03 Use cleanUri instead
         */
        public static function cleanURL(string $dirtyUrl): string
        {
                try {
-                       $url = str_replace('/index.php', '', trim($dirtyUrl, '/'));
-                       return (string)(new Uri($url))->withUserInfo('')->withQuery('')->withFragment('');
+                       return (string)self::cleanUri(new Uri($dirtyUrl));
                } catch (\Throwable $e) {
-                       Logger::warning('Invalid URL', ['dirtyUrl' => $dirtyUrl, 'url' => $url]);
+                       Logger::warning('Invalid URL', ['dirtyUrl' => $dirtyUrl]);
                        return '';
                }
        }
 
+       /**
+        * Remove unwanted content from the given URI
+        *
+        * @param UriInterface $dirtyUri
+        *
+        * @return UriInterface cleaned URI
+        * @throws Exception
+        */
+       public static function cleanUri(UriInterface $dirtyUri): string
+       {
+               return $dirtyUri
+                       ->withUserInfo('')
+                       ->withQuery('')
+                       ->withFragment('')
+                       ->withPath(
+                               preg_replace(
+                                       '#(?:^|/)index\.php#',
+                                       '',
+                                       rtrim($dirtyUri->getPath(), '/')
+                               )
+                       );
+       }
+
        /**
         * Detect server data (type, protocol, version number, ...)
         * The detected data is then updated or inserted in the gserver table.
@@ -344,7 +541,7 @@ class GServer
         *
         * @return boolean 'true' if server could be detected
         */
-       public static function detect(string $url, string $network = '', bool $only_nodeinfo = false): bool
+       private static function detect(string $url, string $network = '', bool $only_nodeinfo = false): bool
        {
                Logger::info('Detect server type', ['server' => $url]);
 
@@ -357,10 +554,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::setFailure($original_url);
-                       if (!self::getID($url, true)) {
+                       self::setFailureByUrl($original_url);
+                       if (!self::getID($url, true) && !Network::isUrlBlocked($url)) {
                                self::detect($url, $network, $only_nodeinfo);
                        }
                        return false;
@@ -368,7 +565,7 @@ class GServer
 
                $valid_url = Network::isUrlValid($url);
                if (!$valid_url) {
-                       self::setFailure($url);
+                       self::setFailureByUrl($url);
                        return false;
                } else {
                        $valid_url = rtrim($valid_url, '/');
@@ -380,8 +577,8 @@ class GServer
                        if (((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))) ||
                                (((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::setFailure($url);
-                               if (!self::getID($valid_url, true)) {
+                               self::setFailureByUrl($url);
+                               if (!self::getID($valid_url, true) && !Network::isUrlBlocked($valid_url)) {
                                        self::detect($valid_url, $network, $only_nodeinfo);
                                }
                                return false;
@@ -390,12 +587,12 @@ class GServer
                        if ((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)) &&
                                (parse_url($url, PHP_URL_PATH) == '')) {
                                Logger::debug('Found redirect. Mark old entry as failure and redirect to the basepath.', ['old' => $url, 'new' => $valid_url]);
-                               $parts = parse_url($valid_url);
+                               $parts = (array)parse_url($valid_url);
                                unset($parts['path']);
                                $valid_url = (string)Uri::fromParts($parts);
 
-                               self::setFailure($url);
-                               if (!self::getID($valid_url, true)) {
+                               self::setFailureByUrl($url);
+                               if (!self::getID($valid_url, true) && !Network::isUrlBlocked($valid_url)) {
                                        self::detect($valid_url, $network, $only_nodeinfo);
                                }
                                return false;
@@ -414,19 +611,23 @@ class GServer
                // When a nodeinfo is present, we don't need to dig further
                $curlResult = DI::httpClient()->get($url . '/.well-known/x-nodeinfo2', HttpClientAccept::JSON);
                if ($curlResult->isTimeout()) {
-                       self::setFailure($url);
+                       self::setFailureByUrl($url);
                        return false;
                }
 
-               $serverdata = self::parseNodeinfo210($curlResult);
-               if (empty($serverdata)) {
-                       $curlResult = DI::httpClient()->get($url . '/.well-known/nodeinfo', HttpClientAccept::JSON);
-                       $serverdata = self::fetchNodeinfo($url, $curlResult);
+               if (!empty($network) && !in_array($network, Protocol::NATIVE_SUPPORT)) {
+                       $serverdata = ['detection-method' => self::DETECT_MANUAL, 'network' => $network, 'platform' => '', 'version' => '', 'site_name' => '', 'info' => ''];
+               } else {
+                       $serverdata = self::parseNodeinfo210($curlResult);
+                       if (empty($serverdata)) {
+                               $curlResult = DI::httpClient()->get($url . '/.well-known/nodeinfo', HttpClientAccept::JSON);
+                               $serverdata = self::fetchNodeinfo($url, $curlResult);
+                       }
                }
 
                if ($only_nodeinfo && empty($serverdata)) {
                        Logger::info('Invalid nodeinfo in nodeinfo-mode, server is marked as failure', ['url' => $url]);
-                       self::setFailure($url);
+                       self::setFailureByUrl($url);
                        return false;
                } elseif (empty($serverdata)) {
                        $serverdata = ['detection-method' => self::DETECT_MANUAL, 'network' => Protocol::PHANTOM, 'platform' => '', 'version' => '', 'site_name' => '', 'info' => ''];
@@ -446,7 +647,7 @@ class GServer
                                }
 
                                if ($curlResult->isSuccess()) {
-                                       $json = json_decode($curlResult->getBody(), true);
+                                       $json = json_decode($curlResult->getBodyString(), true);
                                        if (!empty($json) && is_array($json)) {
                                                $data = self::fetchDataFromSystemActor($json, $serverdata);
                                                $serverdata = $data['server'];
@@ -454,7 +655,7 @@ class GServer
                                                if (!$html_fetched && !in_array($serverdata['detection-method'], [self::DETECT_SYSTEM_ACTOR, self::DETECT_AP_COLLECTION])) {
                                                        $curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML);
                                                }
-                                       } elseif (!$html_fetched && (strlen($curlResult->getBody()) < 1000)) {
+                                       } elseif (!$html_fetched && (strlen($curlResult->getBodyString()) < 1000)) {
                                                $curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML);
                                        }
 
@@ -464,18 +665,24 @@ class GServer
                                        }
                                }
 
-                               if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
-                                       self::setFailure($url);
+                               if (!$curlResult->isSuccess() || empty($curlResult->getBodyString())) {
+                                       self::setFailureByUrl($url);
                                        return false;
                                }
 
+                               if (in_array($url, ['https://www.threads.net', 'https://threads.net'])) {
+                                       $serverdata['detection-method'] = self::DETECT_THREADS;
+                                       $serverdata['network']          = Protocol::ACTIVITYPUB;
+                                       $serverdata['platform']         = 'threads';
+                               }
+
                                if (($serverdata['network'] == Protocol::PHANTOM) || in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
                                        $serverdata = self::detectMastodonAlikes($url, $serverdata);
                                }
                        }
 
                        // All following checks are done for systems that always have got a "host-meta" endpoint.
-                       // With this check we don't have to waste time and ressources for dead systems.
+                       // With this check we don't have to waste time and resources for dead systems.
                        // Also this hopefully prevents us from receiving abuse messages.
                        if (($serverdata['network'] == Protocol::PHANTOM) || in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
                                $validHostMeta = self::validHostMeta($url);
@@ -534,7 +741,7 @@ class GServer
 
                // Most servers aren't installed in a subdirectory, so we declare this entry as failed
                if (($serverdata['network'] == Protocol::PHANTOM) && !empty(parse_url($url, PHP_URL_PATH)) && in_array($serverdata['detection-method'], [self::DETECT_MANUAL])) {
-                       self::setFailure($url);
+                       self::setFailureByUrl($url);
                        return false;
                }
 
@@ -550,6 +757,15 @@ class GServer
                        $serverdata = self::detectNetworkViaContacts($url, $serverdata);
                }
 
+               if (($serverdata['network'] == Protocol::PHANTOM) && in_array($serverdata['detection-method'], [self::DETECT_MANUAL, self::DETECT_BODY])) {
+                       self::setFailureByUrl($url);
+                       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;
 
@@ -566,6 +782,9 @@ class GServer
                }
 
                $serverdata['registered-users'] = $serverdata['registered-users'] ?? 0;
+               $serverdata['next_contact'] = self::getNextUpdateDate(true, '', '', in_array($serverdata['network'], [Protocol::PHANTOM, Protocol::FEED]));
+               $serverdata['last_contact'] = DateTimeFormat::utcNow();
+               $serverdata['failed'] = false;
 
                // Numbers above a reasonable value (10 millions) are ignored
                if ($serverdata['registered-users'] > 10000000) {
@@ -580,9 +799,9 @@ class GServer
                }
 
                $serverdata['next_contact'] = self::getNextUpdateDate(true, '', '', in_array($serverdata['network'], [Protocol::PHANTOM, Protocol::FEED]));
-
                $serverdata['last_contact'] = DateTimeFormat::utcNow();
-               $serverdata['failed'] = false;
+               $serverdata['failed']       = false;
+               $serverdata['blocked']      = false;
 
                $gserver = DBA::selectFirst('gserver', ['network'], ['nurl' => Strings::normaliseLink($url)]);
                if (!DBA::isResult($gserver)) {
@@ -642,7 +861,6 @@ class GServer
         * @param string $server_url address of the server
         *
         * @return void
-        *
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        private static function discoverRelay(string $server_url)
@@ -654,13 +872,13 @@ class GServer
                        return;
                }
 
-               $data = json_decode($curlResult->getBody(), true);
+               $data = json_decode($curlResult->getBodyString(), true);
                if (!is_array($data)) {
                        return;
                }
 
                // Sanitize incoming data, see https://github.com/friendica/friendica/issues/8565
-               $data['subscribe'] = (bool)$data['subscribe'] ?? false;
+               $data['subscribe'] = (bool)($data['subscribe'] ?? false);
 
                if (!$data['subscribe'] || empty($data['scope']) || !in_array(strtolower($data['scope']), ['all', 'tags'])) {
                        $data['scope'] = '';
@@ -749,7 +967,7 @@ class GServer
                        return $serverdata;
                }
 
-               $data = json_decode($curlResult->getBody(), true);
+               $data = json_decode($curlResult->getBodyString(), true);
                if (empty($data)) {
                        return $serverdata;
                }
@@ -841,7 +1059,7 @@ class GServer
                        return [];
                }
 
-               $nodeinfo = json_decode($httpResult->getBody(), true);
+               $nodeinfo = json_decode($httpResult->getBodyString(), true);
 
                if (!is_array($nodeinfo) || empty($nodeinfo['links'])) {
                        return [];
@@ -855,10 +1073,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());
                        }
                }
 
@@ -895,7 +1114,7 @@ class GServer
                        return [];
                }
 
-               $nodeinfo = json_decode($curlResult->getBody(), true);
+               $nodeinfo = json_decode($curlResult->getBodyString(), true);
 
                if (!is_array($nodeinfo)) {
                        return [];
@@ -995,7 +1214,7 @@ class GServer
                        return [];
                }
 
-               $nodeinfo = json_decode($curlResult->getBody(), true);
+               $nodeinfo = json_decode($curlResult->getBodyString(), true);
                if (!is_array($nodeinfo)) {
                        return [];
                }
@@ -1028,6 +1247,11 @@ class GServer
                        }
                }
 
+               // Special treatment for NextCloud, since there you can freely define your software name
+               if (!empty($nodeinfo['rootUrl']) && in_array(parse_url($nodeinfo['rootUrl'], PHP_URL_PATH), ['/index.php/apps/social', '/apps/social'])) {
+                       $server['platform'] = 'nextcloud';
+               }
+
                if (!empty($nodeinfo['metadata']['nodeName'])) {
                        $server['site_name'] = $nodeinfo['metadata']['nodeName'];
                }
@@ -1054,9 +1278,13 @@ class GServer
 
                if (!empty($nodeinfo['protocols'])) {
                        $protocols = [];
-                       foreach ($nodeinfo['protocols'] as $protocol) {
-                               if (is_string($protocol)) {
-                                       $protocols[$protocol] = true;
+                       if (is_string($nodeinfo['protocols'])) {
+                               $protocols[$nodeinfo['protocols']] = true;
+                       } else {
+                               foreach ($nodeinfo['protocols'] as $protocol) {
+                                       if (is_string($protocol)) {
+                                               $protocols[$protocol] = true;
+                                       }
                                }
                        }
 
@@ -1089,12 +1317,10 @@ class GServer
        /**
         * Parses NodeInfo2 protocol 1.0
         *
-        * @see https://github.com/jaywink/nodeinfo2/blob/master/PROTOCOL.md
-        *
-        * @param string $nodeinfo_url address of the nodeinfo path
+        * @param ICanHandleHttpResponses $httpResult
         *
         * @return array Server data
-        *
+        * @see https://github.com/jaywink/nodeinfo2/blob/master/PROTOCOL.md
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        private static function parseNodeinfo210(ICanHandleHttpResponses $httpResult): array
@@ -1103,7 +1329,7 @@ class GServer
                        return [];
                }
 
-               $nodeinfo = json_decode($httpResult->getBody(), true);
+               $nodeinfo = json_decode($httpResult->getBodyString(), true);
 
                if (!is_array($nodeinfo)) {
                        return [];
@@ -1155,9 +1381,13 @@ class GServer
 
                if (!empty($nodeinfo['protocols'])) {
                        $protocols = [];
-                       foreach ($nodeinfo['protocols'] as $protocol) {
-                               if (is_string($protocol)) {
-                                       $protocols[$protocol] = true;
+                       if (is_string($nodeinfo['protocols'])) {
+                               $protocols[$nodeinfo['protocols']] = true;
+                       } else {
+                               foreach ($nodeinfo['protocols'] as $protocol) {
+                                       if (is_string($protocol)) {
+                                               $protocols[$protocol] = true;
+                                       }
                                }
                        }
 
@@ -1202,7 +1432,7 @@ class GServer
                        return $serverdata;
                }
 
-               $data = json_decode($curlResult->getBody(), true);
+               $data = json_decode($curlResult->getBodyString(), true);
                if (empty($data)) {
                        return $serverdata;
                }
@@ -1211,7 +1441,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';
                }
@@ -1293,9 +1523,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;
@@ -1319,6 +1554,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') && in_array('nomad', [$tag['sc:value'] ?? '', $tag['as:content'] ?? ''])) {
+                               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->getBodyString() == '')) {
+                       return $name;
+               }
+
+               $data = json_decode($curlResult->getBodyString(), 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->getBodyString() == '')) {
+                       return '';
+               }
+
+               $data = json_decode($curlResult->getBodyString(), true);
+               if (empty($data)) {
+                       return '';
+               }
+               return $data ?? '';
+       }
+
        /**
         * Checks if the server contains a valid host meta file
         *
@@ -1334,7 +1632,7 @@ class GServer
                        return false;
                }
 
-               $xrd = XML::parseString($curlResult->getBody(), true);
+               $xrd = XML::parseString($curlResult->getBodyString(), true);
                if (!is_object($xrd)) {
                        return false;
                }
@@ -1433,7 +1731,7 @@ class GServer
                        return $serverdata;
                }
 
-               $data = json_decode($curlResult->getBody(), true);
+               $data = json_decode($curlResult->getBodyString(), true);
                if (empty($data)) {
                        return $serverdata;
                }
@@ -1463,7 +1761,7 @@ class GServer
                        return $serverdata;
                }
 
-               $data = json_decode($curlResult->getBody(), true);
+               $data = json_decode($curlResult->getBodyString(), true);
                if (empty($data)) {
                        return $serverdata;
                }
@@ -1486,11 +1784,11 @@ class GServer
        private static function detectPeertube(string $url, array $serverdata): array
        {
                $curlResult = DI::httpClient()->get($url . '/api/v1/config', HttpClientAccept::JSON);
-               if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
+               if (!$curlResult->isSuccess() || ($curlResult->getBodyString() == '')) {
                        return $serverdata;
                }
 
-               $data = json_decode($curlResult->getBody(), true);
+               $data = json_decode($curlResult->getBodyString(), true);
                if (empty($data)) {
                        return $serverdata;
                }
@@ -1534,11 +1832,11 @@ class GServer
        private static function detectNextcloud(string $url, array $serverdata, bool $validHostMeta): array
        {
                $curlResult = DI::httpClient()->get($url . '/status.php', HttpClientAccept::JSON);
-               if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
+               if (!$curlResult->isSuccess() || ($curlResult->getBodyString() == '')) {
                        return $serverdata;
                }
 
-               $data = json_decode($curlResult->getBody(), true);
+               $data = json_decode($curlResult->getBodyString(), true);
                if (empty($data)) {
                        return $serverdata;
                }
@@ -1570,11 +1868,11 @@ class GServer
        private static function fetchWeeklyUsage(string $url, array $serverdata): array
        {
                $curlResult = DI::httpClient()->get($url . '/api/v1/instance/activity', HttpClientAccept::JSON);
-               if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
+               if (!$curlResult->isSuccess() || ($curlResult->getBodyString() == '')) {
                        return $serverdata;
                }
 
-               $data = json_decode($curlResult->getBody(), true);
+               $data = json_decode($curlResult->getBodyString(), true);
                if (empty($data)) {
                        return $serverdata;
                }
@@ -1610,11 +1908,11 @@ class GServer
        private static function detectMastodonAlikes(string $url, array $serverdata): array
        {
                $curlResult = DI::httpClient()->get($url . '/api/v1/instance', HttpClientAccept::JSON);
-               if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
+               if (!$curlResult->isSuccess() || ($curlResult->getBodyString() == '')) {
                        return $serverdata;
                }
 
-               $data = json_decode($curlResult->getBody(), true);
+               $data = json_decode($curlResult->getBodyString(), true);
                if (empty($data)) {
                        return $serverdata;
                }
@@ -1682,11 +1980,11 @@ class GServer
        private static function detectHubzilla(string $url, array $serverdata): array
        {
                $curlResult = DI::httpClient()->get($url . '/api/statusnet/config.json', HttpClientAccept::JSON);
-               if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
+               if (!$curlResult->isSuccess() || ($curlResult->getBodyString() == '')) {
                        return $serverdata;
                }
 
-               $data = json_decode($curlResult->getBody(), true);
+               $data = json_decode($curlResult->getBodyString(), true);
                if (empty($data) || empty($data['site'])) {
                        return $serverdata;
                }
@@ -1779,11 +2077,11 @@ class GServer
        {
                // Test for GNU Social
                $curlResult = DI::httpClient()->get($url . '/api/gnusocial/version.json', HttpClientAccept::JSON);
-               if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') &&
-                       ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) {
+               if ($curlResult->isSuccess() && ($curlResult->getBodyString() != '{"error":"not implemented"}') &&
+                       ($curlResult->getBodyString() != '') && (strlen($curlResult->getBodyString()) < 30)) {
                        $serverdata['platform'] = 'gnusocial';
                        // Remove junk that some GNU Social servers return
-                       $serverdata['version'] = str_replace(chr(239) . chr(187) . chr(191), '', $curlResult->getBody());
+                       $serverdata['version'] = str_replace(chr(239) . chr(187) . chr(191), '', $curlResult->getBodyString());
                        $serverdata['version'] = str_replace(["\r", "\n", "\t"], '', $serverdata['version']);
                        $serverdata['version'] = trim($serverdata['version'], '"');
                        $serverdata['network'] = Protocol::OSTATUS;
@@ -1797,11 +2095,11 @@ class GServer
 
                // Test for Statusnet
                $curlResult = DI::httpClient()->get($url . '/api/statusnet/version.json', HttpClientAccept::JSON);
-               if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') &&
-                       ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) {
+               if ($curlResult->isSuccess() && ($curlResult->getBodyString() != '{"error":"not implemented"}') &&
+                       ($curlResult->getBodyString() != '') && (strlen($curlResult->getBodyString()) < 30)) {
 
                        // Remove junk that some GNU Social servers return
-                       $serverdata['version'] = str_replace(chr(239).chr(187).chr(191), '', $curlResult->getBody());
+                       $serverdata['version'] = str_replace(chr(239).chr(187).chr(191), '', $curlResult->getBodyString());
                        $serverdata['version'] = str_replace(["\r", "\n", "\t"], '', $serverdata['version']);
                        $serverdata['version'] = trim($serverdata['version'], '"');
 
@@ -1848,7 +2146,7 @@ class GServer
                        return $serverdata;
                }
 
-               $data = json_decode($curlResult->getBody(), true);
+               $data = json_decode($curlResult->getBodyString(), true);
                if (empty($data) || empty($data['version'])) {
                        return $serverdata;
                }
@@ -2093,6 +2391,10 @@ class GServer
         */
        public static function discover()
        {
+               if (!DI::config('system', 'discover_servers')) {
+                       return;
+               }
+
                // Update the server list
                self::discoverFederation();
 
@@ -2100,14 +2402,10 @@ class GServer
 
                $requery_days = intval(DI::config()->get('system', 'poco_requery_days'));
 
-               if ($requery_days == 0) {
-                       $requery_days = 7;
-               }
-
                $last_update = date('c', time() - (60 * 60 * 24 * $requery_days));
 
                $gservers = DBA::select('gserver', ['id', 'url', 'nurl', 'network', 'poco', 'directory-type'],
-                       ["NOT `failed` AND `directory-type` != ? AND `last_poco_query` < ?", GServer::DT_NONE, $last_update],
+                       ["NOT `blocked` AND NOT `failed` AND `directory-type` != ? AND `last_poco_query` < ?", GServer::DT_NONE, $last_update],
                        ['order' => ['RAND()']]);
 
                while ($gserver = DBA::fetch($gservers)) {
@@ -2133,7 +2431,7 @@ class GServer
         */
        private static function discoverFederation()
        {
-               $last = DI::config()->get('poco', 'last_federation_discovery');
+               $last = DI::keyValue()->get('poco_last_federation_discovery');
 
                if ($last) {
                        $next = $last + (24 * 60 * 60);
@@ -2159,14 +2457,14 @@ class GServer
                        }
                }
 
-               // Disvover Mastodon servers
+               // Discover Mastodon servers
                $accesstoken = DI::config()->get('system', 'instances_social_key');
 
                if (!empty($accesstoken)) {
                        $api = 'https://instances.social/api/1.0/instances/list?count=0';
                        $curlResult = DI::httpClient()->get($api, HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $accesstoken]]]);
                        if ($curlResult->isSuccess()) {
-                               $servers = json_decode($curlResult->getBody(), true);
+                               $servers = json_decode($curlResult->getBodyString(), true);
 
                                if (!empty($servers['instances'])) {
                                        foreach ($servers['instances'] as $server) {
@@ -2177,7 +2475,7 @@ class GServer
                        }
                }
 
-               DI::config()->set('poco', 'last_federation_discovery', time());
+               DI::keyValue()->set('poco_last_federation_discovery', time());
        }
 
        /**
@@ -2237,7 +2535,7 @@ class GServer
                        }
                }
 
-               Logger::info('Protocol for server', ['protocol' => $protocol, 'old' => $old, 'id' => $gsid, 'url' => $gserver['url'], 'callstack' => System::callstack(20)]);
+               Logger::info('Protocol for server', ['protocol' => $protocol, 'old' => $old, 'id' => $gsid, 'url' => $gserver['url']]);
                self::update(['protocol' => $protocol], ['id' => $gsid]);
        }