]> git.mxchange.org Git - friendica.git/commitdiff
We now support querying nodeinfo 2.0
authorMichael <heluecht@pirati.ca>
Fri, 2 Feb 2018 13:53:40 +0000 (13:53 +0000)
committerMichael <heluecht@pirati.ca>
Fri, 2 Feb 2018 13:53:40 +0000 (13:53 +0000)
src/Network/Probe.php
src/Protocol/PortableContact.php

index f7c3b84fdd46803c35eb334a505366189595004f..dc533d18de7f464480083f7baed14271bfb28f27 100644 (file)
@@ -90,6 +90,9 @@ class Probe
        /**
         * @brief Probes for webfinger path via "host-meta"
         *
+        * We have to check if the servers in the future still will offer this.
+        * It seems as if it was dropped from the standard.
+        *
         * @param string $host The host part of an url
         *
         * @return array with template and type of the webfinger template for JSON or XML
index fb9a725f082b7008de6565b06acf5d317dcbe66d..e70e67366cf8333beae39f09a6efe522dcb50351 100644 (file)
@@ -646,30 +646,51 @@ class PortableContact
                        return false;
                }
 
-               $nodeinfo_url = '';
+               $nodeinfo1_url = '';
+               $nodeinfo2_url = '';
 
                foreach ($nodeinfo->links as $link) {
                        if ($link->rel == 'http://nodeinfo.diaspora.software/ns/schema/1.0') {
-                               $nodeinfo_url = $link->href;
+                               $nodeinfo1_url = $link->href;
+                       }
+                       if ($link->rel == 'http://nodeinfo.diaspora.software/ns/schema/2.0') {
+                               $nodeinfo2_url = $link->href;
                        }
                }
 
-               if ($nodeinfo_url == '') {
+               if ($nodeinfo1_url . $nodeinfo2_url == '') {
                        return false;
                }
 
+               $server = [];
+
                // When the nodeinfo url isn't on the same host, then there is obviously something wrong
-               if (parse_url($server_url, PHP_URL_HOST) != parse_url($nodeinfo_url, PHP_URL_HOST)) {
-                       return false;
+               if (!empty($nodeinfo2_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo2_url, PHP_URL_HOST))) {
+                       $server = self::parseNodeinfo2($nodeinfo2_url);
+               }
+
+               // When the nodeinfo url isn't on the same host, then there is obviously something wrong
+               if (empty($server) && !empty($nodeinfo1_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo1_url, PHP_URL_HOST))) {
+                       $server = self::parseNodeinfo1($nodeinfo1_url);
                }
 
+               return $server;
+       }
+
+       /**
+        * @brief Parses Nodeinfo 1
+        *
+        * @param string $nodeinfo_url address of the nodeinfo path
+        * @return array Server data
+        */
+       private static function parseNodeinfo1($nodeinfo_url)
+       {
                $serverret = Network::curl($nodeinfo_url);
                if (!$serverret["success"]) {
                        return false;
                }
 
                $nodeinfo = json_decode($serverret['body']);
-
                if (!is_object($nodeinfo)) {
                        return false;
                }
@@ -740,6 +761,90 @@ class PortableContact
                return $server;
        }
 
+       /**
+        * @brief Parses Nodeinfo 2
+        *
+        * @param string $nodeinfo_url address of the nodeinfo path
+        * @return array Server data
+        */
+       private static function parseNodeinfo2($nodeinfo_url)
+       {
+               $serverret = Network::curl($nodeinfo_url);
+               if (!$serverret["success"]) {
+                       return false;
+               }
+
+               $nodeinfo = json_decode($serverret['body']);
+               if (!is_object($nodeinfo)) {
+                       return false;
+               }
+
+               $server = [];
+
+               $server['register_policy'] = REGISTER_CLOSED;
+
+               if (is_bool($nodeinfo->openRegistrations) && $nodeinfo->openRegistrations) {
+                       $server['register_policy'] = REGISTER_OPEN;
+               }
+
+               if (is_object($nodeinfo->software)) {
+                       if (isset($nodeinfo->software->name)) {
+                               $server['platform'] = $nodeinfo->software->name;
+                       }
+
+                       if (isset($nodeinfo->software->version)) {
+                               $server['version'] = $nodeinfo->software->version;
+                               // 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']);
+                       }
+               }
+
+               if (is_object($nodeinfo->metadata)) {
+                       if (isset($nodeinfo->metadata->nodeName)) {
+                               $server['site_name'] = $nodeinfo->metadata->nodeName;
+                       }
+               }
+
+               if (!empty($nodeinfo->usage->users->total)) {
+                       $server['registered-users'] = $nodeinfo->usage->users->total;
+               }
+
+               $diaspora = false;
+               $friendica = false;
+               $gnusocial = false;
+
+               if (is_array($nodeinfo->protocols)) {
+                       foreach ($nodeinfo->protocols as $protocol) {
+                               if ($protocol == 'diaspora') {
+                                       $diaspora = true;
+                               }
+                               if ($protocol == 'friendica') {
+                                       $friendica = true;
+                               }
+                               if ($protocol == 'gnusocial') {
+                                       $gnusocial = true;
+                               }
+                       }
+               }
+
+               if ($gnusocial) {
+                       $server['network'] = NETWORK_OSTATUS;
+               }
+               if ($diaspora) {
+                       $server['network'] = NETWORK_DIASPORA;
+               }
+               if ($friendica) {
+                       $server['network'] = NETWORK_DFRN;
+               }
+
+               if (!$server) {
+                       return false;
+               }
+
+               return $server;
+       }
+
        /**
         * @brief Detect server type (Hubzilla or Friendica) via the front page body
         *