]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/GServer.php
We now store the receivers as well
[friendica.git] / src / Model / GServer.php
index 713d6f114601d901b53036b7a46ade4b83805a0e..c0e4899923d86cb375109e9b2240675d166cc5eb 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -23,6 +23,7 @@ namespace Friendica\Model;
 
 use DOMDocument;
 use DOMXPath;
+use Exception;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\System;
@@ -31,7 +32,9 @@ use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Module\Register;
-use Friendica\Network\CurlResult;
+use Friendica\Network\HTTPClient\Client\HttpClientAccept;
+use Friendica\Network\HTTPClient\Client\HttpClientOptions;
+use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
 use Friendica\Protocol\Relay;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
@@ -64,12 +67,30 @@ class GServer
        const DETECT_SITEINFO_JSON = 15; // Newer Hubzilla
        const DETECT_MASTODON_API = 16;
        const DETECT_STATUS_PHP = 17; // Nextcloud
+       const DETECT_V1_CONFIG = 18;
+       const DETECT_PUMPIO = 19;
 
        // Standardized endpoints
        const DETECT_STATISTICS_JSON = 100;
        const DETECT_NODEINFO_1 = 101;
        const DETECT_NODEINFO_2 = 102;
 
+       /**
+        * Check for the existance of a server and adds it in the background if not existant
+        *
+        * @param string $url
+        * @param boolean $only_nodeinfo
+        * @return void
+        */
+       public static function add(string $url, bool $only_nodeinfo = false)
+       {
+               if (self::getID($url, false)) {
+                       return;
+               }
+
+               Worker::add(PRIORITY_LOW, 'UpdateGServer', $url, $only_nodeinfo);
+       }
+
        /**
         * Get the ID for the given server URL
         *
@@ -94,10 +115,37 @@ class GServer
                if ($no_check || !self::check($url)) {
                        return null;
                }
-       
+
                return self::getID($url, true);
        }
 
+       /**
+        * Retrieves all the servers which base domain are matching the provided domain pattern
+        *
+        * The pattern is a simple fnmatch() pattern with ? for single wildcard and * for multiple wildcard
+        *
+        * @param string $pattern
+        * @return array
+        * @throws Exception
+        */
+       public static function listByDomainPattern(string $pattern): array
+       {
+               $likePattern = 'http://' . strtr($pattern, ['_' => '\_', '%' => '\%', '?' => '_', '*' => '%']);
+
+               // The SUBSTRING_INDEX returns everything before the eventual third /, which effectively trims an
+               // eventual server path and keep only the server domain which we're matching against the pattern.
+               $sql = "SELECT `gserver`.*, COUNT(*) AS `contacts`
+                       FROM `gserver`
+                       LEFT JOIN `contact` ON `gserver`.`id` = `contact`.`gsid`
+                       WHERE SUBSTRING_INDEX(`gserver`.`nurl`, '/', 3) LIKE ?
+                       AND NOT `gserver`.`failed`
+                       GROUP BY `gserver`.`id`";
+
+               $stmt = DI::dba()->p($sql, $likePattern);
+
+               return DI::dba()->toArray($stmt);
+       }
+
        /**
         * Checks if the given server is reachable
         *
@@ -124,59 +172,53 @@ class GServer
                return self::check($server, $network, $force);
        }
 
-       /**
-        * Decides if a server needs to be updated, based upon several date fields
-        *
-        * @param date $created      Creation date of that server entry
-        * @param date $updated      When had the server entry be updated
-        * @param date $last_failure Last failure when contacting that server
-        * @param date $last_contact Last time the server had been contacted
-        *
-        * @return boolean Does the server record needs an update?
-        */
-       public static function updateNeeded($created, $updated, $last_failure, $last_contact)
+       public static function getNextUpdateDate(bool $success, string $created = '', string $last_contact = '')
        {
+               // On successful contact process check again next week
+               if ($success) {
+                       return DateTimeFormat::utc('now +7 day');
+               }
+
                $now = strtotime(DateTimeFormat::utcNow());
 
-               if ($updated > $last_contact) {
-                       $contact_time = strtotime($updated);
+               if ($created > $last_contact) {
+                       $contact_time = strtotime($created);
                } else {
                        $contact_time = strtotime($last_contact);
                }
 
-               $failure_time = strtotime($last_failure);
-               $created_time = strtotime($created);
+               // If the last contact was less than 6 hours before then try again in 6 hours
+               if (($now - $contact_time) < (60 * 60 * 6)) {
+                       return DateTimeFormat::utc('now +6 hour');
+               }
 
-               // If there is no "created" time then use the current time
-               if ($created_time <= 0) {
-                       $created_time = $now;
+               // If the last contact was less than 12 hours before then try again in 12 hours
+               if (($now - $contact_time) < (60 * 60 * 12)) {
+                       return DateTimeFormat::utc('now +12 hour');
                }
 
-               // If the last contact was less than 24 hours then don't update
+               // If the last contact was less than 24 hours before then try tomorrow again
                if (($now - $contact_time) < (60 * 60 * 24)) {
-                       return false;
+                       return DateTimeFormat::utc('now +1 day');
                }
 
-               // If the last failure was less than 24 hours then don't update
-               if (($now - $failure_time) < (60 * 60 * 24)) {
-                       return false;
+               // If the last contact was less than a week before then try again in a week
+               if (($now - $contact_time) < (60 * 60 * 24 * 7)) {
+                       return DateTimeFormat::utc('now +1 week');
                }
 
-               // If the last contact was less than a week ago and the last failure is older than a week then don't update
-               //if ((($now - $contact_time) < (60 * 60 * 24 * 7)) && ($contact_time > $failure_time))
-               //      return false;
-
-               // If the last contact time was more than a week ago and the contact was created more than a week ago, then only try once a week
-               if ((($now - $contact_time) > (60 * 60 * 24 * 7)) && (($now - $created_time) > (60 * 60 * 24 * 7)) && (($now - $failure_time) < (60 * 60 * 24 * 7))) {
-                       return false;
+               // If the last contact was less than two weeks before then try again in two week
+               if (($now - $contact_time) < (60 * 60 * 24 * 14)) {
+                       return DateTimeFormat::utc('now +2 week');
                }
 
-               // If the last contact time was more than a month ago and the contact was created more than a month ago, then only try once a month
-               if ((($now - $contact_time) > (60 * 60 * 24 * 30)) && (($now - $created_time) > (60 * 60 * 24 * 30)) && (($now - $failure_time) < (60 * 60 * 24 * 30))) {
-                       return false;
+               // If the last contact was less than a month before then try again in a month
+               if (($now - $contact_time) < (60 * 60 * 24 * 30)) {
+                       return DateTimeFormat::utc('now +1 month');
                }
 
-               return true;
+               // The system hadn't been successul contacted for more than a month, so try again in three months
+               return DateTimeFormat::utc('now +3 month');
        }
 
        /**
@@ -192,7 +234,6 @@ class GServer
        public static function check(string $server_url, string $network = '', bool $force = false, bool $only_nodeinfo = false)
        {
                $server_url = self::cleanURL($server_url);
-
                if ($server_url == '') {
                        return false;
                }
@@ -205,24 +246,11 @@ class GServer
                                DBA::update('gserver', $fields, $condition);
                        }
 
-                       $last_contact = $gserver['last_contact'];
-                       $last_failure = $gserver['last_failure'];
-
-                       // See discussion under https://forum.friendi.ca/display/0b6b25a8135aabc37a5a0f5684081633
-                       // It can happen that a zero date is in the database, but storing it again is forbidden.
-                       if ($last_contact < DBA::NULL_DATETIME) {
-                               $last_contact = DBA::NULL_DATETIME;
-                       }
-
-                       if ($last_failure < DBA::NULL_DATETIME) {
-                               $last_failure = DBA::NULL_DATETIME;
-                       }
-
-                       if (!$force && !self::updateNeeded($gserver['created'], '', $last_failure, $last_contact)) {
+                       if (!$force && (strtotime($gserver['next_contact']) > time())) {
                                Logger::info('No update needed', ['server' => $server_url]);
-                               return ($last_contact >= $last_failure);
+                               return (!$gserver['failed']);
                        }
-                       Logger::info('Server is outdated. Start discovery.', ['Server' => $server_url, 'Force' => $force, 'Created' => $gserver['created'], 'Failure' => $last_failure, 'Contact' => $last_contact]);
+                       Logger::info('Server is outdated. Start discovery.', ['Server' => $server_url, 'Force' => $force]);
                } else {
                        Logger::info('Server is unknown. Start discovery.', ['Server' => $server_url]);
                }
@@ -235,10 +263,13 @@ class GServer
         *
         * @param string $url
         */
-       private static function setFailure(string $url)
+       public static function setFailure(string $url)
        {
-               if (DBA::exists('gserver', ['nurl' => Strings::normaliseLink($url)])) {
-                       DBA::update('gserver', ['failed' => true, 'last_failure' => DateTimeFormat::utcNow(), 'detection-method' => null],
+               $gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($url)]);
+               if (DBA::isResult($gserver)) {
+                       $next_update = self::getNextUpdateDate(false, $gserver['created'], $gserver['last_contact']);
+                       DBA::update('gserver', ['failed' => true, 'last_failure' => DateTimeFormat::utcNow(),
+                       'next_contact' => $next_update, 'detection-method' => null],
                        ['nurl' => Strings::normaliseLink($url)]);
                        Logger::info('Set failed status for existing server', ['url' => $url]);
                        return;
@@ -306,18 +337,30 @@ class GServer
 
                // If the URL missmatches, then we mark the old entry as failure
                if ($url != $original_url) {
+                       /// @todo What to do with "next_contact" here?
                        DBA::update('gserver', ['failed' => true, 'last_failure' => DateTimeFormat::utcNow()],
                                ['nurl' => Strings::normaliseLink($original_url)]);
                }
 
                // When a nodeinfo is present, we don't need to dig further
                $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
-               $curlResult = DI::httpRequest()->get($url . '/.well-known/nodeinfo', ['timeout' => $xrd_timeout]);
+               $curlResult = DI::httpClient()->get($url . '/.well-known/nodeinfo', HttpClientAccept::JSON, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
                if ($curlResult->isTimeout()) {
                        self::setFailure($url);
                        return false;
                }
 
+               // On a redirect follow the new host but mark the old one as failure
+               if ($curlResult->isSuccess() && !empty($curlResult->getRedirectUrl()) && (parse_url($url, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST))) {
+                       $curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
+                       if (!empty($curlResult->getRedirectUrl()) && parse_url($url, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST)) {
+                               Logger::info('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $curlResult->getRedirectUrl()]);
+                               self::setFailure($url);
+                               self::detect($curlResult->getRedirectUrl(), $network, $only_nodeinfo);
+                               return false;
+                       }
+               }
+
                $nodeinfo = self::fetchNodeinfo($url, $curlResult);
                if ($only_nodeinfo && empty($nodeinfo)) {
                        Logger::info('Invalid nodeinfo in nodeinfo-mode, server is marked as failure', ['url' => $url]);
@@ -335,6 +378,12 @@ class GServer
                if (empty($nodeinfo['network']) || in_array($nodeinfo['network'], [Protocol::DFRN, Protocol::ZOT])) {
                        if (!empty($nodeinfo['detection-method'])) {
                                $serverdata['detection-method'] = $nodeinfo['detection-method'];
+
+                               foreach (['registered-users', 'active_users_monthly', 'active-halfyear-users', 'local-posts'] as $field) {
+                                       if (!empty($nodeinfo[$field])) {
+                                               $serverdata[$field] = $nodeinfo[$field];
+                                       }
+                               }
                        }
 
                        // Fetch the landing page, possibly it reveals some data
@@ -345,8 +394,15 @@ class GServer
                                        $basedata = ['detection-method' => self::DETECT_MANUAL];
                                }
 
-                               $curlResult = DI::httpRequest()->get($baseurl, ['timeout' => $xrd_timeout]);
+                               $curlResult = DI::httpClient()->get($baseurl, HttpClientAccept::HTML, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
                                if ($curlResult->isSuccess()) {
+                                       if (!empty($curlResult->getRedirectUrl()) && (parse_url($baseurl, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST))) {
+                                               Logger::info('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $curlResult->getRedirectUrl()]);
+                                               self::setFailure($url);
+                                               self::detect($curlResult->getRedirectUrl(), $network, $only_nodeinfo);
+                                               return false;
+                                       }
+
                                        $basedata = self::analyseRootHeader($curlResult, $basedata);
                                        $basedata = self::analyseRootBody($curlResult, $basedata, $baseurl);
                                }
@@ -362,7 +418,7 @@ class GServer
                                        // When the base path doesn't seem to contain a social network we try the complete path.
                                        // Most detectable system have to be installed in the root directory.
                                        // We checked the base to avoid false positives.
-                                       $curlResult = DI::httpRequest()->get($url, ['timeout' => $xrd_timeout]);
+                                       $curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
                                        if ($curlResult->isSuccess()) {
                                                $urldata = self::analyseRootHeader($curlResult, $serverdata);
                                                $urldata = self::analyseRootBody($curlResult, $urldata, $url);
@@ -382,14 +438,20 @@ class GServer
                                }
                        }
 
-                       if (empty($serverdata['network']) || ($serverdata['network'] == Protocol::ACTIVITYPUB)) {
+                       if (empty($nodeinfo['network']) && (empty($serverdata['network']) || ($serverdata['network'] == Protocol::ACTIVITYPUB))) {
                                $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.
                        // Also this hopefully prevents us from receiving abuse messages.
-                       if (empty($serverdata['network']) && !self::validHostMeta($url)) {
+                       $validHostMeta = self::validHostMeta($url);
+
+                       if (empty($serverdata['network']) && !$validHostMeta) {
+                               $serverdata = self::detectFromContacts($url, $serverdata);
+                       }
+
+                       if (empty($serverdata['network']) && !$validHostMeta) {
                                self::setFailure($url);
                                return false;
                        }
@@ -408,14 +470,22 @@ class GServer
                                $serverdata = self::detectHubzilla($url, $serverdata);
                        }
 
+                       if (empty($serverdata['network']) || in_array($serverdata['detection-method'], [self::DETECT_MANUAL, self::DETECT_BODY])) {
+                               $serverdata = self::detectPeertube($url, $serverdata);
+                       }
+
                        if (empty($serverdata['network'])) {
                                $serverdata = self::detectNextcloud($url, $serverdata);
                        }
 
-                       if (empty($serverdata['network'])) {
+                       if (empty($nodeinfo['network']) && empty($serverdata['network'])) {
                                $serverdata = self::detectGNUSocial($url, $serverdata);
                        }
 
+                       if (empty($serverdata['network'])) {
+                               $serverdata = self::detectPumpIO($url, $serverdata);
+                       }
+
                        $serverdata = array_merge($nodeinfo, $serverdata);
                } else {
                        $serverdata = $nodeinfo;
@@ -423,9 +493,13 @@ class GServer
 
                // Detect the directory type
                $serverdata['directory-type'] = self::DT_NONE;
-               $serverdata = self::checkPoCo($url, $serverdata);
+
                $serverdata = self::checkMastodonDirectory($url, $serverdata);
 
+               if ($serverdata['directory-type'] == self::DT_NONE) {
+                       $serverdata = self::checkPoCo($url, $serverdata);
+               }
+
                // We can't detect the network type. Possibly it is some system that we don't know yet
                if (empty($serverdata['network'])) {
                        $serverdata['network'] = Protocol::PHANTOM;
@@ -439,19 +513,25 @@ class GServer
                $serverdata['url'] = $url;
                $serverdata['nurl'] = Strings::normaliseLink($url);
 
-               // We take the highest number that we do find
-               $registeredUsers = $serverdata['registered-users'] ?? 0;
+               if (in_array($serverdata['network'], [Protocol::PHANTOM, Protocol::FEED])) {
+                       $serverdata = self::detectNetworkViaContacts($url, $serverdata);
+               }
 
-               // On an active server there has to be at least a single user
-               if (($serverdata['network'] != Protocol::PHANTOM) && ($registeredUsers == 0)) {
-                       $registeredUsers = 1;
+               if ($serverdata['network'] == Protocol::ACTIVITYPUB) {
+                       $serverdata = self::fetchWeeklyUsage($url, $serverdata);
                }
 
-               if ($serverdata['network'] == Protocol::PHANTOM) {
-                       $serverdata['registered-users'] = $registeredUsers;
-                       $serverdata = self::detectNetworkViaContacts($url, $serverdata);
+               $serverdata['registered-users'] = $serverdata['registered-users'] ?? 0;
+
+               // On an active server there has to be at least a single user
+               if (!in_array($serverdata['network'], [Protocol::PHANTOM, Protocol::FEED]) && ($serverdata['registered-users'] == 0)) {
+                       $serverdata['registered-users'] = 1;
+               } elseif (in_array($serverdata['network'], [Protocol::PHANTOM, Protocol::FEED])) {
+                       $serverdata['registered-users'] = 0;
                }
 
+               $serverdata['next_contact'] = self::getNextUpdateDate(true);
+
                $serverdata['last_contact'] = DateTimeFormat::utcNow();
                $serverdata['failed'] = false;
 
@@ -461,11 +541,6 @@ class GServer
                        $ret = DBA::insert('gserver', $serverdata);
                        $id = DBA::lastInsertId();
                } else {
-                       // Don't override the network with 'unknown' when there had been a valid entry before
-                       if (($serverdata['network'] == Protocol::PHANTOM) && !empty($gserver['network'])) {
-                               unset($serverdata['network']);
-                       }
-
                        $ret = DBA::update('gserver', $serverdata, ['nurl' => $serverdata['nurl']]);
                        $gserver = DBA::selectFirst('gserver', ['id'], ['nurl' => $serverdata['nurl']]);
                        if (DBA::isResult($gserver)) {
@@ -473,14 +548,31 @@ class GServer
                        }
                }
 
-               if (!empty($serverdata['network']) && !empty($id) && ($serverdata['network'] != Protocol::PHANTOM)) {
+               // Count the number of known contacts from this server
+               if (!empty($id) && !in_array($serverdata['network'], [Protocol::PHANTOM, Protocol::FEED])) {
                        $apcontacts = DBA::count('apcontact', ['gsid' => $id]);
-                       $contacts = DBA::count('contact', ['uid' => 0, 'gsid' => $id]);
-                       $max_users = max($apcontacts, $contacts, $registeredUsers);
-                       if ($max_users > $registeredUsers) {
+                       $contacts = DBA::count('contact', ['uid' => 0, 'gsid' => $id, 'failed' => false]);
+                       $max_users = max($apcontacts, $contacts);
+                       if ($max_users > $serverdata['registered-users']) {
                                Logger::info('Update registered users', ['id' => $id, 'url' => $serverdata['nurl'], 'registered-users' => $max_users]);
                                DBA::update('gserver', ['registered-users' => $max_users], ['id' => $id]);
                        }
+
+                       if (empty($serverdata['active-month-users'])) {
+                               $contacts = DBA::count('contact', ["`uid` = ? AND `gsid` = ? AND NOT `failed` AND `last-item` > ?", 0, $id, DateTimeFormat::utc('now - 30 days')]);
+                               if ($contacts > 0) {
+                                       Logger::info('Update monthly users', ['id' => $id, 'url' => $serverdata['nurl'], 'monthly-users' => $contacts]);
+                                       DBA::update('gserver', ['active-month-users' => $contacts], ['id' => $id]);
+                               }
+                       }
+       
+                       if (empty($serverdata['active-halfyear-users'])) {
+                               $contacts = DBA::count('contact', ["`uid` = ? AND `gsid` = ? AND NOT `failed` AND `last-item` > ?", 0, $id, DateTimeFormat::utc('now - 180 days')]);
+                               if ($contacts > 0) {
+                                       Logger::info('Update halfyear users', ['id' => $id, 'url' => $serverdata['nurl'], 'halfyear-users' => $contacts]);
+                                       DBA::update('gserver', ['active-halfyear-users' => $contacts], ['id' => $id]);
+                               }
+                       }
                }
 
                if (!empty($serverdata['network']) && in_array($serverdata['network'], [Protocol::DFRN, Protocol::DIASPORA])) {
@@ -500,7 +592,7 @@ class GServer
        {
                Logger::info('Discover relay data', ['server' => $server_url]);
 
-               $curlResult = DI::httpRequest()->get($server_url . '/.well-known/x-social-relay');
+               $curlResult = DI::httpClient()->get($server_url . '/.well-known/x-social-relay', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
                        return;
                }
@@ -595,7 +687,7 @@ class GServer
         */
        private static function fetchStatistics(string $url)
        {
-               $curlResult = DI::httpRequest()->get($url . '/statistics.json');
+               $curlResult = DI::httpClient()->get($url . '/statistics.json', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
                        return [];
                }
@@ -632,6 +724,21 @@ class GServer
                        }
                }
 
+               if (!empty($data['total_users'])) {
+                       $serverdata['registered-users'] = max($data['total_users'], 1);
+               }
+
+               if (!empty($data['active_users_monthly'])) {
+                       $serverdata['active-month-users'] = max($data['active_users_monthly'], 0);
+               }
+
+               if (!empty($data['active_users_halfyear'])) {
+                       $serverdata['active-halfyear-users'] = max($data['active_users_halfyear'], 0);
+               }
+
+               if (!empty($data['local_posts'])) {
+                       $serverdata['local-posts'] = max($data['local_posts'], 0);
+               }
 
                if (!empty($data['registrations_open'])) {
                        $serverdata['register_policy'] = Register::OPEN;
@@ -645,18 +752,19 @@ class GServer
        /**
         * Detect server type by using the nodeinfo data
         *
-        * @param string     $url        address of the server
-        * @param CurlResult $curlResult
+        * @param string                  $url        address of the server
+        * @param ICanHandleHttpResponses $httpResult
+        *
         * @return array Server data
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private static function fetchNodeinfo(string $url, CurlResult $curlResult)
+       private static function fetchNodeinfo(string $url, ICanHandleHttpResponses $httpResult)
        {
-               if (!$curlResult->isSuccess()) {
+               if (!$httpResult->isSuccess()) {
                        return [];
                }
 
-               $nodeinfo = json_decode($curlResult->getBody(), true);
+               $nodeinfo = json_decode($httpResult->getBody(), true);
 
                if (!is_array($nodeinfo) || empty($nodeinfo['links'])) {
                        return [];
@@ -705,8 +813,7 @@ class GServer
         */
        private static function parseNodeinfo1(string $nodeinfo_url)
        {
-               $curlResult = DI::httpRequest()->get($nodeinfo_url);
-
+               $curlResult = DI::httpClient()->get($nodeinfo_url, HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
                        return [];
                }
@@ -742,7 +849,23 @@ class GServer
                }
 
                if (!empty($nodeinfo['usage']['users']['total'])) {
-                       $server['registered-users'] = $nodeinfo['usage']['users']['total'];
+                       $server['registered-users'] = max($nodeinfo['usage']['users']['total'], 1);
+               }
+
+               if (!empty($nodeinfo['usage']['users']['activeMonth'])) {
+                       $server['active-month-users'] = max($nodeinfo['usage']['users']['activeMonth'], 0);
+               }
+
+               if (!empty($nodeinfo['usage']['users']['activeHalfyear'])) {
+                       $server['active-halfyear-users'] = max($nodeinfo['usage']['users']['activeHalfyear'], 0);
+               }
+
+               if (!empty($nodeinfo['usage']['localPosts'])) {
+                       $server['local-posts'] = max($nodeinfo['usage']['localPosts'], 0);
+               }
+
+               if (!empty($nodeinfo['usage']['localComments'])) {
+                       $server['local-comments'] = max($nodeinfo['usage']['localComments'], 0);
                }
 
                if (!empty($nodeinfo['protocols']['inbound']) && is_array($nodeinfo['protocols']['inbound'])) {
@@ -776,13 +899,14 @@ class GServer
        /**
         * Parses Nodeinfo 2
         *
+        * @see https://git.feneas.org/jaywink/nodeinfo2
         * @param string $nodeinfo_url address of the nodeinfo path
         * @return array Server data
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        private static function parseNodeinfo2(string $nodeinfo_url)
        {
-               $curlResult = DI::httpRequest()->get($nodeinfo_url);
+               $curlResult = DI::httpClient()->get($nodeinfo_url, HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
                        return [];
                }
@@ -810,6 +934,11 @@ class GServer
                                // Version numbers on Nodeinfo are presented with additional info, e.g.:
                                // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
                                $server['version'] = preg_replace('=(.+)-(.{4,})=ism', '$1', $server['version']);
+
+                               // qoto advertises itself as Mastodon
+                               if (($server['platform'] == 'mastodon') && substr($nodeinfo['software']['version'], -5) == '-qoto') {
+                                       $server['platform'] = 'qoto';
+                               }
                        }
                }
 
@@ -818,13 +947,31 @@ class GServer
                }
 
                if (!empty($nodeinfo['usage']['users']['total'])) {
-                       $server['registered-users'] = $nodeinfo['usage']['users']['total'];
+                       $server['registered-users'] = max($nodeinfo['usage']['users']['total'], 1);
+               }
+
+               if (!empty($nodeinfo['usage']['users']['activeMonth'])) {
+                       $server['active-month-users'] = max($nodeinfo['usage']['users']['activeMonth'], 0);
+               }
+
+               if (!empty($nodeinfo['usage']['users']['activeHalfyear'])) {
+                       $server['active-halfyear-users'] = max($nodeinfo['usage']['users']['activeHalfyear'], 0);
+               }
+
+               if (!empty($nodeinfo['usage']['localPosts'])) {
+                       $server['local-posts'] = max($nodeinfo['usage']['localPosts'], 0);
+               }
+
+               if (!empty($nodeinfo['usage']['localComments'])) {
+                       $server['local-comments'] = max($nodeinfo['usage']['localComments'], 0);
                }
 
                if (!empty($nodeinfo['protocols'])) {
                        $protocols = [];
                        foreach ($nodeinfo['protocols'] as $protocol) {
-                               $protocols[$protocol] = true;
+                               if (is_string($protocol)) {
+                                       $protocols[$protocol] = true;
+                               }
                        }
 
                        if (!empty($protocols['dfrn'])) {
@@ -859,7 +1006,7 @@ class GServer
         */
        private static function fetchSiteinfo(string $url, array $serverdata)
        {
-               $curlResult = DI::httpRequest()->get($url . '/siteinfo.json');
+               $curlResult = DI::httpClient()->get($url . '/siteinfo.json', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
                        return $serverdata;
                }
@@ -895,7 +1042,23 @@ class GServer
                }
 
                if (!empty($data['channels_total'])) {
-                       $serverdata['registered-users'] = $data['channels_total'];
+                       $serverdata['registered-users'] = max($data['channels_total'], 1);
+               }
+
+               if (!empty($data['channels_active_monthly'])) {
+                       $serverdata['active-month-users'] = max($data['channels_active_monthly'], 0);
+               }
+
+               if (!empty($data['channels_active_halfyear'])) {
+                       $serverdata['active-halfyear-users'] = max($data['channels_active_halfyear'], 0);
+               }
+
+               if (!empty($data['local_posts'])) {
+                       $serverdata['local-posts'] = max($data['local_posts'], 0);
+               }
+
+               if (!empty($data['local_comments'])) {
+                       $serverdata['local-comments'] = max($data['local_comments'], 0);
                }
 
                if (!empty($data['register_policy'])) {
@@ -928,7 +1091,7 @@ class GServer
        private static function validHostMeta(string $url)
        {
                $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
-               $curlResult = DI::httpRequest()->get($url . '/.well-known/host-meta', ['timeout' => $xrd_timeout]);
+               $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta', HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
                if (!$curlResult->isSuccess()) {
                        return false;
                }
@@ -991,16 +1154,18 @@ class GServer
                        return $serverdata;
                }
 
+               $time = time();
                foreach ($contacts as $contact) {
-                       $probed = Contact::getByURL($contact);
-                       if (!empty($probed) && in_array($probed['network'], Protocol::FEDERATED)) {
+                       $probed = Contact::getByURL($contact, true);
+                       if (!empty($probed) && !$probed['failed'] && in_array($probed['network'], Protocol::FEDERATED)) {
                                $serverdata['network'] = $probed['network'];
                                break;
+                       } elseif ((time() - $time) > 10) {
+                               // To reduce the stress on remote systems we probe a maximum of 10 seconds
+                               break;
                        }
                }
 
-               $serverdata['registered-users'] = max($serverdata['registered-users'], count($contacts));
-
                return $serverdata;
        }
 
@@ -1018,7 +1183,7 @@ class GServer
        {
                $serverdata['poco'] = '';
 
-               $curlResult = DI::httpRequest()->get($url . '/poco');
+               $curlResult = DI::httpClient()->get($url . '/poco', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
                        return $serverdata;
                }
@@ -1030,7 +1195,7 @@ class GServer
 
                if (!empty($data['totalResults'])) {
                        $registeredUsers = $serverdata['registered-users'] ?? 0;
-                       $serverdata['registered-users'] = max($data['totalResults'], $registeredUsers);
+                       $serverdata['registered-users'] = max($data['totalResults'], $registeredUsers, 1);
                        $serverdata['directory-type'] = self::DT_POCO;
                        $serverdata['poco'] = $url . '/poco';
                }
@@ -1048,7 +1213,7 @@ class GServer
         */
        public static function checkMastodonDirectory(string $url, array $serverdata)
        {
-               $curlResult = DI::httpRequest()->get($url . '/api/v1/directory?limit=1');
+               $curlResult = DI::httpClient()->get($url . '/api/v1/directory?limit=1', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
                        return $serverdata;
                }
@@ -1065,6 +1230,53 @@ class GServer
                return $serverdata;
        }
 
+       /**
+        * Detects Peertube via their known endpoint
+        *
+        * @param string $url        URL of the given server
+        * @param array  $serverdata array with server data
+        *
+        * @return array server data
+        */
+       private static function detectPeertube(string $url, array $serverdata)
+       {
+               $curlResult = DI::httpClient()->get($url . '/api/v1/config', HttpClientAccept::JSON);
+               if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
+                       return $serverdata;
+               }
+
+               $data = json_decode($curlResult->getBody(), true);
+               if (empty($data)) {
+                       return $serverdata;
+               }
+
+               if (!empty($data['instance']) && !empty($data['serverVersion'])) {
+                       $serverdata['platform'] = 'peertube';
+                       $serverdata['version'] = $data['serverVersion'];
+                       $serverdata['network'] = Protocol::ACTIVITYPUB;
+
+                       if (!empty($data['instance']['name'])) {
+                               $serverdata['site_name'] = $data['instance']['name'];
+                       }
+
+                       if (!empty($data['instance']['shortDescription'])) {
+                               $serverdata['info'] = $data['instance']['shortDescription'];
+                       }
+
+                       if (!empty($data['signup'])) {
+                               if (!empty($data['signup']['allowed'])) {
+                                       $serverdata['register_policy'] = Register::OPEN;
+                               }
+                       }
+
+                       if (in_array($serverdata['detection-method'], [self::DETECT_HEADER, self::DETECT_BODY, self::DETECT_MANUAL])) {
+                               $serverdata['detection-method'] = self::DETECT_V1_CONFIG;
+                       }
+               }
+
+               return $serverdata;
+       }
+
        /**
         * Detects the version number of a given server when it was a NextCloud installation
         *
@@ -1075,8 +1287,7 @@ class GServer
         */
        private static function detectNextcloud(string $url, array $serverdata)
        {
-               $curlResult = DI::httpRequest()->get($url . '/status.php');
-
+               $curlResult = DI::httpClient()->get($url . '/status.php', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
                        return $serverdata;
                }
@@ -1099,6 +1310,65 @@ class GServer
                return $serverdata;
        }
 
+       private static function fetchWeeklyUsage(string $url, array $serverdata) {
+               $curlResult = DI::httpClient()->get($url . '/api/v1/instance/activity', HttpClientAccept::JSON);
+               if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
+                       return $serverdata;
+               }
+
+               $data = json_decode($curlResult->getBody(), true);
+               if (empty($data)) {
+                       return $serverdata;
+               }
+
+               $current_week = [];
+               foreach ($data as $week) {
+                       // Use only data from a full week
+                       if (empty($week['week']) || (time() - $week['week']) < 7 * 24 * 60 * 60) {
+                               continue;
+                       }
+
+                       // Most likely the data is sorted correctly. But we better are safe than sorry
+                       if (empty($current_week['week']) || ($current_week['week'] < $week['week'])) {
+                               $current_week = $week;
+                       } 
+               }
+
+               if (!empty($current_week['logins'])) {
+                       $serverdata['active-week-users'] = max($current_week['logins'], 0);
+               }
+
+               return $serverdata;
+       }
+       
+       /**
+        * Detects the server network type from contacts of that server
+        *
+        * @param string $url        URL of the given server
+        * @param array  $serverdata array with server data
+        *
+        * @return array server data
+        */
+       private static function detectFromContacts(string $url, array $serverdata)
+       {
+               $gserver = DBA::selectFirst('gserver', ['id'], ['nurl' => Strings::normaliseLink($url)]);
+               if (empty($gserver)) {
+                       return $serverdata;     
+               }
+
+               $contact = Contact::selectFirst(['id'], ['uid' => 0, 'failed' => false, 'gsid' => $gserver['id']]);
+
+               // Via probing we can be sure that the server is responding
+               if (!empty($contact['id']) && Contact::updateFromProbe($contact['id'])) {
+                       $contact = Contact::selectFirst(['network', 'failed'], ['id' => $contact['id']]);
+                       if (!$contact['failed'] && in_array($contact['network'], Protocol::FEDERATED)) {
+                               $serverdata['network'] = $contact['network'];
+                       }
+               }
+
+               return $serverdata;
+       }
+
        /**
         * Detects data from a given server url if it was a mastodon alike system
         *
@@ -1109,8 +1379,7 @@ class GServer
         */
        private static function detectMastodonAlikes(string $url, array $serverdata)
        {
-               $curlResult = DI::httpRequest()->get($url . '/api/v1/instance');
-
+               $curlResult = DI::httpClient()->get($url . '/api/v1/instance', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
                        return $serverdata;
                }
@@ -1144,7 +1413,7 @@ class GServer
                }
 
                if (!empty($data['stats']['user_count'])) {
-                       $serverdata['registered-users'] = $data['stats']['user_count'];
+                       $serverdata['registered-users'] = max($data['stats']['user_count'], 1);
                }
 
                if (!empty($serverdata['version']) && preg_match('/.*?\(compatible;\s(.*)\s(.*)\)/ism', $serverdata['version'], $matches)) {
@@ -1175,7 +1444,7 @@ class GServer
         */
        private static function detectHubzilla(string $url, array $serverdata)
        {
-               $curlResult = DI::httpRequest()->get($url . '/api/statusnet/config.json');
+               $curlResult = DI::httpClient()->get($url . '/api/statusnet/config.json', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
                        return $serverdata;
                }
@@ -1260,7 +1529,57 @@ class GServer
                }
 
                return $val;
-        }
+       }
+
+       /**
+        * Detect if the URL belongs to a pump.io server
+        *
+        * @param string $url        URL of the given server
+        * @param array  $serverdata array with server data
+        *
+        * @return array server data
+        */
+       private static function detectPumpIO(string $url, array $serverdata)
+       {
+               $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta.json', HttpClientAccept::JSON);
+               if (!$curlResult->isSuccess()) {
+                       return $serverdata;
+               }
+
+               $data = json_decode($curlResult->getBody(), true);
+               if (empty($data['links'])) {
+                       return $serverdata;
+
+               }
+
+               // We are looking for some endpoints that are typical for pump.io
+               $trust = 0;
+               foreach ($data['links'] as $link) {
+                       if (empty($link['rel'])) {
+                               continue;
+                       }
+                       if (in_array($link['rel'], ['registration_endpoint', 'dialback', 'http://apinamespace.org/activitypub/whoami'])) {
+                               ++$trust;
+                       }
+               }
+
+               if ($trust == 3) {
+                       $serverdata['detection-method'] = self::DETECT_PUMPIO;
+
+                       $serverdata['platform'] = 'pumpio';
+                       $serverdata['version']  = '';
+                       $serverdata['network']  = Protocol::PUMPIO;
+
+                       $servers = $curlResult->getHeader('Server');
+                       foreach ($servers as $server) {
+                               if (preg_match("#pump.io/(.*)\s#U", $server, $matches)) {
+                                       $serverdata['version']  = $matches[1];
+                               }
+                       }
+               }
+
+               return $serverdata;
+       }
 
        /**
         * Detect if the URL belongs to a GNU Social server
@@ -1273,7 +1592,7 @@ class GServer
        private static function detectGNUSocial(string $url, array $serverdata)
        {
                // Test for GNU Social
-               $curlResult = DI::httpRequest()->get($url . '/api/gnusocial/version.json');
+               $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)) {
                        $serverdata['platform'] = 'gnusocial';
@@ -1286,12 +1605,12 @@ class GServer
                        if (in_array($serverdata['detection-method'], [self::DETECT_HEADER, self::DETECT_BODY, self::DETECT_MANUAL])) {
                                $serverdata['detection-method'] = self::DETECT_GNUSOCIAL;
                        }
-       
+
                        return $serverdata;
                }
 
                // Test for Statusnet
-               $curlResult = DI::httpRequest()->get($url . '/api/statusnet/version.json');
+               $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)) {
 
@@ -1327,9 +1646,11 @@ class GServer
         */
        private static function detectFriendica(string $url, array $serverdata)
        {
-               $curlResult = DI::httpRequest()->get($url . '/friendica/json');
+               // There is a bug in some versions of Friendica that will return an ActivityStream actor when the content type "application/json" is requested.
+               // Because of this me must not use ACCEPT_JSON here.
+               $curlResult = DI::httpClient()->get($url . '/friendica/json');
                if (!$curlResult->isSuccess()) {
-                       $curlResult = DI::httpRequest()->get($url . '/friendika/json');
+                       $curlResult = DI::httpClient()->get($url . '/friendika/json');
                        $friendika = true;
                        $platform = 'Friendika';
                } else {
@@ -1346,7 +1667,7 @@ class GServer
                        return $serverdata;
                }
 
-               if (in_array($serverdata['detection-method'], [self::DETECT_HEADER, self::DETECT_BODY, self::DETECT_MANUAL])) {                 
+               if (in_array($serverdata['detection-method'], [self::DETECT_HEADER, self::DETECT_BODY, self::DETECT_MANUAL])) {
                        $serverdata['detection-method'] = $friendika ? self::DETECT_FRIENDIKA : self::DETECT_FRIENDICA;
                }
 
@@ -1401,6 +1722,14 @@ class GServer
         */
        private static function analyseRootBody($curlResult, array $serverdata, string $url)
        {
+               if (empty($curlResult->getBody())) {
+                       return $serverdata;
+               }
+
+               // Using only body information we cannot safely detect a lot of systems.
+               // So we define a list of platforms that we can detect safely.
+               $valid_platforms = ['friendica', 'friendika', 'diaspora', 'mastodon', 'hubzilla', 'misskey', 'peertube', 'wordpress', 'write.as'];
+
                $doc = new DOMDocument();
                @$doc->loadHTML($curlResult->getBody());
                $xpath = new DOMXPath($doc);
@@ -1446,15 +1775,12 @@ class GServer
 
                                if (count($version_part) == 2) {
                                        if (in_array($version_part[0], ['WordPress'])) {
-                                               $serverdata['platform'] = strtolower($version_part[0]);
+                                               $serverdata['platform'] = 'wordpress';
                                                $serverdata['version'] = $version_part[1];
 
                                                // We still do need a reliable test if some AP plugin is activated
-                                               if (DBA::exists('apcontact', ['baseurl' => $url])) {
-                                                       $serverdata['network'] = Protocol::ACTIVITYPUB;
-                                               } else {
-                                                       $serverdata['network'] = Protocol::FEED;
-                                               }
+                                               // By now we just check in a later process for some known contacts
+                                               $serverdata['network'] = Protocol::FEED;
 
                                                if ($serverdata['detection-method'] == self::DETECT_MANUAL) {
                                                        $serverdata['detection-method'] = self::DETECT_BODY;
@@ -1514,7 +1840,11 @@ class GServer
                        }
                }
 
-               if (!empty($serverdata['network']) && ($serverdata['detection-method'] == self::DETECT_MANUAL)) {
+               if (!empty($serverdata['platform']) && in_array($serverdata['detection-method'], [self::DETECT_MANUAL, self::DETECT_BODY]) && !in_array($serverdata['platform'], $valid_platforms)) {
+                       $serverdata['network'] = Protocol::PHANTOM;
+                       $serverdata['version'] = '';
+                       $serverdata['detection-method'] = self::DETECT_MANUAL;
+               } elseif (!empty($serverdata['network']) && ($serverdata['detection-method'] == self::DETECT_MANUAL)) {
                        $serverdata['detection-method'] = self::DETECT_BODY;
                }
 
@@ -1537,11 +1867,11 @@ class GServer
                } elseif ($curlResult->inHeader('x-diaspora-version')) {
                        $serverdata['platform'] = 'diaspora';
                        $serverdata['network'] = Protocol::DIASPORA;
-                       $serverdata['version'] = $curlResult->getHeader('x-diaspora-version');
+                       $serverdata['version'] = $curlResult->getHeader('x-diaspora-version')[0] ?? '';
                } elseif ($curlResult->inHeader('x-friendica-version')) {
                        $serverdata['platform'] = 'friendica';
                        $serverdata['network'] = Protocol::DFRN;
-                       $serverdata['version'] = $curlResult->getHeader('x-friendica-version');
+                       $serverdata['version'] = $curlResult->getHeader('x-friendica-version')[0] ?? '';
                } else {
                        return $serverdata;
                }
@@ -1584,22 +1914,11 @@ class GServer
 
                $last_update = date('c', time() - (60 * 60 * 24 * $requery_days));
 
-               $gservers = DBA::p("SELECT `id`, `url`, `nurl`, `network`, `poco`, `directory-type`
-                       FROM `gserver`
-                       WHERE NOT `failed`
-                       AND `directory-type` != ?
-                       AND `last_poco_query` < ?
-                       ORDER BY RAND()", self::DT_NONE, $last_update
-               );
+               $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],
+                       ['order' => ['RAND()']]);
 
                while ($gserver = DBA::fetch($gservers)) {
-                       if (!GServer::check($gserver['url'], $gserver['network'])) {
-                               // The server is not reachable? Okay, then we will try it later
-                               $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
-                               DBA::update('gserver', $fields, ['nurl' => $gserver['nurl']]);
-                               continue;
-                       }
-
                        Logger::info('Update peer list', ['server' => $gserver['url'], 'id' => $gserver['id']]);
                        Worker::add(PRIORITY_LOW, 'UpdateServerPeers', $gserver['url']);
 
@@ -1608,7 +1927,7 @@ class GServer
 
                        $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
                        DBA::update('gserver', $fields, ['nurl' => $gserver['nurl']]);
-       
+
                        if (--$no_of_queries == 0) {
                                break;
                        }
@@ -1636,13 +1955,13 @@ class GServer
                $protocols = ['activitypub', 'diaspora', 'dfrn', 'ostatus'];
                foreach ($protocols as $protocol) {
                        $query = '{nodes(protocol:"' . $protocol . '"){host}}';
-                       $curlResult = DI::httpRequest()->fetch('https://the-federation.info/graphql?query=' . urlencode($query));
+                       $curlResult = DI::httpClient()->fetch('https://the-federation.info/graphql?query=' . urlencode($query), HttpClientAccept::JSON);
                        if (!empty($curlResult)) {
                                $data = json_decode($curlResult, true);
                                if (!empty($data['data']['nodes'])) {
                                        foreach ($data['data']['nodes'] as $server) {
                                                // Using "only_nodeinfo" since servers that are listed on that page should always have it.
-                                               Worker::add(PRIORITY_LOW, 'UpdateGServer', 'https://' . $server['host'], true);
+                                               self::add('https://' . $server['host'], true);
                                        }
                                }
                        }
@@ -1653,19 +1972,99 @@ class GServer
 
                if (!empty($accesstoken)) {
                        $api = 'https://instances.social/api/1.0/instances/list?count=0';
-                       $header = ['Authorization: Bearer '.$accesstoken];
-                       $curlResult = DI::httpRequest()->get($api, ['header' => $header]);
-
+                       $curlResult = DI::httpClient()->get($api, HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $accesstoken]]]);
                        if ($curlResult->isSuccess()) {
                                $servers = json_decode($curlResult->getBody(), true);
 
                                foreach ($servers['instances'] as $server) {
                                        $url = (is_null($server['https_score']) ? 'http' : 'https') . '://' . $server['name'];
-                                       Worker::add(PRIORITY_LOW, 'UpdateGServer', $url);
+                                       self::add($url);
                                }
                        }
                }
 
                DI::config()->set('poco', 'last_federation_discovery', time());
        }
+
+       /**
+        * Set the protocol for the given server
+        *
+        * @param int $gsid     Server id
+        * @param int $protocol Protocol id
+        * @return void
+        * @throws Exception
+        */
+       public static function setProtocol(int $gsid, int $protocol)
+       {
+               if (empty($gsid)) {
+                       return;
+               }
+
+               $gserver = DBA::selectFirst('gserver', ['protocol', 'url'], ['id' => $gsid]);
+               if (!DBA::isResult($gserver)) {
+                       return;
+               }
+
+               $old = $gserver['protocol'];
+
+               if (!is_null($old)) {
+                       /*
+                       The priority for the protocols is:
+                               1. ActivityPub
+                               2. DFRN via Diaspora
+                               3. Legacy DFRN
+                               4. Diaspora
+                               5. OStatus
+                       */
+
+                       // We don't need to change it when nothing is to be changed
+                       if ($old == $protocol) {
+                               return;
+                       }
+
+                       // We don't want to mark a server as OStatus when it had been marked with any other protocol before
+                       if ($protocol == Post\DeliveryData::OSTATUS) {
+                               return;
+                       }
+
+                       // If the server is marked as ActivityPub then we won't change it to anything different
+                       if ($old == Post\DeliveryData::ACTIVITYPUB) {
+                               return;
+                       }
+
+                       // Don't change it to anything lower than DFRN if the new one wasn't ActivityPub
+                       if (($old == Post\DeliveryData::DFRN) && ($protocol != Post\DeliveryData::ACTIVITYPUB)) {
+                               return;
+                       }
+
+                       // Don't change it to Diaspora when it is a legacy DFRN server
+                       if (($old == Post\DeliveryData::LEGACY_DFRN) && ($protocol == Post\DeliveryData::DIASPORA)) {
+                               return;
+                       }
+               }
+
+               Logger::info('Protocol for server', ['protocol' => $protocol, 'old' => $old, 'id' => $gsid, 'url' => $gserver['url'], 'callstack' => System::callstack(20)]);
+               DBA::update('gserver', ['protocol' => $protocol], ['id' => $gsid]);
+       }
+
+       /**
+        * Fetch the protocol of the given server
+        *
+        * @param int $gsid Server id
+        * @return int
+        * @throws Exception
+        */
+       public static function getProtocol(int $gsid)
+       {
+               if (empty($gsid)) {
+                       return null;
+               }
+
+               $gserver = DBA::selectFirst('gserver', ['protocol'], ['id' => $gsid]);
+               if (DBA::isResult($gserver)) {
+                       return $gserver['protocol'];
+               }
+
+               return null;
+       }
 }