]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/GServer.php
spelling: existence
[friendica.git] / src / Model / GServer.php
index ac8a843274ff0c78db89275dcd2b103feadab935..a1c2819515117738fc6d4cb489157f1aefb52ee2 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -44,7 +44,9 @@ use Friendica\Util\Network;
 use Friendica\Util\Strings;
 use Friendica\Util\XML;
 use Friendica\Network\HTTPException;
+use Friendica\Worker\UpdateGServer;
 use GuzzleHttp\Psr7\Uri;
+use Psr\Http\Message\UriInterface;
 
 /**
  * This class handles GServer related functions
@@ -70,6 +72,7 @@ class GServer
        const DETECT_UNSPECIFIC = [self::DETECT_MANUAL, self::DETECT_HEADER, self::DETECT_BODY, self::DETECT_HOST_META, self::DETECT_CONTACTS, self::DETECT_AP_ACTOR];
 
        // Implementation specific endpoints
+       // @todo Possibly add Lemmy detection via the endpoint /api/v3/site
        const DETECT_FRIENDIKA = 10;
        const DETECT_FRIENDICA = 11;
        const DETECT_STATUSNET = 12;
@@ -89,7 +92,7 @@ class GServer
        const DETECT_NODEINFO_210 = 103;
 
        /**
-        * Check for the existance of a server and adds it in the background if not existant
+        * Check for the existence of a server and adds it in the background if not existant
         *
         * @param string $url
         * @param boolean $only_nodeinfo
@@ -98,11 +101,11 @@ class GServer
         */
        public static function add(string $url, bool $only_nodeinfo = false)
        {
-               if (self::getID($url, false)) {
+               if (self::getID($url)) {
                        return;
                }
 
-               Worker::add(PRIORITY_LOW, 'UpdateGServer', $url, $only_nodeinfo);
+               UpdateGServer::add(Worker::PRIORITY_LOW, $url, $only_nodeinfo);
        }
 
        /**
@@ -115,15 +118,22 @@ class GServer
         */
        public static function getID(string $url, bool $no_check = false): ?int
        {
+               $url = self::cleanURL($url);
+
                if (empty($url)) {
                        return null;
                }
 
-               $url = self::cleanURL($url);
-
                $gserver = DBA::selectFirst('gserver', ['id'], ['nurl' => Strings::normaliseLink($url)]);
                if (DBA::isResult($gserver)) {
                        Logger::debug('Got ID for URL', ['id' => $gserver['id'], 'url' => $url, 'callstack' => System::callstack(20)]);
+
+                       if (Network::isUrlBlocked($url)) {
+                               self::setBlockedById($gserver['id']);
+                       } else {
+                               self::setUnblockedById($gserver['id']);
+                       }
+
                        return $gserver['id'];
                }
 
@@ -164,29 +174,99 @@ class GServer
        }
 
        /**
-        * Checks if the given server is reachable
+        * Checks if the given server array is unreachable for a long time now
         *
-        * @param string  $profile URL of the given profile
-        * @param string  $server  URL of the given server (If empty, taken from profile)
-        * @param string  $network Network value that is used, when detection failed
-        * @param boolean $force   Force an update.
+        * @param integer $gsid
+        * @return boolean
+        */
+       private static function isDefunct(array $gserver): bool
+       {
+               return ($gserver['failed'] || in_array($gserver['network'], Protocol::FEDERATED)) &&
+                       ($gserver['last_contact'] >= $gserver['created']) &&
+                       ($gserver['last_contact'] < $gserver['last_failure']) &&
+                       ($gserver['last_contact'] < DateTimeFormat::utc('now - 90 days'));
+       }
+
+       /**
+        * Checks if the given server id is unreachable for a long time now
         *
-        * @return boolean 'true' if server seems vital
+        * @param integer $gsid
+        * @return boolean
+        */
+       public static function isDefunctById(int $gsid): bool
+       {
+               $gserver = DBA::selectFirst('gserver', ['url', 'next_contact', 'last_contact', 'last_failure', 'created', 'failed', 'network'], ['id' => $gsid]);
+               if (empty($gserver)) {
+                       return false;
+               } else {
+                       if (strtotime($gserver['next_contact']) < time()) {
+                               UpdateGServer::add(Worker::PRIORITY_LOW, $gserver['url']);
+                       }
+
+                       return self::isDefunct($gserver);
+               }
+       }
+
+       /**
+        * Checks if the given server id is reachable
+        *
+        * @param integer $gsid
+        * @return boolean
         */
-       public static function reachable(string $profile, string $server = '', string $network = '', bool $force = false): bool
+       public static function isReachableById(int $gsid): bool
        {
-               if ($server == '') {
-                       $contact = Contact::getByURL($profile, null, ['baseurl']);
-                       if (!empty($contact['baseurl'])) {
-                               $server = $contact['baseurl'];
+               $gserver = DBA::selectFirst('gserver', ['url', 'next_contact', 'failed', 'network'], ['id' => $gsid]);
+               if (empty($gserver)) {
+                       return true;
+               } else {
+                       if (strtotime($gserver['next_contact']) < time()) {
+                               UpdateGServer::add(Worker::PRIORITY_LOW, $gserver['url']);
                        }
+
+                       return !$gserver['failed'] && in_array($gserver['network'], Protocol::FEDERATED);
                }
+       }
 
-               if ($server == '') {
+       /**
+        * Checks if the given server is reachable
+        *
+        * @param array $contact Contact that should be checked
+        *
+        * @return boolean 'true' if server seems vital
+        */
+       public static function reachable(array $contact): bool
+       {
+               if (!empty($contact['gsid'])) {
+                       $gsid = $contact['gsid'];
+               } elseif (!empty($contact['baseurl'])) {
+                       $server = $contact['baseurl'];
+               } elseif ($contact['network'] == Protocol::DIASPORA) {
+                       $parts = parse_url($contact['url']);
+                       unset($parts['path']);
+                       $server = (string)Uri::fromParts($parts);
+               } else {
                        return true;
                }
 
-               return self::check($server, $network, $force);
+               if (!empty($gsid)) {
+                       $condition = ['id' => $gsid];
+               } else {
+                       $condition = ['nurl' => Strings::normaliseLink($server)];
+               }
+
+               $gserver = DBA::selectFirst('gserver', ['url', 'next_contact', 'failed', 'network'], $condition);
+               if (empty($gserver)) {
+                       $reachable = true;
+               } else {
+                       $reachable = !$gserver['failed'] && in_array($gserver['network'], Protocol::FEDERATED);
+                       $server    = $gserver['url'];
+               }
+
+               if (!empty($server) && (empty($gserver) || strtotime($gserver['next_contact']) < time())) {
+                       UpdateGServer::add(Worker::PRIORITY_LOW, $server);
+               }
+
+               return $reachable;
        }
 
        /**
@@ -268,6 +348,12 @@ class GServer
                        return false;
                }
 
+               if (Network::isUrlBlocked($server_url)) {
+                       Logger::info('Server is blocked', ['url' => $server_url]);
+                       self::setBlockedByUrl($server_url);
+                       return false;
+               }
+
                $gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($server_url)]);
                if (DBA::isResult($gserver)) {
                        if ($gserver['created'] <= DBA::NULL_DATETIME) {
@@ -288,46 +374,164 @@ class GServer
                return self::detect($server_url, $network, $only_nodeinfo);
        }
 
+       /**
+        * Reset failed server status by gserver id
+        *
+        * @param int    $gsid
+        * @param string $network
+        */
+       public static function setReachableById(int $gsid, string $network)
+       {
+               $gserver = DBA::selectFirst('gserver', ['url', 'failed', 'next_contact', 'network'], ['id' => $gsid]);
+               if (!DBA::isResult($gserver)) {
+                       return;
+               }
+
+               $blocked = Network::isUrlBlocked($gserver['url']);
+               if ($gserver['failed']) {
+                       $fields = ['failed' => false, 'blocked' => $blocked, 'last_contact' => DateTimeFormat::utcNow()];
+                       if (!empty($network) && !in_array($gserver['network'], Protocol::FEDERATED)) {
+                               $fields['network'] = $network;
+                       }
+                       self::update($fields, ['id' => $gsid]);
+                       Logger::info('Reset failed status for server', ['url' => $gserver['url']]);
+
+                       if (strtotime($gserver['next_contact']) < time()) {
+                               UpdateGServer::add(Worker::PRIORITY_LOW, $gserver['url']);
+                       }
+               } elseif ($blocked) {
+                       self::setBlockedById($gsid);
+               } else {
+                       self::setUnblockedById($gsid);
+               }
+       }
+
+       /**
+        * Set failed server status by gserver id
+        *
+        * @param int $gsid
+        */
+       public static function setFailureById(int $gsid)
+       {
+               $gserver = DBA::selectFirst('gserver', ['url', 'failed', 'next_contact'], ['id' => $gsid]);
+               if (DBA::isResult($gserver) && !$gserver['failed']) {
+                       self::update(['failed' => true, 'blocked' => Network::isUrlBlocked($gserver['url']), 'last_failure' => DateTimeFormat::utcNow()], ['id' => $gsid]);
+                       Logger::info('Set failed status for server', ['url' => $gserver['url']]);
+
+                       if (strtotime($gserver['next_contact']) < time()) {
+                               UpdateGServer::add(Worker::PRIORITY_LOW, $gserver['url']);
+                       }
+               }
+       }
+
+       public static function setUnblockedById(int $gsid)
+       {
+               $gserver = DBA::selectFirst('gserver', ['url'], ["(`blocked` OR `blocked` IS NULL) AND `id` = ?", $gsid]);
+               if (DBA::isResult($gserver)) {
+                       self::update(['blocked' => false], ['id' => $gsid]);
+                       Logger::info('Set unblocked status for server', ['url' => $gserver['url']]);
+               }
+       }
+
+       public static function setBlockedById(int $gsid)
+       {
+               $gserver = DBA::selectFirst('gserver', ['url'], ["(NOT `blocked` OR `blocked` IS NULL) AND `id` = ?", $gsid]);
+               if (DBA::isResult($gserver)) {
+                       self::update(['blocked' => true, 'failed' => true], ['id' => $gsid]);
+                       Logger::info('Set blocked status for server', ['url' => $gserver['url']]);
+               }
+       }
+
+       public static function setBlockedByUrl(string $url)
+       {
+               $gserver = DBA::selectFirst('gserver', ['url', 'id'], ["(NOT `blocked` OR `blocked` IS NULL) AND `nurl` = ?", Strings::normaliseLink($url)]);
+               if (DBA::isResult($gserver)) {
+                       self::update(['blocked' => true, 'failed' => true], ['id' => $gserver['id']]);
+                       Logger::info('Set blocked status for server', ['url' => $gserver['url']]);
+               }
+       }
+
        /**
         * Set failed server status
         *
         * @param string $url
+        * @return void
         */
-       public static function setFailure(string $url)
+       public static function setFailureByUrl(string $url)
        {
-               $gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($url)]);
+               $nurl = Strings::normaliseLink($url);
+
+               $gserver = DBA::selectFirst('gserver', [], ['nurl' => $nurl]);
                if (DBA::isResult($gserver)) {
                        $next_update = self::getNextUpdateDate(false, $gserver['created'], $gserver['last_contact']);
-                       self::update(['url' => $url, 'failed' => true, 'last_failure' => DateTimeFormat::utcNow(),
+                       self::update(['url' => $url, 'failed' => true, 'blocked' => Network::isUrlBlocked($url), 'last_failure' => DateTimeFormat::utcNow(),
                        'next_contact' => $next_update, 'network' => Protocol::PHANTOM, 'detection-method' => null],
-                       ['nurl' => Strings::normaliseLink($url)]);
+                       ['nurl' => $nurl]);
                        Logger::info('Set failed status for existing server', ['url' => $url]);
+                       if (self::isDefunct($gserver)) {
+                               self::archiveContacts($gserver['id']);
+                       }
                        return;
                }
-               self::insert(['url' => $url, 'nurl' => Strings::normaliseLink($url),
+
+               self::insert(['url' => $url, 'nurl' => $nurl,
                        'network' => Protocol::PHANTOM, 'created' => DateTimeFormat::utcNow(),
                        'failed' => true, 'last_failure' => DateTimeFormat::utcNow()]);
                Logger::info('Set failed status for new server', ['url' => $url]);
        }
 
+       /**
+        * Archive server related contacts and inboxes
+        *
+        * @param integer $gsid
+        * @return void
+        */
+       private static function archiveContacts(int $gsid)
+       {
+               Contact::update(['archive' => true], ['gsid' => $gsid]);
+               DBA::update('inbox-status', ['archive' => true], ['gsid' => $gsid]);
+       }
+
        /**
         * Remove unwanted content from the given URL
         *
-        * @param string $url
+        * @param string $dirtyUrl
         *
         * @return string cleaned URL
+        * @throws Exception
+        * @deprecated since 2023.03 Use cleanUri instead
+        */
+       public static function cleanURL(string $dirtyUrl): string
+       {
+               try {
+                       return (string)self::cleanUri(new Uri($dirtyUrl));
+               } catch (\Throwable $e) {
+                       Logger::warning('Invalid URL', ['dirtyUrl' => $dirtyUrl]);
+                       return '';
+               }
+       }
+
+       /**
+        * Remove unwanted content from the given URI
+        *
+        * @param UriInterface $dirtyUri
+        *
+        * @return UriInterface cleaned URI
+        * @throws Exception
         */
-       public static function cleanURL(string $url): string
+       public static function cleanUri(UriInterface $dirtyUri): string
        {
-               $url = trim($url, '/');
-               $url = str_replace('/index.php', '', $url);
-
-               $urlparts = parse_url($url);
-               unset($urlparts['user']);
-               unset($urlparts['pass']);
-               unset($urlparts['query']);
-               unset($urlparts['fragment']);
-               return (string)Uri::fromParts($urlparts);
+               return $dirtyUri
+                       ->withUserInfo('')
+                       ->withQuery('')
+                       ->withFragment('')
+                       ->withPath(
+                               preg_replace(
+                                       '#(?:^|/)index\.php#',
+                                       '',
+                                       rtrim($dirtyUri->getPath(), '/')
+                               )
+                       );
        }
 
        /**
@@ -340,7 +544,7 @@ class GServer
         *
         * @return boolean 'true' if server could be detected
         */
-       public static function detect(string $url, string $network = '', bool $only_nodeinfo = false): bool
+       private static function detect(string $url, string $network = '', bool $only_nodeinfo = false): bool
        {
                Logger::info('Detect server type', ['server' => $url]);
 
@@ -355,8 +559,8 @@ class GServer
 
                // If the URL missmatches, then we mark the old entry as failure
                if (!Strings::compareLink($url, $original_url)) {
-                       self::setFailure($original_url);
-                       if (!self::getID($url, true)) {
+                       self::setFailureByUrl($original_url);
+                       if (!self::getID($url, true) && !Network::isUrlBlocked($url)) {
                                self::detect($url, $network, $only_nodeinfo);
                        }
                        return false;
@@ -364,7 +568,7 @@ class GServer
 
                $valid_url = Network::isUrlValid($url);
                if (!$valid_url) {
-                       self::setFailure($url);
+                       self::setFailureByUrl($url);
                        return false;
                } else {
                        $valid_url = rtrim($valid_url, '/');
@@ -376,8 +580,8 @@ class GServer
                        if (((parse_url($url, PHP_URL_HOST) != parse_url($valid_url, PHP_URL_HOST)) && (parse_url($url, PHP_URL_PATH) == parse_url($valid_url, PHP_URL_PATH))) ||
                                (((parse_url($url, PHP_URL_HOST) != parse_url($valid_url, PHP_URL_HOST)) || (parse_url($url, PHP_URL_PATH) != parse_url($valid_url, PHP_URL_PATH))) && empty(parse_url($valid_url, PHP_URL_PATH)))) {
                                Logger::debug('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $valid_url]);
-                               self::setFailure($url);
-                               if (!self::getID($valid_url, true)) {
+                               self::setFailureByUrl($url);
+                               if (!self::getID($valid_url, true) && !Network::isUrlBlocked($valid_url)) {
                                        self::detect($valid_url, $network, $only_nodeinfo);
                                }
                                return false;
@@ -390,8 +594,8 @@ class GServer
                                unset($parts['path']);
                                $valid_url = (string)Uri::fromParts($parts);
 
-                               self::setFailure($url);
-                               if (!self::getID($valid_url, true)) {
+                               self::setFailureByUrl($url);
+                               if (!self::getID($valid_url, true) && !Network::isUrlBlocked($valid_url)) {
                                        self::detect($valid_url, $network, $only_nodeinfo);
                                }
                                return false;
@@ -410,7 +614,7 @@ class GServer
                // When a nodeinfo is present, we don't need to dig further
                $curlResult = DI::httpClient()->get($url . '/.well-known/x-nodeinfo2', HttpClientAccept::JSON);
                if ($curlResult->isTimeout()) {
-                       self::setFailure($url);
+                       self::setFailureByUrl($url);
                        return false;
                }
 
@@ -422,7 +626,7 @@ class GServer
 
                if ($only_nodeinfo && empty($serverdata)) {
                        Logger::info('Invalid nodeinfo in nodeinfo-mode, server is marked as failure', ['url' => $url]);
-                       self::setFailure($url);
+                       self::setFailureByUrl($url);
                        return false;
                } elseif (empty($serverdata)) {
                        $serverdata = ['detection-method' => self::DETECT_MANUAL, 'network' => Protocol::PHANTOM, 'platform' => '', 'version' => '', 'site_name' => '', 'info' => ''];
@@ -461,7 +665,7 @@ class GServer
                                }
 
                                if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
-                                       self::setFailure($url);
+                                       self::setFailureByUrl($url);
                                        return false;
                                }
 
@@ -530,7 +734,7 @@ class GServer
 
                // Most servers aren't installed in a subdirectory, so we declare this entry as failed
                if (($serverdata['network'] == Protocol::PHANTOM) && !empty(parse_url($url, PHP_URL_PATH)) && in_array($serverdata['detection-method'], [self::DETECT_MANUAL])) {
-                       self::setFailure($url);
+                       self::setFailureByUrl($url);
                        return false;
                }
 
@@ -546,6 +750,15 @@ class GServer
                        $serverdata = self::detectNetworkViaContacts($url, $serverdata);
                }
 
+               if (($serverdata['network'] == Protocol::PHANTOM) && in_array($serverdata['detection-method'], [self::DETECT_MANUAL, self::DETECT_BODY])) {
+                       self::setFailureByUrl($url);
+                       return false;
+               }
+
+               if (empty($serverdata['version']) && in_array($serverdata['platform'], ['osada']) && in_array($serverdata['detection-method'], [self::DETECT_CONTACTS, self::DETECT_BODY])) {
+                       $serverdata['version'] = self::getNomadVersion($url);
+               }
+
                // Detect the directory type
                $serverdata['directory-type'] = self::DT_NONE;
 
@@ -576,9 +789,9 @@ class GServer
                }
 
                $serverdata['next_contact'] = self::getNextUpdateDate(true, '', '', in_array($serverdata['network'], [Protocol::PHANTOM, Protocol::FEED]));
-
                $serverdata['last_contact'] = DateTimeFormat::utcNow();
-               $serverdata['failed'] = false;
+               $serverdata['failed']       = false;
+               $serverdata['blocked']      = false;
 
                $gserver = DBA::selectFirst('gserver', ['network'], ['nurl' => Strings::normaliseLink($url)]);
                if (!DBA::isResult($gserver)) {
@@ -851,10 +1064,11 @@ class GServer
                                Logger::info('Invalid nodeinfo format', ['url' => $url]);
                                continue;
                        }
+
                        if ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/1.0') {
-                               $nodeinfo1_url = $link['href'];
+                               $nodeinfo1_url = Network::addBasePath($link['href'], $httpResult->getUrl());
                        } elseif ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/2.0') {
-                               $nodeinfo2_url = $link['href'];
+                               $nodeinfo2_url = Network::addBasePath($link['href'], $httpResult->getUrl());
                        }
                }
 
@@ -1207,9 +1421,9 @@ class GServer
                        $serverdata['detection-method'] = self::DETECT_SITEINFO_JSON;
                }
 
-               if (!empty($data['url'])) {
+               if (!empty($data['platform'])) {
                        $serverdata['platform'] = strtolower($data['platform']);
-                       $serverdata['version'] = $data['version'];
+                       $serverdata['version'] = $data['version'] ?? 'N/A';
                }
 
                if (!empty($data['plugins'])) {
@@ -1289,9 +1503,14 @@ class GServer
                        $serverdata['network'] = Protocol::ACTIVITYPUB;
                        $serverdata['site_name'] = JsonLD::fetchElement($actor, 'as:name', '@value');
                        $serverdata['info'] = JsonLD::fetchElement($actor, 'as:summary', '@value');
-                       if (!empty($actor['as:generator'])) {
+                       if (self::isNomad($actor)) {
+                               $serverdata['platform'] = self::getNomadName($actor['@id']);
+                               $serverdata['version'] = self::getNomadVersion($actor['@id']);
+                               $serverdata['detection-method'] = self::DETECT_SYSTEM_ACTOR;
+                       } elseif (!empty($actor['as:generator'])) {
                                $generator = explode(' ', JsonLD::fetchElement($actor['as:generator'], 'as:name', '@value'));
                                $serverdata['platform'] = strtolower(array_shift($generator));
+                               $serverdata['version'] = self::getNomadVersion($actor['@id']);
                                $serverdata['detection-method'] = self::DETECT_SYSTEM_ACTOR;
                        } else {
                                $serverdata['detection-method'] = self::DETECT_AP_ACTOR;
@@ -1315,6 +1534,69 @@ class GServer
                return ['server' => $serverdata, 'actor' => ''];
        }
 
+       /**
+        * Detect if the given actor is a nomad account
+        *
+        * @param array $actor
+        * @return boolean
+        */
+       private static function isNomad(array $actor): bool
+       {
+               $tags = JsonLD::fetchElementArray($actor, 'as:tag');
+               if (empty($tags)) {
+                       return false;
+               }
+
+               foreach ($tags as $tag) {
+                       if ((($tag['as:name'] ?? '') == 'Protocol') && (($tag['sc:value'] ?? '') == 'nomad')) {
+                               return true;
+                       }
+               }
+               return false;
+       }
+
+       /**
+        * Fetch the name of Nomad implementation
+        *
+        * @param string $url
+        * @return string
+        */
+       private static function getNomadName(string $url): string
+       {
+               $name = 'nomad';
+               $curlResult = DI::httpClient()->get($url . '/manifest', 'application/manifest+json');
+               if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
+                       return $name;
+               }
+
+               $data = json_decode($curlResult->getBody(), true);
+               if (empty($data)) {
+                       return $name;
+               }
+
+               return $data['name'] ?? $name;
+       }
+
+       /**
+        * Fetch the version of the Nomad installation
+        *
+        * @param string $url
+        * @return string
+        */
+       private static function getNomadVersion(string $url): string
+       {
+               $curlResult = DI::httpClient()->get($url . '/api/z/1.0/version', HttpClientAccept::JSON);
+               if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
+                       return '';
+               }
+
+               $data = json_decode($curlResult->getBody(), true);
+               if (empty($data)) {
+                       return '';
+               }
+               return $data ?? '';
+       }
+
        /**
         * Checks if the server contains a valid host meta file
         *
@@ -1325,7 +1607,7 @@ class GServer
        private static function validHostMeta(string $url): bool
        {
                $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
-               $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta', HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
+               $curlResult = DI::httpClient()->get($url . Probe::HOST_META, HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
                if (!$curlResult->isSuccess()) {
                        return false;
                }
@@ -2103,15 +2385,15 @@ class GServer
                $last_update = date('c', time() - (60 * 60 * 24 * $requery_days));
 
                $gservers = DBA::select('gserver', ['id', 'url', 'nurl', 'network', 'poco', 'directory-type'],
-                       ["NOT `failed` AND `directory-type` != ? AND `last_poco_query` < ?", GServer::DT_NONE, $last_update],
+                       ["NOT `blocked` AND NOT `failed` AND `directory-type` != ? AND `last_poco_query` < ?", GServer::DT_NONE, $last_update],
                        ['order' => ['RAND()']]);
 
                while ($gserver = DBA::fetch($gservers)) {
                        Logger::info('Update peer list', ['server' => $gserver['url'], 'id' => $gserver['id']]);
-                       Worker::add(PRIORITY_LOW, 'UpdateServerPeers', $gserver['url']);
+                       Worker::add(Worker::PRIORITY_LOW, 'UpdateServerPeers', $gserver['url']);
 
                        Logger::info('Update directory', ['server' => $gserver['url'], 'id' => $gserver['id']]);
-                       Worker::add(PRIORITY_LOW, 'UpdateServerDirectory', $gserver);
+                       Worker::add(Worker::PRIORITY_LOW, 'UpdateServerDirectory', $gserver);
 
                        $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
                        self::update($fields, ['nurl' => $gserver['nurl']]);
@@ -2129,7 +2411,7 @@ class GServer
         */
        private static function discoverFederation()
        {
-               $last = DI::config()->get('poco', 'last_federation_discovery');
+               $last = DI::keyValue()->get('poco_last_federation_discovery');
 
                if ($last) {
                        $next = $last + (24 * 60 * 60);
@@ -2155,7 +2437,7 @@ class GServer
                        }
                }
 
-               // Disvover Mastodon servers
+               // Discover Mastodon servers
                $accesstoken = DI::config()->get('system', 'instances_social_key');
 
                if (!empty($accesstoken)) {
@@ -2168,12 +2450,12 @@ class GServer
                                        foreach ($servers['instances'] as $server) {
                                                $url = (is_null($server['https_score']) ? 'http' : 'https') . '://' . $server['name'];
                                                self::add($url);
-                                       }       
+                                       }
                                }
                        }
                }
 
-               DI::config()->set('poco', 'last_federation_discovery', time());
+               DI::keyValue()->set('poco_last_federation_discovery', time());
        }
 
        /**