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