]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/PortableContact.php
Added server discovery for their relais configuration
[friendica.git] / src / Protocol / PortableContact.php
index feb4dfdc8dbe9aee53b9b6f1d1de20c1ef6d1b76..c7f58b9fb2adb67c7033100b06cf4ee2b309e01c 100644 (file)
@@ -9,21 +9,21 @@
 
 namespace Friendica\Protocol;
 
+use Friendica\Content\Text\HTML;
 use Friendica\Core\Config;
 use Friendica\Core\Worker;
 use Friendica\Database\DBM;
-use Friendica\Model\GlobalContact;
+use Friendica\Model\GContact;
+use Friendica\Model\Profile;
 use Friendica\Network\Probe;
-use Friendica\Object\Photo;
-use Friendica\Object\Profile;
+use Friendica\Util\DateTimeFormat;
+use Friendica\Util\Network;
 use dba;
 use DOMDocument;
-use DomXPath;
+use DOMXPath;
 use Exception;
 
-require_once 'include/datetime.php';
-require_once 'include/network.php';
-require_once 'include/html2bbcode.php';
+require_once 'include/dba.php';
 
 class PortableContact
 {
@@ -65,22 +65,19 @@ class PortableContact
                $a = get_app();
 
                if ($cid) {
-                       if ((! $url) || (! $uid)) {
-                               $r = q(
-                                       "select `poco`, `uid` from `contact` where `id` = %d limit 1",
-                                       intval($cid)
-                               );
-                               if (DBM::is_result($r)) {
-                                       $url = $r[0]['poco'];
-                                       $uid = $r[0]['uid'];
+                       if (!$url || !$uid) {
+                               $contact = dba::selectFirst('contact', ['poco', 'uid'], ['id' => $cid]);
+                               if (DBM::is_result($contact)) {
+                                       $url = $contact['poco'];
+                                       $uid = $contact['uid'];
                                }
                        }
-                       if (! $uid) {
+                       if (!$uid) {
                                return;
                        }
                }
 
-               if (! $url) {
+               if (!$url) {
                        return;
                }
 
@@ -88,7 +85,7 @@ class PortableContact
 
                logger('load: ' . $url, LOGGER_DEBUG);
 
-               $s = fetch_url($url);
+               $s = Network::fetchUrl($url);
 
                logger('load: returns ' . $s, LOGGER_DATA);
 
@@ -146,7 +143,7 @@ class PortableContact
                        }
 
                        if (isset($entry->updated)) {
-                               $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
+                               $updated = date(DateTimeFormat::MYSQL, strtotime($entry->updated));
                        }
 
                        if (isset($entry->network)) {
@@ -158,7 +155,7 @@ class PortableContact
                        }
 
                        if (isset($entry->aboutMe)) {
-                               $about = html2bbcode($entry->aboutMe);
+                               $about = HTML::toBBCode($entry->aboutMe);
                        }
 
                        if (isset($entry->gender)) {
@@ -179,7 +176,7 @@ class PortableContact
                                $contact_type = $entry->contactType;
                        }
 
-                       $gcontact = array("url" => $profile_url,
+                       $gcontact = ["url" => $profile_url,
                                        "name" => $name,
                                        "network" => $network,
                                        "photo" => $profile_photo,
@@ -190,25 +187,21 @@ class PortableContact
                                        "connect" => $connect_url,
                                        "updated" => $updated,
                                        "contact-type" => $contact_type,
-                                       "generation" => $generation);
+                                       "generation" => $generation];
 
                        try {
-                               $gcontact = GlobalContact::sanitize($gcontact);
-                               $gcid = GlobalContact::update($gcontact);
+                               $gcontact = GContact::sanitize($gcontact);
+                               $gcid = GContact::update($gcontact);
 
-                               GlobalContact::link($gcid, $uid, $cid, $zcid);
+                               GContact::link($gcid, $uid, $cid, $zcid);
                        } catch (Exception $e) {
                                logger($e->getMessage(), LOGGER_DEBUG);
                        }
                }
                logger("load: loaded $total entries", LOGGER_DEBUG);
 
-               q(
-                       "DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
-                       intval($cid),
-                       intval($uid),
-                       intval($zcid)
-               );
+               $condition = ["`cid` = ? AND `uid` = ? AND `zcid` = ? AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY", $cid, $uid, $zcid];
+               dba::delete('glink', $condition);
        }
 
        public static function reachable($profile, $server = "", $network = "", $force = false)
@@ -294,7 +287,7 @@ class PortableContact
                }
 
                // Fetch the host-meta to check if this really is a server
-               $serverret = z_fetch_url($server_url."/.well-known/host-meta");
+               $serverret = Network::curl($server_url."/.well-known/host-meta");
                if (!$serverret["success"]) {
                        return "";
                }
@@ -318,12 +311,13 @@ class PortableContact
                        return false;
                }
 
-               $contact = array("url" => $profile);
+               $contact = ["url" => $profile];
 
                if ($gcontacts[0]["created"] <= NULL_DATE) {
-                       $contact['created'] = datetime_convert();
+                       $contact['created'] = DateTimeFormat::utcNow();
                }
 
