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\Logger;
18 use Friendica\Core\Protocol;
19 use Friendica\Core\Worker;
20 use Friendica\Database\DBA;
21 use Friendica\Model\GContact;
22 use Friendica\Model\Profile;
23 use Friendica\Network\Probe;
24 use Friendica\Util\DateTimeFormat;
25 use Friendica\Util\Network;
26 use Friendica\Util\Strings;
27 use Friendica\Util\XML;
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.
47 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
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
62 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
64 public static function load($cid, $uid, $zcid, $url)
68 $contact = DBA::selectFirst('contact', ['poco', 'uid'], ['id' => $cid]);
69 if (DBA::isResult($contact)) {
70 $url = $contact['poco'];
71 $uid = $contact['uid'];
83 $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');
85 Logger::log('load: ' . $url, Logger::DEBUG);
87 $fetchresult = Network::fetchUrlFull($url);
88 $s = $fetchresult->getBody();
90 Logger::log('load: returns ' . $s, Logger::DATA);
92 Logger::log('load: return code: ' . $fetchresult->getReturnCode(), Logger::DEBUG);
94 if (($fetchresult->getReturnCode() > 299) || (! $s)) {
98 $j = json_decode($s, true);
100 Logger::log('load: json: ' . print_r($j, true), Logger::DATA);
102 if (!isset($j['entry'])) {
107 foreach ($j['entry'] as $entry) {
114 $updated = DBA::NULL_DATETIME;
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::log($e->getMessage(), Logger::DEBUG);
203 Logger::log("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;
234 if ($server_url == "") {
235 $diaspora = preg_replace("=(https?://)(.*)/u/(.*)=ism", "$1$2", $profile);
236 if ($diaspora != $profile) {
237 $server_url = $diaspora;
241 if ($server_url == "") {
242 $red = preg_replace("=(https?://)(.*)/channel/(.*)=ism", "$1$2", $profile);
243 if ($red != $profile) {
249 if ($server_url == "") {
250 $mastodon = preg_replace("=(https?://)(.*)/users/(.*)=ism", "$1$2", $profile);
251 if ($mastodon != $profile) {
252 $server_url = $mastodon;
256 // Numeric OStatus variant
257 if ($server_url == "") {
258 $ostatus = preg_replace("=(https?://)(.*)/user/(.*)=ism", "$1$2", $profile);
259 if ($ostatus != $profile) {
260 $server_url = $ostatus;
265 if ($server_url == "") {
266 $base = preg_replace("=(https?://)(.*?)/(.*)=ism", "$1$2", $profile);
267 if ($base != $profile) {
272 if ($server_url == "") {
277 "SELECT `id` FROM `gserver` WHERE `nurl` = '%s' AND `last_contact` > `last_failure`",
278 DBA::escape(Strings::normaliseLink($server_url))
281 if (DBA::isResult($r)) {
285 // Fetch the host-meta to check if this really is a server
286 $curlResult = Network::curl($server_url."/.well-known/host-meta");
287 if (!$curlResult->isSuccess()) {
294 public static function alternateOStatusUrl($url)
296 return(preg_match("=https?://.+/user/\d+=ism", $url, $matches));
299 public static function lastUpdated($profile, $force = false)
302 "SELECT * FROM `gcontact` WHERE `nurl` = '%s'",
303 DBA::escape(Strings::normaliseLink($profile))
306 if (!DBA::isResult($gcontacts)) {
310 $contact = ["url" => $profile];
312 if ($gcontacts[0]["created"] <= DBA::NULL_DATETIME) {
313 $contact['created'] = DateTimeFormat::utcNow();
318 $server_url = Strings::normaliseLink(self::detectServer($profile));
321 if (($server_url == '') && ($gcontacts[0]["server_url"] != "")) {
322 $server_url = $gcontacts[0]["server_url"];
325 if (!$force && (($server_url == '') || ($gcontacts[0]["server_url"] == $gcontacts[0]["nurl"]))) {
326 $server_url = Strings::normaliseLink(self::detectServer($profile));
329 if (!in_array($gcontacts[0]["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::FEED, Protocol::OSTATUS, ""])) {
330 Logger::log("Profile ".$profile.": Network type ".$gcontacts[0]["network"]." can't be checked", Logger::DEBUG);
334 if ($server_url != "") {
335 if (!self::checkServer($server_url, $gcontacts[0]["network"], $force)) {
337 $fields = ['last_failure' => DateTimeFormat::utcNow()];
338 DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]);
341 Logger::log("Profile ".$profile.": Server ".$server_url." wasn't reachable.", Logger::DEBUG);
344 $contact['server_url'] = $server_url;
347 if (in_array($gcontacts[0]["network"], ["", Protocol::FEED])) {
349 "SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''",
350 DBA::escape(Strings::normaliseLink($server_url))
354 $contact['network'] = $server[0]["network"];
360 // noscrape is really fast so we don't cache the call.
361 if (($server_url != "") && ($gcontacts[0]["nick"] != "")) {
362 // Use noscrape if possible
363 $server = q("SELECT `noscrape`, `network` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", DBA::escape(Strings::normaliseLink($server_url)));
366 $curlResult = Network::curl($server[0]["noscrape"]."/".$gcontacts[0]["nick"]);
368 if ($curlResult->isSuccess() && ($curlResult->getBody() != "")) {
369 $noscrape = json_decode($curlResult->getBody(), true);
371 if (is_array($noscrape)) {
372 $contact["network"] = $server[0]["network"];
374 if (isset($noscrape["fn"])) {
375 $contact["name"] = $noscrape["fn"];
377 if (isset($noscrape["comm"])) {
378 $contact["community"] = $noscrape["comm"];
380 if (isset($noscrape["tags"])) {
381 $keywords = implode(" ", $noscrape["tags"]);
382 if ($keywords != "") {
383 $contact["keywords"] = $keywords;
387 $location = Profile::formatLocation($noscrape);
389 $contact["location"] = $location;
391 if (isset($noscrape["dfrn-notify"])) {
392 $contact["notify"] = $noscrape["dfrn-notify"];
394 // Remove all fields that are not present in the gcontact table
395 unset($noscrape["fn"]);
396 unset($noscrape["key"]);
397 unset($noscrape["homepage"]);
398 unset($noscrape["comm"]);
399 unset($noscrape["tags"]);
400 unset($noscrape["locality"]);
401 unset($noscrape["region"]);
402 unset($noscrape["country-name"]);
403 unset($noscrape["contacts"]);
404 unset($noscrape["dfrn-request"]);
405 unset($noscrape["dfrn-confirm"]);
406 unset($noscrape["dfrn-notify"]);
407 unset($noscrape["dfrn-poll"]);
409 // Set the date of the last contact
410 /// @todo By now the function "update_gcontact" doesn't work with this field
411 //$contact["last_contact"] = DateTimeFormat::utcNow();
413 $contact = array_merge($contact, $noscrape);
415 GContact::update($contact);
417 if (!empty($noscrape["updated"])) {
418 $fields = ['last_contact' => DateTimeFormat::utcNow()];
419 DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]);
421 Logger::log("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", Logger::DEBUG);
423 return $noscrape["updated"];
430 // If we only can poll the feed, then we only do this once a while
431 if (!$force && !self::updateNeeded($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) {
432 Logger::log("Profile ".$profile." was last updated at ".$gcontacts[0]["updated"]." (cached)", Logger::DEBUG);
434 GContact::update($contact);
435 return $gcontacts[0]["updated"];
438 $data = Probe::uri($profile);
440 // Is the profile link the alternate OStatus link notation? (http://domain.tld/user/4711)
441 // Then check the other link and delete this one
442 if (($data["network"] == Protocol::OSTATUS) && self::alternateOStatusUrl($profile)
443 && (Strings::normaliseLink($profile) == Strings::normaliseLink($data["alias"]))
444 && (Strings::normaliseLink($profile) != Strings::normaliseLink($data["url"]))
446 // Delete the old entry
447 DBA::delete('gcontact', ['nurl' => Strings::normaliseLink($profile)]);
449 $gcontact = array_merge($gcontacts[0], $data);
451 $gcontact["server_url"] = $data["baseurl"];
454 $gcontact = GContact::sanitize($gcontact);
455 GContact::update($gcontact);
457 self::lastUpdated($data["url"], $force);
458 } catch (Exception $e) {
459 Logger::log($e->getMessage(), Logger::DEBUG);
462 Logger::log("Profile ".$profile." was deleted", Logger::DEBUG);
466 if (($data["poll"] == "") || (in_array($data["network"], [Protocol::FEED, Protocol::PHANTOM]))) {
467 $fields = ['last_failure' => DateTimeFormat::utcNow()];
468 DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]);
470 Logger::log("Profile ".$profile." wasn't reachable (profile)", Logger::DEBUG);
474 $contact = array_merge($contact, $data);
476 $contact["server_url"] = $data["baseurl"];
478 GContact::update($contact);
480 $curlResult = Network::curl($data["poll"]);
482 if (!$curlResult->isSuccess()) {
483 $fields = ['last_failure' => DateTimeFormat::utcNow()];
484 DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]);
486 Logger::log("Profile ".$profile." wasn't reachable (no feed)", Logger::DEBUG);
490 $doc = new DOMDocument();
491 /// @TODO Avoid error supression here
492 @$doc->loadXML($curlResult->getBody());
494 $xpath = new DOMXPath($doc);
495 $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
497 $entries = $xpath->query('/atom:feed/atom:entry');
501 foreach ($entries as $entry) {
502 $published = DateTimeFormat::utc($xpath->query('atom:published/text()', $entry)->item(0)->nodeValue);
503 $updated = DateTimeFormat::utc($xpath->query('atom:updated/text()' , $entry)->item(0)->nodeValue);
505 if ($last_updated < $published) {
506 $last_updated = $published;
509 if ($last_updated < $updated) {
510 $last_updated = $updated;
514 // Maybe there aren't any entries. Then check if it is a valid feed
515 if ($last_updated == "") {
516 if ($xpath->query('/atom:feed')->length > 0) {
517 $last_updated = DBA::NULL_DATETIME;
521 $fields = ['last_contact' => DateTimeFormat::utcNow()];
523 if (!empty($last_updated)) {
524 $fields['updated'] = $last_updated;
527 DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]);
529 if (($gcontacts[0]["generation"] == 0)) {
530 $fields = ['generation' => 9];
531 DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]);
534 Logger::log("Profile ".$profile." was last updated at ".$last_updated, Logger::DEBUG);
536 return $last_updated;
539 public static function updateNeeded($created, $updated, $last_failure, $last_contact)
541 $now = strtotime(DateTimeFormat::utcNow());
543 if ($updated > $last_contact) {
544 $contact_time = strtotime($updated);
546 $contact_time = strtotime($last_contact);
549 $failure_time = strtotime($last_failure);
550 $created_time = strtotime($created);
552 // If there is no "created" time then use the current time
553 if ($created_time <= 0) {
554 $created_time = $now;
557 // If the last contact was less than 24 hours then don't update
558 if (($now - $contact_time) < (60 * 60 * 24)) {
562 // If the last failure was less than 24 hours then don't update
563 if (($now - $failure_time) < (60 * 60 * 24)) {
567 // If the last contact was less than a week ago and the last failure is older than a week then don't update
568 //if ((($now - $contact_time) < (60 * 60 * 24 * 7)) && ($contact_time > $failure_time))
571 // 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
572 if ((($now - $contact_time) > (60 * 60 * 24 * 7)) && (($now - $created_time) > (60 * 60 * 24 * 7)) && (($now - $failure_time) < (60 * 60 * 24 * 7))) {
576 // 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
577 if ((($now - $contact_time) > (60 * 60 * 24 * 30)) && (($now - $created_time) > (60 * 60 * 24 * 30)) && (($now - $failure_time) < (60 * 60 * 24 * 30))) {
584 /// @TODO Maybe move this out to an utilities class?
585 private static function toBoolean($val)
587 if (($val == "true") || ($val == 1)) {
589 } elseif (($val == "false") || ($val == 0)) {
597 * @brief Detect server type (Hubzilla or Friendica) via the poco data
599 * @param array $data POCO data
600 * @return array Server data
602 private static function detectPocoData(array $data)
604 if (!isset($data['entry'])) {
608 if (count($data['entry']) == 0) {
612 if (!isset($data['entry'][0]['urls'])) {
616 if (count($data['entry'][0]['urls']) == 0) {
620 foreach ($data['entry'][0]['urls'] as $url) {
621 if ($url['type'] == 'zot') {
623 $server["platform"] = 'Hubzilla';
624 $server["network"] = Protocol::DIASPORA;
632 * @brief Detect server type by using the nodeinfo data
634 * @param string $server_url address of the server
635 * @return array Server data
636 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
638 private static function fetchNodeinfo($server_url)
640 $curlResult = Network::curl($server_url."/.well-known/nodeinfo");
641 if (!$curlResult->isSuccess()) {
645 $nodeinfo = json_decode($curlResult->getBody(), true);
647 if (!is_array($nodeinfo) || !isset($nodeinfo['links'])) {
654 foreach ($nodeinfo['links'] as $link) {
655 if (!is_array($link) || empty($link['rel'])) {
656 Logger::log('Invalid nodeinfo format for ' . $server_url, Logger::DEBUG);
659 if ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/1.0') {
660 $nodeinfo1_url = $link['href'];
661 } elseif ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/2.0') {
662 $nodeinfo2_url = $link['href'];
666 if ($nodeinfo1_url . $nodeinfo2_url == '') {
672 // When the nodeinfo url isn't on the same host, then there is obviously something wrong
673 if (!empty($nodeinfo2_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo2_url, PHP_URL_HOST))) {
674 $server = self::parseNodeinfo2($nodeinfo2_url);
677 // When the nodeinfo url isn't on the same host, then there is obviously something wrong
678 if (empty($server) && !empty($nodeinfo1_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo1_url, PHP_URL_HOST))) {
679 $server = self::parseNodeinfo1($nodeinfo1_url);
686 * @brief Parses Nodeinfo 1
688 * @param string $nodeinfo_url address of the nodeinfo path
689 * @return array Server data
690 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
692 private static function parseNodeinfo1($nodeinfo_url)
694 $curlResult = Network::curl($nodeinfo_url);
696 if (!$curlResult->isSuccess()) {
700 $nodeinfo = json_decode($curlResult->getBody(), true);
702 if (!is_array($nodeinfo)) {
708 $server['register_policy'] = REGISTER_CLOSED;
710 if (is_bool($nodeinfo['openRegistrations']) && $nodeinfo['openRegistrations']) {
711 $server['register_policy'] = REGISTER_OPEN;
714 if (is_array($nodeinfo['software'])) {
715 if (isset($nodeinfo['software']['name'])) {
716 $server['platform'] = $nodeinfo['software']['name'];
719 if (isset($nodeinfo['software']['version'])) {
720 $server['version'] = $nodeinfo['software']['version'];
721 // Version numbers on Nodeinfo are presented with additional info, e.g.:
722 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
723 $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']);
727 if (isset($nodeinfo['metadata']['nodeName'])) {
728 $server['site_name'] = $nodeinfo['metadata']['nodeName'];
731 if (!empty($nodeinfo['usage']['users']['total'])) {
732 $server['registered-users'] = $nodeinfo['usage']['users']['total'];
739 if (is_array($nodeinfo['protocols']['inbound'])) {
740 foreach ($nodeinfo['protocols']['inbound'] as $inbound) {
741 if ($inbound == 'diaspora') {
744 if ($inbound == 'friendica') {
747 if ($inbound == 'gnusocial') {
754 $server['network'] = Protocol::OSTATUS;
757 $server['network'] = Protocol::DIASPORA;
760 $server['network'] = Protocol::DFRN;
771 * @brief Parses Nodeinfo 2
773 * @param string $nodeinfo_url address of the nodeinfo path
774 * @return array Server data
775 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
777 private static function parseNodeinfo2($nodeinfo_url)
779 $curlResult = Network::curl($nodeinfo_url);
780 if (!$curlResult->isSuccess()) {
784 $nodeinfo = json_decode($curlResult->getBody(), true);
786 if (!is_array($nodeinfo)) {
792 $server['register_policy'] = REGISTER_CLOSED;
794 if (is_bool($nodeinfo['openRegistrations']) && $nodeinfo['openRegistrations']) {
795 $server['register_policy'] = REGISTER_OPEN;
798 if (is_array($nodeinfo['software'])) {
799 if (isset($nodeinfo['software']['name'])) {
800 $server['platform'] = $nodeinfo['software']['name'];
803 if (isset($nodeinfo['software']['version'])) {
804 $server['version'] = $nodeinfo['software']['version'];
805 // Version numbers on Nodeinfo are presented with additional info, e.g.:
806 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
807 $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']);
811 if (isset($nodeinfo['metadata']['nodeName'])) {
812 $server['site_name'] = $nodeinfo['metadata']['nodeName'];
815 if (!empty($nodeinfo['usage']['users']['total'])) {
816 $server['registered-users'] = $nodeinfo['usage']['users']['total'];
823 if (!empty($nodeinfo['protocols'])) {
824 foreach ($nodeinfo['protocols'] as $protocol) {
825 if ($protocol == 'diaspora') {
827 } elseif ($protocol == 'friendica') {
829 } elseif ($protocol == 'gnusocial') {
836 $server['network'] = Protocol::OSTATUS;
837 } elseif ($diaspora) {
838 $server['network'] = Protocol::DIASPORA;
839 } elseif ($friendica) {
840 $server['network'] = Protocol::DFRN;
843 if (empty($server)) {
851 * @brief Detect server type (Hubzilla or Friendica) via the front page body
853 * @param string $body Front page of the server
854 * @return array Server data
856 private static function detectServerType($body)
860 $doc = new DOMDocument();
861 /// @TODO Acoid supressing error
862 @$doc->loadHTML($body);
863 $xpath = new DOMXPath($doc);
865 $list = $xpath->query("//meta[@name]");
867 foreach ($list as $node) {
869 if ($node->attributes->length) {
870 foreach ($node->attributes as $attribute) {
871 $attr[$attribute->name] = $attribute->value;
874 if ($attr['name'] == 'generator') {
875 $version_part = explode(" ", $attr['content']);
876 if (count($version_part) == 2) {
877 if (in_array($version_part[0], ["Friendika", "Friendica"])) {
879 $server["platform"] = $version_part[0];
880 $server["version"] = $version_part[1];
881 $server["network"] = Protocol::DFRN;
888 $list = $xpath->query("//meta[@property]");
890 foreach ($list as $node) {
892 if ($node->attributes->length) {
893 foreach ($node->attributes as $attribute) {
894 $attr[$attribute->name] = $attribute->value;
897 if ($attr['property'] == 'generator' && in_array($attr['content'], ["hubzilla", "BlaBlaNet"])) {
899 $server["platform"] = $attr['content'];
900 $server["version"] = "";
901 $server["network"] = Protocol::DIASPORA;
910 $server["site_name"] = XML::getFirstNodeValue($xpath, '//head/title/text()');
915 public static function checkServer($server_url, $network = "", $force = false)
917 // Unify the server address
918 $server_url = trim($server_url, "/");
919 $server_url = str_replace("/index.php", "", $server_url);
921 if ($server_url == "") {
925 $gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($server_url)]);
926 if (DBA::isResult($gserver)) {
927 if ($gserver["created"] <= DBA::NULL_DATETIME) {
928 $fields = ['created' => DateTimeFormat::utcNow()];
929 $condition = ['nurl' => Strings::normaliseLink($server_url)];
930 DBA::update('gserver', $fields, $condition);
932 $poco = $gserver["poco"];
933 $noscrape = $gserver["noscrape"];
935 if ($network == "") {
936 $network = $gserver["network"];
939 $last_contact = $gserver["last_contact"];
940 $last_failure = $gserver["last_failure"];
941 $version = $gserver["version"];
942 $platform = $gserver["platform"];
943 $site_name = $gserver["site_name"];
944 $info = $gserver["info"];
945 $register_policy = $gserver["register_policy"];
946 $registered_users = $gserver["registered-users"];
948 // See discussion under https://forum.friendi.ca/display/0b6b25a8135aabc37a5a0f5684081633
949 // It can happen that a zero date is in the database, but storing it again is forbidden.
950 if ($last_contact < DBA::NULL_DATETIME) {
951 $last_contact = DBA::NULL_DATETIME;
954 if ($last_failure < DBA::NULL_DATETIME) {
955 $last_failure = DBA::NULL_DATETIME;
958 if (!$force && !self::updateNeeded($gserver["created"], "", $last_failure, $last_contact)) {
959 Logger::log("Use cached data for server ".$server_url, Logger::DEBUG);
960 return ($last_contact >= $last_failure);
969 $register_policy = -1;
970 $registered_users = 0;
972 $last_contact = DBA::NULL_DATETIME;
973 $last_failure = DBA::NULL_DATETIME;
975 Logger::log("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$gserver["created"]." Failure: ".$last_failure." Contact: ".$last_contact, Logger::DEBUG);
978 $possible_failure = false;
979 $orig_last_failure = $last_failure;
980 $orig_last_contact = $last_contact;
982 // Mastodon uses the "@" for user profiles.
983 // But this can be misunderstood.
984 if (parse_url($server_url, PHP_URL_USER) != '') {
985 DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($server_url)]);
989 // Check if the page is accessible via SSL.
990 $orig_server_url = $server_url;
991 $server_url = str_replace("http://", "https://", $server_url);
993 // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
994 $curlResult = Network::curl($server_url."/.well-known/host-meta", false, $redirects, ['timeout' => 20]);
996 // Quit if there is a timeout.
997 // But we want to make sure to only quit if we are mostly sure that this server url fits.
998 if (DBA::isResult($gserver) && ($orig_server_url == $server_url) &&
999 ($curlResult->isTimeout())) {
1000 Logger::log("Connection to server ".$server_url." timed out.", Logger::DEBUG);
1001 DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($server_url)]);
1005 // Maybe the page is unencrypted only?
1006 $xmlobj = @simplexml_load_string($curlResult->getBody(), 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
1007 if (!$curlResult->isSuccess() || ($curlResult->getBody() == "") || empty($xmlobj) || !is_object($xmlobj)) {
1008 $server_url = str_replace("https://", "http://", $server_url);
1010 // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
1011 $curlResult = Network::curl($server_url."/.well-known/host-meta", false, $redirects, ['timeout' => 20]);
1013 // Quit if there is a timeout
1014 if ($curlResult->isTimeout()) {
1015 Logger::log("Connection to server " . $server_url . " timed out.", Logger::DEBUG);
1016 DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($server_url)]);
1020 $xmlobj = @simplexml_load_string($curlResult->getBody(), 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
1023 if (!$curlResult->isSuccess() || ($curlResult->getBody() == "") || empty($xmlobj) || !is_object($xmlobj)) {
1024 // Workaround for bad configured servers (known nginx problem)
1025 if (!empty($curlResult->getInfo()) && !in_array($curlResult->getInfo()["http_code"], ["403", "404"])) {
1029 $possible_failure = true;
1032 // If the server has no possible failure we reset the cached data
1033 if (!$possible_failure) {
1038 $register_policy = -1;
1042 // This will be too low, but better than no value at all.
1043 $registered_users = DBA::count('gcontact', ['server_url' => Strings::normaliseLink($server_url)]);
1048 $curlResult = Network::curl($server_url."/poco");
1050 if ($curlResult->isSuccess()) {
1051 $data = json_decode($curlResult->getBody(), true);
1053 if (isset($data['totalResults'])) {
1054 $registered_users = $data['totalResults'];
1055 $poco = $server_url . "/poco";
1056 $server = self::detectPocoData($data);
1058 if (!empty($server)) {
1059 $platform = $server['platform'];
1060 $network = $server['network'];
1067 * There are servers out there who don't return 404 on a failure
1068 * We have to be sure that don't misunderstand this
1070 if (is_null($data)) {
1079 // Test for Diaspora, Hubzilla, Mastodon or older Friendica servers
1080 $curlResult = Network::curl($server_url);
1082 if (!$curlResult->isSuccess() || ($curlResult->getBody() == "")) {
1085 $server = self::detectServerType($curlResult->getBody());
1087 if (!empty($server)) {
1088 $platform = $server['platform'];
1089 $network = $server['network'];
1090 $version = $server['version'];
1091 $site_name = $server['site_name'];
1094 $lines = explode("\n", $curlResult->getHeader());
1096 if (count($lines)) {
1097 foreach ($lines as $line) {
1098 $line = trim($line);
1100 if (stristr($line, 'X-Diaspora-Version:')) {
1101 $platform = "Diaspora";
1102 $version = trim(str_replace("X-Diaspora-Version:", "", $line));
1103 $version = trim(str_replace("x-diaspora-version:", "", $version));
1104 $network = Protocol::DIASPORA;
1105 $versionparts = explode("-", $version);
1106 $version = $versionparts[0];
1109 if (stristr($line, 'Server: Mastodon')) {
1110 $platform = "Mastodon";
1111 $network = Protocol::OSTATUS;
1118 if (!$failure && ($poco == "")) {
1119 // Test for Statusnet
1120 // Will also return data for Friendica and GNU Social - but it will be overwritten later
1121 // The "not implemented" is a special treatment for really, really old Friendica versions
1122 $curlResult = Network::curl($server_url."/api/statusnet/version.json");
1124 if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') &&
1125 ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) {
1126 $platform = "StatusNet";
1127 // Remove junk that some GNU Social servers return
1128 $version = str_replace(chr(239).chr(187).chr(191), "", $curlResult->getBody());
1129 $version = trim($version, '"');
1130 $network = Protocol::OSTATUS;
1133 // Test for GNU Social
1134 $curlResult = Network::curl($server_url."/api/gnusocial/version.json");
1136 if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') &&
1137 ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) {
1138 $platform = "GNU Social";
1139 // Remove junk that some GNU Social servers return
1140 $version = str_replace(chr(239) . chr(187) . chr(191), "", $curlResult->getBody());
1141 $version = trim($version, '"');
1142 $network = Protocol::OSTATUS;
1145 // Test for Mastodon
1146 $orig_version = $version;
1147 $curlResult = Network::curl($server_url . "/api/v1/instance");
1149 if ($curlResult->isSuccess() && ($curlResult->getBody() != '')) {
1150 $data = json_decode($curlResult->getBody(), true);
1152 if (isset($data['version'])) {
1153 $platform = "Mastodon";
1154 $version = defaults($data, 'version', '');
1155 $site_name = defaults($data, 'title', '');
1156 $info = defaults($data, 'description', '');
1157 $network = Protocol::OSTATUS;
1160 if (!empty($data['stats']['user_count'])) {
1161 $registered_users = $data['stats']['user_count'];
1165 if (strstr($orig_version . $version, 'Pleroma')) {
1166 $platform = 'Pleroma';
1167 $version = trim(str_replace('Pleroma', '', $version));
1172 // Test for Hubzilla and Red
1173 $curlResult = Network::curl($server_url . "/siteinfo.json");
1175 if ($curlResult->isSuccess()) {
1176 $data = json_decode($curlResult->getBody(), true);
1178 if (isset($data['url'])) {
1179 $platform = $data['platform'];
1180 $version = $data['version'];
1181 $network = Protocol::DIASPORA;
1184 if (!empty($data['site_name'])) {
1185 $site_name = $data['site_name'];
1188 if (!empty($data['channels_total'])) {
1189 $registered_users = $data['channels_total'];
1192 if (!empty($data['register_policy'])) {
1193 switch ($data['register_policy']) {
1194 case "REGISTER_OPEN":
1195 $register_policy = REGISTER_OPEN;
1198 case "REGISTER_APPROVE":
1199 $register_policy = REGISTER_APPROVE;
1202 case "REGISTER_CLOSED":
1204 $register_policy = REGISTER_CLOSED;
1209 // Test for Hubzilla, Redmatrix or Friendica
1210 $curlResult = Network::curl($server_url."/api/statusnet/config.json");
1212 if ($curlResult->isSuccess()) {
1213 $data = json_decode($curlResult->getBody(), true);
1215 if (isset($data['site']['server'])) {
1216 if (isset($data['site']['platform'])) {
1217 $platform = $data['site']['platform']['PLATFORM_NAME'];
1218 $version = $data['site']['platform']['STD_VERSION'];
1219 $network = Protocol::DIASPORA;
1222 if (isset($data['site']['BlaBlaNet'])) {
1223 $platform = $data['site']['BlaBlaNet']['PLATFORM_NAME'];
1224 $version = $data['site']['BlaBlaNet']['STD_VERSION'];
1225 $network = Protocol::DIASPORA;
1228 if (isset($data['site']['hubzilla'])) {
1229 $platform = $data['site']['hubzilla']['PLATFORM_NAME'];
1230 $version = $data['site']['hubzilla']['RED_VERSION'];
1231 $network = Protocol::DIASPORA;
1234 if (isset($data['site']['redmatrix'])) {
1235 if (isset($data['site']['redmatrix']['PLATFORM_NAME'])) {
1236 $platform = $data['site']['redmatrix']['PLATFORM_NAME'];
1237 } elseif (isset($data['site']['redmatrix']['RED_PLATFORM'])) {
1238 $platform = $data['site']['redmatrix']['RED_PLATFORM'];
1241 $version = $data['site']['redmatrix']['RED_VERSION'];
1242 $network = Protocol::DIASPORA;
1245 if (isset($data['site']['friendica'])) {
1246 $platform = $data['site']['friendica']['FRIENDICA_PLATFORM'];
1247 $version = $data['site']['friendica']['FRIENDICA_VERSION'];
1248 $network = Protocol::DFRN;
1251 $site_name = $data['site']['name'];
1254 $inviteonly = false;
1257 if (!empty($data['site']['closed'])) {
1258 $closed = self::toBoolean($data['site']['closed']);
1261 if (!empty($data['site']['private'])) {
1262 $private = self::toBoolean($data['site']['private']);
1265 if (!empty($data['site']['inviteonly'])) {
1266 $inviteonly = self::toBoolean($data['site']['inviteonly']);
1269 if (!$closed && !$private and $inviteonly) {
1270 $register_policy = REGISTER_APPROVE;
1271 } elseif (!$closed && !$private) {
1272 $register_policy = REGISTER_OPEN;
1274 $register_policy = REGISTER_CLOSED;
1281 // Query statistics.json. Optional package for Diaspora, Friendica and Redmatrix
1283 $curlResult = Network::curl($server_url . "/statistics.json");
1285 if ($curlResult->isSuccess()) {
1286 $data = json_decode($curlResult->getBody(), true);
1288 if (isset($data['version'])) {
1289 $version = $data['version'];
1290 // Version numbers on statistics.json are presented with additional info, e.g.:
1291 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
1292 $version = preg_replace("=(.+)-(.{4,})=ism", "$1", $version);
1295 if (!empty($data['name'])) {
1296 $site_name = $data['name'];
1299 if (!empty($data['network'])) {
1300 $platform = $data['network'];
1303 if ($platform == "Diaspora") {
1304 $network = Protocol::DIASPORA;
1307 if (!empty($data['registrations_open']) && $data['registrations_open']) {
1308 $register_policy = REGISTER_OPEN;
1310 $register_policy = REGISTER_CLOSED;
1315 // Query nodeinfo. Working for (at least) Diaspora and Friendica.
1317 $server = self::fetchNodeinfo($server_url);
1319 if (!empty($server)) {
1320 $register_policy = $server['register_policy'];
1322 if (isset($server['platform'])) {
1323 $platform = $server['platform'];
1326 if (isset($server['network'])) {
1327 $network = $server['network'];
1330 if (isset($server['version'])) {
1331 $version = $server['version'];
1334 if (isset($server['site_name'])) {
1335 $site_name = $server['site_name'];
1338 if (isset($server['registered-users'])) {
1339 $registered_users = $server['registered-users'];
1344 // Check for noscrape
1345 // Friendica servers could be detected as OStatus servers
1346 if (!$failure && in_array($network, [Protocol::DFRN, Protocol::OSTATUS])) {
1347 $curlResult = Network::curl($server_url . "/friendica/json");
1349 if (!$curlResult->isSuccess()) {
1350 $curlResult = Network::curl($server_url . "/friendika/json");
1353 if ($curlResult->isSuccess()) {
1354 $data = json_decode($curlResult->getBody(), true);
1356 if (isset($data['version'])) {
1357 $network = Protocol::DFRN;
1359 if (!empty($data['no_scrape_url'])) {
1360 $noscrape = $data['no_scrape_url'];
1363 $version = $data['version'];
1365 if (!empty($data['site_name'])) {
1366 $site_name = $data['site_name'];
1369 $info = defaults($data, 'info', '');
1370 $register_policy = defaults($data, 'register_policy', REGISTER_CLOSED);
1371 if (in_array($register_policy, ['REGISTER_CLOSED', 'REGISTER_APPROVE', 'REGISTER_OPEN'])) {
1372 $register_policy = constant($register_policy);
1374 Logger::log("Register policy '$register_policy' from $server_url is invalid.");
1375 $register_policy = REGISTER_CLOSED; // set a default value
1377 $platform = defaults($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::log("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::log("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' => Strings::normaliseLink($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' => Strings::normaliseLink($server_url)]);
1421 } elseif (!$failure) {
1422 $fields['nurl'] = Strings::normaliseLink($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::log("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
1440 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1442 private static function discoverRelay($server_url)
1444 Logger::log("Discover relay data for server " . $server_url, Logger::DEBUG);
1446 $curlResult = Network::curl($server_url . "/.well-known/x-social-relay");
1448 if (!$curlResult->isSuccess()) {
1452 $data = json_decode($curlResult->getBody(), true);
1454 if (!is_array($data)) {
1458 $gserver = DBA::selectFirst('gserver', ['id', 'relay-subscribe', 'relay-scope'], ['nurl' => Strings::normaliseLink($server_url)]);
1460 if (!DBA::isResult($gserver)) {
1464 if (($gserver['relay-subscribe'] != $data['subscribe']) || ($gserver['relay-scope'] != $data['scope'])) {
1465 $fields = ['relay-subscribe' => $data['subscribe'], 'relay-scope' => $data['scope']];
1466 DBA::update('gserver', $fields, ['id' => $gserver['id']]);
1469 DBA::delete('gserver-tag', ['gserver-id' => $gserver['id']]);
1471 if ($data['scope'] == 'tags') {
1474 foreach ($data['tags'] as $tag) {
1475 $tag = mb_strtolower($tag);
1476 if (strlen($tag) < 100) {
1481 foreach ($tags as $tag) {
1482 DBA::insert('gserver-tag', ['gserver-id' => $gserver['id'], 'tag' => $tag], true);
1486 // Create or update the relay contact
1488 if (isset($data['protocols'])) {
1489 if (isset($data['protocols']['diaspora'])) {
1490 $fields['network'] = Protocol::DIASPORA;
1492 if (isset($data['protocols']['diaspora']['receive'])) {
1493 $fields['batch'] = $data['protocols']['diaspora']['receive'];
1494 } elseif (is_string($data['protocols']['diaspora'])) {
1495 $fields['batch'] = $data['protocols']['diaspora'];
1499 if (isset($data['protocols']['dfrn'])) {
1500 $fields['network'] = Protocol::DFRN;
1502 if (isset($data['protocols']['dfrn']['receive'])) {
1503 $fields['batch'] = $data['protocols']['dfrn']['receive'];
1504 } elseif (is_string($data['protocols']['dfrn'])) {
1505 $fields['batch'] = $data['protocols']['dfrn'];
1509 Diaspora::setRelayContact($server_url, $fields);
1513 * @brief Returns a list of all known servers
1514 * @return array List of server urls
1517 public static function serverlist()
1520 "SELECT `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver`
1521 WHERE `network` IN ('%s', '%s', '%s') AND `last_contact` > `last_failure`
1522 ORDER BY `last_contact`
1524 DBA::escape(Protocol::DFRN),
1525 DBA::escape(Protocol::DIASPORA),
1526 DBA::escape(Protocol::OSTATUS)
1529 if (!DBA::isResult($r)) {
1537 * @brief Fetch server list from remote servers and adds them when they are new.
1539 * @param string $poco URL to the POCO endpoint
1540 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1542 private static function fetchServerlist($poco)
1544 $curlResult = Network::curl($poco . "/@server");
1546 if (!$curlResult->isSuccess()) {
1550 $serverlist = json_decode($curlResult->getBody(), true);
1552 if (!is_array($serverlist)) {
1556 foreach ($serverlist as $server) {
1557 $server_url = str_replace("/index.php", "", $server['url']);
1559 $r = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", DBA::escape(Strings::normaliseLink($server_url)));
1561 if (!DBA::isResult($r)) {
1562 Logger::log("Call server check for server ".$server_url, Logger::DEBUG);
1563 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $server_url);
1568 private static function discoverFederation()
1570 $last = Config::get('poco', 'last_federation_discovery');
1573 $next = $last + (24 * 60 * 60);
1575 if ($next > time()) {
1580 // Discover Friendica, Hubzilla and Diaspora servers
1581 $curlResult = Network::fetchUrl("http://the-federation.info/pods.json");
1583 if (!empty($curlResult)) {
1584 $servers = json_decode($curlResult, true);
1586 if (!empty($servers['pods'])) {
1587 foreach ($servers['pods'] as $server) {
1588 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", "https://" . $server['host']);
1593 // Disvover Mastodon servers
1594 if (!Config::get('system', 'ostatus_disabled')) {
1595 $accesstoken = Config::get('system', 'instances_social_key');
1597 if (!empty($accesstoken)) {
1598 $api = 'https://instances.social/api/1.0/instances/list?count=0';
1599 $header = ['Authorization: Bearer '.$accesstoken];
1600 $curlResult = Network::curl($api, false, $redirects, ['headers' => $header]);
1602 if ($curlResult->isSuccess()) {
1603 $servers = json_decode($curlResult->getBody(), true);
1605 foreach ($servers['instances'] as $server) {
1606 $url = (is_null($server['https_score']) ? 'http' : 'https') . '://' . $server['name'];
1607 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $url);
1613 // Currently disabled, since the service isn't available anymore.
1614 // It is not removed since I hope that there will be a successor.
1615 // Discover GNU Social Servers.
1616 //if (!Config::get('system','ostatus_disabled')) {
1617 // $serverdata = "http://gstools.org/api/get_open_instances/";
1619 // $curlResult = Network::curl($serverdata);
1620 // if ($curlResult->isSuccess()) {
1621 // $servers = json_decode($result->getBody(), true);
1623 // foreach($servers['data'] as $server)
1624 // self::checkServer($server['instance_address']);
1628 Config::set('poco', 'last_federation_discovery', time());
1631 public static function discoverSingleServer($id)
1633 $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `id` = %d", intval($id));
1635 if (!DBA::isResult($r)) {
1641 // Discover new servers out there (Works from Friendica version 3.5.2)
1642 self::fetchServerlist($server["poco"]);
1644 // Fetch all users from the other server
1645 $url = $server["poco"] . "/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1647 Logger::log("Fetch all users from the server " . $server["url"], Logger::DEBUG);
1649 $curlResult = Network::curl($url);
1651 if ($curlResult->isSuccess() && !empty($curlResult->getBody())) {
1652 $data = json_decode($curlResult->getBody(), true);
1654 if (!empty($data)) {
1655 self::discoverServer($data, 2);
1658 if (Config::get('system', 'poco_discovery') > 1) {
1659 $timeframe = Config::get('system', 'poco_discovery_since');
1661 if ($timeframe == 0) {
1665 $updatedSince = date(DateTimeFormat::MYSQL, time() - $timeframe * 86400);
1667 // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3)
1668 $url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1672 $curlResult = Network::curl($url);
1674 if ($curlResult->isSuccess() && !empty($curlResult->getBody())) {
1675 Logger::log("Fetch all global contacts from the server " . $server["nurl"], Logger::DEBUG);
1676 $data = json_decode($curlResult->getBody(), true);
1678 if (!empty($data)) {
1679 $success = self::discoverServer($data);
1683 if (!$success && (Config::get('system', 'poco_discovery') > 2)) {
1684 Logger::log("Fetch contacts from users of the server " . $server["nurl"], Logger::DEBUG);
1685 self::discoverServerUsers($data, $server);
1689 $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
1690 DBA::update('gserver', $fields, ['nurl' => $server["nurl"]]);
1694 // If the server hadn't replied correctly, then force a sanity check
1695 self::checkServer($server["url"], $server["network"], true);
1697 // If we couldn't reach the server, we will try it some time later
1698 $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
1699 DBA::update('gserver', $fields, ['nurl' => $server["nurl"]]);
1705 public static function discover($complete = false)
1707 // Update the server list
1708 self::discoverFederation();
1712 $requery_days = intval(Config::get('system', 'poco_requery_days'));
1714 if ($requery_days == 0) {
1718 $last_update = date('c', time() - (60 * 60 * 24 * $requery_days));
1720 $gservers = q("SELECT `id`, `url`, `nurl`, `network`
1722 WHERE `last_contact` >= `last_failure`
1724 AND `last_poco_query` < '%s'
1725 ORDER BY RAND()", DBA::escape($last_update)
1728 if (DBA::isResult($gservers)) {
1729 foreach ($gservers as $gserver) {
1730 if (!self::checkServer($gserver['url'], $gserver['network'])) {
1731 // The server is not reachable? Okay, then we will try it later
1732 $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
1733 DBA::update('gserver', $fields, ['nurl' => $gserver['nurl']]);
1737 Logger::log('Update directory from server ' . $gserver['url'] . ' with ID ' . $gserver['id'], Logger::DEBUG);
1738 Worker::add(PRIORITY_LOW, 'DiscoverPoCo', 'update_server_directory', (int) $gserver['id']);
1740 if (!$complete && ( --$no_of_queries == 0)) {
1747 private static function discoverServerUsers(array $data, array $server)
1749 if (!isset($data['entry'])) {
1753 foreach ($data['entry'] as $entry) {
1756 if (isset($entry['urls'])) {
1757 foreach ($entry['urls'] as $url) {
1758 if ($url['type'] == 'profile') {
1759 $profile_url = $url['value'];
1760 $path_array = explode('/', parse_url($profile_url, PHP_URL_PATH));
1761 $username = end($path_array);
1766 if ($username != '') {
1767 Logger::log('Fetch contacts for the user ' . $username . ' from the server ' . $server['nurl'], Logger::DEBUG);
1769 // Fetch all contacts from a given user from the other server
1770 $url = $server['poco'] . '/' . $username . '/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation';
1772 $curlResult = Network::curl($url);
1774 if ($curlResult->isSuccess()) {
1775 $data = json_decode($curlResult->getBody(), true);
1777 if (!empty($data)) {
1778 self::discoverServer($data, 3);
1785 private static function discoverServer(array $data, $default_generation = 0)
1787 if (empty($data['entry'])) {
1793 foreach ($data['entry'] as $entry) {
1795 $profile_photo = '';
1799 $updated = DBA::NULL_DATETIME;
1805 $generation = $default_generation;
1807 if (!empty($entry['displayName'])) {
1808 $name = $entry['displayName'];
1811 if (isset($entry['urls'])) {
1812 foreach ($entry['urls'] as $url) {
1813 if ($url['type'] == 'profile') {
1814 $profile_url = $url['value'];
1817 if ($url['type'] == 'webfinger') {
1818 $connect_url = str_replace('acct:' , '', $url['value']);
1824 if (isset($entry['photos'])) {
1825 foreach ($entry['photos'] as $photo) {
1826 if ($photo['type'] == 'profile') {
1827 $profile_photo = $photo['value'];
1833 if (isset($entry['updated'])) {
1834 $updated = date(DateTimeFormat::MYSQL, strtotime($entry['updated']));
1837 if (isset($entry['network'])) {
1838 $network = $entry['network'];
1841 if (isset($entry['currentLocation'])) {
1842 $location = $entry['currentLocation'];
1845 if (isset($entry['aboutMe'])) {
1846 $about = HTML::toBBCode($entry['aboutMe']);
1849 if (isset($entry['gender'])) {
1850 $gender = $entry['gender'];
1853 if (isset($entry['generation']) && ($entry['generation'] > 0)) {
1854 $generation = ++$entry['generation'];
1857 if (isset($entry['contactType']) && ($entry['contactType'] >= 0)) {
1858 $contact_type = $entry['contactType'];
1861 if (isset($entry['tags'])) {
1862 foreach ($entry['tags'] as $tag) {
1863 $keywords = implode(", ", $tag);
1867 if ($generation > 0) {
1870 Logger::log("Store profile ".$profile_url, Logger::DEBUG);
1872 $gcontact = ["url" => $profile_url,
1874 "network" => $network,
1875 "photo" => $profile_photo,
1877 "location" => $location,
1878 "gender" => $gender,
1879 "keywords" => $keywords,
1880 "connect" => $connect_url,
1881 "updated" => $updated,
1882 "contact-type" => $contact_type,
1883 "generation" => $generation];
1886 $gcontact = GContact::sanitize($gcontact);
1887 GContact::update($gcontact);
1888 } catch (Exception $e) {
1889 Logger::log($e->getMessage(), Logger::DEBUG);
1892 Logger::log("Done for profile ".$profile_url, Logger::DEBUG);