]> git.mxchange.org Git - friendica.git/blob - include/socgraph.php
e5c612e82722045e117c5386631825f3c1afb7bd
[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 use Friendica\App;
11 use Friendica\Core\System;
12 use Friendica\Core\Config;
13 use Friendica\Core\Worker;
14 use Friendica\Network\Probe;
15
16 require_once 'include/datetime.php';
17 require_once 'include/network.php';
18 require_once 'include/html2bbcode.php';
19 require_once 'include/Contact.php';
20 require_once 'include/Photo.php';
21
22 /**
23  * @brief Fetch POCO data
24  *
25  * @param integer $cid Contact ID
26  * @param integer $uid User ID
27  * @param integer $zcid Global Contact ID
28  * @param integer $url POCO address that should be polled
29  *
30  * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
31  * and add the entries to the gcontact (Global Contact) table, or update existing entries
32  * if anything (name or photo) has changed.
33  * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
34  *
35  * Once the global contact is stored add (if necessary) the contact linkage which associates
36  * the given uid, cid to the global contact entry. There can be many uid/cid combinations
37  * pointing to the same global contact id.
38  *
39  */
40 function poco_load($cid, $uid = 0, $zcid = 0, $url = null) {
41         // Call the function "poco_load_worker" via the worker
42         Worker::add(PRIORITY_LOW, "discover_poco", "poco_load", (int)$cid, (int)$uid, (int)$zcid, $url);
43 }
44
45 /**
46  * @brief Fetch POCO data from the worker
47  *
48  * @param integer $cid Contact ID
49  * @param integer $uid User ID
50  * @param integer $zcid Global Contact ID
51  * @param integer $url POCO address that should be polled
52  *
53  */
54 function poco_load_worker($cid, $uid, $zcid, $url) {
55         $a = get_app();
56
57         if ($cid) {
58                 if ((! $url) || (! $uid)) {
59                         $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
60                                 intval($cid)
61                         );
62                         if (dbm::is_result($r)) {
63                                 $url = $r[0]['poco'];
64                                 $uid = $r[0]['uid'];
65                         }
66                 }
67                 if (! $uid) {
68                         return;
69                 }
70         }
71
72         if (! $url) {
73                 return;
74         }
75
76         $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation' : '?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation') ;
77
78         logger('poco_load: ' . $url, LOGGER_DEBUG);
79
80         $s = fetch_url($url);
81
82         logger('poco_load: returns ' . $s, LOGGER_DATA);
83
84         logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
85
86         if (($a->get_curl_code() > 299) || (! $s)) {
87                 return;
88         }
89
90         $j = json_decode($s);
91
92         logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
93
94         if (! isset($j->entry)) {
95                 return;
96         }
97
98         $total = 0;
99         foreach ($j->entry as $entry) {
100
101                 $total ++;
102                 $profile_url = '';
103                 $profile_photo = '';
104                 $connect_url = '';
105                 $name = '';
106                 $network = '';
107                 $updated = NULL_DATE;
108                 $location = '';
109                 $about = '';
110                 $keywords = '';
111                 $gender = '';
112                 $contact_type = -1;
113                 $generation = 0;
114
115                 $name = $entry->displayName;
116
117                 if (isset($entry->urls)) {
118                         foreach ($entry->urls as $url) {
119                                 if ($url->type == 'profile') {
120                                         $profile_url = $url->value;
121                                         continue;
122                                 }
123                                 if ($url->type == 'webfinger') {
124                                         $connect_url = str_replace('acct:' , '', $url->value);
125                                         continue;
126                                 }
127                         }
128                 }
129                 if (isset($entry->photos)) {
130                         foreach ($entry->photos as $photo) {
131                                 if ($photo->type == 'profile') {
132                                         $profile_photo = $photo->value;
133                                         continue;
134                                 }
135                         }
136                 }
137
138                 if (isset($entry->updated)) {
139                         $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
140                 }
141
142                 if (isset($entry->network)) {
143                         $network = $entry->network;
144                 }
145
146                 if (isset($entry->currentLocation)) {
147                         $location = $entry->currentLocation;
148                 }
149
150                 if (isset($entry->aboutMe)) {
151                         $about = html2bbcode($entry->aboutMe);
152                 }
153
154                 if (isset($entry->gender)) {
155                         $gender = $entry->gender;
156                 }
157
158                 if (isset($entry->generation) && ($entry->generation > 0)) {
159                         $generation = ++$entry->generation;
160                 }
161
162                 if (isset($entry->tags)) {
163                         foreach ($entry->tags as $tag) {
164                                 $keywords = implode(", ", $tag);
165                         }
166                 }
167
168                 if (isset($entry->contactType) && ($entry->contactType >= 0)) {
169                         $contact_type = $entry->contactType;
170                 }
171
172                 $gcontact = array("url" => $profile_url,
173                                 "name" => $name,
174                                 "network" => $network,
175                                 "photo" => $profile_photo,
176                                 "about" => $about,
177                                 "location" => $location,
178                                 "gender" => $gender,
179                                 "keywords" => $keywords,
180                                 "connect" => $connect_url,
181                                 "updated" => $updated,
182                                 "contact-type" => $contact_type,
183                                 "generation" => $generation);
184
185                 try {
186                         $gcontact = sanitize_gcontact($gcontact);
187                         $gcid = update_gcontact($gcontact);
188
189                         link_gcontact($gcid, $uid, $cid, $zcid);
190                 } catch (Exception $e) {
191                         logger($e->getMessage(), LOGGER_DEBUG);
192                 }
193         }
194         logger("poco_load: loaded $total entries",LOGGER_DEBUG);
195
196         q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
197                 intval($cid),
198                 intval($uid),
199                 intval($zcid)
200         );
201
202 }
203 /**
204  * @brief Sanitize the given gcontact data
205  *
206  * @param array $gcontact array with gcontact data
207  * @throw Exception
208  *
209  * Generation:
210  *  0: No definition
211  *  1: Profiles on this server
212  *  2: Contacts of profiles on this server
213  *  3: Contacts of contacts of profiles on this server
214  *  4: ...
215  *
216  */
217 function sanitize_gcontact($gcontact) {
218
219         if ($gcontact['url'] == "") {
220                 throw new Exception('URL is empty');
221         }
222
223         $urlparts = parse_url($gcontact['url']);
224         if (!isset($urlparts["scheme"])) {
225                 throw new Exception("This (".$gcontact['url'].") doesn't seem to be an url.");
226         }
227
228         if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com",
229                                                 "identi.ca", "alpha.app.net"))) {
230                 throw new Exception('Contact from a non federated network ignored. ('.$gcontact['url'].')');
231         }
232
233         // Don't store the statusnet connector as network
234         // We can't simply set this to NETWORK_OSTATUS since the connector could have fetched posts from friendica as well
235         if ($gcontact['network'] == NETWORK_STATUSNET) {
236                 $gcontact['network'] = "";
237         }
238
239         // Assure that there are no parameter fragments in the profile url
240         if (in_array($gcontact['network'], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) {
241                 $gcontact['url'] = clean_contact_url($gcontact['url']);
242         }
243
244         $alternate = poco_alternate_ostatus_url($gcontact['url']);
245
246         // The global contacts should contain the original picture, not the cached one
247         if (($gcontact['generation'] != 1) && stristr(normalise_link($gcontact['photo']), normalise_link(System::baseUrl()."/photo/"))) {
248                 $gcontact['photo'] = "";
249         }
250
251         if (!isset($gcontact['network'])) {
252                 $r = q("SELECT `network` FROM `contact` WHERE `uid` = 0 AND `nurl` = '%s' AND `network` != '' AND `network` != '%s' LIMIT 1",
253                         dbesc(normalise_link($gcontact['url'])), dbesc(NETWORK_STATUSNET)
254                 );
255                 if (dbm::is_result($r)) {
256                         $gcontact['network'] = $r[0]["network"];
257                 }
258
259                 if (($gcontact['network'] == "") || ($gcontact['network'] == NETWORK_OSTATUS)) {
260                         $r = q("SELECT `network`, `url` FROM `contact` WHERE `uid` = 0 AND `alias` IN ('%s', '%s') AND `network` != '' AND `network` != '%s' LIMIT 1",
261                                 dbesc($gcontact['url']), dbesc(normalise_link($gcontact['url'])), dbesc(NETWORK_STATUSNET)
262                         );
263                         if (dbm::is_result($r)) {
264                                 $gcontact['network'] = $r[0]["network"];
265                         }
266                 }
267         }
268
269         $gcontact['server_url'] = '';
270         $gcontact['network'] = '';
271
272         $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
273                 dbesc(normalise_link($gcontact['url']))
274         );
275
276         if (dbm::is_result($x)) {
277                 if (!isset($gcontact['network']) && ($x[0]["network"] != NETWORK_STATUSNET)) {
278                         $gcontact['network'] = $x[0]["network"];
279                 }
280                 if ($gcontact['updated'] <= NULL_DATE) {
281                         $gcontact['updated'] = $x[0]["updated"];
282                 }
283                 if (!isset($gcontact['server_url']) && (normalise_link($x[0]["server_url"]) != normalise_link($x[0]["url"]))) {
284                         $gcontact['server_url'] = $x[0]["server_url"];
285                 }
286                 if (!isset($gcontact['addr'])) {
287                         $gcontact['addr'] = $x[0]["addr"];
288                 }
289         }
290
291         if ((!isset($gcontact['network']) || !isset($gcontact['name']) || !isset($gcontact['addr']) || !isset($gcontact['photo']) || !isset($gcontact['server_url']) || $alternate)
292                 && poco_reachable($gcontact['url'], $gcontact['server_url'], $gcontact['network'], false)) {
293                 $data = Probe::uri($gcontact['url']);
294
295                 if ($data["network"] == NETWORK_PHANTOM) {
296                         throw new Exception('Probing for URL '.$gcontact['url'].' failed');
297                 }
298
299                 $orig_profile = $gcontact['url'];
300
301                 $gcontact["server_url"] = $data["baseurl"];
302
303                 $gcontact = array_merge($gcontact, $data);
304
305                 if ($alternate && ($gcontact['network'] == NETWORK_OSTATUS)) {
306                         // Delete the old entry - if it exists
307                         $r = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($orig_profile)));
308                         if (dbm::is_result($r)) {
309                                 q("DELETE FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($orig_profile)));
310                                 q("DELETE FROM `glink` WHERE `gcid` = %d", intval($r[0]["id"]));
311                         }
312                 }
313         }
314
315         if (!isset($gcontact['name']) || !isset($gcontact['photo'])) {
316                 throw new Exception('No name and photo for URL '.$gcontact['url']);
317         }
318
319         if (!in_array($gcontact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA))) {
320                 throw new Exception('No federated network ('.$gcontact['network'].') detected for URL '.$gcontact['url']);
321         }
322
323         if (!isset($gcontact['server_url'])) {
324                 // We check the server url to be sure that it is a real one
325                 $server_url = poco_detect_server($gcontact['url']);
326
327                 // We are now sure that it is a correct URL. So we use it in the future
328                 if ($server_url != "") {
329                         $gcontact['server_url'] = $server_url;
330                 }
331         }
332
333         // The server URL doesn't seem to be valid, so we don't store it.
334         if (!poco_check_server($gcontact['server_url'], $gcontact['network'])) {
335                 $gcontact['server_url'] = "";
336         }
337
338         return $gcontact;
339 }
340
341 /**
342  * @brief Link the gcontact entry with user, contact and global contact
343  *
344  * @param integer $gcid Global contact ID
345  * @param integer $cid Contact ID
346  * @param integer $uid User ID
347  * @param integer $zcid Global Contact ID
348  * *
349  */
350 function link_gcontact($gcid, $uid = 0, $cid = 0, $zcid = 0) {
351
352         if ($gcid <= 0) {
353                 return;
354         }
355
356         $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
357                 intval($cid),
358                 intval($uid),
359                 intval($gcid),
360                 intval($zcid)
361         );
362
363         if (!dbm::is_result($r)) {
364                 q("INSERT INTO `glink` (`cid`, `uid`, `gcid`, `zcid`, `updated`) VALUES (%d, %d, %d, %d, '%s') ",
365                         intval($cid),
366                         intval($uid),
367                         intval($gcid),
368                         intval($zcid),
369                         dbesc(datetime_convert())
370                 );
371         } else {
372                 q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
373                         dbesc(datetime_convert()),
374                         intval($cid),
375                         intval($uid),
376                         intval($gcid),
377                         intval($zcid)
378                 );
379         }
380 }
381
382 function poco_reachable($profile, $server = "", $network = "", $force = false) {
383
384         if ($server == "") {
385                 $server = poco_detect_server($profile);
386         }
387
388         if ($server == "") {
389                 return true;
390         }
391
392         return poco_check_server($server, $network, $force);
393 }
394
395 function poco_detect_server($profile) {
396
397         // Try to detect the server path based upon some known standard paths
398         $server_url = "";
399
400         if ($server_url == "") {
401                 $friendica = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $profile);
402                 if ($friendica != $profile) {
403                         $server_url = $friendica;
404                         $network = NETWORK_DFRN;
405                 }
406         }
407
408         if ($server_url == "") {
409                 $diaspora = preg_replace("=(https?://)(.*)/u/(.*)=ism", "$1$2", $profile);
410                 if ($diaspora != $profile) {
411                         $server_url = $diaspora;
412                         $network = NETWORK_DIASPORA;
413                 }
414         }
415
416         if ($server_url == "") {
417                 $red = preg_replace("=(https?://)(.*)/channel/(.*)=ism", "$1$2", $profile);
418                 if ($red != $profile) {
419                         $server_url = $red;
420                         $network = NETWORK_DIASPORA;
421                 }
422         }
423
424         // Mastodon
425         if ($server_url == "") {
426                 $mastodon = preg_replace("=(https?://)(.*)/users/(.*)=ism", "$1$2", $profile);
427                 if ($mastodon != $profile) {
428                         $server_url = $mastodon;
429                         $network = NETWORK_OSTATUS;
430                 }
431         }
432
433         // Numeric OStatus variant
434         if ($server_url == "") {
435                 $ostatus = preg_replace("=(https?://)(.*)/user/(.*)=ism", "$1$2", $profile);
436                 if ($ostatus != $profile) {
437                         $server_url = $ostatus;
438                         $network = NETWORK_OSTATUS;
439                 }
440         }
441
442         // Wild guess
443         if ($server_url == "") {
444                 $base = preg_replace("=(https?://)(.*?)/(.*)=ism", "$1$2", $profile);
445                 if ($base != $profile) {
446                         $server_url = $base;
447                         $network = NETWORK_PHANTOM;
448                 }
449         }
450
451         if ($server_url == "") {
452                 return "";
453         }
454
455         $r = q("SELECT `id` FROM `gserver` WHERE `nurl` = '%s' AND `last_contact` > `last_failure`",
456                 dbesc(normalise_link($server_url)));
457         if (dbm::is_result($r)) {
458                 return $server_url;
459         }
460
461         // Fetch the host-meta to check if this really is a server
462         $serverret = z_fetch_url($server_url."/.well-known/host-meta");
463         if (!$serverret["success"]) {
464                 return "";
465         }
466
467         return $server_url;
468 }
469
470 function poco_alternate_ostatus_url($url) {
471         return(preg_match("=https?://.+/user/\d+=ism", $url, $matches));
472 }
473
474 function poco_last_updated($profile, $force = false) {
475
476         $gcontacts = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'",
477                         dbesc(normalise_link($profile)));
478
479         if (!dbm::is_result($gcontacts)) {
480                 return false;
481         }
482
483         $contact = array("url" => $profile);
484
485         if ($gcontacts[0]["created"] <= NULL_DATE) {
486                 $contact['created'] = datetime_convert();
487         }
488
489         if ($force) {
490                 $server_url = normalise_link(poco_detect_server($profile));
491         }
492
493         if (($server_url == '') && ($gcontacts[0]["server_url"] != "")) {
494                 $server_url = $gcontacts[0]["server_url"];
495         }
496
497         if (!$force && (($server_url == '') || ($gcontacts[0]["server_url"] == $gcontacts[0]["nurl"]))) {
498                 $server_url = normalise_link(poco_detect_server($profile));
499         }
500
501         if (!in_array($gcontacts[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_FEED, NETWORK_OSTATUS, ""))) {
502                 logger("Profile ".$profile.": Network type ".$gcontacts[0]["network"]." can't be checked", LOGGER_DEBUG);
503                 return false;
504         }
505
506         if ($server_url != "") {
507                 if (!poco_check_server($server_url, $gcontacts[0]["network"], $force)) {
508                         if ($force) {
509                                 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
510                                         dbesc(datetime_convert()), dbesc(normalise_link($profile)));
511                         }
512
513                         logger("Profile ".$profile.": Server ".$server_url." wasn't reachable.", LOGGER_DEBUG);
514                         return false;
515                 }
516                 $contact['server_url'] = $server_url;
517         }
518
519         if (in_array($gcontacts[0]["network"], array("", NETWORK_FEED))) {
520                 $server = q("SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''",
521                         dbesc(normalise_link($server_url)));
522
523                 if ($server) {
524                         $contact['network'] = $server[0]["network"];
525                 } else {
526                         return false;
527                 }
528         }
529
530         // noscrape is really fast so we don't cache the call.
531         if (($server_url != "") && ($gcontacts[0]["nick"] != "")) {
532
533                 //  Use noscrape if possible
534                 $server = q("SELECT `noscrape`, `network` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", dbesc(normalise_link($server_url)));
535
536                 if ($server) {
537                         $noscraperet = z_fetch_url($server[0]["noscrape"]."/".$gcontacts[0]["nick"]);
538
539                         if ($noscraperet["success"] && ($noscraperet["body"] != "")) {
540
541                                 $noscrape = json_decode($noscraperet["body"], true);
542
543                                 if (is_array($noscrape)) {
544                                         $contact["network"] = $server[0]["network"];
545
546                                         if (isset($noscrape["fn"])) {
547                                                 $contact["name"] = $noscrape["fn"];
548                                         }
549                                         if (isset($noscrape["comm"])) {
550                                                 $contact["community"] = $noscrape["comm"];
551                                         }
552                                         if (isset($noscrape["tags"])) {
553                                                 $keywords = implode(" ", $noscrape["tags"]);
554                                                 if ($keywords != "") {
555                                                         $contact["keywords"] = $keywords;
556                                                 }
557                                         }
558
559                                         $location = formatted_location($noscrape);
560                                         if ($location) {
561                                                 $contact["location"] = $location;
562                                         }
563                                         if (isset($noscrape["dfrn-notify"])) {
564                                                 $contact["notify"] = $noscrape["dfrn-notify"];
565                                         }
566                                         // Remove all fields that are not present in the gcontact table
567                                         unset($noscrape["fn"]);
568                                         unset($noscrape["key"]);
569                                         unset($noscrape["homepage"]);
570                                         unset($noscrape["comm"]);
571                                         unset($noscrape["tags"]);
572                                         unset($noscrape["locality"]);
573                                         unset($noscrape["region"]);
574                                         unset($noscrape["country-name"]);
575                                         unset($noscrape["contacts"]);
576                                         unset($noscrape["dfrn-request"]);
577                                         unset($noscrape["dfrn-confirm"]);
578                                         unset($noscrape["dfrn-notify"]);
579                                         unset($noscrape["dfrn-poll"]);
580
581                                         // Set the date of the last contact
582                                         /// @todo By now the function "update_gcontact" doesn't work with this field
583                                         //$contact["last_contact"] = datetime_convert();
584
585                                         $contact = array_merge($contact, $noscrape);
586
587                                         update_gcontact($contact);
588
589                                         if (trim($noscrape["updated"]) != "") {
590                                                 q("UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'",
591                                                         dbesc(datetime_convert()), dbesc(normalise_link($profile)));
592
593                                                 logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG);
594
595                                                 return $noscrape["updated"];
596                                         }
597                                 }
598                         }
599                 }
600         }
601
602         // If we only can poll the feed, then we only do this once a while
603         if (!$force && !poco_do_update($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) {
604                 logger("Profile ".$profile." was last updated at ".$gcontacts[0]["updated"]." (cached)", LOGGER_DEBUG);
605
606                 update_gcontact($contact);
607                 return $gcontacts[0]["updated"];
608         }
609
610         $data = Probe::uri($profile);
611
612         // Is the profile link the alternate OStatus link notation? (http://domain.tld/user/4711)
613         // Then check the other link and delete this one
614         if (($data["network"] == NETWORK_OSTATUS) && poco_alternate_ostatus_url($profile) &&
615                 (normalise_link($profile) == normalise_link($data["alias"])) &&
616                 (normalise_link($profile) != normalise_link($data["url"]))) {
617
618                 // Delete the old entry
619                 q("DELETE FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($profile)));
620                 q("DELETE FROM `glink` WHERE `gcid` = %d", intval($gcontacts[0]["id"]));
621
622                 $gcontact = array_merge($gcontacts[0], $data);
623
624                 $gcontact["server_url"] = $data["baseurl"];
625
626                 try {
627                         $gcontact = sanitize_gcontact($gcontact);
628                         update_gcontact($gcontact);
629
630                         poco_last_updated($data["url"], $force);
631                 } catch (Exception $e) {
632                         logger($e->getMessage(), LOGGER_DEBUG);
633                 }
634
635                 logger("Profile ".$profile." was deleted", LOGGER_DEBUG);
636                 return false;
637         }
638
639         if (($data["poll"] == "") || (in_array($data["network"], array(NETWORK_FEED, NETWORK_PHANTOM)))) {
640                 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
641                         dbesc(datetime_convert()), dbesc(normalise_link($profile)));
642
643                 logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG);
644                 return false;
645         }
646
647         $contact = array_merge($contact, $data);
648
649         $contact["server_url"] = $data["baseurl"];
650
651         update_gcontact($contact);
652
653         $feedret = z_fetch_url($data["poll"]);
654
655         if (!$feedret["success"]) {
656                 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
657                         dbesc(datetime_convert()), dbesc(normalise_link($profile)));
658
659                 logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG);
660                 return false;
661         }
662
663         $doc = new DOMDocument();
664         @$doc->loadXML($feedret["body"]);
665
666         $xpath = new DomXPath($doc);
667         $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
668
669         $entries = $xpath->query('/atom:feed/atom:entry');
670
671         $last_updated = "";
672
673         foreach ($entries as $entry) {
674                 $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
675                 $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
676
677                 if ($last_updated < $published)
678                         $last_updated = $published;
679
680                 if ($last_updated < $updated)
681                         $last_updated = $updated;
682         }
683
684         // Maybe there aren't any entries. Then check if it is a valid feed
685         if ($last_updated == "") {
686                 if ($xpath->query('/atom:feed')->length > 0) {
687                         $last_updated = NULL_DATE;
688                 }
689         }
690         q("UPDATE `gcontact` SET `updated` = '%s', `last_contact` = '%s' WHERE `nurl` = '%s'",
691                 dbesc(dbm::date($last_updated)), dbesc(dbm::date()), dbesc(normalise_link($profile)));
692
693         if (($gcontacts[0]["generation"] == 0)) {
694                 q("UPDATE `gcontact` SET `generation` = 9 WHERE `nurl` = '%s'",
695                         dbesc(normalise_link($profile)));
696         }
697
698         logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG);
699
700         return($last_updated);
701 }
702
703 function poco_do_update($created, $updated, $last_failure,  $last_contact) {
704         $now = strtotime(datetime_convert());
705
706         if ($updated > $last_contact) {
707                 $contact_time = strtotime($updated);
708         } else {
709                 $contact_time = strtotime($last_contact);
710         }
711
712         $failure_time = strtotime($last_failure);
713         $created_time = strtotime($created);
714
715         // If there is no "created" time then use the current time
716         if ($created_time <= 0) {
717                 $created_time = $now;
718         }
719
720         // If the last contact was less than 24 hours then don't update
721         if (($now - $contact_time) < (60 * 60 * 24)) {
722                 return false;
723         }
724
725         // If the last failure was less than 24 hours then don't update
726         if (($now - $failure_time) < (60 * 60 * 24)) {
727                 return false;
728         }
729
730         // If the last contact was less than a week ago and the last failure is older than a week then don't update
731         //if ((($now - $contact_time) < (60 * 60 * 24 * 7)) && ($contact_time > $failure_time))
732         //      return false;
733
734         // If the last contact time was more than a week ago and the contact was created more than a week ago, then only try once a week
735         if ((($now - $contact_time) > (60 * 60 * 24 * 7)) && (($now - $created_time) > (60 * 60 * 24 * 7)) && (($now - $failure_time) < (60 * 60 * 24 * 7))) {
736                 return false;
737         }
738
739         // If the last contact time was more than a month ago and the contact was created more than a month ago, then only try once a month
740         if ((($now - $contact_time) > (60 * 60 * 24 * 30)) && (($now - $created_time) > (60 * 60 * 24 * 30)) && (($now - $failure_time) < (60 * 60 * 24 * 30))) {
741                 return false;
742         }
743
744         return true;
745 }
746
747 function poco_to_boolean($val) {
748         if (($val == "true") || ($val == 1)) {
749                 return true;
750         } elseif (($val == "false") || ($val == 0)) {
751                 return false;
752         }
753
754         return $val;
755 }
756
757 /**
758  * @brief Detect server type (Hubzilla or Friendica) via the poco data
759  *
760  * @param object $data POCO data
761  * @return array Server data
762  */
763 function poco_detect_poco_data($data) {
764         $server = false;
765
766         if (!isset($data->entry)) {
767                 return false;
768         }
769
770         if (count($data->entry) == 0) {
771                 return false;
772         }
773
774         if (!isset($data->entry[0]->urls)) {
775                 return false;
776         }
777
778         if (count($data->entry[0]->urls) == 0) {
779                 return false;
780         }
781
782         foreach ($data->entry[0]->urls as $url) {
783                 if ($url->type == 'zot') {
784                         $server = array();
785                         $server["platform"] = 'Hubzilla';
786                         $server["network"] = NETWORK_DIASPORA;
787                         return $server;
788                 }
789         }
790         return false;
791 }
792
793 /**
794  * @brief Detect server type by using the nodeinfo data
795  *
796  * @param string $server_url address of the server
797  * @return array Server data
798  */
799 function poco_fetch_nodeinfo($server_url) {
800         $serverret = z_fetch_url($server_url."/.well-known/nodeinfo");
801         if (!$serverret["success"]) {
802                 return false;
803         }
804
805         $nodeinfo = json_decode($serverret['body']);
806
807         if (!is_object($nodeinfo)) {
808                 return false;
809         }
810
811         if (!is_array($nodeinfo->links)) {
812                 return false;
813         }
814
815         $nodeinfo_url = '';
816
817         foreach ($nodeinfo->links as $link) {
818                 if ($link->rel == 'http://nodeinfo.diaspora.software/ns/schema/1.0') {
819                         $nodeinfo_url = $link->href;
820                 }
821         }
822
823         if ($nodeinfo_url == '') {
824                 return false;
825         }
826
827         $serverret = z_fetch_url($nodeinfo_url);
828         if (!$serverret["success"]) {
829                 return false;
830         }
831
832         $nodeinfo = json_decode($serverret['body']);
833
834         if (!is_object($nodeinfo)) {
835                 return false;
836         }
837
838         $server = array();
839
840         $server['register_policy'] = REGISTER_CLOSED;
841
842         if (is_bool($nodeinfo->openRegistrations) && $nodeinfo->openRegistrations) {
843                 $server['register_policy'] = REGISTER_OPEN;
844         }
845
846         if (is_object($nodeinfo->software)) {
847                 if (isset($nodeinfo->software->name)) {
848                         $server['platform'] = $nodeinfo->software->name;
849                 }
850
851                 if (isset($nodeinfo->software->version)) {
852                         $server['version'] = $nodeinfo->software->version;
853                         // Version numbers on Nodeinfo are presented with additional info, e.g.:
854                         // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
855                         $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']);
856                 }
857         }
858
859         if (is_object($nodeinfo->metadata)) {
860                 if (isset($nodeinfo->metadata->nodeName)) {
861                         $server['site_name'] = $nodeinfo->metadata->nodeName;
862                 }
863         }
864
865         $diaspora = false;
866         $friendica = false;
867         $gnusocial = false;
868
869         if (is_array($nodeinfo->protocols->inbound)) {
870                 foreach ($nodeinfo->protocols->inbound as $inbound) {
871                         if ($inbound == 'diaspora') {
872                                 $diaspora = true;
873                         }
874                         if ($inbound == 'friendica') {
875                                 $friendica = true;
876                         }
877                         if ($inbound == 'gnusocial') {
878                                 $gnusocial = true;
879                         }
880                 }
881         }
882
883         if ($gnusocial) {
884                 $server['network'] = NETWORK_OSTATUS;
885         }
886         if ($diaspora) {
887                 $server['network'] = NETWORK_DIASPORA;
888         }
889         if ($friendica) {
890                 $server['network'] = NETWORK_DFRN;
891         }
892
893         if (!$server) {
894                 return false;
895         }
896
897         return $server;
898 }
899
900 /**
901  * @brief Detect server type (Hubzilla or Friendica) via the front page body
902  *
903  * @param string $body Front page of the server
904  * @return array Server data
905  */
906 function poco_detect_server_type($body) {
907         $server = false;
908
909         $doc = new DOMDocument();
910         @$doc->loadHTML($body);
911         $xpath = new DomXPath($doc);
912
913         $list = $xpath->query("//meta[@name]");
914
915         foreach ($list as $node) {
916                 $attr = array();
917                 if ($node->attributes->length) {
918                         foreach ($node->attributes as $attribute) {
919                                 $attr[$attribute->name] = $attribute->value;
920                         }
921                 }
922                 if ($attr['name'] == 'generator') {
923                         $version_part = explode(" ", $attr['content']);
924                         if (count($version_part) == 2) {
925                                 if (in_array($version_part[0], array("Friendika", "Friendica"))) {
926                                         $server = array();
927                                         $server["platform"] = $version_part[0];
928                                         $server["version"] = $version_part[1];
929                                         $server["network"] = NETWORK_DFRN;
930                                 }
931                         }
932                 }
933         }
934
935         if (!$server) {
936                 $list = $xpath->query("//meta[@property]");
937
938                 foreach ($list as $node) {
939                         $attr = array();
940                         if ($node->attributes->length) {
941                                 foreach ($node->attributes as $attribute) {
942                                         $attr[$attribute->name] = $attribute->value;
943                                 }
944                         }
945                         if ($attr['property'] == 'generator' && in_array($attr['content'], array("hubzilla", "BlaBlaNet"))) {
946                                 $server = array();
947                                 $server["platform"] = $attr['content'];
948                                 $server["version"] = "";
949                                 $server["network"] = NETWORK_DIASPORA;
950                         }
951                 }
952         }
953
954         if (!$server) {
955                 return false;
956         }
957
958         $server["site_name"] = $xpath->evaluate($element."//head/title/text()", $context)->item(0)->nodeValue;
959         return $server;
960 }
961
962 function poco_check_server($server_url, $network = "", $force = false) {
963
964         // Unify the server address
965         $server_url = trim($server_url, "/");
966         $server_url = str_replace("/index.php", "", $server_url);
967
968         if ($server_url == "") {
969                 return false;
970         }
971
972         $servers = q("SELECT * FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
973         if (dbm::is_result($servers)) {
974
975                 if ($servers[0]["created"] <= NULL_DATE) {
976                         q("UPDATE `gserver` SET `created` = '%s' WHERE `nurl` = '%s'",
977                                 dbesc(datetime_convert()), dbesc(normalise_link($server_url)));
978                 }
979                 $poco = $servers[0]["poco"];
980                 $noscrape = $servers[0]["noscrape"];
981
982                 if ($network == "") {
983                         $network = $servers[0]["network"];
984                 }
985
986                 $last_contact = $servers[0]["last_contact"];
987                 $last_failure = $servers[0]["last_failure"];
988                 $version = $servers[0]["version"];
989                 $platform = $servers[0]["platform"];
990                 $site_name = $servers[0]["site_name"];
991                 $info = $servers[0]["info"];
992                 $register_policy = $servers[0]["register_policy"];
993
994                 if (!$force && !poco_do_update($servers[0]["created"], "", $last_failure, $last_contact)) {
995                         logger("Use cached data for server ".$server_url, LOGGER_DEBUG);
996                         return ($last_contact >= $last_failure);
997                 }
998         } else {
999                 $poco = "";
1000                 $noscrape = "";
1001                 $version = "";
1002                 $platform = "";
1003                 $site_name = "";
1004                 $info = "";
1005                 $register_policy = -1;
1006
1007                 $last_contact = NULL_DATE;
1008                 $last_failure = NULL_DATE;
1009         }
1010         logger("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$servers[0]["created"]." Failure: ".$last_failure." Contact: ".$last_contact, LOGGER_DEBUG);
1011
1012         $failure = false;
1013         $possible_failure = false;
1014         $orig_last_failure = $last_failure;
1015         $orig_last_contact = $last_contact;
1016
1017         // Check if the page is accessible via SSL.
1018         $orig_server_url = $server_url;
1019         $server_url = str_replace("http://", "https://", $server_url);
1020
1021         // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
1022         $serverret = z_fetch_url($server_url."/.well-known/host-meta", false, $redirects, array('timeout' => 20));
1023
1024         // Quit if there is a timeout.
1025         // But we want to make sure to only quit if we are mostly sure that this server url fits.
1026         if (dbm::is_result($servers) && ($orig_server_url == $server_url) &&
1027                 ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT)) {
1028                 logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
1029                 dba::update('gserver', array('last_failure' => datetime_convert()), array('nurl' => normalise_link($server_url)));
1030                 return false;
1031         }
1032
1033         // Maybe the page is unencrypted only?
1034         $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
1035         if (!$serverret["success"] || ($serverret["body"] == "") || (@sizeof($xmlobj) == 0) || !is_object($xmlobj)) {
1036                 $server_url = str_replace("https://", "http://", $server_url);
1037
1038                 // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
1039                 $serverret = z_fetch_url($server_url."/.well-known/host-meta", false, $redirects, array('timeout' => 20));
1040
1041                 // Quit if there is a timeout
1042                 if ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT) {
1043                         logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
1044                         dba::update('gserver', array('last_failure' => datetime_convert()), array('nurl' => normalise_link($server_url)));
1045                         return false;
1046                 }
1047
1048                 $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
1049         }
1050
1051         if (!$serverret["success"] || ($serverret["body"] == "") || (sizeof($xmlobj) == 0) || !is_object($xmlobj)) {
1052                 // Workaround for bad configured servers (known nginx problem)
1053                 if (!in_array($serverret["debug"]["http_code"], array("403", "404"))) {
1054                         $failure = true;
1055                 }
1056                 $possible_failure = true;
1057         }
1058
1059         // If the server has no possible failure we reset the cached data
1060         if (!$possible_failure) {
1061                 $version = "";
1062                 $platform = "";
1063                 $site_name = "";
1064                 $info = "";
1065                 $register_policy = -1;
1066         }
1067
1068         // Look for poco
1069         if (!$failure) {
1070                 $serverret = z_fetch_url($server_url."/poco");
1071                 if ($serverret["success"]) {
1072                         $data = json_decode($serverret["body"]);
1073                         if (isset($data->totalResults)) {
1074                                 $poco = $server_url."/poco";
1075                                 $server = poco_detect_poco_data($data);
1076                                 if ($server) {
1077                                         $platform = $server['platform'];
1078                                         $network = $server['network'];
1079                                         $version = '';
1080                                         $site_name = '';
1081                                 }
1082                         }
1083                 }
1084         }
1085
1086         if (!$failure) {
1087                 // Test for Diaspora, Hubzilla, Mastodon or older Friendica servers
1088                 $serverret = z_fetch_url($server_url);
1089
1090                 if (!$serverret["success"] || ($serverret["body"] == "")) {
1091                         $failure = true;
1092                 } else {
1093                         $server = poco_detect_server_type($serverret["body"]);
1094                         if ($server) {
1095                                 $platform = $server['platform'];
1096                                 $network = $server['network'];
1097                                 $version = $server['version'];
1098                                 $site_name = $server['site_name'];
1099                         }
1100
1101                         $lines = explode("\n",$serverret["header"]);
1102                         if (count($lines)) {
1103                                 foreach($lines as $line) {
1104                                         $line = trim($line);
1105                                         if (stristr($line,'X-Diaspora-Version:')) {
1106                                                 $platform = "Diaspora";
1107                                                 $version = trim(str_replace("X-Diaspora-Version:", "", $line));
1108                                                 $version = trim(str_replace("x-diaspora-version:", "", $version));
1109                                                 $network = NETWORK_DIASPORA;
1110                                                 $versionparts = explode("-", $version);
1111                                                 $version = $versionparts[0];
1112                                         }
1113
1114                                         if (stristr($line,'Server: Mastodon')) {
1115                                                 $platform = "Mastodon";
1116                                                 $network = NETWORK_OSTATUS;
1117                                         }
1118                                 }
1119                         }
1120                 }
1121         }
1122
1123         if (!$failure && ($poco == "")) {
1124                 // Test for Statusnet
1125                 // Will also return data for Friendica and GNU Social - but it will be overwritten later
1126                 // The "not implemented" is a special treatment for really, really old Friendica versions
1127                 $serverret = z_fetch_url($server_url."/api/statusnet/version.json");
1128                 if ($serverret["success"] && ($serverret["body"] != '{"error":"not implemented"}') &&
1129                         ($serverret["body"] != '') && (strlen($serverret["body"]) < 30)) {
1130                         $platform = "StatusNet";
1131                         // Remove junk that some GNU Social servers return
1132                         $version = str_replace(chr(239).chr(187).chr(191), "", $serverret["body"]);
1133                         $version = trim($version, '"');
1134                         $network = NETWORK_OSTATUS;
1135                 }
1136
1137                 // Test for GNU Social
1138                 $serverret = z_fetch_url($server_url."/api/gnusocial/version.json");
1139                 if ($serverret["success"] && ($serverret["body"] != '{"error":"not implemented"}') &&
1140                         ($serverret["body"] != '') && (strlen($serverret["body"]) < 30)) {
1141                         $platform = "GNU Social";
1142                         // Remove junk that some GNU Social servers return
1143                         $version = str_replace(chr(239).chr(187).chr(191), "", $serverret["body"]);
1144                         $version = trim($version, '"');
1145                         $network = NETWORK_OSTATUS;
1146                 }
1147
1148                 // Test for Mastodon
1149                 $orig_version = $version;
1150                 $serverret = z_fetch_url($server_url."/api/v1/instance");
1151                 if ($serverret["success"] && ($serverret["body"] != '')) {
1152                         $data = json_decode($serverret["body"]);
1153                         if (isset($data->version)) {
1154                                 $platform = "Mastodon";
1155                                 $version = $data->version;
1156                                 $site_name = $data->title;
1157                                 $info = $data->description;
1158                                 $network = NETWORK_OSTATUS;
1159                         }
1160                 }
1161                 if (strstr($orig_version.$version, 'Pleroma')) {
1162                         $platform = 'Pleroma';
1163                         $version = trim(str_replace('Pleroma', '', $version));
1164                 }
1165         }
1166
1167         if (!$failure) {
1168                 // Test for Hubzilla and Red
1169                 $serverret = z_fetch_url($server_url."/siteinfo.json");
1170                 if ($serverret["success"]) {
1171                         $data = json_decode($serverret["body"]);
1172                         if (isset($data->url)) {
1173                                 $platform = $data->platform;
1174                                 $version = $data->version;
1175                                 $network = NETWORK_DIASPORA;
1176                         }
1177                         if (!empty($data->site_name)) {
1178                                 $site_name = $data->site_name;
1179                         }
1180                         switch ($data->register_policy) {
1181                                 case "REGISTER_OPEN":
1182                                         $register_policy = REGISTER_OPEN;
1183                                         break;
1184                                 case "REGISTER_APPROVE":
1185                                         $register_policy = REGISTER_APPROVE;
1186                                         break;
1187                                 case "REGISTER_CLOSED":
1188                                 default:
1189                                         $register_policy = REGISTER_CLOSED;
1190                                         break;
1191                         }
1192                 } else {
1193                         // Test for Hubzilla, Redmatrix or Friendica
1194                         $serverret = z_fetch_url($server_url."/api/statusnet/config.json");
1195                         if ($serverret["success"]) {
1196                                 $data = json_decode($serverret["body"]);
1197                                 if (isset($data->site->server)) {
1198                                         if (isset($data->site->platform)) {
1199                                                 $platform = $data->site->platform->PLATFORM_NAME;
1200                                                 $version = $data->site->platform->STD_VERSION;
1201                                                 $network = NETWORK_DIASPORA;
1202                                         }
1203                                         if (isset($data->site->BlaBlaNet)) {
1204                                                 $platform = $data->site->BlaBlaNet->PLATFORM_NAME;
1205                                                 $version = $data->site->BlaBlaNet->STD_VERSION;
1206                                                 $network = NETWORK_DIASPORA;
1207                                         }
1208                                         if (isset($data->site->hubzilla)) {
1209                                                 $platform = $data->site->hubzilla->PLATFORM_NAME;
1210                                                 $version = $data->site->hubzilla->RED_VERSION;
1211                                                 $network = NETWORK_DIASPORA;
1212                                         }
1213                                         if (isset($data->site->redmatrix)) {
1214                                                 if (isset($data->site->redmatrix->PLATFORM_NAME)) {
1215                                                         $platform = $data->site->redmatrix->PLATFORM_NAME;
1216                                                 } elseif (isset($data->site->redmatrix->RED_PLATFORM)) {
1217                                                         $platform = $data->site->redmatrix->RED_PLATFORM;
1218                                                 }
1219
1220                                                 $version = $data->site->redmatrix->RED_VERSION;
1221                                                 $network = NETWORK_DIASPORA;
1222                                         }
1223                                         if (isset($data->site->friendica)) {
1224                                                 $platform = $data->site->friendica->FRIENDICA_PLATFORM;
1225                                                 $version = $data->site->friendica->FRIENDICA_VERSION;
1226                                                 $network = NETWORK_DFRN;
1227                                         }
1228
1229                                         $site_name = $data->site->name;
1230
1231                                         $data->site->closed = poco_to_boolean($data->site->closed);
1232                                         $data->site->private = poco_to_boolean($data->site->private);
1233                                         $data->site->inviteonly = poco_to_boolean($data->site->inviteonly);
1234
1235                                         if (!$data->site->closed && !$data->site->private and $data->site->inviteonly) {
1236                                                 $register_policy = REGISTER_APPROVE;
1237                                         } elseif (!$data->site->closed && !$data->site->private) {
1238                                                 $register_policy = REGISTER_OPEN;
1239                                         } else {
1240                                                 $register_policy = REGISTER_CLOSED;
1241                                         }
1242                                 }
1243                         }
1244                 }
1245         }
1246
1247         // Query statistics.json. Optional package for Diaspora, Friendica and Redmatrix
1248         if (!$failure) {
1249                 $serverret = z_fetch_url($server_url."/statistics.json");
1250                 if ($serverret["success"]) {
1251                         $data = json_decode($serverret["body"]);
1252                         if (isset($data->version)) {
1253                                 $version = $data->version;
1254                                 // Version numbers on statistics.json are presented with additional info, e.g.:
1255                                 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
1256                                 $version = preg_replace("=(.+)-(.{4,})=ism", "$1", $version);
1257                         }
1258
1259                         if (!empty($data->name)) {
1260                                 $site_name = $data->name;
1261                         }
1262
1263                         if (!empty($data->network)) {
1264                                 $platform = $data->network;
1265                         }
1266
1267                         if ($platform == "Diaspora") {
1268                                 $network = NETWORK_DIASPORA;
1269                         }
1270
1271                         if ($data->registrations_open) {
1272                                 $register_policy = REGISTER_OPEN;
1273                         } else {
1274                                 $register_policy = REGISTER_CLOSED;
1275                         }
1276                 }
1277         }
1278
1279         // Query nodeinfo. Working for (at least) Diaspora and Friendica.
1280         if (!$failure) {
1281                 $server = poco_fetch_nodeinfo($server_url);
1282                 if ($server) {
1283                         $register_policy = $server['register_policy'];
1284
1285                         if (isset($server['platform'])) {
1286                                 $platform = $server['platform'];
1287                         }
1288
1289                         if (isset($server['network'])) {
1290                                 $network = $server['network'];
1291                         }
1292
1293                         if (isset($server['version'])) {
1294                                 $version = $server['version'];
1295                         }
1296
1297                         if (isset($server['site_name'])) {
1298                                 $site_name = $server['site_name'];
1299                         }
1300                 }
1301         }
1302
1303         // Check for noscrape
1304         // Friendica servers could be detected as OStatus servers
1305         if (!$failure && in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS))) {
1306                 $serverret = z_fetch_url($server_url."/friendica/json");
1307
1308                 if (!$serverret["success"]) {
1309                         $serverret = z_fetch_url($server_url."/friendika/json");
1310                 }
1311
1312                 if ($serverret["success"]) {
1313                         $data = json_decode($serverret["body"]);
1314
1315                         if (isset($data->version)) {
1316                                 $network = NETWORK_DFRN;
1317
1318                                 $noscrape = $data->no_scrape_url;
1319                                 $version = $data->version;
1320                                 $site_name = $data->site_name;
1321                                 $info = $data->info;
1322                                 $register_policy_str = $data->register_policy;
1323                                 $platform = $data->platform;
1324
1325                                 switch ($register_policy_str) {
1326                                         case "REGISTER_CLOSED":
1327                                                 $register_policy = REGISTER_CLOSED;
1328                                                 break;
1329                                         case "REGISTER_APPROVE":
1330                                                 $register_policy = REGISTER_APPROVE;
1331                                                 break;
1332                                         case "REGISTER_OPEN":
1333                                                 $register_policy = REGISTER_OPEN;
1334                                                 break;
1335                                 }
1336                         }
1337                 }
1338         }
1339
1340         if ($possible_failure && !$failure) {
1341                 $failure = true;
1342         }
1343
1344         if ($failure) {
1345                 $last_contact = $orig_last_contact;
1346                 $last_failure = datetime_convert();
1347         } else {
1348                 $last_contact = datetime_convert();
1349                 $last_failure = $orig_last_failure;
1350         }
1351
1352         if (($last_contact <= $last_failure) && !$failure) {
1353                 logger("Server ".$server_url." seems to be alive, but last contact wasn't set - could be a bug", LOGGER_DEBUG);
1354         } elseif (($last_contact >= $last_failure) && $failure) {
1355                 logger("Server ".$server_url." seems to be dead, but last failure wasn't set - could be a bug", LOGGER_DEBUG);
1356         }
1357
1358         // Check again if the server exists
1359         $servers = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
1360
1361         $version = strip_tags($version);
1362         $site_name = strip_tags($site_name);
1363         $info = strip_tags($info);
1364         $platform = strip_tags($platform);
1365
1366         if ($servers) {
1367                  q("UPDATE `gserver` SET `url` = '%s', `version` = '%s', `site_name` = '%s', `info` = '%s', `register_policy` = %d, `poco` = '%s', `noscrape` = '%s',
1368                         `network` = '%s', `platform` = '%s', `last_contact` = '%s', `last_failure` = '%s' WHERE `nurl` = '%s'",
1369                         dbesc($server_url),
1370                         dbesc($version),
1371                         dbesc($site_name),
1372                         dbesc($info),
1373                         intval($register_policy),
1374                         dbesc($poco),
1375                         dbesc($noscrape),
1376                         dbesc($network),
1377                         dbesc($platform),
1378                         dbesc($last_contact),
1379                         dbesc($last_failure),
1380                         dbesc(normalise_link($server_url))
1381                 );
1382         } elseif (!$failure) {
1383                 q("INSERT INTO `gserver` (`url`, `nurl`, `version`, `site_name`, `info`, `register_policy`, `poco`, `noscrape`, `network`, `platform`, `created`, `last_contact`, `last_failure`)
1384                                         VALUES ('%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
1385                                 dbesc($server_url),
1386                                 dbesc(normalise_link($server_url)),
1387                                 dbesc($version),
1388                                 dbesc($site_name),
1389                                 dbesc($info),
1390                                 intval($register_policy),
1391                                 dbesc($poco),
1392                                 dbesc($noscrape),
1393                                 dbesc($network),
1394                                 dbesc($platform),
1395                                 dbesc(datetime_convert()),
1396                                 dbesc($last_contact),
1397                                 dbesc($last_failure),
1398                                 dbesc(datetime_convert())
1399                 );
1400         }
1401         logger("End discovery for server " . $server_url, LOGGER_DEBUG);
1402
1403         return !$failure;
1404 }
1405
1406 function count_common_friends($uid, $cid) {
1407
1408         $r = q("SELECT count(*) as `total`
1409                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1410                 WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND
1411                 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
1412                 AND `gcontact`.`nurl` IN (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
1413                 intval($cid),
1414                 intval($uid),
1415                 intval($uid),
1416                 intval($cid)
1417         );
1418
1419         // logger("count_common_friends: $uid $cid {$r[0]['total']}");
1420         if (dbm::is_result($r)) {
1421                 return $r[0]['total'];
1422         }
1423         return 0;
1424
1425 }
1426
1427
1428 function common_friends($uid, $cid, $start = 0, $limit = 9999, $shuffle = false) {
1429
1430         if ($shuffle) {
1431                 $sql_extra = " order by rand() ";
1432         } else {
1433                 $sql_extra = " order by `gcontact`.`name` asc ";
1434         }
1435
1436         $r = q("SELECT `gcontact`.*, `contact`.`id` AS `cid`
1437                 FROM `glink`
1438                 INNER JOIN `gcontact` ON `glink`.`gcid` = `gcontact`.`id`
1439                 INNER JOIN `contact` ON `gcontact`.`nurl` = `contact`.`nurl`
1440                 WHERE `glink`.`cid` = %d and `glink`.`uid` = %d
1441                         AND `contact`.`uid` = %d AND `contact`.`self` = 0 AND `contact`.`blocked` = 0
1442                         AND `contact`.`hidden` = 0 AND `contact`.`id` != %d
1443                         AND ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
1444                         $sql_extra LIMIT %d, %d",
1445                 intval($cid),
1446                 intval($uid),
1447                 intval($uid),
1448                 intval($cid),
1449                 intval($start),
1450                 intval($limit)
1451         );
1452
1453         /// @TODO Check all calling-findings of this function if they properly use dbm::is_result()
1454         return $r;
1455
1456 }
1457
1458
1459 function count_common_friends_zcid($uid, $zcid) {
1460
1461         $r = q("SELECT count(*) as `total`
1462                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1463                 where `glink`.`zcid` = %d
1464                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
1465                 intval($zcid),
1466                 intval($uid)
1467         );
1468
1469         if (dbm::is_result($r)) {
1470                 return $r[0]['total'];
1471         }
1472         return 0;
1473
1474 }
1475
1476 function common_friends_zcid($uid, $zcid, $start = 0, $limit = 9999, $shuffle = false) {
1477
1478         if ($shuffle) {
1479                 $sql_extra = " order by rand() ";
1480         } else {
1481                 $sql_extra = " order by `gcontact`.`name` asc ";
1482         }
1483
1484         $r = q("SELECT `gcontact`.*
1485                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1486                 where `glink`.`zcid` = %d
1487                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
1488                 $sql_extra limit %d, %d",
1489                 intval($zcid),
1490                 intval($uid),
1491                 intval($start),
1492                 intval($limit)
1493         );
1494
1495         /// @TODO Check all calling-findings of this function if they properly use dbm::is_result()
1496         return $r;
1497
1498 }
1499
1500
1501 function count_all_friends($uid, $cid) {
1502
1503         $r = q("SELECT count(*) as `total`
1504                 FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1505                 where `glink`.`cid` = %d and `glink`.`uid` = %d AND
1506                 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))",
1507                 intval($cid),
1508                 intval($uid)
1509         );
1510
1511         if (dbm::is_result($r)) {
1512                 return $r[0]['total'];
1513         }
1514         return 0;
1515
1516 }
1517
1518
1519 function all_friends($uid, $cid, $start = 0, $limit = 80) {
1520
1521         $r = q("SELECT `gcontact`.*, `contact`.`id` AS `cid`
1522                 FROM `glink`
1523                 INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
1524                 LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl` AND `contact`.`uid` = %d
1525                 WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND
1526                 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
1527                 ORDER BY `gcontact`.`name` ASC LIMIT %d, %d ",
1528                 intval($uid),
1529                 intval($cid),
1530                 intval($uid),
1531                 intval($start),
1532                 intval($limit)
1533         );
1534
1535         /// @TODO Check all calling-findings of this function if they properly use dbm::is_result()
1536         return $r;
1537 }
1538
1539
1540
1541 function suggestion_query($uid, $start = 0, $limit = 80) {
1542
1543         if (!$uid) {
1544                 return array();
1545         }
1546
1547         /*
1548          * Uncommented because the result of the queries are to big to store it in the cache.
1549          * We need to decide if we want to change the db column type or if we want to delete it.
1550          */
1551         //$list = Cache::get("suggestion_query:".$uid.":".$start.":".$limit);
1552         //if (!is_null($list)) {
1553         //      return $list;
1554         //}
1555
1556         $network = array(NETWORK_DFRN);
1557
1558         if (Config::get('system','diaspora_enabled')) {
1559                 $network[] = NETWORK_DIASPORA;
1560         }
1561
1562         if (!Config::get('system','ostatus_disabled')) {
1563                 $network[] = NETWORK_OSTATUS;
1564         }
1565
1566         $sql_network = implode("', '", $network);
1567         $sql_network = "'".$sql_network."'";
1568
1569         /// @todo This query is really slow
1570         // By now we cache the data for five minutes
1571         $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
1572                 INNER JOIN `glink` ON `glink`.`gcid` = `gcontact`.`id`
1573                 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
1574                 AND NOT `gcontact`.`name` IN (SELECT `name` FROM `contact` WHERE `uid` = %d)
1575                 AND NOT `gcontact`.`id` IN (SELECT `gcid` FROM `gcign` WHERE `uid` = %d)
1576                 AND `gcontact`.`updated` >= '%s'
1577                 AND `gcontact`.`last_contact` >= `gcontact`.`last_failure`
1578                 AND `gcontact`.`network` IN (%s)
1579                 GROUP BY `glink`.`gcid` ORDER BY `gcontact`.`updated` DESC,`total` DESC LIMIT %d, %d",
1580                 intval($uid),
1581                 intval($uid),
1582                 intval($uid),
1583                 intval($uid),
1584                 dbesc(NULL_DATE),
1585                 $sql_network,
1586                 intval($start),
1587                 intval($limit)
1588         );
1589
1590         if (dbm::is_result($r) && count($r) >= ($limit -1)) {
1591                 /*
1592                  * Uncommented because the result of the queries are to big to store it in the cache.
1593                  * We need to decide if we want to change the db column type or if we want to delete it.
1594                  */
1595                 //Cache::set("suggestion_query:".$uid.":".$start.":".$limit, $r, CACHE_FIVE_MINUTES);
1596
1597                 return $r;
1598         }
1599
1600         $r2 = q("SELECT gcontact.* FROM gcontact
1601                 INNER JOIN `glink` ON `glink`.`gcid` = `gcontact`.`id`
1602                 WHERE `glink`.`uid` = 0 AND `glink`.`cid` = 0 AND `glink`.`zcid` = 0 AND NOT `gcontact`.`nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = %d)
1603                 AND NOT `gcontact`.`name` IN (SELECT `name` FROM `contact` WHERE `uid` = %d)
1604                 AND NOT `gcontact`.`id` IN (SELECT `gcid` FROM `gcign` WHERE `uid` = %d)
1605                 AND `gcontact`.`updated` >= '%s'
1606                 AND `gcontact`.`last_contact` >= `gcontact`.`last_failure`
1607                 AND `gcontact`.`network` IN (%s)
1608                 ORDER BY rand() LIMIT %d, %d",
1609                 intval($uid),
1610                 intval($uid),
1611                 intval($uid),
1612                 dbesc(NULL_DATE),
1613                 $sql_network,
1614                 intval($start),
1615                 intval($limit)
1616         );
1617
1618         $list = array();
1619         foreach ($r2 as $suggestion) {
1620                 $list[$suggestion["nurl"]] = $suggestion;
1621         }
1622
1623         foreach ($r as $suggestion) {
1624                 $list[$suggestion["nurl"]] = $suggestion;
1625         }
1626
1627         while (sizeof($list) > ($limit)) {
1628                 array_pop($list);
1629         }
1630
1631         /*
1632          * Uncommented because the result of the queries are to big to store it in the cache.
1633          * We need to decide if we want to change the db column type or if we want to delete it.
1634          */
1635         //Cache::set("suggestion_query:".$uid.":".$start.":".$limit, $list, CACHE_FIVE_MINUTES);
1636         return $list;
1637 }
1638
1639 function update_suggestions() {
1640
1641         $a = get_app();
1642
1643         $done = array();
1644
1645         /// @TODO Check if it is really neccessary to poll the own server
1646         poco_load(0, 0, 0, System::baseUrl() . '/poco');
1647
1648         $done[] = System::baseUrl() . '/poco';
1649
1650         if (strlen(Config::get('system','directory'))) {
1651                 $x = fetch_url(get_server()."/pubsites");
1652                 if ($x) {
1653                         $j = json_decode($x);
1654                         if ($j->entries) {
1655                                 foreach ($j->entries as $entry) {
1656
1657                                         poco_check_server($entry->url);
1658
1659                                         $url = $entry->url . '/poco';
1660                                         if (! in_array($url,$done)) {
1661                                                 poco_load(0,0,0,$entry->url . '/poco');
1662                                         }
1663                                 }
1664                         }
1665                 }
1666         }
1667
1668         // Query your contacts from Friendica and Redmatrix/Hubzilla for their contacts
1669         $r = q("SELECT DISTINCT(`poco`) AS `poco` FROM `contact` WHERE `network` IN ('%s', '%s')",
1670                 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA)
1671         );
1672
1673         if (dbm::is_result($r)) {
1674                 foreach ($r as $rr) {
1675                         $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
1676                         if (! in_array($base,$done)) {
1677                                 poco_load(0,0,0,$base);
1678                         }
1679                 }
1680         }
1681 }
1682
1683 /**
1684  * @brief Fetch server list from remote servers and adds them when they are new.
1685  *
1686  * @param string $poco URL to the POCO endpoint
1687  */
1688 function poco_fetch_serverlist($poco) {
1689         $serverret = z_fetch_url($poco."/@server");
1690         if (!$serverret["success"]) {
1691                 return;
1692         }
1693         $serverlist = json_decode($serverret['body']);
1694
1695         if (!is_array($serverlist)) {
1696                 return;
1697         }
1698
1699         foreach ($serverlist as $server) {
1700                 $server_url = str_replace("/index.php", "", $server->url);
1701
1702                 $r = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
1703                 if (!dbm::is_result($r)) {
1704                         logger("Call server check for server ".$server_url, LOGGER_DEBUG);
1705                         Worker::add(PRIORITY_LOW, "discover_poco", "server", $server_url);
1706                 }
1707         }
1708 }
1709
1710 function poco_discover_federation() {
1711         $last = Config::get('poco','last_federation_discovery');
1712
1713         if ($last) {
1714                 $next = $last + (24 * 60 * 60);
1715                 if ($next > time()) {
1716                         return;
1717                 }
1718         }
1719
1720         // Discover Friendica, Hubzilla and Diaspora servers
1721         $serverdata = fetch_url("http://the-federation.info/pods.json");
1722
1723         if ($serverdata) {
1724                 $servers = json_decode($serverdata);
1725
1726                 foreach ($servers->pods as $server) {
1727                         Worker::add(PRIORITY_LOW, "discover_poco", "server", "https://".$server->host);
1728                 }
1729         }
1730
1731         // Disvover Mastodon servers
1732         if (!Config::get('system','ostatus_disabled')) {
1733                 $serverdata = fetch_url("https://instances.mastodon.xyz/instances.json");
1734
1735                 if ($serverdata) {
1736                         $servers = json_decode($serverdata);
1737
1738                         foreach ($servers as $server) {
1739                                 $url = (is_null($server->https_score) ? 'http' : 'https').'://'.$server->name;
1740                                 Worker::add(PRIORITY_LOW, "discover_poco", "server", $url);
1741                         }
1742                 }
1743         }
1744
1745         // Currently disabled, since the service isn't available anymore.
1746         // It is not removed since I hope that there will be a successor.
1747         // Discover GNU Social Servers.
1748         //if (!Config::get('system','ostatus_disabled')) {
1749         //      $serverdata = "http://gstools.org/api/get_open_instances/";
1750
1751         //      $result = z_fetch_url($serverdata);
1752         //      if ($result["success"]) {
1753         //              $servers = json_decode($result["body"]);
1754
1755         //              foreach($servers->data as $server)
1756         //                      poco_check_server($server->instance_address);
1757         //      }
1758         //}
1759
1760         Config::set('poco','last_federation_discovery', time());
1761 }
1762
1763 function poco_discover_single_server($id) {
1764         $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `id` = %d", intval($id));
1765         if (!dbm::is_result($r)) {
1766                 return false;
1767         }
1768
1769         $server = $r[0];
1770
1771         // Discover new servers out there (Works from Friendica version 3.5.2)
1772         poco_fetch_serverlist($server["poco"]);
1773
1774         // Fetch all users from the other server
1775         $url = $server["poco"]."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1776
1777         logger("Fetch all users from the server ".$server["url"], LOGGER_DEBUG);
1778
1779         $retdata = z_fetch_url($url);
1780         if ($retdata["success"]) {
1781                 $data = json_decode($retdata["body"]);
1782
1783                 poco_discover_server($data, 2);
1784
1785                 if (Config::get('system','poco_discovery') > 1) {
1786
1787                         $timeframe = Config::get('system','poco_discovery_since');
1788                         if ($timeframe == 0) {
1789                                 $timeframe = 30;
1790                         }
1791
1792                         $updatedSince = date("Y-m-d H:i:s", time() - $timeframe * 86400);
1793
1794                         // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3)
1795                         $url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1796
1797                         $success = false;
1798
1799                         $retdata = z_fetch_url($url);
1800                         if ($retdata["success"]) {
1801                                 logger("Fetch all global contacts from the server ".$server["nurl"], LOGGER_DEBUG);
1802                                 $success = poco_discover_server(json_decode($retdata["body"]));
1803                         }
1804
1805                         if (!$success && (Config::get('system','poco_discovery') > 2)) {
1806                                 logger("Fetch contacts from users of the server ".$server["nurl"], LOGGER_DEBUG);
1807                                 poco_discover_server_users($data, $server);
1808                         }
1809                 }
1810
1811                 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
1812
1813                 return true;
1814         } else {
1815                 // If the server hadn't replied correctly, then force a sanity check
1816                 poco_check_server($server["url"], $server["network"], true);
1817
1818                 // If we couldn't reach the server, we will try it some time later
1819                 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
1820
1821                 return false;
1822         }
1823 }
1824
1825 function poco_discover($complete = false) {
1826
1827         // Update the server list
1828         poco_discover_federation();
1829
1830         $no_of_queries = 5;
1831
1832         $requery_days = intval(Config::get("system", "poco_requery_days"));
1833
1834         if ($requery_days == 0) {
1835                 $requery_days = 7;
1836         }
1837         $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
1838
1839         $r = q("SELECT `id`, `url`, `network` FROM `gserver` WHERE `last_contact` >= `last_failure` AND `poco` != '' AND `last_poco_query` < '%s' ORDER BY RAND()", dbesc($last_update));
1840         if (dbm::is_result($r)) {
1841                 foreach ($r as $server) {
1842
1843                         if (!poco_check_server($server["url"], $server["network"])) {
1844                                 // The server is not reachable? Okay, then we will try it later
1845                                 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
1846                                 continue;
1847                         }
1848
1849                         logger('Update directory from server '.$server['url'].' with ID '.$server['id'], LOGGER_DEBUG);
1850                         Worker::add(PRIORITY_LOW, "discover_poco", "update_server_directory", (int)$server['id']);
1851
1852                         if (!$complete && (--$no_of_queries == 0)) {
1853                                 break;
1854                         }
1855                 }
1856         }
1857 }
1858
1859 function poco_discover_server_users($data, $server) {
1860
1861         if (!isset($data->entry)) {
1862                 return;
1863         }
1864
1865         foreach ($data->entry as $entry) {
1866                 $username = "";
1867                 if (isset($entry->urls)) {
1868                         foreach ($entry->urls as $url) {
1869                                 if ($url->type == 'profile') {
1870                                         $profile_url = $url->value;
1871                                         $urlparts = parse_url($profile_url);
1872                                         $username = end(explode("/", $urlparts["path"]));
1873                                 }
1874                         }
1875                 }
1876                 if ($username != "") {
1877                         logger("Fetch contacts for the user ".$username." from the server ".$server["nurl"], LOGGER_DEBUG);
1878
1879                         // Fetch all contacts from a given user from the other server
1880                         $url = $server["poco"]."/".$username."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1881
1882                         $retdata = z_fetch_url($url);
1883                         if ($retdata["success"]) {
1884                                 poco_discover_server(json_decode($retdata["body"]), 3);
1885                         }
1886                 }
1887         }
1888 }
1889
1890 function poco_discover_server($data, $default_generation = 0) {
1891
1892         if (!isset($data->entry) || !count($data->entry)) {
1893                 return false;
1894         }
1895
1896         $success = false;
1897
1898         foreach ($data->entry as $entry) {
1899                 $profile_url = '';
1900                 $profile_photo = '';
1901                 $connect_url = '';
1902                 $name = '';
1903                 $network = '';
1904                 $updated = NULL_DATE;
1905                 $location = '';
1906                 $about = '';
1907                 $keywords = '';
1908                 $gender = '';
1909                 $contact_type = -1;
1910                 $generation = $default_generation;
1911
1912                 $name = $entry->displayName;
1913
1914                 if (isset($entry->urls)) {
1915                         foreach ($entry->urls as $url) {
1916                                 if ($url->type == 'profile') {
1917                                         $profile_url = $url->value;
1918                                         continue;
1919                                 }
1920                                 if ($url->type == 'webfinger') {
1921                                         $connect_url = str_replace('acct:' , '', $url->value);
1922                                         continue;
1923                                 }
1924                         }
1925                 }
1926
1927                 if (isset($entry->photos)) {
1928                         foreach ($entry->photos as $photo) {
1929                                 if ($photo->type == 'profile') {
1930                                         $profile_photo = $photo->value;
1931                                         continue;
1932                                 }
1933                         }
1934                 }
1935
1936                 if (isset($entry->updated)) {
1937                         $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
1938                 }
1939
1940                 if (isset($entry->network)) {
1941                         $network = $entry->network;
1942                 }
1943
1944                 if (isset($entry->currentLocation)) {
1945                         $location = $entry->currentLocation;
1946                 }
1947
1948                 if (isset($entry->aboutMe)) {
1949                         $about = html2bbcode($entry->aboutMe);
1950                 }
1951
1952                 if (isset($entry->gender)) {
1953                         $gender = $entry->gender;
1954                 }
1955
1956                 if(isset($entry->generation) && ($entry->generation > 0)) {
1957                         $generation = ++$entry->generation;
1958                 }
1959
1960                 if(isset($entry->contactType) && ($entry->contactType >= 0)) {
1961                         $contact_type = $entry->contactType;
1962                 }
1963
1964                 if (isset($entry->tags)) {
1965                         foreach ($entry->tags as $tag) {
1966                                 $keywords = implode(", ", $tag);
1967                         }
1968                 }
1969
1970                 if ($generation > 0) {
1971                         $success = true;
1972
1973                         logger("Store profile ".$profile_url, LOGGER_DEBUG);
1974
1975                         $gcontact = array("url" => $profile_url,
1976                                         "name" => $name,
1977                                         "network" => $network,
1978                                         "photo" => $profile_photo,
1979                                         "about" => $about,
1980                                         "location" => $location,
1981                                         "gender" => $gender,
1982                                         "keywords" => $keywords,
1983                                         "connect" => $connect_url,
1984                                         "updated" => $updated,
1985                                         "contact-type" => $contact_type,
1986                                         "generation" => $generation);
1987
1988                         try {
1989                                 $gcontact = sanitize_gcontact($gcontact);
1990                                 update_gcontact($gcontact);
1991                         } catch (Exception $e) {
1992                                 logger($e->getMessage(), LOGGER_DEBUG);
1993                         }
1994
1995                         logger("Done for profile ".$profile_url, LOGGER_DEBUG);
1996                 }
1997         }
1998         return $success;
1999 }
2000
2001 /**
2002  * @brief Removes unwanted parts from a contact url
2003  *
2004  * @param string $url Contact url
2005  * @return string Contact url with the wanted parts
2006  */
2007 function clean_contact_url($url) {
2008         $parts = parse_url($url);
2009
2010         if (!isset($parts["scheme"]) || !isset($parts["host"])) {
2011                 return $url;
2012         }
2013
2014         $new_url = $parts["scheme"]."://".$parts["host"];
2015
2016         if (isset($parts["port"])) {
2017                 $new_url .= ":".$parts["port"];
2018         }
2019
2020         if (isset($parts["path"])) {
2021                 $new_url .= $parts["path"];
2022         }
2023
2024         if ($new_url != $url) {
2025                 logger("Cleaned contact url ".$url." to ".$new_url." - Called by: ".System::callstack(), LOGGER_DEBUG);
2026         }
2027
2028         return $new_url;
2029 }
2030
2031 /**
2032  * @brief Replace alternate OStatus user format with the primary one
2033  *
2034  * @param arr $contact contact array (called by reference)
2035  */
2036 function fix_alternate_contact_address(&$contact) {
2037         if (($contact["network"] == NETWORK_OSTATUS) && poco_alternate_ostatus_url($contact["url"])) {
2038                 $data = Probe::uri($contact["url"]);
2039                 if ($contact["network"] == NETWORK_OSTATUS) {
2040                         logger("Fix primary url from ".$contact["url"]." to ".$data["url"]." - Called by: ".System::callstack(), LOGGER_DEBUG);
2041                         $contact["url"] = $data["url"];
2042                         $contact["addr"] = $data["addr"];
2043                         $contact["alias"] = $data["alias"];
2044                         $contact["server_url"] = $data["baseurl"];
2045                 }
2046         }
2047 }
2048
2049 /**
2050  * @brief Fetch the gcontact id, add an entry if not existed
2051  *
2052  * @param arr $contact contact array
2053  * @return bool|int Returns false if not found, integer if contact was found
2054  */
2055 function get_gcontact_id($contact) {
2056
2057         $gcontact_id = 0;
2058         $doprobing = false;
2059
2060         if (in_array($contact["network"], array(NETWORK_PHANTOM))) {
2061                 logger("Invalid network for contact url ".$contact["url"]." - Called by: ".System::callstack(), LOGGER_DEBUG);
2062                 return false;
2063         }
2064
2065         if ($contact["network"] == NETWORK_STATUSNET) {
2066                 $contact["network"] = NETWORK_OSTATUS;
2067         }
2068
2069         // All new contacts are hidden by default
2070         if (!isset($contact["hide"])) {
2071                 $contact["hide"] = true;
2072         }
2073
2074         // Replace alternate OStatus user format with the primary one
2075         fix_alternate_contact_address($contact);
2076
2077         // Remove unwanted parts from the contact url (e.g. "?zrl=...")
2078         if (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) {
2079                 $contact["url"] = clean_contact_url($contact["url"]);
2080         }
2081
2082         dba::lock('gcontact');
2083         $r = q("SELECT `id`, `last_contact`, `last_failure`, `network` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
2084                 dbesc(normalise_link($contact["url"])));
2085
2086         if (dbm::is_result($r)) {
2087                 $gcontact_id = $r[0]["id"];
2088
2089                 // Update every 90 days
2090                 if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) {
2091                         $last_failure_str = $r[0]["last_failure"];
2092                         $last_failure = strtotime($r[0]["last_failure"]);
2093                         $last_contact_str = $r[0]["last_contact"];
2094                         $last_contact = strtotime($r[0]["last_contact"]);
2095                         $doprobing = (((time() - $last_contact) > (90 * 86400)) && ((time() - $last_failure) > (90 * 86400)));
2096                 }
2097         } else {
2098                 q("INSERT INTO `gcontact` (`name`, `nick`, `addr` , `network`, `url`, `nurl`, `photo`, `created`, `updated`, `location`, `about`, `hide`, `generation`)
2099                         VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d)",
2100                         dbesc($contact["name"]),
2101                         dbesc($contact["nick"]),
2102                         dbesc($contact["addr"]),
2103                         dbesc($contact["network"]),
2104                         dbesc($contact["url"]),
2105                         dbesc(normalise_link($contact["url"])),
2106                         dbesc($contact["photo"]),
2107                         dbesc(datetime_convert()),
2108                         dbesc(datetime_convert()),
2109                         dbesc($contact["location"]),
2110                         dbesc($contact["about"]),
2111                         intval($contact["hide"]),
2112                         intval($contact["generation"])
2113                 );
2114
2115                 $r = q("SELECT `id`, `network` FROM `gcontact` WHERE `nurl` = '%s' ORDER BY `id` LIMIT 2",
2116                         dbesc(normalise_link($contact["url"])));
2117
2118                 if (dbm::is_result($r)) {
2119                         $gcontact_id = $r[0]["id"];
2120
2121                         $doprobing = in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""));
2122                 }
2123         }
2124         dba::unlock();
2125
2126         if ($doprobing) {
2127                 logger("Last Contact: ". $last_contact_str." - Last Failure: ".$last_failure_str." - Checking: ".$contact["url"], LOGGER_DEBUG);
2128                 Worker::add(PRIORITY_LOW, 'gprobe', $contact["url"]);
2129         }
2130
2131         return $gcontact_id;
2132 }
2133
2134 /**
2135  * @brief Updates the gcontact table from a given array
2136  *
2137  * @param arr $contact contact array
2138  * @return bool|int Returns false if not found, integer if contact was found
2139  */
2140 function update_gcontact($contact) {
2141
2142         // Check for invalid "contact-type" value
2143         if (isset($contact['contact-type']) && (intval($contact['contact-type']) < 0)) {
2144                 $contact['contact-type'] = 0;
2145         }
2146
2147         /// @todo update contact table as well
2148
2149         $gcontact_id = get_gcontact_id($contact);
2150
2151         if (!$gcontact_id) {
2152                 return false;
2153         }
2154
2155         $r = q("SELECT `name`, `nick`, `photo`, `location`, `about`, `addr`, `generation`, `birthday`, `gender`, `keywords`,
2156                         `contact-type`, `hide`, `nsfw`, `network`, `alias`, `notify`, `server_url`, `connect`, `updated`, `url`
2157                 FROM `gcontact` WHERE `id` = %d LIMIT 1",
2158                 intval($gcontact_id));
2159
2160         // Get all field names
2161         $fields = array();
2162         foreach ($r[0] as $field => $data) {
2163                 $fields[$field] = $data;
2164         }
2165
2166         unset($fields["url"]);
2167         unset($fields["updated"]);
2168         unset($fields["hide"]);
2169
2170         // Bugfix: We had an error in the storing of keywords which lead to the "0"
2171         // This value is still transmitted via poco.
2172         if ($contact["keywords"] == "0") {
2173                 unset($contact["keywords"]);
2174         }
2175
2176         if ($r[0]["keywords"] == "0") {
2177                 $r[0]["keywords"] = "";
2178         }
2179
2180         // assign all unassigned fields from the database entry
2181         foreach ($fields as $field => $data) {
2182                 if (!isset($contact[$field]) || ($contact[$field] == "")) {
2183                         $contact[$field] = $r[0][$field];
2184                 }
2185         }
2186
2187         if (!isset($contact["hide"])) {
2188                 $contact["hide"] = $r[0]["hide"];
2189         }
2190
2191         $fields["hide"] = $r[0]["hide"];
2192
2193         if ($contact["network"] == NETWORK_STATUSNET) {
2194                 $contact["network"] = NETWORK_OSTATUS;
2195         }
2196
2197         // Replace alternate OStatus user format with the primary one
2198         fix_alternate_contact_address($contact);
2199
2200         if (!isset($contact["updated"])) {
2201                 $contact["updated"] = dbm::date();
2202         }
2203
2204         if ($contact["network"] == NETWORK_TWITTER) {
2205                 $contact["server_url"] = 'http://twitter.com';
2206         }
2207
2208         if ($contact["server_url"] == "") {
2209                 $data = Probe::uri($contact["url"]);
2210                 if ($data["network"] != NETWORK_PHANTOM) {
2211                         $contact["server_url"] = $data['baseurl'];
2212                 }
2213         } else {
2214                 $contact["server_url"] = normalise_link($contact["server_url"]);
2215         }
2216
2217         if (($contact["addr"] == "") && ($contact["server_url"] != "") && ($contact["nick"] != "")) {
2218                 $hostname = str_replace("http://", "", $contact["server_url"]);
2219                 $contact["addr"] = $contact["nick"]."@".$hostname;
2220         }
2221
2222         // Check if any field changed
2223         $update = false;
2224         unset($fields["generation"]);
2225
2226         if ((($contact["generation"] > 0) && ($contact["generation"] <= $r[0]["generation"])) || ($r[0]["generation"] == 0)) {
2227                 foreach ($fields as $field => $data) {
2228                         if ($contact[$field] != $r[0][$field]) {
2229                                 logger("Difference for contact ".$contact["url"]." in field '".$field."'. New value: '".$contact[$field]."', old value '".$r[0][$field]."'", LOGGER_DEBUG);
2230                                 $update = true;
2231                         }
2232                 }
2233
2234                 if ($contact["generation"] < $r[0]["generation"]) {
2235                         logger("Difference for contact ".$contact["url"]." in field 'generation'. new value: '".$contact["generation"]."', old value '".$r[0]["generation"]."'", LOGGER_DEBUG);
2236                         $update = true;
2237                 }
2238         }
2239
2240         if ($update) {
2241                 logger("Update gcontact for ".$contact["url"], LOGGER_DEBUG);
2242                 $condition = array('`nurl` = ? AND (`generation` = 0 OR `generation` >= ?)',
2243                                 normalise_link($contact["url"]), $contact["generation"]);
2244                 $contact["updated"] = dbm::date($contact["updated"]);
2245
2246                 $updated = array('photo' => $contact['photo'], 'name' => $contact['name'],
2247                                 'nick' => $contact['nick'], 'addr' => $contact['addr'],
2248                                 'network' => $contact['network'], 'birthday' => $contact['birthday'],
2249                                 'gender' => $contact['gender'], 'keywords' => $contact['keywords'],
2250                                 'hide' => $contact['hide'], 'nsfw' => $contact['nsfw'],
2251                                 'contact-type' => $contact['contact-type'], 'alias' => $contact['alias'],
2252                                 'notify' => $contact['notify'], 'url' => $contact['url'],
2253                                 'location' => $contact['location'], 'about' => $contact['about'],
2254                                 'generation' => $contact['generation'], 'updated' => $contact['updated'],
2255                                 'server_url' => $contact['server_url'], 'connect' => $contact['connect']);
2256
2257                 dba::update('gcontact', $updated, $condition, $fields);
2258
2259                 // Now update the contact entry with the user id "0" as well.
2260                 // This is used for the shadow copies of public items.
2261                 $r = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = 0 ORDER BY `id` LIMIT 1",
2262                         dbesc(normalise_link($contact["url"])));
2263
2264                 if (dbm::is_result($r)) {
2265                         logger("Update public contact ".$r[0]["id"], LOGGER_DEBUG);
2266
2267                         update_contact_avatar($contact["photo"], 0, $r[0]["id"]);
2268
2269                         $fields = array('name', 'nick', 'addr',
2270                                         'network', 'bd', 'gender',
2271                                         'keywords', 'alias', 'contact-type',
2272                                         'url', 'location', 'about');
2273                         $old_contact = dba::select('contact', $fields, array('id' => $r[0]["id"]), array('limit' => 1));
2274
2275                         // Update it with the current values
2276                         $fields = array('name' => $contact['name'], 'nick' => $contact['nick'],
2277                                         'addr' => $contact['addr'], 'network' => $contact['network'],
2278                                         'bd' => $contact['birthday'], 'gender' => $contact['gender'],
2279                                         'keywords' => $contact['keywords'], 'alias' => $contact['alias'],
2280                                         'contact-type' => $contact['contact-type'], 'url' => $contact['url'],
2281                                         'location' => $contact['location'], 'about' => $contact['about']);
2282
2283                         dba::update('contact', $fields, array('id' => $r[0]["id"]), $old_contact);
2284                 }
2285         }
2286
2287         return $gcontact_id;
2288 }
2289
2290 /**
2291  * @brief Updates the gcontact entry from probe
2292  *
2293  * @param str $url profile link
2294  */
2295 function update_gcontact_from_probe($url) {
2296         $data = Probe::uri($url);
2297
2298         if (in_array($data["network"], array(NETWORK_PHANTOM))) {
2299                 logger("Invalid network for contact url ".$data["url"]." - Called by: ".System::callstack(), LOGGER_DEBUG);
2300                 return;
2301         }
2302
2303         $data["server_url"] = $data["baseurl"];
2304
2305         update_gcontact($data);
2306 }
2307
2308 /**
2309  * @brief Update the gcontact entry for a given user id
2310  *
2311  * @param int $uid User ID
2312  */
2313 function update_gcontact_for_user($uid) {
2314         $r = q("SELECT `profile`.`locality`, `profile`.`region`, `profile`.`country-name`,
2315                         `profile`.`name`, `profile`.`about`, `profile`.`gender`,
2316                         `profile`.`pub_keywords`, `profile`.`dob`, `profile`.`photo`,
2317                         `profile`.`net-publish`, `user`.`nickname`, `user`.`hidewall`,
2318                         `contact`.`notify`, `contact`.`url`, `contact`.`addr`
2319                 FROM `profile`
2320                         INNER JOIN `user` ON `user`.`uid` = `profile`.`uid`
2321                         INNER JOIN `contact` ON `contact`.`uid` = `profile`.`uid`
2322                 WHERE `profile`.`uid` = %d AND `profile`.`is-default` AND `contact`.`self`",
2323                 intval($uid));
2324
2325         $location = formatted_location(array("locality" => $r[0]["locality"], "region" => $r[0]["region"],
2326                                                 "country-name" => $r[0]["country-name"]));
2327
2328         // The "addr" field was added in 3.4.3 so it can be empty for older users
2329         if ($r[0]["addr"] != "") {
2330                 $addr = $r[0]["nickname"].'@'.str_replace(array("http://", "https://"), "", System::baseUrl());
2331         } else {
2332                 $addr = $r[0]["addr"];
2333         }
2334
2335         $gcontact = array("name" => $r[0]["name"], "location" => $location, "about" => $r[0]["about"],
2336                         "gender" => $r[0]["gender"], "keywords" => $r[0]["pub_keywords"],
2337                         "birthday" => $r[0]["dob"], "photo" => $r[0]["photo"],
2338                         "notify" => $r[0]["notify"], "url" => $r[0]["url"],
2339                         "hide" => ($r[0]["hidewall"] || !$r[0]["net-publish"]),
2340                         "nick" => $r[0]["nickname"], "addr" => $addr,
2341                         "connect" => $addr, "server_url" => System::baseUrl(),
2342                         "generation" => 1, "network" => NETWORK_DFRN);
2343
2344         update_gcontact($gcontact);
2345 }
2346
2347 /**
2348  * @brief Fetches users of given GNU Social server
2349  *
2350  * If the "Statistics" plugin is enabled (See http://gstools.org/ for details) we query user data with this.
2351  *
2352  * @param str $server Server address
2353  */
2354 function gs_fetch_users($server) {
2355
2356         logger("Fetching users from GNU Social server ".$server, LOGGER_DEBUG);
2357
2358         $url = $server."/main/statistics";
2359
2360         $result = z_fetch_url($url);
2361         if (!$result["success"]) {
2362                 return false;
2363         }
2364
2365         $statistics = json_decode($result["body"]);
2366
2367         if (is_object($statistics->config)) {
2368                 if ($statistics->config->instance_with_ssl) {
2369                         $server = "https://";
2370                 } else {
2371                         $server = "http://";
2372                 }
2373
2374                 $server .= $statistics->config->instance_address;
2375
2376                 $hostname = $statistics->config->instance_address;
2377         } else {
2378                 /// @TODO is_object() above means here no object, still $statistics is being used as object
2379                 if ($statistics->instance_with_ssl) {
2380                         $server = "https://";
2381                 } else {
2382                         $server = "http://";
2383                 }
2384
2385                 $server .= $statistics->instance_address;
2386
2387                 $hostname = $statistics->instance_address;
2388         }
2389
2390         if (is_object($statistics->users)) {
2391                 foreach ($statistics->users as $nick => $user) {
2392                         $profile_url = $server."/".$user->nickname;
2393
2394                         $contact = array("url" => $profile_url,
2395                                         "name" => $user->fullname,
2396                                         "addr" => $user->nickname."@".$hostname,
2397                                         "nick" => $user->nickname,
2398                                         "about" => $user->bio,
2399                                         "network" => NETWORK_OSTATUS,
2400                                         "photo" => System::baseUrl()."/images/person-175.jpg");
2401                         get_gcontact_id($contact);
2402                 }
2403         }
2404 }
2405
2406 /**
2407  * @brief Asking GNU Social server on a regular base for their user data
2408  *
2409  */
2410 function gs_discover() {
2411
2412         $requery_days = intval(Config::get("system", "poco_requery_days"));
2413
2414         $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
2415
2416         $r = q("SELECT `nurl`, `url` FROM `gserver` WHERE `last_contact` >= `last_failure` AND `network` = '%s' AND `last_poco_query` < '%s' ORDER BY RAND() LIMIT 5",
2417                 dbesc(NETWORK_OSTATUS), dbesc($last_update));
2418
2419         if (!dbm::is_result($r)) {
2420                 return;
2421         }
2422
2423         foreach ($r as $server) {
2424                 gs_fetch_users($server["url"]);
2425                 q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
2426         }
2427 }
2428
2429 /**
2430  * @brief Returns a list of all known servers
2431  * @return array List of server urls
2432  */
2433 function poco_serverlist() {
2434         $r = q("SELECT `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver`
2435                 WHERE `network` IN ('%s', '%s', '%s') AND `last_contact` > `last_failure`
2436                 ORDER BY `last_contact`
2437                 LIMIT 1000",
2438                 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
2439         if (!dbm::is_result($r)) {
2440                 return false;
2441         }
2442
2443         return $r;
2444 }