]> git.mxchange.org Git - friendica.git/blob - src/Protocol/PortableContact.php
Merge pull request #4429 from MrPetovan/bug/4426-handle-auth-exceptions
[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 = DateTimeFormat::utc($xpath->query('atom:published/text()', $entry)->item(0)->nodeValue);
505                         $updated   = DateTimeFormat::utc($xpath->query('atom:updated/text()'  , $entry)->item(0)->nodeValue);
506
507                         if ($last_updated < $published) {
508                                 $last_updated = $published;
509                         }
510
511                         if ($last_updated < $updated) {
512                                 $last_updated = $updated;
513                         }
514                 }
515
516                 // Maybe there aren't any entries. Then check if it is a valid feed
517                 if ($last_updated == "") {
518                         if ($xpath->query('/atom:feed')->length > 0) {
519                                 $last_updated = NULL_DATE;
520                         }
521                 }
522
523                 $fields = ['updated' => $last_updated, 'last_contact' => DateTimeFormat::utcNow()];
524                 dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
525
526                 if (($gcontacts[0]["generation"] == 0)) {
527                         $fields = ['generation' => 9];
528                         dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
529                 }
530
531                 logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG);
532
533                 return $last_updated;
534         }
535
536         public static function updateNeeded($created, $updated, $last_failure, $last_contact)
537         {
538                 $now = strtotime(DateTimeFormat::utcNow());
539
540                 if ($updated > $last_contact) {
541                         $contact_time = strtotime($updated);
542                 } else {
543                         $contact_time = strtotime($last_contact);
544                 }
545
546                 $failure_time = strtotime($last_failure);
547                 $created_time = strtotime($created);
548
549                 // If there is no "created" time then use the current time
550                 if ($created_time <= 0) {
551                         $created_time = $now;
552                 }
553
554                 // If the last contact was less than 24 hours then don't update
555                 if (($now - $contact_time) < (60 * 60 * 24)) {
556                         return false;
557                 }
558
559                 // If the last failure was less than 24 hours then don't update
560                 if (($now - $failure_time) < (60 * 60 * 24)) {
561                         return false;
562                 }
563
564                 // If the last contact was less than a week ago and the last failure is older than a week then don't update
565                 //if ((($now - $contact_time) < (60 * 60 * 24 * 7)) && ($contact_time > $failure_time))
566                 //      return false;
567
568                 // 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
569                 if ((($now - $contact_time) > (60 * 60 * 24 * 7)) && (($now - $created_time) > (60 * 60 * 24 * 7)) && (($now - $failure_time) < (60 * 60 * 24 * 7))) {
570                         return false;
571                 }
572
573                 // 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
574                 if ((($now - $contact_time) > (60 * 60 * 24 * 30)) && (($now - $created_time) > (60 * 60 * 24 * 30)) && (($now - $failure_time) < (60 * 60 * 24 * 30))) {
575                         return false;
576                 }
577
578                 return true;
579         }
580
581         private static function toBoolean($val)
582         {
583                 if (($val == "true") || ($val == 1)) {
584                         return true;
585                 } elseif (($val == "false") || ($val == 0)) {
586                         return false;
587                 }
588
589                 return $val;
590         }
591
592         /**
593          * @brief Detect server type (Hubzilla or Friendica) via the poco data
594          *
595          * @param object $data POCO data
596          * @return array Server data
597          */
598         private static function detectPocoData($data)
599         {
600                 $server = false;
601
602                 if (!isset($data->entry)) {
603                         return false;
604                 }
605
606                 if (count($data->entry) == 0) {
607                         return false;
608                 }
609
610                 if (!isset($data->entry[0]->urls)) {
611                         return false;
612                 }
613
614                 if (count($data->entry[0]->urls) == 0) {
615                         return false;
616                 }
617
618                 foreach ($data->entry[0]->urls as $url) {
619                         if ($url->type == 'zot') {
620                                 $server = [];
621                                 $server["platform"] = 'Hubzilla';
622                                 $server["network"] = NETWORK_DIASPORA;
623                                 return $server;
624                         }
625                 }
626                 return false;
627         }
628
629         /**
630          * @brief Detect server type by using the nodeinfo data
631          *
632          * @param string $server_url address of the server
633          * @return array Server data
634          */
635         private static function fetchNodeinfo($server_url)
636         {
637                 $serverret = Network::curl($server_url."/.well-known/nodeinfo");
638                 if (!$serverret["success"]) {
639                         return false;
640                 }
641
642                 $nodeinfo = json_decode($serverret['body']);
643
644                 if (!is_object($nodeinfo)) {
645                         return false;
646                 }
647
648                 if (!is_array($nodeinfo->links)) {
649                         return false;
650                 }
651
652                 $nodeinfo1_url = '';
653                 $nodeinfo2_url = '';
654
655                 foreach ($nodeinfo->links as $link) {
656                         if ($link->rel == 'http://nodeinfo.diaspora.software/ns/schema/1.0') {
657                                 $nodeinfo1_url = $link->href;
658                         }
659                         if ($link->rel == 'http://nodeinfo.diaspora.software/ns/schema/2.0') {
660                                 $nodeinfo2_url = $link->href;
661                         }
662                 }
663
664                 if ($nodeinfo1_url . $nodeinfo2_url == '') {
665                         return false;
666                 }
667
668                 $server = [];
669
670                 // When the nodeinfo url isn't on the same host, then there is obviously something wrong
671                 if (!empty($nodeinfo2_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo2_url, PHP_URL_HOST))) {
672                         $server = self::parseNodeinfo2($nodeinfo2_url);
673                 }
674
675                 // When the nodeinfo url isn't on the same host, then there is obviously something wrong
676                 if (empty($server) && !empty($nodeinfo1_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo1_url, PHP_URL_HOST))) {
677                         $server = self::parseNodeinfo1($nodeinfo1_url);
678                 }
679
680                 return $server;
681         }
682
683         /**
684          * @brief Parses Nodeinfo 1
685          *
686          * @param string $nodeinfo_url address of the nodeinfo path
687          * @return array Server data
688          */
689         private static function parseNodeinfo1($nodeinfo_url)
690         {
691                 $serverret = Network::curl($nodeinfo_url);
692                 if (!$serverret["success"]) {
693                         return false;
694                 }
695
696                 $nodeinfo = json_decode($serverret['body']);
697                 if (!is_object($nodeinfo)) {
698                         return false;
699                 }
700
701                 $server = [];
702
703                 $server['register_policy'] = REGISTER_CLOSED;
704
705                 if (is_bool($nodeinfo->openRegistrations) && $nodeinfo->openRegistrations) {
706                         $server['register_policy'] = REGISTER_OPEN;
707                 }
708
709                 if (is_object($nodeinfo->software)) {
710                         if (isset($nodeinfo->software->name)) {
711                                 $server['platform'] = $nodeinfo->software->name;
712                         }
713
714                         if (isset($nodeinfo->software->version)) {
715                                 $server['version'] = $nodeinfo->software->version;
716                                 // Version numbers on Nodeinfo are presented with additional info, e.g.:
717                                 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
718                                 $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']);
719                         }
720                 }
721
722                 if (is_object($nodeinfo->metadata)) {
723                         if (isset($nodeinfo->metadata->nodeName)) {
724                                 $server['site_name'] = $nodeinfo->metadata->nodeName;
725                         }
726                 }
727
728                 if (!empty($nodeinfo->usage->users->total)) {
729                         $server['registered-users'] = $nodeinfo->usage->users->total;
730                 }
731
732                 $diaspora = false;
733                 $friendica = false;
734                 $gnusocial = false;
735
736                 if (is_array($nodeinfo->protocols->inbound)) {
737                         foreach ($nodeinfo->protocols->inbound as $inbound) {
738                                 if ($inbound == 'diaspora') {
739                                         $diaspora = true;
740                                 }
741                                 if ($inbound == 'friendica') {
742                                         $friendica = true;
743                                 }
744                                 if ($inbound == 'gnusocial') {
745                                         $gnusocial = true;
746                                 }
747                         }
748                 }
749
750                 if ($gnusocial) {
751                         $server['network'] = NETWORK_OSTATUS;
752                 }
753                 if ($diaspora) {
754                         $server['network'] = NETWORK_DIASPORA;
755                 }
756                 if ($friendica) {
757                         $server['network'] = NETWORK_DFRN;
758                 }
759
760                 if (!$server) {
761                         return false;
762                 }
763
764                 return $server;
765         }
766
767         /**
768          * @brief Parses Nodeinfo 2
769          *
770          * @param string $nodeinfo_url address of the nodeinfo path
771          * @return array Server data
772          */
773         private static function parseNodeinfo2($nodeinfo_url)
774         {
775                 $serverret = Network::curl($nodeinfo_url);
776                 if (!$serverret["success"]) {
777                         return false;
778                 }
779
780                 $nodeinfo = json_decode($serverret['body']);
781                 if (!is_object($nodeinfo)) {
782                         return false;
783                 }
784
785                 $server = [];
786
787                 $server['register_policy'] = REGISTER_CLOSED;
788
789                 if (is_bool($nodeinfo->openRegistrations) && $nodeinfo->openRegistrations) {
790                         $server['register_policy'] = REGISTER_OPEN;
791                 }
792
793                 if (is_object($nodeinfo->software)) {
794                         if (isset($nodeinfo->software->name)) {
795                                 $server['platform'] = $nodeinfo->software->name;
796                         }
797
798                         if (isset($nodeinfo->software->version)) {
799                                 $server['version'] = $nodeinfo->software->version;
800                                 // Version numbers on Nodeinfo are presented with additional info, e.g.:
801                                 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
802                                 $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']);
803                         }
804                 }
805
806                 if (is_object($nodeinfo->metadata)) {
807                         if (isset($nodeinfo->metadata->nodeName)) {
808                                 $server['site_name'] = $nodeinfo->metadata->nodeName;
809                         }
810                 }
811
812                 if (!empty($nodeinfo->usage->users->total)) {
813                         $server['registered-users'] = $nodeinfo->usage->users->total;
814                 }
815
816                 $diaspora = false;
817                 $friendica = false;
818                 $gnusocial = false;
819
820                 if (is_array($nodeinfo->protocols)) {
821                         foreach ($nodeinfo->protocols as $protocol) {
822                                 if ($protocol == 'diaspora') {
823                                         $diaspora = true;
824                                 }
825                                 if ($protocol == 'friendica') {
826                                         $friendica = true;
827                                 }
828                                 if ($protocol == 'gnusocial') {
829                                         $gnusocial = true;
830                                 }
831                         }
832                 }
833
834                 if ($gnusocial) {
835                         $server['network'] = NETWORK_OSTATUS;
836                 }
837                 if ($diaspora) {
838                         $server['network'] = NETWORK_DIASPORA;
839                 }
840                 if ($friendica) {
841                         $server['network'] = NETWORK_DFRN;
842                 }
843
844                 if (!$server) {
845                         return false;
846                 }
847
848                 return $server;
849         }
850
851         /**
852          * @brief Detect server type (Hubzilla or Friendica) via the front page body
853          *
854          * @param string $body Front page of the server
855          * @return array Server data
856          */
857         private static function detectServerType($body)
858         {
859                 $server = false;
860
861                 $doc = new DOMDocument();
862                 @$doc->loadHTML($body);
863                 $xpath = new DOMXPath($doc);
864
865                 $list = $xpath->query("//meta[@name]");
866
867                 foreach ($list as $node) {
868                         $attr = [];
869                         if ($node->attributes->length) {
870                                 foreach ($node->attributes as $attribute) {
871                                         $attr[$attribute->name] = $attribute->value;
872                                 }
873                         }
874                         if ($attr['name'] == 'generator') {
875                                 $version_part = explode(" ", $attr['content']);
876                                 if (count($version_part) == 2) {
877                                         if (in_array($version_part[0], ["Friendika", "Friendica"])) {
878                                                 $server = [];
879                                                 $server["platform"] = $version_part[0];
880                                                 $server["version"] = $version_part[1];
881                                                 $server["network"] = NETWORK_DFRN;
882                                         }
883                                 }
884                         }
885                 }
886
887                 if (!$server) {
888                         $list = $xpath->query("//meta[@property]");
889
890                         foreach ($list as $node) {
891                                 $attr = [];
892                                 if ($node->attributes->length) {
893                                         foreach ($node->attributes as $attribute) {
894                                                 $attr[$attribute->name] = $attribute->value;
895                                         }
896                                 }
897                                 if ($attr['property'] == 'generator' && in_array($attr['content'], ["hubzilla", "BlaBlaNet"])) {
898                                         $server = [];
899                                         $server["platform"] = $attr['content'];
900                                         $server["version"] = "";
901                                         $server["network"] = NETWORK_DIASPORA;
902                                 }
903                         }
904                 }
905
906                 if (!$server) {
907                         return false;
908                 }
909
910                 $server["site_name"] = $xpath->evaluate("//head/title/text()")->item(0)->nodeValue;
911                 return $server;
912         }
913
914         public static function checkServer($server_url, $network = "", $force = false)
915         {
916                 // Unify the server address
917                 $server_url = trim($server_url, "/");
918                 $server_url = str_replace("/index.php", "", $server_url);
919
920                 if ($server_url == "") {
921                         return false;
922                 }
923
924                 $gserver = dba::selectFirst('gserver', [], ['nurl' => normalise_link($server_url)]);
925                 if (DBM::is_result($gserver)) {
926                         if ($gserver["created"] <= NULL_DATE) {
927                                 $fields = ['created' => DateTimeFormat::utcNow()];
928                                 $condition = ['nurl' => normalise_link($server_url)];
929                                 dba::update('gserver', $fields, $condition);
930                         }
931                         $poco = $gserver["poco"];
932                         $noscrape = $gserver["noscrape"];
933
934                         if ($network == "") {
935                                 $network = $gserver["network"];
936                         }
937
938                         $last_contact = $gserver["last_contact"];
939                         $last_failure = $gserver["last_failure"];
940                         $version = $gserver["version"];
941                         $platform = $gserver["platform"];
942                         $site_name = $gserver["site_name"];
943                         $info = $gserver["info"];
944                         $register_policy = $gserver["register_policy"];
945                         $registered_users = $gserver["registered-users"];
946
947                         if (!$force && !self::updateNeeded($gserver["created"], "", $last_failure, $last_contact)) {
948                                 logger("Use cached data for server ".$server_url, LOGGER_DEBUG);
949                                 return ($last_contact >= $last_failure);
950                         }
951                 } else {
952                         $poco = "";
953                         $noscrape = "";
954                         $version = "";
955                         $platform = "";
956                         $site_name = "";
957                         $info = "";
958                         $register_policy = -1;
959                         $registered_users = 0;
960
961                         $last_contact = NULL_DATE;
962                         $last_failure = NULL_DATE;
963                 }
964                 logger("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$gserver["created"]." Failure: ".$last_failure." Contact: ".$last_contact, LOGGER_DEBUG);
965
966                 $failure = false;
967                 $possible_failure = false;
968                 $orig_last_failure = $last_failure;
969                 $orig_last_contact = $last_contact;
970
971                 // Mastodon uses the "@" for user profiles.
972                 // But this can be misunderstood.
973                 if (parse_url($server_url, PHP_URL_USER) != '') {
974                         dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
975                         return false;
976                 }
977
978                 // Check if the page is accessible via SSL.
979                 $orig_server_url = $server_url;
980                 $server_url = str_replace("http://", "https://", $server_url);
981
982                 // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
983                 $serverret = Network::curl($server_url."/.well-known/host-meta", false, $redirects, ['timeout' => 20]);
984
985                 // Quit if there is a timeout.
986                 // But we want to make sure to only quit if we are mostly sure that this server url fits.
987                 if (DBM::is_result($gserver) && ($orig_server_url == $server_url) &&
988                         ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT)) {
989                         logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
990                         dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
991                         return false;
992                 }
993
994                 // Maybe the page is unencrypted only?
995                 $xmlobj = @simplexml_load_string($serverret["body"], 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
996                 if (!$serverret["success"] || ($serverret["body"] == "") || (@sizeof($xmlobj) == 0) || !is_object($xmlobj)) {
997                         $server_url = str_replace("https://", "http://", $server_url);
998
999                         // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
1000                         $serverret = Network::curl($server_url."/.well-known/host-meta", false, $redirects, ['timeout' => 20]);
1001
1002                         // Quit if there is a timeout
1003                         if ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT) {
1004                                 logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
1005                                 dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
1006                                 return false;
1007                         }
1008
1009                         $xmlobj = @simplexml_load_string($serverret["body"], 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
1010                 }
1011
1012                 if (!$serverret["success"] || ($serverret["body"] == "") || (sizeof($xmlobj) == 0) || !is_object($xmlobj)) {
1013                         // Workaround for bad configured servers (known nginx problem)
1014                         if (!in_array($serverret["debug"]["http_code"], ["403", "404"])) {
1015                                 $failure = true;
1016                         }
1017                         $possible_failure = true;
1018                 }
1019
1020                 // If the server has no possible failure we reset the cached data
1021                 if (!$possible_failure) {
1022                         $version = "";
1023                         $platform = "";
1024                         $site_name = "";
1025                         $info = "";
1026                         $register_policy = -1;
1027                 }
1028
1029                 if (!$failure) {
1030                         // This will be too low, but better than no value at all.
1031                         $registered_users = dba::count('gcontact', ['server_url' => normalise_link($server_url)]);
1032                 }
1033
1034                 // Look for poco
1035                 if (!$failure) {
1036                         $serverret = Network::curl($server_url."/poco");
1037                         if ($serverret["success"]) {
1038                                 $data = json_decode($serverret["body"]);
1039                                 if (isset($data->totalResults)) {
1040                                         $registered_users = $data->totalResults;
1041                                         $poco = $server_url."/poco";
1042                                         $server = self::detectPocoData($data);
1043                                         if ($server) {
1044                                                 $platform = $server['platform'];
1045                                                 $network = $server['network'];
1046                                                 $version = '';
1047                                                 $site_name = '';
1048                                         }
1049                                 }
1050                                 // There are servers out there who don't return 404 on a failure
1051                                 // We have to be sure that don't misunderstand this
1052                                 if (is_null($data)) {
1053                                         $poco = "";
1054                                         $noscrape = "";
1055                                         $network = "";
1056                                 }
1057                         }
1058                 }
1059
1060                 if (!$failure) {
1061                         // Test for Diaspora, Hubzilla, Mastodon or older Friendica servers
1062                         $serverret = Network::curl($server_url);
1063
1064                         if (!$serverret["success"] || ($serverret["body"] == "")) {
1065                                 $failure = true;
1066                         } else {
1067                                 $server = self::detectServerType($serverret["body"]);
1068                                 if ($server) {
1069                                         $platform = $server['platform'];
1070                                         $network = $server['network'];
1071                                         $version = $server['version'];
1072                                         $site_name = $server['site_name'];
1073                                 }
1074
1075                                 $lines = explode("\n", $serverret["header"]);
1076                                 if (count($lines)) {
1077                                         foreach ($lines as $line) {
1078                                                 $line = trim($line);
1079                                                 if (stristr($line, 'X-Diaspora-Version:')) {
1080                                                         $platform = "Diaspora";
1081                                                         $version = trim(str_replace("X-Diaspora-Version:", "", $line));
1082                                                         $version = trim(str_replace("x-diaspora-version:", "", $version));
1083                                                         $network = NETWORK_DIASPORA;
1084                                                         $versionparts = explode("-", $version);
1085                                                         $version = $versionparts[0];
1086                                                 }
1087
1088                                                 if (stristr($line, 'Server: Mastodon')) {
1089                                                         $platform = "Mastodon";
1090                                                         $network = NETWORK_OSTATUS;
1091                                                 }
1092                                         }
1093                                 }
1094                         }
1095                 }
1096
1097                 if (!$failure && ($poco == "")) {
1098                         // Test for Statusnet
1099                         // Will also return data for Friendica and GNU Social - but it will be overwritten later
1100                         // The "not implemented" is a special treatment for really, really old Friendica versions
1101                         $serverret = Network::curl($server_url."/api/statusnet/version.json");
1102                         if ($serverret["success"] && ($serverret["body"] != '{"error":"not implemented"}') &&
1103                                 ($serverret["body"] != '') && (strlen($serverret["body"]) < 30)) {
1104                                 $platform = "StatusNet";
1105                                 // Remove junk that some GNU Social servers return
1106                                 $version = str_replace(chr(239).chr(187).chr(191), "", $serverret["body"]);
1107                                 $version = trim($version, '"');
1108                                 $network = NETWORK_OSTATUS;
1109                         }
1110
1111                         // Test for GNU Social
1112                         $serverret = Network::curl($server_url."/api/gnusocial/version.json");
1113                         if ($serverret["success"] && ($serverret["body"] != '{"error":"not implemented"}') &&
1114                                 ($serverret["body"] != '') && (strlen($serverret["body"]) < 30)) {
1115                                 $platform = "GNU Social";
1116                                 // Remove junk that some GNU Social servers return
1117                                 $version = str_replace(chr(239).chr(187).chr(191), "", $serverret["body"]);
1118                                 $version = trim($version, '"');
1119                                 $network = NETWORK_OSTATUS;
1120                         }
1121
1122                         // Test for Mastodon
1123                         $orig_version = $version;
1124                         $serverret = Network::curl($server_url."/api/v1/instance");
1125                         if ($serverret["success"] && ($serverret["body"] != '')) {
1126                                 $data = json_decode($serverret["body"]);
1127
1128                                 if (isset($data->version)) {
1129                                         $platform = "Mastodon";
1130                                         $version = $data->version;
1131                                         $site_name = $data->title;
1132                                         $info = $data->description;
1133                                         $network = NETWORK_OSTATUS;
1134                                 }
1135                                 if (!empty($data->stats->user_count)) {
1136                                         $registered_users = $data->stats->user_count;
1137                                 }
1138                         }
1139                         if (strstr($orig_version.$version, 'Pleroma')) {
1140                                 $platform = 'Pleroma';
1141                                 $version = trim(str_replace('Pleroma', '', $version));
1142                         }
1143                 }
1144
1145                 if (!$failure) {
1146                         // Test for Hubzilla and Red
1147                         $serverret = Network::curl($server_url."/siteinfo.json");
1148                         if ($serverret["success"]) {
1149                                 $data = json_decode($serverret["body"]);
1150                                 if (isset($data->url)) {
1151                                         $platform = $data->platform;
1152                                         $version = $data->version;
1153                                         $network = NETWORK_DIASPORA;
1154                                 }
1155                                 if (!empty($data->site_name)) {
1156                                         $site_name = $data->site_name;
1157                                 }
1158                                 if (!empty($data->channels_total)) {
1159                                         $registered_users = $data->channels_total;
1160                                 }
1161                                 switch ($data->register_policy) {
1162                                         case "REGISTER_OPEN":
1163                                                 $register_policy = REGISTER_OPEN;
1164                                                 break;
1165                                         case "REGISTER_APPROVE":
1166                                                 $register_policy = REGISTER_APPROVE;
1167                                                 break;
1168                                         case "REGISTER_CLOSED":
1169                                         default:
1170                                                 $register_policy = REGISTER_CLOSED;
1171                                                 break;
1172                                 }
1173                         } else {
1174                                 // Test for Hubzilla, Redmatrix or Friendica
1175                                 $serverret = Network::curl($server_url."/api/statusnet/config.json");
1176                                 if ($serverret["success"]) {
1177                                         $data = json_decode($serverret["body"]);
1178                                         if (isset($data->site->server)) {
1179                                                 if (isset($data->site->platform)) {
1180                                                         $platform = $data->site->platform->PLATFORM_NAME;
1181                                                         $version = $data->site->platform->STD_VERSION;
1182                                                         $network = NETWORK_DIASPORA;
1183                                                 }
1184                                                 if (isset($data->site->BlaBlaNet)) {
1185                                                         $platform = $data->site->BlaBlaNet->PLATFORM_NAME;
1186                                                         $version = $data->site->BlaBlaNet->STD_VERSION;
1187                                                         $network = NETWORK_DIASPORA;
1188                                                 }
1189                                                 if (isset($data->site->hubzilla)) {
1190                                                         $platform = $data->site->hubzilla->PLATFORM_NAME;
1191                                                         $version = $data->site->hubzilla->RED_VERSION;
1192                                                         $network = NETWORK_DIASPORA;
1193                                                 }
1194                                                 if (isset($data->site->redmatrix)) {
1195                                                         if (isset($data->site->redmatrix->PLATFORM_NAME)) {
1196                                                                 $platform = $data->site->redmatrix->PLATFORM_NAME;
1197                                                         } elseif (isset($data->site->redmatrix->RED_PLATFORM)) {
1198                                                                 $platform = $data->site->redmatrix->RED_PLATFORM;
1199                                                         }
1200
1201                                                         $version = $data->site->redmatrix->RED_VERSION;
1202                                                         $network = NETWORK_DIASPORA;
1203                                                 }
1204                                                 if (isset($data->site->friendica)) {
1205                                                         $platform = $data->site->friendica->FRIENDICA_PLATFORM;
1206                                                         $version = $data->site->friendica->FRIENDICA_VERSION;
1207                                                         $network = NETWORK_DFRN;
1208                                                 }
1209
1210                                                 $site_name = $data->site->name;
1211
1212                                                 $data->site->closed = self::toBoolean($data->site->closed);
1213                                                 $data->site->private = self::toBoolean($data->site->private);
1214                                                 $data->site->inviteonly = self::toBoolean($data->site->inviteonly);
1215
1216                                                 if (!$data->site->closed && !$data->site->private and $data->site->inviteonly) {
1217                                                         $register_policy = REGISTER_APPROVE;
1218                                                 } elseif (!$data->site->closed && !$data->site->private) {
1219                                                         $register_policy = REGISTER_OPEN;
1220                                                 } else {
1221                                                         $register_policy = REGISTER_CLOSED;
1222                                                 }
1223                                         }
1224                                 }
1225                         }
1226                 }
1227
1228                 // Query statistics.json. Optional package for Diaspora, Friendica and Redmatrix
1229                 if (!$failure) {
1230                         $serverret = Network::curl($server_url."/statistics.json");
1231                         if ($serverret["success"]) {
1232                                 $data = json_decode($serverret["body"]);
1233
1234                                 if (isset($data->version)) {
1235                                         $version = $data->version;
1236                                         // Version numbers on statistics.json are presented with additional info, e.g.:
1237                                         // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
1238                                         $version = preg_replace("=(.+)-(.{4,})=ism", "$1", $version);
1239                                 }
1240
1241                                 if (!empty($data->name)) {
1242                                         $site_name = $data->name;
1243                                 }
1244
1245                                 if (!empty($data->network)) {
1246                                         $platform = $data->network;
1247                                 }
1248
1249                                 if ($platform == "Diaspora") {
1250                                         $network = NETWORK_DIASPORA;
1251                                 }
1252
1253                                 if ($data->registrations_open) {
1254                                         $register_policy = REGISTER_OPEN;
1255                                 } else {
1256                                         $register_policy = REGISTER_CLOSED;
1257                                 }
1258                         }
1259                 }
1260
1261                 // Query nodeinfo. Working for (at least) Diaspora and Friendica.
1262                 if (!$failure) {
1263                         $server = self::fetchNodeinfo($server_url);
1264                         if ($server) {
1265                                 $register_policy = $server['register_policy'];
1266
1267                                 if (isset($server['platform'])) {
1268                                         $platform = $server['platform'];
1269                                 }
1270
1271                                 if (isset($server['network'])) {
1272                                         $network = $server['network'];
1273                                 }
1274
1275                                 if (isset($server['version'])) {
1276                                         $version = $server['version'];
1277                                 }
1278
1279                                 if (isset($server['site_name'])) {
1280                                         $site_name = $server['site_name'];
1281                                 }
1282
1283                                 if (isset($server['registered-users'])) {
1284                                         $registered_users = $server['registered-users'];
1285                                 }
1286                         }
1287                 }
1288
1289                 // Check for noscrape
1290                 // Friendica servers could be detected as OStatus servers
1291                 if (!$failure && in_array($network, [NETWORK_DFRN, NETWORK_OSTATUS])) {
1292                         $serverret = Network::curl($server_url."/friendica/json");
1293
1294                         if (!$serverret["success"]) {
1295                                 $serverret = Network::curl($server_url."/friendika/json");
1296                         }
1297
1298                         if ($serverret["success"]) {
1299                                 $data = json_decode($serverret["body"]);
1300
1301                                 if (isset($data->version)) {
1302                                         $network = NETWORK_DFRN;
1303
1304                                         $noscrape = $data->no_scrape_url;
1305                                         $version = $data->version;
1306                                         $site_name = $data->site_name;
1307                                         $info = $data->info;
1308                                         $register_policy_str = $data->register_policy;
1309                                         $platform = $data->platform;
1310
1311                                         switch ($register_policy_str) {
1312                                                 case "REGISTER_CLOSED":
1313                                                         $register_policy = REGISTER_CLOSED;
1314                                                         break;
1315                                                 case "REGISTER_APPROVE":
1316                                                         $register_policy = REGISTER_APPROVE;
1317                                                         break;
1318                                                 case "REGISTER_OPEN":
1319                                                         $register_policy = REGISTER_OPEN;
1320                                                         break;
1321                                         }
1322                                 }
1323                         }
1324                 }
1325
1326                 // Every server has got at least an admin account
1327                 if (!$failure && ($registered_users == 0)) {
1328                         $registered_users = 1;
1329                 }
1330
1331                 if ($possible_failure && !$failure) {
1332                         $failure = true;
1333                 }
1334
1335                 if ($failure) {
1336                         $last_contact = $orig_last_contact;
1337                         $last_failure = DateTimeFormat::utcNow();
1338                 } else {
1339                         $last_contact = DateTimeFormat::utcNow();
1340                         $last_failure = $orig_last_failure;
1341                 }
1342
1343                 if (($last_contact <= $last_failure) && !$failure) {
1344                         logger("Server ".$server_url." seems to be alive, but last contact wasn't set - could be a bug", LOGGER_DEBUG);
1345                 } elseif (($last_contact >= $last_failure) && $failure) {
1346                         logger("Server ".$server_url." seems to be dead, but last failure wasn't set - could be a bug", LOGGER_DEBUG);
1347                 }
1348
1349                 // Check again if the server exists
1350                 $found = dba::exists('gserver', ['nurl' => normalise_link($server_url)]);
1351
1352                 $version = strip_tags($version);
1353                 $site_name = strip_tags($site_name);
1354                 $info = strip_tags($info);
1355                 $platform = strip_tags($platform);
1356
1357                 $fields = ['url' => $server_url, 'version' => $version,
1358                                 'site_name' => $site_name, 'info' => $info, 'register_policy' => $register_policy,
1359                                 'poco' => $poco, 'noscrape' => $noscrape, 'network' => $network,
1360                                 'platform' => $platform, 'registered-users' => $registered_users,
1361                                 'last_contact' => $last_contact, 'last_failure' => $last_failure];
1362
1363                 if ($found) {
1364                         dba::update('gserver', $fields, ['nurl' => normalise_link($server_url)]);
1365                 } elseif (!$failure) {
1366                         $fields['nurl'] = normalise_link($server_url);
1367                         $fields['created'] = DateTimeFormat::utcNow();
1368                         dba::insert('gserver', $fields);
1369                 }
1370                 logger("End discovery for server " . $server_url, LOGGER_DEBUG);
1371
1372                 return !$failure;
1373         }
1374
1375         /**
1376          * @brief Returns a list of all known servers
1377          * @return array List of server urls
1378          */
1379         public static function serverlist()
1380         {
1381                 $r = q(
1382                         "SELECT `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver`
1383                         WHERE `network` IN ('%s', '%s', '%s') AND `last_contact` > `last_failure`
1384                         ORDER BY `last_contact`
1385                         LIMIT 1000",
1386                         dbesc(NETWORK_DFRN),
1387                         dbesc(NETWORK_DIASPORA),
1388                         dbesc(NETWORK_OSTATUS)
1389                 );
1390
1391                 if (!DBM::is_result($r)) {
1392                         return false;
1393                 }
1394
1395                 return $r;
1396         }
1397
1398         /**
1399          * @brief Fetch server list from remote servers and adds them when they are new.
1400          *
1401          * @param string $poco URL to the POCO endpoint
1402          */
1403         private static function fetchServerlist($poco)
1404         {
1405                 $serverret = Network::curl($poco."/@server");
1406                 if (!$serverret["success"]) {
1407                         return;
1408                 }
1409                 $serverlist = json_decode($serverret['body']);
1410
1411                 if (!is_array($serverlist)) {
1412                         return;
1413                 }
1414
1415                 foreach ($serverlist as $server) {
1416                         $server_url = str_replace("/index.php", "", $server->url);
1417
1418                         $r = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
1419                         if (!DBM::is_result($r)) {
1420                                 logger("Call server check for server ".$server_url, LOGGER_DEBUG);
1421                                 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $server_url);
1422                         }
1423                 }
1424         }
1425
1426         private static function discoverFederation()
1427         {
1428                 $last = Config::get('poco', 'last_federation_discovery');
1429
1430                 if ($last) {
1431                         $next = $last + (24 * 60 * 60);
1432                         if ($next > time()) {
1433                                 return;
1434                         }
1435                 }
1436
1437                 // Discover Friendica, Hubzilla and Diaspora servers
1438                 $serverdata = Network::fetchUrl("http://the-federation.info/pods.json");
1439
1440                 if ($serverdata) {
1441                         $servers = json_decode($serverdata);
1442
1443                         foreach ($servers->pods as $server) {
1444                                 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", "https://".$server->host);
1445                         }
1446                 }
1447
1448                 // Disvover Mastodon servers
1449                 if (!Config::get('system', 'ostatus_disabled')) {
1450                         $accesstoken = Config::get('system', 'instances_social_key');
1451                         if (!empty($accesstoken)) {
1452                                 $api = 'https://instances.social/api/1.0/instances/list?count=0';
1453                                 $header = ['Authorization: Bearer '.$accesstoken];
1454                                 $serverdata = Network::curl($api, false, $redirects, ['headers' => $header]);
1455                                 if ($serverdata['success']) {
1456                                         $servers = json_decode($serverdata['body']);
1457                                         foreach ($servers->instances as $server) {
1458                                                 $url = (is_null($server->https_score) ? 'http' : 'https').'://'.$server->name;
1459                                                 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "server", $url);
1460                                         }
1461                                 }
1462                         }
1463                 }
1464
1465                 // Currently disabled, since the service isn't available anymore.
1466                 // It is not removed since I hope that there will be a successor.
1467                 // Discover GNU Social Servers.
1468                 //if (!Config::get('system','ostatus_disabled')) {
1469                 //      $serverdata = "http://gstools.org/api/get_open_instances/";
1470
1471                 //      $result = Network::curl($serverdata);
1472                 //      if ($result["success"]) {
1473                 //              $servers = json_decode($result["body"]);
1474
1475                 //              foreach($servers->data as $server)
1476                 //                      self::checkServer($server->instance_address);
1477                 //      }
1478                 //}
1479
1480                 Config::set('poco', 'last_federation_discovery', time());
1481         }
1482
1483         public static function discoverSingleServer($id)
1484         {
1485                 $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `id` = %d", intval($id));
1486                 if (!DBM::is_result($r)) {
1487                         return false;
1488                 }
1489
1490                 $server = $r[0];
1491
1492                 // Discover new servers out there (Works from Friendica version 3.5.2)
1493                 self::fetchServerlist($server["poco"]);
1494
1495                 // Fetch all users from the other server
1496                 $url = $server["poco"]."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1497
1498                 logger("Fetch all users from the server ".$server["url"], LOGGER_DEBUG);
1499
1500                 $retdata = Network::curl($url);
1501                 if ($retdata["success"]) {
1502                         $data = json_decode($retdata["body"]);
1503
1504                         self::discoverServer($data, 2);
1505
1506                         if (Config::get('system', 'poco_discovery') > 1) {
1507                                 $timeframe = Config::get('system', 'poco_discovery_since');
1508                                 if ($timeframe == 0) {
1509                                         $timeframe = 30;
1510                                 }
1511
1512                                 $updatedSince = date(DateTimeFormat::MYSQL, time() - $timeframe * 86400);
1513
1514                                 // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3)
1515                                 $url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1516
1517                                 $success = false;
1518
1519                                 $retdata = Network::curl($url);
1520                                 if ($retdata["success"]) {
1521                                         logger("Fetch all global contacts from the server ".$server["nurl"], LOGGER_DEBUG);
1522                                         $success = self::discoverServer(json_decode($retdata["body"]));
1523                                 }
1524
1525                                 if (!$success && (Config::get('system', 'poco_discovery') > 2)) {
1526                                         logger("Fetch contacts from users of the server ".$server["nurl"], LOGGER_DEBUG);
1527                                         self::discoverServerUsers($data, $server);
1528                                 }
1529                         }
1530
1531                         $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
1532                         dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
1533
1534                         return true;
1535                 } else {
1536                         // If the server hadn't replied correctly, then force a sanity check
1537                         self::checkServer($server["url"], $server["network"], true);
1538
1539                         // If we couldn't reach the server, we will try it some time later
1540                         $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
1541                         dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
1542
1543                         return false;
1544                 }
1545         }
1546
1547         public static function discover($complete = false)
1548         {
1549                 // Update the server list
1550                 self::discoverFederation();
1551
1552                 $no_of_queries = 5;
1553
1554                 $requery_days = intval(Config::get("system", "poco_requery_days"));
1555
1556                 if ($requery_days == 0) {
1557                         $requery_days = 7;
1558                 }
1559                 $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
1560
1561                 $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));
1562                 if (DBM::is_result($r)) {
1563                         foreach ($r as $server) {
1564                                 if (!self::checkServer($server["url"], $server["network"])) {
1565                                         // The server is not reachable? Okay, then we will try it later
1566                                         $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
1567                                         dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
1568                                         continue;
1569                                 }
1570
1571                                 logger('Update directory from server '.$server['url'].' with ID '.$server['id'], LOGGER_DEBUG);
1572                                 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "update_server_directory", (int)$server['id']);
1573
1574                                 if (!$complete && (--$no_of_queries == 0)) {
1575                                         break;
1576                                 }
1577                         }
1578                 }
1579         }
1580
1581         private static function discoverServerUsers($data, $server)
1582         {
1583                 if (!isset($data->entry)) {
1584                         return;
1585                 }
1586
1587                 foreach ($data->entry as $entry) {
1588                         $username = "";
1589                         if (isset($entry->urls)) {
1590                                 foreach ($entry->urls as $url) {
1591                                         if ($url->type == 'profile') {
1592                                                 $profile_url = $url->value;
1593                                                 $urlparts = parse_url($profile_url);
1594                                                 $username = end(explode("/", $urlparts["path"]));
1595                                         }
1596                                 }
1597                         }
1598                         if ($username != "") {
1599                                 logger("Fetch contacts for the user ".$username." from the server ".$server["nurl"], LOGGER_DEBUG);
1600
1601                                 // Fetch all contacts from a given user from the other server
1602                                 $url = $server["poco"]."/".$username."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
1603
1604                                 $retdata = Network::curl($url);
1605                                 if ($retdata["success"]) {
1606                                         self::discoverServer(json_decode($retdata["body"]), 3);
1607                                 }
1608                         }
1609                 }
1610         }
1611
1612         private static function discoverServer($data, $default_generation = 0)
1613         {
1614                 if (!isset($data->entry) || !count($data->entry)) {
1615                         return false;
1616                 }
1617
1618                 $success = false;
1619
1620                 foreach ($data->entry as $entry) {
1621                         $profile_url = '';
1622                         $profile_photo = '';
1623                         $connect_url = '';
1624                         $name = '';
1625                         $network = '';
1626                         $updated = NULL_DATE;
1627                         $location = '';
1628                         $about = '';
1629                         $keywords = '';
1630                         $gender = '';
1631                         $contact_type = -1;
1632                         $generation = $default_generation;
1633
1634                         $name = $entry->displayName;
1635
1636                         if (isset($entry->urls)) {
1637                                 foreach ($entry->urls as $url) {
1638                                         if ($url->type == 'profile') {
1639                                                 $profile_url = $url->value;
1640                                                 continue;
1641                                         }
1642                                         if ($url->type == 'webfinger') {
1643                                                 $connect_url = str_replace('acct:' , '', $url->value);
1644                                                 continue;
1645                                         }
1646                                 }
1647                         }
1648
1649                         if (isset($entry->photos)) {
1650                                 foreach ($entry->photos as $photo) {
1651                                         if ($photo->type == 'profile') {
1652                                                 $profile_photo = $photo->value;
1653                                                 continue;
1654                                         }
1655                                 }
1656                         }
1657
1658                         if (isset($entry->updated)) {
1659                                 $updated = date(DateTimeFormat::MYSQL, strtotime($entry->updated));
1660                         }
1661
1662                         if (isset($entry->network)) {
1663                                 $network = $entry->network;
1664                         }
1665
1666                         if (isset($entry->currentLocation)) {
1667                                 $location = $entry->currentLocation;
1668                         }
1669
1670                         if (isset($entry->aboutMe)) {
1671                                 $about = html2bbcode($entry->aboutMe);
1672                         }
1673
1674                         if (isset($entry->gender)) {
1675                                 $gender = $entry->gender;
1676                         }
1677
1678                         if (isset($entry->generation) && ($entry->generation > 0)) {
1679                                 $generation = ++$entry->generation;
1680                         }
1681
1682                         if (isset($entry->contactType) && ($entry->contactType >= 0)) {
1683                                 $contact_type = $entry->contactType;
1684                         }
1685
1686                         if (isset($entry->tags)) {
1687                                 foreach ($entry->tags as $tag) {
1688                                         $keywords = implode(", ", $tag);
1689                                 }
1690                         }
1691
1692                         if ($generation > 0) {
1693                                 $success = true;
1694
1695                                 logger("Store profile ".$profile_url, LOGGER_DEBUG);
1696
1697                                 $gcontact = ["url" => $profile_url,
1698                                                 "name" => $name,
1699                                                 "network" => $network,
1700                                                 "photo" => $profile_photo,
1701                                                 "about" => $about,
1702                                                 "location" => $location,
1703                                                 "gender" => $gender,
1704                                                 "keywords" => $keywords,
1705                                                 "connect" => $connect_url,
1706                                                 "updated" => $updated,
1707                                                 "contact-type" => $contact_type,
1708                                                 "generation" => $generation];
1709
1710                                 try {
1711                                         $gcontact = GContact::sanitize($gcontact);
1712                                         GContact::update($gcontact);
1713                                 } catch (Exception $e) {
1714                                         logger($e->getMessage(), LOGGER_DEBUG);
1715                                 }
1716
1717                                 logger("Done for profile ".$profile_url, LOGGER_DEBUG);
1718                         }
1719                 }
1720                 return $success;
1721         }
1722
1723 }