]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/GServer.php
Merge pull request #11685 from MrPetovan/bug/11638-gserver_site_name-length
[friendica.git] / src / Model / GServer.php
index c4138cc2d01aba5f6eeb8670acf6d8018baf65f6..aa7a2bd6471bbe3b5730397ad4a60274243a3f98 100644 (file)
@@ -30,8 +30,10 @@ use Friendica\Core\System;
 use Friendica\Core\Worker;
 use Friendica\Database\Database;
 use Friendica\Database\DBA;
+use Friendica\Database\DBStructure;
 use Friendica\DI;
 use Friendica\Module\Register;
+use Friendica\Network\HTTPClient\Client\HttpClientAccept;
 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
 use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
 use Friendica\Protocol\Relay;
@@ -95,7 +97,7 @@ class GServer
         *
         * @param string $url
         * @param boolean $no_check Don't check if the server hadn't been found
-        * @return int gserver id
+        * @return int|null gserver id or NULL on empty URL or failed check
         */
        public static function getID(string $url, bool $no_check = false)
        {
@@ -155,7 +157,7 @@ class GServer
         *
         * @return boolean 'true' if server seems vital
         */
-       public static function reachable(string $profile, string $server = '', string $network = '', bool $force = false)
+       public static function reachable(string $profile, string $server = '', string $network = '', bool $force = false): bool
        {
                if ($server == '') {
                        $contact = Contact::getByURL($profile, null, ['baseurl']);
@@ -171,7 +173,7 @@ class GServer
                return self::check($server, $network, $force);
        }
 
-       public static function getNextUpdateDate(bool $success, string $created = '', string $last_contact = '')
+       public static function getNextUpdateDate(bool $success, string $created = '', string $last_contact = ''): string
        {
                // On successful contact process check again next week
                if ($success) {
@@ -230,7 +232,7 @@ class GServer
         *
         * @return boolean 'true' if server seems vital
         */
-       public static function check(string $server_url, string $network = '', bool $force = false, bool $only_nodeinfo = false)
+       public static function check(string $server_url, string $network = '', bool $force = false, bool $only_nodeinfo = false): bool
        {
                $server_url = self::cleanURL($server_url);
                if ($server_url == '') {
@@ -242,7 +244,7 @@ class GServer
                        if ($gserver['created'] <= DBA::NULL_DATETIME) {
                                $fields = ['created' => DateTimeFormat::utcNow()];
                                $condition = ['nurl' => Strings::normaliseLink($server_url)];
-                               DBA::update('gserver', $fields, $condition);
+                               self::update($fields, $condition);
                        }
 
                        if (!$force && (strtotime($gserver['next_contact']) > time())) {
@@ -267,7 +269,7 @@ class GServer
                $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(),
+                       self::update(['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]);
@@ -285,7 +287,7 @@ class GServer
         * @param string $url
         * @return string cleaned URL
         */
-       public static function cleanURL(string $url)
+       public static function cleanURL(string $url): string
        {
                $url = trim($url, '/');
                $url = str_replace('/index.php', '', $url);
@@ -304,7 +306,7 @@ class GServer
         * @param string $url
         * @return string base URL
         */
-       private static function getBaseURL(string $url)
+       private static function getBaseURL(string $url): string
        {
                $urlparts = parse_url(self::cleanURL($url));
                unset($urlparts['path']);
@@ -321,7 +323,7 @@ class GServer
         *
         * @return boolean 'true' if server could be detected
         */
-       public static function detect(string $url, string $network = '', bool $only_nodeinfo = false)
+       public static function detect(string $url, string $network = '', bool $only_nodeinfo = false): bool
        {
                Logger::info('Detect server type', ['server' => $url]);
                $serverdata = ['detection-method' => self::DETECT_MANUAL];
@@ -337,13 +339,13 @@ 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()],
+                       self::update(['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::httpClient()->get($url . '/.well-known/nodeinfo', [HttpClientOptions::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;
@@ -351,7 +353,7 @@ class GServer
 
                // 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, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
+                       $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);
@@ -393,7 +395,7 @@ class GServer
                                        $basedata = ['detection-method' => self::DETECT_MANUAL];
                                }
 
-                               $curlResult = DI::httpClient()->get($baseurl, [HttpClientOptions::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()]);
@@ -417,7 +419,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::httpClient()->get($url, [HttpClientOptions::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);
@@ -437,14 +439,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;
                        }
@@ -471,7 +479,7 @@ class GServer
                                $serverdata = self::detectNextcloud($url, $serverdata);
                        }
 
-                       if (empty($serverdata['network'])) {
+                       if (empty($nodeinfo['network']) && empty($serverdata['network'])) {
                                $serverdata = self::detectGNUSocial($url, $serverdata);
                        }
 
@@ -486,9 +494,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;
@@ -524,13 +536,13 @@ class GServer
                $serverdata['last_contact'] = DateTimeFormat::utcNow();
                $serverdata['failed'] = false;
 
-               $gserver = DBA::selectFirst('gserver', ['network'], ['nurl' => Strings::normaliseLink($url)]);
+               $gserver = DBA::selectFirst('gserver', ['network'], ['nurl' => $serverdata['nurl']]);
                if (!DBA::isResult($gserver)) {
                        $serverdata['created'] = DateTimeFormat::utcNow();
                        $ret = DBA::insert('gserver', $serverdata);
                        $id = DBA::lastInsertId();
                } else {
-                       $ret = DBA::update('gserver', $serverdata, ['nurl' => $serverdata['nurl']]);
+                       $ret = self::update($serverdata, ['nurl' => $serverdata['nurl']]);
                        $gserver = DBA::selectFirst('gserver', ['id'], ['nurl' => $serverdata['nurl']]);
                        if (DBA::isResult($gserver)) {
                                $id = $gserver['id'];
@@ -544,7 +556,23 @@ class GServer
                        $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]);
+                               self::update(['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]);
+                                       self::update(['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]);
+                                       self::update(['active-halfyear-users' => $contacts], ['id' => $id]);
+                               }
                        }
                }
 
@@ -559,13 +587,14 @@ class GServer
         * Fetch relay data from a given server url
         *
         * @param string $server_url address of the server
+        * @return void
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        private static function discoverRelay(string $server_url)
        {
                Logger::info('Discover relay data', ['server' => $server_url]);
 
-               $curlResult = DI::httpClient()->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;
                }
@@ -591,7 +620,7 @@ class GServer
 
                if (($gserver['relay-subscribe'] != $data['subscribe']) || ($gserver['relay-scope'] != $data['scope'])) {
                        $fields = ['relay-subscribe' => $data['subscribe'], 'relay-scope' => $data['scope']];
-                       DBA::update('gserver', $fields, ['id' => $gserver['id']]);
+                       self::update($fields, ['id' => $gserver['id']]);
                }
 
                DBA::delete('gserver-tag', ['gserver-id' => $gserver['id']]);
@@ -655,12 +684,11 @@ class GServer
         * Fetch server data from '/statistics.json' on the given server
         *
         * @param string $url URL of the given server
-        *
         * @return array server data
         */
-       private static function fetchStatistics(string $url)
+       private static function fetchStatistics(string $url): array
        {
-               $curlResult = DI::httpClient()->get($url . '/statistics.json');
+               $curlResult = DI::httpClient()->get($url . '/statistics.json', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
                        return [];
                }
@@ -731,7 +759,7 @@ class GServer
         * @return array Server data
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private static function fetchNodeinfo(string $url, ICanHandleHttpResponses $httpResult)
+       private static function fetchNodeinfo(string $url, ICanHandleHttpResponses $httpResult): array
        {
                if (!$httpResult->isSuccess()) {
                        return [];
@@ -784,10 +812,9 @@ class GServer
         * @return array Server data
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private static function parseNodeinfo1(string $nodeinfo_url)
+       private static function parseNodeinfo1(string $nodeinfo_url): array
        {
-               $curlResult = DI::httpClient()->get($nodeinfo_url);
-
+               $curlResult = DI::httpClient()->get($nodeinfo_url, HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
                        return [];
                }
@@ -878,9 +905,9 @@ class GServer
         * @return array Server data
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private static function parseNodeinfo2(string $nodeinfo_url)
+       private static function parseNodeinfo2(string $nodeinfo_url): array
        {
-               $curlResult = DI::httpClient()->get($nodeinfo_url);
+               $curlResult = DI::httpClient()->get($nodeinfo_url, HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
                        return [];
                }
@@ -891,8 +918,11 @@ class GServer
                        return [];
                }
 
-               $server = ['detection-method' => self::DETECT_NODEINFO_2,
-                       'register_policy' => Register::CLOSED];
+               $server = [
+                       'detection-method' => self::DETECT_NODEINFO_2,
+                       'register_policy' => Register::CLOSED,
+                       'platform' => 'unknown',
+               ];
 
                if (!empty($nodeinfo['openRegistrations'])) {
                        $server['register_policy'] = Register::OPEN;
@@ -908,6 +938,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';
+                               }
                        }
                }
 
@@ -970,12 +1005,11 @@ class GServer
         *
         * @param string $url        URL of the given server
         * @param array  $serverdata array with server data
-        *
         * @return array server data
         */
-       private static function fetchSiteinfo(string $url, array $serverdata)
+       private static function fetchSiteinfo(string $url, array $serverdata): array
        {
-               $curlResult = DI::httpClient()->get($url . '/siteinfo.json');
+               $curlResult = DI::httpClient()->get($url . '/siteinfo.json', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
                        return $serverdata;
                }
@@ -1054,13 +1088,12 @@ class GServer
         * Checks if the server contains a valid host meta file
         *
         * @param string $url URL of the given server
-        *
         * @return boolean 'true' if the server seems to be vital
         */
-       private static function validHostMeta(string $url)
+       private static function validHostMeta(string $url): bool
        {
                $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
-               $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta', [HttpClientOptions::TIMEOUT => $xrd_timeout]);
+               $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta', HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
                if (!$curlResult->isSuccess()) {
                        return false;
                }
@@ -1100,10 +1133,9 @@ class GServer
         *
         * @param string $url        URL of the given server
         * @param array  $serverdata array with server data
-        *
         * @return array server data
         */
-       private static function detectNetworkViaContacts(string $url, array $serverdata)
+       private static function detectNetworkViaContacts(string $url, array $serverdata): array
        {
                $contacts = [];
 
@@ -1145,14 +1177,13 @@ class GServer
         *
         * @param string $url        URL of the given server
         * @param array  $serverdata array with server data
-        *
         * @return array server data
         */
-       private static function checkPoCo(string $url, array $serverdata)
+       private static function checkPoCo(string $url, array $serverdata): array
        {
                $serverdata['poco'] = '';
 
-               $curlResult = DI::httpClient()->get($url . '/poco');
+               $curlResult = DI::httpClient()->get($url . '/poco', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
                        return $serverdata;
                }
@@ -1177,12 +1208,11 @@ class GServer
         *
         * @param string $url        URL of the given server
         * @param array  $serverdata array with server data
-        *
         * @return array server data
         */
-       public static function checkMastodonDirectory(string $url, array $serverdata)
+       public static function checkMastodonDirectory(string $url, array $serverdata): array
        {
-               $curlResult = DI::httpClient()->get($url . '/api/v1/directory?limit=1');
+               $curlResult = DI::httpClient()->get($url . '/api/v1/directory?limit=1', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
                        return $serverdata;
                }
@@ -1207,10 +1237,9 @@ class GServer
         *
         * @return array server data
         */
-       private static function detectPeertube(string $url, array $serverdata)
+       private static function detectPeertube(string $url, array $serverdata): array
        {
-               $curlResult = DI::httpClient()->get($url . '/api/v1/config');
-
+               $curlResult = DI::httpClient()->get($url . '/api/v1/config', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
                        return $serverdata;
                }
@@ -1252,13 +1281,11 @@ class GServer
         *
         * @param string $url        URL of the given server
         * @param array  $serverdata array with server data
-        *
         * @return array server data
         */
-       private static function detectNextcloud(string $url, array $serverdata)
+       private static function detectNextcloud(string $url, array $serverdata): array
        {
-               $curlResult = DI::httpClient()->get($url . '/status.php');
-
+               $curlResult = DI::httpClient()->get($url . '/status.php', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
                        return $serverdata;
                }
@@ -1281,9 +1308,16 @@ class GServer
                return $serverdata;
        }
 
-       private static function fetchWeeklyUsage(string $url, array $serverdata) {
-               $curlResult = DI::httpClient()->get($url . '/api/v1/instance/activity');
-
+       /**
+        * Fetches weekly usage data
+        *
+        * @param string $url        URL of the given server
+        * @param array  $serverdata array with server data
+        * @return array server data
+        */
+       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() == '')) {
                        return $serverdata;
                }
@@ -1312,19 +1346,44 @@ class GServer
 
                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): array
+       {
+               $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
         *
         * @param string $url        URL of the given server
         * @param array  $serverdata array with server data
-        *
         * @return array server data
         */
-       private static function detectMastodonAlikes(string $url, array $serverdata)
+       private static function detectMastodonAlikes(string $url, array $serverdata): array
        {
-               $curlResult = DI::httpClient()->get($url . '/api/v1/instance');
-
+               $curlResult = DI::httpClient()->get($url . '/api/v1/instance', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
                        return $serverdata;
                }
@@ -1384,12 +1443,11 @@ class GServer
         *
         * @param string $url        URL of the given server
         * @param array  $serverdata array with server data
-        *
         * @return array server data
         */
-       private static function detectHubzilla(string $url, array $serverdata)
+       private static function detectHubzilla(string $url, array $serverdata): array
        {
-               $curlResult = DI::httpClient()->get($url . '/api/statusnet/config.json');
+               $curlResult = DI::httpClient()->get($url . '/api/statusnet/config.json', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
                        return $serverdata;
                }
@@ -1462,10 +1520,9 @@ class GServer
         * Converts input value to a boolean value
         *
         * @param string|integer $val
-        *
         * @return boolean
         */
-       private static function toBoolean($val)
+       private static function toBoolean($val): bool
        {
                if (($val == 'true') || ($val == 1)) {
                        return true;
@@ -1481,12 +1538,11 @@ class GServer
         *
         * @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)
+       private static function detectPumpIO(string $url, array $serverdata): array
        {
-               $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta.json');
+               $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta.json', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
                        return $serverdata;
                }
@@ -1494,7 +1550,6 @@ class GServer
                $data = json_decode($curlResult->getBody(), true);
                if (empty($data['links'])) {
                        return $serverdata;
-
                }
 
                // We are looking for some endpoints that are typical for pump.io
@@ -1531,13 +1586,12 @@ class GServer
         *
         * @param string $url        URL of the given server
         * @param array  $serverdata array with server data
-        *
         * @return array server data
         */
-       private static function detectGNUSocial(string $url, array $serverdata)
+       private static function detectGNUSocial(string $url, array $serverdata): array
        {
                // Test for GNU Social
-               $curlResult = DI::httpClient()->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';
@@ -1555,7 +1609,7 @@ class GServer
                }
 
                // Test for Statusnet
-               $curlResult = DI::httpClient()->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)) {
 
@@ -1586,11 +1640,12 @@ class GServer
         *
         * @param string $url        URL of the given server
         * @param array  $serverdata array with server data
-        *
         * @return array server data
         */
-       private static function detectFriendica(string $url, array $serverdata)
+       private static function detectFriendica(string $url, array $serverdata): array
        {
+               // 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::httpClient()->get($url . '/friendika/json');
@@ -1660,10 +1715,9 @@ class GServer
         * @param object $curlResult result of curl execution
         * @param array  $serverdata array with server data
         * @param string $url        Server URL
-        *
         * @return array server data
         */
-       private static function analyseRootBody($curlResult, array $serverdata, string $url)
+       private static function analyseRootBody($curlResult, array $serverdata, string $url): array
        {
                if (empty($curlResult->getBody())) {
                        return $serverdata;
@@ -1802,7 +1856,7 @@ class GServer
         *
         * @return array server data
         */
-       private static function analyseRootHeader($curlResult, array $serverdata)
+       private static function analyseRootHeader($curlResult, array $serverdata): array
        {
                if ($curlResult->getHeader('server') == 'Mastodon') {
                        $serverdata['platform'] = 'mastodon';
@@ -1869,7 +1923,7 @@ class GServer
                        Worker::add(PRIORITY_LOW, 'UpdateServerDirectory', $gserver);
 
                        $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
-                       DBA::update('gserver', $fields, ['nurl' => $gserver['nurl']]);
+                       self::update($fields, ['nurl' => $gserver['nurl']]);
 
                        if (--$no_of_queries == 0) {
                                break;
@@ -1898,7 +1952,7 @@ class GServer
                $protocols = ['activitypub', 'diaspora', 'dfrn', 'ostatus'];
                foreach ($protocols as $protocol) {
                        $query = '{nodes(protocol:"' . $protocol . '"){host}}';
-                       $curlResult = DI::httpClient()->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'])) {
@@ -1915,8 +1969,7 @@ class GServer
 
                if (!empty($accesstoken)) {
                        $api = 'https://instances.social/api/1.0/instances/list?count=0';
-                       $curlResult = DI::httpClient()->get($api, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $accesstoken]]]);
-
+                       $curlResult = DI::httpClient()->get($api, HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $accesstoken]]]);
                        if ($curlResult->isSuccess()) {
                                $servers = json_decode($curlResult->getBody(), true);
 
@@ -1988,17 +2041,17 @@ class GServer
                }
 
                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]);
+               self::update(['protocol' => $protocol], ['id' => $gsid]);
        }
 
        /**
         * Fetch the protocol of the given server
         *
         * @param int $gsid Server id
-        * @return int
+        * @return ?int One of Post\DeliveryData protocol constants or null if unknown or gserver is missing
         * @throws Exception
         */
-       public static function getProtocol(int $gsid)
+       public static function getProtocol(int $gsid): ?int
        {
                if (empty($gsid)) {
                        return null;
@@ -2011,4 +2064,19 @@ class GServer
 
                return null;
        }
+
+       /**
+        * Enforces gserver table field maximum sizes to avoid "Data too long" database errors
+        *
+        * @param array $fields
+        * @param array $condition
+        * @return bool
+        * @throws Exception
+        */
+       public static function update(array $fields, array $condition): bool
+       {
+               $fields = DBStructure::getFieldsForTable('gserver', $fields);
+
+               return DBA::update('gserver', $fields, $condition);
+       }
 }