3 * @file include/socgraph.php
5 * @todo Move GNU Social URL schemata (http://server.tld/user/number) to http://server.tld/username
6 * @todo Fetch profile data from profile page for Redmatrix users
7 * @todo Detect if it is a forum
10 require_once('include/datetime.php');
11 require_once("include/Scrape.php");
12 require_once("include/network.php");
13 require_once("include/html2bbcode.php");
14 require_once("include/Contact.php");
15 require_once("include/Photo.php");
18 * @brief Fetch POCO data
20 * @param integer $cid Contact ID
21 * @param integer $uid User ID
22 * @param integer $zcid Global Contact ID
23 * @param integer $url POCO address that should be polled
25 * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
26 * and add the entries to the gcontact (Global Contact) table, or update existing entries
27 * if anything (name or photo) has changed.
28 * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
30 * Once the global contact is stored add (if necessary) the contact linkage which associates
31 * the given uid, cid to the global contact entry. There can be many uid/cid combinations
32 * pointing to the same global contact id.
35 function poco_load($cid, $uid = 0, $zcid = 0, $url = null) {
36 // Call the function "poco_load_worker" via the worker
37 proc_run(PRIORITY_LOW, "include/discover_poco.php", "poco_load", intval($cid), intval($uid), intval($zcid), base64_encode($url));
41 * @brief Fetch POCO data from the worker
43 * @param integer $cid Contact ID
44 * @param integer $uid User ID
45 * @param integer $zcid Global Contact ID
46 * @param integer $url POCO address that should be polled
49 function poco_load_worker($cid, $uid, $zcid, $url) {
53 if((! $url) || (! $uid)) {
54 $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
57 if (dbm::is_result($r)) {
69 $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') ;
71 logger('poco_load: ' . $url, LOGGER_DEBUG);
75 logger('poco_load: returns ' . $s, LOGGER_DATA);
77 logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
79 if(($a->get_curl_code() > 299) || (! $s))
84 logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
86 if(! isset($j->entry))
90 foreach($j->entry as $entry) {
106 $name = $entry->displayName;
108 if (isset($entry->urls)) {
109 foreach ($entry->urls as $url) {
110 if ($url->type == 'profile') {
111 $profile_url = $url->value;
114 if ($url->type == 'webfinger') {
115 $connect_url = str_replace('acct:' , '', $url->value);
120 if (isset($entry->photos)) {
121 foreach ($entry->photos as $photo) {
122 if ($photo->type == 'profile') {
123 $profile_photo = $photo->value;
129 if (isset($entry->updated)) {
130 $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
133 if (isset($entry->network)) {
134 $network = $entry->network;
137 if (isset($entry->currentLocation)) {
138 $location = $entry->currentLocation;
141 if (isset($entry->aboutMe)) {
142 $about = html2bbcode($entry->aboutMe);
145 if (isset($entry->gender)) {
146 $gender = $entry->gender;
149 if (isset($entry->generation) AND ($entry->generation > 0)) {
150 $generation = ++$entry->generation;
153 if (isset($entry->tags)) {
154 foreach($entry->tags as $tag) {
155 $keywords = implode(", ", $tag);
159 if (isset($entry->contactType) AND ($entry->contactType >= 0))
160 $contact_type = $entry->contactType;
162 // If you query a Friendica server for its profiles, the network has to be Friendica
163 /// TODO It could also be a Redmatrix server
165 // $network = NETWORK_DFRN;
167 poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid, $uid, $zcid);
169 $gcontact = array("url" => $profile_url, "contact-type" => $contact_type, "generation" => $generation);
170 update_gcontact($gcontact);
172 // Update the Friendica contacts. Diaspora is doing it via a message. (See include/diaspora.php)
173 // Deactivated because we now update Friendica contacts in dfrn.php
174 //if (($location != "") OR ($about != "") OR ($keywords != "") OR ($gender != ""))
175 // q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s'
176 // WHERE `nurl` = '%s' AND NOT `self` AND `network` = '%s'",
181 // dbesc(normalise_link($profile_url)),
182 // dbesc(NETWORK_DFRN));
184 logger("poco_load: loaded $total entries",LOGGER_DEBUG);
186 q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
194 * @brief Validate the given gcontact data
196 * @param array $gcontact array with gcontact data
197 * @param integer $cid Contact ID
198 * @param integer $uid User ID
199 * @param integer $zcid Global Contact ID
203 * 1: Profiles on this server
204 * 2: Contacts of profiles on this server
205 * 3: Contacts of contacts of profiles on this server
209 function validate_gcontact(&$gcontact) {
211 if ($gcontact['url'] == "") {
215 $urlparts = parse_url($gcontact['url']);
216 if (!isset($urlparts["scheme"])) {
220 if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com",
221 "identi.ca", "alpha.app.net"))) {
225 // Don't store the statusnet connector as network
226 // We can't simply set this to NETWORK_OSTATUS since the connector could have fetched posts from friendica as well
227 if ($gcontact['network'] == NETWORK_STATUSNET) {
228 $gcontact['network'] = "";
231 // Assure that there are no parameter fragments in the profile url
232 if (in_array($gcontact['network'], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) {
233 $gcontact['url'] = clean_contact_url($gcontact['url']);
236 $alternate = poco_alternate_ostatus_url($gcontact['url']);
238 // The global contacts should contain the original picture, not the cached one
239 if (($gcontact['generation'] != 1) AND stristr(normalise_link($gcontact['photo']), normalise_link(App::get_baseurl()."/photo/"))) {
240 $gcontact['photo'] = "";
243 if (!isset($gcontact['network'])) {
244 $r = q("SELECT `network` FROM `contact` WHERE `uid` = 0 AND `nurl` = '%s' AND `network` != '' AND `network` != '%s' LIMIT 1",
245 dbesc(normalise_link($gcontact['url'])), dbesc(NETWORK_STATUSNET)
247 if (dbm::is_result($r)) {
248 $gcontact['network'] = $r[0]["network"];
251 if (($gcontact['network'] == "") OR ($gcontact['network'] == NETWORK_OSTATUS)) {
252 $r = q("SELECT `network`, `url` FROM `contact` WHERE `uid` = 0 AND `alias` IN ('%s', '%s') AND `network` != '' AND `network` != '%s' LIMIT 1",
253 dbesc($gcontact['url']), dbesc(normalise_link($gcontact['url'])), dbesc(NETWORK_STATUSNET)
255 if (dbm::is_result($r)) {
256 $gcontact['network'] = $r[0]["network"];
261 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
262 dbesc(normalise_link($gcontact['url']))
266 if (!isset($gcontact['network']) AND ($x[0]["network"] != NETWORK_STATUSNET)) {
267 $gcontact['network'] = $x[0]["network"];
269 if ($gcontact['updated'] <= NULL_DATE) {
270 $gcontact['updated'] = $x[0]["updated"];
272 if (!isset($gcontact['server_url'])) {
273 $gcontact['server_url'] = $x[0]["server_url"];
275 if (!isset($gcontact['addr'])) {
276 $gcontact['addr'] = $x[0]["addr"];
279 if (!isset($gcontact['server_url'])) {
280 $gcontact['server_url'] = '';
282 if (!isset($gcontact['network'])) {
283 $gcontact['network'] = '';
287 if ((!isset($gcontact['network']) OR !isset($gcontact['name']) OR !isset($gcontact['addr']) OR !isset($gcontact['photo']) OR !isset($gcontact['server_url']) OR $alternate)
288 AND poco_reachable($gcontact['url'], $gcontact['server_url'], $gcontact['network'], false)) {
289 $data = Probe::uri($gcontact['url']);
291 if ($data["network"] == NETWORK_PHANTOM) {
295 $orig_profile = $gcontact['url'];
297 $gcontact["server_url"] = $data["baseurl"];
299 unset($data["guid"]);
300 unset($data["batch"]);
301 unset($data["poll"]);
302 unset($data["request"]);
303 unset($data["confirm"]);
304 unset($data["poco"]);
305 unset($data["priority"]);
306 unset($data["pubkey"]);
307 unset($data["baseurl"]);
309 $gcontact = array_merge($gcontact, $data);
311 if ($alternate AND ($gcontact['network'] == NETWORK_OSTATUS)) {
312 // Delete the old entry - if it exists
313 $r = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($orig_profile)));
315 q("DELETE FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($orig_profile)));
316 q("DELETE FROM `glink` WHERE `gcid` = %d", intval($r[0]["id"]));
321 if (!isset($gcontact['name']) OR !isset($gcontact['photo'])) {
325 if (!in_array($gcontact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA))) {
329 if (!isset($gcontact['server_url'])) {
330 // We check the server url to be sure that it is a real one
331 $server_url = poco_detect_server($gcontact['url']);
333 // We are now sure that it is a correct URL. So we use it in the future
334 if ($server_url != "") {
335 $gcontact['server_url'] = $server_url;
339 // The server URL doesn't seem to be valid, so we don't store it.
340 if (!poco_check_server($gcontact['server_url'], $gcontact['network'])) {
341 $gcontact['server_url'] = "";
347 function poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid = 0, $uid = 0, $zcid = 0) {
351 // 1: Profiles on this server
352 // 2: Contacts of profiles on this server
353 // 3: Contacts of contacts of profiles on this server
358 if ($profile_url == "")
361 $urlparts = parse_url($profile_url);
362 if (!isset($urlparts["scheme"]))
365 if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com",
366 "identi.ca", "alpha.app.net")))
369 // Don't store the statusnet connector as network
370 // We can't simply set this to NETWORK_OSTATUS since the connector could have fetched posts from friendica as well
371 if ($network == NETWORK_STATUSNET)
374 // Assure that there are no parameter fragments in the profile url
375 if (in_array($network, array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, "")))
376 $profile_url = clean_contact_url($profile_url);
378 $alternate = poco_alternate_ostatus_url($profile_url);
380 $orig_updated = $updated;
382 // The global contacts should contain the original picture, not the cached one
383 if (($generation != 1) AND stristr(normalise_link($profile_photo), normalise_link(App::get_baseurl()."/photo/"))) {
387 $r = q("SELECT `network` FROM `contact` WHERE `nurl` = '%s' AND `network` != '' AND `network` != '%s' LIMIT 1",
388 dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET)
390 if (dbm::is_result($r)) {
391 $network = $r[0]["network"];
394 if (($network == "") OR ($network == NETWORK_OSTATUS)) {
395 $r = q("SELECT `network`, `url` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `network` != '' AND `network` != '%s' LIMIT 1",
396 dbesc($profile_url), dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET)
398 if (dbm::is_result($r)) {
399 $network = $r[0]["network"];
400 //$profile_url = $r[0]["url"];
404 $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
405 dbesc(normalise_link($profile_url))
409 if (($network == "") AND ($x[0]["network"] != NETWORK_STATUSNET)) {
410 $network = $x[0]["network"];
412 if ($updated <= NULL_DATE) {
413 $updated = $x[0]["updated"];
415 $created = $x[0]["created"];
416 $server_url = $x[0]["server_url"];
417 $nick = $x[0]["nick"];
418 $addr = $x[0]["addr"];
419 $alias = $x[0]["alias"];
420 $notify = $x[0]["notify"];
422 $created = NULL_DATE;
425 $urlparts = parse_url($profile_url);
426 $nick = end(explode("/", $urlparts["path"]));
432 if ((($network == "") OR ($name == "") OR ($addr == "") OR ($profile_photo == "") OR ($server_url == "") OR $alternate)
433 AND poco_reachable($profile_url, $server_url, $network, false)) {
434 $data = probe_url($profile_url);
436 $orig_profile = $profile_url;
438 $network = $data["network"];
439 $name = $data["name"];
440 $nick = $data["nick"];
441 $addr = $data["addr"];
442 $alias = $data["alias"];
443 $notify = $data["notify"];
444 $profile_url = $data["url"];
445 $profile_photo = $data["photo"];
446 $server_url = $data["baseurl"];
448 if ($alternate AND ($network == NETWORK_OSTATUS)) {
449 // Delete the old entry - if it exists
450 $r = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($orig_profile)));
452 q("DELETE FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($orig_profile)));
453 q("DELETE FROM `glink` WHERE `gcid` = %d", intval($r[0]["id"]));
456 // possibly create a new entry
457 poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid, $uid, $zcid);
461 if ($alternate AND ($network == NETWORK_OSTATUS))
464 if (count($x) AND ($x[0]["network"] == "") AND ($network != "")) {
465 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
467 dbesc(normalise_link($profile_url))
471 if (($name == "") OR ($profile_photo == ""))
474 if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
477 logger("profile-check generation: ".$generation." Network: ".$network." URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
479 // We check the server url to be sure that it is a real one
480 $server_url2 = poco_detect_server($profile_url);
482 // We are no sure that it is a correct URL. So we use it in the future
483 if ($server_url2 != "") {
484 $server_url = $server_url2;
487 // The server URL doesn't seem to be valid, so we don't store it.
488 if (!poco_check_server($server_url, $network)) {
492 $gcontact = array("url" => $profile_url,
496 "network" => $network,
497 "photo" => $profile_photo,
499 "location" => $location,
501 "keywords" => $keywords,
502 "server_url" => $server_url,
503 "connect" => $connect_url,
505 "updated" => $updated,
506 "generation" => $generation);
508 $gcid = update_gcontact($gcontact);
513 link_gcontact($gcid, $uid, $cid, $zcid);
518 function link_gcontact($gcid, $uid = 0, $cid = 0, $zcid = 0) {
519 $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
525 if (!dbm::is_result($r)) {
526 q("INSERT INTO `glink` (`cid`, `uid`, `gcid`, `zcid`, `updated`) VALUES (%d, %d, %d, %d, '%s') ",
531 dbesc(datetime_convert())
534 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
535 dbesc(datetime_convert()),
544 function poco_reachable($profile, $server = "", $network = "", $force = false) {
547 $server = poco_detect_server($profile);
552 return poco_check_server($server, $network, $force);
555 function poco_detect_server($profile) {
557 // Try to detect the server path based upon some known standard paths
560 if ($server_url == "") {
561 $friendica = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $profile);
562 if ($friendica != $profile) {
563 $server_url = $friendica;
564 $network = NETWORK_DFRN;
568 if ($server_url == "") {
569 $diaspora = preg_replace("=(https?://)(.*)/u/(.*)=ism", "$1$2", $profile);
570 if ($diaspora != $profile) {
571 $server_url = $diaspora;
572 $network = NETWORK_DIASPORA;
576 if ($server_url == "") {
577 $red = preg_replace("=(https?://)(.*)/channel/(.*)=ism", "$1$2", $profile);
578 if ($red != $profile) {
580 $network = NETWORK_DIASPORA;
585 if ($server_url == "") {
586 $mastodon = preg_replace("=(https?://)(.*)/users/(.*)=ism", "$1$2", $profile);
587 if ($mastodon != $profile) {
588 $server_url = $mastodon;
589 $network = NETWORK_OSTATUS;
593 // Numeric OStatus variant
594 if ($server_url == "") {
595 $ostatus = preg_replace("=(https?://)(.*)/user/(.*)=ism", "$1$2", $profile);
596 if ($ostatus != $profile) {
597 $server_url = $ostatus;
598 $network = NETWORK_OSTATUS;
603 if ($server_url == "") {
604 $base = preg_replace("=(https?://)(.*?)/(.*)=ism", "$1$2", $profile);
605 if ($base != $profile) {
607 $network = NETWORK_PHANTOM;
611 if ($server_url == "") {
615 $r = q("SELECT `id` FROM `gserver` WHERE `nurl` = '%s' AND `last_contact` > `last_failure`",
616 dbesc(normalise_link($server_url)));
617 if (dbm::is_result($r)) {
621 // Fetch the host-meta to check if this really is a server
622 $serverret = z_fetch_url($server_url."/.well-known/host-meta");
623 if (!$serverret["success"]) {
630 function poco_alternate_ostatus_url($url) {
631 return(preg_match("=https?://.+/user/\d+=ism", $url, $matches));
634 function poco_last_updated($profile, $force = false) {
636 $gcontacts = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'",
637 dbesc(normalise_link($profile)));
639 if ($gcontacts[0]["created"] <= NULL_DATE) {
640 q("UPDATE `gcontact` SET `created` = '%s' WHERE `nurl` = '%s'",
641 dbesc(datetime_convert()), dbesc(normalise_link($profile)));
643 if ($gcontacts[0]["server_url"] != "") {
644 $server_url = $gcontacts[0]["server_url"];
646 if (($server_url == '') OR ($gcontacts[0]["server_url"] == $gcontacts[0]["nurl"])) {
647 $server_url = poco_detect_server($profile);
650 if (!in_array($gcontacts[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_FEED, NETWORK_OSTATUS, ""))) {
651 logger("Profile ".$profile.": Network type ".$gcontacts[0]["network"]." can't be checked", LOGGER_DEBUG);
655 if ($server_url != "") {
656 if (!poco_check_server($server_url, $gcontacts[0]["network"], $force)) {
659 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
660 dbesc(datetime_convert()), dbesc(normalise_link($profile)));
662 logger("Profile ".$profile.": Server ".$server_url." wasn't reachable.", LOGGER_DEBUG);
666 q("UPDATE `gcontact` SET `server_url` = '%s' WHERE `nurl` = '%s'",
667 dbesc($server_url), dbesc(normalise_link($profile)));
670 if (in_array($gcontacts[0]["network"], array("", NETWORK_FEED))) {
671 $server = q("SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''",
672 dbesc(normalise_link($server_url)));
675 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
676 dbesc($server[0]["network"]), dbesc(normalise_link($profile)));
681 // noscrape is really fast so we don't cache the call.
682 if (($gcontacts[0]["server_url"] != "") AND ($gcontacts[0]["nick"] != "")) {
684 // Use noscrape if possible
685 $server = q("SELECT `noscrape`, `network` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", dbesc(normalise_link($gcontacts[0]["server_url"])));
688 $noscraperet = z_fetch_url($server[0]["noscrape"]."/".$gcontacts[0]["nick"]);
690 if ($noscraperet["success"] AND ($noscraperet["body"] != "")) {
692 $noscrape = json_decode($noscraperet["body"], true);
694 if (is_array($noscrape)) {
695 $contact = array("url" => $profile,
696 "network" => $server[0]["network"],
697 "generation" => $gcontacts[0]["generation"]);
699 if (isset($noscrape["fn"]))
700 $contact["name"] = $noscrape["fn"];
702 if (isset($noscrape["comm"]))
703 $contact["community"] = $noscrape["comm"];
705 if (isset($noscrape["tags"])) {
706 $keywords = implode(" ", $noscrape["tags"]);
708 $contact["keywords"] = $keywords;
711 $location = formatted_location($noscrape);
713 $contact["location"] = $location;
715 if (isset($noscrape["dfrn-notify"]))
716 $contact["notify"] = $noscrape["dfrn-notify"];
718 // Remove all fields that are not present in the gcontact table
719 unset($noscrape["fn"]);
720 unset($noscrape["key"]);
721 unset($noscrape["homepage"]);
722 unset($noscrape["comm"]);
723 unset($noscrape["tags"]);
724 unset($noscrape["locality"]);
725 unset($noscrape["region"]);
726 unset($noscrape["country-name"]);
727 unset($noscrape["contacts"]);
728 unset($noscrape["dfrn-request"]);
729 unset($noscrape["dfrn-confirm"]);
730 unset($noscrape["dfrn-notify"]);
731 unset($noscrape["dfrn-poll"]);
733 // Set the date of the last contact
734 /// @todo By now the function "update_gcontact" doesn't work with this field
735 //$contact["last_contact"] = datetime_convert();
737 $contact = array_merge($contact, $noscrape);
739 update_gcontact($contact);
741 if (trim($noscrape["updated"]) != "") {
742 q("UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'",
743 dbesc(datetime_convert()), dbesc(normalise_link($profile)));
745 logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG);
747 return $noscrape["updated"];
754 // If we only can poll the feed, then we only do this once a while
755 if (!$force AND !poco_do_update($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) {
756 logger("Profile ".$profile." was last updated at ".$gcontacts[0]["updated"]." (cached)", LOGGER_DEBUG);
757 return $gcontacts[0]["updated"];
760 $data = Probe::uri($profile);
762 // Is the profile link the alternate OStatus link notation? (http://domain.tld/user/4711)
763 // Then check the other link and delete this one
764 if (($data["network"] == NETWORK_OSTATUS) AND poco_alternate_ostatus_url($profile) AND
765 (normalise_link($profile) == normalise_link($data["alias"])) AND
766 (normalise_link($profile) != normalise_link($data["url"]))) {
768 // Delete the old entry
769 q("DELETE FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($profile)));
770 q("DELETE FROM `glink` WHERE `gcid` = %d", intval($gcontacts[0]["id"]));
772 poco_check($data["url"], $data["name"], $data["network"], $data["photo"], $gcontacts[0]["about"], $gcontacts[0]["location"],
773 $gcontacts[0]["gender"], $gcontacts[0]["keywords"], $data["addr"], $gcontacts[0]["updated"], $gcontacts[0]["generation"]);
775 poco_last_updated($data["url"], $force);
777 logger("Profile ".$profile." was deleted", LOGGER_DEBUG);
781 if (($data["poll"] == "") OR (in_array($data["network"], array(NETWORK_FEED, NETWORK_PHANTOM)))) {
782 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
783 dbesc(datetime_convert()), dbesc(normalise_link($profile)));
785 logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG);
789 $contact = array("generation" => $gcontacts[0]["generation"]);
791 $contact = array_merge($contact, $data);
793 $contact["server_url"] = $data["baseurl"];
795 unset($contact["guid"]);
796 unset($contact["batch"]);
797 unset($contact["poll"]);
798 unset($contact["request"]);
799 unset($contact["confirm"]);
800 unset($contact["poco"]);
801 unset($contact["priority"]);
802 unset($contact["pubkey"]);
803 unset($contact["baseurl"]);
805 update_gcontact($contact);
807 $feedret = z_fetch_url($data["poll"]);
809 if (!$feedret["success"]) {
810 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
811 dbesc(datetime_convert()), dbesc(normalise_link($profile)));
813 logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG);
817 $doc = new DOMDocument();
818 @$doc->loadXML($feedret["body"]);
820 $xpath = new DomXPath($doc);
821 $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
823 $entries = $xpath->query('/atom:feed/atom:entry');
827 foreach ($entries AS $entry) {
828 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
829 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
831 if ($last_updated < $published)
832 $last_updated = $published;
834 if ($last_updated < $updated)
835 $last_updated = $updated;
838 // Maybe there aren't any entries. Then check if it is a valid feed
839 if ($last_updated == "") {
840 if ($xpath->query('/atom:feed')->length > 0) {
841 $last_updated = NULL_DATE;
844 q("UPDATE `gcontact` SET `updated` = '%s', `last_contact` = '%s' WHERE `nurl` = '%s'",
845 dbesc(dbm::date($last_updated)), dbesc(dbm::date()), dbesc(normalise_link($profile)));
847 if (($gcontacts[0]["generation"] == 0))
848 q("UPDATE `gcontact` SET `generation` = 9 WHERE `nurl` = '%s'",
849 dbesc(normalise_link($profile)));
851 logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG);
853 return($last_updated);
856 function poco_do_update($created, $updated, $last_failure, $last_contact) {
857 $now = strtotime(datetime_convert());
859 if ($updated > $last_contact)
860 $contact_time = strtotime($updated);
862 $contact_time = strtotime($last_contact);
864 $failure_time = strtotime($last_failure);
865 $created_time = strtotime($created);
867 // If there is no "created" time then use the current time
868 if ($created_time <= 0)
869 $created_time = $now;
871 // If the last contact was less than 24 hours then don't update
872 if (($now - $contact_time) < (60 * 60 * 24))
875 // If the last failure was less than 24 hours then don't update
876 if (($now - $failure_time) < (60 * 60 * 24))
879 // If the last contact was less than a week ago and the last failure is older than a week then don't update
880 //if ((($now - $contact_time) < (60 * 60 * 24 * 7)) AND ($contact_time > $failure_time))
883 // 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
884 if ((($now - $contact_time) > (60 * 60 * 24 * 7)) AND (($now - $created_time) > (60 * 60 * 24 * 7)) AND (($now - $failure_time) < (60 * 60 * 24 * 7)))
887 // 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
888 if ((($now - $contact_time) > (60 * 60 * 24 * 30)) AND (($now - $created_time) > (60 * 60 * 24 * 30)) AND (($now - $failure_time) < (60 * 60 * 24 * 30)))
894 function poco_to_boolean($val) {
895 if (($val == "true") OR ($val == 1))
897 if (($val == "false") OR ($val == 0))
904 * @brief Detect server type (Hubzilla or Friendica) via the poco data
906 * @param object $data POCO data
907 * @return array Server data
909 function poco_detect_poco_data($data) {
912 if (!isset($data->entry)) {
916 if (count($data->entry) == 0) {
920 if (!isset($data->entry[0]->urls)) {
924 if (count($data->entry[0]->urls) == 0) {
928 foreach ($data->entry[0]->urls AS $url) {
929 if ($url->type == 'zot') {
931 $server["platform"] = 'Hubzilla';
932 $server["network"] = NETWORK_DIASPORA;
940 * @brief Detect server type by using the nodeinfo data
942 * @param string $server_url address of the server
943 * @return array Server data
945 function poco_fetch_nodeinfo($server_url) {
946 $serverret = z_fetch_url($server_url."/.well-known/nodeinfo");
947 if (!$serverret["success"]) {
951 $nodeinfo = json_decode($serverret['body']);
953 if (!is_object($nodeinfo)) {
957 if (!is_array($nodeinfo->links)) {
963 foreach ($nodeinfo->links AS $link) {
964 if ($link->rel == 'http://nodeinfo.diaspora.software/ns/schema/1.0') {
965 $nodeinfo_url = $link->href;
969 if ($nodeinfo_url == '') {
973 $serverret = z_fetch_url($nodeinfo_url);
974 if (!$serverret["success"]) {
978 $nodeinfo = json_decode($serverret['body']);
980 if (!is_object($nodeinfo)) {
986 $server['register_policy'] = REGISTER_CLOSED;
988 if (is_bool($nodeinfo->openRegistrations) AND $nodeinfo->openRegistrations) {
989 $server['register_policy'] = REGISTER_OPEN;
992 if (is_object($nodeinfo->software)) {
993 if (isset($nodeinfo->software->name)) {
994 $server['platform'] = $nodeinfo->software->name;
997 if (isset($nodeinfo->software->version)) {
998 $server['version'] = $nodeinfo->software->version;
999 // Version numbers on Nodeinfo are presented with additional info, e.g.:
1000 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
1001 $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']);
1005 if (is_object($nodeinfo->metadata)) {
1006 if (isset($nodeinfo->metadata->nodeName)) {
1007 $server['site_name'] = $nodeinfo->metadata->nodeName;
1015 if (is_array($nodeinfo->protocols->inbound)) {
1016 foreach ($nodeinfo->protocols->inbound AS $inbound) {
1017 if ($inbound == 'diaspora') {
1020 if ($inbound == 'friendica') {
1023 if ($inbound == 'gnusocial') {
1030 $server['network'] = NETWORK_OSTATUS;
1033 $server['network'] = NETWORK_DIASPORA;
1036 $server['network'] = NETWORK_DFRN;
1047 * @brief Detect server type (Hubzilla or Friendica) via the front page body
1049 * @param string $body Front page of the server
1050 * @return array Server data
1052 function poco_detect_server_type($body) {
1055 $doc = new \DOMDocument();
1056 @$doc->loadHTML($body);
1057 $xpath = new \DomXPath($doc);
1059 $list = $xpath->query("//meta[@name]");
1061 foreach ($list as $node) {
1063 if ($node->attributes->length) {
1064 foreach ($node->attributes as $attribute) {
1065 $attr[$attribute->name] = $attribute->value;
1068 if ($attr['name'] == 'generator') {
1069 $version_part = explode(" ", $attr['content']);
1070 if (count($version_part) == 2) {
1071 if (in_array($version_part[0], array("Friendika", "Friendica"))) {
1073 $server["platform"] = $version_part[0];
1074 $server["version"] = $version_part[1];
1075 $server["network"] = NETWORK_DFRN;
1082 $list = $xpath->query("//meta[@property]");
1084 foreach ($list as $node) {
1086 if ($node->attributes->length) {
1087 foreach ($node->attributes as $attribute) {
1088 $attr[$attribute->name] = $attribute->value;
1091 if ($attr['property'] == 'generator') {
1092 if (in_array($attr['content'], array("hubzilla", "BlaBlaNet"))) {
1094 $server["platform"] = $attr['content'];
1095 $server["version"] = "";
1096 $server["network"] = NETWORK_DIASPORA;
1106 $server["site_name"] = $xpath->evaluate($element."//head/title/text()", $context)->item(0)->nodeValue;
1110 function poco_check_server($server_url, $network = "", $force = false) {
1112 // Unify the server address
1113 $server_url = trim($server_url, "/");
1114 $server_url = str_replace("/index.php", "", $server_url);
1116 if ($server_url == "")
1119 $servers = q("SELECT * FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
1120 if (dbm::is_result($servers)) {
1122 if ($servers[0]["created"] <= NULL_DATE) {
1123 q("UPDATE `gserver` SET `created` = '%s' WHERE `nurl` = '%s'",
1124 dbesc(datetime_convert()), dbesc(normalise_link($server_url)));
1126 $poco = $servers[0]["poco"];
1127 $noscrape = $servers[0]["noscrape"];
1130 $network = $servers[0]["network"];
1132 $last_contact = $servers[0]["last_contact"];
1133 $last_failure = $servers[0]["last_failure"];
1134 $version = $servers[0]["version"];
1135 $platform = $servers[0]["platform"];
1136 $site_name = $servers[0]["site_name"];
1137 $info = $servers[0]["info"];
1138 $register_policy = $servers[0]["register_policy"];
1140 if (!$force AND !poco_do_update($servers[0]["created"], "", $last_failure, $last_contact)) {
1141 logger("Use cached data for server ".$server_url, LOGGER_DEBUG);
1142 return ($last_contact >= $last_failure);
1151 $register_policy = -1;
1153 $last_contact = NULL_DATE;
1154 $last_failure = NULL_DATE;
1156 logger("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$servers[0]["created"]." Failure: ".$last_failure." Contact: ".$last_contact, LOGGER_DEBUG);
1159 $possible_failure = false;
1160 $orig_last_failure = $last_failure;
1161 $orig_last_contact = $last_contact;
1163 // Check if the page is accessible via SSL.
1164 $orig_server_url = $server_url;
1165 $server_url = str_replace("http://", "https://", $server_url);
1167 // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
1168 $serverret = z_fetch_url($server_url."/.well-known/host-meta", false, $redirects, array('timeout' => 20));
1170 // Quit if there is a timeout.
1171 // But we want to make sure to only quit if we are mostly sure that this server url fits.
1172 if (dbm::is_result($servers) AND ($orig_server_url == $server_url) AND
1173 ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT)) {
1174 logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
1178 // Maybe the page is unencrypted only?
1179 $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
1180 if (!$serverret["success"] OR ($serverret["body"] == "") OR (@sizeof($xmlobj) == 0) OR !is_object($xmlobj)) {
1181 $server_url = str_replace("https://", "http://", $server_url);
1183 // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
1184 $serverret = z_fetch_url($server_url."/.well-known/host-meta", false, $redirects, array('timeout' => 20));
1186 // Quit if there is a timeout
1187 if ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT) {
1188 logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
1192 $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
1195 if (!$serverret["success"] OR ($serverret["body"] == "") OR (sizeof($xmlobj) == 0) OR !is_object($xmlobj)) {
1196 // Workaround for bad configured servers (known nginx problem)
1197 if (!in_array($serverret["debug"]["http_code"], array("403", "404"))) {
1198 $last_failure = datetime_convert();
1201 $possible_failure = true;
1202 } elseif ($network == NETWORK_DIASPORA)
1203 $last_contact = datetime_convert();
1205 // If the server has no possible failure we reset the cached data
1206 if (!$possible_failure) {
1211 $register_policy = -1;
1216 $serverret = z_fetch_url($server_url."/poco");
1217 if ($serverret["success"]) {
1218 $data = json_decode($serverret["body"]);
1219 if (isset($data->totalResults)) {
1220 $poco = $server_url."/poco";
1221 $last_contact = datetime_convert();
1223 $server = poco_detect_poco_data($data);
1225 $platform = $server['platform'];
1226 $network = $server['network'];
1235 // Test for Diaspora, Hubzilla, Mastodon or older Friendica servers
1236 $serverret = z_fetch_url($server_url);
1238 if (!$serverret["success"] OR ($serverret["body"] == "")) {
1239 $last_failure = datetime_convert();
1242 $server = poco_detect_server_type($serverret["body"]);
1244 $platform = $server['platform'];
1245 $network = $server['network'];
1246 $version = $server['version'];
1247 $site_name = $server['site_name'];
1248 $last_contact = datetime_convert();
1251 $lines = explode("\n",$serverret["header"]);
1253 foreach($lines as $line) {
1254 $line = trim($line);
1255 if(stristr($line,'X-Diaspora-Version:')) {
1256 $platform = "Diaspora";
1257 $version = trim(str_replace("X-Diaspora-Version:", "", $line));
1258 $version = trim(str_replace("x-diaspora-version:", "", $version));
1259 $network = NETWORK_DIASPORA;
1260 $versionparts = explode("-", $version);
1261 $version = $versionparts[0];
1262 $last_contact = datetime_convert();
1265 if(stristr($line,'Server: Mastodon')) {
1266 $platform = "Mastodon";
1267 $network = NETWORK_OSTATUS;
1268 // Mastodon doesn't reveal version numbers
1270 $last_contact = datetime_convert();
1277 if (!$failure AND ($poco == "")) {
1278 // Test for Statusnet
1279 // Will also return data for Friendica and GNU Social - but it will be overwritten later
1280 // The "not implemented" is a special treatment for really, really old Friendica versions
1281 $serverret = z_fetch_url($server_url."/api/statusnet/version.json");
1282 if ($serverret["success"] AND ($serverret["body"] != '{"error":"not implemented"}') AND
1283 ($serverret["body"] != '') AND (strlen($serverret["body"]) < 30)) {
1284 $platform = "StatusNet";
1285 // Remove junk that some GNU Social servers return
1286 $version = str_replace(chr(239).chr(187).chr(191), "", $serverret["body"]);
1287 $version = trim($version, '"');
1288 $network = NETWORK_OSTATUS;
1289 $last_contact = datetime_convert();
1292 // Test for GNU Social
1293 $serverret = z_fetch_url($server_url."/api/gnusocial/version.json");
1294 if ($serverret["success"] AND ($serverret["body"] != '{"error":"not implemented"}') AND
1295 ($serverret["body"] != '') AND (strlen($serverret["body"]) < 30)) {
1296 $platform = "GNU Social";
1297 // Remove junk that some GNU Social servers return
1298 $version = str_replace(chr(239).chr(187).chr(191), "", $serverret["body"]);
1299 $version = trim($version, '"');
1300 $network = NETWORK_OSTATUS;
1301 $last_contact = datetime_convert();
1306 // Test for Hubzilla, Redmatrix or Friendica
1307 $serverret = z_fetch_url($server_url."/api/statusnet/config.json");
1308 if ($serverret["success"]) {
1309 $data = json_decode($serverret["body"]);
1310 if (isset($data->site->server)) {
1311 $last_contact = datetime_convert();
1313 if (isset($data->site->platform)) {
1314 $platform = $data->site->platform->PLATFORM_NAME;
1315 $version = $data->site->platform->STD_VERSION;
1316 $network = NETWORK_DIASPORA;
1318 if (isset($data->site->BlaBlaNet)) {
1319 $platform = $data->site->BlaBlaNet->PLATFORM_NAME;
1320 $version = $data->site->BlaBlaNet->STD_VERSION;
1321 $network = NETWORK_DIASPORA;
1323 if (isset($data->site->hubzilla)) {
1324 $platform = $data->site->hubzilla->PLATFORM_NAME;
1325 $version = $data->site->hubzilla->RED_VERSION;
1326 $network = NETWORK_DIASPORA;
1328 if (isset($data->site->redmatrix)) {
1329 if (isset($data->site->redmatrix->PLATFORM_NAME))
1330 $platform = $data->site->redmatrix->PLATFORM_NAME;
1331 elseif (isset($data->site->redmatrix->RED_PLATFORM))
1332 $platform = $data->site->redmatrix->RED_PLATFORM;
1334 $version = $data->site->redmatrix->RED_VERSION;
1335 $network = NETWORK_DIASPORA;
1337 if (isset($data->site->friendica)) {
1338 $platform = $data->site->friendica->FRIENDICA_PLATFORM;
1339 $version = $data->site->friendica->FRIENDICA_VERSION;
1340 $network = NETWORK_DFRN;
1343 $site_name = $data->site->name;
1345 $data->site->closed = poco_to_boolean($data->site->closed);
1346 $data->site->private = poco_to_boolean($data->site->private);
1347 $data->site->inviteonly = poco_to_boolean($data->site->inviteonly);
1349 if (!$data->site->closed AND !$data->site->private and $data->site->inviteonly)
1350 $register_policy = REGISTER_APPROVE;
1351 elseif (!$data->site->closed AND !$data->site->private)
1352 $register_policy = REGISTER_OPEN;
1354 $register_policy = REGISTER_CLOSED;
1360 // Query statistics.json. Optional package for Diaspora, Friendica and Redmatrix
1362 $serverret = z_fetch_url($server_url."/statistics.json");
1363 if ($serverret["success"]) {
1364 $data = json_decode($serverret["body"]);
1365 if (isset($data->version)) {
1366 $version = $data->version;
1367 // Version numbers on statistics.json are presented with additional info, e.g.:
1368 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
1369 $version = preg_replace("=(.+)-(.{4,})=ism", "$1", $version);
1372 $site_name = $data->name;
1374 if (isset($data->network)) {
1375 $platform = $data->network;
1378 if ($platform == "Diaspora") {
1379 $network = NETWORK_DIASPORA;
1382 if ($data->registrations_open) {
1383 $register_policy = REGISTER_OPEN;
1385 $register_policy = REGISTER_CLOSED;
1388 if (isset($data->version))
1389 $last_contact = datetime_convert();
1393 // Query nodeinfo. Working for (at least) Diaspora and Friendica.
1395 $server = poco_fetch_nodeinfo($server_url);
1397 $register_policy = $server['register_policy'];
1399 if (isset($server['platform'])) {
1400 $platform = $server['platform'];
1403 if (isset($server['network'])) {
1404 $network = $server['network'];
1407 if (isset($server['version'])) {
1408 $version = $server['version'];
1411 if (isset($server['site_name'])) {
1412 $site_name = $server['site_name'];
1415 $last_contact = datetime_convert();
1419 // Check for noscrape
1420 // Friendica servers could be detected as OStatus servers
1421 if (!$failure AND in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS))) {
1422 $serverret = z_fetch_url($server_url."/friendica/json");
1424 if (!$serverret["success"])
1425 $serverret = z_fetch_url($server_url."/friendika/json");
1427 if ($serverret["success"]) {
1428 $data = json_decode($serverret["body"]);
1430 if (isset($data->version)) {
1431 $last_contact = datetime_convert();
1432 $network = NETWORK_DFRN;
1434 $noscrape = $data->no_scrape_url;
1435 $version = $data->version;
1436 $site_name = $data->site_name;
1437 $info = $data->info;
1438 $register_policy_str = $data->register_policy;
1439 $platform = $data->platform;
1441 switch ($register_policy_str) {
1442 case "REGISTER_CLOSED":
1443 $register_policy = REGISTER_CLOSED;
1445 case "REGISTER_APPROVE":
1446 $register_policy = REGISTER_APPROVE;
1448 case "REGISTER_OPEN":
1449 $register_policy = REGISTER_OPEN;
1456 if ($possible_failure AND !$failure) {
1457 $last_failure = datetime_convert();
1462 $last_contact = $orig_last_contact;
1464 $last_failure = $orig_last_failure;
1467 if (($last_contact <= $last_failure) AND !$failure) {
1468 logger("Server ".$server_url." seems to be alive, but last contact wasn't set - could be a bug", LOGGER_DEBUG);
1469 } else if (($last_contact >= $last_failure) AND $failure) {
1470 logger("Server ".$server_url." seems to be dead, but last failure wasn't set - could be a bug", LOGGER_DEBUG);
1473 // Check again if the server exists
1474 $servers = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
1476 $version = strip_tags($version);
1477 $site_name = strip_tags($site_name);
1478 $info = strip_tags($info);
1479 $platform = strip_tags($platform);
1482 q("UPDATE `gserver` SET `url` = '%s', `version` = '%s', `site_name` = '%s', `info` = '%s', `register_policy` = %d, `poco` = '%s', `noscrape` = '%s',
1483 `network` = '%s', `platform` = '%s', `last_contact` = '%s', `last_failure` = '%s' WHERE `nurl` = '%s'",
1488 intval($register_policy),
1493 dbesc($last_contact),
1494 dbesc($last_failure),
1495 dbesc(normalise_link($server_url))
1497 } elseif (!$failure) {
1498 q("INSERT INTO `gserver` (`url`, `nurl`, `version`, `site_name`, `info`, `register_policy`, `poco`, `noscrape`, `network`, `platform`, `created`, `last_contact`, `last_failure`)
1499 VALUES ('%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
1501 dbesc(normalise_link($server_url)),
1505 intval($register_policy),
1510 dbesc(datetime_convert()),
1511 dbesc($last_contact),
1512 dbesc($last_failure),
1513 dbesc(datetime_convert())
1516 logger("End discovery for server ".$server_url, LOGGER_DEBUG);
1521 function count_common_friends($uid,$cid) {
1523 $r = q("SELECT count(*) as `total`
1524 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1525 WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND
1526 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
1527 AND `gcontact`.`nurl` IN (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
1534 // logger("count_common_friends: $uid $cid {$r[0]['total']}");
1535 if (dbm::is_result($r))
1536 return $r[0]['total'];
1542 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
1545 $sql_extra = " order by rand() ";
1547 $sql_extra = " order by `gcontact`.`name` asc ";
1549 $r = q("SELECT `gcontact`.*, `contact`.`id` AS `cid`
1551 INNER JOIN `gcontact` ON `glink`.`gcid` = `gcontact`.`id`
1552 INNER JOIN `contact` ON `gcontact`.`nurl` = `contact`.`nurl`
1553 WHERE `glink`.`cid` = %d and `glink`.`uid` = %d
1554 AND `contact`.`uid` = %d AND `contact`.`self` = 0 AND `contact`.`blocked` = 0
1555 AND `contact`.`hidden` = 0 AND `contact`.`id` != %d
1556 AND ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
1557 $sql_extra LIMIT %d, %d",
1571 function count_common_friends_zcid($uid,$zcid) {
1573 $r = q("SELECT count(*) as `total`
1574 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1575 where `glink`.`zcid` = %d
1576 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
1581 if (dbm::is_result($r))
1582 return $r[0]['total'];
1587 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
1590 $sql_extra = " order by rand() ";
1592 $sql_extra = " order by `gcontact`.`name` asc ";
1594 $r = q("SELECT `gcontact`.*
1595 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1596 where `glink`.`zcid` = %d
1597 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
1598 $sql_extra limit %d, %d",
1610 function count_all_friends($uid,$cid) {
1612 $r = q("SELECT count(*) as `total`
1613 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1614 where `glink`.`cid` = %d and `glink`.`uid` = %d AND
1615 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))",
1620 if (dbm::is_result($r))
1621 return $r[0]['total'];
1627 function all_friends($uid,$cid,$start = 0, $limit = 80) {
1629 $r = q("SELECT `gcontact`.*, `contact`.`id` AS `cid`
1631 INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1632 LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl` AND `contact`.`uid` = %d
1633 WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND
1634 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
1635 ORDER BY `gcontact`.`name` ASC LIMIT %d, %d ",
1648 function suggestion_query($uid, $start = 0, $limit = 80) {
1654 // Uncommented because the result of the queries are to big to store it in the cache.
1655 // We need to decide if we want to change the db column type or if we want to delete it.
1656 // $list = Cache::get("suggestion_query:".$uid.":".$start.":".$limit);
1657 // if (!is_null($list)) {
1661 $network = array(NETWORK_DFRN);
1663 if (get_config('system','diaspora_enabled'))
1664 $network[] = NETWORK_DIASPORA;
1666 if (!get_config('system','ostatus_disabled'))
1667 $network[] = NETWORK_OSTATUS;
1669 $sql_network = implode("', '", $network);
1670 $sql_network = "'".$sql_network."'";
1672 /// @todo This query is really slow
1673 // By now we cache the data for five minutes
1674 $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
1675 INNER JOIN `glink` ON `glink`.`gcid` = `gcontact`.`id`
1676 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
1677 AND NOT `gcontact`.`name` IN (SELECT `name` FROM `contact` WHERE `uid` = %d)
1678 AND NOT `gcontact`.`id` IN (SELECT `gcid` FROM `gcign` WHERE `uid` = %d)
1679 AND `gcontact`.`updated` >= '%s'
1680 AND `gcontact`.`last_contact` >= `gcontact`.`last_failure`
1681 AND `gcontact`.`network` IN (%s)
1682 GROUP BY `glink`.`gcid` ORDER BY `gcontact`.`updated` DESC,`total` DESC LIMIT %d, %d",
1693 if (dbm::is_result($r) && count($r) >= ($limit -1)) {
1694 // Uncommented because the result of the queries are to big to store it in the cache.
1695 // We need to decide if we want to change the db column type or if we want to delete it.
1696 // Cache::set("suggestion_query:".$uid.":".$start.":".$limit, $r, CACHE_FIVE_MINUTES);
1701 $r2 = q("SELECT gcontact.* FROM gcontact
1702 INNER JOIN `glink` ON `glink`.`gcid` = `gcontact`.`id`
1703 WHERE `glink`.`uid` = 0 AND `glink`.`cid` = 0 AND `glink`.`zcid` = 0 AND NOT `gcontact`.`nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = %d)
1704 AND NOT `gcontact`.`name` IN (SELECT `name` FROM `contact` WHERE `uid` = %d)
1705 AND NOT `gcontact`.`id` IN (SELECT `gcid` FROM `gcign` WHERE `uid` = %d)
1706 AND `gcontact`.`updated` >= '%s'
1707 AND `gcontact`.`last_contact` >= `gcontact`.`last_failure`
1708 AND `gcontact`.`network` IN (%s)
1709 ORDER BY rand() LIMIT %d, %d",
1720 foreach ($r2 AS $suggestion)
1721 $list[$suggestion["nurl"]] = $suggestion;
1723 foreach ($r AS $suggestion)
1724 $list[$suggestion["nurl"]] = $suggestion;
1726 while (sizeof($list) > ($limit))
1729 // Uncommented because the result of the queries are to big to store it in the cache.
1730 // We need to decide if we want to change the db column type or if we want to delete it.
1731 // Cache::set("suggestion_query:".$uid.":".$start.":".$limit, $list, CACHE_FIVE_MINUTES);
1735 function update_suggestions() {
1741 /// @TODO Check if it is really neccessary to poll the own server
1742 poco_load(0,0,0,App::get_baseurl() . '/poco');
1744 $done[] = App::get_baseurl() . '/poco';
1746 if (strlen(get_config('system','directory'))) {
1747 $x = fetch_url(get_server()."/pubsites");
1749 $j = json_decode($x);
1751 foreach ($j->entries as $entry) {
1753 poco_check_server($entry->url);
1755 $url = $entry->url . '/poco';
1756 if (! in_array($url,$done)) {
1757 poco_load(0,0,0,$entry->url . '/poco');
1764 // Query your contacts from Friendica and Redmatrix/Hubzilla for their contacts
1765 $r = q("SELECT DISTINCT(`poco`) AS `poco` FROM `contact` WHERE `network` IN ('%s', '%s')",
1766 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA)
1769 if (dbm::is_result($r)) {
1770 foreach ($r as $rr) {
1771 $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
1772 if(! in_array($base,$done))
1773 poco_load(0,0,0,$base);
1779 * @brief Fetch server list from remote servers and adds them when they are new.
1781 * @param string $poco URL to the POCO endpoint
1783 function poco_fetch_serverlist($poco) {
1784 $serverret = z_fetch_url($poco."/@server");
1785 if (!$serverret["success"]) {
1788 $serverlist = json_decode($serverret['body']);
1790 if (!is_array($serverlist)) {
1794 foreach ($serverlist AS $server) {
1795 $server_url = str_replace("/index.php", "", $server->url);
1797 $r = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
1798 if (!dbm::is_result($r)) {
1799 logger("Call server check for server ".$server_url, LOGGER_DEBUG);
1800 proc_run(PRIORITY_LOW, "include/discover_poco.php", "server", base64_encode($server_url));
1805 function poco_discover_federation() {
1806 $last = get_config('poco','last_federation_discovery');
1809 $next = $last + (24 * 60 * 60);
1814 // Discover Friendica, Hubzilla and Diaspora servers
1815 $serverdata = fetch_url("http://the-federation.info/pods.json");
1818 $servers = json_decode($serverdata);
1820 foreach ($servers->pods AS $server) {
1821 proc_run(PRIORITY_LOW, "include/discover_poco.php", "server", base64_encode("https://".$server->host));
1825 // Currently disabled, since the service isn't available anymore.
1826 // It is not removed since I hope that there will be a successor.
1827 // Discover GNU Social Servers.
1828 //if (!get_config('system','ostatus_disabled')) {
1829 // $serverdata = "http://gstools.org/api/get_open_instances/";
1831 // $result = z_fetch_url($serverdata);
1832 // if ($result["success"]) {
1833 // $servers = json_decode($result["body"]);
1835 // foreach($servers->data AS $server)
1836 // poco_check_server($server->instance_address);
1840 set_config('poco','last_federation_discovery', time());
1843 function poco_discover_single_server($id) {
1844 $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `id` = %d", intval($id));
1845 if (!dbm::is_result($r)) {
1851 // Discover new servers out there (Works from Friendica version 3.5.2)
1852 poco_fetch_serverlist($server["poco"]);
1854 // Fetch all users from the other server
1855 $url = $server["poco"]."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1857 logger("Fetch all users from the server ".$server["url"], LOGGER_DEBUG);
1859 $retdata = z_fetch_url($url);
1860 if ($retdata["success"]) {
1861 $data = json_decode($retdata["body"]);
1863 poco_discover_server($data, 2);
1865 if (get_config('system','poco_discovery') > 1) {
1867 $timeframe = get_config('system','poco_discovery_since');
1868 if ($timeframe == 0) {
1872 $updatedSince = date("Y-m-d H:i:s", time() - $timeframe * 86400);
1874 // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3)
1875 $url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1879 $retdata = z_fetch_url($url);
1880 if ($retdata["success"]) {
1881 logger("Fetch all global contacts from the server ".$server["nurl"], LOGGER_DEBUG);
1882 $success = poco_discover_server(json_decode($retdata["body"]));
1885 if (!$success AND (get_config('system','poco_discovery') > 2)) {
1886 logger("Fetch contacts from users of the server ".$server["nurl"], LOGGER_DEBUG);
1887 poco_discover_server_users($data, $server);
1891 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
1895 // If the server hadn't replied correctly, then force a sanity check
1896 poco_check_server($server["url"], $server["network"], true);
1898 // If we couldn't reach the server, we will try it some time later
1899 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
1905 function poco_discover($complete = false) {
1907 // Update the server list
1908 poco_discover_federation();
1912 $requery_days = intval(get_config("system", "poco_requery_days"));
1914 if ($requery_days == 0) {
1917 $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
1919 $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));
1920 if (dbm::is_result($r)) {
1921 foreach ($r AS $server) {
1923 if (!poco_check_server($server["url"], $server["network"])) {
1924 // The server is not reachable? Okay, then we will try it later
1925 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
1929 logger('Update directory from server '.$server['url'].' with ID '.$server['id'], LOGGER_DEBUG);
1930 proc_run(PRIORITY_LOW, "include/discover_poco.php", "update_server_directory", intval($server['id']));
1932 if (!$complete AND (--$no_of_queries == 0)) {
1939 function poco_discover_server_users($data, $server) {
1941 if (!isset($data->entry))
1944 foreach ($data->entry AS $entry) {
1946 if (isset($entry->urls)) {
1947 foreach($entry->urls as $url)
1948 if ($url->type == 'profile') {
1949 $profile_url = $url->value;
1950 $urlparts = parse_url($profile_url);
1951 $username = end(explode("/", $urlparts["path"]));
1954 if ($username != "") {
1955 logger("Fetch contacts for the user ".$username." from the server ".$server["nurl"], LOGGER_DEBUG);
1957 // Fetch all contacts from a given user from the other server
1958 $url = $server["poco"]."/".$username."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1960 $retdata = z_fetch_url($url);
1961 if ($retdata["success"])
1962 poco_discover_server(json_decode($retdata["body"]), 3);
1967 function poco_discover_server($data, $default_generation = 0) {
1969 if (!isset($data->entry) OR !count($data->entry))
1974 foreach ($data->entry AS $entry) {
1976 $profile_photo = '';
1980 $updated = NULL_DATE;
1986 $generation = $default_generation;
1988 $name = $entry->displayName;
1990 if (isset($entry->urls)) {
1991 foreach($entry->urls as $url) {
1992 if ($url->type == 'profile') {
1993 $profile_url = $url->value;
1996 if ($url->type == 'webfinger') {
1997 $connect_url = str_replace('acct:' , '', $url->value);
2003 if (isset($entry->photos)) {
2004 foreach ($entry->photos as $photo) {
2005 if ($photo->type == 'profile') {
2006 $profile_photo = $photo->value;
2012 if (isset($entry->updated)) {
2013 $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
2016 if(isset($entry->network)) {
2017 $network = $entry->network;
2020 if(isset($entry->currentLocation)) {
2021 $location = $entry->currentLocation;
2024 if(isset($entry->aboutMe)) {
2025 $about = html2bbcode($entry->aboutMe);
2028 if(isset($entry->gender)) {
2029 $gender = $entry->gender;
2032 if(isset($entry->generation) AND ($entry->generation > 0)) {
2033 $generation = ++$entry->generation;
2036 if(isset($entry->contactType) AND ($entry->contactType >= 0)) {
2037 $contact_type = $entry->contactType;
2040 if(isset($entry->tags)) {
2041 foreach ($entry->tags as $tag) {
2042 $keywords = implode(", ", $tag);
2046 if ($generation > 0) {
2049 logger("Store profile ".$profile_url, LOGGER_DEBUG);
2051 $gcontact = array("url" => $profile_url,
2053 "network" => $network,
2054 "photo" => $profile_photo,
2056 "location" => $location,
2057 "gender" => $gender,
2058 "keywords" => $keywords,
2059 "connect" => $connect_url,
2060 "updated" => $updated,
2061 "contact-type" => $contact_type,
2062 "generation" => $generation);
2064 if (validate_gcontact($gcontact)) {
2065 update_gcontact($gcontact);
2068 logger("Done for profile ".$profile_url, LOGGER_DEBUG);
2075 * @brief Removes unwanted parts from a contact url
2077 * @param string $url Contact url
2078 * @return string Contact url with the wanted parts
2080 function clean_contact_url($url) {
2081 $parts = parse_url($url);
2083 if (!isset($parts["scheme"]) OR !isset($parts["host"]))
2086 $new_url = $parts["scheme"]."://".$parts["host"];
2088 if (isset($parts["port"]))
2089 $new_url .= ":".$parts["port"];
2091 if (isset($parts["path"]))
2092 $new_url .= $parts["path"];
2094 if ($new_url != $url)
2095 logger("Cleaned contact url ".$url." to ".$new_url." - Called by: ".App::callstack(), LOGGER_DEBUG);
2101 * @brief Replace alternate OStatus user format with the primary one
2103 * @param arr $contact contact array (called by reference)
2105 function fix_alternate_contact_address(&$contact) {
2106 if (($contact["network"] == NETWORK_OSTATUS) AND poco_alternate_ostatus_url($contact["url"])) {
2107 $data = probe_url($contact["url"]);
2108 if ($contact["network"] == NETWORK_OSTATUS) {
2109 logger("Fix primary url from ".$contact["url"]." to ".$data["url"]." - Called by: ".App::callstack(), LOGGER_DEBUG);
2110 $contact["url"] = $data["url"];
2111 $contact["addr"] = $data["addr"];
2112 $contact["alias"] = $data["alias"];
2113 $contact["server_url"] = $data["baseurl"];
2119 * @brief Fetch the gcontact id, add an entry if not existed
2121 * @param arr $contact contact array
2122 * @return bool|int Returns false if not found, integer if contact was found
2124 function get_gcontact_id($contact) {
2129 if (in_array($contact["network"], array(NETWORK_PHANTOM))) {
2130 logger("Invalid network for contact url ".$contact["url"]." - Called by: ".App::callstack(), LOGGER_DEBUG);
2134 if ($contact["network"] == NETWORK_STATUSNET)
2135 $contact["network"] = NETWORK_OSTATUS;
2137 // All new contacts are hidden by default
2138 if (!isset($contact["hide"]))
2139 $contact["hide"] = true;
2141 // Replace alternate OStatus user format with the primary one
2142 fix_alternate_contact_address($contact);
2144 // Remove unwanted parts from the contact url (e.g. "?zrl=...")
2145 if (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS)))
2146 $contact["url"] = clean_contact_url($contact["url"]);
2148 $r = q("SELECT `id`, `last_contact`, `last_failure`, `network` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 2",
2149 dbesc(normalise_link($contact["url"])));
2152 $gcontact_id = $r[0]["id"];
2154 // Update every 90 days
2155 if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) {
2156 $last_failure_str = $r[0]["last_failure"];
2157 $last_failure = strtotime($r[0]["last_failure"]);
2158 $last_contact_str = $r[0]["last_contact"];
2159 $last_contact = strtotime($r[0]["last_contact"]);
2160 $doprobing = (((time() - $last_contact) > (90 * 86400)) AND ((time() - $last_failure) > (90 * 86400)));
2163 q("INSERT INTO `gcontact` (`name`, `nick`, `addr` , `network`, `url`, `nurl`, `photo`, `created`, `updated`, `location`, `about`, `hide`, `generation`)
2164 VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d)",
2165 dbesc($contact["name"]),
2166 dbesc($contact["nick"]),
2167 dbesc($contact["addr"]),
2168 dbesc($contact["network"]),
2169 dbesc($contact["url"]),
2170 dbesc(normalise_link($contact["url"])),
2171 dbesc($contact["photo"]),
2172 dbesc(datetime_convert()),
2173 dbesc(datetime_convert()),
2174 dbesc($contact["location"]),
2175 dbesc($contact["about"]),
2176 intval($contact["hide"]),
2177 intval($contact["generation"])
2180 $r = q("SELECT `id`, `network` FROM `gcontact` WHERE `nurl` = '%s' ORDER BY `id` LIMIT 2",
2181 dbesc(normalise_link($contact["url"])));
2184 $gcontact_id = $r[0]["id"];
2186 $doprobing = in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""));
2191 logger("Last Contact: ". $last_contact_str." - Last Failure: ".$last_failure_str." - Checking: ".$contact["url"], LOGGER_DEBUG);
2192 proc_run(PRIORITY_LOW, 'include/gprobe.php', bin2hex($contact["url"]));
2195 if ((dbm::is_result($r)) AND (count($r) > 1) AND ($gcontact_id > 0) AND ($contact["url"] != ""))
2196 q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d",
2197 dbesc(normalise_link($contact["url"])),
2198 intval($gcontact_id));
2200 return $gcontact_id;
2204 * @brief Updates the gcontact table from a given array
2206 * @param arr $contact contact array
2207 * @return bool|int Returns false if not found, integer if contact was found
2209 function update_gcontact($contact) {
2211 // Check for invalid "contact-type" value
2212 if (isset($contact['contact-type']) AND (intval($contact['contact-type']) < 0)) {
2213 $contact['contact-type'] = 0;
2216 /// @todo update contact table as well
2218 $gcontact_id = get_gcontact_id($contact);
2223 $r = q("SELECT `name`, `nick`, `photo`, `location`, `about`, `addr`, `generation`, `birthday`, `gender`, `keywords`,
2224 `contact-type`, `hide`, `nsfw`, `network`, `alias`, `notify`, `server_url`, `connect`, `updated`, `url`
2225 FROM `gcontact` WHERE `id` = %d LIMIT 1",
2226 intval($gcontact_id));
2228 // Get all field names
2230 foreach ($r[0] AS $field => $data)
2231 $fields[$field] = $data;
2233 unset($fields["url"]);
2234 unset($fields["updated"]);
2235 unset($fields["hide"]);
2237 // Bugfix: We had an error in the storing of keywords which lead to the "0"
2238 // This value is still transmitted via poco.
2239 if ($contact["keywords"] == "0")
2240 unset($contact["keywords"]);
2242 if ($r[0]["keywords"] == "0")
2243 $r[0]["keywords"] = "";
2245 // assign all unassigned fields from the database entry
2246 foreach ($fields AS $field => $data)
2247 if (!isset($contact[$field]) OR ($contact[$field] == ""))
2248 $contact[$field] = $r[0][$field];
2250 if (!isset($contact["hide"]))
2251 $contact["hide"] = $r[0]["hide"];
2253 $fields["hide"] = $r[0]["hide"];
2255 if ($contact["network"] == NETWORK_STATUSNET)
2256 $contact["network"] = NETWORK_OSTATUS;
2258 // Replace alternate OStatus user format with the primary one
2259 fix_alternate_contact_address($contact);
2261 if (!isset($contact["updated"]))
2262 $contact["updated"] = datetime_convert();
2264 if ($contact["server_url"] == "") {
2265 $server_url = $contact["url"];
2267 $server_url = matching_url($server_url, $contact["alias"]);
2268 if ($server_url != "")
2269 $contact["server_url"] = $server_url;
2271 $server_url = matching_url($server_url, $contact["photo"]);
2272 if ($server_url != "")
2273 $contact["server_url"] = $server_url;
2275 $server_url = matching_url($server_url, $contact["notify"]);
2276 if ($server_url != "")
2277 $contact["server_url"] = $server_url;
2279 $contact["server_url"] = normalise_link($contact["server_url"]);
2281 if (($contact["addr"] == "") AND ($contact["server_url"] != "") AND ($contact["nick"] != "")) {
2282 $hostname = str_replace("http://", "", $contact["server_url"]);
2283 $contact["addr"] = $contact["nick"]."@".$hostname;
2286 // Check if any field changed
2288 unset($fields["generation"]);
2290 if ((($contact["generation"] > 0) AND ($contact["generation"] <= $r[0]["generation"])) OR ($r[0]["generation"] == 0)) {
2291 foreach ($fields AS $field => $data)
2292 if ($contact[$field] != $r[0][$field]) {
2293 logger("Difference for contact ".$contact["url"]." in field '".$field."'. New value: '".$contact[$field]."', old value '".$r[0][$field]."'", LOGGER_DEBUG);
2297 if ($contact["generation"] < $r[0]["generation"]) {
2298 logger("Difference for contact ".$contact["url"]." in field 'generation'. new value: '".$contact["generation"]."', old value '".$r[0]["generation"]."'", LOGGER_DEBUG);
2304 logger("Update gcontact for ".$contact["url"], LOGGER_DEBUG);
2306 q("UPDATE `gcontact` SET `photo` = '%s', `name` = '%s', `nick` = '%s', `addr` = '%s', `network` = '%s',
2307 `birthday` = '%s', `gender` = '%s', `keywords` = '%s', `hide` = %d, `nsfw` = %d,
2308 `contact-type` = %d, `alias` = '%s', `notify` = '%s', `url` = '%s',
2309 `location` = '%s', `about` = '%s', `generation` = %d, `updated` = '%s',
2310 `server_url` = '%s', `connect` = '%s'
2311 WHERE `nurl` = '%s' AND (`generation` = 0 OR `generation` >= %d)",
2312 dbesc($contact["photo"]), dbesc($contact["name"]), dbesc($contact["nick"]),
2313 dbesc($contact["addr"]), dbesc($contact["network"]), dbesc($contact["birthday"]),
2314 dbesc($contact["gender"]), dbesc($contact["keywords"]), intval($contact["hide"]),
2315 intval($contact["nsfw"]), intval($contact["contact-type"]), dbesc($contact["alias"]),
2316 dbesc($contact["notify"]), dbesc($contact["url"]), dbesc($contact["location"]),
2317 dbesc($contact["about"]), intval($contact["generation"]), dbesc($contact["updated"]),
2318 dbesc($contact["server_url"]), dbesc($contact["connect"]),
2319 dbesc(normalise_link($contact["url"])), intval($contact["generation"]));
2322 // Now update the contact entry with the user id "0" as well.
2323 // This is used for the shadow copies of public items.
2324 $r = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = 0 ORDER BY `id` LIMIT 1",
2325 dbesc(normalise_link($contact["url"])));
2328 logger("Update shadow contact ".$r[0]["id"], LOGGER_DEBUG);
2330 update_contact_avatar($contact["photo"], 0, $r[0]["id"]);
2332 q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `addr` = '%s',
2333 `network` = '%s', `bd` = '%s', `gender` = '%s',
2334 `keywords` = '%s', `alias` = '%s', `contact-type` = %d,
2335 `url` = '%s', `location` = '%s', `about` = '%s'
2337 dbesc($contact["name"]), dbesc($contact["nick"]), dbesc($contact["addr"]),
2338 dbesc($contact["network"]), dbesc($contact["birthday"]), dbesc($contact["gender"]),
2339 dbesc($contact["keywords"]), dbesc($contact["alias"]), intval($contact["contact-type"]),
2340 dbesc($contact["url"]), dbesc($contact["location"]), dbesc($contact["about"]),
2341 intval($r[0]["id"]));
2345 return $gcontact_id;
2349 * @brief Updates the gcontact entry from probe
2351 * @param str $url profile link
2353 function update_gcontact_from_probe($url) {
2354 $data = probe_url($url);
2356 if (in_array($data["network"], array(NETWORK_PHANTOM))) {
2357 logger("Invalid network for contact url ".$data["url"]." - Called by: ".App::callstack(), LOGGER_DEBUG);
2361 update_gcontact($data);
2365 * @brief Update the gcontact entry for a given user id
2367 * @param int $uid User ID
2369 function update_gcontact_for_user($uid) {
2370 $r = q("SELECT `profile`.`locality`, `profile`.`region`, `profile`.`country-name`,
2371 `profile`.`name`, `profile`.`about`, `profile`.`gender`,
2372 `profile`.`pub_keywords`, `profile`.`dob`, `profile`.`photo`,
2373 `profile`.`net-publish`, `user`.`nickname`, `user`.`hidewall`,
2374 `contact`.`notify`, `contact`.`url`, `contact`.`addr`
2376 INNER JOIN `user` ON `user`.`uid` = `profile`.`uid`
2377 INNER JOIN `contact` ON `contact`.`uid` = `profile`.`uid`
2378 WHERE `profile`.`uid` = %d AND `profile`.`is-default` AND `contact`.`self`",
2381 $location = formatted_location(array("locality" => $r[0]["locality"], "region" => $r[0]["region"],
2382 "country-name" => $r[0]["country-name"]));
2384 // The "addr" field was added in 3.4.3 so it can be empty for older users
2385 if ($r[0]["addr"] != "")
2386 $addr = $r[0]["nickname"].'@'.str_replace(array("http://", "https://"), "", App::get_baseurl());
2388 $addr = $r[0]["addr"];
2390 $gcontact = array("name" => $r[0]["name"], "location" => $location, "about" => $r[0]["about"],
2391 "gender" => $r[0]["gender"], "keywords" => $r[0]["pub_keywords"],
2392 "birthday" => $r[0]["dob"], "photo" => $r[0]["photo"],
2393 "notify" => $r[0]["notify"], "url" => $r[0]["url"],
2394 "hide" => ($r[0]["hidewall"] OR !$r[0]["net-publish"]),
2395 "nick" => $r[0]["nickname"], "addr" => $addr,
2396 "connect" => $addr, "server_url" => App::get_baseurl(),
2397 "generation" => 1, "network" => NETWORK_DFRN);
2399 update_gcontact($gcontact);
2403 * @brief Fetches users of given GNU Social server
2405 * If the "Statistics" plugin is enabled (See http://gstools.org/ for details) we query user data with this.
2407 * @param str $server Server address
2409 function gs_fetch_users($server) {
2411 logger("Fetching users from GNU Social server ".$server, LOGGER_DEBUG);
2413 $url = $server."/main/statistics";
2415 $result = z_fetch_url($url);
2416 if (!$result["success"])
2419 $statistics = json_decode($result["body"]);
2421 if (is_object($statistics->config)) {
2422 if ($statistics->config->instance_with_ssl)
2423 $server = "https://";
2425 $server = "http://";
2427 $server .= $statistics->config->instance_address;
2429 $hostname = $statistics->config->instance_address;
2431 if ($statistics->instance_with_ssl)
2432 $server = "https://";
2434 $server = "http://";
2436 $server .= $statistics->instance_address;
2438 $hostname = $statistics->instance_address;
2441 if (is_object($statistics->users))
2442 foreach ($statistics->users AS $nick => $user) {
2443 $profile_url = $server."/".$user->nickname;
2445 $contact = array("url" => $profile_url,
2446 "name" => $user->fullname,
2447 "addr" => $user->nickname."@".$hostname,
2448 "nick" => $user->nickname,
2449 "about" => $user->bio,
2450 "network" => NETWORK_OSTATUS,
2451 "photo" => App::get_baseurl()."/images/person-175.jpg");
2452 get_gcontact_id($contact);
2457 * @brief Asking GNU Social server on a regular base for their user data
2460 function gs_discover() {
2462 $requery_days = intval(get_config("system", "poco_requery_days"));
2464 $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
2466 $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",
2467 dbesc(NETWORK_OSTATUS), dbesc($last_update));
2472 foreach ($r AS $server) {
2473 gs_fetch_users($server["url"]);
2474 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
2479 * @brief Returns a list of all known servers
2480 * @return array List of server urls
2482 function poco_serverlist() {
2483 $r = q("SELECT `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver`
2484 WHERE `network` IN ('%s', '%s', '%s') AND `last_contact` > `last_failure`
2485 ORDER BY `last_contact`
2487 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
2488 if (!dbm::is_result($r)) {