]> git.mxchange.org Git - friendica.git/blob - src/Protocol/PortableContact.php
7c1015c9aa526dbef411e29a9b71748fa73c0690
[friendica.git] / src / Protocol / PortableContact.php
1 <?php
2 /**
3  * @file src/Protocol/PortableContact.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 namespace Friendica\Protocol;
11
12 use Friendica\Core\Config;
13 use Friendica\Core\Worker;
14 use Friendica\Database\DBM;
15 use Friendica\Model\GContact;
16 use Friendica\Model\Profile;
17 use Friendica\Network\Probe;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Util\Network;
20 use dba;
21 use DOMDocument;
22 use DOMXPath;
23 use Exception;
24
25 require_once 'include/dba.php';
26 require_once 'include/html2bbcode.php';
27
28 class PortableContact
29 {
30         /**
31          * @brief Fetch POCO data
32          *
33          * @param integer $cid  Contact ID
34          * @param integer $uid  User ID
35          * @param integer $zcid Global Contact ID
36          * @param integer $url  POCO address that should be polled
37          *
38          * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
39          * and add the entries to the gcontact (Global Contact) table, or update existing entries
40          * if anything (name or photo) has changed.
41          * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
42          *
43          * Once the global contact is stored add (if necessary) the contact linkage which associates
44          * the given uid, cid to the global contact entry. There can be many uid/cid combinations
45          * pointing to the same global contact id.
46          *
47          */
48         public static function loadWorker($cid, $uid = 0, $zcid = 0, $url = null)
49         {
50                 // Call the function "load" via the worker
51                 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "load", (int)$cid, (int)$uid, (int)$zcid, $url);
52         }
53
54         /**
55          * @brief Fetch POCO data from the worker
56          *
57          * @param integer $cid  Contact ID
58          * @param integer $uid  User ID
59          * @param integer $zcid Global Contact ID
60          * @param integer $url  POCO address that should be polled
61          *
62          */
63         public static function load($cid, $uid, $zcid, $url)
64         {
65                 $a = get_app();
66
67                 if ($cid) {
68                         if (!$url || !$uid) {
69                                 $contact = dba::selectFirst('contact', ['poco', 'uid'], ['id' => $cid]);
70                                 if (DBM::is_result($contact)) {
71                                         $url = $contact['poco'];
72                                         $uid = $contact['uid'];
73                                 }
74                         }
75                         if (!$uid) {
76                                 return;
77                         }
78                 }
79
80                 if (!$url) {
81                         return;
82                 }
83
84                 $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') ;
85
86                 logger('load: ' . $url, LOGGER_DEBUG);
87
88                 $s = Network::fetchUrl($url);
89
90                 logger('load: returns ' . $s, LOGGER_DATA);
91
92                 logger('load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
93
94                 if (($a->get_curl_code() > 299) || (! $s)) {
95                         return;
96                 }
97
98                 $j = json_decode($s);
99
100                 logger('load: json: ' . print_r($j, true), LOGGER_DATA);
101
102                 if (! isset($j->entry)) {
103                         return;
104                 }
105
106                 $total = 0;
107                 foreach ($j->entry as $entry) {
108                         $total ++;
109                         $profile_url = '';
110                         $profile_photo = '';
111                         $connect_url = '';
112                         $name = '';
113                         $network = '';
114                         $updated = NULL_DATE;
115                         $location = '';
116                         $about = '';
117                         $keywords = '';
118                         $gender = '';
119                         $contact_type = -1;
120                         $generation = 0;
121
122                         $name = $entry->displayName;
123
124                         if (isset($entry->urls)) {
125                                 foreach ($entry->urls as $url) {
126                                         if ($url->type == 'profile') {
127                                                 $profile_url = $url->value;
128                                                 continue;
129                                         }
130                                         if ($url->type == 'webfinger') {
131                                                 $connect_url = str_replace('acct:', '', $url->value);
132                                                 continue;
133                                         }
134                                 }
135                         }
136                         if (isset($entry->photos)) {
137                                 foreach ($entry->photos as $photo) {
138                                         if ($photo->type == 'profile') {
139                                                 $profile_photo = $photo->value;
140                                                 continue;
141                                         }
142                                 }
143                         }
144
145                         if (isset($entry->updated)) {
146                                 $updated = date(DateTimeFormat::MYSQL, strtotime($entry->updated));
147                         }
148
149                         if (isset($entry->network)) {
150                                 $network = $entry->network;
151                         }
152
153                         if (isset($entry->currentLocation)) {
154                                 $location = $entry->currentLocation;
155                         }
156
157                         if (isset($entry->aboutMe)) {
158                                 $about = html2bbcode($entry->aboutMe);
159                         }
160
161                         if (isset($entry->gender)) {
162                                 $gender = $entry->gender;
163                         }
164
165                         if (isset($entry->generation) && ($entry->generation > 0)) {
166                                 $generation = ++$entry->generation;
167                         }
168
169                         if (isset($entry->tags)) {
170                                 foreach ($entry->tags as $tag) {
171                                         $keywords = implode(", ", $tag);
172                                 }
173                         }
174
175                         if (isset($entry->contactType) && ($entry->contactType >= 0)) {
176                                 $contact_type = $entry->contactType;
177                         }
178
179                         $gcontact = ["url" => $profile_url,
180                                         "name" => $name,
181                                         "network" => $network,
182                                         "photo" => $profile_photo,
183                                         "about" => $about,
184                                         "location" => $location,
185                                         "gender" => $gender,
186                                         "keywords" => $keywords,
187                                         "connect" => $connect_url,
188                                         "updated" => $updated,
189                                         "contact-type" => $contact_type,
190                                         "generation" => $generation];
191
192                         try {
193                                 $gcontact = GContact::sanitize($gcontact);
194                                 $gcid = GContact::update($gcontact);
195
196                                 GContact::link($gcid, $uid, $cid, $zcid);
197                         } catch (Exception $e) {
198                                 logger($e->getMessage(), LOGGER_DEBUG);
199                         }
200                 }
201                 logger("load: loaded $total entries", LOGGER_DEBUG);
202
203                 $condition = ["`cid` = ? AND `uid` = ? AND `zcid` = ? AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY", $cid, $uid, $zcid];
204                 dba::delete('glink', $condition);
205         }
206
207         public static function reachable($profile, $server = "", $network = "", $force = false)
208         {
209                 if ($server == "") {
210                         $server = self::detectServer($profile);
211                 }
212
213                 if ($server == "") {
214                         return true;
215                 }
216
217                 return self::checkServer($server, $network, $force);
218         }
219
220         public static function detectServer($profile)
221         {
222                 // Try to detect the server path based upon some known standard paths
223                 $server_url = "";
224
225                 if ($server_url == "") {
226                         $friendica = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $profile);
227                         if ($friendica != $profile) {
228                                 $server_url = $friendica;
229                                 $network = NETWORK_DFRN;
230                         }
231                 }
232
233                 if ($server_url == "") {
234                         $diaspora = preg_replace("=(https?://)(.*)/u/(.*)=ism", "$1$2", $profile);
235                         if ($diaspora != $profile) {
236                                 $server_url = $diaspora;
237                                 $network = NETWORK_DIASPORA;
238                         }
239                 }
240
241                 if ($server_url == "") {
242                         $red = preg_replace("=(https?://)(.*)/channel/(.*)=ism", "$1$2", $profile);
243                         if ($red != $profile) {
244                                 $server_url = $red;
245                                 $network = NETWORK_DIASPORA;
246                         }
247                 }
248
249                 // Mastodon
250                 if ($server_url == "") {
251                         $mastodon = preg_replace("=(https?://)(.*)/users/(.*)=ism", "$1$2", $profile);
252                         if ($mastodon != $profile) {
253                                 $server_url = $mastodon;
254                                 $network = NETWORK_OSTATUS;
255                         }
256                 }
257
258                 // Numeric OStatus variant
259                 if ($server_url == "") {
260                         $ostatus = preg_replace("=(https?://)(.*)/user/(.*)=ism", "$1$2", $profile);
261                         if ($ostatus != $profile) {
262                                 $server_url = $ostatus;
263                                 $network = NETWORK_OSTATUS;
264                         }
265                 }
266
267                 // Wild guess
268                 if ($server_url == "") {
269                         $base = preg_replace("=(https?://)(.*?)/(.*)=ism", "$1$2", $profile);
270                         if ($base != $profile) {
271                                 $server_url = $base;
272                                 $network = NETWORK_PHANTOM;
273                         }
274                 }
275
276                 if ($server_url == "") {
277                         return "";
278                 }
279
280                 $r = q(
281                         "SELECT `id` FROM `gserver` WHERE `nurl` = '%s' AND `last_contact` > `last_failure`",
282                         dbesc(normalise_link($server_url))
283                 );
284
285                 if (DBM::is_result($r)) {
286                         return $server_url;
287                 }
288
289                 // Fetch the host-meta to check if this really is a server
290                 $serverret = Network::curl($server_url."/.well-known/host-meta");
291                 if (!$serverret["success"]) {
292                         return "";
293                 }
294
295                 return $server_url;
296         }
297
298         public static function alternateOStatusUrl($url)
299         {
300                 return(preg_match("=https?://.+/user/\d+=ism", $url, $matches));
301         }
302
303         public static function lastUpdated($profile, $force = false)
304         {
305                 $gcontacts = q(
306                         "SELECT * FROM `gcontact` WHERE `nurl` = '%s'",
307                         dbesc(normalise_link($profile))
308                 );
309
310                 if (!DBM::is_result($gcontacts)) {
311                         return false;
312                 }
313
314                 $contact = ["url" => $profile];
315
316                 if ($gcontacts[0]["created"] <= NULL_DATE) {
317                         $contact['created'] = DateTimeFormat::utcNow();
318                 }
319
320                 if ($force) {
321                         $server_url = normalise_link(self::detectServer($profile));
322                 }
323
324                 if (($server_url == '') && ($gcontacts[0]["server_url"] != "")) {
325                         $server_url = $gcontacts[0]["server_url"];
326                 }
327
328                 if (!$force && (($server_url == '') || ($gcontacts[0]["server_url"] == $gcontacts[0]["nurl"]))) {
329                         $server_url = normalise_link(self::detectServer($profile));
330                 }
331
332                 if (!in_array($gcontacts[0]["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_FEED, NETWORK_OSTATUS, ""])) {
333                         logger("Profile ".$profile.": Network type ".$gcontacts[0]["network"]." can't be checked", LOGGER_DEBUG);
334                         return false;
335                 }
336
337                 if ($server_url != "") {
338                         if (!self::checkServer($server_url, $gcontacts[0]["network"], $force)) {
339                                 if ($force) {
340                                         $fields = ['last_failure' => DateTimeFormat::utcNow()];
341                                         dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
342                                 }
343
344                                 logger("Profile ".$profile.": Server ".$server_url." wasn't reachable.", LOGGER_DEBUG);
345                                 return false;
346                         }
347                         $contact['server_url'] = $server_url;
348                 }
349
350                 if (in_array($gcontacts[0]["network"], ["", NETWORK_FEED])) {
351                         $server = q(
352                                 "SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''",
353                                 dbesc(normalise_link($server_url))
354                         );
355
356                         if ($server) {
357                                 $contact['network'] = $server[0]["network"];
358                         } else {
359                                 return false;
360                         }
361                 }
362
363                 // noscrape is really fast so we don't cache the call.
364                 if (($server_url != "") && ($gcontacts[0]["nick"] != "")) {
365                         //  Use noscrape if possible
366                         $server = q("SELECT `noscrape`, `network` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", dbesc(normalise_link($server_url)));
367
368                         if ($server) {
369                                 $noscraperet = Network::curl($server[0]["noscrape"]."/".$gcontacts[0]["nick"]);
370
371                                 if ($noscraperet["success"] && ($noscraperet["body"] != "")) {
372                                         $noscrape = json_decode($noscraperet["body"], true);
373
374                                         if (is_array($noscrape)) {
375                                                 $contact["network"] = $server[0]["network"];
376
377                                                 if (isset($noscrape["fn"])) {
378                                                         $contact["name"] = $noscrape["fn"];
379                                                 }
380                                                 if (isset($noscrape["comm"])) {
381                                                         $contact["community"] = $noscrape["comm"];
382                                                 }
383                                                 if (isset($noscrape["tags"])) {
384                                                         $keywords = implode(" ", $noscrape["tags"]);
385                                                         if ($keywords != "") {
386                                                                 $contact["keywords"] = $keywords;
387                                                         }
388                                                 }
389
390                                                 $location = Profile::formatLocation($noscrape);
391                                                 if ($location) {
392                                                         $contact["location"] = $location;
393                                                 }
394                                                 if (isset($noscrape["dfrn-notify"])) {
395                                                         $contact["notify"] = $noscrape["dfrn-notify"];
396                                                 }
397                                                 // Remove all fields that are not present in the gcontact table
398                                                 unset($noscrape["fn"]);
399                                                 unset($noscrape["key"]);
400                                                 unset($noscrape["homepage"]);
401                                                 unset($noscrape["comm"]);
402                                                 unset($noscrape["tags"]);
403                                                 unset($noscrape["locality"]);
404                                                 unset($noscrape["region"]);
405                                                 unset($noscrape["country-name"]);
406                                                 unset($noscrape["contacts"]);
407                                                 unset($noscrape["dfrn-request"]);
408                                                 unset($noscrape["dfrn-confirm"]);
409                                                 unset($noscrape["dfrn-notify"]);
410                                                 unset($noscrape["dfrn-poll"]);
411
412                                                 // Set the date of the last contact
413                                                 /// @todo By now the function "update_gcontact" doesn't work with this field
414                                                 //$contact["last_contact"] = DateTimeFormat::utcNow();
415
416                                                 $contact = array_merge($contact, $noscrape);
417
418                                                 GContact::update($contact);
419
420                                                 if (trim($noscrape["updated"]) != "") {
421                                                         $fields = ['last_contact' => DateTimeFormat::utcNow()];
422                                                         dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
423
424                                                         logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG);
425
426                                                         return $noscrape["updated"];
427                                                 }
428                                         }
429                                 }
430                         }
431                 }
432
433                 // If we only can poll the feed, then we only do this once a while
434                 if (!$force && !self::updateNeeded($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) {
435                         logger("Profile ".$profile." was last updated at ".$gcontacts[0]["updated"]." (cached)", LOGGER_DEBUG);
436
437                         GContact::update($contact);
438                         return $gcontacts[0]["updated"];
439                 }
440
441                 $data = Probe::uri($profile);
442
443                 // Is the profile link the alternate OStatus link notation? (http://domain.tld/user/4711)
444                 // Then check the other link and delete this one
445                 if (($data["network"] == NETWORK_OSTATUS) && self::alternateOStatusUrl($profile)
446                         && (normalise_link($profile) == normalise_link($data["alias"]))
447                         && (normalise_link($profile) != normalise_link($data["url"]))
448                 ) {
449                         // Delete the old entry
450                         dba::delete('gcontact', ['nurl' => normalise_link($profile)]);
451
452                         $gcontact = array_merge($gcontacts[0], $data);
453
454                         $gcontact["server_url"] = $data["baseurl"];
455
456                         try {
457                                 $gcontact = GContact::sanitize($gcontact);
458                                 GContact::update($gcontact);
459
460                                 self::lastUpdated($data["url"], $force);
461                         } catch (Exception $e) {
462                                 logger($e->getMessage(), LOGGER_DEBUG);
463                         }
464
465                         logger("Profile ".$profile." was deleted", LOGGER_DEBUG);
466                         return false;
467                 }
468
469                 if (($data["poll"] == "") || (in_array($data["network"], [NETWORK_FEED, NETWORK_PHANTOM]))) {
470                         $fields = ['last_failure' => DateTimeFormat::utcNow()];
471                         dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
472
473                         logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG);
474                         return false;
475                 }
476
477                 $contact = array_merge($contact, $data);
478
479                 $contact["server_url"] = $data["baseurl"];
480
481                 GContact::update($contact);
482
483                 $feedret = Network::curl($data["poll"]);
484
485                 if (!$feedret["success"]) {
486                         $fields = ['last_failure' => DateTimeFormat::utcNow()];
487                         dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
488
489                         logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG);
490                         return false;
491                 }
492
493                 $doc = new DOMDocument();
494                 @$doc->loadXML($feedret["body"]);
495
496                 $xpath = new DOMXPath($doc);
497                 $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
498
499                 $entries = $xpath->query('/atom:feed/atom:entry');
500
501                 $last_updated = "";
502
503                 foreach ($entries as $entry) {
504                         $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
505                         $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
506
507                         if ($last_updated < $published)
508                                 $last_updated = $published;
509
510                         if ($last_updated < $updated)
511                                 $last_updated = $updated;
512                 }
513
514                 // Maybe there aren't any entries. Then check if it is a valid feed
515                 if ($last_updated == "") {
516                         if ($xpath->query('/atom:feed')->length > 0) {
517                                 $last_updated = NULL_DATE;
518                         }
519                 }
520                 $fields = ['updated' => DBM::date($last_updated), 'last_contact' => DBM::date()];
521                 dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
522
523                 if (($gcontacts[0]["generation"] == 0)) {
524                         $fields = ['generation' => 9];
525                         dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
526                 }
527
528                 logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG);
529
530                 return($last_updated);
531         }
532
533         public static function updateNeeded($created, $updated, $last_failure, $last_contact)
534         {
535                 $now = strtotime(DateTimeFormat::utcNow());
536
537                 if ($updated > $last_contact) {
538                         $contact_time = strtotime($updated);
539                 } else {
540                         $contact_time = strtotime($last_contact);
541                 }
542
543                 $failure_time = strtotime($last_failure);
544                 $created_time = strtotime($created);
545
546                 // If there is no "created" time then use the current time
547                 if ($created_time <= 0) {
548                         $created_time = $now;
549                 }
550
551                 // If the last contact was less than 24 hours then don't update
552                 if (($now - $contact_time) < (60 * 60 * 24)) {
553                         return false;
554                 }
555
556                 // If the last failure was less than 24 hours then don't update
557                 if (($now - $failure_time) < (60 * 60 * 24)) {
558                         return false;
559                 }
560
561                 // If the last contact was less than a week ago and the last failure is older than a week then don't update
562                 //if ((($now - $contact_time) < (60 * 60 * 24 * 7)) && ($contact_time > $failure_time))
563                 //      return false;
564
565                 // 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
566                 if ((($now - $contact_time) > (60 * 60 * 24 * 7)) && (($now - $created_time) > (60 * 60 * 24 * 7)) && (($now - $failure_time) < (60 * 60 * 24 * 7))) {
567                         return false;
568                 }
569
570                 // 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
571                 if ((($now - $contact_time) > (60 * 60 * 24 * 30)) && (($now - $created_time) > (60 * 60 * 24 * 30)) && (($now - $failure_time) < (60 * 60 * 24 * 30))) {
572                         return false;
573                 }
574
575                 return true;
576         }
577
578         private static function toBoolean($val)
579         {
580                 if (($val == "true") || ($val == 1)) {
581                         return true;
582                 } elseif (($val == "false") || ($val == 0)) {
583                         return false;
584                 }
585
586                 return $val;
587         }
588
589         /**
590          * @brief Detect server type (Hubzilla or Friendica) via the poco data
591          *
592          * @param object $data POCO data
593          * @return array Server data
594          */
595         private static function detectPocoData($data)
596         {
597                 $server = false;
598
599                 if (!isset($data->entry)) {
600                         return false;
601                 }
602
603                 if (count($data->entry) == 0) {
604                         return false;
605                 }
606
607                 if (!isset($data->entry[0]->urls)) {
608                         return false;
609                 }
610
611                 if (count($data->entry[0]->urls) == 0) {
612                         return false;
613                 }
614
615                 foreach ($data->entry[0]->urls as $url) {
616                         if ($url->type == 'zot') {
617                                 $server = [];
618                                 $server["platform"] = 'Hubzilla';
619                                 $server["network"] = NETWORK_DIASPORA;
620                                 return $server;
621                         }
622                 }
623                 return false;
624         }
625
626         /**
627          * @brief Detect server type by using the nodeinfo data
628          *
629          * @param string $server_url address of the server
630          * @return array Server data
631          */
632         private static function fetchNodeinfo($server_url)
633         {
634                 $serverret = Network::curl($server_url."/.well-known/nodeinfo");
635                 if (!$serverret["success"]) {
636                         return false;
637                 }
638
639                 $nodeinfo = json_decode($serverret['body']);
640
641                 if (!is_object($nodeinfo)) {
642                         return false;
643                 }
644
645                 if (!is_array($nodeinfo->links)) {
646                         return false;
647                 }
648
649                 $nodeinfo1_url = '';
650                 $nodeinfo2_url = '';
651
652                 foreach ($nodeinfo->links as $link) {
653                         if ($link->rel == 'http://nodeinfo.diaspora.software/ns/schema/1.0') {
654                                 $nodeinfo1_url = $link->href;
655                         }
656                         if ($link->rel == 'http://nodeinfo.diaspora.software/ns/schema/2.0') {
657                                 $nodeinfo2_url = $link->href;
658                         }
659                 }
660
661                 if ($nodeinfo1_url . $nodeinfo2_url == '') {
662                         return false;
663                 }
664
665                 $server = [];
666
667                 // When the nodeinfo url isn't on the same host, then there is obviously something wrong
668                 if (!empty($nodeinfo2_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo2_url, PHP_URL_HOST))) {
669                         $server = self::parseNodeinfo2($nodeinfo2_url);
670                 }
671
672                 // When the nodeinfo url isn't on the same host, then there is obviously something wrong
673                 if (empty($server) && !empty($nodeinfo1_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo1_url, PHP_URL_HOST))) {
674                         $server = self::parseNodeinfo1($nodeinfo1_url);
675                 }
676
677                 return $server;
678         }
679
680         /**
681          * @brief Parses Nodeinfo 1
682          *
683          * @param string $nodeinfo_url address of the nodeinfo path
684          * @return array Server data
685          */
686         private static function parseNodeinfo1($nodeinfo_url)
687         {
688                 $serverret = Network::curl($nodeinfo_url);
689                 if (!$serverret["success"]) {
690                         return false;
691                 }
692
693                 $nodeinfo = json_decode($serverret['body']);
694                 if (!is_object($nodeinfo)) {
695                         return false;
696                 }
697
698                 $server = [];
699
700                 $server['register_policy'] = REGISTER_CLOSED;
701
702                 if (is_bool($nodeinfo->openRegistrations) && $nodeinfo->openRegistrations) {
703                         $server['register_policy'] = REGISTER_OPEN;
704                 }
705
706                 if (is_object($nodeinfo->software)) {
707                         if (isset($nodeinfo->software->name)) {
708                                 $server['platform'] = $nodeinfo->software->name;
709                         }
710
711                         if (isset($nodeinfo->software->version)) {
712                                 $server['version'] = $nodeinfo->software->version;
713                                 // Version numbers on Nodeinfo are presented with additional info, e.g.:
714                                 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
715                                 $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']);
716                         }
717                 }
718
719                 if (is_object($nodeinfo->metadata)) {
720                         if (isset($nodeinfo->metadata->nodeName)) {
721                                 $server['site_name'] = $nodeinfo->metadata->nodeName;
722                         }
723                 }
724
725                 if (!empty($nodeinfo->usage->users->total)) {
726                         $server['registered-users'] = $nodeinfo->usage->users->total;
727                 }
728
729                 $diaspora = false;
730                 $friendica = false;
731                 $gnusocial = false;
732
733                 if (is_array($nodeinfo->protocols->inbound)) {
734                         foreach ($nodeinfo->protocols->inbound as $inbound) {
735                                 if ($inbound == 'diaspora') {
736                                         $diaspora = true;
737                                 }
738                                 if ($inbound == 'friendica') {
739                                         $friendica = true;
740                                 }
741                                 if ($inbound == 'gnusocial') {
742                                         $gnusocial = true;
743                                 }
744                         }
745                 }
746
747                 if ($gnusocial) {
748                         $server['network'] = NETWORK_OSTATUS;
749                 }
750                 if ($diaspora) {
751                         $server['network'] = NETWORK_DIASPORA;
752                 }
753                 if ($friendica) {
754                         $server['network'] = NETWORK_DFRN;
755                 }
756
757                 if (!$server) {
758                         return false;
759                 }
760
761                 return $server;
762         }
763
764         /**
765          * @brief Parses Nodeinfo 2
766          *
767          * @param string $nodeinfo_url address of the nodeinfo path
768          * @return array Server data
769          */
770         private static function parseNodeinfo2($nodeinfo_url)
771         {
772                 $serverret = Network::curl($nodeinfo_url);
773                 if (!$serverret["success"]) {
774                         return false;
775                 }
776
777                 $nodeinfo = json_decode($serverret['body']);
778                 if (!is_object($nodeinfo)) {
779                         return false;
780                 }
781
782                 $server = [];
783
784                 $server['register_policy'] = REGISTER_CLOSED;
785
786                 if (is_bool($nodeinfo->openRegistrations) && $nodeinfo->openRegistrations) {
787                         $server['register_policy'] = REGISTER_OPEN;
788                 }
789
790                 if (is_object($nodeinfo->software)) {
791                         if (isset($nodeinfo->software->name)) {
792                                 $server['platform'] = $nodeinfo->software->name;
793                         }
794
795                         if (isset($nodeinfo->software->version)) {
796                                 $server['version'] = $nodeinfo->software->version;
797                                 // Version numbers on Nodeinfo are presented with additional info, e.g.:
798                                 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
799                                 $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']);
800                         }
801                 }
802
803                 if (is_object($nodeinfo->metadata)) {
804                         if (isset($nodeinfo->metadata->nodeName)) {
805                                 $server['site_name'] = $nodeinfo->metadata->nodeName;
806                         }
807                 }
808
809                 if (!empty($nodeinfo->usage->users->total)) {
810                         $server['registered-users'] = $nodeinfo->usage->users->total;
811                 }
812
813                 $diaspora = false;
814                 $friendica = false;
815                 $gnusocial = false;
816
817                 if (is_array($nodeinfo->protocols)) {
818                         foreach ($nodeinfo->protocols as $protocol) {
819                                 if ($protocol == 'diaspora') {
820                                         $diaspora = true;
821                                 }
822                                 if ($protocol == 'friendica') {
823                                         $friendica = true;
824                                 }
825                                 if ($protocol == 'gnusocial') {
826                                         $gnusocial = true;
827                                 }
828                         }
829                 }
830
831                 if ($gnusocial) {
832                         $server['network'] = NETWORK_OSTATUS;
833                 }
834                 if ($diaspora) {
835                         $server['network'] = NETWORK_DIASPORA;
836                 }
837                 if ($friendica) {
838                         $server['network'] = NETWORK_DFRN;
839                 }
840
841                 if (!$server) {
842                         return false;
843                 }
844
845                 return $server;
846         }
847
848         /**
849          * @brief Detect server type (Hubzilla or Friendica) via the front page body
850          *
851          * @param string $body Front page of the server
852          * @return array Server data
853          */
854         private static function detectServerType($body)
855         {
856                 $server = false;
857
858                 $doc = new DOMDocument();
859                 @$doc->loadHTML($body);
860                 $xpath = new DOMXPath($doc);
861
862                 $list = $xpath->query("//meta[@name]");
863
864                 foreach ($list as $node) {
865                         $attr = [];
866                         if ($node->attributes->length) {
867                                 foreach ($node->attributes as $attribute) {
868                                         $attr[$attribute->name] = $attribute->value;
869                                 }
870                         }
871                         if ($attr['name'] == 'generator') {
872                                 $version_part = explode(" ", $attr['content']);
873                                 if (count($version_part) == 2) {
874                                         if (in_array($version_part[0], ["Friendika", "Friendica"])) {
875                                                 $server = [];
876                                                 $server["platform"] = $version_part[0];
877                                                 $server["version"] = $version_part[1];
878                                                 $server["network"] = NETWORK_DFRN;
879                                         }
880                                 }
881                         }
882                 }
883
884                 if (!$server) {
885                         $list = $xpath->query("//meta[@property]");
886
887                         foreach ($list as $node) {
888                                 $attr = [];
889                                 if ($node->attributes->length) {
890                                         foreach ($node->attributes as $attribute) {
891                                                 $attr[$attribute->name] = $attribute->value;
892                                         }
893                                 }
894                                 if ($attr['property'] == 'generator' && in_array($attr['content'], ["hubzilla", "BlaBlaNet"])) {
895                                         $server = [];
896                                         $server["platform"] = $attr['content'];
897                                         $server["version"] = "";
898                                         $server["network"] = NETWORK_DIASPORA;
899                                 }
900                         }
901                 }
902
903                 if (!$server) {
904                         return false;
905                 }
906
907                 $server["site_name"] = $xpath->evaluate("//head/title/text()")->item(0)->nodeValue;
908                 return $server;
909         }
910
911         public static function checkServer($server_url, $network = "", $force = false)
912         {
913                 // Unify the server address
914                 $server_url = trim($server_url, "/");
915                 $server_url = str_replace("/index.php", "", $server_url);
916
917                 if ($server_url == "") {
918                         return false;
919                 }
920
921                 $gserver = dba::selectFirst('gserver', [], ['nurl' => normalise_link($server_url)]);
922                 if (DBM::is_result($gserver)) {
923                         if ($gserver["created"] <= NULL_DATE) {
924                                 $fields = ['created' => DateTimeFormat::utcNow()];
925                                 $condition = ['nurl' => normalise_link($server_url)];
926                                 dba::update('gserver', $fields, $condition);
927                         }
928                         $poco = $gserver["poco"];
929                         $noscrape = $gserver["noscrape"];
930
931                         if ($network == "") {
932                                 $network = $gserver["network"];
933                         }
934
935                         $last_contact = $gserver["last_contact"];
936                         $last_failure = $gserver["last_failure"];
937                         $version = $gserver["version"];
938                         $platform = $gserver["platform"];
939                         $site_name = $gserver["site_name"];
940                         $info = $gserver["info"];
941                         $register_policy = $gserver["register_policy"];
942                         $registered_users = $gserver["registered-users"];
943
944                         if (!$force && !self::updateNeeded($gserver["created"], "", $last_failure, $last_contact)) {
945                                 logger("Use cached data for server ".$server_url, LOGGER_DEBUG);
946                                 return ($last_contact >= $last_failure);
947                         }
948                 } else {
949                         $poco = "";
950                         $noscrape = "";
951                         $version = "";
952                         $platform = "";
953                         $site_name = "";
954                         $info = "";
955                         $register_policy = -1;
956                         $registered_users = 0;
957
958                         $last_contact = NULL_DATE;
959                         $last_failure = NULL_DATE;
960                 }
961                 logger("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$gserver["created"]." Failure: ".$last_failure." Contact: ".$last_contact, LOGGER_DEBUG);
962
963                 $failure = false;
964                 $possible_failure = false;
965                 $orig_last_failure = $last_failure;
966                 $orig_last_contact = $last_contact;
967
968                 // Mastodon uses the "@" for user profiles.
969                 // But this can be misunderstood.
970                 if (parse_url($server_url, PHP_URL_USER) != '') {
971                         dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
972                         return false;
973                 }
974
975                 // Check if the page is accessible via SSL.
976                 $orig_server_url = $server_url;
977                 $server_url = str_replace("http://", "https://", $server_url);
978
979                 // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
980                 $serverret = Network::curl($server_url."/.well-known/host-meta", false, $redirects, ['timeout' => 20]);
981
982                 // Quit if there is a timeout.
983                 // But we want to make sure to only quit if we are mostly sure that this server url fits.
984                 if (DBM::is_result($gserver) && ($orig_server_url == $server_url) &&
985                         ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT)) {
986                         logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
987                         dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
988                         return false;
989                 }
990
991                 // Maybe the page is unencrypted only?
992                 $xmlobj = @simplexml_load_string($serverret["body"], 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
993                 if (!$serverret["success"] || ($serverret["body"] == "") || (@sizeof($xmlobj) == 0) || !is_object($xmlobj)) {
994                         $server_url = str_replace("https://", "http://", $server_url);
995
996                         // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
997                         $serverret = Network::curl($server_url."/.well-known/host-meta", false, $redirects, ['timeout' => 20]);
998
999                         // Quit if there is a timeout
1000                         if ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT) {
1001                                 logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
1002                                 dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
1003                                 return false;
1004                         }
1005
1006                         $xmlobj = @simplexml_load_string($serverret["body"], 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
1007                 }
1008
1009                 if (!$serverret["success"] || ($serverret["body"] == "") || (sizeof($xmlobj) == 0) || !is_object($xmlobj)) {
1010                         // Workaround for bad configured servers (known nginx problem)
1011                         if (!in_array($serverret["debug"]["http_code"], ["403", "404"])) {
1012                                 $failure = true;
1013                         }
1014                         $possible_failure = true;
1015                 }
1016
1017                 // If the server has no possible failure we reset the cached data
1018                 if (!$possible_failure) {
1019                         $version = "";
1020                         $platform = "";
1021                         $site_name = "";
1022                         $info = "";
1023                         $register_policy = -1;
1024                 }
1025
1026                 if (!$failure) {
1027                         // This will be too low, but better than no value at all.
1028                         $registered_users = dba::count('gcontact', ['server_url' => normalise_link($server_url)]);
1029                 }
1030
1031                 // Look for poco
1032                 if (!$failure) {
1033                         $serverret = Network::curl($server_url."/poco");
1034                         if ($serverret["success"]) {
1035                                 $data = json_decode($serverret["body"]);
1036                                 if (isset($data->totalResults)) {
1037                                         $registered_users = $data->totalResults;
1038                                         $poco = $server_url."/poco";
1039                                         $server = self::detectPocoData($data);
1040                                         if ($server) {
1041                                                 $platform = $server['platform'];
1042                                                 $network = $server['network'];
1043                                                 $version = '';
1044                                                 $site_name = '';
1045                                         }
1046                                 }
1047                                 // There are servers out there who don't return 404 on a failure
1048                                 // We have to be sure that don't misunderstand this
1049                                 if (is_null($data)) {
1050                                         $poco = "";
1051                                         $noscrape = "";
1052                                         $network = "";
1053                                 }
1054                         }
1055                 }
1056
1057                 if (!$failure) {
1058                         // Test for Diaspora, Hubzilla, Mastodon or older Friendica servers
1059                         $serverret = Network::curl($server_url);
1060
1061                         if (!$serverret["success"] || ($serverret["body"] == "")) {
1062                                 $failure = true;
1063                         } else {
1064                                 $server = self::detectServerType($serverret["body"]);
1065                                 if ($server) {
1066                                         $platform = $server['platform'];
1067                                         $network = $server['network'];
1068                                         $version = $server['version'];
1069                                         $site_name = $server['site_name'];
1070                                 }
1071
1072                                 $lines = explode("\n", $serverret["header"]);
1073                                 if (count($lines)) {
1074                                         foreach ($lines as $line) {
1075                                                 $line = trim($line);
1076                                                 if (stristr($line, 'X-Diaspora-Version:')) {
1077                                                         $platform = "Diaspora";
1078                                                         $version = trim(str_replace("X-Diaspora-Version:", "", $line));
1079                                                         $version = trim(str_replace("x-diaspora-version:", "", $version));
1080                                                         $network = NETWORK_DIASPORA;
1081                                                         $versionparts = explode("-", $version);
1082                                                         $version = $versionparts[0];
1083                                                 }
1084
1085                                                 if (stristr($line, 'Server: Mastodon')) {
1086                                                         $platform = "Mastodon";
1087                                                         $network = NETWORK_OSTATUS;
1088                                                 }
1089                                         }
1090                                 }
1091                         }
1092                 }
1093
1094                 if (!$failure && ($poco == "")) {
1095                         // Test for Statusnet
1096                         // Will also return data for Friendica and GNU Social - but it will be overwritten later
1097                         // The "not implemented" is a special treatment for really, really old Friendica versions
1098                         $serverret = Network::curl($server_url."/api/statusnet/version.json");
1099                         if ($serverret["success"] && ($serverret["body"] != '{"error":"not implemented"}') &&
1100                                 ($serverret["body"] != '') && (strlen($serverret["body"]) < 30)) {
1101                                 $platform = "StatusNet";
1102                                 // Remove junk that some GNU Social servers return
1103                                 $version = str_replace(chr(239).chr(187).chr(191), "", $serverret["body"]);
1104                                 $version = trim($version, '"');
1105                                 $network = NETWORK_OSTATUS;
1106                         }
1107
1108                         // Test for GNU Social
1109                         $serverret = Network::curl($server_url."/api/gnusocial/version.json");
1110                         if ($serverret["success"] && ($serverret["body"] != '{"error":"not implemented"}') &&
1111                                 ($serverret["body"] != '') && (strlen($serverret["body"]) < 30)) {
1112                                 $platform = "GNU Social";
1113                                 // Remove junk that some GNU Social servers return
1114                                 $version = str_replace(chr(239).chr(187).chr(191), "", $serverret["body"]);
1115                                 $version = trim($version, '"');
1116                                 $network = NETWORK_OSTATUS;
1117                         }
1118
1119                         // Test for Mastodon
1120                         $orig_version = $version;
1121                         $serverret = Network::curl($server_url."/api/v1/instance");
1122                         if ($serverret["success"] && ($serverret["body"] != '')) {
1123                                 $data = json_decode($serverret["body"]);
1124
1125                                 if (isset($data->version)) {
1126                                         $platform = "Mastodon";
1127                                         $version = $data->version;
1128                                         $site_name = $data->title;
1129                                         $info = $data->description;
1130                                         $network = NETWORK_OSTATUS;
1131                                 }
1132                                 if (!empty($data->stats->user_count)) {
1133                                         $registered_users = $data->stats->user_count;
1134                                 }
1135                         }
1136                         if (strstr($orig_version.$version, 'Pleroma')) {
1137                                 $platform = 'Pleroma';
1138                                 $version = trim(str_replace('Pleroma', '', $version));
1139                         }
1140                 }
1141
1142                 if (!$failure) {
1143                         // Test for Hubzilla and Red
1144                         $serverret = Network::curl($server_url."/siteinfo.json");
1145                         if ($serverret["success"]) {
1146                                 $data = json_decode($serverret["body"]);
1147                                 if (isset($data->url)) {
1148                                         $platform = $data->platform;
1149                                         $version = $data->version;
1150                                         $network = NETWORK_DIASPORA;
1151                                 }
1152                                 if (!empty($data->site_name)) {
1153                                         $site_name = $data->site_name;
1154                                 }
1155                                 if (!empty($data->channels_total)) {
1156                                         $registered_users = $data->channels_total;
1157                                 }
1158                                 switch ($data->register_policy) {
1159                                         case "REGISTER_OPEN":
1160                                                 $register_policy = REGISTER_OPEN;
1161                                                 break;
1162                                         case "REGISTER_APPROVE":
1163                                                 $register_policy = REGISTER_APPROVE;
1164                                                 break;
1165                                         case "REGISTER_CLOSED":
1166                                         default:
1167                                                 $register_policy = REGISTER_CLOSED;
1168                                                 break;
1169                                 }
1170                         } else {
1171                                 // Test for Hubzilla, Redmatrix or Friendica
1172                                 $serverret = Network::curl($server_url."/api/statusnet/config.json");
1173                                 if ($serverret["success"]) {
1174                                         $data = json_decode($serverret["body"]);
1175                                         if (isset($data->site->server)) {
1176                                                 if (isset($data->site->platform)) {
1177                                                         $platform = $data->site->platform->PLATFORM_NAME;
1178                                                         $version = $data->site->platform->STD_VERSION;
1179                                                         $network = NETWORK_DIASPORA;
1180                                                 }
1181                                                 if (isset($data->site->BlaBlaNet)) {
1182                                                         $platform = $data->site->BlaBlaNet->PLATFORM_NAME;
1183                                                         $version = $data->site->BlaBlaNet->STD_VERSION;
1184                                                         $network = NETWORK_DIASPORA;
1185                                                 }
1186                                                 if (isset($data->site->hubzilla)) {
1187                                                         $platform = $data->site->hubzilla->PLATFORM_NAME;
1188                                                         $version = $data->site->hubzilla->RED_VERSION;
1189                                                         $network = NETWORK_DIASPORA;
1190                                                 }
1191                                                 if (isset($data->site->redmatrix)) {
1192                                                         if (isset($data->site->redmatrix->PLATFORM_NAME)) {
1193                                                                 $platform = $data->site->redmatrix->PLATFORM_NAME;
1194                                                         } elseif (isset($data->site->redmatrix->RED_PLATFORM)) {
1195                                                                 $platform = $data->site->redmatrix->RED_PLATFORM;
1196                                                         }
1197
1198                                                         $version = $data->site->redmatrix->RED_VERSION;
1199                                                         $network = NETWORK_DIASPORA;
1200                                                 }
1201                                                 if (isset($data->site->friendica)) {
1202                                                         $platform = $data->site->friendica->FRIENDICA_PLATFORM;
1203                                                         $version = $data->site->friendica->FRIENDICA_VERSION;
1204                                                         $network = NETWORK_DFRN;
1205                                                 }
1206
1207                                                 $site_name = $data->site->name;
1208
1209                                                 $data->site->closed = self::toBoolean($data->site->closed);
1210                                                 $data->site->private = self::toBoolean($data->site->private);
1211                                                 $data->site->inviteonly = self::toBoolean($data->site->inviteonly);
1212
1213                                                 if (!$data->site->closed && !$data->site->private and $data->site->inviteonly) {
1214                                                         $register_policy = REGISTER_APPROVE;
1215                                                 } elseif (!$data->site->closed && !$data->site->private) {
1216                                                         $register_policy = REGISTER_OPEN;
1217                                                 } else {
1218                                                         $register_policy = REGISTER_CLOSED;
1219                                                 }
1220                                         }
1221                                 }
1222                         }
1223                 }
1224
1225                 // Query statistics.json. Optional package for Diaspora, Friendica and Redmatrix
1226                 if (!$failure) {
1227                         $serverret = Network::curl($server_url."/statistics.json");
1228                         if ($serverret["success"]) {
1229                                 $data = json_decode($serverret["body"]);
1230
1231                                 if (isset($data->version)) {
1232                                         $version = $data->version;
1233                                         // Version numbers on statistics.json are presented with additional info, e.g.:
1234                                         // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
1235                                         $version = preg_replace("=(.+)-(.{4,})=ism", "$1", $version);
1236                                 }
1237
1238                                 if (!empty($data->name)) {
1239                                         $site_name = $data->name;
1240                                 }
1241
1242                                 if (!empty($data->network)) {
1243                                         $platform = $data->network;
1244                                 }
1245
1246                                 if ($platform == "Diaspora") {
1247                                         $network = NETWORK_DIASPORA;
1248                                 }
1249
1250                                 if ($data->registrations_open) {
1251                                         $register_policy = REGISTER_OPEN;
1252                                 } else {
1253                                         $register_policy = REGISTER_CLOSED;
1254                                 }
1255                         }
1256                 }
1257
1258                 // Query nodeinfo. Working for (at least) Diaspora and Friendica.
1259                 if (!$failure) {
1260                         $server = self::fetchNodeinfo($server_url);
1261                         if ($server) {
1262                                 $register_policy = $server['register_policy'];
1263
1264                                 if (isset($server['platform'])) {
1265                                         $platform = $server['platform'];
1266                                 }
1267
1268                                 if (isset($server['network'])) {
1269                                         $network = $server['network'];
1270                                 }
1271
1272                                 if (isset($server['version'])) {
1273                                         $version = $server['version'];
1274                                 }
1275
1276                                 if (isset($server['site_name'])) {
1277                                         $site_name = $server['site_name'];
1278                                 }
1279
1280                                 if (isset($server['registered-users'])) {
1281                                         $registered_users = $server['registered-users'];
1282                                 }
1283                         }
1284                 }
1285
1286                 // Check for noscrape
1287                 // Friendica servers could be detected as OStatus servers
1288                 if (!$failure && in_array($network, [NETWORK_DFRN, NETWORK_OSTATUS])) {
1289                         $serverret = Network::curl($server_url."/friendica/json");
1290
1291                         if (!$serverret["success"]) {
1292                                 $serverret = Network::curl($server_url."/friendika/json");
1293                         }
1294
1295                         if ($serverret["success"]) {
1296                                 $data = json_decode($serverret["body"]);
1297
1298                                 if (isset($data->version)) {
1299                                         $network = NETWORK_DFRN;
1300
1301                                         $noscrape = $data->no_scrape_url;
1302                                         $version = $data->version;
1303                                         $site_name = $data->site_name;
1304                                         $info = $data->info;
1305                                         $register_policy_str = $data->register_policy;
1306                                         $platform = $data->platform;
1307
1308                                         switch ($register_policy_str) {
1309                                                 case "REGISTER_CLOSED":
1310                                                         $register_policy = REGISTER_CLOSED;
1311                                                         break;
1312                                                 case "REGISTER_APPROVE":
1313                                                         $register_policy = REGISTER_APPROVE;
1314                                                         break;
1315                                                 case "REGISTER_OPEN":
1316                                                         $register_policy = REGISTER_OPEN;
1317                                                         break;
1318                                         }
1319                                 }
1320                         }
1321                 }
1322
1323                 // Every server has got at least an admin account
1324                 if (!$failure && ($registered_users == 0)) {
1325                         $registered_users = 1;
1326                 }
1327
1328                 if ($possible_failure && !$failure) {
1329                         $failure = true;
1330                 }
1331
1332                 if ($failure) {
1333                         $last_contact = $orig_last_contact;
1334                         $last_failure = DateTimeFormat::utcNow();
1335                 } else {
1336                         $last_contact = DateTimeFormat::utcNow();
1337                         $last_failure = $orig_last_failure;
1338                 }
1339
1340                 if (($last_contact <= $last_failure) && !$failure) {
1341                         logger("Server ".$server_url." seems to be alive, but last contact wasn't set - could be a bug", LOGGER_DEBUG);
1342                 } elseif (($last_contact >= $last_failure) && $failure) {
1343                         logger("Server ".$server_url." seems to be dead, but last failure wasn't set - could be a bug", LOGGER_DEBUG);
1344                 }
1345
1346                 // Check again if the server exists
1347                 $found = dba::exists('gserver', ['nurl' => normalise_link($server_url)]);
1348
1349                 $version = strip_tags($version);
1350                 $site_name = strip_tags($site_name);
1351                 $info = strip_tags($info);
1352                 $platform = strip_tags($platform);
1353
1354                 $fields = ['url' => $server_url, 'version' => $version,
1355                                 'site_name' => $site_name, 'info' => $info, 'register_policy' => $register_policy,
1356                                 'poco' => $poco, 'noscrape' => $noscrape, 'network' => $network,
1357                                 'platform' => $platform, 'registered-users' => $registered_users,
1358                                 'last_contact' => $last_contact, 'last_failure' => $last_failure];
1359
1360                 if ($found) {
1361                         dba::update('gserver', $fields, ['nurl' => normalise_link($server_url)]);
1362                 } elseif (!$failure) {
1363                         $fields['nurl'] = normalise_link($server_url);
1364                         $fields['created'] = DateTimeFormat::utcNow();
1365                         dba::insert('gserver', $fields);
1366                 }
1367                 logger("End discovery for server " . $server_url, LOGGER_DEBUG);
1368
1369                 return !$failure;
1370         }
1371
1372         /**
1373          * @brief Returns a list of all known servers
1374          * @return array List of server urls
1375          */
1376         public static function serverlist()
1377         {
1378                 $r = q(
1379                         "SELECT `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver`
1380                         WHERE `network` IN ('%s', '%s', '%s') AND `last_contact` > `last_failure`
1381                         ORDER BY `last_contact`
1382                         LIMIT 1000",
1383                         dbesc(NETWORK_DFRN),
1384                         dbesc(NETWORK_DIASPORA),
1385                         dbesc(NETWORK_OSTATUS)
1386                 );
1387
1388                 if (!DBM::is_result($r)) {
1389                         return false;
1390                 }
1391
1392                 return $r;
1393         }
1394
1395         /**
1396          * @brief Fetch server list from remote servers and adds them when they are new.
1397          *
1398          * @param string $poco URL to the POCO endpoint
1399          */
1400         private static function fetchServerlist($poco)
1401         {
1402                 $serverret = Network::curl($poco."/@server");
1403                 if (!$serverret["success"]) {
1404                         return;
1405                 }
1406                 $serverlist = json_decode($serverret['body']);
1407
1408                 if (!is_array($serverlist)) {
1409                         return;
1410                 }
1411
1412                 foreach ($serverlist as $server) {
1413                         $server_url = str_replace("/index.php", "", $server->url);
1414
1415                         $r = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
1416                         if (!DBM::is_result($r)) {
1417                                 logger("Call server check for server ".$server_url, LOGGER_DEBUG);
1418                                 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $server_url);
1419                         }
1420                 }
1421         }
1422
1423         private static function discoverFederation()
1424         {
1425                 $last = Config::get('poco', 'last_federation_discovery');
1426
1427                 if ($last) {
1428                         $next = $last + (24 * 60 * 60);
1429                         if ($next > time()) {
1430                                 return;
1431                         }
1432                 }
1433
1434                 // Discover Friendica, Hubzilla and Diaspora servers
1435                 $serverdata = Network::fetchUrl("http://the-federation.info/pods.json");
1436
1437                 if ($serverdata) {
1438                         $servers = json_decode($serverdata);
1439
1440                         foreach ($servers->pods as $server) {
1441                                 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", "https://".$server->host);
1442                         }
1443                 }
1444
1445                 // Disvover Mastodon servers
1446                 if (!Config::get('system', 'ostatus_disabled')) {
1447                         $accesstoken = Config::get('system', 'instances_social_key');
1448                         if (!empty($accesstoken)) {
1449                                 $api = 'https://instances.social/api/1.0/instances/list?count=0';
1450                                 $header = ['Authorization: Bearer '.$accesstoken];
1451                                 $serverdata = Network::curl($api, false, $redirects, ['headers' => $header]);
1452                                 if ($serverdata['success']) {
1453                                         $servers = json_decode($serverdata['body']);
1454                                         foreach ($servers->instances as $server) {
1455                                                 $url = (is_null($server->https_score) ? 'http' : 'https').'://'.$server->name;
1456                                                 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $url);
1457                                         }
1458                                 }
1459                         }
1460                 }
1461
1462                 // Currently disabled, since the service isn't available anymore.
1463                 // It is not removed since I hope that there will be a successor.
1464                 // Discover GNU Social Servers.
1465                 //if (!Config::get('system','ostatus_disabled')) {
1466                 //      $serverdata = "http://gstools.org/api/get_open_instances/";
1467
1468                 //      $result = Network::curl($serverdata);
1469                 //      if ($result["success"]) {
1470                 //              $servers = json_decode($result["body"]);
1471
1472                 //              foreach($servers->data as $server)
1473                 //                      self::checkServer($server->instance_address);
1474                 //      }
1475                 //}
1476
1477                 Config::set('poco', 'last_federation_discovery', time());
1478         }
1479
1480         public static function discoverSingleServer($id)
1481         {
1482                 $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `id` = %d", intval($id));
1483                 if (!DBM::is_result($r)) {
1484                         return false;
1485                 }
1486
1487                 $server = $r[0];
1488
1489                 // Discover new servers out there (Works from Friendica version 3.5.2)
1490                 self::fetchServerlist($server["poco"]);
1491
1492                 // Fetch all users from the other server
1493                 $url = $server["poco"]."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1494
1495                 logger("Fetch all users from the server ".$server["url"], LOGGER_DEBUG);
1496
1497                 $retdata = Network::curl($url);
1498                 if ($retdata["success"]) {
1499                         $data = json_decode($retdata["body"]);
1500
1501                         self::discoverServer($data, 2);
1502
1503                         if (Config::get('system', 'poco_discovery') > 1) {
1504                                 $timeframe = Config::get('system', 'poco_discovery_since');
1505                                 if ($timeframe == 0) {
1506                                         $timeframe = 30;
1507                                 }
1508
1509                                 $updatedSince = date(DateTimeFormat::MYSQL, time() - $timeframe * 86400);
1510
1511                                 // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3)
1512                                 $url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1513
1514                                 $success = false;
1515
1516                                 $retdata = Network::curl($url);
1517                                 if ($retdata["success"]) {
1518                                         logger("Fetch all global contacts from the server ".$server["nurl"], LOGGER_DEBUG);
1519                                         $success = self::discoverServer(json_decode($retdata["body"]));
1520                                 }
1521
1522                                 if (!$success && (Config::get('system', 'poco_discovery') > 2)) {
1523                                         logger("Fetch contacts from users of the server ".$server["nurl"], LOGGER_DEBUG);
1524                                         self::discoverServerUsers($data, $server);
1525                                 }
1526                         }
1527
1528                         $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
1529                         dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
1530
1531                         return true;
1532                 } else {
1533                         // If the server hadn't replied correctly, then force a sanity check
1534                         self::checkServer($server["url"], $server["network"], true);
1535
1536                         // If we couldn't reach the server, we will try it some time later
1537                         $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
1538                         dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
1539
1540                         return false;
1541                 }
1542         }
1543
1544         public static function discover($complete = false)
1545         {
1546                 // Update the server list
1547                 self::discoverFederation();
1548
1549                 $no_of_queries = 5;
1550
1551                 $requery_days = intval(Config::get("system", "poco_requery_days"));
1552
1553                 if ($requery_days == 0) {
1554                         $requery_days = 7;
1555                 }
1556                 $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
1557
1558                 $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));
1559                 if (DBM::is_result($r)) {
1560                         foreach ($r as $server) {
1561                                 if (!self::checkServer($server["url"], $server["network"])) {
1562                                         // The server is not reachable? Okay, then we will try it later
1563                                         $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
1564                                         dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
1565                                         continue;
1566                                 }
1567
1568                                 logger('Update directory from server '.$server['url'].' with ID '.$server['id'], LOGGER_DEBUG);
1569                                 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "update_server_directory", (int)$server['id']);
1570
1571                                 if (!$complete && (--$no_of_queries == 0)) {
1572                                         break;
1573                                 }
1574                         }
1575                 }
1576         }
1577
1578         private static function discoverServerUsers($data, $server)
1579         {
1580                 if (!isset($data->entry)) {
1581                         return;
1582                 }
1583
1584                 foreach ($data->entry as $entry) {
1585                         $username = "";
1586                         if (isset($entry->urls)) {
1587                                 foreach ($entry->urls as $url) {
1588                                         if ($url->type == 'profile') {
1589                                                 $profile_url = $url->value;
1590                                                 $urlparts = parse_url($profile_url);
1591                                                 $username = end(explode("/", $urlparts["path"]));
1592                                         }
1593                                 }
1594                         }
1595                         if ($username != "") {
1596                                 logger("Fetch contacts for the user ".$username." from the server ".$server["nurl"], LOGGER_DEBUG);
1597
1598                                 // Fetch all contacts from a given user from the other server
1599                                 $url = $server["poco"]."/".$username."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1600
1601                                 $retdata = Network::curl($url);
1602                                 if ($retdata["success"]) {
1603                                         self::discoverServer(json_decode($retdata["body"]), 3);
1604                                 }
1605                         }
1606                 }
1607         }
1608
1609         private static function discoverServer($data, $default_generation = 0)
1610         {
1611                 if (!isset($data->entry) || !count($data->entry)) {
1612                         return false;
1613                 }
1614
1615                 $success = false;
1616
1617                 foreach ($data->entry as $entry) {
1618                         $profile_url = '';
1619                         $profile_photo = '';
1620                         $connect_url = '';
1621                         $name = '';
1622                         $network = '';
1623                         $updated = NULL_DATE;
1624                         $location = '';
1625                         $about = '';
1626                         $keywords = '';
1627                         $gender = '';
1628                         $contact_type = -1;
1629                         $generation = $default_generation;
1630
1631                         $name = $entry->displayName;
1632
1633                         if (isset($entry->urls)) {
1634                                 foreach ($entry->urls as $url) {
1635                                         if ($url->type == 'profile') {
1636                                                 $profile_url = $url->value;
1637                                                 continue;
1638                                         }
1639                                         if ($url->type == 'webfinger') {
1640                                                 $connect_url = str_replace('acct:' , '', $url->value);
1641                                                 continue;
1642                                         }
1643                                 }
1644                         }
1645
1646                         if (isset($entry->photos)) {
1647                                 foreach ($entry->photos as $photo) {
1648                                         if ($photo->type == 'profile') {
1649                                                 $profile_photo = $photo->value;
1650                                                 continue;
1651                                         }
1652                                 }
1653                         }
1654
1655                         if (isset($entry->updated)) {
1656                                 $updated = date(DateTimeFormat::MYSQL, strtotime($entry->updated));
1657                         }
1658
1659                         if (isset($entry->network)) {
1660                                 $network = $entry->network;
1661                         }
1662
1663                         if (isset($entry->currentLocation)) {
1664                                 $location = $entry->currentLocation;
1665                         }
1666
1667                         if (isset($entry->aboutMe)) {
1668                                 $about = html2bbcode($entry->aboutMe);
1669                         }
1670
1671                         if (isset($entry->gender)) {
1672                                 $gender = $entry->gender;
1673                         }
1674
1675                         if (isset($entry->generation) && ($entry->generation > 0)) {
1676                                 $generation = ++$entry->generation;
1677                         }
1678
1679                         if (isset($entry->contactType) && ($entry->contactType >= 0)) {
1680                                 $contact_type = $entry->contactType;
1681                         }
1682
1683                         if (isset($entry->tags)) {
1684                                 foreach ($entry->tags as $tag) {
1685                                         $keywords = implode(", ", $tag);
1686                                 }
1687                         }
1688
1689                         if ($generation > 0) {
1690                                 $success = true;
1691
1692                                 logger("Store profile ".$profile_url, LOGGER_DEBUG);
1693
1694                                 $gcontact = ["url" => $profile_url,
1695                                                 "name" => $name,
1696                                                 "network" => $network,
1697                                                 "photo" => $profile_photo,
1698                                                 "about" => $about,
1699                                                 "location" => $location,
1700                                                 "gender" => $gender,
1701                                                 "keywords" => $keywords,
1702                                                 "connect" => $connect_url,
1703                                                 "updated" => $updated,
1704                                                 "contact-type" => $contact_type,
1705                                                 "generation" => $generation];
1706
1707                                 try {
1708                                         $gcontact = GContact::sanitize($gcontact);
1709                                         GContact::update($gcontact);
1710                                 } catch (Exception $e) {
1711                                         logger($e->getMessage(), LOGGER_DEBUG);
1712                                 }
1713
1714                                 logger("Done for profile ".$profile_url, LOGGER_DEBUG);
1715                         }
1716                 }
1717                 return $success;
1718         }
1719
1720 }