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