]> git.mxchange.org Git - friendica.git/blob - include/socgraph.php
3d72286ea83ab81d5944ab72df62dae910a39dbd
[friendica.git] / include / socgraph.php
1 <?php
2 /**
3  * @file include/socgraph.php
4  * 
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
8  */
9
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");
16
17 /**
18  * @brief Fetch POCO data
19  *
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
24  *
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
29  *
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.
33  *
34  */
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));
38 }
39
40 /**
41  * @brief Fetch POCO data from the worker
42  *
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
47  *
48  */
49 function poco_load_worker($cid, $uid, $zcid, $url) {
50         $a = get_app();
51
52         if($cid) {
53                 if((! $url) || (! $uid)) {
54                         $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
55                                 intval($cid)
56                         );
57                         if (dbm::is_result($r)) {
58                                 $url = $r[0]['poco'];
59                                 $uid = $r[0]['uid'];
60                         }
61                 }
62                 if(! $uid)
63                         return;
64         }
65
66         if(! $url)
67                 return;
68
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') ;
70
71         logger('poco_load: ' . $url, LOGGER_DEBUG);
72
73         $s = fetch_url($url);
74
75         logger('poco_load: returns ' . $s, LOGGER_DATA);
76
77         logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
78
79         if(($a->get_curl_code() > 299) || (! $s))
80                 return;
81
82         $j = json_decode($s);
83
84         logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
85
86         if(! isset($j->entry))
87                 return;
88
89         $total = 0;
90         foreach($j->entry as $entry) {
91
92                 $total ++;
93                 $profile_url = '';
94                 $profile_photo = '';
95                 $connect_url = '';
96                 $name = '';
97                 $network = '';
98                 $updated = NULL_DATE;
99                 $location = '';
100                 $about = '';
101                 $keywords = '';
102                 $gender = '';
103                 $contact_type = -1;
104                 $generation = 0;
105
106                 $name = $entry->displayName;
107
108                 if (isset($entry->urls)) {
109                         foreach ($entry->urls as $url) {
110                                 if ($url->type == 'profile') {
111                                         $profile_url = $url->value;
112                                         continue;
113                                 }
114                                 if ($url->type == 'webfinger') {
115                                         $connect_url = str_replace('acct:' , '', $url->value);
116                                         continue;
117                                 }
118                         }
119                 }
120                 if (isset($entry->photos)) {
121                         foreach ($entry->photos as $photo) {
122                                 if ($photo->type == 'profile') {
123                                         $profile_photo = $photo->value;
124                                         continue;
125                                 }
126                         }
127                 }
128
129                 if (isset($entry->updated)) {
130                         $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
131                 }
132
133                 if (isset($entry->network)) {
134                         $network = $entry->network;
135                 }
136
137                 if (isset($entry->currentLocation)) {
138                         $location = $entry->currentLocation;
139                 }
140
141                 if (isset($entry->aboutMe)) {
142                         $about = html2bbcode($entry->aboutMe);
143                 }
144
145                 if (isset($entry->gender)) {
146                         $gender = $entry->gender;
147                 }
148
149                 if (isset($entry->generation) AND ($entry->generation > 0)) {
150                         $generation = ++$entry->generation;
151                 }
152
153                 if (isset($entry->tags)) {
154                         foreach($entry->tags as $tag) {
155                                 $keywords = implode(", ", $tag);
156                         }
157                 }
158
159                 if (isset($entry->contactType) AND ($entry->contactType >= 0))
160                         $contact_type = $entry->contactType;
161
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
164                 //if ($uid == 0)
165                 //      $network = NETWORK_DFRN;
166
167                 poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid, $uid, $zcid);
168
169                 $gcontact = array("url" => $profile_url, "contact-type" => $contact_type, "generation" => $generation);
170                 update_gcontact($gcontact);
171
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'",
177                 //              dbesc($location),
178                 //              dbesc($about),
179                 //              dbesc($keywords),
180                 //              dbesc($gender),
181                 //              dbesc(normalise_link($profile_url)),
182                 //              dbesc(NETWORK_DFRN));
183         }
184         logger("poco_load: loaded $total entries",LOGGER_DEBUG);
185
186         q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
187                 intval($cid),
188                 intval($uid),
189                 intval($zcid)
190         );
191
192 }
193 /**
194  * @brief Validate the given gcontact data
195  *
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
200  *
201  * Generation:
202  *  0: No definition
203  *  1: Profiles on this server
204  *  2: Contacts of profiles on this server
205  *  3: Contacts of contacts of profiles on this server
206  *  4: ...
207  *
208  */
209 function validate_gcontact(&$gcontact) {
210
211         if ($gcontact['url'] == "") {
212                 return false;
213         }
214
215         $urlparts = parse_url($gcontact['url']);
216         if (!isset($urlparts["scheme"])) {
217                 return false;
218         }
219
220         if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com",
221                                                 "identi.ca", "alpha.app.net"))) {
222                 return false;
223         }
224
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'] = "";
229         }
230
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']);
234         }
235
236         $alternate = poco_alternate_ostatus_url($gcontact['url']);
237
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'] = "";
241         }
242
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)
246                 );
247                 if (dbm::is_result($r)) {
248                         $gcontact['network'] = $r[0]["network"];
249                 }
250
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)
254                         );
255                         if (dbm::is_result($r)) {
256                                 $gcontact['network'] = $r[0]["network"];
257                         }
258                 }
259         }
260
261         $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
262                 dbesc(normalise_link($gcontact['url']))
263         );
264
265         if (count($x)) {
266                 if (!isset($gcontact['network']) AND ($x[0]["network"] != NETWORK_STATUSNET)) {
267                         $gcontact['network'] = $x[0]["network"];
268                 }
269                 if ($gcontact['updated'] <= NULL_DATE) {
270                         $gcontact['updated'] = $x[0]["updated"];
271                 }
272                 if (!isset($gcontact['server_url'])) {
273                         $gcontact['server_url'] = $x[0]["server_url"];
274                 }
275                 if (!isset($gcontact['addr'])) {
276                         $gcontact['addr'] = $x[0]["addr"];
277                 }
278         } else {
279                 if (!isset($gcontact['server_url'])) {
280                         $gcontact['server_url'] = '';
281                 }
282                 if (!isset($gcontact['network'])) {
283                         $gcontact['network'] = '';
284                 }
285         }
286
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']);
290
291                 if ($data["network"] == NETWORK_PHANTOM) {
292                         return false;
293                 }
294
295                 $orig_profile = $gcontact['url'];
296
297                 $gcontact["server_url"] = $data["baseurl"];
298
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"]);
308
309                 $gcontact = array_merge($gcontact, $data);
310
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)));
314                         if ($r) {
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"]));
317                         }
318                 }
319         }
320
321         if (!isset($gcontact['name']) OR !isset($gcontact['photo'])) {
322                 return false;
323         }
324
325         if (!in_array($gcontact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA))) {
326                 return false;
327         }
328
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']);
332
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;
336                 }
337         }
338
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'] = "";
342         }
343
344         return true;
345 }
346
347 function poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid = 0, $uid = 0, $zcid = 0) {
348
349         // Generation:
350         //  0: No definition
351         //  1: Profiles on this server
352         //  2: Contacts of profiles on this server
353         //  3: Contacts of contacts of profiles on this server
354         //  4: ...
355
356         $gcid = "";
357
358         if ($profile_url == "")
359                 return $gcid;
360
361         $urlparts = parse_url($profile_url);
362         if (!isset($urlparts["scheme"]))
363                 return $gcid;
364
365         if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com",
366                                                 "identi.ca", "alpha.app.net")))
367                 return $gcid;
368
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)
372                 $network = "";
373
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);
377
378         $alternate = poco_alternate_ostatus_url($profile_url);
379
380         $orig_updated = $updated;
381
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/"))) {
384                 $profile_photo = "";
385         }
386
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)
389         );
390         if (dbm::is_result($r)) {
391                 $network = $r[0]["network"];
392         }
393
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)
397                 );
398                 if (dbm::is_result($r)) {
399                         $network = $r[0]["network"];
400                         //$profile_url = $r[0]["url"];
401                 }
402         }
403
404         $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
405                 dbesc(normalise_link($profile_url))
406         );
407
408         if (count($x)) {
409                 if (($network == "") AND ($x[0]["network"] != NETWORK_STATUSNET)) {
410                         $network = $x[0]["network"];
411                 }
412                 if ($updated <= NULL_DATE) {
413                         $updated = $x[0]["updated"];
414                 }
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"];
421         } else {
422                 $created = NULL_DATE;
423                 $server_url = "";
424
425                 $urlparts = parse_url($profile_url);
426                 $nick = end(explode("/", $urlparts["path"]));
427                 $addr = "";
428                 $alias = "";
429                 $notify = "";
430         }
431
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);
435
436                 $orig_profile = $profile_url;
437
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"];
447
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)));
451                         if ($r) {
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"]));
454                         }
455
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);
458                 }
459         }
460
461         if ($alternate AND ($network == NETWORK_OSTATUS))
462                 return $gcid;
463
464         if (count($x) AND ($x[0]["network"] == "") AND ($network != "")) {
465                 q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
466                         dbesc($network),
467                         dbesc(normalise_link($profile_url))
468                 );
469         }
470
471         if (($name == "") OR ($profile_photo == ""))
472                 return $gcid;
473
474         if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
475                 return $gcid;
476
477         logger("profile-check generation: ".$generation." Network: ".$network." URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
478
479         // We check the server url to be sure that it is a real one
480         $server_url2 = poco_detect_server($profile_url);
481
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;
485         }
486
487         // The server URL doesn't seem to be valid, so we don't store it.
488         if (!poco_check_server($server_url, $network)) {
489                 $server_url = "";
490         }
491
492         $gcontact = array("url" => $profile_url,
493                         "addr" => $addr,
494                         "alias" => $alias,
495                         "name" => $name,
496                         "network" => $network,
497                         "photo" => $profile_photo,
498                         "about" => $about,
499                         "location" => $location,
500                         "gender" => $gender,
501                         "keywords" => $keywords,
502                         "server_url" => $server_url,
503                         "connect" => $connect_url,
504                         "notify" => $notify,
505                         "updated" => $updated,
506                         "generation" => $generation);
507
508         $gcid = update_gcontact($gcontact);
509
510         if(!$gcid)
511                 return $gcid;
512
513         link_gcontact($gcid, $uid, $cid, $zcid);
514
515         return $gcid;
516 }
517
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",
520                 intval($cid),
521                 intval($uid),
522                 intval($gcid),
523                 intval($zcid)
524         );
525         if (!dbm::is_result($r)) {
526                 q("INSERT INTO `glink` (`cid`, `uid`, `gcid`, `zcid`, `updated`) VALUES (%d, %d, %d, %d, '%s') ",
527                         intval($cid),
528                         intval($uid),
529                         intval($gcid),
530                         intval($zcid),
531                         dbesc(datetime_convert())
532                 );
533         } else {
534                 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
535                         dbesc(datetime_convert()),
536                         intval($cid),
537                         intval($uid),
538                         intval($gcid),
539                         intval($zcid)
540                 );
541         }
542 }
543
544 function poco_reachable($profile, $server = "", $network = "", $force = false) {
545
546         if ($server == "")
547                 $server = poco_detect_server($profile);
548
549         if ($server == "")
550                 return true;
551
552         return poco_check_server($server, $network, $force);
553 }
554
555 function poco_detect_server($profile) {
556
557         // Try to detect the server path based upon some known standard paths
558         $server_url = "";
559
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;
565                 }
566         }
567
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;
573                 }
574         }
575
576         if ($server_url == "") {
577                 $red = preg_replace("=(https?://)(.*)/channel/(.*)=ism", "$1$2", $profile);
578                 if ($red != $profile) {
579                         $server_url = $red;
580                         $network = NETWORK_DIASPORA;
581                 }
582         }
583
584         // Mastodon
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;
590                 }
591         }
592
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;
599                 }
600         }
601
602         // Wild guess
603         if ($server_url == "") {
604                 $base = preg_replace("=(https?://)(.*?)/(.*)=ism", "$1$2", $profile);
605                 if ($base != $profile) {
606                         $server_url = $base;
607                         $network = NETWORK_PHANTOM;
608                 }
609         }
610
611         if ($server_url == "") {
612                 return "";
613         }
614
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)) {
618                 return $server_url;
619         }
620
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"]) {
624                 return "";
625         }
626
627         return $server_url;
628 }
629
630 function poco_alternate_ostatus_url($url) {
631         return(preg_match("=https?://.+/user/\d+=ism", $url, $matches));
632 }
633
634 function poco_last_updated($profile, $force = false) {
635
636         $gcontacts = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'",
637                         dbesc(normalise_link($profile)));
638
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)));
642         }
643         if ($gcontacts[0]["server_url"] != "") {
644                 $server_url = $gcontacts[0]["server_url"];
645         }
646         if (($server_url == '') OR ($gcontacts[0]["server_url"] == $gcontacts[0]["nurl"])) {
647                 $server_url = poco_detect_server($profile);
648         }
649
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);
652                 return false;
653         }
654
655         if ($server_url != "") {
656                 if (!poco_check_server($server_url, $gcontacts[0]["network"], $force)) {
657
658                         if ($force)
659                                 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
660                                         dbesc(datetime_convert()), dbesc(normalise_link($profile)));
661
662                         logger("Profile ".$profile.": Server ".$server_url." wasn't reachable.", LOGGER_DEBUG);
663                         return false;
664                 }
665
666                 q("UPDATE `gcontact` SET `server_url` = '%s' WHERE `nurl` = '%s'",
667                         dbesc($server_url), dbesc(normalise_link($profile)));
668         }
669
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)));
673
674                 if ($server)
675                         q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
676                                 dbesc($server[0]["network"]), dbesc(normalise_link($profile)));
677                 else
678                         return false;
679         }
680
681         // noscrape is really fast so we don't cache the call.
682         if (($gcontacts[0]["server_url"] != "") AND ($gcontacts[0]["nick"] != "")) {
683
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"])));
686
687                 if ($server) {
688                         $noscraperet = z_fetch_url($server[0]["noscrape"]."/".$gcontacts[0]["nick"]);
689
690                          if ($noscraperet["success"] AND ($noscraperet["body"] != "")) {
691
692                                 $noscrape = json_decode($noscraperet["body"], true);
693
694                                 if (is_array($noscrape)) {
695                                         $contact = array("url" => $profile,
696                                                         "network" => $server[0]["network"],
697                                                         "generation" => $gcontacts[0]["generation"]);
698
699                                         if (isset($noscrape["fn"]))
700                                                 $contact["name"] = $noscrape["fn"];
701
702                                         if (isset($noscrape["comm"]))
703                                                 $contact["community"] = $noscrape["comm"];
704
705                                         if (isset($noscrape["tags"])) {
706                                                 $keywords = implode(" ", $noscrape["tags"]);
707                                                 if ($keywords != "")
708                                                         $contact["keywords"] = $keywords;
709                                         }
710
711                                         $location = formatted_location($noscrape);
712                                         if ($location)
713                                                 $contact["location"] = $location;
714
715                                         if (isset($noscrape["dfrn-notify"]))
716                                                 $contact["notify"] = $noscrape["dfrn-notify"];
717
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"]);
732
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();
736
737                                         $contact = array_merge($contact, $noscrape);
738
739                                         update_gcontact($contact);
740
741                                         if (trim($noscrape["updated"]) != "") {
742                                                 q("UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'",
743                                                         dbesc(datetime_convert()), dbesc(normalise_link($profile)));
744
745                                                 logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG);
746
747                                                 return $noscrape["updated"];
748                                         }
749                                 }
750                         }
751                 }
752         }
753
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"];
758         }
759
760         $data = Probe::uri($profile);
761
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"]))) {
767
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"]));
771
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"]);
774
775                 poco_last_updated($data["url"], $force);
776
777                 logger("Profile ".$profile." was deleted", LOGGER_DEBUG);
778                 return false;
779         }
780
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)));
784
785                 logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG);
786                 return false;
787         }
788
789         $contact = array("generation" => $gcontacts[0]["generation"]);
790
791         $contact = array_merge($contact, $data);
792
793         $contact["server_url"] = $data["baseurl"];
794
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"]);
804
805         update_gcontact($contact);
806
807         $feedret = z_fetch_url($data["poll"]);
808
809         if (!$feedret["success"]) {
810                 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
811                         dbesc(datetime_convert()), dbesc(normalise_link($profile)));
812
813                 logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG);
814                 return false;
815         }
816
817         $doc = new DOMDocument();
818         @$doc->loadXML($feedret["body"]);
819
820         $xpath = new DomXPath($doc);
821         $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
822
823         $entries = $xpath->query('/atom:feed/atom:entry');
824
825         $last_updated = "";
826
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;
830
831                 if ($last_updated < $published)
832                         $last_updated = $published;
833
834                 if ($last_updated < $updated)
835                         $last_updated = $updated;
836         }
837
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;
842                 }
843         }
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)));
846
847         if (($gcontacts[0]["generation"] == 0))
848                 q("UPDATE `gcontact` SET `generation` = 9 WHERE `nurl` = '%s'",
849                         dbesc(normalise_link($profile)));
850
851         logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG);
852
853         return($last_updated);
854 }
855
856 function poco_do_update($created, $updated, $last_failure,  $last_contact) {
857         $now = strtotime(datetime_convert());
858
859         if ($updated > $last_contact)
860                 $contact_time = strtotime($updated);
861         else
862                 $contact_time = strtotime($last_contact);
863
864         $failure_time = strtotime($last_failure);
865         $created_time = strtotime($created);
866
867         // If there is no "created" time then use the current time
868         if ($created_time <= 0)
869                 $created_time = $now;
870
871         // If the last contact was less than 24 hours then don't update
872         if (($now - $contact_time) < (60 * 60 * 24))
873                 return false;
874
875         // If the last failure was less than 24 hours then don't update
876         if (($now - $failure_time) < (60 * 60 * 24))
877                 return false;
878
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))
881         //      return false;
882
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)))
885                 return false;
886
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)))
889                 return false;
890
891         return true;
892 }
893
894 function poco_to_boolean($val) {
895         if (($val == "true") OR ($val == 1))
896                 return(true);
897         if (($val == "false") OR ($val == 0))
898                 return(false);
899
900         return ($val);
901 }
902
903 /**
904  * @brief Detect server type (Hubzilla or Friendica) via the poco data
905  *
906  * @param object $data POCO data
907  * @return array Server data
908  */
909 function poco_detect_poco_data($data) {
910         $server = false;
911
912         if (!isset($data->entry)) {
913                 return false;
914         }
915
916         if (count($data->entry) == 0) {
917                 return false;
918         }
919
920         if (!isset($data->entry[0]->urls)) {
921                 return false;
922         }
923
924         if (count($data->entry[0]->urls) == 0) {
925                 return false;
926         }
927
928         foreach ($data->entry[0]->urls AS $url) {
929                 if ($url->type == 'zot') {
930                         $server = array();
931                         $server["platform"] = 'Hubzilla';
932                         $server["network"] = NETWORK_DIASPORA;
933                         return $server;
934                 }
935         }
936         return false;
937 }
938
939 /**
940  * @brief Detect server type by using the nodeinfo data
941  *
942  * @param string $server_url address of the server
943  * @return array Server data
944  */
945 function poco_fetch_nodeinfo($server_url) {
946         $serverret = z_fetch_url($server_url."/.well-known/nodeinfo");
947         if (!$serverret["success"]) {
948                 return false;
949         }
950
951         $nodeinfo = json_decode($serverret['body']);
952
953         if (!is_object($nodeinfo)) {
954                 return false;
955         }
956
957         if (!is_array($nodeinfo->links)) {
958                 return false;
959         }
960
961         $nodeinfo_url = '';
962
963         foreach ($nodeinfo->links AS $link) {
964                 if ($link->rel == 'http://nodeinfo.diaspora.software/ns/schema/1.0') {
965                         $nodeinfo_url = $link->href;
966                 }
967         }
968
969         if ($nodeinfo_url == '') {
970                 return false;
971         }
972
973         $serverret = z_fetch_url($nodeinfo_url);
974         if (!$serverret["success"]) {
975                 return false;
976         }
977
978         $nodeinfo = json_decode($serverret['body']);
979
980         if (!is_object($nodeinfo)) {
981                 return false;
982         }
983
984         $server = array();
985
986         $server['register_policy'] = REGISTER_CLOSED;
987
988         if (is_bool($nodeinfo->openRegistrations) AND $nodeinfo->openRegistrations) {
989                 $server['register_policy'] = REGISTER_OPEN;
990         }
991
992         if (is_object($nodeinfo->software)) {
993                 if (isset($nodeinfo->software->name)) {
994                         $server['platform'] = $nodeinfo->software->name;
995                 }
996
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']);
1002                 }
1003         }
1004
1005         if (is_object($nodeinfo->metadata)) {
1006                 if (isset($nodeinfo->metadata->nodeName)) {
1007                         $server['site_name'] = $nodeinfo->metadata->nodeName;
1008                 }
1009         }
1010
1011         $diaspora = false;
1012         $friendica = false;
1013         $gnusocial = false;
1014
1015         if (is_array($nodeinfo->protocols->inbound)) {
1016                 foreach ($nodeinfo->protocols->inbound AS $inbound) {
1017                         if ($inbound == 'diaspora') {
1018                                 $diaspora = true;
1019                         }
1020                         if ($inbound == 'friendica') {
1021                                 $friendica = true;
1022                         }
1023                         if ($inbound == 'gnusocial') {
1024                                 $gnusocial = true;
1025                         }
1026                 }
1027         }
1028
1029         if ($gnusocial) {
1030                 $server['network'] = NETWORK_OSTATUS;
1031         }
1032         if ($diaspora) {
1033                 $server['network'] = NETWORK_DIASPORA;
1034         }
1035         if ($friendica) {
1036                 $server['network'] = NETWORK_DFRN;
1037         }
1038
1039         if (!$server) {
1040                 return false;
1041         }
1042
1043         return $server;
1044 }
1045
1046 /**
1047  * @brief Detect server type (Hubzilla or Friendica) via the front page body
1048  *
1049  * @param string $body Front page of the server
1050  * @return array Server data
1051  */
1052 function poco_detect_server_type($body) {
1053         $server = false;
1054
1055         $doc = new \DOMDocument();
1056         @$doc->loadHTML($body);
1057         $xpath = new \DomXPath($doc);
1058
1059         $list = $xpath->query("//meta[@name]");
1060
1061         foreach ($list as $node) {
1062                 $attr = array();
1063                 if ($node->attributes->length) {
1064                         foreach ($node->attributes as $attribute) {
1065                                 $attr[$attribute->name] = $attribute->value;
1066                         }
1067                 }
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"))) {
1072                                         $server = array();
1073                                         $server["platform"] = $version_part[0];
1074                                         $server["version"] = $version_part[1];
1075                                         $server["network"] = NETWORK_DFRN;
1076                                 }
1077                         }
1078                 }
1079         }
1080
1081         if (!$server) {
1082                 $list = $xpath->query("//meta[@property]");
1083
1084                 foreach ($list as $node) {
1085                         $attr = array();
1086                         if ($node->attributes->length) {
1087                                 foreach ($node->attributes as $attribute) {
1088                                         $attr[$attribute->name] = $attribute->value;
1089                                 }
1090                         }
1091                         if ($attr['property'] == 'generator') {
1092                                 if (in_array($attr['content'], array("hubzilla", "BlaBlaNet"))) {
1093                                         $server = array();
1094                                         $server["platform"] = $attr['content'];
1095                                         $server["version"] = "";
1096                                         $server["network"] = NETWORK_DIASPORA;
1097                                 }
1098                         }
1099                 }
1100         }
1101
1102         if (!$server) {
1103                 return false;
1104         }
1105
1106         $server["site_name"] = $xpath->evaluate($element."//head/title/text()", $context)->item(0)->nodeValue;
1107         return $server;
1108 }
1109
1110 function poco_check_server($server_url, $network = "", $force = false) {
1111
1112         // Unify the server address
1113         $server_url = trim($server_url, "/");
1114         $server_url = str_replace("/index.php", "", $server_url);
1115
1116         if ($server_url == "")
1117                 return false;
1118
1119         $servers = q("SELECT * FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
1120         if (dbm::is_result($servers)) {
1121
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)));
1125                 }
1126                 $poco = $servers[0]["poco"];
1127                 $noscrape = $servers[0]["noscrape"];
1128
1129                 if ($network == "")
1130                         $network = $servers[0]["network"];
1131
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"];
1139
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);
1143                 }
1144         } else {
1145                 $poco = "";
1146                 $noscrape = "";
1147                 $version = "";
1148                 $platform = "";
1149                 $site_name = "";
1150                 $info = "";
1151                 $register_policy = -1;
1152
1153                 $last_contact = NULL_DATE;
1154                 $last_failure = NULL_DATE;
1155         }
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);
1157
1158         $failure = false;
1159         $possible_failure = false;
1160         $orig_last_failure = $last_failure;
1161         $orig_last_contact = $last_contact;
1162
1163         // Check if the page is accessible via SSL.
1164         $orig_server_url = $server_url;
1165         $server_url = str_replace("http://", "https://", $server_url);
1166
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));
1169
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);
1175                 return false;
1176         }
1177
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);
1182
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));
1185
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);
1189                         return false;
1190                 }
1191
1192                 $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
1193         }
1194
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();
1199                         $failure = true;
1200                 }
1201                 $possible_failure = true;
1202         } elseif ($network == NETWORK_DIASPORA)
1203                 $last_contact = datetime_convert();
1204
1205         // If the server has no possible failure we reset the cached data
1206         if (!$possible_failure) {
1207                 $version = "";
1208                 $platform = "";
1209                 $site_name = "";
1210                 $info = "";
1211                 $register_policy = -1;
1212         }
1213
1214         // Look for poco
1215         if (!$failure) {
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();
1222
1223                                 $server = poco_detect_poco_data($data);
1224                                 if ($server) {
1225                                         $platform = $server['platform'];
1226                                         $network = $server['network'];
1227                                         $version = '';
1228                                         $site_name = '';
1229                                 }
1230                         }
1231                 }
1232         }
1233
1234         if (!$failure) {
1235                 // Test for Diaspora, Hubzilla, Mastodon or older Friendica servers
1236                 $serverret = z_fetch_url($server_url);
1237
1238                 if (!$serverret["success"] OR ($serverret["body"] == "")) {
1239                         $last_failure = datetime_convert();
1240                         $failure = true;
1241                 } else {
1242                         $server = poco_detect_server_type($serverret["body"]);
1243                         if ($server) {
1244                                 $platform = $server['platform'];
1245                                 $network = $server['network'];
1246                                 $version = $server['version'];
1247                                 $site_name = $server['site_name'];
1248                                 $last_contact = datetime_convert();
1249                         }
1250
1251                         $lines = explode("\n",$serverret["header"]);
1252                         if(count($lines)) {
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();
1263                                         }
1264
1265                                         if(stristr($line,'Server: Mastodon')) {
1266                                                 $platform = "Mastodon";
1267                                                 $network = NETWORK_OSTATUS;
1268                                                 // Mastodon doesn't reveal version numbers
1269                                                 $version = "";
1270                                                 $last_contact = datetime_convert();
1271                                         }
1272                                 }
1273                         }
1274                 }
1275         }
1276
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();
1290                 }
1291
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();
1302                 }
1303         }
1304
1305         if (!$failure) {
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();
1312
1313                                 if (isset($data->site->platform)) {
1314                                         $platform = $data->site->platform->PLATFORM_NAME;
1315                                         $version = $data->site->platform->STD_VERSION;
1316                                         $network = NETWORK_DIASPORA;
1317                                 }
1318                                 if (isset($data->site->BlaBlaNet)) {
1319                                         $platform = $data->site->BlaBlaNet->PLATFORM_NAME;
1320                                         $version = $data->site->BlaBlaNet->STD_VERSION;
1321                                         $network = NETWORK_DIASPORA;
1322                                 }
1323                                 if (isset($data->site->hubzilla)) {
1324                                         $platform = $data->site->hubzilla->PLATFORM_NAME;
1325                                         $version = $data->site->hubzilla->RED_VERSION;
1326                                         $network = NETWORK_DIASPORA;
1327                                 }
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;
1333
1334                                         $version = $data->site->redmatrix->RED_VERSION;
1335                                         $network = NETWORK_DIASPORA;
1336                                 }
1337                                 if (isset($data->site->friendica)) {
1338                                         $platform = $data->site->friendica->FRIENDICA_PLATFORM;
1339                                         $version = $data->site->friendica->FRIENDICA_VERSION;
1340                                         $network = NETWORK_DFRN;
1341                                 }
1342
1343                                 $site_name = $data->site->name;
1344
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);
1348
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;
1353                                 else
1354                                         $register_policy = REGISTER_CLOSED;
1355                         }
1356                 }
1357         }
1358
1359
1360         // Query statistics.json. Optional package for Diaspora, Friendica and Redmatrix
1361         if (!$failure) {
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);
1370                         }
1371
1372                         $site_name = $data->name;
1373
1374                         if (isset($data->network)) {
1375                                 $platform = $data->network;
1376                         }
1377
1378                         if ($platform == "Diaspora") {
1379                                 $network = NETWORK_DIASPORA;
1380                         }
1381
1382                         if ($data->registrations_open) {
1383                                 $register_policy = REGISTER_OPEN;
1384                         } else {
1385                                 $register_policy = REGISTER_CLOSED;
1386                         }
1387
1388                         if (isset($data->version))
1389                                 $last_contact = datetime_convert();
1390                 }
1391         }
1392
1393         // Query nodeinfo. Working for (at least) Diaspora and Friendica.
1394         if (!$failure) {
1395                 $server = poco_fetch_nodeinfo($server_url);
1396                 if ($server) {
1397                         $register_policy = $server['register_policy'];
1398
1399                         if (isset($server['platform'])) {
1400                                 $platform = $server['platform'];
1401                         }
1402
1403                         if (isset($server['network'])) {
1404                                 $network = $server['network'];
1405                         }
1406
1407                         if (isset($server['version'])) {
1408                                 $version = $server['version'];
1409                         }
1410
1411                         if (isset($server['site_name'])) {
1412                                 $site_name = $server['site_name'];
1413                         }
1414
1415                         $last_contact = datetime_convert();
1416                 }
1417         }
1418
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");
1423
1424                 if (!$serverret["success"])
1425                         $serverret = z_fetch_url($server_url."/friendika/json");
1426
1427                 if ($serverret["success"]) {
1428                         $data = json_decode($serverret["body"]);
1429
1430                         if (isset($data->version)) {
1431                                 $last_contact = datetime_convert();
1432                                 $network = NETWORK_DFRN;
1433
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;
1440
1441                                 switch ($register_policy_str) {
1442                                         case "REGISTER_CLOSED":
1443                                                 $register_policy = REGISTER_CLOSED;
1444                                                 break;
1445                                         case "REGISTER_APPROVE":
1446                                                 $register_policy = REGISTER_APPROVE;
1447                                                 break;
1448                                         case "REGISTER_OPEN":
1449                                                 $register_policy = REGISTER_OPEN;
1450                                                 break;
1451                                 }
1452                         }
1453                 }
1454         }
1455
1456         if ($possible_failure AND !$failure) {
1457                 $last_failure = datetime_convert();
1458                 $failure = true;
1459         }
1460
1461         if ($failure) {
1462                 $last_contact = $orig_last_contact;
1463         } else {
1464                 $last_failure = $orig_last_failure;
1465         }
1466
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);
1471         }
1472
1473         // Check again if the server exists
1474         $servers = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
1475
1476         $version = strip_tags($version);
1477         $site_name = strip_tags($site_name);
1478         $info = strip_tags($info);
1479         $platform = strip_tags($platform);
1480
1481         if ($servers) {
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'",
1484                         dbesc($server_url),
1485                         dbesc($version),
1486                         dbesc($site_name),
1487                         dbesc($info),
1488                         intval($register_policy),
1489                         dbesc($poco),
1490                         dbesc($noscrape),
1491                         dbesc($network),
1492                         dbesc($platform),
1493                         dbesc($last_contact),
1494                         dbesc($last_failure),
1495                         dbesc(normalise_link($server_url))
1496                 );
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')",
1500                                 dbesc($server_url),
1501                                 dbesc(normalise_link($server_url)),
1502                                 dbesc($version),
1503                                 dbesc($site_name),
1504                                 dbesc($info),
1505                                 intval($register_policy),
1506                                 dbesc($poco),
1507                                 dbesc($noscrape),
1508                                 dbesc($network),
1509                                 dbesc($platform),
1510                                 dbesc(datetime_convert()),
1511                                 dbesc($last_contact),
1512                                 dbesc($last_failure),
1513                                 dbesc(datetime_convert())
1514                 );
1515         }
1516         logger("End discovery for server ".$server_url, LOGGER_DEBUG);
1517
1518         return !$failure;
1519 }
1520
1521 function count_common_friends($uid,$cid) {
1522
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 ) ",
1528                 intval($cid),
1529                 intval($uid),
1530                 intval($uid),
1531                 intval($cid)
1532         );
1533
1534 //      logger("count_common_friends: $uid $cid {$r[0]['total']}");
1535         if (dbm::is_result($r))
1536                 return $r[0]['total'];
1537         return 0;
1538
1539 }
1540
1541
1542 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
1543
1544         if($shuffle)
1545                 $sql_extra = " order by rand() ";
1546         else
1547                 $sql_extra = " order by `gcontact`.`name` asc ";
1548
1549         $r = q("SELECT `gcontact`.*, `contact`.`id` AS `cid`
1550                 FROM `glink`
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",
1558                 intval($cid),
1559                 intval($uid),
1560                 intval($uid),
1561                 intval($cid),
1562                 intval($start),
1563                 intval($limit)
1564         );
1565
1566         return $r;
1567
1568 }
1569
1570
1571 function count_common_friends_zcid($uid,$zcid) {
1572
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 ) ",
1577                 intval($zcid),
1578                 intval($uid)
1579         );
1580
1581         if (dbm::is_result($r))
1582                 return $r[0]['total'];
1583         return 0;
1584
1585 }
1586
1587 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
1588
1589         if($shuffle)
1590                 $sql_extra = " order by rand() ";
1591         else
1592                 $sql_extra = " order by `gcontact`.`name` asc ";
1593
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",
1599                 intval($zcid),
1600                 intval($uid),
1601                 intval($start),
1602                 intval($limit)
1603         );
1604
1605         return $r;
1606
1607 }
1608
1609
1610 function count_all_friends($uid,$cid) {
1611
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`))",
1616                 intval($cid),
1617                 intval($uid)
1618         );
1619
1620         if (dbm::is_result($r))
1621                 return $r[0]['total'];
1622         return 0;
1623
1624 }
1625
1626
1627 function all_friends($uid,$cid,$start = 0, $limit = 80) {
1628
1629         $r = q("SELECT `gcontact`.*, `contact`.`id` AS `cid`
1630                 FROM `glink`
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 ",
1636                 intval($uid),
1637                 intval($cid),
1638                 intval($uid),
1639                 intval($start),
1640                 intval($limit)
1641         );
1642
1643         return $r;
1644 }
1645
1646
1647
1648 function suggestion_query($uid, $start = 0, $limit = 80) {
1649
1650         if (!$uid) {
1651                 return array();
1652         }
1653
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)) {
1658 //              return $list;
1659 //      }
1660
1661         $network = array(NETWORK_DFRN);
1662
1663         if (get_config('system','diaspora_enabled'))
1664                 $network[] = NETWORK_DIASPORA;
1665
1666         if (!get_config('system','ostatus_disabled'))
1667                 $network[] = NETWORK_OSTATUS;
1668
1669         $sql_network = implode("', '", $network);
1670         $sql_network = "'".$sql_network."'";
1671
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",
1683                 intval($uid),
1684                 intval($uid),
1685                 intval($uid),
1686                 intval($uid),
1687                 dbesc(NULL_DATE),
1688                 $sql_network,
1689                 intval($start),
1690                 intval($limit)
1691         );
1692
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);
1697
1698                 return $r;
1699         }
1700
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",
1710                 intval($uid),
1711                 intval($uid),
1712                 intval($uid),
1713                 dbesc(NULL_DATE),
1714                 $sql_network,
1715                 intval($start),
1716                 intval($limit)
1717         );
1718
1719         $list = array();
1720         foreach ($r2 AS $suggestion)
1721                 $list[$suggestion["nurl"]] = $suggestion;
1722
1723         foreach ($r AS $suggestion)
1724                 $list[$suggestion["nurl"]] = $suggestion;
1725
1726         while (sizeof($list) > ($limit))
1727                 array_pop($list);
1728
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);
1732         return $list;
1733 }
1734
1735 function update_suggestions() {
1736
1737         $a = get_app();
1738
1739         $done = array();
1740
1741         /// @TODO Check if it is really neccessary to poll the own server
1742         poco_load(0,0,0,App::get_baseurl() . '/poco');
1743
1744         $done[] = App::get_baseurl() . '/poco';
1745
1746         if (strlen(get_config('system','directory'))) {
1747                 $x = fetch_url(get_server()."/pubsites");
1748                 if ($x) {
1749                         $j = json_decode($x);
1750                         if ($j->entries) {
1751                                 foreach ($j->entries as $entry) {
1752
1753                                         poco_check_server($entry->url);
1754
1755                                         $url = $entry->url . '/poco';
1756                                         if (! in_array($url,$done)) {
1757                                                 poco_load(0,0,0,$entry->url . '/poco');
1758                                         }
1759                                 }
1760                         }
1761                 }
1762         }
1763
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)
1767         );
1768
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);
1774                 }
1775         }
1776 }
1777
1778 /**
1779  * @brief Fetch server list from remote servers and adds them when they are new.
1780  *
1781  * @param string $poco URL to the POCO endpoint
1782  */
1783 function poco_fetch_serverlist($poco) {
1784         $serverret = z_fetch_url($poco."/@server");
1785         if (!$serverret["success"]) {
1786                 return;
1787         }
1788         $serverlist = json_decode($serverret['body']);
1789
1790         if (!is_array($serverlist)) {
1791                 return;
1792         }
1793
1794         foreach ($serverlist AS $server) {
1795                 $server_url = str_replace("/index.php", "", $server->url);
1796
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));
1801                 }
1802         }
1803 }
1804
1805 function poco_discover_federation() {
1806         $last = get_config('poco','last_federation_discovery');
1807
1808         if ($last) {
1809                 $next = $last + (24 * 60 * 60);
1810                 if($next > time())
1811                         return;
1812         }
1813
1814         // Discover Friendica, Hubzilla and Diaspora servers
1815         $serverdata = fetch_url("http://the-federation.info/pods.json");
1816
1817         if ($serverdata) {
1818                 $servers = json_decode($serverdata);
1819
1820                 foreach ($servers->pods AS $server) {
1821                         proc_run(PRIORITY_LOW, "include/discover_poco.php", "server", base64_encode("https://".$server->host));
1822                 }
1823         }
1824
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/";
1830
1831         //      $result = z_fetch_url($serverdata);
1832         //      if ($result["success"]) {
1833         //              $servers = json_decode($result["body"]);
1834
1835         //              foreach($servers->data AS $server)
1836         //                      poco_check_server($server->instance_address);
1837         //      }
1838         //}
1839
1840         set_config('poco','last_federation_discovery', time());
1841 }
1842
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)) {
1846                 return false;
1847         }
1848
1849         $server = $r[0];
1850
1851         // Discover new servers out there (Works from Friendica version 3.5.2)
1852         poco_fetch_serverlist($server["poco"]);
1853
1854         // Fetch all users from the other server
1855         $url = $server["poco"]."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1856
1857         logger("Fetch all users from the server ".$server["url"], LOGGER_DEBUG);
1858
1859         $retdata = z_fetch_url($url);
1860         if ($retdata["success"]) {
1861                 $data = json_decode($retdata["body"]);
1862
1863                 poco_discover_server($data, 2);
1864
1865                 if (get_config('system','poco_discovery') > 1) {
1866
1867                         $timeframe = get_config('system','poco_discovery_since');
1868                         if ($timeframe == 0) {
1869                                 $timeframe = 30;
1870                         }
1871
1872                         $updatedSince = date("Y-m-d H:i:s", time() - $timeframe * 86400);
1873
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";
1876
1877                         $success = false;
1878
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"]));
1883                         }
1884
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);
1888                         }
1889                 }
1890
1891                 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
1892
1893                 return true;
1894         } else {
1895                 // If the server hadn't replied correctly, then force a sanity check
1896                 poco_check_server($server["url"], $server["network"], true);
1897
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"]));
1900
1901                 return false;
1902         }
1903 }
1904
1905 function poco_discover($complete = false) {
1906
1907         // Update the server list
1908         poco_discover_federation();
1909
1910         $no_of_queries = 5;
1911
1912         $requery_days = intval(get_config("system", "poco_requery_days"));
1913
1914         if ($requery_days == 0) {
1915                 $requery_days = 7;
1916         }
1917         $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
1918
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) {
1922
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"]));
1926                                 continue;
1927                         }
1928
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']));
1931
1932                         if (!$complete AND (--$no_of_queries == 0)) {
1933                                 break;
1934                         }
1935                 }
1936         }
1937 }
1938
1939 function poco_discover_server_users($data, $server) {
1940
1941         if (!isset($data->entry))
1942                 return;
1943
1944         foreach ($data->entry AS $entry) {
1945                 $username = "";
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"]));
1952                                 }
1953                 }
1954                 if ($username != "") {
1955                         logger("Fetch contacts for the user ".$username." from the server ".$server["nurl"], LOGGER_DEBUG);
1956
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";
1959
1960                         $retdata = z_fetch_url($url);
1961                         if ($retdata["success"])
1962                                 poco_discover_server(json_decode($retdata["body"]), 3);
1963                 }
1964         }
1965 }
1966
1967 function poco_discover_server($data, $default_generation = 0) {
1968
1969         if (!isset($data->entry) OR !count($data->entry))
1970                 return false;
1971
1972         $success = false;
1973
1974         foreach ($data->entry AS $entry) {
1975                 $profile_url = '';
1976                 $profile_photo = '';
1977                 $connect_url = '';
1978                 $name = '';
1979                 $network = '';
1980                 $updated = NULL_DATE;
1981                 $location = '';
1982                 $about = '';
1983                 $keywords = '';
1984                 $gender = '';
1985                 $contact_type = -1;
1986                 $generation = $default_generation;
1987
1988                 $name = $entry->displayName;
1989
1990                 if (isset($entry->urls)) {
1991                         foreach($entry->urls as $url) {
1992                                 if ($url->type == 'profile') {
1993                                         $profile_url = $url->value;
1994                                         continue;
1995                                 }
1996                                 if ($url->type == 'webfinger') {
1997                                         $connect_url = str_replace('acct:' , '', $url->value);
1998                                         continue;
1999                                 }
2000                         }
2001                 }
2002
2003                 if (isset($entry->photos)) {
2004                         foreach ($entry->photos as $photo) {
2005                                 if ($photo->type == 'profile') {
2006                                         $profile_photo = $photo->value;
2007                                         continue;
2008                                 }
2009                         }
2010                 }
2011
2012                 if (isset($entry->updated)) {
2013                         $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
2014                 }
2015
2016                 if(isset($entry->network)) {
2017                         $network = $entry->network;
2018                 }
2019
2020                 if(isset($entry->currentLocation)) {
2021                         $location = $entry->currentLocation;
2022                 }
2023
2024                 if(isset($entry->aboutMe)) {
2025                         $about = html2bbcode($entry->aboutMe);
2026                 }
2027
2028                 if(isset($entry->gender)) {
2029                         $gender = $entry->gender;
2030                 }
2031
2032                 if(isset($entry->generation) AND ($entry->generation > 0)) {
2033                         $generation = ++$entry->generation;
2034                 }
2035
2036                 if(isset($entry->contactType) AND ($entry->contactType >= 0)) {
2037                         $contact_type = $entry->contactType;
2038                 }
2039
2040                 if(isset($entry->tags)) {
2041                         foreach ($entry->tags as $tag) {
2042                                 $keywords = implode(", ", $tag);
2043                         }
2044                 }
2045
2046                 if ($generation > 0) {
2047                         $success = true;
2048
2049                         logger("Store profile ".$profile_url, LOGGER_DEBUG);
2050
2051                         $gcontact = array("url" => $profile_url,
2052                                         "name" => $name,
2053                                         "network" => $network,
2054                                         "photo" => $profile_photo,
2055                                         "about" => $about,
2056                                         "location" => $location,
2057                                         "gender" => $gender,
2058                                         "keywords" => $keywords,
2059                                         "connect" => $connect_url,
2060                                         "updated" => $updated,
2061                                         "contact-type" => $contact_type,
2062                                         "generation" => $generation);
2063
2064                         if (validate_gcontact($gcontact)) {
2065                                 update_gcontact($gcontact);
2066                         }
2067
2068                         logger("Done for profile ".$profile_url, LOGGER_DEBUG);
2069                 }
2070         }
2071         return $success;
2072 }
2073
2074 /**
2075  * @brief Removes unwanted parts from a contact url
2076  *
2077  * @param string $url Contact url
2078  * @return string Contact url with the wanted parts
2079  */
2080 function clean_contact_url($url) {
2081         $parts = parse_url($url);
2082
2083         if (!isset($parts["scheme"]) OR !isset($parts["host"]))
2084                 return $url;
2085
2086         $new_url = $parts["scheme"]."://".$parts["host"];
2087
2088         if (isset($parts["port"]))
2089                 $new_url .= ":".$parts["port"];
2090
2091         if (isset($parts["path"]))
2092                 $new_url .= $parts["path"];
2093
2094         if ($new_url != $url)
2095                 logger("Cleaned contact url ".$url." to ".$new_url." - Called by: ".App::callstack(), LOGGER_DEBUG);
2096
2097         return $new_url;
2098 }
2099
2100 /**
2101  * @brief Replace alternate OStatus user format with the primary one
2102  *
2103  * @param arr $contact contact array (called by reference)
2104  */
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"];
2114                 }
2115         }
2116 }
2117
2118 /**
2119  * @brief Fetch the gcontact id, add an entry if not existed
2120  *
2121  * @param arr $contact contact array
2122  * @return bool|int Returns false if not found, integer if contact was found
2123  */
2124 function get_gcontact_id($contact) {
2125
2126         $gcontact_id = 0;
2127         $doprobing = false;
2128
2129         if (in_array($contact["network"], array(NETWORK_PHANTOM))) {
2130                 logger("Invalid network for contact url ".$contact["url"]." - Called by: ".App::callstack(), LOGGER_DEBUG);
2131                 return false;
2132         }
2133
2134         if ($contact["network"] == NETWORK_STATUSNET)
2135                 $contact["network"] = NETWORK_OSTATUS;
2136
2137         // All new contacts are hidden by default
2138         if (!isset($contact["hide"]))
2139                 $contact["hide"] = true;
2140
2141         // Replace alternate OStatus user format with the primary one
2142         fix_alternate_contact_address($contact);
2143
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"]);
2147
2148         $r = q("SELECT `id`, `last_contact`, `last_failure`, `network` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 2",
2149                 dbesc(normalise_link($contact["url"])));
2150
2151         if ($r) {
2152                 $gcontact_id = $r[0]["id"];
2153
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)));
2161                 }
2162         } else {
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"])
2178                 );
2179
2180                 $r = q("SELECT `id`, `network` FROM `gcontact` WHERE `nurl` = '%s' ORDER BY `id` LIMIT 2",
2181                         dbesc(normalise_link($contact["url"])));
2182
2183                 if ($r) {
2184                         $gcontact_id = $r[0]["id"];
2185
2186                         $doprobing = in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""));
2187                 }
2188         }
2189
2190         if ($doprobing) {
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"]));
2193         }
2194
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));
2199
2200         return $gcontact_id;
2201 }
2202
2203 /**
2204  * @brief Updates the gcontact table from a given array
2205  *
2206  * @param arr $contact contact array
2207  * @return bool|int Returns false if not found, integer if contact was found
2208  */
2209 function update_gcontact($contact) {
2210
2211         // Check for invalid "contact-type" value
2212         if (isset($contact['contact-type']) AND (intval($contact['contact-type']) < 0)) {
2213                 $contact['contact-type'] = 0;
2214         }
2215
2216         /// @todo update contact table as well
2217
2218         $gcontact_id = get_gcontact_id($contact);
2219
2220         if (!$gcontact_id)
2221                 return false;
2222
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));
2227
2228         // Get all field names
2229         $fields = array();
2230         foreach ($r[0] AS $field => $data)
2231                 $fields[$field] = $data;
2232
2233         unset($fields["url"]);
2234         unset($fields["updated"]);
2235         unset($fields["hide"]);
2236
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"]);
2241
2242         if ($r[0]["keywords"] == "0")
2243                 $r[0]["keywords"] = "";
2244
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];
2249
2250         if (!isset($contact["hide"]))
2251                 $contact["hide"] = $r[0]["hide"];
2252
2253         $fields["hide"] = $r[0]["hide"];
2254
2255         if ($contact["network"] == NETWORK_STATUSNET)
2256                 $contact["network"] = NETWORK_OSTATUS;
2257
2258         // Replace alternate OStatus user format with the primary one
2259         fix_alternate_contact_address($contact);
2260
2261         if (!isset($contact["updated"]))
2262                 $contact["updated"] = datetime_convert();
2263
2264         if ($contact["server_url"] == "") {
2265                 $server_url = $contact["url"];
2266
2267                 $server_url = matching_url($server_url, $contact["alias"]);
2268                 if ($server_url != "")
2269                         $contact["server_url"] = $server_url;
2270
2271                 $server_url = matching_url($server_url, $contact["photo"]);
2272                 if ($server_url != "")
2273                         $contact["server_url"] = $server_url;
2274
2275                 $server_url = matching_url($server_url, $contact["notify"]);
2276                 if ($server_url != "")
2277                         $contact["server_url"] = $server_url;
2278         } else
2279                 $contact["server_url"] = normalise_link($contact["server_url"]);
2280
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;
2284         }
2285
2286         // Check if any field changed
2287         $update = false;
2288         unset($fields["generation"]);
2289
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);
2294                                 $update = true;
2295                         }
2296
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);
2299                         $update = true;
2300                 }
2301         }
2302
2303         if ($update) {
2304                 logger("Update gcontact for ".$contact["url"], LOGGER_DEBUG);
2305
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"]));
2320
2321
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"])));
2326
2327                 if ($r) {
2328                         logger("Update shadow contact ".$r[0]["id"], LOGGER_DEBUG);
2329
2330                         update_contact_avatar($contact["photo"], 0, $r[0]["id"]);
2331
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'
2336                                         WHERE `id` = %d",
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"]));
2342                 }
2343         }
2344
2345         return $gcontact_id;
2346 }
2347
2348 /**
2349  * @brief Updates the gcontact entry from probe
2350  *
2351  * @param str $url profile link
2352  */
2353 function update_gcontact_from_probe($url) {
2354         $data = probe_url($url);
2355
2356         if (in_array($data["network"], array(NETWORK_PHANTOM))) {
2357                 logger("Invalid network for contact url ".$data["url"]." - Called by: ".App::callstack(), LOGGER_DEBUG);
2358                 return;
2359         }
2360
2361         update_gcontact($data);
2362 }
2363
2364 /**
2365  * @brief Update the gcontact entry for a given user id
2366  *
2367  * @param int $uid User ID
2368  */
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`
2375                 FROM `profile`
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`",
2379                 intval($uid));
2380
2381         $location = formatted_location(array("locality" => $r[0]["locality"], "region" => $r[0]["region"],
2382                                                 "country-name" => $r[0]["country-name"]));
2383
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());
2387         else
2388                 $addr = $r[0]["addr"];
2389
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);
2398
2399         update_gcontact($gcontact);
2400 }
2401
2402 /**
2403  * @brief Fetches users of given GNU Social server
2404  *
2405  * If the "Statistics" plugin is enabled (See http://gstools.org/ for details) we query user data with this.
2406  *
2407  * @param str $server Server address
2408  */
2409 function gs_fetch_users($server) {
2410
2411         logger("Fetching users from GNU Social server ".$server, LOGGER_DEBUG);
2412
2413         $url = $server."/main/statistics";
2414
2415         $result = z_fetch_url($url);
2416         if (!$result["success"])
2417                 return false;
2418
2419         $statistics = json_decode($result["body"]);
2420
2421         if (is_object($statistics->config)) {
2422                 if ($statistics->config->instance_with_ssl)
2423                         $server = "https://";
2424                 else
2425                         $server = "http://";
2426
2427                 $server .= $statistics->config->instance_address;
2428
2429                 $hostname = $statistics->config->instance_address;
2430         } else {
2431                 if ($statistics->instance_with_ssl)
2432                         $server = "https://";
2433                 else
2434                         $server = "http://";
2435
2436                 $server .= $statistics->instance_address;
2437
2438                 $hostname = $statistics->instance_address;
2439         }
2440
2441         if (is_object($statistics->users))
2442                 foreach ($statistics->users AS $nick => $user) {
2443                         $profile_url = $server."/".$user->nickname;
2444
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);
2453                 }
2454 }
2455
2456 /**
2457  * @brief Asking GNU Social server on a regular base for their user data
2458  *
2459  */
2460 function gs_discover() {
2461
2462         $requery_days = intval(get_config("system", "poco_requery_days"));
2463
2464         $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
2465
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));
2468
2469         if (!$r)
2470                 return;
2471
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"]));
2475         }
2476 }
2477
2478 /**
2479  * @brief Returns a list of all known servers
2480  * @return array List of server urls
2481  */
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`
2486                 LIMIT 1000",
2487                 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
2488         if (!dbm::is_result($r)) {
2489                 return false;
2490         }
2491         return $r;
2492 }
2493 ?>