3 * @file src/Protocol/PortableContact.php
5 * @todo Move GNU Social URL schemata (http://server.tld/user/number) to http://server.tld/username
6 * @todo Fetch profile data from profile page for Redmatrix users
7 * @todo Detect if it is a forum
10 namespace Friendica\Protocol;
15 use Friendica\Content\Text\HTML;
16 use Friendica\Core\Config;
17 use Friendica\Core\Protocol;
18 use Friendica\Core\Worker;
19 use Friendica\Database\DBA;
20 use Friendica\Model\GContact;
21 use Friendica\Model\Profile;
22 use Friendica\Network\Probe;
23 use Friendica\Util\DateTimeFormat;
24 use Friendica\Util\Network;
25 use Friendica\Util\XML;
27 require_once 'include/dba.php';
32 * @brief Fetch POCO data
34 * @param integer $cid Contact ID
35 * @param integer $uid User ID
36 * @param integer $zcid Global Contact ID
37 * @param integer $url POCO address that should be polled
39 * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
40 * and add the entries to the gcontact (Global Contact) table, or update existing entries
41 * if anything (name or photo) has changed.
42 * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
44 * Once the global contact is stored add (if necessary) the contact linkage which associates
45 * the given uid, cid to the global contact entry. There can be many uid/cid combinations
46 * pointing to the same global contact id.
49 public static function loadWorker($cid, $uid = 0, $zcid = 0, $url = null)
51 // Call the function "load" via the worker
52 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "load", (int)$cid, (int)$uid, (int)$zcid, $url);
56 * @brief Fetch POCO data from the worker
58 * @param integer $cid Contact ID
59 * @param integer $uid User ID
60 * @param integer $zcid Global Contact ID
61 * @param integer $url POCO address that should be polled
64 public static function load($cid, $uid, $zcid, $url)
70 $contact = DBA::selectFirst('contact', ['poco', 'uid'], ['id' => $cid]);
71 if (DBA::isResult($contact)) {
72 $url = $contact['poco'];
73 $uid = $contact['uid'];
85 $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation' : '?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation') ;
87 logger('load: ' . $url, LOGGER_DEBUG);
89 $s = Network::fetchUrl($url);
91 logger('load: returns ' . $s, LOGGER_DATA);
93 logger('load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
95 if (($a->get_curl_code() > 299) || (! $s)) {
99 $j = json_decode($s, true);
101 logger('load: json: ' . print_r($j, true), LOGGER_DATA);
103 if (!isset($j['entry'])) {
108 foreach ($j['entry'] as $entry) {
115 $updated = NULL_DATE;
123 if (!empty($entry['displayName'])) {
124 $name = $entry['displayName'];
127 if (isset($entry['urls'])) {
128 foreach ($entry['urls'] as $url) {
129 if ($url['type'] == 'profile') {
130 $profile_url = $url['value'];
133 if ($url['type'] == 'webfinger') {
134 $connect_url = str_replace('acct:', '', $url['value']);
139 if (isset($entry['photos'])) {
140 foreach ($entry['photos'] as $photo) {
141 if ($photo['type'] == 'profile') {
142 $profile_photo = $photo['value'];
148 if (isset($entry['updated'])) {
149 $updated = date(DateTimeFormat::MYSQL, strtotime($entry['updated']));
152 if (isset($entry['network'])) {
153 $network = $entry['network'];
156 if (isset($entry['currentLocation'])) {
157 $location = $entry['currentLocation'];
160 if (isset($entry['aboutMe'])) {
161 $about = HTML::toBBCode($entry['aboutMe']);
164 if (isset($entry['gender'])) {
165 $gender = $entry['gender'];
168 if (isset($entry['generation']) && ($entry['generation'] > 0)) {
169 $generation = ++$entry['generation'];
172 if (isset($entry['tags'])) {
173 foreach ($entry['tags'] as $tag) {
174 $keywords = implode(", ", $tag);
178 if (isset($entry['contactType']) && ($entry['contactType'] >= 0)) {
179 $contact_type = $entry['contactType'];
182 $gcontact = ["url" => $profile_url,
184 "network" => $network,
185 "photo" => $profile_photo,
187 "location" => $location,
189 "keywords" => $keywords,
190 "connect" => $connect_url,
191 "updated" => $updated,
192 "contact-type" => $contact_type,
193 "generation" => $generation];
196 $gcontact = GContact::sanitize($gcontact);
197 $gcid = GContact::update($gcontact);
199 GContact::link($gcid, $uid, $cid, $zcid);
200 } catch (Exception $e) {
201 logger($e->getMessage(), LOGGER_DEBUG);
204 logger("load: loaded $total entries", LOGGER_DEBUG);
206 $condition = ["`cid` = ? AND `uid` = ? AND `zcid` = ? AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY", $cid, $uid, $zcid];
207 DBA::delete('glink', $condition);
210 public static function reachable($profile, $server = "", $network = "", $force = false)
213 $server = self::detectServer($profile);
220 return self::checkServer($server, $network, $force);
223 public static function detectServer($profile)
225 // Try to detect the server path based upon some known standard paths
228 if ($server_url == "") {
229 $friendica = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $profile);
230 if ($friendica != $profile) {
231 $server_url = $friendica;
232 $network = Protocol::DFRN;
236 if ($server_url == "") {
237 $diaspora = preg_replace("=(https?://)(.*)/u/(.*)=ism", "$1$2", $profile);
238 if ($diaspora != $profile) {
239 $server_url = $diaspora;
240 $network = Protocol::DIASPORA;
244 if ($server_url == "") {
245 $red = preg_replace("=(https?://)(.*)/channel/(.*)=ism", "$1$2", $profile);
246 if ($red != $profile) {
248 $network = Protocol::DIASPORA;
253 if ($server_url == "") {
254 $mastodon = preg_replace("=(https?://)(.*)/users/(.*)=ism", "$1$2", $profile);
255 if ($mastodon != $profile) {
256 $server_url = $mastodon;
257 $network = Protocol::OSTATUS;
261 // Numeric OStatus variant
262 if ($server_url == "") {
263 $ostatus = preg_replace("=(https?://)(.*)/user/(.*)=ism", "$1$2", $profile);
264 if ($ostatus != $profile) {
265 $server_url = $ostatus;
266 $network = Protocol::OSTATUS;
271 if ($server_url == "") {
272 $base = preg_replace("=(https?://)(.*?)/(.*)=ism", "$1$2", $profile);
273 if ($base != $profile) {
275 $network = Protocol::PHANTOM;
279 if ($server_url == "") {
284 "SELECT `id` FROM `gserver` WHERE `nurl` = '%s' AND `last_contact` > `last_failure`",
285 DBA::escape(normalise_link($server_url))
288 if (DBA::isResult($r)) {
292 // Fetch the host-meta to check if this really is a server
293 $serverret = Network::curl($server_url."/.well-known/host-meta");
294 if (!$serverret["success"]) {
301 public static function alternateOStatusUrl($url)
303 return(preg_match("=https?://.+/user/\d+=ism", $url, $matches));
306 public static function lastUpdated($profile, $force = false)
309 "SELECT * FROM `gcontact` WHERE `nurl` = '%s'",
310 DBA::escape(normalise_link($profile))
313 if (!DBA::isResult($gcontacts)) {
317 $contact = ["url" => $profile];
319 if ($gcontacts[0]["created"] <= NULL_DATE) {
320 $contact['created'] = DateTimeFormat::utcNow();
325 $server_url = normalise_link(self::detectServer($profile));
328 if (($server_url == '') && ($gcontacts[0]["server_url"] != "")) {
329 $server_url = $gcontacts[0]["server_url"];
332 if (!$force && (($server_url == '') || ($gcontacts[0]["server_url"] == $gcontacts[0]["nurl"]))) {
333 $server_url = normalise_link(self::detectServer($profile));
336 if (!in_array($gcontacts[0]["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::FEED, Protocol::OSTATUS, ""])) {
337 logger("Profile ".$profile.": Network type ".$gcontacts[0]["network"]." can't be checked", LOGGER_DEBUG);
341 if ($server_url != "") {
342 if (!self::checkServer($server_url, $gcontacts[0]["network"], $force)) {
344 $fields = ['last_failure' => DateTimeFormat::utcNow()];
345 DBA::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
348 logger("Profile ".$profile.": Server ".$server_url." wasn't reachable.", LOGGER_DEBUG);
351 $contact['server_url'] = $server_url;
354 if (in_array($gcontacts[0]["network"], ["", Protocol::FEED])) {
356 "SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''",
357 DBA::escape(normalise_link($server_url))
361 $contact['network'] = $server[0]["network"];
367 // noscrape is really fast so we don't cache the call.
368 if (($server_url != "") && ($gcontacts[0]["nick"] != "")) {
369 // Use noscrape if possible
370 $server = q("SELECT `noscrape`, `network` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", DBA::escape(normalise_link($server_url)));
373 $noscraperet = Network::curl($server[0]["noscrape"]."/".$gcontacts[0]["nick"]);
375 if ($noscraperet["success"] && ($noscraperet["body"] != "")) {
376 $noscrape = json_decode($noscraperet["body"], true);
378 if (is_array($noscrape)) {
379 $contact["network"] = $server[0]["network"];
381 if (isset($noscrape["fn"])) {
382 $contact["name"] = $noscrape["fn"];
384 if (isset($noscrape["comm"])) {
385 $contact["community"] = $noscrape["comm"];
387 if (isset($noscrape["tags"])) {
388 $keywords = implode(" ", $noscrape["tags"]);
389 if ($keywords != "") {
390 $contact["keywords"] = $keywords;
394 $location = Profile::formatLocation($noscrape);
396 $contact["location"] = $location;
398 if (isset($noscrape["dfrn-notify"])) {
399 $contact["notify"] = $noscrape["dfrn-notify"];
401 // Remove all fields that are not present in the gcontact table
402 unset($noscrape["fn"]);
403 unset($noscrape["key"]);
404 unset($noscrape["homepage"]);
405 unset($noscrape["comm"]);
406 unset($noscrape["tags"]);
407 unset($noscrape["locality"]);
408 unset($noscrape["region"]);
409 unset($noscrape["country-name"]);
410 unset($noscrape["contacts"]);
411 unset($noscrape["dfrn-request"]);
412 unset($noscrape["dfrn-confirm"]);
413 unset($noscrape["dfrn-notify"]);
414 unset($noscrape["dfrn-poll"]);
416 // Set the date of the last contact
417 /// @todo By now the function "update_gcontact" doesn't work with this field
418 //$contact["last_contact"] = DateTimeFormat::utcNow();
420 $contact = array_merge($contact, $noscrape);
422 GContact::update($contact);
424 if (!empty($noscrape["updated"])) {
425 $fields = ['last_contact' => DateTimeFormat::utcNow()];
426 DBA::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
428 logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG);
430 return $noscrape["updated"];
437 // If we only can poll the feed, then we only do this once a while
438 if (!$force && !self::updateNeeded($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) {
439 logger("Profile ".$profile." was last updated at ".$gcontacts[0]["updated"]." (cached)", LOGGER_DEBUG);
441 GContact::update($contact);
442 return $gcontacts[0]["updated"];
445 $data = Probe::uri($profile);
447 // Is the profile link the alternate OStatus link notation? (http://domain.tld/user/4711)
448 // Then check the other link and delete this one
449 if (($data["network"] == Protocol::OSTATUS) && self::alternateOStatusUrl($profile)
450 && (normalise_link($profile) == normalise_link($data["alias"]))
451 && (normalise_link($profile) != normalise_link($data["url"]))
453 // Delete the old entry
454 DBA::delete('gcontact', ['nurl' => normalise_link($profile)]);
456 $gcontact = array_merge($gcontacts[0], $data);
458 $gcontact["server_url"] = $data["baseurl"];
461 $gcontact = GContact::sanitize($gcontact);
462 GContact::update($gcontact);
464 self::lastUpdated($data["url"], $force);
465 } catch (Exception $e) {
466 logger($e->getMessage(), LOGGER_DEBUG);
469 logger("Profile ".$profile." was deleted", LOGGER_DEBUG);
473 if (($data["poll"] == "") || (in_array($data["network"], [Protocol::FEED, Protocol::PHANTOM]))) {
474 $fields = ['last_failure' => DateTimeFormat::utcNow()];
475 DBA::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
477 logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG);
481 $contact = array_merge($contact, $data);
483 $contact["server_url"] = $data["baseurl"];
485 GContact::update($contact);
487 $feedret = Network::curl($data["poll"]);
489 if (!$feedret["success"]) {
490 $fields = ['last_failure' => DateTimeFormat::utcNow()];
491 DBA::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
493 logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG);
497 $doc = new DOMDocument();
498 /// @TODO Avoid error supression here
499 @$doc->loadXML($feedret["body"]);
501 $xpath = new DOMXPath($doc);
502 $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
504 $entries = $xpath->query('/atom:feed/atom:entry');
508 foreach ($entries as $entry) {
509 $published = DateTimeFormat::utc($xpath->query('atom:published/text()', $entry)->item(0)->nodeValue);
510 $updated = DateTimeFormat::utc($xpath->query('atom:updated/text()' , $entry)->item(0)->nodeValue);
512 if ($last_updated < $published) {
513 $last_updated = $published;
516 if ($last_updated < $updated) {
517 $last_updated = $updated;
521 // Maybe there aren't any entries. Then check if it is a valid feed
522 if ($last_updated == "") {
523 if ($xpath->query('/atom:feed')->length > 0) {
524 $last_updated = NULL_DATE;
528 $fields = ['last_contact' => DateTimeFormat::utcNow()];
530 if (!empty($last_updated)) {
531 $fields['updated'] = $last_updated;
534 DBA::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
536 if (($gcontacts[0]["generation"] == 0)) {
537 $fields = ['generation' => 9];
538 DBA::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
541 logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG);
543 return $last_updated;
546 public static function updateNeeded($created, $updated, $last_failure, $last_contact)
548 $now = strtotime(DateTimeFormat::utcNow());
550 if ($updated > $last_contact) {
551 $contact_time = strtotime($updated);
553 $contact_time = strtotime($last_contact);
556 $failure_time = strtotime($last_failure);
557 $created_time = strtotime($created);
559 // If there is no "created" time then use the current time
560 if ($created_time <= 0) {
561 $created_time = $now;
564 // If the last contact was less than 24 hours then don't update
565 if (($now - $contact_time) < (60 * 60 * 24)) {
569 // If the last failure was less than 24 hours then don't update
570 if (($now - $failure_time) < (60 * 60 * 24)) {
574 // If the last contact was less than a week ago and the last failure is older than a week then don't update
575 //if ((($now - $contact_time) < (60 * 60 * 24 * 7)) && ($contact_time > $failure_time))
578 // If the last contact time was more than a week ago and the contact was created more than a week ago, then only try once a week
579 if ((($now - $contact_time) > (60 * 60 * 24 * 7)) && (($now - $created_time) > (60 * 60 * 24 * 7)) && (($now - $failure_time) < (60 * 60 * 24 * 7))) {
583 // If the last contact time was more than a month ago and the contact was created more than a month ago, then only try once a month
584 if ((($now - $contact_time) > (60 * 60 * 24 * 30)) && (($now - $created_time) > (60 * 60 * 24 * 30)) && (($now - $failure_time) < (60 * 60 * 24 * 30))) {
591 /// @TODO Maybe move this out to an utilities class?
592 private static function toBoolean($val)
594 if (($val == "true") || ($val == 1)) {
596 } elseif (($val == "false") || ($val == 0)) {
604 * @brief Detect server type (Hubzilla or Friendica) via the poco data
606 * @param array $data POCO data
607 * @return array Server data
609 private static function detectPocoData(array $data)
613 if (!isset($data['entry'])) {
617 if (count($data['entry']) == 0) {
621 if (!isset($data['entry'][0]['urls'])) {
625 if (count($data['entry'][0]['urls']) == 0) {
629 foreach ($data['entry'][0]['urls'] as $url) {
630 if ($url['type'] == 'zot') {
632 $server["platform"] = 'Hubzilla';
633 $server["network"] = Protocol::DIASPORA;
641 * @brief Detect server type by using the nodeinfo data
643 * @param string $server_url address of the server
644 * @return array Server data
646 private static function fetchNodeinfo($server_url)
648 $serverret = Network::curl($server_url."/.well-known/nodeinfo");
649 if (!$serverret["success"]) {
653 $nodeinfo = json_decode($serverret['body'], true);
655 if (!is_array($nodeinfo) || !isset($nodeinfo['links'])) {
662 foreach ($nodeinfo['links'] as $link) {
663 if (!is_array($link) || empty($link['rel'])) {
664 logger('Invalid nodeinfo format for ' . $server_url, LOGGER_DEBUG);
667 if ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/1.0') {
668 $nodeinfo1_url = $link['href'];
669 } elseif ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/2.0') {
670 $nodeinfo2_url = $link['href'];
674 if ($nodeinfo1_url . $nodeinfo2_url == '') {
680 // When the nodeinfo url isn't on the same host, then there is obviously something wrong
681 if (!empty($nodeinfo2_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo2_url, PHP_URL_HOST))) {
682 $server = self::parseNodeinfo2($nodeinfo2_url);
685 // When the nodeinfo url isn't on the same host, then there is obviously something wrong
686 if (empty($server) && !empty($nodeinfo1_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo1_url, PHP_URL_HOST))) {
687 $server = self::parseNodeinfo1($nodeinfo1_url);
694 * @brief Parses Nodeinfo 1
696 * @param string $nodeinfo_url address of the nodeinfo path
697 * @return array Server data
699 private static function parseNodeinfo1($nodeinfo_url)
701 $serverret = Network::curl($nodeinfo_url);
703 if (!$serverret["success"]) {
707 $nodeinfo = json_decode($serverret['body'], true);
709 if (!is_array($nodeinfo)) {
715 $server['register_policy'] = REGISTER_CLOSED;
717 if (is_bool($nodeinfo['openRegistrations']) && $nodeinfo['openRegistrations']) {
718 $server['register_policy'] = REGISTER_OPEN;
721 if (is_array($nodeinfo['software'])) {
722 if (isset($nodeinfo['software']['name'])) {
723 $server['platform'] = $nodeinfo['software']['name'];
726 if (isset($nodeinfo['software']['version'])) {
727 $server['version'] = $nodeinfo['software']['version'];
728 // Version numbers on Nodeinfo are presented with additional info, e.g.:
729 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
730 $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']);
734 if (is_array($nodeinfo['metadata']) && isset($nodeinfo['metadata']['nodeName'])) {
735 $server['site_name'] = $nodeinfo['metadata']['nodeName'];
738 if (!empty($nodeinfo['usage']['users']['total'])) {
739 $server['registered-users'] = $nodeinfo['usage']['users']['total'];
746 if (is_array($nodeinfo['protocols']['inbound'])) {
747 foreach ($nodeinfo['protocols']['inbound'] as $inbound) {
748 if ($inbound == 'diaspora') {
751 if ($inbound == 'friendica') {
754 if ($inbound == 'gnusocial') {
761 $server['network'] = Protocol::OSTATUS;
764 $server['network'] = Protocol::DIASPORA;
767 $server['network'] = Protocol::DFRN;
778 * @brief Parses Nodeinfo 2
780 * @param string $nodeinfo_url address of the nodeinfo path
781 * @return array Server data
783 private static function parseNodeinfo2($nodeinfo_url)
785 $serverret = Network::curl($nodeinfo_url);
786 if (!$serverret["success"]) {
790 $nodeinfo = json_decode($serverret['body'], true);
792 if (!is_array($nodeinfo)) {
798 $server['register_policy'] = REGISTER_CLOSED;
800 if (is_bool($nodeinfo['openRegistrations']) && $nodeinfo['openRegistrations']) {
801 $server['register_policy'] = REGISTER_OPEN;
804 if (is_array($nodeinfo['software'])) {
805 if (isset($nodeinfo['software']['name'])) {
806 $server['platform'] = $nodeinfo['software']['name'];
809 if (isset($nodeinfo['software']['version'])) {
810 $server['version'] = $nodeinfo['software']['version'];
811 // Version numbers on Nodeinfo are presented with additional info, e.g.:
812 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
813 $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']);
817 if (is_array($nodeinfo['metadata']) && isset($nodeinfo['metadata']['nodeName'])) {
818 $server['site_name'] = $nodeinfo['metadata']['nodeName'];
821 if (!empty($nodeinfo['usage']['users']['total'])) {
822 $server['registered-users'] = $nodeinfo['usage']['users']['total'];
829 if (!empty($nodeinfo['protocols'])) {
830 foreach ($nodeinfo['protocols'] as $protocol) {
831 if ($protocol == 'diaspora') {
833 } elseif ($protocol == 'friendica') {
835 } elseif ($protocol == 'gnusocial') {
842 $server['network'] = Protocol::OSTATUS;
843 } elseif ($diaspora) {
844 $server['network'] = Protocol::DIASPORA;
845 } elseif ($friendica) {
846 $server['network'] = Protocol::DFRN;
849 if (empty($server)) {
857 * @brief Detect server type (Hubzilla or Friendica) via the front page body
859 * @param string $body Front page of the server
860 * @return array Server data
862 private static function detectServerType($body)
866 $doc = new DOMDocument();
867 /// @TODO Acoid supressing error
868 @$doc->loadHTML($body);
869 $xpath = new DOMXPath($doc);
871 $list = $xpath->query("//meta[@name]");
873 foreach ($list as $node) {
875 if ($node->attributes->length) {
876 foreach ($node->attributes as $attribute) {
877 $attr[$attribute->name] = $attribute->value;
880 if ($attr['name'] == 'generator') {
881 $version_part = explode(" ", $attr['content']);
882 if (count($version_part) == 2) {
883 if (in_array($version_part[0], ["Friendika", "Friendica"])) {
885 $server["platform"] = $version_part[0];
886 $server["version"] = $version_part[1];
887 $server["network"] = Protocol::DFRN;
894 $list = $xpath->query("//meta[@property]");
896 foreach ($list as $node) {
898 if ($node->attributes->length) {
899 foreach ($node->attributes as $attribute) {
900 $attr[$attribute->name] = $attribute->value;
903 if ($attr['property'] == 'generator' && in_array($attr['content'], ["hubzilla", "BlaBlaNet"])) {
905 $server["platform"] = $attr['content'];
906 $server["version"] = "";
907 $server["network"] = Protocol::DIASPORA;
916 $server["site_name"] = XML::getFirstNodeValue($xpath, '//head/title/text()');
921 public static function checkServer($server_url, $network = "", $force = false)
923 // Unify the server address
924 $server_url = trim($server_url, "/");
925 $server_url = str_replace("/index.php", "", $server_url);
927 if ($server_url == "") {
931 $gserver = DBA::selectFirst('gserver', [], ['nurl' => normalise_link($server_url)]);
932 if (DBA::isResult($gserver)) {
933 if ($gserver["created"] <= NULL_DATE) {
934 $fields = ['created' => DateTimeFormat::utcNow()];
935 $condition = ['nurl' => normalise_link($server_url)];
936 DBA::update('gserver', $fields, $condition);
938 $poco = $gserver["poco"];
939 $noscrape = $gserver["noscrape"];
941 if ($network == "") {
942 $network = $gserver["network"];
945 $last_contact = $gserver["last_contact"];
946 $last_failure = $gserver["last_failure"];
947 $version = $gserver["version"];
948 $platform = $gserver["platform"];
949 $site_name = $gserver["site_name"];
950 $info = $gserver["info"];
951 $register_policy = $gserver["register_policy"];
952 $registered_users = $gserver["registered-users"];
954 // See discussion under https://forum.friendi.ca/display/0b6b25a8135aabc37a5a0f5684081633
955 // It can happen that a zero date is in the database, but storing it again is forbidden.
956 if ($last_contact < NULL_DATE) {
957 $last_contact = NULL_DATE;
960 if ($last_failure < NULL_DATE) {
961 $last_failure = NULL_DATE;
964 if (!$force && !self::updateNeeded($gserver["created"], "", $last_failure, $last_contact)) {
965 logger("Use cached data for server ".$server_url, LOGGER_DEBUG);
966 return ($last_contact >= $last_failure);
975 $register_policy = -1;
976 $registered_users = 0;
978 $last_contact = NULL_DATE;
979 $last_failure = NULL_DATE;
981 logger("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$gserver["created"]." Failure: ".$last_failure." Contact: ".$last_contact, LOGGER_DEBUG);
984 $possible_failure = false;
985 $orig_last_failure = $last_failure;
986 $orig_last_contact = $last_contact;
988 // Mastodon uses the "@" for user profiles.
989 // But this can be misunderstood.
990 if (parse_url($server_url, PHP_URL_USER) != '') {
991 DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
995 // Check if the page is accessible via SSL.
996 $orig_server_url = $server_url;
997 $server_url = str_replace("http://", "https://", $server_url);
999 // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
1000 $serverret = Network::curl($server_url."/.well-known/host-meta", false, $redirects, ['timeout' => 20]);
1002 // Quit if there is a timeout.
1003 // But we want to make sure to only quit if we are mostly sure that this server url fits.
1004 if (DBA::isResult($gserver) && ($orig_server_url == $server_url) &&
1005 (!empty($serverret["errno"]) && ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT))) {
1006 logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
1007 DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
1011 // Maybe the page is unencrypted only?
1012 $xmlobj = @simplexml_load_string($serverret["body"], 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
1013 if (!$serverret["success"] || ($serverret["body"] == "") || empty($xmlobj) || !is_object($xmlobj)) {
1014 $server_url = str_replace("https://", "http://", $server_url);
1016 // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
1017 $serverret = Network::curl($server_url."/.well-known/host-meta", false, $redirects, ['timeout' => 20]);
1019 // Quit if there is a timeout
1020 if (!empty($serverret["errno"]) && ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT)) {
1021 logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
1022 DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
1026 $xmlobj = @simplexml_load_string($serverret["body"], 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
1029 if (!$serverret["success"] || ($serverret["body"] == "") || empty($xmlobj) || !is_object($xmlobj)) {
1030 // Workaround for bad configured servers (known nginx problem)
1031 if (!empty($serverret["debug"]) && !in_array($serverret["debug"]["http_code"], ["403", "404"])) {
1035 $possible_failure = true;
1038 // If the server has no possible failure we reset the cached data
1039 if (!$possible_failure) {
1044 $register_policy = -1;
1048 // This will be too low, but better than no value at all.
1049 $registered_users = DBA::count('gcontact', ['server_url' => normalise_link($server_url)]);
1054 $serverret = Network::curl($server_url."/poco");
1056 if ($serverret["success"]) {
1057 $data = json_decode($serverret["body"], true);
1059 if (isset($data['totalResults'])) {
1060 $registered_users = $data['totalResults'];
1061 $poco = $server_url . "/poco";
1062 $server = self::detectPocoData($data);
1064 if (!empty($server)) {
1065 $platform = $server['platform'];
1066 $network = $server['network'];
1073 * There are servers out there who don't return 404 on a failure
1074 * We have to be sure that don't misunderstand this
1076 if (is_null($data)) {
1085 // Test for Diaspora, Hubzilla, Mastodon or older Friendica servers
1086 $serverret = Network::curl($server_url);
1088 if (!$serverret["success"] || ($serverret["body"] == "")) {
1091 $server = self::detectServerType($serverret["body"]);
1093 if (!empty($server)) {
1094 $platform = $server['platform'];
1095 $network = $server['network'];
1096 $version = $server['version'];
1097 $site_name = $server['site_name'];
1100 $lines = explode("\n", $serverret["header"]);
1102 if (count($lines)) {
1103 foreach ($lines as $line) {
1104 $line = trim($line);
1106 if (stristr($line, 'X-Diaspora-Version:')) {
1107 $platform = "Diaspora";
1108 $version = trim(str_replace("X-Diaspora-Version:", "", $line));
1109 $version = trim(str_replace("x-diaspora-version:", "", $version));
1110 $network = Protocol::DIASPORA;
1111 $versionparts = explode("-", $version);
1112 $version = $versionparts[0];
1115 if (stristr($line, 'Server: Mastodon')) {
1116 $platform = "Mastodon";
1117 $network = Protocol::OSTATUS;
1124 if (!$failure && ($poco == "")) {
1125 // Test for Statusnet
1126 // Will also return data for Friendica and GNU Social - but it will be overwritten later
1127 // The "not implemented" is a special treatment for really, really old Friendica versions
1128 $serverret = Network::curl($server_url."/api/statusnet/version.json");
1130 if ($serverret["success"] && ($serverret["body"] != '{"error":"not implemented"}') &&
1131 ($serverret["body"] != '') && (strlen($serverret["body"]) < 30)) {
1132 $platform = "StatusNet";
1133 // Remove junk that some GNU Social servers return
1134 $version = str_replace(chr(239).chr(187).chr(191), "", $serverret["body"]);
1135 $version = trim($version, '"');
1136 $network = Protocol::OSTATUS;
1139 // Test for GNU Social
1140 $serverret = Network::curl($server_url."/api/gnusocial/version.json");
1142 if ($serverret["success"] && ($serverret["body"] != '{"error":"not implemented"}') &&
1143 ($serverret["body"] != '') && (strlen($serverret["body"]) < 30)) {
1144 $platform = "GNU Social";
1145 // Remove junk that some GNU Social servers return
1146 $version = str_replace(chr(239) . chr(187) . chr(191), "", $serverret["body"]);
1147 $version = trim($version, '"');
1148 $network = Protocol::OSTATUS;
1151 // Test for Mastodon
1152 $orig_version = $version;
1153 $serverret = Network::curl($server_url . "/api/v1/instance");
1155 if ($serverret["success"] && ($serverret["body"] != '')) {
1156 $data = json_decode($serverret["body"], true);
1158 if (isset($data['version'])) {
1159 $platform = "Mastodon";
1160 $version = defaults($data, 'version', '');
1161 $site_name = defaults($data, 'title', '');
1162 $info = defaults($data, 'description', '');
1163 $network = Protocol::OSTATUS;
1166 if (!empty($data['stats']['user_count'])) {
1167 $registered_users = $data['stats']['user_count'];
1171 if (strstr($orig_version . $version, 'Pleroma')) {
1172 $platform = 'Pleroma';
1173 $version = trim(str_replace('Pleroma', '', $version));
1178 // Test for Hubzilla and Red
1179 $serverret = Network::curl($server_url . "/siteinfo.json");
1181 if ($serverret["success"]) {
1182 $data = json_decode($serverret["body"], true);
1184 if (isset($data['url'])) {
1185 $platform = $data['platform'];
1186 $version = $data['version'];
1187 $network = Protocol::DIASPORA;
1190 if (!empty($data['site_name'])) {
1191 $site_name = $data['site_name'];
1194 if (!empty($data['channels_total'])) {
1195 $registered_users = $data['channels_total'];
1198 if (!empty($data['register_policy'])) {
1199 switch ($data['register_policy']) {
1200 case "REGISTER_OPEN":
1201 $register_policy = REGISTER_OPEN;
1204 case "REGISTER_APPROVE":
1205 $register_policy = REGISTER_APPROVE;
1208 case "REGISTER_CLOSED":
1210 $register_policy = REGISTER_CLOSED;
1215 // Test for Hubzilla, Redmatrix or Friendica
1216 $serverret = Network::curl($server_url."/api/statusnet/config.json");
1218 if ($serverret["success"]) {
1219 $data = json_decode($serverret["body"], true);
1221 if (isset($data['site']['server'])) {
1222 if (isset($data['site']['platform'])) {
1223 $platform = $data['site']['platform']['PLATFORM_NAME'];
1224 $version = $data['site']['platform']['STD_VERSION'];
1225 $network = Protocol::DIASPORA;
1228 if (isset($data['site']['BlaBlaNet'])) {
1229 $platform = $data['site']['BlaBlaNet']['PLATFORM_NAME'];
1230 $version = $data['site']['BlaBlaNet']['STD_VERSION'];
1231 $network = Protocol::DIASPORA;
1234 if (isset($data['site']['hubzilla'])) {
1235 $platform = $data['site']['hubzilla']['PLATFORM_NAME'];
1236 $version = $data['site']['hubzilla']['RED_VERSION'];
1237 $network = Protocol::DIASPORA;
1240 if (isset($data['site']['redmatrix'])) {
1241 if (isset($data['site']['redmatrix']['PLATFORM_NAME'])) {
1242 $platform = $data['site']['redmatrix']['PLATFORM_NAME'];
1243 } elseif (isset($data['site']['redmatrix']['RED_PLATFORM'])) {
1244 $platform = $data['site']['redmatrix']['RED_PLATFORM'];
1247 $version = $data['site']['redmatrix']['RED_VERSION'];
1248 $network = Protocol::DIASPORA;
1251 if (isset($data['site']['friendica'])) {
1252 $platform = $data['site']['friendica']['FRIENDICA_PLATFORM'];
1253 $version = $data['site']['friendica']['FRIENDICA_VERSION'];
1254 $network = Protocol::DFRN;
1257 $site_name = $data['site']['name'];
1260 $inviteonly = false;
1263 if (!empty($data['site']['closed'])) {
1264 $closed = self::toBoolean($data['site']['closed']);
1267 if (!empty($data['site']['private'])) {
1268 $private = self::toBoolean($data['site']['private']);
1271 if (!empty($data['site']['inviteonly'])) {
1272 $inviteonly = self::toBoolean($data['site']['inviteonly']);
1275 if (!$closed && !$private and $inviteonly) {
1276 $register_policy = REGISTER_APPROVE;
1277 } elseif (!$closed && !$private) {
1278 $register_policy = REGISTER_OPEN;
1280 $register_policy = REGISTER_CLOSED;
1287 // Query statistics.json. Optional package for Diaspora, Friendica and Redmatrix
1289 $serverret = Network::curl($server_url . "/statistics.json");
1291 if ($serverret["success"]) {
1292 $data = json_decode($serverret["body"], true);
1294 if (isset($data['version'])) {
1295 $version = $data['version'];
1296 // Version numbers on statistics.json are presented with additional info, e.g.:
1297 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
1298 $version = preg_replace("=(.+)-(.{4,})=ism", "$1", $version);
1301 if (!empty($data['name'])) {
1302 $site_name = $data['name'];
1305 if (!empty($data['network'])) {
1306 $platform = $data['network'];
1309 if ($platform == "Diaspora") {
1310 $network = Protocol::DIASPORA;
1313 if (!empty($data['registrations_open']) && $data['registrations_open']) {
1314 $register_policy = REGISTER_OPEN;
1316 $register_policy = REGISTER_CLOSED;
1321 // Query nodeinfo. Working for (at least) Diaspora and Friendica.
1323 $server = self::fetchNodeinfo($server_url);
1325 if (!empty($server)) {
1326 $register_policy = $server['register_policy'];
1328 if (isset($server['platform'])) {
1329 $platform = $server['platform'];
1332 if (isset($server['network'])) {
1333 $network = $server['network'];
1336 if (isset($server['version'])) {
1337 $version = $server['version'];
1340 if (isset($server['site_name'])) {
1341 $site_name = $server['site_name'];
1344 if (isset($server['registered-users'])) {
1345 $registered_users = $server['registered-users'];
1350 // Check for noscrape
1351 // Friendica servers could be detected as OStatus servers
1352 if (!$failure && in_array($network, [Protocol::DFRN, Protocol::OSTATUS])) {
1353 $serverret = Network::curl($server_url . "/friendica/json");
1355 if (!$serverret["success"]) {
1356 $serverret = Network::curl($server_url . "/friendika/json");
1359 if ($serverret["success"]) {
1360 $data = json_decode($serverret["body"], true);
1362 if (isset($data['version'])) {
1363 $network = Protocol::DFRN;
1365 if (!empty($data['no_scrape_url'])) {
1366 $noscrape = $data['no_scrape_url'];
1369 $version = $data['version'];
1371 if (!empty($data['site_name'])) {
1372 $site_name = $data['site_name'];
1375 $info = $data['info'];
1376 $register_policy = constant($data['register_policy']);
1377 $platform = $data['platform'];
1382 // Every server has got at least an admin account
1383 if (!$failure && ($registered_users == 0)) {
1384 $registered_users = 1;
1387 if ($possible_failure && !$failure) {
1392 $last_contact = $orig_last_contact;
1393 $last_failure = DateTimeFormat::utcNow();
1395 $last_contact = DateTimeFormat::utcNow();
1396 $last_failure = $orig_last_failure;
1399 if (($last_contact <= $last_failure) && !$failure) {
1400 logger("Server ".$server_url." seems to be alive, but last contact wasn't set - could be a bug", LOGGER_DEBUG);
1401 } elseif (($last_contact >= $last_failure) && $failure) {
1402 logger("Server ".$server_url." seems to be dead, but last failure wasn't set - could be a bug", LOGGER_DEBUG);
1405 // Check again if the server exists
1406 $found = DBA::exists('gserver', ['nurl' => normalise_link($server_url)]);
1408 $version = strip_tags($version);
1409 $site_name = strip_tags($site_name);
1410 $info = strip_tags($info);
1411 $platform = strip_tags($platform);
1413 $fields = ['url' => $server_url, 'version' => $version,
1414 'site_name' => $site_name, 'info' => $info, 'register_policy' => $register_policy,
1415 'poco' => $poco, 'noscrape' => $noscrape, 'network' => $network,
1416 'platform' => $platform, 'registered-users' => $registered_users,
1417 'last_contact' => $last_contact, 'last_failure' => $last_failure];
1420 DBA::update('gserver', $fields, ['nurl' => normalise_link($server_url)]);
1421 } elseif (!$failure) {
1422 $fields['nurl'] = normalise_link($server_url);
1423 $fields['created'] = DateTimeFormat::utcNow();
1424 DBA::insert('gserver', $fields);
1427 if (!$failure && in_array($fields['network'], [Protocol::DFRN, Protocol::DIASPORA])) {
1428 self::discoverRelay($server_url);
1431 logger("End discovery for server " . $server_url, LOGGER_DEBUG);
1437 * @brief Fetch relay data from a given server url
1439 * @param string $server_url address of the server
1441 private static function discoverRelay($server_url)
1443 logger("Discover relay data for server " . $server_url, LOGGER_DEBUG);
1445 $serverret = Network::curl($server_url . "/.well-known/x-social-relay");
1447 if (!$serverret["success"]) {
1451 $data = json_decode($serverret['body'], true);
1453 if (!is_array($data)) {
1457 $gserver = DBA::selectFirst('gserver', ['id', 'relay-subscribe', 'relay-scope'], ['nurl' => normalise_link($server_url)]);
1459 if (!DBA::isResult($gserver)) {
1463 if (($gserver['relay-subscribe'] != $data['subscribe']) || ($gserver['relay-scope'] != $data['scope'])) {
1464 $fields = ['relay-subscribe' => $data['subscribe'], 'relay-scope' => $data['scope']];
1465 DBA::update('gserver', $fields, ['id' => $gserver['id']]);
1468 DBA::delete('gserver-tag', ['gserver-id' => $gserver['id']]);
1470 if ($data['scope'] == 'tags') {
1473 foreach ($data['tags'] as $tag) {
1474 $tag = mb_strtolower($tag);
1475 if (strlen($tag) < 100) {
1480 foreach ($tags as $tag) {
1481 DBA::insert('gserver-tag', ['gserver-id' => $gserver['id'], 'tag' => $tag], true);
1485 // Create or update the relay contact
1487 if (isset($data['protocols'])) {
1488 if (isset($data['protocols']['diaspora'])) {
1489 $fields['network'] = Protocol::DIASPORA;
1491 if (isset($data['protocols']['diaspora']['receive'])) {
1492 $fields['batch'] = $data['protocols']['diaspora']['receive'];
1493 } elseif (is_string($data['protocols']['diaspora'])) {
1494 $fields['batch'] = $data['protocols']['diaspora'];
1498 if (isset($data['protocols']['dfrn'])) {
1499 $fields['network'] = Protocol::DFRN;
1501 if (isset($data['protocols']['dfrn']['receive'])) {
1502 $fields['batch'] = $data['protocols']['dfrn']['receive'];
1503 } elseif (is_string($data['protocols']['dfrn'])) {
1504 $fields['batch'] = $data['protocols']['dfrn'];
1508 Diaspora::setRelayContact($server_url, $fields);
1512 * @brief Returns a list of all known servers
1513 * @return array List of server urls
1515 public static function serverlist()
1518 "SELECT `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver`
1519 WHERE `network` IN ('%s', '%s', '%s') AND `last_contact` > `last_failure`
1520 ORDER BY `last_contact`
1522 DBA::escape(Protocol::DFRN),
1523 DBA::escape(Protocol::DIASPORA),
1524 DBA::escape(Protocol::OSTATUS)
1527 if (!DBA::isResult($r)) {
1535 * @brief Fetch server list from remote servers and adds them when they are new.
1537 * @param string $poco URL to the POCO endpoint
1539 private static function fetchServerlist($poco)
1541 $serverret = Network::curl($poco . "/@server");
1543 if (!$serverret["success"]) {
1547 $serverlist = json_decode($serverret['body'], true);
1549 if (!is_array($serverlist)) {
1553 foreach ($serverlist as $server) {
1554 $server_url = str_replace("/index.php", "", $server['url']);
1556 $r = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", DBA::escape(normalise_link($server_url)));
1558 if (!DBA::isResult($r)) {
1559 logger("Call server check for server ".$server_url, LOGGER_DEBUG);
1560 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $server_url);
1565 private static function discoverFederation()
1567 $last = Config::get('poco', 'last_federation_discovery');
1570 $next = $last + (24 * 60 * 60);
1572 if ($next > time()) {
1577 // Discover Friendica, Hubzilla and Diaspora servers
1578 $serverdata = Network::fetchUrl("http://the-federation.info/pods.json");
1580 if (!empty($serverdata)) {
1581 $servers = json_decode($serverdata, true);
1583 if (!empty($servers['pods'])) {
1584 foreach ($servers['pods'] as $server) {
1585 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", "https://" . $server['host']);
1590 // Disvover Mastodon servers
1591 if (!Config::get('system', 'ostatus_disabled')) {
1592 $accesstoken = Config::get('system', 'instances_social_key');
1594 if (!empty($accesstoken)) {
1595 $api = 'https://instances.social/api/1.0/instances/list?count=0';
1596 $header = ['Authorization: Bearer '.$accesstoken];
1597 $serverdata = Network::curl($api, false, $redirects, ['headers' => $header]);
1599 if ($serverdata['success']) {
1600 $servers = json_decode($serverdata['body'], true);
1602 foreach ($servers['instances'] as $server) {
1603 $url = (is_null($server['https_score']) ? 'http' : 'https') . '://' . $server['name'];
1604 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $url);
1610 // Currently disabled, since the service isn't available anymore.
1611 // It is not removed since I hope that there will be a successor.
1612 // Discover GNU Social Servers.
1613 //if (!Config::get('system','ostatus_disabled')) {
1614 // $serverdata = "http://gstools.org/api/get_open_instances/";
1616 // $result = Network::curl($serverdata);
1617 // if ($result["success"]) {
1618 // $servers = json_decode($result["body"], true);
1620 // foreach($servers['data'] as $server)
1621 // self::checkServer($server['instance_address']);
1625 Config::set('poco', 'last_federation_discovery', time());
1628 public static function discoverSingleServer($id)
1630 $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `id` = %d", intval($id));
1632 if (!DBA::isResult($r)) {
1638 // Discover new servers out there (Works from Friendica version 3.5.2)
1639 self::fetchServerlist($server["poco"]);
1641 // Fetch all users from the other server
1642 $url = $server["poco"] . "/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1644 logger("Fetch all users from the server " . $server["url"], LOGGER_DEBUG);
1646 $retdata = Network::curl($url);
1648 if ($retdata["success"] && !empty($retdata["body"])) {
1649 $data = json_decode($retdata["body"], true);
1651 if (!empty($data)) {
1652 self::discoverServer($data, 2);
1655 if (Config::get('system', 'poco_discovery') > 1) {
1656 $timeframe = Config::get('system', 'poco_discovery_since');
1658 if ($timeframe == 0) {
1662 $updatedSince = date(DateTimeFormat::MYSQL, time() - $timeframe * 86400);
1664 // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3)
1665 $url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1669 $retdata = Network::curl($url);
1671 if ($retdata["success"] && !empty($retdata["body"])) {
1672 logger("Fetch all global contacts from the server " . $server["nurl"], LOGGER_DEBUG);
1673 $data = json_decode($retdata["body"], true);
1675 if (!empty($data)) {
1676 $success = self::discoverServer($data);
1680 if (!$success && (Config::get('system', 'poco_discovery') > 2)) {
1681 logger("Fetch contacts from users of the server " . $server["nurl"], LOGGER_DEBUG);
1682 self::discoverServerUsers($data, $server);
1686 $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
1687 DBA::update('gserver', $fields, ['nurl' => $server["nurl"]]);
1691 // If the server hadn't replied correctly, then force a sanity check
1692 self::checkServer($server["url"], $server["network"], true);
1694 // If we couldn't reach the server, we will try it some time later
1695 $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
1696 DBA::update('gserver', $fields, ['nurl' => $server["nurl"]]);
1702 public static function discover($complete = false)
1704 // Update the server list
1705 self::discoverFederation();
1709 $requery_days = intval(Config::get('system', 'poco_requery_days'));
1711 if ($requery_days == 0) {
1715 $last_update = date('c', time() - (60 * 60 * 24 * $requery_days));
1717 $gservers = q("SELECT `id`, `url`, `nurl`, `network`
1719 WHERE `last_contact` >= `last_failure`
1721 AND `last_poco_query` < '%s'
1722 ORDER BY RAND()", DBA::escape($last_update)
1725 if (DBA::isResult($gservers)) {
1726 foreach ($gservers as $gserver) {
1727 if (!self::checkServer($gserver['url'], $gserver['network'])) {
1728 // The server is not reachable? Okay, then we will try it later
1729 $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
1730 DBA::update('gserver', $fields, ['nurl' => $gserver['nurl']]);
1734 logger('Update directory from server ' . $gserver['url'] . ' with ID ' . $gserver['id'], LOGGER_DEBUG);
1735 Worker::add(PRIORITY_LOW, 'DiscoverPoCo', 'update_server_directory', (int) $gserver['id']);
1737 if (!$complete && ( --$no_of_queries == 0)) {
1744 private static function discoverServerUsers(array $data, array $server)
1746 if (!isset($data['entry'])) {
1750 foreach ($data['entry'] as $entry) {
1753 if (isset($entry['urls'])) {
1754 foreach ($entry['urls'] as $url) {
1755 if ($url['type'] == 'profile') {
1756 $profile_url = $url['value'];
1757 $path_array = explode('/', parse_url($profile_url, PHP_URL_PATH));
1758 $username = end($path_array);
1763 if ($username != '') {
1764 logger('Fetch contacts for the user ' . $username . ' from the server ' . $server['nurl'], LOGGER_DEBUG);
1766 // Fetch all contacts from a given user from the other server
1767 $url = $server['poco'] . '/' . $username . '/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation';
1769 $retdata = Network::curl($url);
1771 if (!empty($retdata['success'])) {
1772 $data = json_decode($retdata["body"], true);
1774 if (!empty($data)) {
1775 self::discoverServer($data, 3);
1782 private static function discoverServer(array $data, $default_generation = 0)
1784 if (empty($data['entry'])) {
1790 foreach ($data['entry'] as $entry) {
1792 $profile_photo = '';
1796 $updated = NULL_DATE;
1802 $generation = $default_generation;
1804 if (!empty($entry['displayName'])) {
1805 $name = $entry['displayName'];
1808 if (isset($entry['urls'])) {
1809 foreach ($entry['urls'] as $url) {
1810 if ($url['type'] == 'profile') {
1811 $profile_url = $url['value'];
1814 if ($url['type'] == 'webfinger') {
1815 $connect_url = str_replace('acct:' , '', $url['value']);
1821 if (isset($entry['photos'])) {
1822 foreach ($entry['photos'] as $photo) {
1823 if ($photo['type'] == 'profile') {
1824 $profile_photo = $photo['value'];
1830 if (isset($entry['updated'])) {
1831 $updated = date(DateTimeFormat::MYSQL, strtotime($entry['updated']));
1834 if (isset($entry['network'])) {
1835 $network = $entry['network'];
1838 if (isset($entry['currentLocation'])) {
1839 $location = $entry['currentLocation'];
1842 if (isset($entry['aboutMe'])) {
1843 $about = HTML::toBBCode($entry['aboutMe']);
1846 if (isset($entry['gender'])) {
1847 $gender = $entry['gender'];
1850 if (isset($entry['generation']) && ($entry['generation'] > 0)) {
1851 $generation = ++$entry['generation'];
1854 if (isset($entry['contactType']) && ($entry['contactType'] >= 0)) {
1855 $contact_type = $entry['contactType'];
1858 if (isset($entry['tags'])) {
1859 foreach ($entry['tags'] as $tag) {
1860 $keywords = implode(", ", $tag);
1864 if ($generation > 0) {
1867 logger("Store profile ".$profile_url, LOGGER_DEBUG);
1869 $gcontact = ["url" => $profile_url,
1871 "network" => $network,
1872 "photo" => $profile_photo,
1874 "location" => $location,
1875 "gender" => $gender,
1876 "keywords" => $keywords,
1877 "connect" => $connect_url,
1878 "updated" => $updated,
1879 "contact-type" => $contact_type,
1880 "generation" => $generation];
1883 $gcontact = GContact::sanitize($gcontact);
1884 GContact::update($gcontact);
1885 } catch (Exception $e) {
1886 logger($e->getMessage(), LOGGER_DEBUG);
1889 logger("Done for profile ".$profile_url, LOGGER_DEBUG);