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\Worker;
18 use Friendica\Database\DBA;
19 use Friendica\Model\GContact;
20 use Friendica\Model\Profile;
21 use Friendica\Network\Probe;
22 use Friendica\Util\DateTimeFormat;
23 use Friendica\Util\Network;
24 use Friendica\Util\XML;
26 require_once 'include/dba.php';
31 * @brief Fetch POCO data
33 * @param integer $cid Contact ID
34 * @param integer $uid User ID
35 * @param integer $zcid Global Contact ID
36 * @param integer $url POCO address that should be polled
38 * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
39 * and add the entries to the gcontact (Global Contact) table, or update existing entries
40 * if anything (name or photo) has changed.
41 * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
43 * Once the global contact is stored add (if necessary) the contact linkage which associates
44 * the given uid, cid to the global contact entry. There can be many uid/cid combinations
45 * pointing to the same global contact id.
48 public static function loadWorker($cid, $uid = 0, $zcid = 0, $url = null)
50 // Call the function "load" via the worker
51 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "load", (int)$cid, (int)$uid, (int)$zcid, $url);
55 * @brief Fetch POCO data from the worker
57 * @param integer $cid Contact ID
58 * @param integer $uid User ID
59 * @param integer $zcid Global Contact ID
60 * @param integer $url POCO address that should be polled
63 public static function load($cid, $uid, $zcid, $url)
69 $contact = DBA::selectFirst('contact', ['poco', 'uid'], ['id' => $cid]);
70 if (DBA::isResult($contact)) {
71 $url = $contact['poco'];
72 $uid = $contact['uid'];
84 $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') ;
86 logger('load: ' . $url, LOGGER_DEBUG);
88 $s = Network::fetchUrl($url);
90 logger('load: returns ' . $s, LOGGER_DATA);
92 logger('load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
94 if (($a->get_curl_code() > 299) || (! $s)) {
98 $j = json_decode($s, true);
100 logger('load: json: ' . print_r($j, true), LOGGER_DATA);
102 if (!isset($j['entry'])) {
107 foreach ($j['entry'] as $entry) {
114 $updated = NULL_DATE;
122 if (!empty($entry['displayName'])) {
123 $name = $entry['displayName'];
126 if (isset($entry['urls'])) {
127 foreach ($entry['urls'] as $url) {
128 if ($url['type'] == 'profile') {
129 $profile_url = $url['value'];
132 if ($url['type'] == 'webfinger') {
133 $connect_url = str_replace('acct:', '', $url['value']);
138 if (isset($entry['photos'])) {
139 foreach ($entry['photos'] as $photo) {
140 if ($photo['type'] == 'profile') {
141 $profile_photo = $photo['value'];
147 if (isset($entry['updated'])) {
148 $updated = date(DateTimeFormat::MYSQL, strtotime($entry['updated']));
151 if (isset($entry['network'])) {
152 $network = $entry['network'];
155 if (isset($entry['currentLocation'])) {
156 $location = $entry['currentLocation'];
159 if (isset($entry['aboutMe'])) {
160 $about = HTML::toBBCode($entry['aboutMe']);
163 if (isset($entry['gender'])) {
164 $gender = $entry['gender'];
167 if (isset($entry['generation']) && ($entry['generation'] > 0)) {
168 $generation = ++$entry['generation'];
171 if (isset($entry['tags'])) {
172 foreach ($entry['tags'] as $tag) {
173 $keywords = implode(", ", $tag);
177 if (isset($entry['contactType']) && ($entry['contactType'] >= 0)) {
178 $contact_type = $entry['contactType'];
181 $gcontact = ["url" => $profile_url,
183 "network" => $network,
184 "photo" => $profile_photo,
186 "location" => $location,
188 "keywords" => $keywords,
189 "connect" => $connect_url,
190 "updated" => $updated,
191 "contact-type" => $contact_type,
192 "generation" => $generation];
195 $gcontact = GContact::sanitize($gcontact);
196 $gcid = GContact::update($gcontact);
198 GContact::link($gcid, $uid, $cid, $zcid);
199 } catch (Exception $e) {
200 logger($e->getMessage(), LOGGER_DEBUG);
203 logger("load: loaded $total entries", LOGGER_DEBUG);
205 $condition = ["`cid` = ? AND `uid` = ? AND `zcid` = ? AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY", $cid, $uid, $zcid];
206 DBA::delete('glink', $condition);
209 public static function reachable($profile, $server = "", $network = "", $force = false)
212 $server = self::detectServer($profile);
219 return self::checkServer($server, $network, $force);
222 public static function detectServer($profile)
224 // Try to detect the server path based upon some known standard paths
227 if ($server_url == "") {
228 $friendica = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $profile);
229 if ($friendica != $profile) {
230 $server_url = $friendica;
231 $network = NETWORK_DFRN;
235 if ($server_url == "") {
236 $diaspora = preg_replace("=(https?://)(.*)/u/(.*)=ism", "$1$2", $profile);
237 if ($diaspora != $profile) {
238 $server_url = $diaspora;
239 $network = NETWORK_DIASPORA;
243 if ($server_url == "") {
244 $red = preg_replace("=(https?://)(.*)/channel/(.*)=ism", "$1$2", $profile);
245 if ($red != $profile) {
247 $network = NETWORK_DIASPORA;
252 if ($server_url == "") {
253 $mastodon = preg_replace("=(https?://)(.*)/users/(.*)=ism", "$1$2", $profile);
254 if ($mastodon != $profile) {
255 $server_url = $mastodon;
256 $network = NETWORK_OSTATUS;
260 // Numeric OStatus variant
261 if ($server_url == "") {
262 $ostatus = preg_replace("=(https?://)(.*)/user/(.*)=ism", "$1$2", $profile);
263 if ($ostatus != $profile) {
264 $server_url = $ostatus;
265 $network = NETWORK_OSTATUS;
270 if ($server_url == "") {
271 $base = preg_replace("=(https?://)(.*?)/(.*)=ism", "$1$2", $profile);
272 if ($base != $profile) {
274 $network = NETWORK_PHANTOM;
278 if ($server_url == "") {
283 "SELECT `id` FROM `gserver` WHERE `nurl` = '%s' AND `last_contact` > `last_failure`",
284 DBA::escape(normalise_link($server_url))
287 if (DBA::isResult($r)) {
291 // Fetch the host-meta to check if this really is a server
292 $serverret = Network::curl($server_url."/.well-known/host-meta");
293 if (!$serverret["success"]) {
300 public static function alternateOStatusUrl($url)
302 return(preg_match("=https?://.+/user/\d+=ism", $url, $matches));
305 public static function lastUpdated($profile, $force = false)
308 "SELECT * FROM `gcontact` WHERE `nurl` = '%s'",
309 DBA::escape(normalise_link($profile))
312 if (!DBA::isResult($gcontacts)) {
316 $contact = ["url" => $profile];
318 if ($gcontacts[0]["created"] <= NULL_DATE) {
319 $contact['created'] = DateTimeFormat::utcNow();
324 $server_url = normalise_link(self::detectServer($profile));
327 if (($server_url == '') && ($gcontacts[0]["server_url"] != "")) {
328 $server_url = $gcontacts[0]["server_url"];
331 if (!$force && (($server_url == '') || ($gcontacts[0]["server_url"] == $gcontacts[0]["nurl"]))) {
332 $server_url = normalise_link(self::detectServer($profile));
335 if (!in_array($gcontacts[0]["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_FEED, NETWORK_OSTATUS, ""])) {
336 logger("Profile ".$profile.": Network type ".$gcontacts[0]["network"]." can't be checked", LOGGER_DEBUG);
340 if ($server_url != "") {
341 if (!self::checkServer($server_url, $gcontacts[0]["network"], $force)) {
343 $fields = ['last_failure' => DateTimeFormat::utcNow()];
344 DBA::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
347 logger("Profile ".$profile.": Server ".$server_url." wasn't reachable.", LOGGER_DEBUG);
350 $contact['server_url'] = $server_url;
353 if (in_array($gcontacts[0]["network"], ["", NETWORK_FEED])) {
355 "SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''",
356 DBA::escape(normalise_link($server_url))
360 $contact['network'] = $server[0]["network"];
366 // noscrape is really fast so we don't cache the call.
367 if (($server_url != "") && ($gcontacts[0]["nick"] != "")) {
368 // Use noscrape if possible
369 $server = q("SELECT `noscrape`, `network` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", DBA::escape(normalise_link($server_url)));
372 $noscraperet = Network::curl($server[0]["noscrape"]."/".$gcontacts[0]["nick"]);
374 if ($noscraperet["success"] && ($noscraperet["body"] != "")) {
375 $noscrape = json_decode($noscraperet["body"], true);
377 if (is_array($noscrape)) {
378 $contact["network"] = $server[0]["network"];
380 if (isset($noscrape["fn"])) {
381 $contact["name"] = $noscrape["fn"];
383 if (isset($noscrape["comm"])) {
384 $contact["community"] = $noscrape["comm"];
386 if (isset($noscrape["tags"])) {
387 $keywords = implode(" ", $noscrape["tags"]);
388 if ($keywords != "") {
389 $contact["keywords"] = $keywords;
393 $location = Profile::formatLocation($noscrape);
395 $contact["location"] = $location;
397 if (isset($noscrape["dfrn-notify"])) {
398 $contact["notify"] = $noscrape["dfrn-notify"];
400 // Remove all fields that are not present in the gcontact table
401 unset($noscrape["fn"]);
402 unset($noscrape["key"]);
403 unset($noscrape["homepage"]);
404 unset($noscrape["comm"]);
405 unset($noscrape["tags"]);
406 unset($noscrape["locality"]);
407 unset($noscrape["region"]);
408 unset($noscrape["country-name"]);
409 unset($noscrape["contacts"]);
410 unset($noscrape["dfrn-request"]);
411 unset($noscrape["dfrn-confirm"]);
412 unset($noscrape["dfrn-notify"]);
413 unset($noscrape["dfrn-poll"]);
415 // Set the date of the last contact
416 /// @todo By now the function "update_gcontact" doesn't work with this field
417 //$contact["last_contact"] = DateTimeFormat::utcNow();
419 $contact = array_merge($contact, $noscrape);
421 GContact::update($contact);
423 if (!empty($noscrape["updated"])) {
424 $fields = ['last_contact' => DateTimeFormat::utcNow()];
425 DBA::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
427 logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG);
429 return $noscrape["updated"];
436 // If we only can poll the feed, then we only do this once a while
437 if (!$force && !self::updateNeeded($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) {
438 logger("Profile ".$profile." was last updated at ".$gcontacts[0]["updated"]." (cached)", LOGGER_DEBUG);
440 GContact::update($contact);
441 return $gcontacts[0]["updated"];
444 $data = Probe::uri($profile);
446 // Is the profile link the alternate OStatus link notation? (http://domain.tld/user/4711)
447 // Then check the other link and delete this one
448 if (($data["network"] == NETWORK_OSTATUS) && self::alternateOStatusUrl($profile)
449 && (normalise_link($profile) == normalise_link($data["alias"]))
450 && (normalise_link($profile) != normalise_link($data["url"]))
452 // Delete the old entry
453 DBA::delete('gcontact', ['nurl' => normalise_link($profile)]);
455 $gcontact = array_merge($gcontacts[0], $data);
457 $gcontact["server_url"] = $data["baseurl"];
460 $gcontact = GContact::sanitize($gcontact);
461 GContact::update($gcontact);
463 self::lastUpdated($data["url"], $force);
464 } catch (Exception $e) {
465 logger($e->getMessage(), LOGGER_DEBUG);
468 logger("Profile ".$profile." was deleted", LOGGER_DEBUG);
472 if (($data["poll"] == "") || (in_array($data["network"], [NETWORK_FEED, NETWORK_PHANTOM]))) {
473 $fields = ['last_failure' => DateTimeFormat::utcNow()];
474 DBA::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
476 logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG);
480 $contact = array_merge($contact, $data);
482 $contact["server_url"] = $data["baseurl"];
484 GContact::update($contact);
486 $feedret = Network::curl($data["poll"]);
488 if (!$feedret["success"]) {
489 $fields = ['last_failure' => DateTimeFormat::utcNow()];
490 DBA::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
492 logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG);
496 $doc = new DOMDocument();
497 /// @TODO Avoid error supression here
498 @$doc->loadXML($feedret["body"]);
500 $xpath = new DOMXPath($doc);
501 $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
503 $entries = $xpath->query('/atom:feed/atom:entry');
507 foreach ($entries as $entry) {
508 $published = DateTimeFormat::utc($xpath->query('atom:published/text()', $entry)->item(0)->nodeValue);
509 $updated = DateTimeFormat::utc($xpath->query('atom:updated/text()' , $entry)->item(0)->nodeValue);
511 if ($last_updated < $published) {
512 $last_updated = $published;
515 if ($last_updated < $updated) {
516 $last_updated = $updated;
520 // Maybe there aren't any entries. Then check if it is a valid feed
521 if ($last_updated == "") {
522 if ($xpath->query('/atom:feed')->length > 0) {
523 $last_updated = NULL_DATE;
527 $fields = ['last_contact' => DateTimeFormat::utcNow()];
529 if (!empty($last_updated)) {
530 $fields['updated'] = $last_updated;
533 DBA::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
535 if (($gcontacts[0]["generation"] == 0)) {
536 $fields = ['generation' => 9];
537 DBA::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
540 logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG);
542 return $last_updated;
545 public static function updateNeeded($created, $updated, $last_failure, $last_contact)
547 $now = strtotime(DateTimeFormat::utcNow());
549 if ($updated > $last_contact) {
550 $contact_time = strtotime($updated);
552 $contact_time = strtotime($last_contact);
555 $failure_time = strtotime($last_failure);
556 $created_time = strtotime($created);
558 // If there is no "created" time then use the current time
559 if ($created_time <= 0) {
560 $created_time = $now;
563 // If the last contact was less than 24 hours then don't update
564 if (($now - $contact_time) < (60 * 60 * 24)) {
568 // If the last failure was less than 24 hours then don't update
569 if (($now - $failure_time) < (60 * 60 * 24)) {
573 // If the last contact was less than a week ago and the last failure is older than a week then don't update
574 //if ((($now - $contact_time) < (60 * 60 * 24 * 7)) && ($contact_time > $failure_time))
577 // 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
578 if ((($now - $contact_time) > (60 * 60 * 24 * 7)) && (($now - $created_time) > (60 * 60 * 24 * 7)) && (($now - $failure_time) < (60 * 60 * 24 * 7))) {
582 // 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
583 if ((($now - $contact_time) > (60 * 60 * 24 * 30)) && (($now - $created_time) > (60 * 60 * 24 * 30)) && (($now - $failure_time) < (60 * 60 * 24 * 30))) {
590 /// @TODO Maybe move this out to an utilities class?
591 private static function toBoolean($val)
593 if (($val == "true") || ($val == 1)) {
595 } elseif (($val == "false") || ($val == 0)) {
603 * @brief Detect server type (Hubzilla or Friendica) via the poco data
605 * @param array $data POCO data
606 * @return array Server data
608 private static function detectPocoData(array $data)
612 if (!isset($data['entry'])) {
616 if (count($data['entry']) == 0) {
620 if (!isset($data['entry'][0]['urls'])) {
624 if (count($data['entry'][0]['urls']) == 0) {
628 foreach ($data['entry'][0]['urls'] as $url) {
629 if ($url['type'] == 'zot') {
631 $server["platform"] = 'Hubzilla';
632 $server["network"] = NETWORK_DIASPORA;
640 * @brief Detect server type by using the nodeinfo data
642 * @param string $server_url address of the server
643 * @return array Server data
645 private static function fetchNodeinfo($server_url)
647 $serverret = Network::curl($server_url."/.well-known/nodeinfo");
648 if (!$serverret["success"]) {
652 $nodeinfo = json_decode($serverret['body'], true);
654 if (!is_array($nodeinfo) || !isset($nodeinfo['links'])) {
661 foreach ($nodeinfo['links'] as $link) {
662 if ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/1.0') {
663 $nodeinfo1_url = $link['href'];
664 } elseif ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/2.0') {
665 $nodeinfo2_url = $link['href'];
669 if ($nodeinfo1_url . $nodeinfo2_url == '') {
675 // When the nodeinfo url isn't on the same host, then there is obviously something wrong
676 if (!empty($nodeinfo2_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo2_url, PHP_URL_HOST))) {
677 $server = self::parseNodeinfo2($nodeinfo2_url);
680 // When the nodeinfo url isn't on the same host, then there is obviously something wrong
681 if (empty($server) && !empty($nodeinfo1_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo1_url, PHP_URL_HOST))) {
682 $server = self::parseNodeinfo1($nodeinfo1_url);
689 * @brief Parses Nodeinfo 1
691 * @param string $nodeinfo_url address of the nodeinfo path
692 * @return array Server data
694 private static function parseNodeinfo1($nodeinfo_url)
696 $serverret = Network::curl($nodeinfo_url);
698 if (!$serverret["success"]) {
702 $nodeinfo = json_decode($serverret['body'], true);
704 if (!is_array($nodeinfo)) {
710 $server['register_policy'] = REGISTER_CLOSED;
712 if (is_bool($nodeinfo['openRegistrations']) && $nodeinfo['openRegistrations']) {
713 $server['register_policy'] = REGISTER_OPEN;
716 if (is_array($nodeinfo['software'])) {
717 if (isset($nodeinfo['software']['name'])) {
718 $server['platform'] = $nodeinfo['software']['name'];
721 if (isset($nodeinfo['software']['version'])) {
722 $server['version'] = $nodeinfo['software']['version'];
723 // Version numbers on Nodeinfo are presented with additional info, e.g.:
724 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
725 $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']);
729 if (is_array($nodeinfo['metadata']) && isset($nodeinfo['metadata']['nodeName'])) {
730 $server['site_name'] = $nodeinfo['metadata']['nodeName'];
733 if (!empty($nodeinfo['usage']['users']['total'])) {
734 $server['registered-users'] = $nodeinfo['usage']['users']['total'];
741 if (is_array($nodeinfo['protocols']['inbound'])) {
742 foreach ($nodeinfo['protocols']['inbound'] as $inbound) {
743 if ($inbound == 'diaspora') {
746 if ($inbound == 'friendica') {
749 if ($inbound == 'gnusocial') {
756 $server['network'] = NETWORK_OSTATUS;
759 $server['network'] = NETWORK_DIASPORA;
762 $server['network'] = NETWORK_DFRN;
773 * @brief Parses Nodeinfo 2
775 * @param string $nodeinfo_url address of the nodeinfo path
776 * @return array Server data
778 private static function parseNodeinfo2($nodeinfo_url)
780 $serverret = Network::curl($nodeinfo_url);
781 if (!$serverret["success"]) {
785 $nodeinfo = json_decode($serverret['body'], true);
787 if (!is_array($nodeinfo)) {
793 $server['register_policy'] = REGISTER_CLOSED;
795 if (is_bool($nodeinfo['openRegistrations']) && $nodeinfo['openRegistrations']) {
796 $server['register_policy'] = REGISTER_OPEN;
799 if (is_array($nodeinfo['software'])) {
800 if (isset($nodeinfo['software']['name'])) {
801 $server['platform'] = $nodeinfo['software']['name'];
804 if (isset($nodeinfo['software']['version'])) {
805 $server['version'] = $nodeinfo['software']['version'];
806 // Version numbers on Nodeinfo are presented with additional info, e.g.:
807 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
808 $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']);
812 if (is_array($nodeinfo['metadata']) && isset($nodeinfo['metadata']['nodeName'])) {
813 $server['site_name'] = $nodeinfo['metadata']['nodeName'];
816 if (!empty($nodeinfo['usage']['users']['total'])) {
817 $server['registered-users'] = $nodeinfo['usage']['users']['total'];
824 if (!empty($nodeinfo['protocols'])) {
825 foreach ($nodeinfo['protocols'] as $protocol) {
826 if ($protocol == 'diaspora') {
828 } elseif ($protocol == 'friendica') {
830 } elseif ($protocol == 'gnusocial') {
837 $server['network'] = NETWORK_OSTATUS;
838 } elseif ($diaspora) {
839 $server['network'] = NETWORK_DIASPORA;
840 } elseif ($friendica) {
841 $server['network'] = NETWORK_DFRN;
844 if (empty($server)) {
852 * @brief Detect server type (Hubzilla or Friendica) via the front page body
854 * @param string $body Front page of the server
855 * @return array Server data
857 private static function detectServerType($body)
861 $doc = new DOMDocument();
862 /// @TODO Acoid supressing error
863 @$doc->loadHTML($body);
864 $xpath = new DOMXPath($doc);
866 $list = $xpath->query("//meta[@name]");
868 foreach ($list as $node) {
870 if ($node->attributes->length) {
871 foreach ($node->attributes as $attribute) {
872 $attr[$attribute->name] = $attribute->value;
875 if ($attr['name'] == 'generator') {
876 $version_part = explode(" ", $attr['content']);
877 if (count($version_part) == 2) {
878 if (in_array($version_part[0], ["Friendika", "Friendica"])) {
880 $server["platform"] = $version_part[0];
881 $server["version"] = $version_part[1];
882 $server["network"] = NETWORK_DFRN;
889 $list = $xpath->query("//meta[@property]");
891 foreach ($list as $node) {
893 if ($node->attributes->length) {
894 foreach ($node->attributes as $attribute) {
895 $attr[$attribute->name] = $attribute->value;
898 if ($attr['property'] == 'generator' && in_array($attr['content'], ["hubzilla", "BlaBlaNet"])) {
900 $server["platform"] = $attr['content'];
901 $server["version"] = "";
902 $server["network"] = NETWORK_DIASPORA;
911 $server["site_name"] = XML::getFirstNodeValue($xpath, '//head/title/text()');
916 public static function checkServer($server_url, $network = "", $force = false)
918 // Unify the server address
919 $server_url = trim($server_url, "/");
920 $server_url = str_replace("/index.php", "", $server_url);
922 if ($server_url == "") {
926 $gserver = DBA::selectFirst('gserver', [], ['nurl' => normalise_link($server_url)]);
927 if (DBA::isResult($gserver)) {
928 if ($gserver["created"] <= NULL_DATE) {
929 $fields = ['created' => DateTimeFormat::utcNow()];
930 $condition = ['nurl' => normalise_link($server_url)];
931 DBA::update('gserver', $fields, $condition);
933 $poco = $gserver["poco"];
934 $noscrape = $gserver["noscrape"];
936 if ($network == "") {
937 $network = $gserver["network"];
940 $last_contact = $gserver["last_contact"];
941 $last_failure = $gserver["last_failure"];
942 $version = $gserver["version"];
943 $platform = $gserver["platform"];
944 $site_name = $gserver["site_name"];
945 $info = $gserver["info"];
946 $register_policy = $gserver["register_policy"];
947 $registered_users = $gserver["registered-users"];
949 // See discussion under https://forum.friendi.ca/display/0b6b25a8135aabc37a5a0f5684081633
950 // It can happen that a zero date is in the database, but storing it again is forbidden.
951 if ($last_contact < NULL_DATE) {
952 $last_contact = NULL_DATE;
955 if ($last_failure < NULL_DATE) {
956 $last_failure = NULL_DATE;
959 if (!$force && !self::updateNeeded($gserver["created"], "", $last_failure, $last_contact)) {
960 logger("Use cached data for server ".$server_url, LOGGER_DEBUG);
961 return ($last_contact >= $last_failure);
970 $register_policy = -1;
971 $registered_users = 0;
973 $last_contact = NULL_DATE;
974 $last_failure = NULL_DATE;
976 logger("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$gserver["created"]." Failure: ".$last_failure." Contact: ".$last_contact, LOGGER_DEBUG);
979 $possible_failure = false;
980 $orig_last_failure = $last_failure;
981 $orig_last_contact = $last_contact;
983 // Mastodon uses the "@" for user profiles.
984 // But this can be misunderstood.
985 if (parse_url($server_url, PHP_URL_USER) != '') {
986 DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
990 // Check if the page is accessible via SSL.
991 $orig_server_url = $server_url;
992 $server_url = str_replace("http://", "https://", $server_url);
994 // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
995 $serverret = Network::curl($server_url."/.well-known/host-meta", false, $redirects, ['timeout' => 20]);
997 // Quit if there is a timeout.
998 // But we want to make sure to only quit if we are mostly sure that this server url fits.
999 if (DBA::isResult($gserver) && ($orig_server_url == $server_url) &&
1000 (!empty($serverret["errno"]) && ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT))) {
1001 logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
1002 DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
1006 // Maybe the page is unencrypted only?
1007 $xmlobj = @simplexml_load_string($serverret["body"], 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
1008 if (!$serverret["success"] || ($serverret["body"] == "") || empty($xmlobj) || !is_object($xmlobj)) {
1009 $server_url = str_replace("https://", "http://", $server_url);
1011 // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
1012 $serverret = Network::curl($server_url."/.well-known/host-meta", false, $redirects, ['timeout' => 20]);
1014 // Quit if there is a timeout
1015 if (!empty($serverret["errno"]) && ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT)) {
1016 logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
1017 DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
1021 $xmlobj = @simplexml_load_string($serverret["body"], 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
1024 if (!$serverret["success"] || ($serverret["body"] == "") || empty($xmlobj) || !is_object($xmlobj)) {
1025 // Workaround for bad configured servers (known nginx problem)
1026 if (!empty($serverret["debug"]) && !in_array($serverret["debug"]["http_code"], ["403", "404"])) {
1030 $possible_failure = true;
1033 // If the server has no possible failure we reset the cached data
1034 if (!$possible_failure) {
1039 $register_policy = -1;
1043 // This will be too low, but better than no value at all.
1044 $registered_users = DBA::count('gcontact', ['server_url' => normalise_link($server_url)]);
1049 $serverret = Network::curl($server_url."/poco");
1051 if ($serverret["success"]) {
1052 $data = json_decode($serverret["body"], true);
1054 if (isset($data['totalResults'])) {
1055 $registered_users = $data['totalResults'];
1056 $poco = $server_url . "/poco";
1057 $server = self::detectPocoData($data);
1059 if (!empty($server)) {
1060 $platform = $server['platform'];
1061 $network = $server['network'];
1068 * There are servers out there who don't return 404 on a failure
1069 * We have to be sure that don't misunderstand this
1071 if (is_null($data)) {
1080 // Test for Diaspora, Hubzilla, Mastodon or older Friendica servers
1081 $serverret = Network::curl($server_url);
1083 if (!$serverret["success"] || ($serverret["body"] == "")) {
1086 $server = self::detectServerType($serverret["body"]);
1088 if (!empty($server)) {
1089 $platform = $server['platform'];
1090 $network = $server['network'];
1091 $version = $server['version'];
1092 $site_name = $server['site_name'];
1095 $lines = explode("\n", $serverret["header"]);
1097 if (count($lines)) {
1098 foreach ($lines as $line) {
1099 $line = trim($line);
1101 if (stristr($line, 'X-Diaspora-Version:')) {
1102 $platform = "Diaspora";
1103 $version = trim(str_replace("X-Diaspora-Version:", "", $line));
1104 $version = trim(str_replace("x-diaspora-version:", "", $version));
1105 $network = NETWORK_DIASPORA;
1106 $versionparts = explode("-", $version);
1107 $version = $versionparts[0];
1110 if (stristr($line, 'Server: Mastodon')) {
1111 $platform = "Mastodon";
1112 $network = NETWORK_OSTATUS;
1119 if (!$failure && ($poco == "")) {
1120 // Test for Statusnet
1121 // Will also return data for Friendica and GNU Social - but it will be overwritten later
1122 // The "not implemented" is a special treatment for really, really old Friendica versions
1123 $serverret = Network::curl($server_url."/api/statusnet/version.json");
1125 if ($serverret["success"] && ($serverret["body"] != '{"error":"not implemented"}') &&
1126 ($serverret["body"] != '') && (strlen($serverret["body"]) < 30)) {
1127 $platform = "StatusNet";
1128 // Remove junk that some GNU Social servers return
1129 $version = str_replace(chr(239).chr(187).chr(191), "", $serverret["body"]);
1130 $version = trim($version, '"');
1131 $network = NETWORK_OSTATUS;
1134 // Test for GNU Social
1135 $serverret = Network::curl($server_url."/api/gnusocial/version.json");
1137 if ($serverret["success"] && ($serverret["body"] != '{"error":"not implemented"}') &&
1138 ($serverret["body"] != '') && (strlen($serverret["body"]) < 30)) {
1139 $platform = "GNU Social";
1140 // Remove junk that some GNU Social servers return
1141 $version = str_replace(chr(239) . chr(187) . chr(191), "", $serverret["body"]);
1142 $version = trim($version, '"');
1143 $network = NETWORK_OSTATUS;
1146 // Test for Mastodon
1147 $orig_version = $version;
1148 $serverret = Network::curl($server_url . "/api/v1/instance");
1150 if ($serverret["success"] && ($serverret["body"] != '')) {
1151 $data = json_decode($serverret["body"], true);
1153 if (isset($data['version'])) {
1154 $platform = "Mastodon";
1155 $version = $data['version'];
1156 $site_name = $data['title'];
1157 $info = $data['description'];
1158 $network = NETWORK_OSTATUS;
1161 if (!empty($data['stats']['user_count'])) {
1162 $registered_users = $data['stats']['user_count'];
1166 if (strstr($orig_version . $version, 'Pleroma')) {
1167 $platform = 'Pleroma';
1168 $version = trim(str_replace('Pleroma', '', $version));
1173 // Test for Hubzilla and Red
1174 $serverret = Network::curl($server_url . "/siteinfo.json");
1176 if ($serverret["success"]) {
1177 $data = json_decode($serverret["body"], true);
1179 if (isset($data['url'])) {
1180 $platform = $data['platform'];
1181 $version = $data['version'];
1182 $network = NETWORK_DIASPORA;
1185 if (!empty($data['site_name'])) {
1186 $site_name = $data['site_name'];
1189 if (!empty($data['channels_total'])) {
1190 $registered_users = $data['channels_total'];
1193 if (!empty($data['register_policy'])) {
1194 switch ($data['register_policy']) {
1195 case "REGISTER_OPEN":
1196 $register_policy = REGISTER_OPEN;
1199 case "REGISTER_APPROVE":
1200 $register_policy = REGISTER_APPROVE;
1203 case "REGISTER_CLOSED":
1205 $register_policy = REGISTER_CLOSED;
1210 // Test for Hubzilla, Redmatrix or Friendica
1211 $serverret = Network::curl($server_url."/api/statusnet/config.json");
1213 if ($serverret["success"]) {
1214 $data = json_decode($serverret["body"], true);
1216 if (isset($data['site']['server'])) {
1217 if (isset($data['site']['platform'])) {
1218 $platform = $data['site']['platform']['PLATFORM_NAME'];
1219 $version = $data['site']['platform']['STD_VERSION'];
1220 $network = NETWORK_DIASPORA;
1223 if (isset($data['site']['BlaBlaNet'])) {
1224 $platform = $data['site']['BlaBlaNet']['PLATFORM_NAME'];
1225 $version = $data['site']['BlaBlaNet']['STD_VERSION'];
1226 $network = NETWORK_DIASPORA;
1229 if (isset($data['site']['hubzilla'])) {
1230 $platform = $data['site']['hubzilla']['PLATFORM_NAME'];
1231 $version = $data['site']['hubzilla']['RED_VERSION'];
1232 $network = NETWORK_DIASPORA;
1235 if (isset($data['site']['redmatrix'])) {
1236 if (isset($data['site']['redmatrix']['PLATFORM_NAME'])) {
1237 $platform = $data['site']['redmatrix']['PLATFORM_NAME'];
1238 } elseif (isset($data['site']['redmatrix']['RED_PLATFORM'])) {
1239 $platform = $data['site']['redmatrix']['RED_PLATFORM'];
1242 $version = $data['site']['redmatrix']['RED_VERSION'];
1243 $network = NETWORK_DIASPORA;
1246 if (isset($data['site']['friendica'])) {
1247 $platform = $data['site']['friendica']['FRIENDICA_PLATFORM'];
1248 $version = $data['site']['friendica']['FRIENDICA_VERSION'];
1249 $network = NETWORK_DFRN;
1252 $site_name = $data['site']['name'];
1255 $inviteonly = false;
1258 if (!empty($data['site']['closed'])) {
1259 $closed = self::toBoolean($data['site']['closed']);
1262 if (!empty($data['site']['private'])) {
1263 $private = self::toBoolean($data['site']['private']);
1266 if (!empty($data['site']['inviteonly'])) {
1267 $inviteonly = self::toBoolean($data['site']['inviteonly']);
1270 if (!$closed && !$private and $inviteonly) {
1271 $register_policy = REGISTER_APPROVE;
1272 } elseif (!$closed && !$private) {
1273 $register_policy = REGISTER_OPEN;
1275 $register_policy = REGISTER_CLOSED;
1282 // Query statistics.json. Optional package for Diaspora, Friendica and Redmatrix
1284 $serverret = Network::curl($server_url . "/statistics.json");
1286 if ($serverret["success"]) {
1287 $data = json_decode($serverret["body"], true);
1289 if (isset($data['version'])) {
1290 $version = $data['version'];
1291 // Version numbers on statistics.json are presented with additional info, e.g.:
1292 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
1293 $version = preg_replace("=(.+)-(.{4,})=ism", "$1", $version);
1296 if (!empty($data['name'])) {
1297 $site_name = $data['name'];
1300 if (!empty($data['network'])) {
1301 $platform = $data['network'];
1304 if ($platform == "Diaspora") {
1305 $network = NETWORK_DIASPORA;
1308 if (!empty($data['registrations_open']) && $data['registrations_open']) {
1309 $register_policy = REGISTER_OPEN;
1311 $register_policy = REGISTER_CLOSED;
1316 // Query nodeinfo. Working for (at least) Diaspora and Friendica.
1318 $server = self::fetchNodeinfo($server_url);
1320 if (!empty($server)) {
1321 $register_policy = $server['register_policy'];
1323 if (isset($server['platform'])) {
1324 $platform = $server['platform'];
1327 if (isset($server['network'])) {
1328 $network = $server['network'];
1331 if (isset($server['version'])) {
1332 $version = $server['version'];
1335 if (isset($server['site_name'])) {
1336 $site_name = $server['site_name'];
1339 if (isset($server['registered-users'])) {
1340 $registered_users = $server['registered-users'];
1345 // Check for noscrape
1346 // Friendica servers could be detected as OStatus servers
1347 if (!$failure && in_array($network, [NETWORK_DFRN, NETWORK_OSTATUS])) {
1348 $serverret = Network::curl($server_url . "/friendica/json");
1350 if (!$serverret["success"]) {
1351 $serverret = Network::curl($server_url . "/friendika/json");
1354 if ($serverret["success"]) {
1355 $data = json_decode($serverret["body"], true);
1357 if (isset($data['version'])) {
1358 $network = NETWORK_DFRN;
1360 if (!empty($data['no_scrape_url'])) {
1361 $noscrape = $data['no_scrape_url'];
1364 $version = $data['version'];
1366 if (!empty($data['site_name'])) {
1367 $site_name = $data['site_name'];
1370 $info = $data['info'];
1371 $register_policy = constant($data['register_policy']);
1372 $platform = $data['platform'];
1377 // Every server has got at least an admin account
1378 if (!$failure && ($registered_users == 0)) {
1379 $registered_users = 1;
1382 if ($possible_failure && !$failure) {
1387 $last_contact = $orig_last_contact;
1388 $last_failure = DateTimeFormat::utcNow();
1390 $last_contact = DateTimeFormat::utcNow();
1391 $last_failure = $orig_last_failure;
1394 if (($last_contact <= $last_failure) && !$failure) {
1395 logger("Server ".$server_url." seems to be alive, but last contact wasn't set - could be a bug", LOGGER_DEBUG);
1396 } elseif (($last_contact >= $last_failure) && $failure) {
1397 logger("Server ".$server_url." seems to be dead, but last failure wasn't set - could be a bug", LOGGER_DEBUG);
1400 // Check again if the server exists
1401 $found = DBA::exists('gserver', ['nurl' => normalise_link($server_url)]);
1403 $version = strip_tags($version);
1404 $site_name = strip_tags($site_name);
1405 $info = strip_tags($info);
1406 $platform = strip_tags($platform);
1408 $fields = ['url' => $server_url, 'version' => $version,
1409 'site_name' => $site_name, 'info' => $info, 'register_policy' => $register_policy,
1410 'poco' => $poco, 'noscrape' => $noscrape, 'network' => $network,
1411 'platform' => $platform, 'registered-users' => $registered_users,
1412 'last_contact' => $last_contact, 'last_failure' => $last_failure];
1415 DBA::update('gserver', $fields, ['nurl' => normalise_link($server_url)]);
1416 } elseif (!$failure) {
1417 $fields['nurl'] = normalise_link($server_url);
1418 $fields['created'] = DateTimeFormat::utcNow();
1419 DBA::insert('gserver', $fields);
1422 if (!$failure && in_array($fields['network'], [NETWORK_DFRN, NETWORK_DIASPORA])) {
1423 self::discoverRelay($server_url);
1426 logger("End discovery for server " . $server_url, LOGGER_DEBUG);
1432 * @brief Fetch relay data from a given server url
1434 * @param string $server_url address of the server
1436 private static function discoverRelay($server_url)
1438 logger("Discover relay data for server " . $server_url, LOGGER_DEBUG);
1440 $serverret = Network::curl($server_url . "/.well-known/x-social-relay");
1442 if (!$serverret["success"]) {
1446 $data = json_decode($serverret['body'], true);
1448 if (!is_array($data)) {
1452 $gserver = DBA::selectFirst('gserver', ['id', 'relay-subscribe', 'relay-scope'], ['nurl' => normalise_link($server_url)]);
1454 if (!DBA::isResult($gserver)) {
1458 if (($gserver['relay-subscribe'] != $data['subscribe']) || ($gserver['relay-scope'] != $data['scope'])) {
1459 $fields = ['relay-subscribe' => $data['subscribe'], 'relay-scope' => $data['scope']];
1460 DBA::update('gserver', $fields, ['id' => $gserver['id']]);
1463 DBA::delete('gserver-tag', ['gserver-id' => $gserver['id']]);
1465 if ($data['scope'] == 'tags') {
1468 foreach ($data['tags'] as $tag) {
1469 $tag = mb_strtolower($tag);
1473 foreach ($tags as $tag) {
1474 DBA::insert('gserver-tag', ['gserver-id' => $gserver['id'], 'tag' => $tag], true);
1478 // Create or update the relay contact
1480 if (isset($data['protocols'])) {
1481 if (isset($data['protocols']['diaspora'])) {
1482 $fields['network'] = NETWORK_DIASPORA;
1484 if (isset($data['protocols']['diaspora']['receive'])) {
1485 $fields['batch'] = $data['protocols']['diaspora']['receive'];
1486 } elseif (is_string($data['protocols']['diaspora'])) {
1487 $fields['batch'] = $data['protocols']['diaspora'];
1491 if (isset($data['protocols']['dfrn'])) {
1492 $fields['network'] = NETWORK_DFRN;
1494 if (isset($data['protocols']['dfrn']['receive'])) {
1495 $fields['batch'] = $data['protocols']['dfrn']['receive'];
1496 } elseif (is_string($data['protocols']['dfrn'])) {
1497 $fields['batch'] = $data['protocols']['dfrn'];
1501 Diaspora::setRelayContact($server_url, $fields);
1505 * @brief Returns a list of all known servers
1506 * @return array List of server urls
1508 public static function serverlist()
1511 "SELECT `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver`
1512 WHERE `network` IN ('%s', '%s', '%s') AND `last_contact` > `last_failure`
1513 ORDER BY `last_contact`
1515 DBA::escape(NETWORK_DFRN),
1516 DBA::escape(NETWORK_DIASPORA),
1517 DBA::escape(NETWORK_OSTATUS)
1520 if (!DBA::isResult($r)) {
1528 * @brief Fetch server list from remote servers and adds them when they are new.
1530 * @param string $poco URL to the POCO endpoint
1532 private static function fetchServerlist($poco)
1534 $serverret = Network::curl($poco . "/@server");
1536 if (!$serverret["success"]) {
1540 $serverlist = json_decode($serverret['body'], true);
1542 if (!is_array($serverlist)) {
1546 foreach ($serverlist as $server) {
1547 $server_url = str_replace("/index.php", "", $server['url'];
1549 $r = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", DBA::escape(normalise_link($server_url)));
1551 if (!DBA::isResult($r)) {
1552 logger("Call server check for server ".$server_url, LOGGER_DEBUG);
1553 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $server_url);
1558 private static function discoverFederation()
1560 $last = Config::get('poco', 'last_federation_discovery');
1563 $next = $last + (24 * 60 * 60);
1565 if ($next > time()) {
1570 // Discover Friendica, Hubzilla and Diaspora servers
1571 $serverdata = Network::fetchUrl("http://the-federation.info/pods.json");
1573 if (!empty($serverdata)) {
1574 $servers = json_decode($serverdata, true);
1576 if (!empty($servers['pods'])) {
1577 foreach ($servers['pods'] as $server) {
1578 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", "https://" . $server['host']);
1583 // Disvover Mastodon servers
1584 if (!Config::get('system', 'ostatus_disabled')) {
1585 $accesstoken = Config::get('system', 'instances_social_key');
1587 if (!empty($accesstoken)) {
1588 $api = 'https://instances.social/api/1.0/instances/list?count=0';
1589 $header = ['Authorization: Bearer '.$accesstoken];
1590 $serverdata = Network::curl($api, false, $redirects, ['headers' => $header]);
1592 if ($serverdata['success']) {
1593 $servers = json_decode($serverdata['body'], true);
1595 foreach ($servers['instances'] as $server) {
1596 $url = (is_null($server['https_score']) ? 'http' : 'https') . '://' . $server['name'];
1597 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $url);
1603 // Currently disabled, since the service isn't available anymore.
1604 // It is not removed since I hope that there will be a successor.
1605 // Discover GNU Social Servers.
1606 //if (!Config::get('system','ostatus_disabled')) {
1607 // $serverdata = "http://gstools.org/api/get_open_instances/";
1609 // $result = Network::curl($serverdata);
1610 // if ($result["success"]) {
1611 // $servers = json_decode($result["body"], true);
1613 // foreach($servers['data'] as $server)
1614 // self::checkServer($server['instance_address']);
1618 Config::set('poco', 'last_federation_discovery', time());
1621 public static function discoverSingleServer($id)
1623 $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `id` = %d", intval($id));
1625 if (!DBA::isResult($r)) {
1631 // Discover new servers out there (Works from Friendica version 3.5.2)
1632 self::fetchServerlist($server["poco"]);
1634 // Fetch all users from the other server
1635 $url = $server["poco"] . "/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1637 logger("Fetch all users from the server " . $server["url"], LOGGER_DEBUG);
1639 $retdata = Network::curl($url);
1641 if ($retdata["success"]) {
1642 $data = json_decode($retdata["body"], true);
1644 self::discoverServer($data, 2);
1646 if (Config::get('system', 'poco_discovery') > 1) {
1647 $timeframe = Config::get('system', 'poco_discovery_since');
1649 if ($timeframe == 0) {
1653 $updatedSince = date(DateTimeFormat::MYSQL, time() - $timeframe * 86400);
1655 // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3)
1656 $url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1660 $retdata = Network::curl($url);
1662 if ($retdata["success"]) {
1663 logger("Fetch all global contacts from the server " . $server["nurl"], LOGGER_DEBUG);
1664 $success = self::discoverServer(json_decode($retdata["body"], true));
1667 if (!$success && (Config::get('system', 'poco_discovery') > 2)) {
1668 logger("Fetch contacts from users of the server " . $server["nurl"], LOGGER_DEBUG);
1669 self::discoverServerUsers($data, $server);
1673 $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
1674 DBA::update('gserver', $fields, ['nurl' => $server["nurl"]]);
1678 // If the server hadn't replied correctly, then force a sanity check
1679 self::checkServer($server["url"], $server["network"], true);
1681 // If we couldn't reach the server, we will try it some time later
1682 $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
1683 DBA::update('gserver', $fields, ['nurl' => $server["nurl"]]);
1689 public static function discover($complete = false)
1691 // Update the server list
1692 self::discoverFederation();
1696 $requery_days = intval(Config::get('system', 'poco_requery_days'));
1698 if ($requery_days == 0) {
1702 $last_update = date('c', time() - (60 * 60 * 24 * $requery_days));
1704 $gservers = q("SELECT `id`, `url`, `nurl`, `network`
1706 WHERE `last_contact` >= `last_failure`
1708 AND `last_poco_query` < '%s'
1709 ORDER BY RAND()", DBA::escape($last_update)
1712 if (DBA::isResult($gservers)) {
1713 foreach ($gservers as $gserver) {
1714 if (!self::checkServer($gserver['url'], $gserver['network'])) {
1715 // The server is not reachable? Okay, then we will try it later
1716 $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
1717 DBA::update('gserver', $fields, ['nurl' => $gserver['nurl']]);
1721 logger('Update directory from server ' . $gserver['url'] . ' with ID ' . $gserver['id'], LOGGER_DEBUG);
1722 Worker::add(PRIORITY_LOW, 'DiscoverPoCo', 'update_server_directory', (int) $gserver['id']);
1724 if (!$complete && ( --$no_of_queries == 0)) {
1731 private static function discoverServerUsers(array $data, array $server)
1733 if (!isset($data['entry'])) {
1737 foreach ($data['entry'] as $entry) {
1740 if (isset($entry['urls'])) {
1741 foreach ($entry['urls'] as $url) {
1742 if ($url['type'] == 'profile') {
1743 $profile_url = $url['value'];
1744 $path_array = explode('/', parse_url($profile_url, PHP_URL_PATH));
1745 $username = end($path_array);
1750 if ($username != '') {
1751 logger('Fetch contacts for the user ' . $username . ' from the server ' . $server['nurl'], LOGGER_DEBUG);
1753 // Fetch all contacts from a given user from the other server
1754 $url = $server['poco'] . '/' . $username . '/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation';
1756 $retdata = Network::curl($url);
1758 if (!empty($retdata['success'])) {
1759 self::discoverServer(json_decode($retdata['body'], true), 3);
1765 private static function discoverServer(array $data, $default_generation = 0)
1767 if (empty($data['entry'])) {
1773 foreach ($data['entry'] as $entry) {
1775 $profile_photo = '';
1779 $updated = NULL_DATE;
1785 $generation = $default_generation;
1787 if (!empty($entry['displayName'])) {
1788 $name = $entry['displayName'];
1791 if (isset($entry['urls'])) {
1792 foreach ($entry['urls'] as $url) {
1793 if ($url['type'] == 'profile') {
1794 $profile_url = $url['value'];
1797 if ($url['type'] == 'webfinger') {
1798 $connect_url = str_replace('acct:' , '', $url['value']);
1804 if (isset($entry['photos'])) {
1805 foreach ($entry['photos'] as $photo) {
1806 if ($photo['type'] == 'profile') {
1807 $profile_photo = $photo['value'];
1813 if (isset($entry['updated'])) {
1814 $updated = date(DateTimeFormat::MYSQL, strtotime($entry['updated']));
1817 if (isset($entry['network'])) {
1818 $network = $entry['network'];
1821 if (isset($entry['currentLocation'])) {
1822 $location = $entry['currentLocation'];
1825 if (isset($entry['aboutMe'])) {
1826 $about = HTML::toBBCode($entry['aboutMe']);
1829 if (isset($entry['gender'])) {
1830 $gender = $entry['gender'];
1833 if (isset($entry['generation']) && ($entry['generation'] > 0)) {
1834 $generation = ++$entry['generation'];
1837 if (isset($entry['contactType']) && ($entry['contactType'] >= 0)) {
1838 $contact_type = $entry['contactType'];
1841 if (isset($entry['tags'])) {
1842 foreach ($entry['tags'] as $tag) {
1843 $keywords = implode(", ", $tag);
1847 if ($generation > 0) {
1850 logger("Store profile ".$profile_url, LOGGER_DEBUG);
1852 $gcontact = ["url" => $profile_url,
1854 "network" => $network,
1855 "photo" => $profile_photo,
1857 "location" => $location,
1858 "gender" => $gender,
1859 "keywords" => $keywords,
1860 "connect" => $connect_url,
1861 "updated" => $updated,
1862 "contact-type" => $contact_type,
1863 "generation" => $generation];
1866 $gcontact = GContact::sanitize($gcontact);
1867 GContact::update($gcontact);
1868 } catch (Exception $e) {
1869 logger($e->getMessage(), LOGGER_DEBUG);
1872 logger("Done for profile ".$profile_url, LOGGER_DEBUG);