X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FProtocol%2FPortableContact.php;h=aea68eb998e9bd9043e528f0bc516c1fefe8d45c;hb=8aff8a76eb9efc4acaab0af3c8c0ca7a011f4349;hp=e0213ca5c4cf18d587bbbe36cd932325bdb86e31;hpb=5926cd534aac8bf481c49267be3cf9e9636a8a41;p=friendica.git diff --git a/src/Protocol/PortableContact.php b/src/Protocol/PortableContact.php index e0213ca5c4..aea68eb998 100644 --- a/src/Protocol/PortableContact.php +++ b/src/Protocol/PortableContact.php @@ -15,6 +15,8 @@ use Friendica\Database\DBM; use Friendica\Model\GContact; use Friendica\Model\Profile; use Friendica\Network\Probe; +use Friendica\Util\Network; +use Friendica\Util\Temporal; use dba; use DOMDocument; use DOMXPath; @@ -22,7 +24,6 @@ use Exception; require_once 'include/dba.php'; require_once 'include/datetime.php'; -require_once 'include/network.php'; require_once 'include/html2bbcode.php'; class PortableContact @@ -65,22 +66,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 +86,7 @@ class PortableContact logger('load: ' . $url, LOGGER_DEBUG); - $s = fetch_url($url); + $s = Network::fetchUrl($url); logger('load: returns ' . $s, LOGGER_DATA); @@ -146,7 +144,7 @@ class PortableContact } if (isset($entry->updated)) { - $updated = date("Y-m-d H:i:s", strtotime($entry->updated)); + $updated = date(Temporal::MYSQL, strtotime($entry->updated)); } if (isset($entry->network)) { @@ -179,7 +177,7 @@ class PortableContact $contact_type = $entry->contactType; } - $gcontact = array("url" => $profile_url, + $gcontact = ["url" => $profile_url, "name" => $name, "network" => $network, "photo" => $profile_photo, @@ -190,7 +188,7 @@ class PortableContact "connect" => $connect_url, "updated" => $updated, "contact-type" => $contact_type, - "generation" => $generation); + "generation" => $generation]; try { $gcontact = GContact::sanitize($gcontact); @@ -203,12 +201,8 @@ class PortableContact } 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 +288,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,10 +312,10 @@ class PortableContact return false; } - $contact = array("url" => $profile); + $contact = ["url" => $profile]; if ($gcontacts[0]["created"] <= NULL_DATE) { - $contact['created'] = datetime_convert(); + $contact['created'] = Temporal::utcNow(); } if ($force) { @@ -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' => Temporal::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"] = Temporal::utcNow(); $contact = array_merge($contact, $noscrape); 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' => Temporal::utcNow()]; + dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]); logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG); @@ -460,8 +448,7 @@ 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); @@ -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' => Temporal::utcNow()]; + dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]); logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG); return false; @@ -497,14 +481,11 @@ class PortableContact 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' => Temporal::utcNow()]; + dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]); logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG); return false; @@ -537,18 +518,12 @@ 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' => DBM::date($last_updated), 'last_contact' => DBM::date()]; + 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); @@ -558,7 +533,7 @@ class PortableContact public static function updateNeeded($created, $updated, $last_failure, $last_contact) { - $now = strtotime(datetime_convert()); + $now = strtotime(Temporal::utcNow()); if ($updated > $last_contact) { $contact_time = strtotime($updated); @@ -601,7 +576,7 @@ class PortableContact return true; } - public static function toBoolean($val) + private static function toBoolean($val) { if (($val == "true") || ($val == 1)) { return true; @@ -618,7 +593,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 +615,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 +630,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 +647,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 +723,10 @@ class PortableContact } } + if (!empty($nodeinfo->usage->users->total)) { + $server['registered-users'] = $nodeinfo->usage->users->total; + } + $diaspora = false; $friendica = false; $gnusocial = false; @@ -757,13 +762,97 @@ 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; @@ -774,7 +863,7 @@ class PortableContact $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 +872,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 +886,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,8 +905,7 @@ class PortableContact return false; } - /// @fixme $element is unavailable here - $server["site_name"] = $xpath->evaluate($element."//head/title/text()")->item(0)->nodeValue; + $server["site_name"] = $xpath->evaluate("//head/title/text()")->item(0)->nodeValue; return $server; } @@ -831,31 +919,30 @@ 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' => Temporal::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)) { + 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); } @@ -867,30 +954,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' => Temporal::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' => Temporal::utcNow()], ['nurl' => normalise_link($server_url)]); return false; } @@ -900,12 +995,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' => Temporal::utcNow()], ['nurl' => normalise_link($server_url)]); return false; } @@ -914,7 +1009,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; @@ -929,12 +1024,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) { @@ -944,12 +1045,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; @@ -988,7 +1096,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"; @@ -999,7 +1107,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"; @@ -1011,9 +1119,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; @@ -1021,6 +1130,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'; @@ -1030,7 +1142,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)) { @@ -1041,6 +1153,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; @@ -1055,7 +1170,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)) { @@ -1110,9 +1225,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.: @@ -1161,16 +1277,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"]) { @@ -1201,15 +1321,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 = Temporal::utcNow(); } else { - $last_contact = datetime_convert(); + $last_contact = Temporal::utcNow(); $last_failure = $orig_last_failure; } @@ -1220,49 +1345,25 @@ 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'] = Temporal::utcNow(); + dba::insert('gserver', $fields); } logger("End discovery for server " . $server_url, LOGGER_DEBUG); @@ -1297,9 +1398,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; } @@ -1320,7 +1421,7 @@ class PortableContact } } - public static function discoverFederation() + private static function discoverFederation() { $last = Config::get('poco', 'last_federation_discovery'); @@ -1332,7 +1433,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); @@ -1347,8 +1448,8 @@ class PortableContact $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]); + $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) { @@ -1365,7 +1466,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"]); @@ -1394,7 +1495,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"]); @@ -1406,14 +1507,14 @@ class PortableContact $timeframe = 30; } - $updatedSince = date("Y-m-d H:i:s", time() - $timeframe * 86400); + $updatedSince = date(Temporal::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"])); @@ -1425,7 +1526,8 @@ class PortableContact } } - q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"])); + $fields = ['last_poco_query' => Temporal::utcNow()]; + dba::update('gserver', $fields, ['nurl' => $server["nurl"]]); return true; } else { @@ -1433,7 +1535,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' => Temporal::utcNow()]; + dba::update('gserver', $fields, ['nurl' => $server["nurl"]]); return false; } @@ -1458,7 +1561,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' => Temporal::utcNow()]; + dba::update('gserver', $fields, ['nurl' => $server["nurl"]]); continue; } @@ -1472,7 +1576,7 @@ class PortableContact } } - public static function discoverServerUsers($data, $server) + private static function discoverServerUsers($data, $server) { if (!isset($data->entry)) { return; @@ -1495,7 +1599,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); } @@ -1503,7 +1607,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; @@ -1550,7 +1654,7 @@ class PortableContact } if (isset($entry->updated)) { - $updated = date("Y-m-d H:i:s", strtotime($entry->updated)); + $updated = date(Temporal::MYSQL, strtotime($entry->updated)); } if (isset($entry->network)) { @@ -1588,7 +1692,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, @@ -1599,7 +1703,7 @@ class PortableContact "connect" => $connect_url, "updated" => $updated, "contact-type" => $contact_type, - "generation" => $generation); + "generation" => $generation]; try { $gcontact = GContact::sanitize($gcontact);