3 * @file include/socgraph.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 require_once('include/datetime.php');
11 require_once("include/Scrape.php");
12 require_once("include/network.php");
13 require_once("include/html2bbcode.php");
14 require_once("include/Contact.php");
15 require_once("include/Photo.php");
20 * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
21 * and add the entries to the gcontact (Global Contact) table, or update existing entries
22 * if anything (name or photo) has changed.
23 * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
25 * Once the global contact is stored add (if necessary) the contact linkage which associates
26 * the given uid, cid to the global contact entry. There can be many uid/cid combinations
27 * pointing to the same global contact id.
34 function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
39 if((! $url) || (! $uid)) {
40 $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
43 if (dbm::is_result($r)) {
55 $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') ;
57 logger('poco_load: ' . $url, LOGGER_DEBUG);
61 logger('poco_load: returns ' . $s, LOGGER_DATA);
63 logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
65 if(($a->get_curl_code() > 299) || (! $s))
70 logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
72 if(! isset($j->entry))
76 foreach($j->entry as $entry) {
92 $name = $entry->displayName;
94 if (isset($entry->urls)) {
95 foreach ($entry->urls as $url) {
96 if ($url->type == 'profile') {
97 $profile_url = $url->value;
100 if ($url->type == 'webfinger') {
101 $connect_url = str_replace('acct:' , '', $url->value);
106 if (isset($entry->photos)) {
107 foreach ($entry->photos as $photo) {
108 if ($photo->type == 'profile') {
109 $profile_photo = $photo->value;
115 if (isset($entry->updated)) {
116 $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
119 if (isset($entry->network)) {
120 $network = $entry->network;
123 if (isset($entry->currentLocation)) {
124 $location = $entry->currentLocation;
127 if (isset($entry->aboutMe)) {
128 $about = html2bbcode($entry->aboutMe);
131 if (isset($entry->gender)) {
132 $gender = $entry->gender;
135 if (isset($entry->generation) AND ($entry->generation > 0)) {
136 $generation = ++$entry->generation;
139 if (isset($entry->tags)) {
140 foreach($entry->tags as $tag) {
141 $keywords = implode(", ", $tag);
145 if (isset($entry->contactType) AND ($entry->contactType >= 0))
146 $contact_type = $entry->contactType;
148 // If you query a Friendica server for its profiles, the network has to be Friendica
149 /// TODO It could also be a Redmatrix server
151 // $network = NETWORK_DFRN;
153 poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid, $uid, $zcid);
155 $gcontact = array("url" => $profile_url, "contact-type" => $contact_type, "generation" => $generation);
156 update_gcontact($gcontact);
158 // Update the Friendica contacts. Diaspora is doing it via a message. (See include/diaspora.php)
159 // Deactivated because we now update Friendica contacts in dfrn.php
160 //if (($location != "") OR ($about != "") OR ($keywords != "") OR ($gender != ""))
161 // q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s'
162 // WHERE `nurl` = '%s' AND NOT `self` AND `network` = '%s'",
167 // dbesc(normalise_link($profile_url)),
168 // dbesc(NETWORK_DFRN));
170 logger("poco_load: loaded $total entries",LOGGER_DEBUG);
172 q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
180 function poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid = 0, $uid = 0, $zcid = 0) {
184 // 1: Profiles on this server
185 // 2: Contacts of profiles on this server
186 // 3: Contacts of contacts of profiles on this server
191 if ($profile_url == "")
194 $urlparts = parse_url($profile_url);
195 if (!isset($urlparts["scheme"]))
198 if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com",
199 "identi.ca", "alpha.app.net")))
202 // Don't store the statusnet connector as network
203 // We can't simply set this to NETWORK_OSTATUS since the connector could have fetched posts from friendica as well
204 if ($network == NETWORK_STATUSNET)
207 // Assure that there are no parameter fragments in the profile url
208 if (in_array($network, array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, "")))
209 $profile_url = clean_contact_url($profile_url);
211 $alternate = poco_alternate_ostatus_url($profile_url);
213 $orig_updated = $updated;
215 // The global contacts should contain the original picture, not the cached one
216 if (($generation != 1) AND stristr(normalise_link($profile_photo), normalise_link(App::get_baseurl()."/photo/"))) {
220 $r = q("SELECT `network` FROM `contact` WHERE `nurl` = '%s' AND `network` != '' AND `network` != '%s' LIMIT 1",
221 dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET)
223 if (dbm::is_result($r)) {
224 $network = $r[0]["network"];
227 if (($network == "") OR ($network == NETWORK_OSTATUS)) {
228 $r = q("SELECT `network`, `url` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `network` != '' AND `network` != '%s' LIMIT 1",
229 dbesc($profile_url), dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET)
231 if (dbm::is_result($r)) {
232 $network = $r[0]["network"];
233 //$profile_url = $r[0]["url"];
237 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
238 dbesc(normalise_link($profile_url))
242 if (($network == "") AND ($x[0]["network"] != NETWORK_STATUSNET))
243 $network = $x[0]["network"];
245 if ($updated <= NULL_DATE)
246 $updated = $x[0]["updated"];
248 $created = $x[0]["created"];
249 $server_url = $x[0]["server_url"];
250 $nick = $x[0]["nick"];
251 $addr = $x[0]["addr"];
252 $alias = $x[0]["alias"];
253 $notify = $x[0]["notify"];
255 $created = NULL_DATE;
258 $urlparts = parse_url($profile_url);
259 $nick = end(explode("/", $urlparts["path"]));
265 if ((($network == "") OR ($name == "") OR ($addr == "") OR ($profile_photo == "") OR ($server_url == "") OR $alternate)
266 AND poco_reachable($profile_url, $server_url, $network, false)) {
267 $data = probe_url($profile_url);
269 $orig_profile = $profile_url;
271 $network = $data["network"];
272 $name = $data["name"];
273 $nick = $data["nick"];
274 $addr = $data["addr"];
275 $alias = $data["alias"];
276 $notify = $data["notify"];
277 $profile_url = $data["url"];
278 $profile_photo = $data["photo"];
279 $server_url = $data["baseurl"];
281 if ($alternate AND ($network == NETWORK_OSTATUS)) {
282 // Delete the old entry - if it exists
283 $r = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($orig_profile)));
285 q("DELETE FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($orig_profile)));
286 q("DELETE FROM `glink` WHERE `gcid` = %d", intval($r[0]["id"]));
289 // possibly create a new entry
290 poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid, $uid, $zcid);
294 if ($alternate AND ($network == NETWORK_OSTATUS))
297 if (count($x) AND ($x[0]["network"] == "") AND ($network != "")) {
298 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
300 dbesc(normalise_link($profile_url))
304 if (($name == "") OR ($profile_photo == ""))
307 if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
310 logger("profile-check generation: ".$generation." Network: ".$network." URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
312 // We check the server url to be sure that it is a real one
313 $server_url2 = poco_detect_server($profile_url);
315 // We are no sure that it is a correct URL. So we use it in the future
316 if ($server_url2 != "") {
317 $server_url = $server_url2;
320 // The server URL doesn't seem to be valid, so we don't store it.
321 if (!poco_check_server($server_url, $network)) {
325 $gcontact = array("url" => $profile_url,
329 "network" => $network,
330 "photo" => $profile_photo,
332 "location" => $location,
334 "keywords" => $keywords,
335 "server_url" => $server_url,
336 "connect" => $connect_url,
338 "updated" => $updated,
339 "generation" => $generation);
341 $gcid = update_gcontact($gcontact);
346 $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
352 if (! dbm::is_result($r)) {
353 q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
358 dbesc(datetime_convert())
361 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
362 dbesc(datetime_convert()),
373 function poco_reachable($profile, $server = "", $network = "", $force = false) {
376 $server = poco_detect_server($profile);
381 return poco_check_server($server, $network, $force);
384 function poco_detect_server($profile) {
386 // Try to detect the server path based upon some known standard paths
389 if ($server_url == "") {
390 $friendica = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $profile);
391 if ($friendica != $profile) {
392 $server_url = $friendica;
393 $network = NETWORK_DFRN;
397 if ($server_url == "") {
398 $diaspora = preg_replace("=(https?://)(.*)/u/(.*)=ism", "$1$2", $profile);
399 if ($diaspora != $profile) {
400 $server_url = $diaspora;
401 $network = NETWORK_DIASPORA;
405 if ($server_url == "") {
406 $red = preg_replace("=(https?://)(.*)/channel/(.*)=ism", "$1$2", $profile);
407 if ($red != $profile) {
409 $network = NETWORK_DIASPORA;
414 if ($server_url == "") {
415 $mastodon = preg_replace("=(https?://)(.*)/users/(.*)=ism", "$1$2", $profile);
416 if ($mastodon != $profile) {
417 $server_url = $mastodon;
418 $network = NETWORK_OSTATUS;
422 // Numeric OStatus variant
423 if ($server_url == "") {
424 $ostatus = preg_replace("=(https?://)(.*)/user/(.*)=ism", "$1$2", $profile);
425 if ($ostatus != $profile) {
426 $server_url = $ostatus;
427 $network = NETWORK_OSTATUS;
432 if ($server_url == "") {
433 $base = preg_replace("=(https?://)(.*?)/(.*)=ism", "$1$2", $profile);
434 if ($base != $profile) {
436 $network = NETWORK_PHANTOM;
440 if ($server_url == "") {
444 $r = q("SELECT `id` FROM `gserver` WHERE `nurl` = '%s' AND `last_contact` > `last_failure`",
445 dbesc(normalise_link($server_url)));
446 if (dbm::is_result($r)) {
450 // Fetch the host-meta to check if this really is a server
451 $serverret = z_fetch_url($server_url."/.well-known/host-meta");
452 if (!$serverret["success"]) {
459 function poco_alternate_ostatus_url($url) {
460 return(preg_match("=https?://.+/user/\d+=ism", $url, $matches));
463 function poco_last_updated($profile, $force = false) {
465 $gcontacts = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'",
466 dbesc(normalise_link($profile)));
468 if ($gcontacts[0]["created"] <= NULL_DATE)
469 q("UPDATE `gcontact` SET `created` = '%s' WHERE `nurl` = '%s'",
470 dbesc(datetime_convert()), dbesc(normalise_link($profile)));
472 if ($gcontacts[0]["server_url"] != "") {
473 $server_url = $gcontacts[0]["server_url"];
475 if (($server_url == '') OR ($gcontacts[0]["server_url"] == $gcontacts[0]["nurl"])) {
476 $server_url = poco_detect_server($profile);
479 if (!in_array($gcontacts[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_FEED, NETWORK_OSTATUS, ""))) {
480 logger("Profile ".$profile.": Network type ".$gcontacts[0]["network"]." can't be checked", LOGGER_DEBUG);
484 if ($server_url != "") {
485 if (!poco_check_server($server_url, $gcontacts[0]["network"], $force)) {
488 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
489 dbesc(datetime_convert()), dbesc(normalise_link($profile)));
491 logger("Profile ".$profile.": Server ".$server_url." wasn't reachable.", LOGGER_DEBUG);
495 q("UPDATE `gcontact` SET `server_url` = '%s' WHERE `nurl` = '%s'",
496 dbesc($server_url), dbesc(normalise_link($profile)));
499 if (in_array($gcontacts[0]["network"], array("", NETWORK_FEED))) {
500 $server = q("SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''",
501 dbesc(normalise_link($server_url)));
504 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
505 dbesc($server[0]["network"]), dbesc(normalise_link($profile)));
510 // noscrape is really fast so we don't cache the call.
511 if (($gcontacts[0]["server_url"] != "") AND ($gcontacts[0]["nick"] != "")) {
513 // Use noscrape if possible
514 $server = q("SELECT `noscrape`, `network` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", dbesc(normalise_link($gcontacts[0]["server_url"])));
517 $noscraperet = z_fetch_url($server[0]["noscrape"]."/".$gcontacts[0]["nick"]);
519 if ($noscraperet["success"] AND ($noscraperet["body"] != "")) {
521 $noscrape = json_decode($noscraperet["body"], true);
523 if (is_array($noscrape)) {
524 $contact = array("url" => $profile,
525 "network" => $server[0]["network"],
526 "generation" => $gcontacts[0]["generation"]);
528 if (isset($noscrape["fn"]))
529 $contact["name"] = $noscrape["fn"];
531 if (isset($noscrape["comm"]))
532 $contact["community"] = $noscrape["comm"];
534 if (isset($noscrape["tags"])) {
535 $keywords = implode(" ", $noscrape["tags"]);
537 $contact["keywords"] = $keywords;
540 $location = formatted_location($noscrape);
542 $contact["location"] = $location;
544 if (isset($noscrape["dfrn-notify"]))
545 $contact["notify"] = $noscrape["dfrn-notify"];
547 // Remove all fields that are not present in the gcontact table
548 unset($noscrape["fn"]);
549 unset($noscrape["key"]);
550 unset($noscrape["homepage"]);
551 unset($noscrape["comm"]);
552 unset($noscrape["tags"]);
553 unset($noscrape["locality"]);
554 unset($noscrape["region"]);
555 unset($noscrape["country-name"]);
556 unset($noscrape["contacts"]);
557 unset($noscrape["dfrn-request"]);
558 unset($noscrape["dfrn-confirm"]);
559 unset($noscrape["dfrn-notify"]);
560 unset($noscrape["dfrn-poll"]);
562 // Set the date of the last contact
563 /// @todo By now the function "update_gcontact" doesn't work with this field
564 //$contact["last_contact"] = datetime_convert();
566 $contact = array_merge($contact, $noscrape);
568 update_gcontact($contact);
570 if (trim($noscrape["updated"]) != "") {
571 q("UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'",
572 dbesc(datetime_convert()), dbesc(normalise_link($profile)));
574 logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG);
576 return $noscrape["updated"];
583 // If we only can poll the feed, then we only do this once a while
584 if (!$force AND !poco_do_update($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) {
585 logger("Profile ".$profile." was last updated at ".$gcontacts[0]["updated"]." (cached)", LOGGER_DEBUG);
586 return $gcontacts[0]["updated"];
589 $data = probe_url($profile);
591 // Is the profile link the alternate OStatus link notation? (http://domain.tld/user/4711)
592 // Then check the other link and delete this one
593 if (($data["network"] == NETWORK_OSTATUS) AND poco_alternate_ostatus_url($profile) AND
594 (normalise_link($profile) == normalise_link($data["alias"])) AND
595 (normalise_link($profile) != normalise_link($data["url"]))) {
597 // Delete the old entry
598 q("DELETE FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($profile)));
599 q("DELETE FROM `glink` WHERE `gcid` = %d", intval($gcontacts[0]["id"]));
601 poco_check($data["url"], $data["name"], $data["network"], $data["photo"], $gcontacts[0]["about"], $gcontacts[0]["location"],
602 $gcontacts[0]["gender"], $gcontacts[0]["keywords"], $data["addr"], $gcontacts[0]["updated"], $gcontacts[0]["generation"]);
604 poco_last_updated($data["url"], $force);
606 logger("Profile ".$profile." was deleted", LOGGER_DEBUG);
610 if (($data["poll"] == "") OR (in_array($data["network"], array(NETWORK_FEED, NETWORK_PHANTOM)))) {
611 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
612 dbesc(datetime_convert()), dbesc(normalise_link($profile)));
614 logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG);
618 $contact = array("generation" => $gcontacts[0]["generation"]);
620 $contact = array_merge($contact, $data);
622 $contact["server_url"] = $data["baseurl"];
624 unset($contact["batch"]);
625 unset($contact["poll"]);
626 unset($contact["request"]);
627 unset($contact["confirm"]);
628 unset($contact["poco"]);
629 unset($contact["priority"]);
630 unset($contact["pubkey"]);
631 unset($contact["baseurl"]);
633 update_gcontact($contact);
635 $feedret = z_fetch_url($data["poll"]);
637 if (!$feedret["success"]) {
638 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
639 dbesc(datetime_convert()), dbesc(normalise_link($profile)));
641 logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG);
645 $doc = new DOMDocument();
646 @$doc->loadXML($feedret["body"]);
648 $xpath = new DomXPath($doc);
649 $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
651 $entries = $xpath->query('/atom:feed/atom:entry');
655 foreach ($entries AS $entry) {
656 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
657 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
659 if ($last_updated < $published)
660 $last_updated = $published;
662 if ($last_updated < $updated)
663 $last_updated = $updated;
666 // Maybe there aren't any entries. Then check if it is a valid feed
667 if ($last_updated == "")
668 if ($xpath->query('/atom:feed')->length > 0)
669 $last_updated = NULL_DATE;
671 q("UPDATE `gcontact` SET `updated` = '%s', `last_contact` = '%s' WHERE `nurl` = '%s'",
672 dbesc(dbm::date($last_updated)), dbesc(dbm::date()), dbesc(normalise_link($profile)));
674 if (($gcontacts[0]["generation"] == 0))
675 q("UPDATE `gcontact` SET `generation` = 9 WHERE `nurl` = '%s'",
676 dbesc(normalise_link($profile)));
678 logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG);
680 return($last_updated);
683 function poco_do_update($created, $updated, $last_failure, $last_contact) {
684 $now = strtotime(datetime_convert());
686 if ($updated > $last_contact)
687 $contact_time = strtotime($updated);
689 $contact_time = strtotime($last_contact);
691 $failure_time = strtotime($last_failure);
692 $created_time = strtotime($created);
694 // If there is no "created" time then use the current time
695 if ($created_time <= 0)
696 $created_time = $now;
698 // If the last contact was less than 24 hours then don't update
699 if (($now - $contact_time) < (60 * 60 * 24))
702 // If the last failure was less than 24 hours then don't update
703 if (($now - $failure_time) < (60 * 60 * 24))
706 // If the last contact was less than a week ago and the last failure is older than a week then don't update
707 //if ((($now - $contact_time) < (60 * 60 * 24 * 7)) AND ($contact_time > $failure_time))
710 // 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
711 if ((($now - $contact_time) > (60 * 60 * 24 * 7)) AND (($now - $created_time) > (60 * 60 * 24 * 7)) AND (($now - $failure_time) < (60 * 60 * 24 * 7)))
714 // 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
715 if ((($now - $contact_time) > (60 * 60 * 24 * 30)) AND (($now - $created_time) > (60 * 60 * 24 * 30)) AND (($now - $failure_time) < (60 * 60 * 24 * 30)))
721 function poco_to_boolean($val) {
722 if (($val == "true") OR ($val == 1))
724 if (($val == "false") OR ($val == 0))
731 * @brief Detect server type (Hubzilla or Friendica) via the poco data
733 * @param object $data POCO data
734 * @return array Server data
736 function poco_detect_poco_data($data) {
739 if (!isset($data->entry)) {
743 if (count($data->entry) == 0) {
747 if (!isset($data->entry[0]->urls)) {
751 if (count($data->entry[0]->urls) == 0) {
755 foreach ($data->entry[0]->urls AS $url) {
756 if ($url->type == 'zot') {
758 $server["platform"] = 'Hubzilla';
759 $server["network"] = NETWORK_DIASPORA;
767 * @brief Detect server type by using the nodeinfo data
769 * @param string $server_url address of the server
770 * @return array Server data
772 function poco_fetch_nodeinfo($server_url) {
773 $serverret = z_fetch_url($server_url."/.well-known/nodeinfo");
774 if (!$serverret["success"]) {
778 $nodeinfo = json_decode($serverret['body']);
780 if (!is_object($nodeinfo)) {
784 if (!is_array($nodeinfo->links)) {
790 foreach ($nodeinfo->links AS $link) {
791 if ($link->rel == 'http://nodeinfo.diaspora.software/ns/schema/1.0') {
792 $nodeinfo_url = $link->href;
796 if ($nodeinfo_url == '') {
800 $serverret = z_fetch_url($nodeinfo_url);
801 if (!$serverret["success"]) {
805 $nodeinfo = json_decode($serverret['body']);
807 if (!is_object($nodeinfo)) {
813 $server['register_policy'] = REGISTER_CLOSED;
815 if (is_bool($nodeinfo->openRegistrations) AND $nodeinfo->openRegistrations) {
816 $server['register_policy'] = REGISTER_OPEN;
819 if (is_object($nodeinfo->software)) {
820 if (isset($nodeinfo->software->name)) {
821 $server['platform'] = $nodeinfo->software->name;
824 if (isset($nodeinfo->software->version)) {
825 $server['version'] = $nodeinfo->software->version;
826 // Version numbers on Nodeinfo are presented with additional info, e.g.:
827 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
828 $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']);
832 if (is_object($nodeinfo->metadata)) {
833 if (isset($nodeinfo->metadata->nodeName)) {
834 $server['site_name'] = $nodeinfo->metadata->nodeName;
842 if (is_array($nodeinfo->protocols->inbound)) {
843 foreach ($nodeinfo->protocols->inbound AS $inbound) {
844 if ($inbound == 'diaspora') {
847 if ($inbound == 'friendica') {
850 if ($inbound == 'gnusocial') {
857 $server['network'] = NETWORK_OSTATUS;
860 $server['network'] = NETWORK_DIASPORA;
863 $server['network'] = NETWORK_DFRN;
874 * @brief Detect server type (Hubzilla or Friendica) via the front page body
876 * @param string $body Front page of the server
877 * @return array Server data
879 function poco_detect_server_type($body) {
882 $doc = new \DOMDocument();
883 @$doc->loadHTML($body);
884 $xpath = new \DomXPath($doc);
886 $list = $xpath->query("//meta[@name]");
888 foreach ($list as $node) {
890 if ($node->attributes->length) {
891 foreach ($node->attributes as $attribute) {
892 $attr[$attribute->name] = $attribute->value;
895 if ($attr['name'] == 'generator') {
896 $version_part = explode(" ", $attr['content']);
897 if (count($version_part) == 2) {
898 if (in_array($version_part[0], array("Friendika", "Friendica"))) {
900 $server["platform"] = $version_part[0];
901 $server["version"] = $version_part[1];
902 $server["network"] = NETWORK_DFRN;
909 $list = $xpath->query("//meta[@property]");
911 foreach ($list as $node) {
913 if ($node->attributes->length) {
914 foreach ($node->attributes as $attribute) {
915 $attr[$attribute->name] = $attribute->value;
918 if ($attr['property'] == 'generator') {
919 if (in_array($attr['content'], array("hubzilla", "BlaBlaNet"))) {
921 $server["platform"] = $attr['content'];
922 $server["version"] = "";
923 $server["network"] = NETWORK_DIASPORA;
933 $server["site_name"] = $xpath->evaluate($element."//head/title/text()", $context)->item(0)->nodeValue;
937 function poco_check_server($server_url, $network = "", $force = false) {
939 // Unify the server address
940 $server_url = trim($server_url, "/");
941 $server_url = str_replace("/index.php", "", $server_url);
943 if ($server_url == "")
946 $servers = q("SELECT * FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
947 if (dbm::is_result($servers)) {
949 if ($servers[0]["created"] <= NULL_DATE)
950 q("UPDATE `gserver` SET `created` = '%s' WHERE `nurl` = '%s'",
951 dbesc(datetime_convert()), dbesc(normalise_link($server_url)));
953 $poco = $servers[0]["poco"];
954 $noscrape = $servers[0]["noscrape"];
957 $network = $servers[0]["network"];
959 $last_contact = $servers[0]["last_contact"];
960 $last_failure = $servers[0]["last_failure"];
961 $version = $servers[0]["version"];
962 $platform = $servers[0]["platform"];
963 $site_name = $servers[0]["site_name"];
964 $info = $servers[0]["info"];
965 $register_policy = $servers[0]["register_policy"];
967 if (!$force AND !poco_do_update($servers[0]["created"], "", $last_failure, $last_contact)) {
968 logger("Use cached data for server ".$server_url, LOGGER_DEBUG);
969 return ($last_contact >= $last_failure);
978 $register_policy = -1;
980 $last_contact = NULL_DATE;
981 $last_failure = NULL_DATE;
983 logger("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$servers[0]["created"]." Failure: ".$last_failure." Contact: ".$last_contact, LOGGER_DEBUG);
986 $possible_failure = false;
987 $orig_last_failure = $last_failure;
988 $orig_last_contact = $last_contact;
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 = z_fetch_url($server_url."/.well-known/host-meta", false, $redirects, array('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 (dbm::is_result($servers) AND ($orig_server_url == $server_url) AND
1000 ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT)) {
1001 logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
1005 // Maybe the page is unencrypted only?
1006 $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
1007 if (!$serverret["success"] OR ($serverret["body"] == "") OR (@sizeof($xmlobj) == 0) OR !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 $serverret = z_fetch_url($server_url."/.well-known/host-meta", false, $redirects, array('timeout' => 20));
1013 // Quit if there is a timeout
1014 if ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT) {
1015 logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
1019 $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
1022 if (!$serverret["success"] OR ($serverret["body"] == "") OR (sizeof($xmlobj) == 0) OR !is_object($xmlobj)) {
1023 // Workaround for bad configured servers (known nginx problem)
1024 if (!in_array($serverret["debug"]["http_code"], array("403", "404"))) {
1025 $last_failure = datetime_convert();
1028 $possible_failure = true;
1029 } elseif ($network == NETWORK_DIASPORA)
1030 $last_contact = datetime_convert();
1032 // If the server has no possible failure we reset the cached data
1033 if (!$possible_failure) {
1038 $register_policy = -1;
1043 $serverret = z_fetch_url($server_url."/poco");
1044 if ($serverret["success"]) {
1045 $data = json_decode($serverret["body"]);
1046 if (isset($data->totalResults)) {
1047 $poco = $server_url."/poco";
1048 $last_contact = datetime_convert();
1050 $server = poco_detect_poco_data($data);
1052 $platform = $server['platform'];
1053 $network = $server['network'];
1062 // Test for Diaspora, Hubzilla, Mastodon or older Friendica servers
1063 $serverret = z_fetch_url($server_url);
1065 if (!$serverret["success"] OR ($serverret["body"] == "")) {
1066 $last_failure = datetime_convert();
1069 $server = poco_detect_server_type($serverret["body"]);
1071 $platform = $server['platform'];
1072 $network = $server['network'];
1073 $version = $server['version'];
1074 $site_name = $server['site_name'];
1075 $last_contact = datetime_convert();
1078 $lines = explode("\n",$serverret["header"]);
1080 foreach($lines as $line) {
1081 $line = trim($line);
1082 if(stristr($line,'X-Diaspora-Version:')) {
1083 $platform = "Diaspora";
1084 $version = trim(str_replace("X-Diaspora-Version:", "", $line));
1085 $version = trim(str_replace("x-diaspora-version:", "", $version));
1086 $network = NETWORK_DIASPORA;
1087 $versionparts = explode("-", $version);
1088 $version = $versionparts[0];
1089 $last_contact = datetime_convert();
1092 if(stristr($line,'Server: Mastodon')) {
1093 $platform = "Mastodon";
1094 $network = NETWORK_OSTATUS;
1095 // Mastodon doesn't reveal version numbers
1097 $last_contact = datetime_convert();
1104 if (!$failure AND ($poco == "")) {
1105 // Test for Statusnet
1106 // Will also return data for Friendica and GNU Social - but it will be overwritten later
1107 // The "not implemented" is a special treatment for really, really old Friendica versions
1108 $serverret = z_fetch_url($server_url."/api/statusnet/version.json");
1109 if ($serverret["success"] AND ($serverret["body"] != '{"error":"not implemented"}') AND
1110 ($serverret["body"] != '') AND (strlen($serverret["body"]) < 30)) {
1111 $platform = "StatusNet";
1112 // Remove junk that some GNU Social servers return
1113 $version = str_replace(chr(239).chr(187).chr(191), "", $serverret["body"]);
1114 $version = trim($version, '"');
1115 $network = NETWORK_OSTATUS;
1116 $last_contact = datetime_convert();
1119 // Test for GNU Social
1120 $serverret = z_fetch_url($server_url."/api/gnusocial/version.json");
1121 if ($serverret["success"] AND ($serverret["body"] != '{"error":"not implemented"}') AND
1122 ($serverret["body"] != '') AND (strlen($serverret["body"]) < 30)) {
1123 $platform = "GNU Social";
1124 // Remove junk that some GNU Social servers return
1125 $version = str_replace(chr(239).chr(187).chr(191), "", $serverret["body"]);
1126 $version = trim($version, '"');
1127 $network = NETWORK_OSTATUS;
1128 $last_contact = datetime_convert();
1133 // Test for Hubzilla, Redmatrix or Friendica
1134 $serverret = z_fetch_url($server_url."/api/statusnet/config.json");
1135 if ($serverret["success"]) {
1136 $data = json_decode($serverret["body"]);
1137 if (isset($data->site->server)) {
1138 $last_contact = datetime_convert();
1140 if (isset($data->site->platform)) {
1141 $platform = $data->site->platform->PLATFORM_NAME;
1142 $version = $data->site->platform->STD_VERSION;
1143 $network = NETWORK_DIASPORA;
1145 if (isset($data->site->BlaBlaNet)) {
1146 $platform = $data->site->BlaBlaNet->PLATFORM_NAME;
1147 $version = $data->site->BlaBlaNet->STD_VERSION;
1148 $network = NETWORK_DIASPORA;
1150 if (isset($data->site->hubzilla)) {
1151 $platform = $data->site->hubzilla->PLATFORM_NAME;
1152 $version = $data->site->hubzilla->RED_VERSION;
1153 $network = NETWORK_DIASPORA;
1155 if (isset($data->site->redmatrix)) {
1156 if (isset($data->site->redmatrix->PLATFORM_NAME))
1157 $platform = $data->site->redmatrix->PLATFORM_NAME;
1158 elseif (isset($data->site->redmatrix->RED_PLATFORM))
1159 $platform = $data->site->redmatrix->RED_PLATFORM;
1161 $version = $data->site->redmatrix->RED_VERSION;
1162 $network = NETWORK_DIASPORA;
1164 if (isset($data->site->friendica)) {
1165 $platform = $data->site->friendica->FRIENDICA_PLATFORM;
1166 $version = $data->site->friendica->FRIENDICA_VERSION;
1167 $network = NETWORK_DFRN;
1170 $site_name = $data->site->name;
1172 $data->site->closed = poco_to_boolean($data->site->closed);
1173 $data->site->private = poco_to_boolean($data->site->private);
1174 $data->site->inviteonly = poco_to_boolean($data->site->inviteonly);
1176 if (!$data->site->closed AND !$data->site->private and $data->site->inviteonly)
1177 $register_policy = REGISTER_APPROVE;
1178 elseif (!$data->site->closed AND !$data->site->private)
1179 $register_policy = REGISTER_OPEN;
1181 $register_policy = REGISTER_CLOSED;
1187 // Query statistics.json. Optional package for Diaspora, Friendica and Redmatrix
1189 $serverret = z_fetch_url($server_url."/statistics.json");
1190 if ($serverret["success"]) {
1191 $data = json_decode($serverret["body"]);
1192 if (isset($data->version)) {
1193 $version = $data->version;
1194 // Version numbers on statistics.json are presented with additional info, e.g.:
1195 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
1196 $version = preg_replace("=(.+)-(.{4,})=ism", "$1", $version);
1199 $site_name = $data->name;
1201 if (isset($data->network)) {
1202 $platform = $data->network;
1205 if ($platform == "Diaspora") {
1206 $network = NETWORK_DIASPORA;
1209 if ($data->registrations_open) {
1210 $register_policy = REGISTER_OPEN;
1212 $register_policy = REGISTER_CLOSED;
1215 if (isset($data->version))
1216 $last_contact = datetime_convert();
1220 // Query nodeinfo. Working for (at least) Diaspora and Friendica.
1222 $server = poco_fetch_nodeinfo($server_url);
1224 $register_policy = $server['register_policy'];
1226 if (isset($server['platform'])) {
1227 $platform = $server['platform'];
1230 if (isset($server['network'])) {
1231 $network = $server['network'];
1234 if (isset($server['version'])) {
1235 $version = $server['version'];
1238 if (isset($server['site_name'])) {
1239 $site_name = $server['site_name'];
1242 $last_contact = datetime_convert();
1246 // Check for noscrape
1247 // Friendica servers could be detected as OStatus servers
1248 if (!$failure AND in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS))) {
1249 $serverret = z_fetch_url($server_url."/friendica/json");
1251 if (!$serverret["success"])
1252 $serverret = z_fetch_url($server_url."/friendika/json");
1254 if ($serverret["success"]) {
1255 $data = json_decode($serverret["body"]);
1257 if (isset($data->version)) {
1258 $last_contact = datetime_convert();
1259 $network = NETWORK_DFRN;
1261 $noscrape = $data->no_scrape_url;
1262 $version = $data->version;
1263 $site_name = $data->site_name;
1264 $info = $data->info;
1265 $register_policy_str = $data->register_policy;
1266 $platform = $data->platform;
1268 switch ($register_policy_str) {
1269 case "REGISTER_CLOSED":
1270 $register_policy = REGISTER_CLOSED;
1272 case "REGISTER_APPROVE":
1273 $register_policy = REGISTER_APPROVE;
1275 case "REGISTER_OPEN":
1276 $register_policy = REGISTER_OPEN;
1283 if ($possible_failure AND !$failure) {
1284 $last_failure = datetime_convert();
1289 $last_contact = $orig_last_contact;
1291 $last_failure = $orig_last_failure;
1294 if (($last_contact <= $last_failure) AND !$failure) {
1295 logger("Server ".$server_url." seems to be alive, but last contact wasn't set - could be a bug", LOGGER_DEBUG);
1296 } else if (($last_contact >= $last_failure) AND $failure) {
1297 logger("Server ".$server_url." seems to be dead, but last failure wasn't set - could be a bug", LOGGER_DEBUG);
1300 // Check again if the server exists
1301 $servers = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
1303 $version = strip_tags($version);
1304 $site_name = strip_tags($site_name);
1305 $info = strip_tags($info);
1306 $platform = strip_tags($platform);
1309 q("UPDATE `gserver` SET `url` = '%s', `version` = '%s', `site_name` = '%s', `info` = '%s', `register_policy` = %d, `poco` = '%s', `noscrape` = '%s',
1310 `network` = '%s', `platform` = '%s', `last_contact` = '%s', `last_failure` = '%s' WHERE `nurl` = '%s'",
1315 intval($register_policy),
1320 dbesc($last_contact),
1321 dbesc($last_failure),
1322 dbesc(normalise_link($server_url))
1324 } elseif (!$failure) {
1325 q("INSERT INTO `gserver` (`url`, `nurl`, `version`, `site_name`, `info`, `register_policy`, `poco`, `noscrape`, `network`, `platform`, `created`, `last_contact`, `last_failure`)
1326 VALUES ('%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
1328 dbesc(normalise_link($server_url)),
1332 intval($register_policy),
1337 dbesc(datetime_convert()),
1338 dbesc($last_contact),
1339 dbesc($last_failure),
1340 dbesc(datetime_convert())
1343 logger("End discovery for server ".$server_url, LOGGER_DEBUG);
1348 function count_common_friends($uid,$cid) {
1350 $r = q("SELECT count(*) as `total`
1351 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1352 WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND
1353 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
1354 AND `gcontact`.`nurl` IN (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
1361 // logger("count_common_friends: $uid $cid {$r[0]['total']}");
1362 if (dbm::is_result($r))
1363 return $r[0]['total'];
1369 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
1372 $sql_extra = " order by rand() ";
1374 $sql_extra = " order by `gcontact`.`name` asc ";
1376 $r = q("SELECT `gcontact`.*, `contact`.`id` AS `cid`
1378 INNER JOIN `gcontact` ON `glink`.`gcid` = `gcontact`.`id`
1379 INNER JOIN `contact` ON `gcontact`.`nurl` = `contact`.`nurl`
1380 WHERE `glink`.`cid` = %d and `glink`.`uid` = %d
1381 AND `contact`.`uid` = %d AND `contact`.`self` = 0 AND `contact`.`blocked` = 0
1382 AND `contact`.`hidden` = 0 AND `contact`.`id` != %d
1383 AND ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
1384 $sql_extra LIMIT %d, %d",
1398 function count_common_friends_zcid($uid,$zcid) {
1400 $r = q("SELECT count(*) as `total`
1401 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1402 where `glink`.`zcid` = %d
1403 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
1408 if (dbm::is_result($r))
1409 return $r[0]['total'];
1414 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
1417 $sql_extra = " order by rand() ";
1419 $sql_extra = " order by `gcontact`.`name` asc ";
1421 $r = q("SELECT `gcontact`.*
1422 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1423 where `glink`.`zcid` = %d
1424 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
1425 $sql_extra limit %d, %d",
1437 function count_all_friends($uid,$cid) {
1439 $r = q("SELECT count(*) as `total`
1440 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1441 where `glink`.`cid` = %d and `glink`.`uid` = %d AND
1442 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))",
1447 if (dbm::is_result($r))
1448 return $r[0]['total'];
1454 function all_friends($uid,$cid,$start = 0, $limit = 80) {
1456 $r = q("SELECT `gcontact`.*, `contact`.`id` AS `cid`
1458 INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1459 LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl` AND `contact`.`uid` = %d
1460 WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND
1461 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
1462 ORDER BY `gcontact`.`name` ASC LIMIT %d, %d ",
1475 function suggestion_query($uid, $start = 0, $limit = 80) {
1481 // Uncommented because the result of the queries are to big to store it in the cache.
1482 // We need to decide if we want to change the db column type or if we want to delete it.
1483 // $list = Cache::get("suggestion_query:".$uid.":".$start.":".$limit);
1484 // if (!is_null($list)) {
1488 $network = array(NETWORK_DFRN);
1490 if (get_config('system','diaspora_enabled'))
1491 $network[] = NETWORK_DIASPORA;
1493 if (!get_config('system','ostatus_disabled'))
1494 $network[] = NETWORK_OSTATUS;
1496 $sql_network = implode("', '", $network);
1497 $sql_network = "'".$sql_network."'";
1499 /// @todo This query is really slow
1500 // By now we cache the data for five minutes
1501 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
1502 INNER JOIN `glink` ON `glink`.`gcid` = `gcontact`.`id`
1503 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
1504 AND NOT `gcontact`.`name` IN (SELECT `name` FROM `contact` WHERE `uid` = %d)
1505 AND NOT `gcontact`.`id` IN (SELECT `gcid` FROM `gcign` WHERE `uid` = %d)
1506 AND `gcontact`.`updated` >= '%s'
1507 AND `gcontact`.`last_contact` >= `gcontact`.`last_failure`
1508 AND `gcontact`.`network` IN (%s)
1509 GROUP BY `glink`.`gcid` ORDER BY `gcontact`.`updated` DESC,`total` DESC LIMIT %d, %d",
1520 if (dbm::is_result($r) && count($r) >= ($limit -1)) {
1521 // Uncommented because the result of the queries are to big to store it in the cache.
1522 // We need to decide if we want to change the db column type or if we want to delete it.
1523 // Cache::set("suggestion_query:".$uid.":".$start.":".$limit, $r, CACHE_FIVE_MINUTES);
1528 $r2 = q("SELECT gcontact.* FROM gcontact
1529 INNER JOIN `glink` ON `glink`.`gcid` = `gcontact`.`id`
1530 WHERE `glink`.`uid` = 0 AND `glink`.`cid` = 0 AND `glink`.`zcid` = 0 AND NOT `gcontact`.`nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = %d)
1531 AND NOT `gcontact`.`name` IN (SELECT `name` FROM `contact` WHERE `uid` = %d)
1532 AND NOT `gcontact`.`id` IN (SELECT `gcid` FROM `gcign` WHERE `uid` = %d)
1533 AND `gcontact`.`updated` >= '%s'
1534 AND `gcontact`.`last_contact` >= `gcontact`.`last_failure`
1535 AND `gcontact`.`network` IN (%s)
1536 ORDER BY rand() LIMIT %d, %d",
1547 foreach ($r2 AS $suggestion)
1548 $list[$suggestion["nurl"]] = $suggestion;
1550 foreach ($r AS $suggestion)
1551 $list[$suggestion["nurl"]] = $suggestion;
1553 while (sizeof($list) > ($limit))
1556 // Uncommented because the result of the queries are to big to store it in the cache.
1557 // We need to decide if we want to change the db column type or if we want to delete it.
1558 // Cache::set("suggestion_query:".$uid.":".$start.":".$limit, $list, CACHE_FIVE_MINUTES);
1562 function update_suggestions() {
1568 /// @TODO Check if it is really neccessary to poll the own server
1569 poco_load(0,0,0,App::get_baseurl() . '/poco');
1571 $done[] = App::get_baseurl() . '/poco';
1573 if (strlen(get_config('system','directory'))) {
1574 $x = fetch_url(get_server()."/pubsites");
1576 $j = json_decode($x);
1578 foreach ($j->entries as $entry) {
1580 poco_check_server($entry->url);
1582 $url = $entry->url . '/poco';
1583 if (! in_array($url,$done)) {
1584 poco_load(0,0,0,$entry->url . '/poco');
1591 // Query your contacts from Friendica and Redmatrix/Hubzilla for their contacts
1592 $r = q("SELECT DISTINCT(`poco`) AS `poco` FROM `contact` WHERE `network` IN ('%s', '%s')",
1593 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA)
1596 if (dbm::is_result($r)) {
1597 foreach ($r as $rr) {
1598 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
1599 if(! in_array($base,$done))
1600 poco_load(0,0,0,$base);
1606 * @brief Fetch server list from remote servers and adds them when they are new.
1608 * @param string $poco URL to the POCO endpoint
1610 function poco_fetch_serverlist($poco) {
1611 $serverret = z_fetch_url($poco."/@server");
1612 if (!$serverret["success"]) {
1615 $serverlist = json_decode($serverret['body']);
1617 if (!is_array($serverlist)) {
1621 foreach ($serverlist AS $server) {
1622 $server_url = str_replace("/index.php", "", $server->url);
1624 $r = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
1625 if (!dbm::is_result($r)) {
1626 logger("Call server check for server ".$server_url, LOGGER_DEBUG);
1627 proc_run(PRIORITY_LOW, "include/discover_poco.php", "server", base64_encode($server_url));
1632 function poco_discover_federation() {
1633 $last = get_config('poco','last_federation_discovery');
1636 $next = $last + (24 * 60 * 60);
1641 // Discover Friendica, Hubzilla and Diaspora servers
1642 $serverdata = fetch_url("http://the-federation.info/pods.json");
1645 $servers = json_decode($serverdata);
1647 foreach ($servers->pods AS $server) {
1648 proc_run(PRIORITY_LOW, "include/discover_poco.php", "server", base64_encode("https://".$server->host));
1652 // Currently disabled, since the service isn't available anymore.
1653 // It is not removed since I hope that there will be a successor.
1654 // Discover GNU Social Servers.
1655 //if (!get_config('system','ostatus_disabled')) {
1656 // $serverdata = "http://gstools.org/api/get_open_instances/";
1658 // $result = z_fetch_url($serverdata);
1659 // if ($result["success"]) {
1660 // $servers = json_decode($result["body"]);
1662 // foreach($servers->data AS $server)
1663 // poco_check_server($server->instance_address);
1667 set_config('poco','last_federation_discovery', time());
1670 function poco_discover($complete = false) {
1672 // Update the server list
1673 poco_discover_federation();
1677 $requery_days = intval(get_config("system", "poco_requery_days"));
1679 if ($requery_days == 0)
1682 $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
1684 $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `last_contact` >= `last_failure` AND `poco` != '' AND `last_poco_query` < '%s' ORDER BY RAND()", dbesc($last_update));
1686 foreach ($r AS $server) {
1688 if (!poco_check_server($server["url"], $server["network"])) {
1689 // The server is not reachable? Okay, then we will try it later
1690 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
1694 // Discover new servers out there
1695 poco_fetch_serverlist($server["poco"]);
1697 // Fetch all users from the other server
1698 $url = $server["poco"]."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1700 logger("Fetch all users from the server ".$server["nurl"], LOGGER_DEBUG);
1702 $retdata = z_fetch_url($url);
1703 if ($retdata["success"]) {
1704 $data = json_decode($retdata["body"]);
1706 poco_discover_server($data, 2);
1708 if (get_config('system','poco_discovery') > 1) {
1710 $timeframe = get_config('system','poco_discovery_since');
1711 if ($timeframe == 0)
1714 $updatedSince = date("Y-m-d H:i:s", time() - $timeframe * 86400);
1716 // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3)
1717 $url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1721 $retdata = z_fetch_url($url);
1722 if ($retdata["success"]) {
1723 logger("Fetch all global contacts from the server ".$server["nurl"], LOGGER_DEBUG);
1724 $success = poco_discover_server(json_decode($retdata["body"]));
1727 if (!$success AND (get_config('system','poco_discovery') > 2)) {
1728 logger("Fetch contacts from users of the server ".$server["nurl"], LOGGER_DEBUG);
1729 poco_discover_server_users($data, $server);
1733 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
1734 if (!$complete AND (--$no_of_queries == 0))
1737 // If the server hadn't replied correctly, then force a sanity check
1738 poco_check_server($server["url"], $server["network"], true);
1740 // If we couldn't reach the server, we will try it some time later
1741 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
1746 function poco_discover_server_users($data, $server) {
1748 if (!isset($data->entry))
1751 foreach ($data->entry AS $entry) {
1753 if (isset($entry->urls)) {
1754 foreach($entry->urls as $url)
1755 if ($url->type == 'profile') {
1756 $profile_url = $url->value;
1757 $urlparts = parse_url($profile_url);
1758 $username = end(explode("/", $urlparts["path"]));
1761 if ($username != "") {
1762 logger("Fetch contacts for the user ".$username." from the server ".$server["nurl"], LOGGER_DEBUG);
1764 // Fetch all contacts from a given user from the other server
1765 $url = $server["poco"]."/".$username."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1767 $retdata = z_fetch_url($url);
1768 if ($retdata["success"])
1769 poco_discover_server(json_decode($retdata["body"]), 3);
1774 function poco_discover_server($data, $default_generation = 0) {
1776 if (!isset($data->entry) OR !count($data->entry))
1781 foreach ($data->entry AS $entry) {
1783 $profile_photo = '';
1787 $updated = NULL_DATE;
1793 $generation = $default_generation;
1795 $name = $entry->displayName;
1797 if (isset($entry->urls)) {
1798 foreach($entry->urls as $url) {
1799 if ($url->type == 'profile') {
1800 $profile_url = $url->value;
1803 if ($url->type == 'webfinger') {
1804 $connect_url = str_replace('acct:' , '', $url->value);
1810 if (isset($entry->photos)) {
1811 foreach ($entry->photos as $photo) {
1812 if ($photo->type == 'profile') {
1813 $profile_photo = $photo->value;
1819 if (isset($entry->updated)) {
1820 $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
1823 if(isset($entry->network)) {
1824 $network = $entry->network;
1827 if(isset($entry->currentLocation)) {
1828 $location = $entry->currentLocation;
1831 if(isset($entry->aboutMe)) {
1832 $about = html2bbcode($entry->aboutMe);
1835 if(isset($entry->gender)) {
1836 $gender = $entry->gender;
1839 if(isset($entry->generation) AND ($entry->generation > 0)) {
1840 $generation = ++$entry->generation;
1843 if(isset($entry->contactType) AND ($entry->contactType >= 0)) {
1844 $contact_type = $entry->contactType;
1847 if(isset($entry->tags)) {
1848 foreach ($entry->tags as $tag) {
1849 $keywords = implode(", ", $tag);
1853 if ($generation > 0) {
1856 logger("Store profile ".$profile_url, LOGGER_DEBUG);
1857 poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, 0, 0, 0);
1859 $gcontact = array("url" => $profile_url, "contact-type" => $contact_type, "generation" => $generation);
1860 update_gcontact($gcontact);
1862 logger("Done for profile ".$profile_url, LOGGER_DEBUG);
1869 * @brief Removes unwanted parts from a contact url
1871 * @param string $url Contact url
1872 * @return string Contact url with the wanted parts
1874 function clean_contact_url($url) {
1875 $parts = parse_url($url);
1877 if (!isset($parts["scheme"]) OR !isset($parts["host"]))
1880 $new_url = $parts["scheme"]."://".$parts["host"];
1882 if (isset($parts["port"]))
1883 $new_url .= ":".$parts["port"];
1885 if (isset($parts["path"]))
1886 $new_url .= $parts["path"];
1888 if ($new_url != $url)
1889 logger("Cleaned contact url ".$url." to ".$new_url." - Called by: ".App::callstack(), LOGGER_DEBUG);
1895 * @brief Replace alternate OStatus user format with the primary one
1897 * @param arr $contact contact array (called by reference)
1899 function fix_alternate_contact_address(&$contact) {
1900 if (($contact["network"] == NETWORK_OSTATUS) AND poco_alternate_ostatus_url($contact["url"])) {
1901 $data = probe_url($contact["url"]);
1902 if ($contact["network"] == NETWORK_OSTATUS) {
1903 logger("Fix primary url from ".$contact["url"]." to ".$data["url"]." - Called by: ".App::callstack(), LOGGER_DEBUG);
1904 $contact["url"] = $data["url"];
1905 $contact["addr"] = $data["addr"];
1906 $contact["alias"] = $data["alias"];
1907 $contact["server_url"] = $data["baseurl"];
1913 * @brief Fetch the gcontact id, add an entry if not existed
1915 * @param arr $contact contact array
1916 * @return bool|int Returns false if not found, integer if contact was found
1918 function get_gcontact_id($contact) {
1923 if (in_array($contact["network"], array(NETWORK_PHANTOM))) {
1924 logger("Invalid network for contact url ".$contact["url"]." - Called by: ".App::callstack(), LOGGER_DEBUG);
1928 if ($contact["network"] == NETWORK_STATUSNET)
1929 $contact["network"] = NETWORK_OSTATUS;
1931 // All new contacts are hidden by default
1932 if (!isset($contact["hide"]))
1933 $contact["hide"] = true;
1935 // Replace alternate OStatus user format with the primary one
1936 fix_alternate_contact_address($contact);
1938 // Remove unwanted parts from the contact url (e.g. "?zrl=...")
1939 if (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS)))
1940 $contact["url"] = clean_contact_url($contact["url"]);
1942 $r = q("SELECT `id`, `last_contact`, `last_failure`, `network` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 2",
1943 dbesc(normalise_link($contact["url"])));
1946 $gcontact_id = $r[0]["id"];
1948 // Update every 90 days
1949 if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) {
1950 $last_failure_str = $r[0]["last_failure"];
1951 $last_failure = strtotime($r[0]["last_failure"]);
1952 $last_contact_str = $r[0]["last_contact"];
1953 $last_contact = strtotime($r[0]["last_contact"]);
1954 $doprobing = (((time() - $last_contact) > (90 * 86400)) AND ((time() - $last_failure) > (90 * 86400)));
1957 q("INSERT INTO `gcontact` (`name`, `nick`, `addr` , `network`, `url`, `nurl`, `photo`, `created`, `updated`, `location`, `about`, `hide`, `generation`)
1958 VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d)",
1959 dbesc($contact["name"]),
1960 dbesc($contact["nick"]),
1961 dbesc($contact["addr"]),
1962 dbesc($contact["network"]),
1963 dbesc($contact["url"]),
1964 dbesc(normalise_link($contact["url"])),
1965 dbesc($contact["photo"]),
1966 dbesc(datetime_convert()),
1967 dbesc(datetime_convert()),
1968 dbesc($contact["location"]),
1969 dbesc($contact["about"]),
1970 intval($contact["hide"]),
1971 intval($contact["generation"])
1974 $r = q("SELECT `id`, `network` FROM `gcontact` WHERE `nurl` = '%s' ORDER BY `id` LIMIT 2",
1975 dbesc(normalise_link($contact["url"])));
1978 $gcontact_id = $r[0]["id"];
1980 $doprobing = in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""));
1985 logger("Last Contact: ". $last_contact_str." - Last Failure: ".$last_failure_str." - Checking: ".$contact["url"], LOGGER_DEBUG);
1986 proc_run(PRIORITY_LOW, 'include/gprobe.php', bin2hex($contact["url"]));
1989 if ((dbm::is_result($r)) AND (count($r) > 1) AND ($gcontact_id > 0) AND ($contact["url"] != ""))
1990 q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d",
1991 dbesc(normalise_link($contact["url"])),
1992 intval($gcontact_id));
1994 return $gcontact_id;
1998 * @brief Updates the gcontact table from a given array
2000 * @param arr $contact contact array
2001 * @return bool|int Returns false if not found, integer if contact was found
2003 function update_gcontact($contact) {
2005 // Check for invalid "contact-type" value
2006 if (isset($contact['contact-type']) AND (intval($contact['contact-type']) < 0)) {
2007 $contact['contact-type'] = 0;
2010 /// @todo update contact table as well
2012 $gcontact_id = get_gcontact_id($contact);
2017 $r = q("SELECT `name`, `nick`, `photo`, `location`, `about`, `addr`, `generation`, `birthday`, `gender`, `keywords`,
2018 `contact-type`, `hide`, `nsfw`, `network`, `alias`, `notify`, `server_url`, `connect`, `updated`, `url`
2019 FROM `gcontact` WHERE `id` = %d LIMIT 1",
2020 intval($gcontact_id));
2022 // Get all field names
2024 foreach ($r[0] AS $field => $data)
2025 $fields[$field] = $data;
2027 unset($fields["url"]);
2028 unset($fields["updated"]);
2029 unset($fields["hide"]);
2031 // Bugfix: We had an error in the storing of keywords which lead to the "0"
2032 // This value is still transmitted via poco.
2033 if ($contact["keywords"] == "0")
2034 unset($contact["keywords"]);
2036 if ($r[0]["keywords"] == "0")
2037 $r[0]["keywords"] = "";
2039 // assign all unassigned fields from the database entry
2040 foreach ($fields AS $field => $data)
2041 if (!isset($contact[$field]) OR ($contact[$field] == ""))
2042 $contact[$field] = $r[0][$field];
2044 if (!isset($contact["hide"]))
2045 $contact["hide"] = $r[0]["hide"];
2047 $fields["hide"] = $r[0]["hide"];
2049 if ($contact["network"] == NETWORK_STATUSNET)
2050 $contact["network"] = NETWORK_OSTATUS;
2052 // Replace alternate OStatus user format with the primary one
2053 fix_alternate_contact_address($contact);
2055 if (!isset($contact["updated"]))
2056 $contact["updated"] = datetime_convert();
2058 if ($contact["server_url"] == "") {
2059 $server_url = $contact["url"];
2061 $server_url = matching_url($server_url, $contact["alias"]);
2062 if ($server_url != "")
2063 $contact["server_url"] = $server_url;
2065 $server_url = matching_url($server_url, $contact["photo"]);
2066 if ($server_url != "")
2067 $contact["server_url"] = $server_url;
2069 $server_url = matching_url($server_url, $contact["notify"]);
2070 if ($server_url != "")
2071 $contact["server_url"] = $server_url;
2073 $contact["server_url"] = normalise_link($contact["server_url"]);
2075 if (($contact["addr"] == "") AND ($contact["server_url"] != "") AND ($contact["nick"] != "")) {
2076 $hostname = str_replace("http://", "", $contact["server_url"]);
2077 $contact["addr"] = $contact["nick"]."@".$hostname;
2080 // Check if any field changed
2082 unset($fields["generation"]);
2084 if ((($contact["generation"] > 0) AND ($contact["generation"] <= $r[0]["generation"])) OR ($r[0]["generation"] == 0)) {
2085 foreach ($fields AS $field => $data)
2086 if ($contact[$field] != $r[0][$field]) {
2087 logger("Difference for contact ".$contact["url"]." in field '".$field."'. New value: '".$contact[$field]."', old value '".$r[0][$field]."'", LOGGER_DEBUG);
2091 if ($contact["generation"] < $r[0]["generation"]) {
2092 logger("Difference for contact ".$contact["url"]." in field 'generation'. new value: '".$contact["generation"]."', old value '".$r[0]["generation"]."'", LOGGER_DEBUG);
2098 logger("Update gcontact for ".$contact["url"], LOGGER_DEBUG);
2100 q("UPDATE `gcontact` SET `photo` = '%s', `name` = '%s', `nick` = '%s', `addr` = '%s', `network` = '%s',
2101 `birthday` = '%s', `gender` = '%s', `keywords` = '%s', `hide` = %d, `nsfw` = %d,
2102 `contact-type` = %d, `alias` = '%s', `notify` = '%s', `url` = '%s',
2103 `location` = '%s', `about` = '%s', `generation` = %d, `updated` = '%s',
2104 `server_url` = '%s', `connect` = '%s'
2105 WHERE `nurl` = '%s' AND (`generation` = 0 OR `generation` >= %d)",
2106 dbesc($contact["photo"]), dbesc($contact["name"]), dbesc($contact["nick"]),
2107 dbesc($contact["addr"]), dbesc($contact["network"]), dbesc($contact["birthday"]),
2108 dbesc($contact["gender"]), dbesc($contact["keywords"]), intval($contact["hide"]),
2109 intval($contact["nsfw"]), intval($contact["contact-type"]), dbesc($contact["alias"]),
2110 dbesc($contact["notify"]), dbesc($contact["url"]), dbesc($contact["location"]),
2111 dbesc($contact["about"]), intval($contact["generation"]), dbesc($contact["updated"]),
2112 dbesc($contact["server_url"]), dbesc($contact["connect"]),
2113 dbesc(normalise_link($contact["url"])), intval($contact["generation"]));
2116 // Now update the contact entry with the user id "0" as well.
2117 // This is used for the shadow copies of public items.
2118 $r = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = 0 ORDER BY `id` LIMIT 1",
2119 dbesc(normalise_link($contact["url"])));
2122 logger("Update shadow contact ".$r[0]["id"], LOGGER_DEBUG);
2124 update_contact_avatar($contact["photo"], 0, $r[0]["id"]);
2126 q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `addr` = '%s',
2127 `network` = '%s', `bd` = '%s', `gender` = '%s',
2128 `keywords` = '%s', `alias` = '%s', `contact-type` = %d,
2129 `url` = '%s', `location` = '%s', `about` = '%s'
2131 dbesc($contact["name"]), dbesc($contact["nick"]), dbesc($contact["addr"]),
2132 dbesc($contact["network"]), dbesc($contact["birthday"]), dbesc($contact["gender"]),
2133 dbesc($contact["keywords"]), dbesc($contact["alias"]), intval($contact["contact-type"]),
2134 dbesc($contact["url"]), dbesc($contact["location"]), dbesc($contact["about"]),
2135 intval($r[0]["id"]));
2139 return $gcontact_id;
2143 * @brief Updates the gcontact entry from probe
2145 * @param str $url profile link
2147 function update_gcontact_from_probe($url) {
2148 $data = probe_url($url);
2150 if (in_array($data["network"], array(NETWORK_PHANTOM))) {
2151 logger("Invalid network for contact url ".$data["url"]." - Called by: ".App::callstack(), LOGGER_DEBUG);
2155 update_gcontact($data);
2159 * @brief Update the gcontact entry for a given user id
2161 * @param int $uid User ID
2163 function update_gcontact_for_user($uid) {
2164 $r = q("SELECT `profile`.`locality`, `profile`.`region`, `profile`.`country-name`,
2165 `profile`.`name`, `profile`.`about`, `profile`.`gender`,
2166 `profile`.`pub_keywords`, `profile`.`dob`, `profile`.`photo`,
2167 `profile`.`net-publish`, `user`.`nickname`, `user`.`hidewall`,
2168 `contact`.`notify`, `contact`.`url`, `contact`.`addr`
2170 INNER JOIN `user` ON `user`.`uid` = `profile`.`uid`
2171 INNER JOIN `contact` ON `contact`.`uid` = `profile`.`uid`
2172 WHERE `profile`.`uid` = %d AND `profile`.`is-default` AND `contact`.`self`",
2175 $location = formatted_location(array("locality" => $r[0]["locality"], "region" => $r[0]["region"],
2176 "country-name" => $r[0]["country-name"]));
2178 // The "addr" field was added in 3.4.3 so it can be empty for older users
2179 if ($r[0]["addr"] != "")
2180 $addr = $r[0]["nickname"].'@'.str_replace(array("http://", "https://"), "", App::get_baseurl());
2182 $addr = $r[0]["addr"];
2184 $gcontact = array("name" => $r[0]["name"], "location" => $location, "about" => $r[0]["about"],
2185 "gender" => $r[0]["gender"], "keywords" => $r[0]["pub_keywords"],
2186 "birthday" => $r[0]["dob"], "photo" => $r[0]["photo"],
2187 "notify" => $r[0]["notify"], "url" => $r[0]["url"],
2188 "hide" => ($r[0]["hidewall"] OR !$r[0]["net-publish"]),
2189 "nick" => $r[0]["nickname"], "addr" => $addr,
2190 "connect" => $addr, "server_url" => App::get_baseurl(),
2191 "generation" => 1, "network" => NETWORK_DFRN);
2193 update_gcontact($gcontact);
2197 * @brief Fetches users of given GNU Social server
2199 * If the "Statistics" plugin is enabled (See http://gstools.org/ for details) we query user data with this.
2201 * @param str $server Server address
2203 function gs_fetch_users($server) {
2205 logger("Fetching users from GNU Social server ".$server, LOGGER_DEBUG);
2207 $url = $server."/main/statistics";
2209 $result = z_fetch_url($url);
2210 if (!$result["success"])
2213 $statistics = json_decode($result["body"]);
2215 if (is_object($statistics->config)) {
2216 if ($statistics->config->instance_with_ssl)
2217 $server = "https://";
2219 $server = "http://";
2221 $server .= $statistics->config->instance_address;
2223 $hostname = $statistics->config->instance_address;
2225 if ($statistics->instance_with_ssl)
2226 $server = "https://";
2228 $server = "http://";
2230 $server .= $statistics->instance_address;
2232 $hostname = $statistics->instance_address;
2235 if (is_object($statistics->users))
2236 foreach ($statistics->users AS $nick => $user) {
2237 $profile_url = $server."/".$user->nickname;
2239 $contact = array("url" => $profile_url,
2240 "name" => $user->fullname,
2241 "addr" => $user->nickname."@".$hostname,
2242 "nick" => $user->nickname,
2243 "about" => $user->bio,
2244 "network" => NETWORK_OSTATUS,
2245 "photo" => App::get_baseurl()."/images/person-175.jpg");
2246 get_gcontact_id($contact);
2251 * @brief Asking GNU Social server on a regular base for their user data
2254 function gs_discover() {
2256 $requery_days = intval(get_config("system", "poco_requery_days"));
2258 $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
2260 $r = q("SELECT `nurl`, `url` FROM `gserver` WHERE `last_contact` >= `last_failure` AND `network` = '%s' AND `last_poco_query` < '%s' ORDER BY RAND() LIMIT 5",
2261 dbesc(NETWORK_OSTATUS), dbesc($last_update));
2266 foreach ($r AS $server) {
2267 gs_fetch_users($server["url"]);
2268 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
2273 * @brief Returns a list of all known servers
2274 * @return array List of server urls
2276 function poco_serverlist() {
2277 $r = q("SELECT `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver`
2278 WHERE `network` IN ('%s', '%s', '%s') AND `last_contact` > `last_failure`
2279 ORDER BY `last_contact`
2281 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
2282 if (!dbm::is_result($r)) {