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
11 use Friendica\Core\System;
12 use Friendica\Core\Config;
13 use Friendica\Core\Worker;
14 use Friendica\Network\Probe;
16 require_once 'include/datetime.php';
17 require_once 'include/network.php';
18 require_once 'include/html2bbcode.php';
19 require_once 'include/Contact.php';
20 require_once 'include/Photo.php';
23 * @brief Fetch POCO data
25 * @param integer $cid Contact ID
26 * @param integer $uid User ID
27 * @param integer $zcid Global Contact ID
28 * @param integer $url POCO address that should be polled
30 * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
31 * and add the entries to the gcontact (Global Contact) table, or update existing entries
32 * if anything (name or photo) has changed.
33 * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
35 * Once the global contact is stored add (if necessary) the contact linkage which associates
36 * the given uid, cid to the global contact entry. There can be many uid/cid combinations
37 * pointing to the same global contact id.
40 function poco_load($cid, $uid = 0, $zcid = 0, $url = null) {
41 // Call the function "poco_load_worker" via the worker
42 Worker::add(PRIORITY_LOW, "discover_poco", "poco_load", (int)$cid, (int)$uid, (int)$zcid, $url);
46 * @brief Fetch POCO data from the worker
48 * @param integer $cid Contact ID
49 * @param integer $uid User ID
50 * @param integer $zcid Global Contact ID
51 * @param integer $url POCO address that should be polled
54 function poco_load_worker($cid, $uid, $zcid, $url) {
58 if ((! $url) || (! $uid)) {
59 $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
62 if (dbm::is_result($r)) {
76 $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') ;
78 logger('poco_load: ' . $url, LOGGER_DEBUG);
82 logger('poco_load: returns ' . $s, LOGGER_DATA);
84 logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
86 if (($a->get_curl_code() > 299) || (! $s)) {
92 logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
94 if (! isset($j->entry)) {
99 foreach ($j->entry as $entry) {
107 $updated = NULL_DATE;
115 $name = $entry->displayName;
117 if (isset($entry->urls)) {
118 foreach ($entry->urls as $url) {
119 if ($url->type == 'profile') {
120 $profile_url = $url->value;
123 if ($url->type == 'webfinger') {
124 $connect_url = str_replace('acct:' , '', $url->value);
129 if (isset($entry->photos)) {
130 foreach ($entry->photos as $photo) {
131 if ($photo->type == 'profile') {
132 $profile_photo = $photo->value;
138 if (isset($entry->updated)) {
139 $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
142 if (isset($entry->network)) {
143 $network = $entry->network;
146 if (isset($entry->currentLocation)) {
147 $location = $entry->currentLocation;
150 if (isset($entry->aboutMe)) {
151 $about = html2bbcode($entry->aboutMe);
154 if (isset($entry->gender)) {
155 $gender = $entry->gender;
158 if (isset($entry->generation) && ($entry->generation > 0)) {
159 $generation = ++$entry->generation;
162 if (isset($entry->tags)) {
163 foreach ($entry->tags as $tag) {
164 $keywords = implode(", ", $tag);
168 if (isset($entry->contactType) && ($entry->contactType >= 0)) {
169 $contact_type = $entry->contactType;
172 $gcontact = array("url" => $profile_url,
174 "network" => $network,
175 "photo" => $profile_photo,
177 "location" => $location,
179 "keywords" => $keywords,
180 "connect" => $connect_url,
181 "updated" => $updated,
182 "contact-type" => $contact_type,
183 "generation" => $generation);
186 $gcontact = sanitize_gcontact($gcontact);
187 $gcid = update_gcontact($gcontact);
189 link_gcontact($gcid, $uid, $cid, $zcid);
190 } catch (Exception $e) {
191 logger($e->getMessage(), LOGGER_DEBUG);
194 logger("poco_load: loaded $total entries",LOGGER_DEBUG);
196 q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
204 * @brief Sanitize the given gcontact data
206 * @param array $gcontact array with gcontact data
211 * 1: Profiles on this server
212 * 2: Contacts of profiles on this server
213 * 3: Contacts of contacts of profiles on this server
217 function sanitize_gcontact($gcontact) {
219 if ($gcontact['url'] == "") {
220 throw new Exception('URL is empty');
223 $urlparts = parse_url($gcontact['url']);
224 if (!isset($urlparts["scheme"])) {
225 throw new Exception("This (".$gcontact['url'].") doesn't seem to be an url.");
228 if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com",
229 "identi.ca", "alpha.app.net"))) {
230 throw new Exception('Contact from a non federated network ignored. ('.$gcontact['url'].')');
233 // Don't store the statusnet connector as network
234 // We can't simply set this to NETWORK_OSTATUS since the connector could have fetched posts from friendica as well
235 if ($gcontact['network'] == NETWORK_STATUSNET) {
236 $gcontact['network'] = "";
239 // Assure that there are no parameter fragments in the profile url
240 if (in_array($gcontact['network'], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) {
241 $gcontact['url'] = clean_contact_url($gcontact['url']);
244 $alternate = poco_alternate_ostatus_url($gcontact['url']);
246 // The global contacts should contain the original picture, not the cached one
247 if (($gcontact['generation'] != 1) && stristr(normalise_link($gcontact['photo']), normalise_link(System::baseUrl()."/photo/"))) {
248 $gcontact['photo'] = "";
251 if (!isset($gcontact['network'])) {
252 $r = q("SELECT `network` FROM `contact` WHERE `uid` = 0 AND `nurl` = '%s' AND `network` != '' AND `network` != '%s' LIMIT 1",
253 dbesc(normalise_link($gcontact['url'])), dbesc(NETWORK_STATUSNET)
255 if (dbm::is_result($r)) {
256 $gcontact['network'] = $r[0]["network"];
259 if (($gcontact['network'] == "") || ($gcontact['network'] == NETWORK_OSTATUS)) {
260 $r = q("SELECT `network`, `url` FROM `contact` WHERE `uid` = 0 AND `alias` IN ('%s', '%s') AND `network` != '' AND `network` != '%s' LIMIT 1",
261 dbesc($gcontact['url']), dbesc(normalise_link($gcontact['url'])), dbesc(NETWORK_STATUSNET)
263 if (dbm::is_result($r)) {
264 $gcontact['network'] = $r[0]["network"];
269 $gcontact['server_url'] = '';
270 $gcontact['network'] = '';
272 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
273 dbesc(normalise_link($gcontact['url']))
276 if (dbm::is_result($x)) {
277 if (!isset($gcontact['network']) && ($x[0]["network"] != NETWORK_STATUSNET)) {
278 $gcontact['network'] = $x[0]["network"];
280 if ($gcontact['updated'] <= NULL_DATE) {
281 $gcontact['updated'] = $x[0]["updated"];
283 if (!isset($gcontact['server_url']) && (normalise_link($x[0]["server_url"]) != normalise_link($x[0]["url"]))) {
284 $gcontact['server_url'] = $x[0]["server_url"];
286 if (!isset($gcontact['addr'])) {
287 $gcontact['addr'] = $x[0]["addr"];
291 if ((!isset($gcontact['network']) || !isset($gcontact['name']) || !isset($gcontact['addr']) || !isset($gcontact['photo']) || !isset($gcontact['server_url']) || $alternate)
292 && poco_reachable($gcontact['url'], $gcontact['server_url'], $gcontact['network'], false)) {
293 $data = Probe::uri($gcontact['url']);
295 if ($data["network"] == NETWORK_PHANTOM) {
296 throw new Exception('Probing for URL '.$gcontact['url'].' failed');
299 $orig_profile = $gcontact['url'];
301 $gcontact["server_url"] = $data["baseurl"];
303 $gcontact = array_merge($gcontact, $data);
305 if ($alternate && ($gcontact['network'] == NETWORK_OSTATUS)) {
306 // Delete the old entry - if it exists
307 $r = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($orig_profile)));
308 if (dbm::is_result($r)) {
309 q("DELETE FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($orig_profile)));
310 q("DELETE FROM `glink` WHERE `gcid` = %d", intval($r[0]["id"]));
315 if (!isset($gcontact['name']) || !isset($gcontact['photo'])) {
316 throw new Exception('No name and photo for URL '.$gcontact['url']);
319 if (!in_array($gcontact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA))) {
320 throw new Exception('No federated network ('.$gcontact['network'].') detected for URL '.$gcontact['url']);
323 if (!isset($gcontact['server_url'])) {
324 // We check the server url to be sure that it is a real one
325 $server_url = poco_detect_server($gcontact['url']);
327 // We are now sure that it is a correct URL. So we use it in the future
328 if ($server_url != "") {
329 $gcontact['server_url'] = $server_url;
333 // The server URL doesn't seem to be valid, so we don't store it.
334 if (!poco_check_server($gcontact['server_url'], $gcontact['network'])) {
335 $gcontact['server_url'] = "";
342 * @brief Link the gcontact entry with user, contact and global contact
344 * @param integer $gcid Global contact ID
345 * @param integer $cid Contact ID
346 * @param integer $uid User ID
347 * @param integer $zcid Global Contact ID
350 function link_gcontact($gcid, $uid = 0, $cid = 0, $zcid = 0) {
356 $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
363 if (!dbm::is_result($r)) {
364 q("INSERT INTO `glink` (`cid`, `uid`, `gcid`, `zcid`, `updated`) VALUES (%d, %d, %d, %d, '%s') ",
369 dbesc(datetime_convert())
372 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
373 dbesc(datetime_convert()),
382 function poco_reachable($profile, $server = "", $network = "", $force = false) {
385 $server = poco_detect_server($profile);
392 return poco_check_server($server, $network, $force);
395 function poco_detect_server($profile) {
397 // Try to detect the server path based upon some known standard paths
400 if ($server_url == "") {
401 $friendica = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $profile);
402 if ($friendica != $profile) {
403 $server_url = $friendica;
404 $network = NETWORK_DFRN;
408 if ($server_url == "") {
409 $diaspora = preg_replace("=(https?://)(.*)/u/(.*)=ism", "$1$2", $profile);
410 if ($diaspora != $profile) {
411 $server_url = $diaspora;
412 $network = NETWORK_DIASPORA;
416 if ($server_url == "") {
417 $red = preg_replace("=(https?://)(.*)/channel/(.*)=ism", "$1$2", $profile);
418 if ($red != $profile) {
420 $network = NETWORK_DIASPORA;
425 if ($server_url == "") {
426 $mastodon = preg_replace("=(https?://)(.*)/users/(.*)=ism", "$1$2", $profile);
427 if ($mastodon != $profile) {
428 $server_url = $mastodon;
429 $network = NETWORK_OSTATUS;
433 // Numeric OStatus variant
434 if ($server_url == "") {
435 $ostatus = preg_replace("=(https?://)(.*)/user/(.*)=ism", "$1$2", $profile);
436 if ($ostatus != $profile) {
437 $server_url = $ostatus;
438 $network = NETWORK_OSTATUS;
443 if ($server_url == "") {
444 $base = preg_replace("=(https?://)(.*?)/(.*)=ism", "$1$2", $profile);
445 if ($base != $profile) {
447 $network = NETWORK_PHANTOM;
451 if ($server_url == "") {
455 $r = q("SELECT `id` FROM `gserver` WHERE `nurl` = '%s' AND `last_contact` > `last_failure`",
456 dbesc(normalise_link($server_url)));
457 if (dbm::is_result($r)) {
461 // Fetch the host-meta to check if this really is a server
462 $serverret = z_fetch_url($server_url."/.well-known/host-meta");
463 if (!$serverret["success"]) {
470 function poco_alternate_ostatus_url($url) {
471 return(preg_match("=https?://.+/user/\d+=ism", $url, $matches));
474 function poco_last_updated($profile, $force = false) {
476 $gcontacts = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'",
477 dbesc(normalise_link($profile)));
479 if (!dbm::is_result($gcontacts)) {
483 $contact = array("url" => $profile);
485 if ($gcontacts[0]["created"] <= NULL_DATE) {
486 $contact['created'] = datetime_convert();
490 $server_url = normalise_link(poco_detect_server($profile));
493 if (($server_url == '') && ($gcontacts[0]["server_url"] != "")) {
494 $server_url = $gcontacts[0]["server_url"];
497 if (!$force && (($server_url == '') || ($gcontacts[0]["server_url"] == $gcontacts[0]["nurl"]))) {
498 $server_url = normalise_link(poco_detect_server($profile));
501 if (!in_array($gcontacts[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_FEED, NETWORK_OSTATUS, ""))) {
502 logger("Profile ".$profile.": Network type ".$gcontacts[0]["network"]." can't be checked", LOGGER_DEBUG);
506 if ($server_url != "") {
507 if (!poco_check_server($server_url, $gcontacts[0]["network"], $force)) {
509 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
510 dbesc(datetime_convert()), dbesc(normalise_link($profile)));
513 logger("Profile ".$profile.": Server ".$server_url." wasn't reachable.", LOGGER_DEBUG);
516 $contact['server_url'] = $server_url;
519 if (in_array($gcontacts[0]["network"], array("", NETWORK_FEED))) {
520 $server = q("SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''",
521 dbesc(normalise_link($server_url)));
524 $contact['network'] = $server[0]["network"];
530 // noscrape is really fast so we don't cache the call.
531 if (($server_url != "") && ($gcontacts[0]["nick"] != "")) {
533 // Use noscrape if possible
534 $server = q("SELECT `noscrape`, `network` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", dbesc(normalise_link($server_url)));
537 $noscraperet = z_fetch_url($server[0]["noscrape"]."/".$gcontacts[0]["nick"]);
539 if ($noscraperet["success"] && ($noscraperet["body"] != "")) {
541 $noscrape = json_decode($noscraperet["body"], true);
543 if (is_array($noscrape)) {
544 $contact["network"] = $server[0]["network"];
546 if (isset($noscrape["fn"])) {
547 $contact["name"] = $noscrape["fn"];
549 if (isset($noscrape["comm"])) {
550 $contact["community"] = $noscrape["comm"];
552 if (isset($noscrape["tags"])) {
553 $keywords = implode(" ", $noscrape["tags"]);
554 if ($keywords != "") {
555 $contact["keywords"] = $keywords;
559 $location = formatted_location($noscrape);
561 $contact["location"] = $location;
563 if (isset($noscrape["dfrn-notify"])) {
564 $contact["notify"] = $noscrape["dfrn-notify"];
566 // Remove all fields that are not present in the gcontact table
567 unset($noscrape["fn"]);
568 unset($noscrape["key"]);
569 unset($noscrape["homepage"]);
570 unset($noscrape["comm"]);
571 unset($noscrape["tags"]);
572 unset($noscrape["locality"]);
573 unset($noscrape["region"]);
574 unset($noscrape["country-name"]);
575 unset($noscrape["contacts"]);
576 unset($noscrape["dfrn-request"]);
577 unset($noscrape["dfrn-confirm"]);
578 unset($noscrape["dfrn-notify"]);
579 unset($noscrape["dfrn-poll"]);
581 // Set the date of the last contact
582 /// @todo By now the function "update_gcontact" doesn't work with this field
583 //$contact["last_contact"] = datetime_convert();
585 $contact = array_merge($contact, $noscrape);
587 update_gcontact($contact);
589 if (trim($noscrape["updated"]) != "") {
590 q("UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'",
591 dbesc(datetime_convert()), dbesc(normalise_link($profile)));
593 logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG);
595 return $noscrape["updated"];
602 // If we only can poll the feed, then we only do this once a while
603 if (!$force && !poco_do_update($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) {
604 logger("Profile ".$profile." was last updated at ".$gcontacts[0]["updated"]." (cached)", LOGGER_DEBUG);
606 update_gcontact($contact);
607 return $gcontacts[0]["updated"];
610 $data = Probe::uri($profile);
612 // Is the profile link the alternate OStatus link notation? (http://domain.tld/user/4711)
613 // Then check the other link and delete this one
614 if (($data["network"] == NETWORK_OSTATUS) && poco_alternate_ostatus_url($profile) &&
615 (normalise_link($profile) == normalise_link($data["alias"])) &&
616 (normalise_link($profile) != normalise_link($data["url"]))) {
618 // Delete the old entry
619 q("DELETE FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($profile)));
620 q("DELETE FROM `glink` WHERE `gcid` = %d", intval($gcontacts[0]["id"]));
622 $gcontact = array_merge($gcontacts[0], $data);
624 $gcontact["server_url"] = $data["baseurl"];
627 $gcontact = sanitize_gcontact($gcontact);
628 update_gcontact($gcontact);
630 poco_last_updated($data["url"], $force);
631 } catch (Exception $e) {
632 logger($e->getMessage(), LOGGER_DEBUG);
635 logger("Profile ".$profile." was deleted", LOGGER_DEBUG);
639 if (($data["poll"] == "") || (in_array($data["network"], array(NETWORK_FEED, NETWORK_PHANTOM)))) {
640 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
641 dbesc(datetime_convert()), dbesc(normalise_link($profile)));
643 logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG);
647 $contact = array_merge($contact, $data);
649 $contact["server_url"] = $data["baseurl"];
651 update_gcontact($contact);
653 $feedret = z_fetch_url($data["poll"]);
655 if (!$feedret["success"]) {
656 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
657 dbesc(datetime_convert()), dbesc(normalise_link($profile)));
659 logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG);
663 $doc = new DOMDocument();
664 @$doc->loadXML($feedret["body"]);
666 $xpath = new DomXPath($doc);
667 $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
669 $entries = $xpath->query('/atom:feed/atom:entry');
673 foreach ($entries as $entry) {
674 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
675 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
677 if ($last_updated < $published)
678 $last_updated = $published;
680 if ($last_updated < $updated)
681 $last_updated = $updated;
684 // Maybe there aren't any entries. Then check if it is a valid feed
685 if ($last_updated == "") {
686 if ($xpath->query('/atom:feed')->length > 0) {
687 $last_updated = NULL_DATE;
690 q("UPDATE `gcontact` SET `updated` = '%s', `last_contact` = '%s' WHERE `nurl` = '%s'",
691 dbesc(dbm::date($last_updated)), dbesc(dbm::date()), dbesc(normalise_link($profile)));
693 if (($gcontacts[0]["generation"] == 0)) {
694 q("UPDATE `gcontact` SET `generation` = 9 WHERE `nurl` = '%s'",
695 dbesc(normalise_link($profile)));
698 logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG);
700 return($last_updated);
703 function poco_do_update($created, $updated, $last_failure, $last_contact) {
704 $now = strtotime(datetime_convert());
706 if ($updated > $last_contact) {
707 $contact_time = strtotime($updated);
709 $contact_time = strtotime($last_contact);
712 $failure_time = strtotime($last_failure);
713 $created_time = strtotime($created);
715 // If there is no "created" time then use the current time
716 if ($created_time <= 0) {
717 $created_time = $now;
720 // If the last contact was less than 24 hours then don't update
721 if (($now - $contact_time) < (60 * 60 * 24)) {
725 // If the last failure was less than 24 hours then don't update
726 if (($now - $failure_time) < (60 * 60 * 24)) {
730 // If the last contact was less than a week ago and the last failure is older than a week then don't update
731 //if ((($now - $contact_time) < (60 * 60 * 24 * 7)) && ($contact_time > $failure_time))
734 // 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
735 if ((($now - $contact_time) > (60 * 60 * 24 * 7)) && (($now - $created_time) > (60 * 60 * 24 * 7)) && (($now - $failure_time) < (60 * 60 * 24 * 7))) {
739 // 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
740 if ((($now - $contact_time) > (60 * 60 * 24 * 30)) && (($now - $created_time) > (60 * 60 * 24 * 30)) && (($now - $failure_time) < (60 * 60 * 24 * 30))) {
747 function poco_to_boolean($val) {
748 if (($val == "true") || ($val == 1)) {
750 } elseif (($val == "false") || ($val == 0)) {
758 * @brief Detect server type (Hubzilla or Friendica) via the poco data
760 * @param object $data POCO data
761 * @return array Server data
763 function poco_detect_poco_data($data) {
766 if (!isset($data->entry)) {
770 if (count($data->entry) == 0) {
774 if (!isset($data->entry[0]->urls)) {
778 if (count($data->entry[0]->urls) == 0) {
782 foreach ($data->entry[0]->urls as $url) {
783 if ($url->type == 'zot') {
785 $server["platform"] = 'Hubzilla';
786 $server["network"] = NETWORK_DIASPORA;
794 * @brief Detect server type by using the nodeinfo data
796 * @param string $server_url address of the server
797 * @return array Server data
799 function poco_fetch_nodeinfo($server_url) {
800 $serverret = z_fetch_url($server_url."/.well-known/nodeinfo");
801 if (!$serverret["success"]) {
805 $nodeinfo = json_decode($serverret['body']);
807 if (!is_object($nodeinfo)) {
811 if (!is_array($nodeinfo->links)) {
817 foreach ($nodeinfo->links as $link) {
818 if ($link->rel == 'http://nodeinfo.diaspora.software/ns/schema/1.0') {
819 $nodeinfo_url = $link->href;
823 if ($nodeinfo_url == '') {
827 $serverret = z_fetch_url($nodeinfo_url);
828 if (!$serverret["success"]) {
832 $nodeinfo = json_decode($serverret['body']);
834 if (!is_object($nodeinfo)) {
840 $server['register_policy'] = REGISTER_CLOSED;
842 if (is_bool($nodeinfo->openRegistrations) && $nodeinfo->openRegistrations) {
843 $server['register_policy'] = REGISTER_OPEN;
846 if (is_object($nodeinfo->software)) {
847 if (isset($nodeinfo->software->name)) {
848 $server['platform'] = $nodeinfo->software->name;
851 if (isset($nodeinfo->software->version)) {
852 $server['version'] = $nodeinfo->software->version;
853 // Version numbers on Nodeinfo are presented with additional info, e.g.:
854 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
855 $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']);
859 if (is_object($nodeinfo->metadata)) {
860 if (isset($nodeinfo->metadata->nodeName)) {
861 $server['site_name'] = $nodeinfo->metadata->nodeName;
869 if (is_array($nodeinfo->protocols->inbound)) {
870 foreach ($nodeinfo->protocols->inbound as $inbound) {
871 if ($inbound == 'diaspora') {
874 if ($inbound == 'friendica') {
877 if ($inbound == 'gnusocial') {
884 $server['network'] = NETWORK_OSTATUS;
887 $server['network'] = NETWORK_DIASPORA;
890 $server['network'] = NETWORK_DFRN;
901 * @brief Detect server type (Hubzilla or Friendica) via the front page body
903 * @param string $body Front page of the server
904 * @return array Server data
906 function poco_detect_server_type($body) {
909 $doc = new DOMDocument();
910 @$doc->loadHTML($body);
911 $xpath = new DomXPath($doc);
913 $list = $xpath->query("//meta[@name]");
915 foreach ($list as $node) {
917 if ($node->attributes->length) {
918 foreach ($node->attributes as $attribute) {
919 $attr[$attribute->name] = $attribute->value;
922 if ($attr['name'] == 'generator') {
923 $version_part = explode(" ", $attr['content']);
924 if (count($version_part) == 2) {
925 if (in_array($version_part[0], array("Friendika", "Friendica"))) {
927 $server["platform"] = $version_part[0];
928 $server["version"] = $version_part[1];
929 $server["network"] = NETWORK_DFRN;
936 $list = $xpath->query("//meta[@property]");
938 foreach ($list as $node) {
940 if ($node->attributes->length) {
941 foreach ($node->attributes as $attribute) {
942 $attr[$attribute->name] = $attribute->value;
945 if ($attr['property'] == 'generator' && in_array($attr['content'], array("hubzilla", "BlaBlaNet"))) {
947 $server["platform"] = $attr['content'];
948 $server["version"] = "";
949 $server["network"] = NETWORK_DIASPORA;
958 $server["site_name"] = $xpath->evaluate($element."//head/title/text()", $context)->item(0)->nodeValue;
962 function poco_check_server($server_url, $network = "", $force = false) {
964 // Unify the server address
965 $server_url = trim($server_url, "/");
966 $server_url = str_replace("/index.php", "", $server_url);
968 if ($server_url == "") {
972 $servers = q("SELECT * FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
973 if (dbm::is_result($servers)) {
975 if ($servers[0]["created"] <= NULL_DATE) {
976 q("UPDATE `gserver` SET `created` = '%s' WHERE `nurl` = '%s'",
977 dbesc(datetime_convert()), dbesc(normalise_link($server_url)));
979 $poco = $servers[0]["poco"];
980 $noscrape = $servers[0]["noscrape"];
982 if ($network == "") {
983 $network = $servers[0]["network"];
986 $last_contact = $servers[0]["last_contact"];
987 $last_failure = $servers[0]["last_failure"];
988 $version = $servers[0]["version"];
989 $platform = $servers[0]["platform"];
990 $site_name = $servers[0]["site_name"];
991 $info = $servers[0]["info"];
992 $register_policy = $servers[0]["register_policy"];
994 if (!$force && !poco_do_update($servers[0]["created"], "", $last_failure, $last_contact)) {
995 logger("Use cached data for server ".$server_url, LOGGER_DEBUG);
996 return ($last_contact >= $last_failure);
1005 $register_policy = -1;
1007 $last_contact = NULL_DATE;
1008 $last_failure = NULL_DATE;
1010 logger("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$servers[0]["created"]." Failure: ".$last_failure." Contact: ".$last_contact, LOGGER_DEBUG);
1013 $possible_failure = false;
1014 $orig_last_failure = $last_failure;
1015 $orig_last_contact = $last_contact;
1017 // Check if the page is accessible via SSL.
1018 $orig_server_url = $server_url;
1019 $server_url = str_replace("http://", "https://", $server_url);
1021 // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
1022 $serverret = z_fetch_url($server_url."/.well-known/host-meta", false, $redirects, array('timeout' => 20));
1024 // Quit if there is a timeout.
1025 // But we want to make sure to only quit if we are mostly sure that this server url fits.
1026 if (dbm::is_result($servers) && ($orig_server_url == $server_url) &&
1027 ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT)) {
1028 logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
1029 dba::update('gserver', array('last_failure' => datetime_convert()), array('nurl' => normalise_link($server_url)));
1033 // Maybe the page is unencrypted only?
1034 $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
1035 if (!$serverret["success"] || ($serverret["body"] == "") || (@sizeof($xmlobj) == 0) || !is_object($xmlobj)) {
1036 $server_url = str_replace("https://", "http://", $server_url);
1038 // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
1039 $serverret = z_fetch_url($server_url."/.well-known/host-meta", false, $redirects, array('timeout' => 20));
1041 // Quit if there is a timeout
1042 if ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT) {
1043 logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
1044 dba::update('gserver', array('last_failure' => datetime_convert()), array('nurl' => normalise_link($server_url)));
1048 $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
1051 if (!$serverret["success"] || ($serverret["body"] == "") || (sizeof($xmlobj) == 0) || !is_object($xmlobj)) {
1052 // Workaround for bad configured servers (known nginx problem)
1053 if (!in_array($serverret["debug"]["http_code"], array("403", "404"))) {
1056 $possible_failure = true;
1059 // If the server has no possible failure we reset the cached data
1060 if (!$possible_failure) {
1065 $register_policy = -1;
1070 $serverret = z_fetch_url($server_url."/poco");
1071 if ($serverret["success"]) {
1072 $data = json_decode($serverret["body"]);
1073 if (isset($data->totalResults)) {
1074 $poco = $server_url."/poco";
1075 $server = poco_detect_poco_data($data);
1077 $platform = $server['platform'];
1078 $network = $server['network'];
1087 // Test for Diaspora, Hubzilla, Mastodon or older Friendica servers
1088 $serverret = z_fetch_url($server_url);
1090 if (!$serverret["success"] || ($serverret["body"] == "")) {
1093 $server = poco_detect_server_type($serverret["body"]);
1095 $platform = $server['platform'];
1096 $network = $server['network'];
1097 $version = $server['version'];
1098 $site_name = $server['site_name'];
1101 $lines = explode("\n",$serverret["header"]);
1102 if (count($lines)) {
1103 foreach($lines as $line) {
1104 $line = trim($line);
1105 if (stristr($line,'X-Diaspora-Version:')) {
1106 $platform = "Diaspora";
1107 $version = trim(str_replace("X-Diaspora-Version:", "", $line));
1108 $version = trim(str_replace("x-diaspora-version:", "", $version));
1109 $network = NETWORK_DIASPORA;
1110 $versionparts = explode("-", $version);
1111 $version = $versionparts[0];
1114 if (stristr($line,'Server: Mastodon')) {
1115 $platform = "Mastodon";
1116 $network = NETWORK_OSTATUS;
1123 if (!$failure && ($poco == "")) {
1124 // Test for Statusnet
1125 // Will also return data for Friendica and GNU Social - but it will be overwritten later
1126 // The "not implemented" is a special treatment for really, really old Friendica versions
1127 $serverret = z_fetch_url($server_url."/api/statusnet/version.json");
1128 if ($serverret["success"] && ($serverret["body"] != '{"error":"not implemented"}') &&
1129 ($serverret["body"] != '') && (strlen($serverret["body"]) < 30)) {
1130 $platform = "StatusNet";
1131 // Remove junk that some GNU Social servers return
1132 $version = str_replace(chr(239).chr(187).chr(191), "", $serverret["body"]);
1133 $version = trim($version, '"');
1134 $network = NETWORK_OSTATUS;
1137 // Test for GNU Social
1138 $serverret = z_fetch_url($server_url."/api/gnusocial/version.json");
1139 if ($serverret["success"] && ($serverret["body"] != '{"error":"not implemented"}') &&
1140 ($serverret["body"] != '') && (strlen($serverret["body"]) < 30)) {
1141 $platform = "GNU Social";
1142 // Remove junk that some GNU Social servers return
1143 $version = str_replace(chr(239).chr(187).chr(191), "", $serverret["body"]);
1144 $version = trim($version, '"');
1145 $network = NETWORK_OSTATUS;
1148 // Test for Mastodon
1149 $orig_version = $version;
1150 $serverret = z_fetch_url($server_url."/api/v1/instance");
1151 if ($serverret["success"] && ($serverret["body"] != '')) {
1152 $data = json_decode($serverret["body"]);
1153 if (isset($data->version)) {
1154 $platform = "Mastodon";
1155 $version = $data->version;
1156 $site_name = $data->title;
1157 $info = $data->description;
1158 $network = NETWORK_OSTATUS;
1161 if (strstr($orig_version.$version, 'Pleroma')) {
1162 $platform = 'Pleroma';
1163 $version = trim(str_replace('Pleroma', '', $version));
1168 // Test for Hubzilla and Red
1169 $serverret = z_fetch_url($server_url."/siteinfo.json");
1170 if ($serverret["success"]) {
1171 $data = json_decode($serverret["body"]);
1172 if (isset($data->url)) {
1173 $platform = $data->platform;
1174 $version = $data->version;
1175 $network = NETWORK_DIASPORA;
1177 if (!empty($data->site_name)) {
1178 $site_name = $data->site_name;
1180 switch ($data->register_policy) {
1181 case "REGISTER_OPEN":
1182 $register_policy = REGISTER_OPEN;
1184 case "REGISTER_APPROVE":
1185 $register_policy = REGISTER_APPROVE;
1187 case "REGISTER_CLOSED":
1189 $register_policy = REGISTER_CLOSED;
1193 // Test for Hubzilla, Redmatrix or Friendica
1194 $serverret = z_fetch_url($server_url."/api/statusnet/config.json");
1195 if ($serverret["success"]) {
1196 $data = json_decode($serverret["body"]);
1197 if (isset($data->site->server)) {
1198 if (isset($data->site->platform)) {
1199 $platform = $data->site->platform->PLATFORM_NAME;
1200 $version = $data->site->platform->STD_VERSION;
1201 $network = NETWORK_DIASPORA;
1203 if (isset($data->site->BlaBlaNet)) {
1204 $platform = $data->site->BlaBlaNet->PLATFORM_NAME;
1205 $version = $data->site->BlaBlaNet->STD_VERSION;
1206 $network = NETWORK_DIASPORA;
1208 if (isset($data->site->hubzilla)) {
1209 $platform = $data->site->hubzilla->PLATFORM_NAME;
1210 $version = $data->site->hubzilla->RED_VERSION;
1211 $network = NETWORK_DIASPORA;
1213 if (isset($data->site->redmatrix)) {
1214 if (isset($data->site->redmatrix->PLATFORM_NAME)) {
1215 $platform = $data->site->redmatrix->PLATFORM_NAME;
1216 } elseif (isset($data->site->redmatrix->RED_PLATFORM)) {
1217 $platform = $data->site->redmatrix->RED_PLATFORM;
1220 $version = $data->site->redmatrix->RED_VERSION;
1221 $network = NETWORK_DIASPORA;
1223 if (isset($data->site->friendica)) {
1224 $platform = $data->site->friendica->FRIENDICA_PLATFORM;
1225 $version = $data->site->friendica->FRIENDICA_VERSION;
1226 $network = NETWORK_DFRN;
1229 $site_name = $data->site->name;
1231 $data->site->closed = poco_to_boolean($data->site->closed);
1232 $data->site->private = poco_to_boolean($data->site->private);
1233 $data->site->inviteonly = poco_to_boolean($data->site->inviteonly);
1235 if (!$data->site->closed && !$data->site->private and $data->site->inviteonly) {
1236 $register_policy = REGISTER_APPROVE;
1237 } elseif (!$data->site->closed && !$data->site->private) {
1238 $register_policy = REGISTER_OPEN;
1240 $register_policy = REGISTER_CLOSED;
1247 // Query statistics.json. Optional package for Diaspora, Friendica and Redmatrix
1249 $serverret = z_fetch_url($server_url."/statistics.json");
1250 if ($serverret["success"]) {
1251 $data = json_decode($serverret["body"]);
1252 if (isset($data->version)) {
1253 $version = $data->version;
1254 // Version numbers on statistics.json are presented with additional info, e.g.:
1255 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
1256 $version = preg_replace("=(.+)-(.{4,})=ism", "$1", $version);
1259 if (!empty($data->name)) {
1260 $site_name = $data->name;
1263 if (!empty($data->network)) {
1264 $platform = $data->network;
1267 if ($platform == "Diaspora") {
1268 $network = NETWORK_DIASPORA;
1271 if ($data->registrations_open) {
1272 $register_policy = REGISTER_OPEN;
1274 $register_policy = REGISTER_CLOSED;
1279 // Query nodeinfo. Working for (at least) Diaspora and Friendica.
1281 $server = poco_fetch_nodeinfo($server_url);
1283 $register_policy = $server['register_policy'];
1285 if (isset($server['platform'])) {
1286 $platform = $server['platform'];
1289 if (isset($server['network'])) {
1290 $network = $server['network'];
1293 if (isset($server['version'])) {
1294 $version = $server['version'];
1297 if (isset($server['site_name'])) {
1298 $site_name = $server['site_name'];
1303 // Check for noscrape
1304 // Friendica servers could be detected as OStatus servers
1305 if (!$failure && in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS))) {
1306 $serverret = z_fetch_url($server_url."/friendica/json");
1308 if (!$serverret["success"]) {
1309 $serverret = z_fetch_url($server_url."/friendika/json");
1312 if ($serverret["success"]) {
1313 $data = json_decode($serverret["body"]);
1315 if (isset($data->version)) {
1316 $network = NETWORK_DFRN;
1318 $noscrape = $data->no_scrape_url;
1319 $version = $data->version;
1320 $site_name = $data->site_name;
1321 $info = $data->info;
1322 $register_policy_str = $data->register_policy;
1323 $platform = $data->platform;
1325 switch ($register_policy_str) {
1326 case "REGISTER_CLOSED":
1327 $register_policy = REGISTER_CLOSED;
1329 case "REGISTER_APPROVE":
1330 $register_policy = REGISTER_APPROVE;
1332 case "REGISTER_OPEN":
1333 $register_policy = REGISTER_OPEN;
1340 if ($possible_failure && !$failure) {
1345 $last_contact = $orig_last_contact;
1346 $last_failure = datetime_convert();
1348 $last_contact = datetime_convert();
1349 $last_failure = $orig_last_failure;
1352 if (($last_contact <= $last_failure) && !$failure) {
1353 logger("Server ".$server_url." seems to be alive, but last contact wasn't set - could be a bug", LOGGER_DEBUG);
1354 } elseif (($last_contact >= $last_failure) && $failure) {
1355 logger("Server ".$server_url." seems to be dead, but last failure wasn't set - could be a bug", LOGGER_DEBUG);
1358 // Check again if the server exists
1359 $servers = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
1361 $version = strip_tags($version);
1362 $site_name = strip_tags($site_name);
1363 $info = strip_tags($info);
1364 $platform = strip_tags($platform);
1367 q("UPDATE `gserver` SET `url` = '%s', `version` = '%s', `site_name` = '%s', `info` = '%s', `register_policy` = %d, `poco` = '%s', `noscrape` = '%s',
1368 `network` = '%s', `platform` = '%s', `last_contact` = '%s', `last_failure` = '%s' WHERE `nurl` = '%s'",
1373 intval($register_policy),
1378 dbesc($last_contact),
1379 dbesc($last_failure),
1380 dbesc(normalise_link($server_url))
1382 } elseif (!$failure) {
1383 q("INSERT INTO `gserver` (`url`, `nurl`, `version`, `site_name`, `info`, `register_policy`, `poco`, `noscrape`, `network`, `platform`, `created`, `last_contact`, `last_failure`)
1384 VALUES ('%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
1386 dbesc(normalise_link($server_url)),
1390 intval($register_policy),
1395 dbesc(datetime_convert()),
1396 dbesc($last_contact),
1397 dbesc($last_failure),
1398 dbesc(datetime_convert())
1401 logger("End discovery for server " . $server_url, LOGGER_DEBUG);
1406 function count_common_friends($uid, $cid) {
1408 $r = q("SELECT count(*) as `total`
1409 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1410 WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND
1411 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
1412 AND `gcontact`.`nurl` IN (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
1419 // logger("count_common_friends: $uid $cid {$r[0]['total']}");
1420 if (dbm::is_result($r)) {
1421 return $r[0]['total'];
1428 function common_friends($uid, $cid, $start = 0, $limit = 9999, $shuffle = false) {
1431 $sql_extra = " order by rand() ";
1433 $sql_extra = " order by `gcontact`.`name` asc ";
1436 $r = q("SELECT `gcontact`.*, `contact`.`id` AS `cid`
1438 INNER JOIN `gcontact` ON `glink`.`gcid` = `gcontact`.`id`
1439 INNER JOIN `contact` ON `gcontact`.`nurl` = `contact`.`nurl`
1440 WHERE `glink`.`cid` = %d and `glink`.`uid` = %d
1441 AND `contact`.`uid` = %d AND `contact`.`self` = 0 AND `contact`.`blocked` = 0
1442 AND `contact`.`hidden` = 0 AND `contact`.`id` != %d
1443 AND ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
1444 $sql_extra LIMIT %d, %d",
1453 /// @TODO Check all calling-findings of this function if they properly use dbm::is_result()
1459 function count_common_friends_zcid($uid, $zcid) {
1461 $r = q("SELECT count(*) as `total`
1462 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1463 where `glink`.`zcid` = %d
1464 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
1469 if (dbm::is_result($r)) {
1470 return $r[0]['total'];
1476 function common_friends_zcid($uid, $zcid, $start = 0, $limit = 9999, $shuffle = false) {
1479 $sql_extra = " order by rand() ";
1481 $sql_extra = " order by `gcontact`.`name` asc ";
1484 $r = q("SELECT `gcontact`.*
1485 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1486 where `glink`.`zcid` = %d
1487 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
1488 $sql_extra limit %d, %d",
1495 /// @TODO Check all calling-findings of this function if they properly use dbm::is_result()
1501 function count_all_friends($uid, $cid) {
1503 $r = q("SELECT count(*) as `total`
1504 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1505 where `glink`.`cid` = %d and `glink`.`uid` = %d AND
1506 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))",
1511 if (dbm::is_result($r)) {
1512 return $r[0]['total'];
1519 function all_friends($uid, $cid, $start = 0, $limit = 80) {
1521 $r = q("SELECT `gcontact`.*, `contact`.`id` AS `cid`
1523 INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1524 LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl` AND `contact`.`uid` = %d
1525 WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND
1526 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
1527 ORDER BY `gcontact`.`name` ASC LIMIT %d, %d ",
1535 /// @TODO Check all calling-findings of this function if they properly use dbm::is_result()
1541 function suggestion_query($uid, $start = 0, $limit = 80) {
1548 * Uncommented because the result of the queries are to big to store it in the cache.
1549 * We need to decide if we want to change the db column type or if we want to delete it.
1551 //$list = Cache::get("suggestion_query:".$uid.":".$start.":".$limit);
1552 //if (!is_null($list)) {
1556 $network = array(NETWORK_DFRN);
1558 if (Config::get('system','diaspora_enabled')) {
1559 $network[] = NETWORK_DIASPORA;
1562 if (!Config::get('system','ostatus_disabled')) {
1563 $network[] = NETWORK_OSTATUS;
1566 $sql_network = implode("', '", $network);
1567 $sql_network = "'".$sql_network."'";
1569 /// @todo This query is really slow
1570 // By now we cache the data for five minutes
1571 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
1572 INNER JOIN `glink` ON `glink`.`gcid` = `gcontact`.`id`
1573 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
1574 AND NOT `gcontact`.`name` IN (SELECT `name` FROM `contact` WHERE `uid` = %d)
1575 AND NOT `gcontact`.`id` IN (SELECT `gcid` FROM `gcign` WHERE `uid` = %d)
1576 AND `gcontact`.`updated` >= '%s'
1577 AND `gcontact`.`last_contact` >= `gcontact`.`last_failure`
1578 AND `gcontact`.`network` IN (%s)
1579 GROUP BY `glink`.`gcid` ORDER BY `gcontact`.`updated` DESC,`total` DESC LIMIT %d, %d",
1590 if (dbm::is_result($r) && count($r) >= ($limit -1)) {
1592 * Uncommented because the result of the queries are to big to store it in the cache.
1593 * We need to decide if we want to change the db column type or if we want to delete it.
1595 //Cache::set("suggestion_query:".$uid.":".$start.":".$limit, $r, CACHE_FIVE_MINUTES);
1600 $r2 = q("SELECT gcontact.* FROM gcontact
1601 INNER JOIN `glink` ON `glink`.`gcid` = `gcontact`.`id`
1602 WHERE `glink`.`uid` = 0 AND `glink`.`cid` = 0 AND `glink`.`zcid` = 0 AND NOT `gcontact`.`nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = %d)
1603 AND NOT `gcontact`.`name` IN (SELECT `name` FROM `contact` WHERE `uid` = %d)
1604 AND NOT `gcontact`.`id` IN (SELECT `gcid` FROM `gcign` WHERE `uid` = %d)
1605 AND `gcontact`.`updated` >= '%s'
1606 AND `gcontact`.`last_contact` >= `gcontact`.`last_failure`
1607 AND `gcontact`.`network` IN (%s)
1608 ORDER BY rand() LIMIT %d, %d",
1619 foreach ($r2 as $suggestion) {
1620 $list[$suggestion["nurl"]] = $suggestion;
1623 foreach ($r as $suggestion) {
1624 $list[$suggestion["nurl"]] = $suggestion;
1627 while (sizeof($list) > ($limit)) {
1632 * Uncommented because the result of the queries are to big to store it in the cache.
1633 * We need to decide if we want to change the db column type or if we want to delete it.
1635 //Cache::set("suggestion_query:".$uid.":".$start.":".$limit, $list, CACHE_FIVE_MINUTES);
1639 function update_suggestions() {
1645 /// @TODO Check if it is really neccessary to poll the own server
1646 poco_load(0, 0, 0, System::baseUrl() . '/poco');
1648 $done[] = System::baseUrl() . '/poco';
1650 if (strlen(Config::get('system','directory'))) {
1651 $x = fetch_url(get_server()."/pubsites");
1653 $j = json_decode($x);
1655 foreach ($j->entries as $entry) {
1657 poco_check_server($entry->url);
1659 $url = $entry->url . '/poco';
1660 if (! in_array($url,$done)) {
1661 poco_load(0,0,0,$entry->url . '/poco');
1668 // Query your contacts from Friendica and Redmatrix/Hubzilla for their contacts
1669 $r = q("SELECT DISTINCT(`poco`) AS `poco` FROM `contact` WHERE `network` IN ('%s', '%s')",
1670 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA)
1673 if (dbm::is_result($r)) {
1674 foreach ($r as $rr) {
1675 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
1676 if (! in_array($base,$done)) {
1677 poco_load(0,0,0,$base);
1684 * @brief Fetch server list from remote servers and adds them when they are new.
1686 * @param string $poco URL to the POCO endpoint
1688 function poco_fetch_serverlist($poco) {
1689 $serverret = z_fetch_url($poco."/@server");
1690 if (!$serverret["success"]) {
1693 $serverlist = json_decode($serverret['body']);
1695 if (!is_array($serverlist)) {
1699 foreach ($serverlist as $server) {
1700 $server_url = str_replace("/index.php", "", $server->url);
1702 $r = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
1703 if (!dbm::is_result($r)) {
1704 logger("Call server check for server ".$server_url, LOGGER_DEBUG);
1705 Worker::add(PRIORITY_LOW, "discover_poco", "server", $server_url);
1710 function poco_discover_federation() {
1711 $last = Config::get('poco','last_federation_discovery');
1714 $next = $last + (24 * 60 * 60);
1715 if ($next > time()) {
1720 // Discover Friendica, Hubzilla and Diaspora servers
1721 $serverdata = fetch_url("http://the-federation.info/pods.json");
1724 $servers = json_decode($serverdata);
1726 foreach ($servers->pods as $server) {
1727 Worker::add(PRIORITY_LOW, "discover_poco", "server", "https://".$server->host);
1731 // Disvover Mastodon servers
1732 if (!Config::get('system','ostatus_disabled')) {
1733 $serverdata = fetch_url("https://instances.mastodon.xyz/instances.json");
1736 $servers = json_decode($serverdata);
1738 foreach ($servers as $server) {
1739 $url = (is_null($server->https_score) ? 'http' : 'https').'://'.$server->name;
1740 Worker::add(PRIORITY_LOW, "discover_poco", "server", $url);
1745 // Currently disabled, since the service isn't available anymore.
1746 // It is not removed since I hope that there will be a successor.
1747 // Discover GNU Social Servers.
1748 //if (!Config::get('system','ostatus_disabled')) {
1749 // $serverdata = "http://gstools.org/api/get_open_instances/";
1751 // $result = z_fetch_url($serverdata);
1752 // if ($result["success"]) {
1753 // $servers = json_decode($result["body"]);
1755 // foreach($servers->data as $server)
1756 // poco_check_server($server->instance_address);
1760 Config::set('poco','last_federation_discovery', time());
1763 function poco_discover_single_server($id) {
1764 $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `id` = %d", intval($id));
1765 if (!dbm::is_result($r)) {
1771 // Discover new servers out there (Works from Friendica version 3.5.2)
1772 poco_fetch_serverlist($server["poco"]);
1774 // Fetch all users from the other server
1775 $url = $server["poco"]."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1777 logger("Fetch all users from the server ".$server["url"], LOGGER_DEBUG);
1779 $retdata = z_fetch_url($url);
1780 if ($retdata["success"]) {
1781 $data = json_decode($retdata["body"]);
1783 poco_discover_server($data, 2);
1785 if (Config::get('system','poco_discovery') > 1) {
1787 $timeframe = Config::get('system','poco_discovery_since');
1788 if ($timeframe == 0) {
1792 $updatedSince = date("Y-m-d H:i:s", time() - $timeframe * 86400);
1794 // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3)
1795 $url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1799 $retdata = z_fetch_url($url);
1800 if ($retdata["success"]) {
1801 logger("Fetch all global contacts from the server ".$server["nurl"], LOGGER_DEBUG);
1802 $success = poco_discover_server(json_decode($retdata["body"]));
1805 if (!$success && (Config::get('system','poco_discovery') > 2)) {
1806 logger("Fetch contacts from users of the server ".$server["nurl"], LOGGER_DEBUG);
1807 poco_discover_server_users($data, $server);
1811 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
1815 // If the server hadn't replied correctly, then force a sanity check
1816 poco_check_server($server["url"], $server["network"], true);
1818 // If we couldn't reach the server, we will try it some time later
1819 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
1825 function poco_discover($complete = false) {
1827 // Update the server list
1828 poco_discover_federation();
1832 $requery_days = intval(Config::get("system", "poco_requery_days"));
1834 if ($requery_days == 0) {
1837 $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
1839 $r = q("SELECT `id`, `url`, `network` FROM `gserver` WHERE `last_contact` >= `last_failure` AND `poco` != '' AND `last_poco_query` < '%s' ORDER BY RAND()", dbesc($last_update));
1840 if (dbm::is_result($r)) {
1841 foreach ($r as $server) {
1843 if (!poco_check_server($server["url"], $server["network"])) {
1844 // The server is not reachable? Okay, then we will try it later
1845 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
1849 logger('Update directory from server '.$server['url'].' with ID '.$server['id'], LOGGER_DEBUG);
1850 Worker::add(PRIORITY_LOW, "discover_poco", "update_server_directory", (int)$server['id']);
1852 if (!$complete && (--$no_of_queries == 0)) {
1859 function poco_discover_server_users($data, $server) {
1861 if (!isset($data->entry)) {
1865 foreach ($data->entry as $entry) {
1867 if (isset($entry->urls)) {
1868 foreach ($entry->urls as $url) {
1869 if ($url->type == 'profile') {
1870 $profile_url = $url->value;
1871 $urlparts = parse_url($profile_url);
1872 $username = end(explode("/", $urlparts["path"]));
1876 if ($username != "") {
1877 logger("Fetch contacts for the user ".$username." from the server ".$server["nurl"], LOGGER_DEBUG);
1879 // Fetch all contacts from a given user from the other server
1880 $url = $server["poco"]."/".$username."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1882 $retdata = z_fetch_url($url);
1883 if ($retdata["success"]) {
1884 poco_discover_server(json_decode($retdata["body"]), 3);
1890 function poco_discover_server($data, $default_generation = 0) {
1892 if (!isset($data->entry) || !count($data->entry)) {
1898 foreach ($data->entry as $entry) {
1900 $profile_photo = '';
1904 $updated = NULL_DATE;
1910 $generation = $default_generation;
1912 $name = $entry->displayName;
1914 if (isset($entry->urls)) {
1915 foreach ($entry->urls as $url) {
1916 if ($url->type == 'profile') {
1917 $profile_url = $url->value;
1920 if ($url->type == 'webfinger') {
1921 $connect_url = str_replace('acct:' , '', $url->value);
1927 if (isset($entry->photos)) {
1928 foreach ($entry->photos as $photo) {
1929 if ($photo->type == 'profile') {
1930 $profile_photo = $photo->value;
1936 if (isset($entry->updated)) {
1937 $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
1940 if (isset($entry->network)) {
1941 $network = $entry->network;
1944 if (isset($entry->currentLocation)) {
1945 $location = $entry->currentLocation;
1948 if (isset($entry->aboutMe)) {
1949 $about = html2bbcode($entry->aboutMe);
1952 if (isset($entry->gender)) {
1953 $gender = $entry->gender;
1956 if(isset($entry->generation) && ($entry->generation > 0)) {
1957 $generation = ++$entry->generation;
1960 if(isset($entry->contactType) && ($entry->contactType >= 0)) {
1961 $contact_type = $entry->contactType;
1964 if (isset($entry->tags)) {
1965 foreach ($entry->tags as $tag) {
1966 $keywords = implode(", ", $tag);
1970 if ($generation > 0) {
1973 logger("Store profile ".$profile_url, LOGGER_DEBUG);
1975 $gcontact = array("url" => $profile_url,
1977 "network" => $network,
1978 "photo" => $profile_photo,
1980 "location" => $location,
1981 "gender" => $gender,
1982 "keywords" => $keywords,
1983 "connect" => $connect_url,
1984 "updated" => $updated,
1985 "contact-type" => $contact_type,
1986 "generation" => $generation);
1989 $gcontact = sanitize_gcontact($gcontact);
1990 update_gcontact($gcontact);
1991 } catch (Exception $e) {
1992 logger($e->getMessage(), LOGGER_DEBUG);
1995 logger("Done for profile ".$profile_url, LOGGER_DEBUG);
2002 * @brief Removes unwanted parts from a contact url
2004 * @param string $url Contact url
2005 * @return string Contact url with the wanted parts
2007 function clean_contact_url($url) {
2008 $parts = parse_url($url);
2010 if (!isset($parts["scheme"]) || !isset($parts["host"])) {
2014 $new_url = $parts["scheme"]."://".$parts["host"];
2016 if (isset($parts["port"])) {
2017 $new_url .= ":".$parts["port"];
2020 if (isset($parts["path"])) {
2021 $new_url .= $parts["path"];
2024 if ($new_url != $url) {
2025 logger("Cleaned contact url ".$url." to ".$new_url." - Called by: ".System::callstack(), LOGGER_DEBUG);
2032 * @brief Replace alternate OStatus user format with the primary one
2034 * @param arr $contact contact array (called by reference)
2036 function fix_alternate_contact_address(&$contact) {
2037 if (($contact["network"] == NETWORK_OSTATUS) && poco_alternate_ostatus_url($contact["url"])) {
2038 $data = Probe::uri($contact["url"]);
2039 if ($contact["network"] == NETWORK_OSTATUS) {
2040 logger("Fix primary url from ".$contact["url"]." to ".$data["url"]." - Called by: ".System::callstack(), LOGGER_DEBUG);
2041 $contact["url"] = $data["url"];
2042 $contact["addr"] = $data["addr"];
2043 $contact["alias"] = $data["alias"];
2044 $contact["server_url"] = $data["baseurl"];
2050 * @brief Fetch the gcontact id, add an entry if not existed
2052 * @param arr $contact contact array
2053 * @return bool|int Returns false if not found, integer if contact was found
2055 function get_gcontact_id($contact) {
2060 if (in_array($contact["network"], array(NETWORK_PHANTOM))) {
2061 logger("Invalid network for contact url ".$contact["url"]." - Called by: ".System::callstack(), LOGGER_DEBUG);
2065 if ($contact["network"] == NETWORK_STATUSNET) {
2066 $contact["network"] = NETWORK_OSTATUS;
2069 // All new contacts are hidden by default
2070 if (!isset($contact["hide"])) {
2071 $contact["hide"] = true;
2074 // Replace alternate OStatus user format with the primary one
2075 fix_alternate_contact_address($contact);
2077 // Remove unwanted parts from the contact url (e.g. "?zrl=...")
2078 if (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) {
2079 $contact["url"] = clean_contact_url($contact["url"]);
2082 dba::lock('gcontact');
2083 $r = q("SELECT `id`, `last_contact`, `last_failure`, `network` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
2084 dbesc(normalise_link($contact["url"])));
2086 if (dbm::is_result($r)) {
2087 $gcontact_id = $r[0]["id"];
2089 // Update every 90 days
2090 if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) {
2091 $last_failure_str = $r[0]["last_failure"];
2092 $last_failure = strtotime($r[0]["last_failure"]);
2093 $last_contact_str = $r[0]["last_contact"];
2094 $last_contact = strtotime($r[0]["last_contact"]);
2095 $doprobing = (((time() - $last_contact) > (90 * 86400)) && ((time() - $last_failure) > (90 * 86400)));
2098 q("INSERT INTO `gcontact` (`name`, `nick`, `addr` , `network`, `url`, `nurl`, `photo`, `created`, `updated`, `location`, `about`, `hide`, `generation`)
2099 VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d)",
2100 dbesc($contact["name"]),
2101 dbesc($contact["nick"]),
2102 dbesc($contact["addr"]),
2103 dbesc($contact["network"]),
2104 dbesc($contact["url"]),
2105 dbesc(normalise_link($contact["url"])),
2106 dbesc($contact["photo"]),
2107 dbesc(datetime_convert()),
2108 dbesc(datetime_convert()),
2109 dbesc($contact["location"]),
2110 dbesc($contact["about"]),
2111 intval($contact["hide"]),
2112 intval($contact["generation"])
2115 $r = q("SELECT `id`, `network` FROM `gcontact` WHERE `nurl` = '%s' ORDER BY `id` LIMIT 2",
2116 dbesc(normalise_link($contact["url"])));
2118 if (dbm::is_result($r)) {
2119 $gcontact_id = $r[0]["id"];
2121 $doprobing = in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""));
2127 logger("Last Contact: ". $last_contact_str." - Last Failure: ".$last_failure_str." - Checking: ".$contact["url"], LOGGER_DEBUG);
2128 Worker::add(PRIORITY_LOW, 'gprobe', $contact["url"]);
2131 return $gcontact_id;
2135 * @brief Updates the gcontact table from a given array
2137 * @param arr $contact contact array
2138 * @return bool|int Returns false if not found, integer if contact was found
2140 function update_gcontact($contact) {
2142 // Check for invalid "contact-type" value
2143 if (isset($contact['contact-type']) && (intval($contact['contact-type']) < 0)) {
2144 $contact['contact-type'] = 0;
2147 /// @todo update contact table as well
2149 $gcontact_id = get_gcontact_id($contact);
2151 if (!$gcontact_id) {
2155 $r = q("SELECT `name`, `nick`, `photo`, `location`, `about`, `addr`, `generation`, `birthday`, `gender`, `keywords`,
2156 `contact-type`, `hide`, `nsfw`, `network`, `alias`, `notify`, `server_url`, `connect`, `updated`, `url`
2157 FROM `gcontact` WHERE `id` = %d LIMIT 1",
2158 intval($gcontact_id));
2160 // Get all field names
2162 foreach ($r[0] as $field => $data) {
2163 $fields[$field] = $data;
2166 unset($fields["url"]);
2167 unset($fields["updated"]);
2168 unset($fields["hide"]);
2170 // Bugfix: We had an error in the storing of keywords which lead to the "0"
2171 // This value is still transmitted via poco.
2172 if ($contact["keywords"] == "0") {
2173 unset($contact["keywords"]);
2176 if ($r[0]["keywords"] == "0") {
2177 $r[0]["keywords"] = "";
2180 // assign all unassigned fields from the database entry
2181 foreach ($fields as $field => $data) {
2182 if (!isset($contact[$field]) || ($contact[$field] == "")) {
2183 $contact[$field] = $r[0][$field];
2187 if (!isset($contact["hide"])) {
2188 $contact["hide"] = $r[0]["hide"];
2191 $fields["hide"] = $r[0]["hide"];
2193 if ($contact["network"] == NETWORK_STATUSNET) {
2194 $contact["network"] = NETWORK_OSTATUS;
2197 // Replace alternate OStatus user format with the primary one
2198 fix_alternate_contact_address($contact);
2200 if (!isset($contact["updated"])) {
2201 $contact["updated"] = dbm::date();
2204 if ($contact["network"] == NETWORK_TWITTER) {
2205 $contact["server_url"] = 'http://twitter.com';
2208 if ($contact["server_url"] == "") {
2209 $data = Probe::uri($contact["url"]);
2210 if ($data["network"] != NETWORK_PHANTOM) {
2211 $contact["server_url"] = $data['baseurl'];
2214 $contact["server_url"] = normalise_link($contact["server_url"]);
2217 if (($contact["addr"] == "") && ($contact["server_url"] != "") && ($contact["nick"] != "")) {
2218 $hostname = str_replace("http://", "", $contact["server_url"]);
2219 $contact["addr"] = $contact["nick"]."@".$hostname;
2222 // Check if any field changed
2224 unset($fields["generation"]);
2226 if ((($contact["generation"] > 0) && ($contact["generation"] <= $r[0]["generation"])) || ($r[0]["generation"] == 0)) {
2227 foreach ($fields as $field => $data) {
2228 if ($contact[$field] != $r[0][$field]) {
2229 logger("Difference for contact ".$contact["url"]." in field '".$field."'. New value: '".$contact[$field]."', old value '".$r[0][$field]."'", LOGGER_DEBUG);
2234 if ($contact["generation"] < $r[0]["generation"]) {
2235 logger("Difference for contact ".$contact["url"]." in field 'generation'. new value: '".$contact["generation"]."', old value '".$r[0]["generation"]."'", LOGGER_DEBUG);
2241 logger("Update gcontact for ".$contact["url"], LOGGER_DEBUG);
2242 $condition = array('`nurl` = ? AND (`generation` = 0 OR `generation` >= ?)',
2243 normalise_link($contact["url"]), $contact["generation"]);
2244 $contact["updated"] = dbm::date($contact["updated"]);
2246 $updated = array('photo' => $contact['photo'], 'name' => $contact['name'],
2247 'nick' => $contact['nick'], 'addr' => $contact['addr'],
2248 'network' => $contact['network'], 'birthday' => $contact['birthday'],
2249 'gender' => $contact['gender'], 'keywords' => $contact['keywords'],
2250 'hide' => $contact['hide'], 'nsfw' => $contact['nsfw'],
2251 'contact-type' => $contact['contact-type'], 'alias' => $contact['alias'],
2252 'notify' => $contact['notify'], 'url' => $contact['url'],
2253 'location' => $contact['location'], 'about' => $contact['about'],
2254 'generation' => $contact['generation'], 'updated' => $contact['updated'],
2255 'server_url' => $contact['server_url'], 'connect' => $contact['connect']);
2257 dba::update('gcontact', $updated, $condition, $fields);
2259 // Now update the contact entry with the user id "0" as well.
2260 // This is used for the shadow copies of public items.
2261 $r = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = 0 ORDER BY `id` LIMIT 1",
2262 dbesc(normalise_link($contact["url"])));
2264 if (dbm::is_result($r)) {
2265 logger("Update public contact ".$r[0]["id"], LOGGER_DEBUG);
2267 update_contact_avatar($contact["photo"], 0, $r[0]["id"]);
2269 $fields = array('name', 'nick', 'addr',
2270 'network', 'bd', 'gender',
2271 'keywords', 'alias', 'contact-type',
2272 'url', 'location', 'about');
2273 $old_contact = dba::select('contact', $fields, array('id' => $r[0]["id"]), array('limit' => 1));
2275 // Update it with the current values
2276 $fields = array('name' => $contact['name'], 'nick' => $contact['nick'],
2277 'addr' => $contact['addr'], 'network' => $contact['network'],
2278 'bd' => $contact['birthday'], 'gender' => $contact['gender'],
2279 'keywords' => $contact['keywords'], 'alias' => $contact['alias'],
2280 'contact-type' => $contact['contact-type'], 'url' => $contact['url'],
2281 'location' => $contact['location'], 'about' => $contact['about']);
2283 dba::update('contact', $fields, array('id' => $r[0]["id"]), $old_contact);
2287 return $gcontact_id;
2291 * @brief Updates the gcontact entry from probe
2293 * @param str $url profile link
2295 function update_gcontact_from_probe($url) {
2296 $data = Probe::uri($url);
2298 if (in_array($data["network"], array(NETWORK_PHANTOM))) {
2299 logger("Invalid network for contact url ".$data["url"]." - Called by: ".System::callstack(), LOGGER_DEBUG);
2303 $data["server_url"] = $data["baseurl"];
2305 update_gcontact($data);
2309 * @brief Update the gcontact entry for a given user id
2311 * @param int $uid User ID
2313 function update_gcontact_for_user($uid) {
2314 $r = q("SELECT `profile`.`locality`, `profile`.`region`, `profile`.`country-name`,
2315 `profile`.`name`, `profile`.`about`, `profile`.`gender`,
2316 `profile`.`pub_keywords`, `profile`.`dob`, `profile`.`photo`,
2317 `profile`.`net-publish`, `user`.`nickname`, `user`.`hidewall`,
2318 `contact`.`notify`, `contact`.`url`, `contact`.`addr`
2320 INNER JOIN `user` ON `user`.`uid` = `profile`.`uid`
2321 INNER JOIN `contact` ON `contact`.`uid` = `profile`.`uid`
2322 WHERE `profile`.`uid` = %d AND `profile`.`is-default` AND `contact`.`self`",
2325 $location = formatted_location(array("locality" => $r[0]["locality"], "region" => $r[0]["region"],
2326 "country-name" => $r[0]["country-name"]));
2328 // The "addr" field was added in 3.4.3 so it can be empty for older users
2329 if ($r[0]["addr"] != "") {
2330 $addr = $r[0]["nickname"].'@'.str_replace(array("http://", "https://"), "", System::baseUrl());
2332 $addr = $r[0]["addr"];
2335 $gcontact = array("name" => $r[0]["name"], "location" => $location, "about" => $r[0]["about"],
2336 "gender" => $r[0]["gender"], "keywords" => $r[0]["pub_keywords"],
2337 "birthday" => $r[0]["dob"], "photo" => $r[0]["photo"],
2338 "notify" => $r[0]["notify"], "url" => $r[0]["url"],
2339 "hide" => ($r[0]["hidewall"] || !$r[0]["net-publish"]),
2340 "nick" => $r[0]["nickname"], "addr" => $addr,
2341 "connect" => $addr, "server_url" => System::baseUrl(),
2342 "generation" => 1, "network" => NETWORK_DFRN);
2344 update_gcontact($gcontact);
2348 * @brief Fetches users of given GNU Social server
2350 * If the "Statistics" plugin is enabled (See http://gstools.org/ for details) we query user data with this.
2352 * @param str $server Server address
2354 function gs_fetch_users($server) {
2356 logger("Fetching users from GNU Social server ".$server, LOGGER_DEBUG);
2358 $url = $server."/main/statistics";
2360 $result = z_fetch_url($url);
2361 if (!$result["success"]) {
2365 $statistics = json_decode($result["body"]);
2367 if (is_object($statistics->config)) {
2368 if ($statistics->config->instance_with_ssl) {
2369 $server = "https://";
2371 $server = "http://";
2374 $server .= $statistics->config->instance_address;
2376 $hostname = $statistics->config->instance_address;
2378 /// @TODO is_object() above means here no object, still $statistics is being used as object
2379 if ($statistics->instance_with_ssl) {
2380 $server = "https://";
2382 $server = "http://";
2385 $server .= $statistics->instance_address;
2387 $hostname = $statistics->instance_address;
2390 if (is_object($statistics->users)) {
2391 foreach ($statistics->users as $nick => $user) {
2392 $profile_url = $server."/".$user->nickname;
2394 $contact = array("url" => $profile_url,
2395 "name" => $user->fullname,
2396 "addr" => $user->nickname."@".$hostname,
2397 "nick" => $user->nickname,
2398 "about" => $user->bio,
2399 "network" => NETWORK_OSTATUS,
2400 "photo" => System::baseUrl()."/images/person-175.jpg");
2401 get_gcontact_id($contact);
2407 * @brief Asking GNU Social server on a regular base for their user data
2410 function gs_discover() {
2412 $requery_days = intval(Config::get("system", "poco_requery_days"));
2414 $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
2416 $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",
2417 dbesc(NETWORK_OSTATUS), dbesc($last_update));
2419 if (!dbm::is_result($r)) {
2423 foreach ($r as $server) {
2424 gs_fetch_users($server["url"]);
2425 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
2430 * @brief Returns a list of all known servers
2431 * @return array List of server urls
2433 function poco_serverlist() {
2434 $r = q("SELECT `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver`
2435 WHERE `network` IN ('%s', '%s', '%s') AND `last_contact` > `last_failure`
2436 ORDER BY `last_contact`
2438 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
2439 if (!dbm::is_result($r)) {