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