]> git.mxchange.org Git - friendica.git/commitdiff
Hostmeta check, Pleroma version number cleaning
authorMichael <heluecht@pirati.ca>
Thu, 3 Oct 2019 20:52:04 +0000 (20:52 +0000)
committerMichael <heluecht@pirati.ca>
Thu, 3 Oct 2019 20:52:04 +0000 (20:52 +0000)
src/Model/GServer.php

index 11dd9b5257e9a695fbba936eb8e2ae692cbfc996..9a224aecd80a878c3866be81c21f44cae929ea37 100644 (file)
@@ -54,7 +54,7 @@ class GServer
                }
 
                // If that didn't work out well, we use some protocol specific endpoints
-               if (empty($nodeinfo) || ($nodeinfo['network'] == Protocol::DFRN)) {
+               if (empty($nodeinfo) || empty($nodeinfo['network']) || ($nodeinfo['network'] == Protocol::DFRN)) {
                        // Fetch the landing page, possibly it reveals some data
                        $curlResult = Network::curl($url, false, ['timeout' => $xrd_timeout]);
                        if ($curlResult->isSuccess()) {
@@ -103,6 +103,14 @@ class GServer
                        $serverdata['network'] = Protocol::PHANTOM;
                }
 
+               // Check host-meta for phantom networks.
+               // Although this is not needed, it is a good indicator for a living system,
+               // since most systems had implemented it.
+               if (($serverdata['network'] == Protocol::PHANTOM) && !self::validHostMeta($url)) {
+                       DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($url)]);
+                       return false;
+               }
+
                $serverdata['url'] = $url;
                $serverdata['nurl'] = Strings::normaliseLink($url);
 
@@ -443,6 +451,39 @@ class GServer
                return $serverdata;
        }
 
+       private static function validHostMeta($url)
+       {
+               $xrd_timeout = Config::get('system', 'xrd_timeout');
+               $curlResult = Network::curl($url . '/.well-known/host-meta', false, ['timeout' => $xrd_timeout]);
+               if (!$curlResult->isSuccess()) {
+                       return false;
+               }
+
+               $xrd = XML::parseString($curlResult->getBody(), false);
+               if (!is_object($xrd)) {
+                       return false;
+               }
+
+               $elements = XML::elementToArray($xrd);
+               if (empty($elements) || empty($elements['xrd']) || empty($elements['xrd']['link'])) {
+                       return false;
+               }
+
+               $valid = false;
+               foreach ($elements['xrd']['link'] as $link) {
+                       if (empty($link['rel']) || empty($link['type']) || empty($link['template'])) {
+                               continue;
+                       }
+
+                       if ($link['type'] == 'application/xrd+xml') {
+                               // When the webfinger host is the same like the system host, it should be ok.
+                               $valid = (parse_url($url, PHP_URL_HOST) == parse_url($link['template'], PHP_URL_HOST));
+                       }
+               }
+
+               return $valid;
+       }
+
        private static function checkPoCo($url, $serverdata)
        {
                $curlResult = Network::curl($url. '/poco');
@@ -519,9 +560,15 @@ class GServer
                        $serverdata['registered-users'] = $data['stats']['user_count'];
                }
 
+               if (!empty($serverdata['version']) && preg_match('/.*?\(compatible;\s(.*)\s(.*)\)/ism', $serverdata['version'], $matches)) {
+                       $serverdata['platform'] = $matches[1];
+                       $serverdata['version'] = $matches[2];
+print_r($serverdata);
+               }
+
                if (!empty($serverdata['version']) && strstr($serverdata['version'], 'Pleroma')) {
                        $serverdata['platform'] = 'pleroma';
-                       $serverdata['version'] = trim(str_replace('Pleroma', '', $serverdata['version'])); // 2.7.2 (compatible; Pleroma 1.0.0-1225-gf31ad554-develop)
+                       $serverdata['version'] = trim(str_replace('Pleroma', '', $serverdata['version']));
                }
 
                return $serverdata;