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\Network\Probe;
15 require_once 'include/datetime.php';
16 require_once 'include/probe.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 proc_run(PRIORITY_LOW, "include/discover_poco.php", "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, 'Pleroma')) {
1162 $platform = 'Pleroma';
1167 // Test for Hubzilla and Red
1168 $serverret = z_fetch_url($server_url."/siteinfo.json");
1169 if ($serverret["success"]) {
1170 $data = json_decode($serverret["body"]);
1171 if (isset($data->url)) {
1172 $platform = $data->platform;
1173 $version = $data->version;
1174 $network = NETWORK_DIASPORA;
1176 if (!empty($data->site_name)) {
1177 $site_name = $data->site_name;
1179 switch ($data->register_policy) {
1180 case "REGISTER_OPEN":
1181 $register_policy = REGISTER_OPEN;
1183 case "REGISTER_APPROVE":
1184 $register_policy = REGISTER_APPROVE;
1186 case "REGISTER_CLOSED":
1188 $register_policy = REGISTER_CLOSED;
1192 // Test for Hubzilla, Redmatrix or Friendica
1193 $serverret = z_fetch_url($server_url."/api/statusnet/config.json");
1194 if ($serverret["success"]) {
1195 $data = json_decode($serverret["body"]);
1196 if (isset($data->site->server)) {
1197 if (isset($data->site->platform)) {
1198 $platform = $data->site->platform->PLATFORM_NAME;
1199 $version = $data->site->platform->STD_VERSION;
1200 $network = NETWORK_DIASPORA;
1202 if (isset($data->site->BlaBlaNet)) {
1203 $platform = $data->site->BlaBlaNet->PLATFORM_NAME;
1204 $version = $data->site->BlaBlaNet->STD_VERSION;
1205 $network = NETWORK_DIASPORA;
1207 if (isset($data->site->hubzilla)) {
1208 $platform = $data->site->hubzilla->PLATFORM_NAME;
1209 $version = $data->site->hubzilla->RED_VERSION;
1210 $network = NETWORK_DIASPORA;
1212 if (isset($data->site->redmatrix)) {
1213 if (isset($data->site->redmatrix->PLATFORM_NAME)) {
1214 $platform = $data->site->redmatrix->PLATFORM_NAME;
1215 } elseif (isset($data->site->redmatrix->RED_PLATFORM)) {
1216 $platform = $data->site->redmatrix->RED_PLATFORM;
1219 $version = $data->site->redmatrix->RED_VERSION;
1220 $network = NETWORK_DIASPORA;
1222 if (isset($data->site->friendica)) {
1223 $platform = $data->site->friendica->FRIENDICA_PLATFORM;
1224 $version = $data->site->friendica->FRIENDICA_VERSION;
1225 $network = NETWORK_DFRN;
1228 $site_name = $data->site->name;
1230 $data->site->closed = poco_to_boolean($data->site->closed);
1231 $data->site->private = poco_to_boolean($data->site->private);
1232 $data->site->inviteonly = poco_to_boolean($data->site->inviteonly);
1234 if (!$data->site->closed && !$data->site->private and $data->site->inviteonly) {
1235 $register_policy = REGISTER_APPROVE;
1236 } elseif (!$data->site->closed && !$data->site->private) {
1237 $register_policy = REGISTER_OPEN;
1239 $register_policy = REGISTER_CLOSED;
1246 // Query statistics.json. Optional package for Diaspora, Friendica and Redmatrix
1248 $serverret = z_fetch_url($server_url."/statistics.json");
1249 if ($serverret["success"]) {
1250 $data = json_decode($serverret["body"]);
1251 if (isset($data->version)) {
1252 $version = $data->version;
1253 // Version numbers on statistics.json are presented with additional info, e.g.:
1254 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
1255 $version = preg_replace("=(.+)-(.{4,})=ism", "$1", $version);
1258 if (!empty($data->name)) {
1259 $site_name = $data->name;
1262 if (!empty($data->network)) {
1263 $platform = $data->network;
1266 if ($platform == "Diaspora") {
1267 $network = NETWORK_DIASPORA;
1270 if ($data->registrations_open) {
1271 $register_policy = REGISTER_OPEN;
1273 $register_policy = REGISTER_CLOSED;
1278 // Query nodeinfo. Working for (at least) Diaspora and Friendica.
1280 $server = poco_fetch_nodeinfo($server_url);
1282 $register_policy = $server['register_policy'];
1284 if (isset($server['platform'])) {
1285 $platform = $server['platform'];
1288 if (isset($server['network'])) {
1289 $network = $server['network'];
1292 if (isset($server['version'])) {
1293 $version = $server['version'];
1296 if (isset($server['site_name'])) {
1297 $site_name = $server['site_name'];
1302 // Check for noscrape
1303 // Friendica servers could be detected as OStatus servers
1304 if (!$failure && in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS))) {
1305 $serverret = z_fetch_url($server_url."/friendica/json");
1307 if (!$serverret["success"]) {
1308 $serverret = z_fetch_url($server_url."/friendika/json");
1311 if ($serverret["success"]) {
1312 $data = json_decode($serverret["body"]);
1314 if (isset($data->version)) {
1315 $network = NETWORK_DFRN;
1317 $noscrape = $data->no_scrape_url;
1318 $version = $data->version;
1319 $site_name = $data->site_name;
1320 $info = $data->info;
1321 $register_policy_str = $data->register_policy;
1322 $platform = $data->platform;
1324 switch ($register_policy_str) {
1325 case "REGISTER_CLOSED":
1326 $register_policy = REGISTER_CLOSED;
1328 case "REGISTER_APPROVE":
1329 $register_policy = REGISTER_APPROVE;
1331 case "REGISTER_OPEN":
1332 $register_policy = REGISTER_OPEN;
1339 if ($possible_failure && !$failure) {
1344 $last_contact = $orig_last_contact;
1345 $last_failure = datetime_convert();
1347 $last_contact = datetime_convert();
1348 $last_failure = $orig_last_failure;
1351 if (($last_contact <= $last_failure) && !$failure) {
1352 logger("Server ".$server_url." seems to be alive, but last contact wasn't set - could be a bug", LOGGER_DEBUG);
1353 } elseif (($last_contact >= $last_failure) && $failure) {
1354 logger("Server ".$server_url." seems to be dead, but last failure wasn't set - could be a bug", LOGGER_DEBUG);
1357 // Check again if the server exists
1358 $servers = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
1360 $version = strip_tags($version);
1361 $site_name = strip_tags($site_name);
1362 $info = strip_tags($info);
1363 $platform = strip_tags($platform);
1366 q("UPDATE `gserver` SET `url` = '%s', `version` = '%s', `site_name` = '%s', `info` = '%s', `register_policy` = %d, `poco` = '%s', `noscrape` = '%s',
1367 `network` = '%s', `platform` = '%s', `last_contact` = '%s', `last_failure` = '%s' WHERE `nurl` = '%s'",
1372 intval($register_policy),
1377 dbesc($last_contact),
1378 dbesc($last_failure),
1379 dbesc(normalise_link($server_url))
1381 } elseif (!$failure) {
1382 q("INSERT INTO `gserver` (`url`, `nurl`, `version`, `site_name`, `info`, `register_policy`, `poco`, `noscrape`, `network`, `platform`, `created`, `last_contact`, `last_failure`)
1383 VALUES ('%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
1385 dbesc(normalise_link($server_url)),
1389 intval($register_policy),
1394 dbesc(datetime_convert()),
1395 dbesc($last_contact),
1396 dbesc($last_failure),
1397 dbesc(datetime_convert())
1400 logger("End discovery for server " . $server_url, LOGGER_DEBUG);
1405 function count_common_friends($uid, $cid) {
1407 $r = q("SELECT count(*) as `total`
1408 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1409 WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND
1410 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
1411 AND `gcontact`.`nurl` IN (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
1418 // logger("count_common_friends: $uid $cid {$r[0]['total']}");
1419 if (dbm::is_result($r)) {
1420 return $r[0]['total'];
1427 function common_friends($uid, $cid, $start = 0, $limit = 9999, $shuffle = false) {
1430 $sql_extra = " order by rand() ";
1432 $sql_extra = " order by `gcontact`.`name` asc ";
1435 $r = q("SELECT `gcontact`.*, `contact`.`id` AS `cid`
1437 INNER JOIN `gcontact` ON `glink`.`gcid` = `gcontact`.`id`
1438 INNER JOIN `contact` ON `gcontact`.`nurl` = `contact`.`nurl`
1439 WHERE `glink`.`cid` = %d and `glink`.`uid` = %d
1440 AND `contact`.`uid` = %d AND `contact`.`self` = 0 AND `contact`.`blocked` = 0
1441 AND `contact`.`hidden` = 0 AND `contact`.`id` != %d
1442 AND ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
1443 $sql_extra LIMIT %d, %d",
1452 /// @TODO Check all calling-findings of this function if they properly use dbm::is_result()
1458 function count_common_friends_zcid($uid, $zcid) {
1460 $r = q("SELECT count(*) as `total`
1461 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1462 where `glink`.`zcid` = %d
1463 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
1468 if (dbm::is_result($r)) {
1469 return $r[0]['total'];
1475 function common_friends_zcid($uid, $zcid, $start = 0, $limit = 9999, $shuffle = false) {
1478 $sql_extra = " order by rand() ";
1480 $sql_extra = " order by `gcontact`.`name` asc ";
1483 $r = q("SELECT `gcontact`.*
1484 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1485 where `glink`.`zcid` = %d
1486 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
1487 $sql_extra limit %d, %d",
1494 /// @TODO Check all calling-findings of this function if they properly use dbm::is_result()
1500 function count_all_friends($uid, $cid) {
1502 $r = q("SELECT count(*) as `total`
1503 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1504 where `glink`.`cid` = %d and `glink`.`uid` = %d AND
1505 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))",
1510 if (dbm::is_result($r)) {
1511 return $r[0]['total'];
1518 function all_friends($uid, $cid, $start = 0, $limit = 80) {
1520 $r = q("SELECT `gcontact`.*, `contact`.`id` AS `cid`
1522 INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1523 LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl` AND `contact`.`uid` = %d
1524 WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND
1525 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
1526 ORDER BY `gcontact`.`name` ASC LIMIT %d, %d ",
1534 /// @TODO Check all calling-findings of this function if they properly use dbm::is_result()
1540 function suggestion_query($uid, $start = 0, $limit = 80) {
1547 * Uncommented because the result of the queries are to big to store it in the cache.
1548 * We need to decide if we want to change the db column type or if we want to delete it.
1550 //$list = Cache::get("suggestion_query:".$uid.":".$start.":".$limit);
1551 //if (!is_null($list)) {
1555 $network = array(NETWORK_DFRN);
1557 if (get_config('system','diaspora_enabled')) {
1558 $network[] = NETWORK_DIASPORA;
1561 if (!get_config('system','ostatus_disabled')) {
1562 $network[] = NETWORK_OSTATUS;
1565 $sql_network = implode("', '", $network);
1566 $sql_network = "'".$sql_network."'";
1568 /// @todo This query is really slow
1569 // By now we cache the data for five minutes
1570 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
1571 INNER JOIN `glink` ON `glink`.`gcid` = `gcontact`.`id`
1572 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
1573 AND NOT `gcontact`.`name` IN (SELECT `name` FROM `contact` WHERE `uid` = %d)
1574 AND NOT `gcontact`.`id` IN (SELECT `gcid` FROM `gcign` WHERE `uid` = %d)
1575 AND `gcontact`.`updated` >= '%s'
1576 AND `gcontact`.`last_contact` >= `gcontact`.`last_failure`
1577 AND `gcontact`.`network` IN (%s)
1578 GROUP BY `glink`.`gcid` ORDER BY `gcontact`.`updated` DESC,`total` DESC LIMIT %d, %d",
1589 if (dbm::is_result($r) && count($r) >= ($limit -1)) {
1591 * Uncommented because the result of the queries are to big to store it in the cache.
1592 * We need to decide if we want to change the db column type or if we want to delete it.
1594 //Cache::set("suggestion_query:".$uid.":".$start.":".$limit, $r, CACHE_FIVE_MINUTES);
1599 $r2 = q("SELECT gcontact.* FROM gcontact
1600 INNER JOIN `glink` ON `glink`.`gcid` = `gcontact`.`id`
1601 WHERE `glink`.`uid` = 0 AND `glink`.`cid` = 0 AND `glink`.`zcid` = 0 AND NOT `gcontact`.`nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = %d)
1602 AND NOT `gcontact`.`name` IN (SELECT `name` FROM `contact` WHERE `uid` = %d)
1603 AND NOT `gcontact`.`id` IN (SELECT `gcid` FROM `gcign` WHERE `uid` = %d)
1604 AND `gcontact`.`updated` >= '%s'
1605 AND `gcontact`.`last_contact` >= `gcontact`.`last_failure`
1606 AND `gcontact`.`network` IN (%s)
1607 ORDER BY rand() LIMIT %d, %d",
1618 foreach ($r2 as $suggestion) {
1619 $list[$suggestion["nurl"]] = $suggestion;
1622 foreach ($r as $suggestion) {
1623 $list[$suggestion["nurl"]] = $suggestion;
1626 while (sizeof($list) > ($limit)) {
1631 * Uncommented because the result of the queries are to big to store it in the cache.
1632 * We need to decide if we want to change the db column type or if we want to delete it.
1634 //Cache::set("suggestion_query:".$uid.":".$start.":".$limit, $list, CACHE_FIVE_MINUTES);
1638 function update_suggestions() {
1644 /// @TODO Check if it is really neccessary to poll the own server
1645 poco_load(0, 0, 0, System::baseUrl() . '/poco');
1647 $done[] = System::baseUrl() . '/poco';
1649 if (strlen(get_config('system','directory'))) {
1650 $x = fetch_url(get_server()."/pubsites");
1652 $j = json_decode($x);
1654 foreach ($j->entries as $entry) {
1656 poco_check_server($entry->url);
1658 $url = $entry->url . '/poco';
1659 if (! in_array($url,$done)) {
1660 poco_load(0,0,0,$entry->url . '/poco');
1667 // Query your contacts from Friendica and Redmatrix/Hubzilla for their contacts
1668 $r = q("SELECT DISTINCT(`poco`) AS `poco` FROM `contact` WHERE `network` IN ('%s', '%s')",
1669 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA)
1672 if (dbm::is_result($r)) {
1673 foreach ($r as $rr) {
1674 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
1675 if (! in_array($base,$done)) {
1676 poco_load(0,0,0,$base);
1683 * @brief Fetch server list from remote servers and adds them when they are new.
1685 * @param string $poco URL to the POCO endpoint
1687 function poco_fetch_serverlist($poco) {
1688 $serverret = z_fetch_url($poco."/@server");
1689 if (!$serverret["success"]) {
1692 $serverlist = json_decode($serverret['body']);
1694 if (!is_array($serverlist)) {
1698 foreach ($serverlist as $server) {
1699 $server_url = str_replace("/index.php", "", $server->url);
1701 $r = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
1702 if (!dbm::is_result($r)) {
1703 logger("Call server check for server ".$server_url, LOGGER_DEBUG);
1704 proc_run(PRIORITY_LOW, "include/discover_poco.php", "server", $server_url);
1709 function poco_discover_federation() {
1710 $last = get_config('poco','last_federation_discovery');
1713 $next = $last + (24 * 60 * 60);
1714 if ($next > time()) {
1719 // Discover Friendica, Hubzilla and Diaspora servers
1720 $serverdata = fetch_url("http://the-federation.info/pods.json");
1723 $servers = json_decode($serverdata);
1725 foreach ($servers->pods as $server) {
1726 proc_run(PRIORITY_LOW, "include/discover_poco.php", "server", "https://".$server->host);
1730 // Disvover Mastodon servers
1731 if (!Config::get('system','ostatus_disabled')) {
1732 $serverdata = fetch_url("https://instances.mastodon.xyz/instances.json");
1735 $servers = json_decode($serverdata);
1737 foreach ($servers as $server) {
1738 $url = (is_null($server->https_score) ? 'http' : 'https').'://'.$server->name;
1739 proc_run(PRIORITY_LOW, "include/discover_poco.php", "server", $url);
1744 // Currently disabled, since the service isn't available anymore.
1745 // It is not removed since I hope that there will be a successor.
1746 // Discover GNU Social Servers.
1747 //if (!get_config('system','ostatus_disabled')) {
1748 // $serverdata = "http://gstools.org/api/get_open_instances/";
1750 // $result = z_fetch_url($serverdata);
1751 // if ($result["success"]) {
1752 // $servers = json_decode($result["body"]);
1754 // foreach($servers->data as $server)
1755 // poco_check_server($server->instance_address);
1759 set_config('poco','last_federation_discovery', time());
1762 function poco_discover_single_server($id) {
1763 $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `id` = %d", intval($id));
1764 if (!dbm::is_result($r)) {
1770 // Discover new servers out there (Works from Friendica version 3.5.2)
1771 poco_fetch_serverlist($server["poco"]);
1773 // Fetch all users from the other server
1774 $url = $server["poco"]."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1776 logger("Fetch all users from the server ".$server["url"], LOGGER_DEBUG);
1778 $retdata = z_fetch_url($url);
1779 if ($retdata["success"]) {
1780 $data = json_decode($retdata["body"]);
1782 poco_discover_server($data, 2);
1784 if (get_config('system','poco_discovery') > 1) {
1786 $timeframe = get_config('system','poco_discovery_since');
1787 if ($timeframe == 0) {
1791 $updatedSince = date("Y-m-d H:i:s", time() - $timeframe * 86400);
1793 // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3)
1794 $url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1798 $retdata = z_fetch_url($url);
1799 if ($retdata["success"]) {
1800 logger("Fetch all global contacts from the server ".$server["nurl"], LOGGER_DEBUG);
1801 $success = poco_discover_server(json_decode($retdata["body"]));
1804 if (!$success && (get_config('system','poco_discovery') > 2)) {
1805 logger("Fetch contacts from users of the server ".$server["nurl"], LOGGER_DEBUG);
1806 poco_discover_server_users($data, $server);
1810 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
1814 // If the server hadn't replied correctly, then force a sanity check
1815 poco_check_server($server["url"], $server["network"], true);
1817 // If we couldn't reach the server, we will try it some time later
1818 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
1824 function poco_discover($complete = false) {
1826 // Update the server list
1827 poco_discover_federation();
1831 $requery_days = intval(get_config("system", "poco_requery_days"));
1833 if ($requery_days == 0) {
1836 $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
1838 $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));
1839 if (dbm::is_result($r)) {
1840 foreach ($r as $server) {
1842 if (!poco_check_server($server["url"], $server["network"])) {
1843 // The server is not reachable? Okay, then we will try it later
1844 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
1848 logger('Update directory from server '.$server['url'].' with ID '.$server['id'], LOGGER_DEBUG);
1849 proc_run(PRIORITY_LOW, "include/discover_poco.php", "update_server_directory", (int)$server['id']);
1851 if (!$complete && (--$no_of_queries == 0)) {
1858 function poco_discover_server_users($data, $server) {
1860 if (!isset($data->entry)) {
1864 foreach ($data->entry as $entry) {
1866 if (isset($entry->urls)) {
1867 foreach ($entry->urls as $url) {
1868 if ($url->type == 'profile') {
1869 $profile_url = $url->value;
1870 $urlparts = parse_url($profile_url);
1871 $username = end(explode("/", $urlparts["path"]));
1875 if ($username != "") {
1876 logger("Fetch contacts for the user ".$username." from the server ".$server["nurl"], LOGGER_DEBUG);
1878 // Fetch all contacts from a given user from the other server
1879 $url = $server["poco"]."/".$username."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1881 $retdata = z_fetch_url($url);
1882 if ($retdata["success"]) {
1883 poco_discover_server(json_decode($retdata["body"]), 3);
1889 function poco_discover_server($data, $default_generation = 0) {
1891 if (!isset($data->entry) || !count($data->entry)) {
1897 foreach ($data->entry as $entry) {
1899 $profile_photo = '';
1903 $updated = NULL_DATE;
1909 $generation = $default_generation;
1911 $name = $entry->displayName;
1913 if (isset($entry->urls)) {
1914 foreach ($entry->urls as $url) {
1915 if ($url->type == 'profile') {
1916 $profile_url = $url->value;
1919 if ($url->type == 'webfinger') {
1920 $connect_url = str_replace('acct:' , '', $url->value);
1926 if (isset($entry->photos)) {
1927 foreach ($entry->photos as $photo) {
1928 if ($photo->type == 'profile') {
1929 $profile_photo = $photo->value;
1935 if (isset($entry->updated)) {
1936 $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
1939 if (isset($entry->network)) {
1940 $network = $entry->network;
1943 if (isset($entry->currentLocation)) {
1944 $location = $entry->currentLocation;
1947 if (isset($entry->aboutMe)) {
1948 $about = html2bbcode($entry->aboutMe);
1951 if (isset($entry->gender)) {
1952 $gender = $entry->gender;
1955 if(isset($entry->generation) && ($entry->generation > 0)) {
1956 $generation = ++$entry->generation;
1959 if(isset($entry->contactType) && ($entry->contactType >= 0)) {
1960 $contact_type = $entry->contactType;
1963 if (isset($entry->tags)) {
1964 foreach ($entry->tags as $tag) {
1965 $keywords = implode(", ", $tag);
1969 if ($generation > 0) {
1972 logger("Store profile ".$profile_url, LOGGER_DEBUG);
1974 $gcontact = array("url" => $profile_url,
1976 "network" => $network,
1977 "photo" => $profile_photo,
1979 "location" => $location,
1980 "gender" => $gender,
1981 "keywords" => $keywords,
1982 "connect" => $connect_url,
1983 "updated" => $updated,
1984 "contact-type" => $contact_type,
1985 "generation" => $generation);
1988 $gcontact = sanitize_gcontact($gcontact);
1989 update_gcontact($gcontact);
1990 } catch (Exception $e) {
1991 logger($e->getMessage(), LOGGER_DEBUG);
1994 logger("Done for profile ".$profile_url, LOGGER_DEBUG);
2001 * @brief Removes unwanted parts from a contact url
2003 * @param string $url Contact url
2004 * @return string Contact url with the wanted parts
2006 function clean_contact_url($url) {
2007 $parts = parse_url($url);
2009 if (!isset($parts["scheme"]) || !isset($parts["host"])) {
2013 $new_url = $parts["scheme"]."://".$parts["host"];
2015 if (isset($parts["port"])) {
2016 $new_url .= ":".$parts["port"];
2019 if (isset($parts["path"])) {
2020 $new_url .= $parts["path"];
2023 if ($new_url != $url) {
2024 logger("Cleaned contact url ".$url." to ".$new_url." - Called by: ".System::callstack(), LOGGER_DEBUG);
2031 * @brief Replace alternate OStatus user format with the primary one
2033 * @param arr $contact contact array (called by reference)
2035 function fix_alternate_contact_address(&$contact) {
2036 if (($contact["network"] == NETWORK_OSTATUS) && poco_alternate_ostatus_url($contact["url"])) {
2037 $data = probe_url($contact["url"]);
2038 if ($contact["network"] == NETWORK_OSTATUS) {
2039 logger("Fix primary url from ".$contact["url"]." to ".$data["url"]." - Called by: ".System::callstack(), LOGGER_DEBUG);
2040 $contact["url"] = $data["url"];
2041 $contact["addr"] = $data["addr"];
2042 $contact["alias"] = $data["alias"];
2043 $contact["server_url"] = $data["baseurl"];
2049 * @brief Fetch the gcontact id, add an entry if not existed
2051 * @param arr $contact contact array
2052 * @return bool|int Returns false if not found, integer if contact was found
2054 function get_gcontact_id($contact) {
2059 if (in_array($contact["network"], array(NETWORK_PHANTOM))) {
2060 logger("Invalid network for contact url ".$contact["url"]." - Called by: ".System::callstack(), LOGGER_DEBUG);
2064 if ($contact["network"] == NETWORK_STATUSNET) {
2065 $contact["network"] = NETWORK_OSTATUS;
2068 // All new contacts are hidden by default
2069 if (!isset($contact["hide"])) {
2070 $contact["hide"] = true;
2073 // Replace alternate OStatus user format with the primary one
2074 fix_alternate_contact_address($contact);
2076 // Remove unwanted parts from the contact url (e.g. "?zrl=...")
2077 if (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) {
2078 $contact["url"] = clean_contact_url($contact["url"]);
2081 dba::lock('gcontact');
2082 $r = q("SELECT `id`, `last_contact`, `last_failure`, `network` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
2083 dbesc(normalise_link($contact["url"])));
2085 if (dbm::is_result($r)) {
2086 $gcontact_id = $r[0]["id"];
2088 // Update every 90 days
2089 if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) {
2090 $last_failure_str = $r[0]["last_failure"];
2091 $last_failure = strtotime($r[0]["last_failure"]);
2092 $last_contact_str = $r[0]["last_contact"];
2093 $last_contact = strtotime($r[0]["last_contact"]);
2094 $doprobing = (((time() - $last_contact) > (90 * 86400)) && ((time() - $last_failure) > (90 * 86400)));
2097 q("INSERT INTO `gcontact` (`name`, `nick`, `addr` , `network`, `url`, `nurl`, `photo`, `created`, `updated`, `location`, `about`, `hide`, `generation`)
2098 VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d)",
2099 dbesc($contact["name"]),
2100 dbesc($contact["nick"]),
2101 dbesc($contact["addr"]),
2102 dbesc($contact["network"]),
2103 dbesc($contact["url"]),
2104 dbesc(normalise_link($contact["url"])),
2105 dbesc($contact["photo"]),
2106 dbesc(datetime_convert()),
2107 dbesc(datetime_convert()),
2108 dbesc($contact["location"]),
2109 dbesc($contact["about"]),
2110 intval($contact["hide"]),
2111 intval($contact["generation"])
2114 $r = q("SELECT `id`, `network` FROM `gcontact` WHERE `nurl` = '%s' ORDER BY `id` LIMIT 2",
2115 dbesc(normalise_link($contact["url"])));
2117 if (dbm::is_result($r)) {
2118 $gcontact_id = $r[0]["id"];
2120 $doprobing = in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""));
2126 logger("Last Contact: ". $last_contact_str." - Last Failure: ".$last_failure_str." - Checking: ".$contact["url"], LOGGER_DEBUG);
2127 proc_run(PRIORITY_LOW, 'include/gprobe.php', $contact["url"]);
2130 return $gcontact_id;
2134 * @brief Updates the gcontact table from a given array
2136 * @param arr $contact contact array
2137 * @return bool|int Returns false if not found, integer if contact was found
2139 function update_gcontact($contact) {
2141 // Check for invalid "contact-type" value
2142 if (isset($contact['contact-type']) && (intval($contact['contact-type']) < 0)) {
2143 $contact['contact-type'] = 0;
2146 /// @todo update contact table as well
2148 $gcontact_id = get_gcontact_id($contact);
2150 if (!$gcontact_id) {
2154 $r = q("SELECT `name`, `nick`, `photo`, `location`, `about`, `addr`, `generation`, `birthday`, `gender`, `keywords`,
2155 `contact-type`, `hide`, `nsfw`, `network`, `alias`, `notify`, `server_url`, `connect`, `updated`, `url`
2156 FROM `gcontact` WHERE `id` = %d LIMIT 1",
2157 intval($gcontact_id));
2159 // Get all field names
2161 foreach ($r[0] as $field => $data) {
2162 $fields[$field] = $data;
2165 unset($fields["url"]);
2166 unset($fields["updated"]);
2167 unset($fields["hide"]);
2169 // Bugfix: We had an error in the storing of keywords which lead to the "0"
2170 // This value is still transmitted via poco.
2171 if ($contact["keywords"] == "0") {
2172 unset($contact["keywords"]);
2175 if ($r[0]["keywords"] == "0") {
2176 $r[0]["keywords"] = "";
2179 // assign all unassigned fields from the database entry
2180 foreach ($fields as $field => $data) {
2181 if (!isset($contact[$field]) || ($contact[$field] == "")) {
2182 $contact[$field] = $r[0][$field];
2186 if (!isset($contact["hide"])) {
2187 $contact["hide"] = $r[0]["hide"];
2190 $fields["hide"] = $r[0]["hide"];
2192 if ($contact["network"] == NETWORK_STATUSNET) {
2193 $contact["network"] = NETWORK_OSTATUS;
2196 // Replace alternate OStatus user format with the primary one
2197 fix_alternate_contact_address($contact);
2199 if (!isset($contact["updated"])) {
2200 $contact["updated"] = dbm::date();
2203 if ($contact["network"] == NETWORK_TWITTER) {
2204 $contact["server_url"] = 'http://twitter.com';
2207 if ($contact["server_url"] == "") {
2208 $data = Probe::uri($contact["url"]);
2209 if ($data["network"] != NETWORK_PHANTOM) {
2210 $contact["server_url"] = $data['baseurl'];
2213 $contact["server_url"] = normalise_link($contact["server_url"]);
2216 if (($contact["addr"] == "") && ($contact["server_url"] != "") && ($contact["nick"] != "")) {
2217 $hostname = str_replace("http://", "", $contact["server_url"]);
2218 $contact["addr"] = $contact["nick"]."@".$hostname;
2221 // Check if any field changed
2223 unset($fields["generation"]);
2225 if ((($contact["generation"] > 0) && ($contact["generation"] <= $r[0]["generation"])) || ($r[0]["generation"] == 0)) {
2226 foreach ($fields as $field => $data) {
2227 if ($contact[$field] != $r[0][$field]) {
2228 logger("Difference for contact ".$contact["url"]." in field '".$field."'. New value: '".$contact[$field]."', old value '".$r[0][$field]."'", LOGGER_DEBUG);
2233 if ($contact["generation"] < $r[0]["generation"]) {
2234 logger("Difference for contact ".$contact["url"]." in field 'generation'. new value: '".$contact["generation"]."', old value '".$r[0]["generation"]."'", LOGGER_DEBUG);
2240 logger("Update gcontact for ".$contact["url"], LOGGER_DEBUG);
2242 q("UPDATE `gcontact` SET `photo` = '%s', `name` = '%s', `nick` = '%s', `addr` = '%s', `network` = '%s',
2243 `birthday` = '%s', `gender` = '%s', `keywords` = '%s', `hide` = %d, `nsfw` = %d,
2244 `contact-type` = %d, `alias` = '%s', `notify` = '%s', `url` = '%s',
2245 `location` = '%s', `about` = '%s', `generation` = %d, `updated` = '%s',
2246 `server_url` = '%s', `connect` = '%s'
2247 WHERE `nurl` = '%s' AND (`generation` = 0 OR `generation` >= %d)",
2248 dbesc($contact["photo"]), dbesc($contact["name"]), dbesc($contact["nick"]),
2249 dbesc($contact["addr"]), dbesc($contact["network"]), dbesc($contact["birthday"]),
2250 dbesc($contact["gender"]), dbesc($contact["keywords"]), intval($contact["hide"]),
2251 intval($contact["nsfw"]), intval($contact["contact-type"]), dbesc($contact["alias"]),
2252 dbesc($contact["notify"]), dbesc($contact["url"]), dbesc($contact["location"]),
2253 dbesc($contact["about"]), intval($contact["generation"]), dbesc(dbm::date($contact["updated"])),
2254 dbesc($contact["server_url"]), dbesc($contact["connect"]),
2255 dbesc(normalise_link($contact["url"])), intval($contact["generation"]));
2258 // Now update the contact entry with the user id "0" as well.
2259 // This is used for the shadow copies of public items.
2260 $r = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = 0 ORDER BY `id` LIMIT 1",
2261 dbesc(normalise_link($contact["url"])));
2263 if (dbm::is_result($r)) {
2264 logger("Update shadow contact ".$r[0]["id"], LOGGER_DEBUG);
2266 update_contact_avatar($contact["photo"], 0, $r[0]["id"]);
2268 q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `addr` = '%s',
2269 `network` = '%s', `bd` = '%s', `gender` = '%s',
2270 `keywords` = '%s', `alias` = '%s', `contact-type` = %d,
2271 `url` = '%s', `location` = '%s', `about` = '%s'
2273 dbesc($contact["name"]), dbesc($contact["nick"]), dbesc($contact["addr"]),
2274 dbesc($contact["network"]), dbesc($contact["birthday"]), dbesc($contact["gender"]),
2275 dbesc($contact["keywords"]), dbesc($contact["alias"]), intval($contact["contact-type"]),
2276 dbesc($contact["url"]), dbesc($contact["location"]), dbesc($contact["about"]),
2277 intval($r[0]["id"]));
2281 return $gcontact_id;
2285 * @brief Updates the gcontact entry from probe
2287 * @param str $url profile link
2289 function update_gcontact_from_probe($url) {
2290 $data = probe_url($url);
2292 if (in_array($data["network"], array(NETWORK_PHANTOM))) {
2293 logger("Invalid network for contact url ".$data["url"]." - Called by: ".System::callstack(), LOGGER_DEBUG);
2297 $data["server_url"] = $data["baseurl"];
2299 update_gcontact($data);
2303 * @brief Update the gcontact entry for a given user id
2305 * @param int $uid User ID
2307 function update_gcontact_for_user($uid) {
2308 $r = q("SELECT `profile`.`locality`, `profile`.`region`, `profile`.`country-name`,
2309 `profile`.`name`, `profile`.`about`, `profile`.`gender`,
2310 `profile`.`pub_keywords`, `profile`.`dob`, `profile`.`photo`,
2311 `profile`.`net-publish`, `user`.`nickname`, `user`.`hidewall`,
2312 `contact`.`notify`, `contact`.`url`, `contact`.`addr`
2314 INNER JOIN `user` ON `user`.`uid` = `profile`.`uid`
2315 INNER JOIN `contact` ON `contact`.`uid` = `profile`.`uid`
2316 WHERE `profile`.`uid` = %d AND `profile`.`is-default` AND `contact`.`self`",
2319 $location = formatted_location(array("locality" => $r[0]["locality"], "region" => $r[0]["region"],
2320 "country-name" => $r[0]["country-name"]));
2322 // The "addr" field was added in 3.4.3 so it can be empty for older users
2323 if ($r[0]["addr"] != "") {
2324 $addr = $r[0]["nickname"].'@'.str_replace(array("http://", "https://"), "", System::baseUrl());
2326 $addr = $r[0]["addr"];
2329 $gcontact = array("name" => $r[0]["name"], "location" => $location, "about" => $r[0]["about"],
2330 "gender" => $r[0]["gender"], "keywords" => $r[0]["pub_keywords"],
2331 "birthday" => $r[0]["dob"], "photo" => $r[0]["photo"],
2332 "notify" => $r[0]["notify"], "url" => $r[0]["url"],
2333 "hide" => ($r[0]["hidewall"] || !$r[0]["net-publish"]),
2334 "nick" => $r[0]["nickname"], "addr" => $addr,
2335 "connect" => $addr, "server_url" => System::baseUrl(),
2336 "generation" => 1, "network" => NETWORK_DFRN);
2338 update_gcontact($gcontact);
2342 * @brief Fetches users of given GNU Social server
2344 * If the "Statistics" plugin is enabled (See http://gstools.org/ for details) we query user data with this.
2346 * @param str $server Server address
2348 function gs_fetch_users($server) {
2350 logger("Fetching users from GNU Social server ".$server, LOGGER_DEBUG);
2352 $url = $server."/main/statistics";
2354 $result = z_fetch_url($url);
2355 if (!$result["success"]) {
2359 $statistics = json_decode($result["body"]);
2361 if (is_object($statistics->config)) {
2362 if ($statistics->config->instance_with_ssl) {
2363 $server = "https://";
2365 $server = "http://";
2368 $server .= $statistics->config->instance_address;
2370 $hostname = $statistics->config->instance_address;
2372 /// @TODO is_object() above means here no object, still $statistics is being used as object
2373 if ($statistics->instance_with_ssl) {
2374 $server = "https://";
2376 $server = "http://";
2379 $server .= $statistics->instance_address;
2381 $hostname = $statistics->instance_address;
2384 if (is_object($statistics->users)) {
2385 foreach ($statistics->users as $nick => $user) {
2386 $profile_url = $server."/".$user->nickname;
2388 $contact = array("url" => $profile_url,
2389 "name" => $user->fullname,
2390 "addr" => $user->nickname."@".$hostname,
2391 "nick" => $user->nickname,
2392 "about" => $user->bio,
2393 "network" => NETWORK_OSTATUS,
2394 "photo" => System::baseUrl()."/images/person-175.jpg");
2395 get_gcontact_id($contact);
2401 * @brief Asking GNU Social server on a regular base for their user data
2404 function gs_discover() {
2406 $requery_days = intval(get_config("system", "poco_requery_days"));
2408 $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
2410 $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",
2411 dbesc(NETWORK_OSTATUS), dbesc($last_update));
2413 if (!dbm::is_result($r)) {
2417 foreach ($r as $server) {
2418 gs_fetch_users($server["url"]);
2419 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
2424 * @brief Returns a list of all known servers
2425 * @return array List of server urls
2427 function poco_serverlist() {
2428 $r = q("SELECT `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver`
2429 WHERE `network` IN ('%s', '%s', '%s') AND `last_contact` > `last_failure`
2430 ORDER BY `last_contact`
2432 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
2433 if (!dbm::is_result($r)) {