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