+               $server_url = '';
                if ($force) {
                        $server_url = normalise_link(self::detectServer($profile));
                }
@@ -336,7 +330,7 @@ class PortableContact
                        $server_url = normalise_link(self::detectServer($profile));
                }
 
-               if (!in_array($gcontacts[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_FEED, NETWORK_OSTATUS, ""))) {
+               if (!in_array($gcontacts[0]["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_FEED, NETWORK_OSTATUS, ""])) {
                        logger("Profile ".$profile.": Network type ".$gcontacts[0]["network"]." can't be checked", LOGGER_DEBUG);
                        return false;
                }
@@ -344,11 +338,8 @@ class PortableContact
                if ($server_url != "") {
                        if (!self::checkServer($server_url, $gcontacts[0]["network"], $force)) {
                                if ($force) {
-                                       q(
-                                               "UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
-                                               dbesc(datetime_convert()),
-                                               dbesc(normalise_link($profile))
-                                       );
+                                       $fields = ['last_failure' => DateTimeFormat::utcNow()];
+                                       dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
                                }
 
                                logger("Profile ".$profile.": Server ".$server_url." wasn't reachable.", LOGGER_DEBUG);
@@ -357,7 +348,7 @@ class PortableContact
                        $contact['server_url'] = $server_url;
                }
 
-               if (in_array($gcontacts[0]["network"], array("", NETWORK_FEED))) {
+               if (in_array($gcontacts[0]["network"], ["", NETWORK_FEED])) {
                        $server = q(
                                "SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''",
                                dbesc(normalise_link($server_url))
@@ -376,7 +367,7 @@ class PortableContact
                        $server = q("SELECT `noscrape`, `network` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", dbesc(normalise_link($server_url)));
 
                        if ($server) {
-                               $noscraperet = z_fetch_url($server[0]["noscrape"]."/".$gcontacts[0]["nick"]);
+                               $noscraperet = Network::curl($server[0]["noscrape"]."/".$gcontacts[0]["nick"]);
 
                                if ($noscraperet["success"] && ($noscraperet["body"] != "")) {
                                        $noscrape = json_decode($noscraperet["body"], true);
@@ -421,18 +412,15 @@ class PortableContact
 
                                                // Set the date of the last contact
                                                /// @todo By now the function "update_gcontact" doesn't work with this field
-                                               //$contact["last_contact"] = datetime_convert();
+                                               //$contact["last_contact"] = DateTimeFormat::utcNow();
 
                                                $contact = array_merge($contact, $noscrape);
 
-                                               GlobalContact::update($contact);
+                                               GContact::update($contact);
 
                                                if (trim($noscrape["updated"]) != "") {
-                                                       q(
-                                                               "UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'",
-                                                               dbesc(datetime_convert()),
-                                                               dbesc(normalise_link($profile))
-                                                       );
+                                                       $fields = ['last_contact' => DateTimeFormat::utcNow()];
+                                                       dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
 
                                                        logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG);
 
@@ -447,7 +435,7 @@ class PortableContact
                if (!$force && !self::updateNeeded($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) {
                        logger("Profile ".$profile." was last updated at ".$gcontacts[0]["updated"]." (cached)", LOGGER_DEBUG);
 
-                       GlobalContact::update($contact);
+                       GContact::update($contact);
                        return $gcontacts[0]["updated"];
                }
 
@@ -460,16 +448,15 @@ class PortableContact
                        && (normalise_link($profile) != normalise_link($data["url"]))
                ) {
                        // Delete the old entry
-                       q("DELETE FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($profile)));
-                       q("DELETE FROM `glink` WHERE `gcid` = %d", intval($gcontacts[0]["id"]));
+                       dba::delete('gcontact', ['nurl' => normalise_link($profile)]);
 
                        $gcontact = array_merge($gcontacts[0], $data);
 
                        $gcontact["server_url"] = $data["baseurl"];
 
                        try {
-                               $gcontact = GlobalContact::sanitize($gcontact);
-                               GlobalContact::update($gcontact);
+                               $gcontact = GContact::sanitize($gcontact);
+                               GContact::update($gcontact);
 
                                self::lastUpdated($data["url"], $force);
                        } catch (Exception $e) {
@@ -480,12 +467,9 @@ class PortableContact
                        return false;
                }
 
-               if (($data["poll"] == "") || (in_array($data["network"], array(NETWORK_FEED, NETWORK_PHANTOM)))) {
-                       q(
-                               "UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
-                               dbesc(datetime_convert()),
-                               dbesc(normalise_link($profile))
-                       );
+               if (($data["poll"] == "") || (in_array($data["network"], [NETWORK_FEED, NETWORK_PHANTOM]))) {
+                       $fields = ['last_failure' => DateTimeFormat::utcNow()];
+                       dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
 
                        logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG);
                        return false;
@@ -495,16 +479,13 @@ class PortableContact
 
                $contact["server_url"] = $data["baseurl"];
 
-               GlobalContact::update($contact);
+               GContact::update($contact);
 
-               $feedret = z_fetch_url($data["poll"]);
+               $feedret = Network::curl($data["poll"]);
 
                if (!$feedret["success"]) {
-                       q(
-                               "UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
-                               dbesc(datetime_convert()),
-                               dbesc(normalise_link($profile))
-                       );
+                       $fields = ['last_failure' => DateTimeFormat::utcNow()];
+                       dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
 
                        logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG);
                        return false;
@@ -513,7 +494,7 @@ class PortableContact
                $doc = new DOMDocument();
                @$doc->loadXML($feedret["body"]);
 
-               $xpath = new DomXPath($doc);
+               $xpath = new DOMXPath($doc);
                $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
 
                $entries = $xpath->query('/atom:feed/atom:entry');
@@ -521,14 +502,16 @@ class PortableContact
                $last_updated = "";
 
                foreach ($entries as $entry) {
-                       $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
-                       $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
+                       $published = DateTimeFormat::utc($xpath->query('atom:published/text()', $entry)->item(0)->nodeValue);
+                       $updated   = DateTimeFormat::utc($xpath->query('atom:updated/text()'  , $entry)->item(0)->nodeValue);
 
-                       if ($last_updated < $published)
+                       if ($last_updated < $published) {
                                $last_updated = $published;
+                       }
 
-                       if ($last_updated < $updated)
+                       if ($last_updated < $updated) {
                                $last_updated = $updated;
+                       }
                }
 
                // Maybe there aren't any entries. Then check if it is a valid feed
@@ -537,28 +520,23 @@ class PortableContact
                                $last_updated = NULL_DATE;
                        }
                }
-               q(
-                       "UPDATE `gcontact` SET `updated` = '%s', `last_contact` = '%s' WHERE `nurl` = '%s'",
-                       dbesc(DBM::date($last_updated)),
-                       dbesc(DBM::date()),
-                       dbesc(normalise_link($profile))
-               );
+
+               $fields = ['updated' => $last_updated, 'last_contact' => DateTimeFormat::utcNow()];
+               dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
 
                if (($gcontacts[0]["generation"] == 0)) {
-                       q(
-                               "UPDATE `gcontact` SET `generation` = 9 WHERE `nurl` = '%s'",
-                               dbesc(normalise_link($profile))
-                       );
+                       $fields = ['generation' => 9];
+                       dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
                }
 
                logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG);
 
-               return($last_updated);
+               return $last_updated;
        }
 
        public static function updateNeeded($created, $updated, $last_failure, $last_contact)
        {
-               $now = strtotime(datetime_convert());
+               $now = strtotime(DateTimeFormat::utcNow());
 
                if ($updated > $last_contact) {
                        $contact_time = strtotime($updated);
@@ -601,7 +579,7 @@ class PortableContact
                return true;
        }
 
-       public static function toBoolean($val)
+       private static function toBoolean($val)
        {
                if (($val == "true") || ($val == 1)) {
                        return true;
@@ -618,7 +596,7 @@ class PortableContact
         * @param object $data POCO data
         * @return array Server data
         */
-       public static function detectPocoData($data)
+       private static function detectPocoData($data)
        {
                $server = false;
 
@@ -640,7 +618,7 @@ class PortableContact
 
                foreach ($data->entry[0]->urls as $url) {
                        if ($url->type == 'zot') {
-                               $server = array();
+                               $server = [];
                                $server["platform"] = 'Hubzilla';
                                $server["network"] = NETWORK_DIASPORA;
                                return $server;
@@ -655,9 +633,9 @@ class PortableContact
         * @param string $server_url address of the server
         * @return array Server data
         */
-       public static function fetchNodeinfo($server_url)
+       private static function fetchNodeinfo($server_url)
        {
-               $serverret = z_fetch_url($server_url."/.well-known/nodeinfo");
+               $serverret = Network::curl($server_url."/.well-known/nodeinfo");
                if (!$serverret["success"]) {
                        return false;
                }
@@ -672,30 +650,56 @@ 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;
                }
 
-               $serverret = z_fetch_url($nodeinfo_url);
+               $server = [];
+
+               // When the nodeinfo url isn't on the same host, then there is obviously something wrong
+               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;
                }
 
-               $server = array();
+               $server = [];
 
                $server['register_policy'] = REGISTER_CLOSED;
 
@@ -722,6 +726,10 @@ class PortableContact
                        }
                }
 
+               if (!empty($nodeinfo->usage->users->total)) {
+                       $server['registered-users'] = $nodeinfo->usage->users->total;
+               }
+
                $diaspora = false;
                $friendica = false;
                $gnusocial = false;
@@ -757,24 +765,108 @@ 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
         *
         * @param string $body Front page of the server
         * @return array Server data
         */
-       public static function detectServerType($body)
+       private static function detectServerType($body)
        {
                $server = false;
 
                $doc = new DOMDocument();
                @$doc->loadHTML($body);
-               $xpath = new DomXPath($doc);
+               $xpath = new DOMXPath($doc);
 
                $list = $xpath->query("//meta[@name]");
 
                foreach ($list as $node) {
-                       $attr = array();
+                       $attr = [];
                        if ($node->attributes->length) {
                                foreach ($node->attributes as $attribute) {
                                        $attr[$attribute->name] = $attribute->value;
@@ -783,8 +875,8 @@ class PortableContact
                        if ($attr['name'] == 'generator') {
                                $version_part = explode(" ", $attr['content']);
                                if (count($version_part) == 2) {
-                                       if (in_array($version_part[0], array("Friendika", "Friendica"))) {
-                                               $server = array();
+                                       if (in_array($version_part[0], ["Friendika", "Friendica"])) {
+                                               $server = [];
                                                $server["platform"] = $version_part[0];
                                                $server["version"] = $version_part[1];
                                                $server["network"] = NETWORK_DFRN;
@@ -797,14 +889,14 @@ class PortableContact
                        $list = $xpath->query("//meta[@property]");
 
                        foreach ($list as $node) {
-                               $attr = array();
+                               $attr = [];
                                if ($node->attributes->length) {
                                        foreach ($node->attributes as $attribute) {
                                                $attr[$attribute->name] = $attribute->value;
                                        }
                                }
-                               if ($attr['property'] == 'generator' && in_array($attr['content'], array("hubzilla", "BlaBlaNet"))) {
-                                       $server = array();
+                               if ($attr['property'] == 'generator' && in_array($attr['content'], ["hubzilla", "BlaBlaNet"])) {
+                                       $server = [];
                                        $server["platform"] = $attr['content'];
                                        $server["version"] = "";
                                        $server["network"] = NETWORK_DIASPORA;
@@ -816,7 +908,7 @@ class PortableContact
                        return false;
                }
 
-               $server["site_name"] = $xpath->evaluate($element."//head/title/text()", $context)->item(0)->nodeValue;
+               $server["site_name"] = $xpath->evaluate("//head/title/text()")->item(0)->nodeValue;
                return $server;
        }
 
@@ -830,31 +922,39 @@ class PortableContact
                        return false;
                }
 
-               $servers = q("SELECT * FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
-               if (DBM::is_result($servers)) {
-                       if ($servers[0]["created"] <= NULL_DATE) {
-                               q(
-                                       "UPDATE `gserver` SET `created` = '%s' WHERE `nurl` = '%s'",
-                                       dbesc(datetime_convert()),
-                                       dbesc(normalise_link($server_url))
-                               );
+               $gserver = dba::selectFirst('gserver', [], ['nurl' => normalise_link($server_url)]);
+               if (DBM::is_result($gserver)) {
+                       if ($gserver["created"] <= NULL_DATE) {
+                               $fields = ['created' => DateTimeFormat::utcNow()];
+                               $condition = ['nurl' => normalise_link($server_url)];
+                               dba::update('gserver', $fields, $condition);
                        }
-                       $poco = $servers[0]["poco"];
-                       $noscrape = $servers[0]["noscrape"];
+                       $poco = $gserver["poco"];
+                       $noscrape = $gserver["noscrape"];
 
                        if ($network == "") {
-                               $network = $servers[0]["network"];
+                               $network = $gserver["network"];
                        }
 
-                       $last_contact = $servers[0]["last_contact"];
-                       $last_failure = $servers[0]["last_failure"];
-                       $version = $servers[0]["version"];
-                       $platform = $servers[0]["platform"];
-                       $site_name = $servers[0]["site_name"];
-                       $info = $servers[0]["info"];
-                       $register_policy = $servers[0]["register_policy"];
+                       $last_contact = $gserver["last_contact"];
+                       $last_failure = $gserver["last_failure"];
+                       $version = $gserver["version"];
+                       $platform = $gserver["platform"];
+                       $site_name = $gserver["site_name"];
+                       $info = $gserver["info"];
+                       $register_policy = $gserver["register_policy"];
+                       $registered_users = $gserver["registered-users"];
 
-                       if (!$force && !self::updateNeeded($servers[0]["created"], "", $last_failure, $last_contact)) {
+                       // See discussion under https://forum.friendi.ca/display/0b6b25a8135aabc37a5a0f5684081633
+                       // It can happen that a zero date is in the database, but storing it again is forbidden.
+                       if ($last_contact < NULL_DATE) {
+                               $last_contact = NULL_DATE;
+                       }
+                       if ($last_failure < NULL_DATE) {
+                               $last_failure = NULL_DATE;
+                       }
+
+                       if (!$force && !self::updateNeeded($gserver["created"], "", $last_failure, $last_contact)) {
                                logger("Use cached data for server ".$server_url, LOGGER_DEBUG);
                                return ($last_contact >= $last_failure);
                        }
@@ -866,30 +966,38 @@ class PortableContact
                        $site_name = "";
                        $info = "";
                        $register_policy = -1;
+                       $registered_users = 0;
 
                        $last_contact = NULL_DATE;
                        $last_failure = NULL_DATE;
                }
-               logger("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$servers[0]["created"]." Failure: ".$last_failure." Contact: ".$last_contact, LOGGER_DEBUG);
+               logger("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$gserver["created"]." Failure: ".$last_failure." Contact: ".$last_contact, LOGGER_DEBUG);
 
                $failure = false;
                $possible_failure = false;
                $orig_last_failure = $last_failure;
                $orig_last_contact = $last_contact;
 
+               // Mastodon uses the "@" for user profiles.
+               // But this can be misunderstood.
+               if (parse_url($server_url, PHP_URL_USER) != '') {
+                       dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
+                       return false;
+               }
+
                // Check if the page is accessible via SSL.
                $orig_server_url = $server_url;
                $server_url = str_replace("http://", "https://", $server_url);
 
                // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
-               $serverret = z_fetch_url($server_url."/.well-known/host-meta", false, $redirects, array('timeout' => 20));
+               $serverret = Network::curl($server_url."/.well-known/host-meta", false, $redirects, ['timeout' => 20]);
 
                // Quit if there is a timeout.
                // But we want to make sure to only quit if we are mostly sure that this server url fits.
-               if (DBM::is_result($servers) && ($orig_server_url == $server_url) &&
+               if (DBM::is_result($gserver) && ($orig_server_url == $server_url) &&
                        ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT)) {
                        logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
-                       dba::update('gserver', array('last_failure' => datetime_convert()), array('nurl' => normalise_link($server_url)));
+                       dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
                        return false;
                }
 
@@ -899,12 +1007,12 @@ class PortableContact
                        $server_url = str_replace("https://", "http://", $server_url);
 
                        // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
-                       $serverret = z_fetch_url($server_url."/.well-known/host-meta", false, $redirects, array('timeout' => 20));
+                       $serverret = Network::curl($server_url."/.well-known/host-meta", false, $redirects, ['timeout' => 20]);
 
                        // Quit if there is a timeout
                        if ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT) {
                                logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
-                               dba::update('gserver', array('last_failure' => datetime_convert()), array('nurl' => normalise_link($server_url)));
+                               dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
                                return false;
                        }
 
@@ -913,7 +1021,7 @@ class PortableContact
 
                if (!$serverret["success"] || ($serverret["body"] == "") || (sizeof($xmlobj) == 0) || !is_object($xmlobj)) {
                        // Workaround for bad configured servers (known nginx problem)
-                       if (!in_array($serverret["debug"]["http_code"], array("403", "404"))) {
+                       if (!in_array($serverret["debug"]["http_code"], ["403", "404"])) {
                                $failure = true;
                        }
                        $possible_failure = true;
@@ -928,12 +1036,18 @@ class PortableContact
                        $register_policy = -1;
                }
 
+               if (!$failure) {
+                       // This will be too low, but better than no value at all.
+                       $registered_users = dba::count('gcontact', ['server_url' => normalise_link($server_url)]);
+               }
+
                // Look for poco
                if (!$failure) {
-                       $serverret = z_fetch_url($server_url."/poco");
+                       $serverret = Network::curl($server_url."/poco");
                        if ($serverret["success"]) {
                                $data = json_decode($serverret["body"]);
                                if (isset($data->totalResults)) {
+                                       $registered_users = $data->totalResults;
                                        $poco = $server_url."/poco";
                                        $server = self::detectPocoData($data);
                                        if ($server) {
@@ -943,12 +1057,19 @@ class PortableContact
                                                $site_name = '';
                                        }
                                }
+                               // There are servers out there who don't return 404 on a failure
+                               // We have to be sure that don't misunderstand this
+                               if (is_null($data)) {
+                                       $poco = "";
+                                       $noscrape = "";
+                                       $network = "";
+                               }
                        }
                }
 
                if (!$failure) {
                        // Test for Diaspora, Hubzilla, Mastodon or older Friendica servers
-                       $serverret = z_fetch_url($server_url);
+                       $serverret = Network::curl($server_url);
 
                        if (!$serverret["success"] || ($serverret["body"] == "")) {
                                $failure = true;
@@ -987,7 +1108,7 @@ class PortableContact
                        // Test for Statusnet
                        // Will also return data for Friendica and GNU Social - but it will be overwritten later
                        // The "not implemented" is a special treatment for really, really old Friendica versions
-                       $serverret = z_fetch_url($server_url."/api/statusnet/version.json");
+                       $serverret = Network::curl($server_url."/api/statusnet/version.json");
                        if ($serverret["success"] && ($serverret["body"] != '{"error":"not implemented"}') &&
                                ($serverret["body"] != '') && (strlen($serverret["body"]) < 30)) {
                                $platform = "StatusNet";
@@ -998,7 +1119,7 @@ class PortableContact
                        }
 
                        // Test for GNU Social
-                       $serverret = z_fetch_url($server_url."/api/gnusocial/version.json");
+                       $serverret = Network::curl($server_url."/api/gnusocial/version.json");
                        if ($serverret["success"] && ($serverret["body"] != '{"error":"not implemented"}') &&
                                ($serverret["body"] != '') && (strlen($serverret["body"]) < 30)) {
                                $platform = "GNU Social";
@@ -1010,9 +1131,10 @@ class PortableContact
 
                        // Test for Mastodon
                        $orig_version = $version;
-                       $serverret = z_fetch_url($server_url."/api/v1/instance");
+                       $serverret = Network::curl($server_url."/api/v1/instance");
                        if ($serverret["success"] && ($serverret["body"] != '')) {
                                $data = json_decode($serverret["body"]);
+
                                if (isset($data->version)) {
                                        $platform = "Mastodon";
                                        $version = $data->version;
@@ -1020,6 +1142,9 @@ class PortableContact
                                        $info = $data->description;
                                        $network = NETWORK_OSTATUS;
                                }
+                               if (!empty($data->stats->user_count)) {
+                                       $registered_users = $data->stats->user_count;
+                               }
                        }
                        if (strstr($orig_version.$version, 'Pleroma')) {
                                $platform = 'Pleroma';
@@ -1029,7 +1154,7 @@ class PortableContact
 
                if (!$failure) {
                        // Test for Hubzilla and Red
-                       $serverret = z_fetch_url($server_url."/siteinfo.json");
+                       $serverret = Network::curl($server_url."/siteinfo.json");
                        if ($serverret["success"]) {
                                $data = json_decode($serverret["body"]);
                                if (isset($data->url)) {
@@ -1040,6 +1165,9 @@ class PortableContact
                                if (!empty($data->site_name)) {
                                        $site_name = $data->site_name;
                                }
+                               if (!empty($data->channels_total)) {
+                                       $registered_users = $data->channels_total;
+                               }
                                switch ($data->register_policy) {
                                        case "REGISTER_OPEN":
                                                $register_policy = REGISTER_OPEN;
@@ -1054,7 +1182,7 @@ class PortableContact
                                }
                        } else {
                                // Test for Hubzilla, Redmatrix or Friendica
-                               $serverret = z_fetch_url($server_url."/api/statusnet/config.json");
+                               $serverret = Network::curl($server_url."/api/statusnet/config.json");
                                if ($serverret["success"]) {
                                        $data = json_decode($serverret["body"]);
                                        if (isset($data->site->server)) {
@@ -1109,9 +1237,10 @@ class PortableContact
 
                // Query statistics.json. Optional package for Diaspora, Friendica and Redmatrix
                if (!$failure) {
-                       $serverret = z_fetch_url($server_url."/statistics.json");
+                       $serverret = Network::curl($server_url."/statistics.json");
                        if ($serverret["success"]) {
                                $data = json_decode($serverret["body"]);
+
                                if (isset($data->version)) {
                                        $version = $data->version;
                                        // Version numbers on statistics.json are presented with additional info, e.g.:
@@ -1160,16 +1289,20 @@ class PortableContact
                                if (isset($server['site_name'])) {
                                        $site_name = $server['site_name'];
                                }
+
+                               if (isset($server['registered-users'])) {
+                                       $registered_users = $server['registered-users'];
+                               }
                        }
                }
 
                // Check for noscrape
                // Friendica servers could be detected as OStatus servers
-               if (!$failure && in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS))) {
-                       $serverret = z_fetch_url($server_url."/friendica/json");
+               if (!$failure && in_array($network, [NETWORK_DFRN, NETWORK_OSTATUS])) {
+                       $serverret = Network::curl($server_url."/friendica/json");
 
                        if (!$serverret["success"]) {
-                               $serverret = z_fetch_url($server_url."/friendika/json");
+                               $serverret = Network::curl($server_url."/friendika/json");
                        }
 
                        if ($serverret["success"]) {
@@ -1178,7 +1311,7 @@ class PortableContact
                                if (isset($data->version)) {
                                        $network = NETWORK_DFRN;
 
-                                       $noscrape = $data->no_scrape_url;
+                                       $noscrape = defaults($data->no_scrape_url, '');
                                        $version = $data->version;
                                        $site_name = $data->site_name;
                                        $info = $data->info;
@@ -1200,15 +1333,20 @@ class PortableContact
                        }
                }
 
+               // Every server has got at least an admin account
+               if (!$failure && ($registered_users == 0)) {
+                       $registered_users = 1;
+               }
+
                if ($possible_failure && !$failure) {
                        $failure = true;
                }
 
                if ($failure) {
                        $last_contact = $orig_last_contact;
-                       $last_failure = datetime_convert();
+                       $last_failure = DateTimeFormat::utcNow();
                } else {
-                       $last_contact = datetime_convert();
+                       $last_contact = DateTimeFormat::utcNow();
                        $last_failure = $orig_last_failure;
                }
 
@@ -1219,55 +1357,64 @@ class PortableContact
                }
 
                // Check again if the server exists
-               $servers = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
+               $found = dba::exists('gserver', ['nurl' => normalise_link($server_url)]);
 
                $version = strip_tags($version);
                $site_name = strip_tags($site_name);
                $info = strip_tags($info);
                $platform = strip_tags($platform);
 
-               if ($servers) {
-                       q(
-                               "UPDATE `gserver` SET `url` = '%s', `version` = '%s', `site_name` = '%s', `info` = '%s', `register_policy` = %d, `poco` = '%s', `noscrape` = '%s',
-                               `network` = '%s', `platform` = '%s', `last_contact` = '%s', `last_failure` = '%s' WHERE `nurl` = '%s'",
-                               dbesc($server_url),
-                               dbesc($version),
-                               dbesc($site_name),
-                               dbesc($info),
-                               intval($register_policy),
-                               dbesc($poco),
-                               dbesc($noscrape),
-                               dbesc($network),
-                               dbesc($platform),
-                               dbesc($last_contact),
-                               dbesc($last_failure),
-                               dbesc(normalise_link($server_url))
-                       );
+               $fields = ['url' => $server_url, 'version' => $version,
+                               'site_name' => $site_name, 'info' => $info, 'register_policy' => $register_policy,
+                               'poco' => $poco, 'noscrape' => $noscrape, 'network' => $network,
+                               'platform' => $platform, 'registered-users' => $registered_users,
+                               'last_contact' => $last_contact, 'last_failure' => $last_failure];
+
+               if ($found) {
+                       dba::update('gserver', $fields, ['nurl' => normalise_link($server_url)]);
                } elseif (!$failure) {
-                       q(
-                               "INSERT INTO `gserver` (`url`, `nurl`, `version`, `site_name`, `info`, `register_policy`, `poco`, `noscrape`, `network`, `platform`, `created`, `last_contact`, `last_failure`)
-                                               VALUES ('%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
-                               dbesc($server_url),
-                               dbesc(normalise_link($server_url)),
-                               dbesc($version),
-                               dbesc($site_name),
-                               dbesc($info),
-                               intval($register_policy),
-                               dbesc($poco),
-                               dbesc($noscrape),
-                               dbesc($network),
-                               dbesc($platform),
-                               dbesc(datetime_convert()),
-                               dbesc($last_contact),
-                               dbesc($last_failure),
-                               dbesc(datetime_convert())
-                       );
+                       $fields['nurl'] = normalise_link($server_url);
+                       $fields['created'] = DateTimeFormat::utcNow();
+                       dba::insert('gserver', $fields);
                }
+
+               if (in_array($fields['network'], [NETWORK_DFRN, NETWORK_DIASPORA])) {
+                       self::discoverRelay(server_url);
+               }
+
                logger("End discovery for server " . $server_url, LOGGER_DEBUG);
 
                return !$failure;
        }
 
+       private static function discoverRelay($server_url)
+       {
+               logger("Discover relay data for server " . $server_url, LOGGER_DEBUG);
+
+               $serverret = Network::curl($server_url."/.well-known/x-social-relay");
+               if (!$serverret["success"]) {
+                       return;
+               }
+
+               $data = json_decode($serverret['body']);
+               if (!is_object($data)) {
+                       return;
+               }
+
+               $gserver = dba::selectFirst('gserver', ['id'], ['nurl' => normalise_link($server_url)]);
+               if (!DBM::is_result($gserver)) {
+                       return;
+               }
+
+               $fields = ['relay-subscribe' => $data->subscribe, 'relay-scope' => $data->scope];
+               dba::update('gserver', $fields, ['id' => $gserver['id']]);
+
+               dba::delete('gserver-tag', ['gserver-id' => $gserver['id']]);
+               foreach ($data->tags as $tag) {
+                       dba::insert('gserver-tag', ['gserver-id' => $gserver['id'], 'tag' => $tag]);
+               }
+       }
+
        /**
         * @brief Returns a list of all known servers
         * @return array List of server urls
@@ -1296,9 +1443,9 @@ class PortableContact
         *
         * @param string $poco URL to the POCO endpoint
         */
-       public static function fetchServerlist($poco)
+       private static function fetchServerlist($poco)
        {
-               $serverret = z_fetch_url($poco."/@server");
+               $serverret = Network::curl($poco."/@server");
                if (!$serverret["success"]) {
                        return;
                }
@@ -1319,7 +1466,7 @@ class PortableContact
                }
        }
 
-       public static function discoverFederation()
+       private static function discoverFederation()
        {
                $last = Config::get('poco', 'last_federation_discovery');
 
@@ -1331,7 +1478,7 @@ class PortableContact
                }
 
                // Discover Friendica, Hubzilla and Diaspora servers
-               $serverdata = fetch_url("http://the-federation.info/pods.json");
+               $serverdata = Network::fetchUrl("http://the-federation.info/pods.json");
 
                if ($serverdata) {
                        $servers = json_decode($serverdata);
@@ -1343,14 +1490,17 @@ class PortableContact
 
                // Disvover Mastodon servers
                if (!Config::get('system', 'ostatus_disabled')) {
-                       $serverdata = fetch_url("https://instances.mastodon.xyz/instances.json");
-
-                       if ($serverdata) {
-                               $servers = json_decode($serverdata);
-
-                               foreach ($servers as $server) {
-                                       $url = (is_null($server->https_score) ? 'http' : 'https').'://'.$server->name;
-                                       Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $url);
+                       $accesstoken = Config::get('system', 'instances_social_key');
+                       if (!empty($accesstoken)) {
+                               $api = 'https://instances.social/api/1.0/instances/list?count=0';
+                               $header = ['Authorization: Bearer '.$accesstoken];
+                               $serverdata = Network::curl($api, false, $redirects, ['headers' => $header]);
+                               if ($serverdata['success']) {
+                                       $servers = json_decode($serverdata['body']);
+                                       foreach ($servers->instances as $server) {
+                                               $url = (is_null($server->https_score) ? 'http' : 'https').'://'.$server->name;
+                                               Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $url);
+                                       }
                                }
                        }
                }
@@ -1361,7 +1511,7 @@ class PortableContact
                //if (!Config::get('system','ostatus_disabled')) {
                //      $serverdata = "http://gstools.org/api/get_open_instances/";
 
-               //      $result = z_fetch_url($serverdata);
+               //      $result = Network::curl($serverdata);
                //      if ($result["success"]) {
                //              $servers = json_decode($result["body"]);
 
@@ -1390,7 +1540,7 @@ class PortableContact
 
                logger("Fetch all users from the server ".$server["url"], LOGGER_DEBUG);
 
-               $retdata = z_fetch_url($url);
+               $retdata = Network::curl($url);
                if ($retdata["success"]) {
                        $data = json_decode($retdata["body"]);
 
@@ -1402,14 +1552,14 @@ class PortableContact
                                        $timeframe = 30;
                                }
 
-                               $updatedSince = date("Y-m-d H:i:s", time() - $timeframe * 86400);
+                               $updatedSince = date(DateTimeFormat::MYSQL, time() - $timeframe * 86400);
 
                                // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3)
                                $url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
 
                                $success = false;
 
-                               $retdata = z_fetch_url($url);
+                               $retdata = Network::curl($url);
                                if ($retdata["success"]) {
                                        logger("Fetch all global contacts from the server ".$server["nurl"], LOGGER_DEBUG);
                                        $success = self::discoverServer(json_decode($retdata["body"]));
@@ -1421,7 +1571,8 @@ class PortableContact
                                }
                        }
 
-                       q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
+                       $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
+                       dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
 
                        return true;
                } else {
@@ -1429,7 +1580,8 @@ class PortableContact
                        self::checkServer($server["url"], $server["network"], true);
 
                        // If we couldn't reach the server, we will try it some time later
-                       q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
+                       $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
+                       dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
 
                        return false;
                }
@@ -1454,7 +1606,8 @@ class PortableContact
                        foreach ($r as $server) {
                                if (!self::checkServer($server["url"], $server["network"])) {
                                        // The server is not reachable? Okay, then we will try it later
-                                       q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
+                                       $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
+                                       dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
                                        continue;
                                }
 
@@ -1468,7 +1621,7 @@ class PortableContact
                }
        }
 
-       public static function discoverServerUsers($data, $server)
+       private static function discoverServerUsers($data, $server)
        {
                if (!isset($data->entry)) {
                        return;
@@ -1491,7 +1644,7 @@ class PortableContact
                                // Fetch all contacts from a given user from the other server
                                $url = $server["poco"]."/".$username."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
 
-                               $retdata = z_fetch_url($url);
+                               $retdata = Network::curl($url);
                                if ($retdata["success"]) {
                                        self::discoverServer(json_decode($retdata["body"]), 3);
                                }
@@ -1499,7 +1652,7 @@ class PortableContact
                }
        }
 
-       public static function discoverServer($data, $default_generation = 0)
+       private static function discoverServer($data, $default_generation = 0)
        {
                if (!isset($data->entry) || !count($data->entry)) {
                        return false;
@@ -1546,7 +1699,7 @@ class PortableContact
                        }
 
                        if (isset($entry->updated)) {
-                               $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
+                               $updated = date(DateTimeFormat::MYSQL, strtotime($entry->updated));
                        }
 
                        if (isset($entry->network)) {
@@ -1558,7 +1711,7 @@ class PortableContact
                        }
 
                        if (isset($entry->aboutMe)) {
-                               $about = html2bbcode($entry->aboutMe);
+                               $about = HTML::toBBCode($entry->aboutMe);
                        }
 
                        if (isset($entry->gender)) {
@@ -1584,7 +1737,7 @@ class PortableContact
 
                                logger("Store profile ".$profile_url, LOGGER_DEBUG);
 
-                               $gcontact = array("url" => $profile_url,
+                               $gcontact = ["url" => $profile_url,
                                                "name" => $name,
                                                "network" => $network,
                                                "photo" => $profile_photo,
@@ -1595,11 +1748,11 @@ class PortableContact
                                                "connect" => $connect_url,
                                                "updated" => $updated,
                                                "contact-type" => $contact_type,
-                                               "generation" => $generation);
+                                               "generation" => $generation];
 
                                try {
-                                       $gcontact = GlobalContact::sanitize($gcontact);
-                                       GlobalContact::update($gcontact);
+                                       $gcontact = GContact::sanitize($gcontact);
+                                       GContact::update($gcontact);
                                } catch (Exception $e) {
                                        logger($e->getMessage(), LOGGER_DEBUG);
                                }