]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/GServer.php
Don't perform a delivery to failing servers
[friendica.git] / src / Model / GServer.php
index 8305a3c9650666cff2f8b493efbbd1f852c6fb26..f68851af6826b945cdc25f392a7381a5636c7db8 100644 (file)
@@ -70,6 +70,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;
@@ -102,7 +103,7 @@ class GServer
                        return;
                }
 
-               Worker::add(PRIORITY_LOW, 'UpdateGServer', $url, $only_nodeinfo);
+               Worker::add(Worker::PRIORITY_LOW, 'UpdateGServer', $url, $only_nodeinfo);
        }
 
        /**
@@ -115,12 +116,12 @@ 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)]);
@@ -176,9 +177,13 @@ class GServer
        public static function reachable(string $profile, string $server = '', string $network = '', bool $force = false): bool
        {
                if ($server == '') {
-                       $contact = Contact::getByURL($profile, null, ['baseurl']);
+                       $contact = Contact::getByURL($profile, null, ['baseurl', 'network']);
                        if (!empty($contact['baseurl'])) {
                                $server = $contact['baseurl'];
+                       } elseif ($contact['network'] == Protocol::DIASPORA) {
+                               $parts = parse_url($profile);
+                               unset($parts['path']);
+                               $server =  (string)Uri::fromParts($parts);
                        }
                }
 
@@ -304,7 +309,7 @@ class GServer
                        Logger::info('Set failed status for existing server', ['url' => $url]);
                        return;
                }
-               DBA::insert('gserver', ['url' => $url, 'nurl' => Strings::normaliseLink($url),
+               self::insert(['url' => $url, 'nurl' => Strings::normaliseLink($url),
                        'network' => Protocol::PHANTOM, 'created' => DateTimeFormat::utcNow(),
                        'failed' => true, 'last_failure' => DateTimeFormat::utcNow()]);
                Logger::info('Set failed status for new server', ['url' => $url]);
@@ -313,21 +318,20 @@ class GServer
        /**
         * Remove unwanted content from the given URL
         *
-        * @param string $url
+        * @param string $dirtyUrl
         *
         * @return string cleaned URL
+        * @throws Exception
         */
-       public static function cleanURL(string $url): string
+       public static function cleanURL(string $dirtyUrl): 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);
+               try {
+                       $url = str_replace('/index.php', '', trim($dirtyUrl, '/'));
+                       return (string)(new Uri($url))->withUserInfo('')->withQuery('')->withFragment('');
+               } catch (\Throwable $e) {
+                       Logger::warning('Invalid URL', ['dirtyUrl' => $dirtyUrl, 'url' => $url]);
+                       return '';
+               }
        }
 
        /**
@@ -563,8 +567,13 @@ class GServer
 
                $serverdata['registered-users'] = $serverdata['registered-users'] ?? 0;
 
+               // Numbers above a reasonable value (10 millions) are ignored
+               if ($serverdata['registered-users'] > 10000000) {
+                       $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)) {
+               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;
@@ -578,7 +587,7 @@ class GServer
                $gserver = DBA::selectFirst('gserver', ['network'], ['nurl' => Strings::normaliseLink($url)]);
                if (!DBA::isResult($gserver)) {
                        $serverdata['created'] = DateTimeFormat::utcNow();
-                       $ret = DBA::insert('gserver', $serverdata);
+                       $ret = self::insert($serverdata);
                        $id = DBA::lastInsertId();
                } else {
                        $ret = self::update($serverdata, ['nurl' => $serverdata['nurl']]);
@@ -1204,7 +1213,7 @@ class GServer
 
                if (!empty($data['url'])) {
                        $serverdata['platform'] = strtolower($data['platform']);
-                       $serverdata['version'] = $data['version'];
+                       $serverdata['version'] = $data['version'] ?? 'N/A';
                }
 
                if (!empty($data['plugins'])) {
@@ -1320,12 +1329,12 @@ 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;
                }
 
-               $xrd = XML::parseString($curlResult->getBody());
+               $xrd = XML::parseString($curlResult->getBody(), true);
                if (!is_object($xrd)) {
                        return false;
                }
@@ -2103,10 +2112,10 @@ class GServer
 
                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']]);
@@ -2159,9 +2168,11 @@ class GServer
                        if ($curlResult->isSuccess()) {
                                $servers = json_decode($curlResult->getBody(), true);
 
-                               foreach ($servers['instances'] as $server) {
-                                       $url = (is_null($server['https_score']) ? 'http' : 'https') . '://' . $server['name'];
-                                       self::add($url);
+                               if (!empty($servers['instances'])) {
+                                       foreach ($servers['instances'] as $server) {
+                                               $url = (is_null($server['https_score']) ? 'http' : 'https') . '://' . $server['name'];
+                                               self::add($url);
+                                       }
                                }
                        }
                }
@@ -2254,6 +2265,7 @@ class GServer
        }
 
        /**
+        * Update rows in the gserver table.
         * Enforces gserver table field maximum sizes to avoid "Data too long" database errors
         *
         * @param array $fields
@@ -2269,4 +2281,22 @@ class GServer
 
                return DBA::update('gserver', $fields, $condition);
        }
+
+       /**
+        * Insert a row into the gserver table.
+        * Enforces gserver table field maximum sizes to avoid "Data too long" database errors
+        *
+        * @param array $fields
+        * @param int   $duplicate_mode What to do on a duplicated entry
+        *
+        * @return bool
+        *
+        * @throws Exception
+        */
+       public static function insert(array $fields, int $duplicate_mode = Database::INSERT_DEFAULT): bool
+       {
+               $fields = DI::dbaDefinition()->truncateFieldsForTable('gserver', $fields);
+
+               return DBA::insert('gserver', $fields, $duplicate_mode);
+       }
 }