]> 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 7e4b055d10900981cde793fad20b5f0f6c038d10..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;
@@ -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);
                        }
                }
 
@@ -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 '';
+               }
        }
 
        /**
@@ -1209,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'])) {
@@ -1325,7 +1329,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;
                }
@@ -2168,7 +2172,7 @@ class GServer
                                        foreach ($servers['instances'] as $server) {
                                                $url = (is_null($server['https_score']) ? 'http' : 'https') . '://' . $server['name'];
                                                self::add($url);
-                                       }       
+                                       }
                                }
                        }
                }