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