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