]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/PortableContact.php
Fix docblocks types
[friendica.git] / src / Protocol / PortableContact.php
index f884b0f283a641926f04b83da647b63f6a632990..727cce4e1e2a1661aa74dfa0df078606446364ee 100644 (file)
@@ -9,20 +9,21 @@
 
 namespace Friendica\Protocol;
 
-use Friendica\App;
-use Friendica\Core\System;
-use Friendica\Core\Cache;
 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 dba;
+use DOMDocument;
+use DOMXPath;
+use Exception;
 
+require_once 'include/dba.php';
 require_once 'include/datetime.php';
 require_once 'include/network.php';
 require_once 'include/html2bbcode.php';
-require_once 'include/Contact.php';
-require_once 'include/Photo.php';
 
 class PortableContact
 {
@@ -47,7 +48,7 @@ class PortableContact
        public static function loadWorker($cid, $uid = 0, $zcid = 0, $url = null)
        {
                // Call the function "load" via the worker
-               Worker::add(PRIORITY_LOW, "discover_poco", "load", (int)$cid, (int)$uid, (int)$zcid, $url);
+               Worker::add(PRIORITY_LOW, "DiscoverPoCo", "load", (int)$cid, (int)$uid, (int)$zcid, $url);
        }
 
        /**
@@ -192,10 +193,10 @@ class PortableContact
                                        "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);
                        }
@@ -210,21 +211,21 @@ class PortableContact
                );
        }
 
-       function poco_reachable($profile, $server = "", $network = "", $force = false) {
-
+       public static function reachable($profile, $server = "", $network = "", $force = false)
+       {
                if ($server == "") {
-                       $server = poco_detect_server($profile);
+                       $server = self::detectServer($profile);
                }
 
                if ($server == "") {
                        return true;
                }
 
-               return poco_check_server($server, $network, $force);
+               return self::checkServer($server, $network, $force);
        }
 
-       function poco_detect_server($profile) {
-
+       public static function detectServer($profile)
+       {
                // Try to detect the server path based upon some known standard paths
                $server_url = "";
 
@@ -283,8 +284,11 @@ class PortableContact
                        return "";
                }
 
-               $r = q("SELECT `id` FROM `gserver` WHERE `nurl` = '%s' AND `last_contact` > `last_failure`",
-                       dbesc(normalise_link($server_url)));
+               $r = q(
+                       "SELECT `id` FROM `gserver` WHERE `nurl` = '%s' AND `last_contact` > `last_failure`",
+                       dbesc(normalise_link($server_url))
+               );
+
                if (DBM::is_result($r)) {
                        return $server_url;
                }
@@ -298,14 +302,17 @@ class PortableContact
                return $server_url;
        }
 
-       function poco_alternate_ostatus_url($url) {
+       public static function alternateOStatusUrl($url)
+       {
                return(preg_match("=https?://.+/user/\d+=ism", $url, $matches));
        }
 
-       function poco_last_updated($profile, $force = false) {
-
-               $gcontacts = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'",
-                               dbesc(normalise_link($profile)));
+       public static function lastUpdated($profile, $force = false)
+       {
+               $gcontacts = q(
+                       "SELECT * FROM `gcontact` WHERE `nurl` = '%s'",
+                       dbesc(normalise_link($profile))
+               );
 
                if (!DBM::is_result($gcontacts)) {
                        return false;
@@ -318,7 +325,7 @@ class PortableContact
                }
 
                if ($force) {
-                       $server_url = normalise_link(poco_detect_server($profile));
+                       $server_url = normalise_link(self::detectServer($profile));
                }
 
                if (($server_url == '') && ($gcontacts[0]["server_url"] != "")) {
@@ -326,7 +333,7 @@ class PortableContact
                }
 
                if (!$force && (($server_url == '') || ($gcontacts[0]["server_url"] == $gcontacts[0]["nurl"]))) {
-                       $server_url = normalise_link(poco_detect_server($profile));
+                       $server_url = normalise_link(self::detectServer($profile));
                }
 
                if (!in_array($gcontacts[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_FEED, NETWORK_OSTATUS, ""))) {
@@ -335,10 +342,13 @@ class PortableContact
                }
 
                if ($server_url != "") {
-                       if (!poco_check_server($server_url, $gcontacts[0]["network"], $force)) {
+                       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)));
+                                       q(
+                                               "UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
+                                               dbesc(datetime_convert()),
+                                               dbesc(normalise_link($profile))
+                                       );
                                }
 
                                logger("Profile ".$profile.": Server ".$server_url." wasn't reachable.", LOGGER_DEBUG);
@@ -348,8 +358,10 @@ class PortableContact
                }
 
                if (in_array($gcontacts[0]["network"], array("", NETWORK_FEED))) {
-                       $server = q("SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''",
-                               dbesc(normalise_link($server_url)));
+                       $server = q(
+                               "SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''",
+                               dbesc(normalise_link($server_url))
+                       );
 
                        if ($server) {
                                $contact['network'] = $server[0]["network"];
@@ -360,7 +372,6 @@ class PortableContact
 
                // noscrape is really fast so we don't cache the call.
                if (($server_url != "") && ($gcontacts[0]["nick"] != "")) {
-
                        //  Use noscrape if possible
                        $server = q("SELECT `noscrape`, `network` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", dbesc(normalise_link($server_url)));
 
@@ -368,7 +379,6 @@ class PortableContact
                                $noscraperet = z_fetch_url($server[0]["noscrape"]."/".$gcontacts[0]["nick"]);
 
                                if ($noscraperet["success"] && ($noscraperet["body"] != "")) {
-
                                        $noscrape = json_decode($noscraperet["body"], true);
 
                                        if (is_array($noscrape)) {
@@ -387,7 +397,7 @@ class PortableContact
                                                        }
                                                }
 
-                                               $location = formatted_location($noscrape);
+                                               $location = Profile::formatLocation($noscrape);
                                                if ($location) {
                                                        $contact["location"] = $location;
                                                }
@@ -415,11 +425,14 @@ class PortableContact
 
                                                $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)));
+                                                       q(
+                                                               "UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'",
+                                                               dbesc(datetime_convert()),
+                                                               dbesc(normalise_link($profile))
+                                                       );
 
                                                        logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG);
 
@@ -431,10 +444,10 @@ class PortableContact
                }
 
                // If we only can poll the feed, then we only do this once a while
-               if (!$force && !poco_do_update($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) {
+               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"];
                }
 
@@ -442,7 +455,7 @@ class PortableContact
 
                // Is the profile link the alternate OStatus link notation? (http://domain.tld/user/4711)
                // Then check the other link and delete this one
-               if (($data["network"] == NETWORK_OSTATUS) && poco_alternate_ostatus_url($profile)
+               if (($data["network"] == NETWORK_OSTATUS) && self::alternateOStatusUrl($profile)
                        && (normalise_link($profile) == normalise_link($data["alias"]))
                        && (normalise_link($profile) != normalise_link($data["url"]))
                ) {
@@ -455,10 +468,10 @@ class PortableContact
                        $gcontact["server_url"] = $data["baseurl"];
 
                        try {
-                               $gcontact = GlobalContact::sanitize($gcontact);
-                               GlobalContact::update($gcontact);
+                               $gcontact = GContact::sanitize($gcontact);
+                               GContact::update($gcontact);
 
-                               poco_last_updated($data["url"], $force);
+                               self::lastUpdated($data["url"], $force);
                        } catch (Exception $e) {
                                logger($e->getMessage(), LOGGER_DEBUG);
                        }
@@ -468,8 +481,11 @@ class PortableContact
                }
 
                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)));
+                       q(
+                               "UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
+                               dbesc(datetime_convert()),
+                               dbesc(normalise_link($profile))
+                       );
 
                        logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG);
                        return false;
@@ -479,13 +495,16 @@ class PortableContact
 
                $contact["server_url"] = $data["baseurl"];
 
-               GlobalContact::update($contact);
+               GContact::update($contact);
 
                $feedret = z_fetch_url($data["poll"]);
 
                if (!$feedret["success"]) {
-                       q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
-                               dbesc(datetime_convert()), dbesc(normalise_link($profile)));
+                       q(
+                               "UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
+                               dbesc(datetime_convert()),
+                               dbesc(normalise_link($profile))
+                       );
 
                        logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG);
                        return false;
@@ -494,7 +513,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');
@@ -518,12 +537,18 @@ 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)));
+               q(
+                       "UPDATE `gcontact` SET `updated` = '%s', `last_contact` = '%s' WHERE `nurl` = '%s'",
+                       dbesc(DBM::date($last_updated)),
+                       dbesc(DBM::date()),
+                       dbesc(normalise_link($profile))
+               );
 
                if (($gcontacts[0]["generation"] == 0)) {
-                       q("UPDATE `gcontact` SET `generation` = 9 WHERE `nurl` = '%s'",
-                               dbesc(normalise_link($profile)));
+                       q(
+                               "UPDATE `gcontact` SET `generation` = 9 WHERE `nurl` = '%s'",
+                               dbesc(normalise_link($profile))
+                       );
                }
 
                logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG);
@@ -531,7 +556,8 @@ class PortableContact
                return($last_updated);
        }
 
-       function poco_do_update($created, $updated, $last_failure, $last_contact) {
+       public static function updateNeeded($created, $updated, $last_failure, $last_contact)
+       {
                $now = strtotime(datetime_convert());
 
                if ($updated > $last_contact) {
@@ -575,7 +601,8 @@ class PortableContact
                return true;
        }
 
-       function poco_to_boolean($val) {
+       public static function toBoolean($val)
+       {
                if (($val == "true") || ($val == 1)) {
                        return true;
                } elseif (($val == "false") || ($val == 0)) {
@@ -591,7 +618,8 @@ class PortableContact
         * @param object $data POCO data
         * @return array Server data
         */
-       function poco_detect_poco_data($data) {
+       public static function detectPocoData($data)
+       {
                $server = false;
 
                if (!isset($data->entry)) {
@@ -627,7 +655,8 @@ class PortableContact
         * @param string $server_url address of the server
         * @return array Server data
         */
-       function poco_fetch_nodeinfo($server_url) {
+       public static function fetchNodeinfo($server_url)
+       {
                $serverret = z_fetch_url($server_url."/.well-known/nodeinfo");
                if (!$serverret["success"]) {
                        return false;
@@ -734,12 +763,13 @@ class PortableContact
         * @param string $body Front page of the server
         * @return array Server data
         */
-       function poco_detect_server_type($body) {
+       public 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]");
 
@@ -790,8 +820,8 @@ class PortableContact
                return $server;
        }
 
-       function poco_check_server($server_url, $network = "", $force = false) {
-
+       public static function checkServer($server_url, $network = "", $force = false)
+       {
                // Unify the server address
                $server_url = trim($server_url, "/");
                $server_url = str_replace("/index.php", "", $server_url);
@@ -802,10 +832,12 @@ class PortableContact
 
                $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)));
+                               q(
+                                       "UPDATE `gserver` SET `created` = '%s' WHERE `nurl` = '%s'",
+                                       dbesc(datetime_convert()),
+                                       dbesc(normalise_link($server_url))
+                               );
                        }
                        $poco = $servers[0]["poco"];
                        $noscrape = $servers[0]["noscrape"];
@@ -822,7 +854,7 @@ class PortableContact
                        $info = $servers[0]["info"];
                        $register_policy = $servers[0]["register_policy"];
 
-                       if (!$force && !poco_do_update($servers[0]["created"], "", $last_failure, $last_contact)) {
+                       if (!$force && !self::updateNeeded($servers[0]["created"], "", $last_failure, $last_contact)) {
                                logger("Use cached data for server ".$server_url, LOGGER_DEBUG);
                                return ($last_contact >= $last_failure);
                        }
@@ -862,7 +894,7 @@ class PortableContact
                }
 
                // Maybe the page is unencrypted only?
-               $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
+               $xmlobj = @simplexml_load_string($serverret["body"], 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
                if (!$serverret["success"] || ($serverret["body"] == "") || (@sizeof($xmlobj) == 0) || !is_object($xmlobj)) {
                        $server_url = str_replace("https://", "http://", $server_url);
 
@@ -876,7 +908,7 @@ class PortableContact
                                return false;
                        }
 
-                       $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
+                       $xmlobj = @simplexml_load_string($serverret["body"], 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
                }
 
                if (!$serverret["success"] || ($serverret["body"] == "") || (sizeof($xmlobj) == 0) || !is_object($xmlobj)) {
@@ -903,7 +935,7 @@ class PortableContact
                                $data = json_decode($serverret["body"]);
                                if (isset($data->totalResults)) {
                                        $poco = $server_url."/poco";
-                                       $server = poco_detect_poco_data($data);
+                                       $server = self::detectPocoData($data);
                                        if ($server) {
                                                $platform = $server['platform'];
                                                $network = $server['network'];
@@ -921,7 +953,7 @@ class PortableContact
                        if (!$serverret["success"] || ($serverret["body"] == "")) {
                                $failure = true;
                        } else {
-                               $server = poco_detect_server_type($serverret["body"]);
+                               $server = self::detectServerType($serverret["body"]);
                                if ($server) {
                                        $platform = $server['platform'];
                                        $network = $server['network'];
@@ -929,11 +961,11 @@ class PortableContact
                                        $site_name = $server['site_name'];
                                }
 
-                               $lines = explode("\n",$serverret["header"]);
+                               $lines = explode("\n", $serverret["header"]);
                                if (count($lines)) {
-                                       foreach($lines as $line) {
+                                       foreach ($lines as $line) {
                                                $line = trim($line);
-                                               if (stristr($line,'X-Diaspora-Version:')) {
+                                               if (stristr($line, 'X-Diaspora-Version:')) {
                                                        $platform = "Diaspora";
                                                        $version = trim(str_replace("X-Diaspora-Version:", "", $line));
                                                        $version = trim(str_replace("x-diaspora-version:", "", $version));
@@ -942,7 +974,7 @@ class PortableContact
                                                        $version = $versionparts[0];
                                                }
 
-                                               if (stristr($line,'Server: Mastodon')) {
+                                               if (stristr($line, 'Server: Mastodon')) {
                                                        $platform = "Mastodon";
                                                        $network = NETWORK_OSTATUS;
                                                }
@@ -1059,9 +1091,9 @@ class PortableContact
 
                                                $site_name = $data->site->name;
 
-                                               $data->site->closed = poco_to_boolean($data->site->closed);
-                                               $data->site->private = poco_to_boolean($data->site->private);
-                                               $data->site->inviteonly = poco_to_boolean($data->site->inviteonly);
+                                               $data->site->closed = self::toBoolean($data->site->closed);
+                                               $data->site->private = self::toBoolean($data->site->private);
+                                               $data->site->inviteonly = self::toBoolean($data->site->inviteonly);
 
                                                if (!$data->site->closed && !$data->site->private and $data->site->inviteonly) {
                                                        $register_policy = REGISTER_APPROVE;
@@ -1109,7 +1141,7 @@ class PortableContact
 
                // Query nodeinfo. Working for (at least) Diaspora and Friendica.
                if (!$failure) {
-                       $server = poco_fetch_nodeinfo($server_url);
+                       $server = self::fetchNodeinfo($server_url);
                        if ($server) {
                                $register_policy = $server['register_policy'];
 
@@ -1195,7 +1227,8 @@ class PortableContact
                $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',
+                       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),
@@ -1211,22 +1244,23 @@ class PortableContact
                                dbesc(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`)
+                       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())
+                               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())
                        );
                }
                logger("End discovery for server " . $server_url, LOGGER_DEBUG);
@@ -1238,12 +1272,18 @@ class PortableContact
         * @brief Returns a list of all known servers
         * @return array List of server urls
         */
-       function poco_serverlist() {
-               $r = q("SELECT `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver`
+       public static function serverlist()
+       {
+               $r = q(
+                       "SELECT `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver`
                        WHERE `network` IN ('%s', '%s', '%s') AND `last_contact` > `last_failure`
                        ORDER BY `last_contact`
                        LIMIT 1000",
-                       dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
+                       dbesc(NETWORK_DFRN),
+                       dbesc(NETWORK_DIASPORA),
+                       dbesc(NETWORK_OSTATUS)
+               );
+
                if (!DBM::is_result($r)) {
                        return false;
                }
@@ -1256,7 +1296,8 @@ class PortableContact
         *
         * @param string $poco URL to the POCO endpoint
         */
-       function poco_fetch_serverlist($poco) {
+       public static function fetchServerlist($poco)
+       {
                $serverret = z_fetch_url($poco."/@server");
                if (!$serverret["success"]) {
                        return;
@@ -1273,13 +1314,14 @@ class PortableContact
                        $r = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
                        if (!DBM::is_result($r)) {
                                logger("Call server check for server ".$server_url, LOGGER_DEBUG);
-                               Worker::add(PRIORITY_LOW, "discover_poco", "server", $server_url);
+                               Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $server_url);
                        }
                }
        }
 
-       function poco_discover_federation() {
-               $last = Config::get('poco','last_federation_discovery');
+       public static function discoverFederation()
+       {
+               $last = Config::get('poco', 'last_federation_discovery');
 
                if ($last) {
                        $next = $last + (24 * 60 * 60);
@@ -1295,20 +1337,23 @@ class PortableContact
                        $servers = json_decode($serverdata);
 
                        foreach ($servers->pods as $server) {
-                               Worker::add(PRIORITY_LOW, "discover_poco", "server", "https://".$server->host);
+                               Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", "https://".$server->host);
                        }
                }
 
                // 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, "discover_poco", "server", $url);
+               if (!Config::get('system', 'ostatus_disabled')) {
+                       $accesstoken = Config::get('system', 'instances_social_key');
+                       if (!empty($accesstoken)) {
+                               $api = 'https://instances.social/api/1.0/instances/list?count=0';
+                               $header = array('Authorization: Bearer '.$accesstoken);
+                               $serverdata = z_fetch_url($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);
+                                       }
                                }
                        }
                }
@@ -1324,14 +1369,15 @@ class PortableContact
                //              $servers = json_decode($result["body"]);
 
                //              foreach($servers->data as $server)
-               //                      poco_check_server($server->instance_address);
+               //                      self::checkServer($server->instance_address);
                //      }
                //}
 
-               Config::set('poco','last_federation_discovery', time());
+               Config::set('poco', 'last_federation_discovery', time());
        }
 
-       function poco_discover_single_server($id) {
+       public static function discoverSingleServer($id)
+       {
                $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `id` = %d", intval($id));
                if (!DBM::is_result($r)) {
                        return false;
@@ -1340,7 +1386,7 @@ class PortableContact
                $server = $r[0];
 
                // Discover new servers out there (Works from Friendica version 3.5.2)
-               poco_fetch_serverlist($server["poco"]);
+               self::fetchServerlist($server["poco"]);
 
                // Fetch all users from the other server
                $url = $server["poco"]."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
@@ -1351,11 +1397,10 @@ class PortableContact
                if ($retdata["success"]) {
                        $data = json_decode($retdata["body"]);
 
-                       poco_discover_server($data, 2);
-
-                       if (Config::get('system','poco_discovery') > 1) {
+                       self::discoverServer($data, 2);
 
-                               $timeframe = Config::get('system','poco_discovery_since');
+                       if (Config::get('system', 'poco_discovery') > 1) {
+                               $timeframe = Config::get('system', 'poco_discovery_since');
                                if ($timeframe == 0) {
                                        $timeframe = 30;
                                }
@@ -1370,12 +1415,12 @@ class PortableContact
                                $retdata = z_fetch_url($url);
                                if ($retdata["success"]) {
                                        logger("Fetch all global contacts from the server ".$server["nurl"], LOGGER_DEBUG);
-                                       $success = poco_discover_server(json_decode($retdata["body"]));
+                                       $success = self::discoverServer(json_decode($retdata["body"]));
                                }
 
-                               if (!$success && (Config::get('system','poco_discovery') > 2)) {
+                               if (!$success && (Config::get('system', 'poco_discovery') > 2)) {
                                        logger("Fetch contacts from users of the server ".$server["nurl"], LOGGER_DEBUG);
-                                       poco_discover_server_users($data, $server);
+                                       self::discoverServerUsers($data, $server);
                                }
                        }
 
@@ -1384,7 +1429,7 @@ class PortableContact
                        return true;
                } else {
                        // If the server hadn't replied correctly, then force a sanity check
-                       poco_check_server($server["url"], $server["network"], true);
+                       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"]));
@@ -1393,10 +1438,10 @@ class PortableContact
                }
        }
 
-       function poco_discover($complete = false) {
-
+       public static function discover($complete = false)
+       {
                // Update the server list
-               poco_discover_federation();
+               self::discoverFederation();
 
                $no_of_queries = 5;
 
@@ -1410,15 +1455,14 @@ class PortableContact
                $r = q("SELECT `id`, `url`, `network` FROM `gserver` WHERE `last_contact` >= `last_failure` AND `poco` != '' AND `last_poco_query` < '%s' ORDER BY RAND()", dbesc($last_update));
                if (DBM::is_result($r)) {
                        foreach ($r as $server) {
-
-                               if (!poco_check_server($server["url"], $server["network"])) {
+                               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"]));
                                        continue;
                                }
 
                                logger('Update directory from server '.$server['url'].' with ID '.$server['id'], LOGGER_DEBUG);
-                               Worker::add(PRIORITY_LOW, "discover_poco", "update_server_directory", (int)$server['id']);
+                               Worker::add(PRIORITY_LOW, "DiscoverPoCo", "update_server_directory", (int)$server['id']);
 
                                if (!$complete && (--$no_of_queries == 0)) {
                                        break;
@@ -1427,8 +1471,8 @@ class PortableContact
                }
        }
 
-       function poco_discover_server_users($data, $server) {
-
+       public static function discoverServerUsers($data, $server)
+       {
                if (!isset($data->entry)) {
                        return;
                }
@@ -1452,14 +1496,14 @@ class PortableContact
 
                                $retdata = z_fetch_url($url);
                                if ($retdata["success"]) {
-                                       poco_discover_server(json_decode($retdata["body"]), 3);
+                                       self::discoverServer(json_decode($retdata["body"]), 3);
                                }
                        }
                }
        }
 
-       function poco_discover_server($data, $default_generation = 0) {
-
+       public static function discoverServer($data, $default_generation = 0)
+       {
                if (!isset($data->entry) || !count($data->entry)) {
                        return false;
                }
@@ -1524,11 +1568,11 @@ class PortableContact
                                $gender = $entry->gender;
                        }
 
-                       if(isset($entry->generation) && ($entry->generation > 0)) {
+                       if (isset($entry->generation) && ($entry->generation > 0)) {
                                $generation = ++$entry->generation;
                        }
 
-                       if(isset($entry->contactType) && ($entry->contactType >= 0)) {
+                       if (isset($entry->contactType) && ($entry->contactType >= 0)) {
                                $contact_type = $entry->contactType;
                        }
 
@@ -1557,8 +1601,8 @@ class PortableContact
                                                "generation" => $generation);
 
                                try {
-                                       $gcontact = GlobalContact::sanitize($gcontact);
-                                       update_gcontact($gcontact);
+                                       $gcontact = GContact::sanitize($gcontact);
+                                       GContact::update($gcontact);
                                } catch (Exception $e) {
                                        logger($e->getMessage(), LOGGER_DEBUG);
                                }