X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=src%2FProtocol%2FPortableContact.php;h=aea68eb998e9bd9043e528f0bc516c1fefe8d45c;hb=8aff8a76eb9efc4acaab0af3c8c0ca7a011f4349;hp=fb9a725f082b7008de6565b06acf5d317dcbe66d;hpb=bd9cb4595deaa3ac20e538e7d50f832c9b710d78;p=friendica.git diff --git a/src/Protocol/PortableContact.php b/src/Protocol/PortableContact.php index fb9a725f08..aea68eb998 100644 --- a/src/Protocol/PortableContact.php +++ b/src/Protocol/PortableContact.php @@ -16,6 +16,7 @@ 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; @@ -143,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)) { @@ -314,7 +315,7 @@ class PortableContact $contact = ["url" => $profile]; if ($gcontacts[0]["created"] <= NULL_DATE) { - $contact['created'] = datetime_convert(); + $contact['created'] = Temporal::utcNow(); } if ($force) { @@ -337,7 +338,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' => Temporal::utcNow()]; dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]); } @@ -411,14 +412,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"] = Temporal::utcNow(); $contact = array_merge($contact, $noscrape); GContact::update($contact); if (trim($noscrape["updated"]) != "") { - $fields = ['last_contact' => datetime_convert()]; + $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); @@ -467,7 +468,7 @@ class PortableContact } if (($data["poll"] == "") || (in_array($data["network"], [NETWORK_FEED, NETWORK_PHANTOM]))) { - $fields = ['last_failure' => datetime_convert()]; + $fields = ['last_failure' => Temporal::utcNow()]; dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]); logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG); @@ -483,7 +484,7 @@ class PortableContact $feedret = Network::curl($data["poll"]); if (!$feedret["success"]) { - $fields = ['last_failure' => datetime_convert()]; + $fields = ['last_failure' => Temporal::utcNow()]; dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]); logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG); @@ -532,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); @@ -646,30 +647,51 @@ class PortableContact return false; } - $nodeinfo_url = ''; + $nodeinfo1_url = ''; + $nodeinfo2_url = ''; foreach ($nodeinfo->links as $link) { if ($link->rel == 'http://nodeinfo.diaspora.software/ns/schema/1.0') { - $nodeinfo_url = $link->href; + $nodeinfo1_url = $link->href; + } + if ($link->rel == 'http://nodeinfo.diaspora.software/ns/schema/2.0') { + $nodeinfo2_url = $link->href; } } - if ($nodeinfo_url == '') { + if ($nodeinfo1_url . $nodeinfo2_url == '') { return false; } + $server = []; + // When the nodeinfo url isn't on the same host, then there is obviously something wrong - if (parse_url($server_url, PHP_URL_HOST) != parse_url($nodeinfo_url, PHP_URL_HOST)) { - return false; + if (!empty($nodeinfo2_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo2_url, PHP_URL_HOST))) { + $server = self::parseNodeinfo2($nodeinfo2_url); + } + + // When the nodeinfo url isn't on the same host, then there is obviously something wrong + if (empty($server) && !empty($nodeinfo1_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo1_url, PHP_URL_HOST))) { + $server = self::parseNodeinfo1($nodeinfo1_url); } + return $server; + } + + /** + * @brief Parses Nodeinfo 1 + * + * @param string $nodeinfo_url address of the nodeinfo path + * @return array Server data + */ + private static function parseNodeinfo1($nodeinfo_url) + { $serverret = Network::curl($nodeinfo_url); if (!$serverret["success"]) { return false; } $nodeinfo = json_decode($serverret['body']); - if (!is_object($nodeinfo)) { return false; } @@ -740,6 +762,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 +922,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' => Temporal::utcNow()]; $condition = ['nurl' => normalise_link($server_url)]; dba::update('gserver', $fields, $condition); } @@ -863,7 +969,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' => Temporal::utcNow()], ['nurl' => normalise_link($server_url)]); return false; } @@ -879,7 +985,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' => Temporal::utcNow()], ['nurl' => normalise_link($server_url)]); return false; } @@ -894,7 +1000,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' => Temporal::utcNow()], ['nurl' => normalise_link($server_url)]); return false; } @@ -1226,9 +1332,9 @@ class PortableContact 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; } @@ -1256,7 +1362,7 @@ 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'] = Temporal::utcNow(); dba::insert('gserver', $fields); } logger("End discovery for server " . $server_url, LOGGER_DEBUG); @@ -1401,7 +1507,7 @@ 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"; @@ -1420,7 +1526,7 @@ class PortableContact } } - $fields = ['last_poco_query' => datetime_convert()]; + $fields = ['last_poco_query' => Temporal::utcNow()]; dba::update('gserver', $fields, ['nurl' => $server["nurl"]]); return true; @@ -1429,7 +1535,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' => Temporal::utcNow()]; dba::update('gserver', $fields, ['nurl' => $server["nurl"]]); return false; @@ -1455,7 +1561,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' => Temporal::utcNow()]; dba::update('gserver', $fields, ['nurl' => $server["nurl"]]); continue; } @@ -1548,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)) {