X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FProtocol%2FPortableContact.php;h=4eaf5bbb6c74c76e4eda74763a6478f42f16831f;hb=93daf7883e98d797759fb81dbf12e9e368debf61;hp=f2def21ec1ee2c11c8aca503ec441d12a1eb610e;hpb=2e420a15f8f3e469f9ea3fd298b1283d9806698e;p=friendica.git diff --git a/src/Protocol/PortableContact.php b/src/Protocol/PortableContact.php index f2def21ec1..4eaf5bbb6c 100644 --- a/src/Protocol/PortableContact.php +++ b/src/Protocol/PortableContact.php @@ -9,21 +9,22 @@ namespace Friendica\Protocol; +use Friendica\Content\Text\HTML; use Friendica\Core\Config; use Friendica\Core\Worker; use Friendica\Database\DBM; use Friendica\Model\GContact; use Friendica\Model\Profile; use Friendica\Network\Probe; +use Friendica\Util\DateTimeFormat; use Friendica\Util\Network; +use Friendica\Protocol\Diaspora; use dba; use DOMDocument; use DOMXPath; use Exception; require_once 'include/dba.php'; -require_once 'include/datetime.php'; -require_once 'include/html2bbcode.php'; class PortableContact { @@ -143,7 +144,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)) { @@ -155,7 +156,7 @@ class PortableContact } if (isset($entry->aboutMe)) { - $about = html2bbcode($entry->aboutMe); + $about = HTML::toBBCode($entry->aboutMe); } if (isset($entry->gender)) { @@ -314,9 +315,10 @@ class PortableContact $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)); } @@ -337,7 +339,7 @@ class PortableContact if ($server_url != "") { if (!self::checkServer($server_url, $gcontacts[0]["network"], $force)) { if ($force) { - $fields = ['last_failure' => datetime_convert()]; + $fields = ['last_failure' => DateTimeFormat::utcNow()]; dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]); } @@ -411,14 +413,14 @@ 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); GContact::update($contact); if (trim($noscrape["updated"]) != "") { - $fields = ['last_contact' => datetime_convert()]; + $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); @@ -467,7 +469,7 @@ class PortableContact } if (($data["poll"] == "") || (in_array($data["network"], [NETWORK_FEED, NETWORK_PHANTOM]))) { - $fields = ['last_failure' => datetime_convert()]; + $fields = ['last_failure' => DateTimeFormat::utcNow()]; dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]); logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG); @@ -483,7 +485,7 @@ class PortableContact $feedret = Network::curl($data["poll"]); if (!$feedret["success"]) { - $fields = ['last_failure' => datetime_convert()]; + $fields = ['last_failure' => DateTimeFormat::utcNow()]; dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]); logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG); @@ -501,14 +503,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 @@ -517,7 +521,13 @@ class PortableContact $last_updated = NULL_DATE; } } - $fields = ['updated' => DBM::date($last_updated), 'last_contact' => DBM::date()]; + + $fields = ['last_contact' => DateTimeFormat::utcNow()]; + + if (!empty($last_updated)) { + $fields['updated'] = $last_updated; + } + dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]); if (($gcontacts[0]["generation"] == 0)) { @@ -527,12 +537,12 @@ class PortableContact 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); @@ -646,30 +656,51 @@ class PortableContact return false; } - $nodeinfo_url = ''; + $nodeinfo1_url = ''; + $nodeinfo2_url = ''; foreach ($nodeinfo->links as $link) { if ($link->rel == 'http://nodeinfo.diaspora.software/ns/schema/1.0') { - $nodeinfo_url = $link->href; + $nodeinfo1_url = $link->href; + } + if ($link->rel == 'http://nodeinfo.diaspora.software/ns/schema/2.0') { + $nodeinfo2_url = $link->href; } } - if ($nodeinfo_url == '') { + if ($nodeinfo1_url . $nodeinfo2_url == '') { return false; } + $server = []; + + // When the nodeinfo url isn't on the same host, then there is obviously something wrong + if (!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 (parse_url($server_url, PHP_URL_HOST) != parse_url($nodeinfo_url, PHP_URL_HOST)) { - return false; + if (empty($server) && !empty($nodeinfo1_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo1_url, PHP_URL_HOST))) { + $server = self::parseNodeinfo1($nodeinfo1_url); } + return $server; + } + + /** + * @brief Parses Nodeinfo 1 + * + * @param string $nodeinfo_url address of the nodeinfo path + * @return array Server data + */ + private static function parseNodeinfo1($nodeinfo_url) + { $serverret = Network::curl($nodeinfo_url); if (!$serverret["success"]) { return false; } $nodeinfo = json_decode($serverret['body']); - if (!is_object($nodeinfo)) { return false; } @@ -740,6 +771,90 @@ class PortableContact return $server; } + /** + * @brief Parses Nodeinfo 2 + * + * @param string $nodeinfo_url address of the nodeinfo path + * @return array Server data + */ + private static function parseNodeinfo2($nodeinfo_url) + { + $serverret = Network::curl($nodeinfo_url); + if (!$serverret["success"]) { + return false; + } + + $nodeinfo = json_decode($serverret['body']); + if (!is_object($nodeinfo)) { + return false; + } + + $server = []; + + $server['register_policy'] = REGISTER_CLOSED; + + if (is_bool($nodeinfo->openRegistrations) && $nodeinfo->openRegistrations) { + $server['register_policy'] = REGISTER_OPEN; + } + + if (is_object($nodeinfo->software)) { + if (isset($nodeinfo->software->name)) { + $server['platform'] = $nodeinfo->software->name; + } + + if (isset($nodeinfo->software->version)) { + $server['version'] = $nodeinfo->software->version; + // Version numbers on Nodeinfo are presented with additional info, e.g.: + // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191. + $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']); + } + } + + if (is_object($nodeinfo->metadata)) { + if (isset($nodeinfo->metadata->nodeName)) { + $server['site_name'] = $nodeinfo->metadata->nodeName; + } + } + + if (!empty($nodeinfo->usage->users->total)) { + $server['registered-users'] = $nodeinfo->usage->users->total; + } + + $diaspora = false; + $friendica = false; + $gnusocial = false; + + if (is_array($nodeinfo->protocols)) { + foreach ($nodeinfo->protocols as $protocol) { + if ($protocol == 'diaspora') { + $diaspora = true; + } + if ($protocol == 'friendica') { + $friendica = true; + } + if ($protocol == 'gnusocial') { + $gnusocial = true; + } + } + } + + if ($gnusocial) { + $server['network'] = NETWORK_OSTATUS; + } + if ($diaspora) { + $server['network'] = NETWORK_DIASPORA; + } + if ($friendica) { + $server['network'] = NETWORK_DFRN; + } + + if (!$server) { + return false; + } + + return $server; + } + /** * @brief Detect server type (Hubzilla or Friendica) via the front page body * @@ -816,7 +931,7 @@ class PortableContact $gserver = dba::selectFirst('gserver', [], ['nurl' => normalise_link($server_url)]); if (DBM::is_result($gserver)) { if ($gserver["created"] <= NULL_DATE) { - $fields = ['created' => datetime_convert()]; + $fields = ['created' => DateTimeFormat::utcNow()]; $condition = ['nurl' => normalise_link($server_url)]; dba::update('gserver', $fields, $condition); } @@ -836,6 +951,15 @@ class PortableContact $register_policy = $gserver["register_policy"]; $registered_users = $gserver["registered-users"]; + // 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); @@ -863,7 +987,7 @@ class PortableContact // Mastodon uses the "@" for user profiles. // But this can be misunderstood. if (parse_url($server_url, PHP_URL_USER) != '') { - dba::update('gserver', ['last_failure' => datetime_convert()], ['nurl' => normalise_link($server_url)]); + dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]); return false; } @@ -879,7 +1003,7 @@ class PortableContact 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', ['last_failure' => datetime_convert()], ['nurl' => normalise_link($server_url)]); + dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]); return false; } @@ -894,7 +1018,7 @@ class PortableContact // 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', ['last_failure' => datetime_convert()], ['nurl' => normalise_link($server_url)]); + dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]); return false; } @@ -1193,24 +1317,12 @@ 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; - $register_policy_str = $data->register_policy; + $register_policy = constant($data->register_policy); $platform = $data->platform; - - switch ($register_policy_str) { - case "REGISTER_CLOSED": - $register_policy = REGISTER_CLOSED; - break; - case "REGISTER_APPROVE": - $register_policy = REGISTER_APPROVE; - break; - case "REGISTER_OPEN": - $register_policy = REGISTER_OPEN; - break; - } } } } @@ -1226,9 +1338,9 @@ class PortableContact 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; } @@ -1256,14 +1368,85 @@ class PortableContact dba::update('gserver', $fields, ['nurl' => normalise_link($server_url)]); } elseif (!$failure) { $fields['nurl'] = normalise_link($server_url); - $fields['created'] = datetime_convert(); + $fields['created'] = DateTimeFormat::utcNow(); dba::insert('gserver', $fields); } + + if (!$failure && in_array($fields['network'], [NETWORK_DFRN, NETWORK_DIASPORA])) { + self::discoverRelay($server_url); + } + logger("End discovery for server " . $server_url, LOGGER_DEBUG); return !$failure; } + /** + * @brief Fetch relay data from a given server url + * + * @param string $server_url address of the server + */ + 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', 'relay-subscribe', 'relay-scope'], ['nurl' => normalise_link($server_url)]); + if (!DBM::is_result($gserver)) { + return; + } + + if (($gserver['relay-subscribe'] != $data->subscribe) || ($gserver['relay-scope'] != $data->scope)) { + $fields = ['relay-subscribe' => $data->subscribe, 'relay-scope' => $data->scope]; + dba::update('gserver', $fields, ['id' => $gserver['id']]); + } + + dba::delete('gserver-tag', ['gserver-id' => $gserver['id']]); + if ($data->scope == 'tags') { + // Avoid duplicates + $tags = []; + foreach ($data->tags as $tag) { + $tag = mb_strtolower($tag); + $tags[$tag] = $tag; + } + + foreach ($tags as $tag) { + dba::insert('gserver-tag', ['gserver-id' => $gserver['id'], 'tag' => $tag], true); + } + } + + // Create or update the relay contact + $fields = []; + if (isset($data->protocols)) { + if (isset($data->protocols->diaspora)) { + $fields['network'] = NETWORK_DIASPORA; + if (isset($data->protocols->diaspora->receive)) { + $fields['batch'] = $data->protocols->diaspora->receive; + } elseif (is_string($data->protocols->diaspora)) { + $fields['batch'] = $data->protocols->diaspora; + } + } + if (isset($data->protocols->dfrn)) { + $fields['network'] = NETWORK_DFRN; + if (isset($data->protocols->dfrn->receive)) { + $fields['batch'] = $data->protocols->dfrn->receive; + } elseif (is_string($data->protocols->dfrn)) { + $fields['batch'] = $data->protocols->dfrn; + } + } + } + Diaspora::setRelayContact($server_url, $fields); + } + /** * @brief Returns a list of all known servers * @return array List of server urls @@ -1327,13 +1510,15 @@ 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); - foreach ($servers->pods as $server) { - Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", "https://".$server->host); + if (is_array($servers->pods)) { + foreach ($servers->pods as $server) { + Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", "https://".$server->host); + } } } @@ -1345,8 +1530,8 @@ class PortableContact $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) { + $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); } @@ -1401,7 +1586,7 @@ 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"; @@ -1420,7 +1605,7 @@ class PortableContact } } - $fields = ['last_poco_query' => datetime_convert()]; + $fields = ['last_poco_query' => DateTimeFormat::utcNow()]; dba::update('gserver', $fields, ['nurl' => $server["nurl"]]); return true; @@ -1429,7 +1614,7 @@ class PortableContact self::checkServer($server["url"], $server["network"], true); // If we couldn't reach the server, we will try it some time later - $fields = ['last_poco_query' => datetime_convert()]; + $fields = ['last_poco_query' => DateTimeFormat::utcNow()]; dba::update('gserver', $fields, ['nurl' => $server["nurl"]]); return false; @@ -1455,7 +1640,7 @@ 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 - $fields = ['last_poco_query' => datetime_convert()]; + $fields = ['last_poco_query' => DateTimeFormat::utcNow()]; dba::update('gserver', $fields, ['nurl' => $server["nurl"]]); continue; } @@ -1548,7 +1733,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)) { @@ -1560,7 +1745,7 @@ class PortableContact } if (isset($entry->aboutMe)) { - $about = html2bbcode($entry->aboutMe); + $about = HTML::toBBCode($entry->aboutMe); } if (isset($entry->gender)) {