]> git.mxchange.org Git - friendica.git/blob - src/Model/GServer.php
Merge remote-tracking branch 'upstream/develop' into worker-command
[friendica.git] / src / Model / GServer.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use DOMDocument;
25 use DOMXPath;
26 use Friendica\Core\Logger;
27 use Friendica\Core\Protocol;
28 use Friendica\Core\System;
29 use Friendica\Core\Worker;
30 use Friendica\Database\Database;
31 use Friendica\Database\DBA;
32 use Friendica\DI;
33 use Friendica\Module\Register;
34 use Friendica\Network\CurlResult;
35 use Friendica\Protocol\Relay;
36 use Friendica\Util\DateTimeFormat;
37 use Friendica\Util\Network;
38 use Friendica\Util\Strings;
39 use Friendica\Util\XML;
40
41 /**
42  * This class handles GServer related functions
43  */
44 class GServer
45 {
46         // Directory types
47         const DT_NONE = 0;
48         const DT_POCO = 1;
49         const DT_MASTODON = 2;
50
51         // Methods to detect server types
52
53         // Non endpoint specific methods
54         const DETECT_MANUAL = 0;
55         const DETECT_HEADER = 1;
56         const DETECT_BODY = 2;
57
58         // Implementation specific endpoints
59         const DETECT_FRIENDIKA = 10;
60         const DETECT_FRIENDICA = 11;
61         const DETECT_STATUSNET = 12;
62         const DETECT_GNUSOCIAL = 13;
63         const DETECT_CONFIG_JSON = 14; // Statusnet, GNU Social, Older Hubzilla/Redmatrix
64         const DETECT_SITEINFO_JSON = 15; // Newer Hubzilla
65         const DETECT_MASTODON_API = 16;
66         const DETECT_STATUS_PHP = 17; // Nextcloud
67
68         // Standardized endpoints
69         const DETECT_STATISTICS_JSON = 100;
70         const DETECT_NODEINFO_1 = 101;
71         const DETECT_NODEINFO_2 = 102;
72
73         /**
74          * Get the ID for the given server URL
75          *
76          * @param string $url
77          * @param boolean $no_check Don't check if the server hadn't been found
78          * @return int gserver id
79          */
80         public static function getID(string $url, bool $no_check = false)
81         {
82                 if (empty($url)) {
83                         return null;
84                 }
85
86                 $url = self::cleanURL($url);
87
88                 $gserver = DBA::selectFirst('gserver', ['id'], ['nurl' => Strings::normaliseLink($url)]);
89                 if (DBA::isResult($gserver)) {
90                         Logger::info('Got ID for URL', ['id' => $gserver['id'], 'url' => $url, 'callstack' => System::callstack(20)]);
91                         return $gserver['id'];
92                 }
93
94                 if ($no_check || !self::check($url)) {
95                         return null;
96                 }
97         
98                 return self::getID($url, true);
99         }
100
101         /**
102          * Checks if the given server is reachable
103          *
104          * @param string  $profile URL of the given profile
105          * @param string  $server  URL of the given server (If empty, taken from profile)
106          * @param string  $network Network value that is used, when detection failed
107          * @param boolean $force   Force an update.
108          *
109          * @return boolean 'true' if server seems vital
110          */
111         public static function reachable(string $profile, string $server = '', string $network = '', bool $force = false)
112         {
113                 if ($server == '') {
114                         $contact = Contact::getByURL($profile, null, ['baseurl']);
115                         if (!empty($contact['baseurl'])) {
116                                 $server = $contact['baseurl'];
117                         }
118                 }
119
120                 if ($server == '') {
121                         return true;
122                 }
123
124                 return self::check($server, $network, $force);
125         }
126
127         public static function getNextUpdateDate(bool $success, string $created = '', string $last_contact = '')
128         {
129                 // On successful contact process check again next week
130                 if ($success) {
131                         return DateTimeFormat::utc('now +7 day');
132                 }
133
134                 $now = strtotime(DateTimeFormat::utcNow());
135
136                 if ($created > $last_contact) {
137                         $contact_time = strtotime($created);
138                 } else {
139                         $contact_time = strtotime($last_contact);
140                 }
141
142                 // If the last contact was less than 6 hours before then try again in 6 hours
143                 if (($now - $contact_time) < (60 * 60 * 6)) {
144                         return DateTimeFormat::utc('now +6 hour');
145                 }
146
147                 // If the last contact was less than 12 hours before then try again in 12 hours
148                 if (($now - $contact_time) < (60 * 60 * 12)) {
149                         return DateTimeFormat::utc('now +12 hour');
150                 }
151
152                 // If the last contact was less than 24 hours before then try tomorrow again
153                 if (($now - $contact_time) < (60 * 60 * 24)) {
154                         return DateTimeFormat::utc('now +1 day');
155                 }
156                 
157                 // If the last contact was less than a week before then try again in a week
158                 if (($now - $contact_time) < (60 * 60 * 24 * 7)) {
159                         return DateTimeFormat::utc('now +1 week');
160                 }
161
162                 // If the last contact was less than two weeks before then try again in two week
163                 if (($now - $contact_time) < (60 * 60 * 24 * 14)) {
164                         return DateTimeFormat::utc('now +2 week');
165                 }
166
167                 // If the last contact was less than a month before then try again in a month
168                 if (($now - $contact_time) < (60 * 60 * 24 * 30)) {
169                         return DateTimeFormat::utc('now +1 month');
170                 }
171
172                 // The system hadn't been successul contacted for more than a month, so try again in three months
173                 return DateTimeFormat::utc('now +3 month');
174         }
175
176         /**
177          * Decides if a server needs to be updated, based upon several date fields
178          *
179          * @param date $created      Creation date of that server entry
180          * @param date $updated      When had the server entry be updated
181          * @param date $last_failure Last failure when contacting that server
182          * @param date $last_contact Last time the server had been contacted
183          *
184          * @return boolean Does the server record needs an update?
185          */
186         public static function updateNeeded($created, $updated, $last_failure, $last_contact)
187         {
188                 $now = strtotime(DateTimeFormat::utcNow());
189
190                 if ($updated > $last_contact) {
191                         $contact_time = strtotime($updated);
192                 } else {
193                         $contact_time = strtotime($last_contact);
194                 }
195
196                 $failure_time = strtotime($last_failure);
197                 $created_time = strtotime($created);
198
199                 // If there is no "created" time then use the current time
200                 if ($created_time <= 0) {
201                         $created_time = $now;
202                 }
203
204                 // If the last contact was less than 24 hours then don't update
205                 if (($now - $contact_time) < (60 * 60 * 24)) {
206                         return false;
207                 }
208
209                 // If the last failure was less than 24 hours then don't update
210                 if (($now - $failure_time) < (60 * 60 * 24)) {
211                         return false;
212                 }
213
214                 // If the last contact was less than a week ago and the last failure is older than a week then don't update
215                 //if ((($now - $contact_time) < (60 * 60 * 24 * 7)) && ($contact_time > $failure_time))
216                 //      return false;
217
218                 // 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
219                 if ((($now - $contact_time) > (60 * 60 * 24 * 7)) && (($now - $created_time) > (60 * 60 * 24 * 7)) && (($now - $failure_time) < (60 * 60 * 24 * 7))) {
220                         return false;
221                 }
222
223                 // 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
224                 if ((($now - $contact_time) > (60 * 60 * 24 * 30)) && (($now - $created_time) > (60 * 60 * 24 * 30)) && (($now - $failure_time) < (60 * 60 * 24 * 30))) {
225                         return false;
226                 }
227
228                 return true;
229         }
230
231         /**
232          * Checks the state of the given server.
233          *
234          * @param string  $server_url    URL of the given server
235          * @param string  $network       Network value that is used, when detection failed
236          * @param boolean $force         Force an update.
237          * @param boolean $only_nodeinfo Only use nodeinfo for server detection
238          *
239          * @return boolean 'true' if server seems vital
240          */
241         public static function check(string $server_url, string $network = '', bool $force = false, bool $only_nodeinfo = false)
242         {
243                 $server_url = self::cleanURL($server_url);
244
245                 if ($server_url == '') {
246                         return false;
247                 }
248
249                 $gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($server_url)]);
250                 if (DBA::isResult($gserver)) {
251                         if ($gserver['created'] <= DBA::NULL_DATETIME) {
252                                 $fields = ['created' => DateTimeFormat::utcNow()];
253                                 $condition = ['nurl' => Strings::normaliseLink($server_url)];
254                                 DBA::update('gserver', $fields, $condition);
255                         }
256
257                         $last_contact = $gserver['last_contact'];
258                         $last_failure = $gserver['last_failure'];
259
260                         // See discussion under https://forum.friendi.ca/display/0b6b25a8135aabc37a5a0f5684081633
261                         // It can happen that a zero date is in the database, but storing it again is forbidden.
262                         if ($last_contact < DBA::NULL_DATETIME) {
263                                 $last_contact = DBA::NULL_DATETIME;
264                         }
265
266                         if ($last_failure < DBA::NULL_DATETIME) {
267                                 $last_failure = DBA::NULL_DATETIME;
268                         }
269
270                         if (!$force && !self::updateNeeded($gserver['created'], '', $last_failure, $last_contact)) {
271                                 Logger::info('No update needed', ['server' => $server_url]);
272                                 return ($last_contact >= $last_failure);
273                         }
274                         Logger::info('Server is outdated. Start discovery.', ['Server' => $server_url, 'Force' => $force, 'Created' => $gserver['created'], 'Failure' => $last_failure, 'Contact' => $last_contact]);
275                 } else {
276                         Logger::info('Server is unknown. Start discovery.', ['Server' => $server_url]);
277                 }
278
279                 return self::detect($server_url, $network, $only_nodeinfo);
280         }
281
282         /**
283          * Set failed server status
284          *
285          * @param string $url
286          */
287         public static function setFailure(string $url)
288         {
289                 $gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($url)]);
290                 if (DBA::isResult($gserver)) {
291                         $next_update = self::getNextUpdateDate(false, $gserver['created'], $gserver['last_contact']);
292                         DBA::update('gserver', ['failed' => true, 'last_failure' => DateTimeFormat::utcNow(),
293                         'next_contact' => $next_update, 'detection-method' => null],
294                         ['nurl' => Strings::normaliseLink($url)]);
295                         Logger::info('Set failed status for existing server', ['url' => $url]);
296                         return;
297                 }
298                 DBA::insert('gserver', ['url' => $url, 'nurl' => Strings::normaliseLink($url),
299                         'network' => Protocol::PHANTOM, 'created' => DateTimeFormat::utcNow(),
300                         'failed' => true, 'last_failure' => DateTimeFormat::utcNow()]);
301                 Logger::info('Set failed status for new server', ['url' => $url]);
302         }
303
304         /**
305          * Remove unwanted content from the given URL
306          *
307          * @param string $url
308          * @return string cleaned URL
309          */
310         public static function cleanURL(string $url)
311         {
312                 $url = trim($url, '/');
313                 $url = str_replace('/index.php', '', $url);
314
315                 $urlparts = parse_url($url);
316                 unset($urlparts['user']);
317                 unset($urlparts['pass']);
318                 unset($urlparts['query']);
319                 unset($urlparts['fragment']);
320                 return Network::unparseURL($urlparts);
321         }
322
323         /**
324          * Return the base URL
325          *
326          * @param string $url
327          * @return string base URL
328          */
329         private static function getBaseURL(string $url)
330         {
331                 $urlparts = parse_url(self::cleanURL($url));
332                 unset($urlparts['path']);
333                 return Network::unparseURL($urlparts);
334         }
335
336         /**
337          * Detect server data (type, protocol, version number, ...)
338          * The detected data is then updated or inserted in the gserver table.
339          *
340          * @param string  $url           URL of the given server
341          * @param string  $network       Network value that is used, when detection failed
342          * @param boolean $only_nodeinfo Only use nodeinfo for server detection
343          *
344          * @return boolean 'true' if server could be detected
345          */
346         public static function detect(string $url, string $network = '', bool $only_nodeinfo = false)
347         {
348                 Logger::info('Detect server type', ['server' => $url]);
349                 $serverdata = ['detection-method' => self::DETECT_MANUAL];
350
351                 $original_url = $url;
352
353                 // Remove URL content that is not supposed to exist for a server url
354                 $url = self::cleanURL($url);
355
356                 // Get base URL
357                 $baseurl = self::getBaseURL($url);
358
359                 // If the URL missmatches, then we mark the old entry as failure
360                 if ($url != $original_url) {
361                         /// @todo What to do with "next_contact" here?
362                         DBA::update('gserver', ['failed' => true, 'last_failure' => DateTimeFormat::utcNow()],
363                                 ['nurl' => Strings::normaliseLink($original_url)]);
364                 }
365
366                 // When a nodeinfo is present, we don't need to dig further
367                 $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
368                 $curlResult = DI::httpRequest()->get($url . '/.well-known/nodeinfo', ['timeout' => $xrd_timeout]);
369                 if ($curlResult->isTimeout()) {
370                         self::setFailure($url);
371                         return false;
372                 }
373
374                 $nodeinfo = self::fetchNodeinfo($url, $curlResult);
375                 if ($only_nodeinfo && empty($nodeinfo)) {
376                         Logger::info('Invalid nodeinfo in nodeinfo-mode, server is marked as failure', ['url' => $url]);
377                         self::setFailure($url);
378                         return false;
379                 }
380
381                 // When nodeinfo isn't present, we use the older 'statistics.json' endpoint
382                 if (empty($nodeinfo)) {
383                         $nodeinfo = self::fetchStatistics($url);
384                 }
385
386                 // If that didn't work out well, we use some protocol specific endpoints
387                 // For Friendica and Zot based networks we have to dive deeper to reveal more details
388                 if (empty($nodeinfo['network']) || in_array($nodeinfo['network'], [Protocol::DFRN, Protocol::ZOT])) {
389                         if (!empty($nodeinfo['detection-method'])) {
390                                 $serverdata['detection-method'] = $nodeinfo['detection-method'];
391                         }
392
393                         // Fetch the landing page, possibly it reveals some data
394                         if (empty($nodeinfo['network'])) {
395                                 if ($baseurl == $url) {
396                                         $basedata = $serverdata;
397                                 } else {
398                                         $basedata = ['detection-method' => self::DETECT_MANUAL];
399                                 }
400
401                                 $curlResult = DI::httpRequest()->get($baseurl, ['timeout' => $xrd_timeout]);
402                                 if ($curlResult->isSuccess()) {
403                                         $basedata = self::analyseRootHeader($curlResult, $basedata);
404                                         $basedata = self::analyseRootBody($curlResult, $basedata, $baseurl);
405                                 }
406
407                                 if (!$curlResult->isSuccess() || empty($curlResult->getBody()) || self::invalidBody($curlResult->getBody())) {
408                                         self::setFailure($url);
409                                         return false;
410                                 }
411
412                                 if ($baseurl == $url) {
413                                         $serverdata = $basedata;
414                                 } else {
415                                         // When the base path doesn't seem to contain a social network we try the complete path.
416                                         // Most detectable system have to be installed in the root directory.
417                                         // We checked the base to avoid false positives.
418                                         $curlResult = DI::httpRequest()->get($url, ['timeout' => $xrd_timeout]);
419                                         if ($curlResult->isSuccess()) {
420                                                 $urldata = self::analyseRootHeader($curlResult, $serverdata);
421                                                 $urldata = self::analyseRootBody($curlResult, $urldata, $url);
422
423                                                 $comparebase = $basedata;
424                                                 unset($comparebase['info']);
425                                                 unset($comparebase['site_name']);
426                                                 $compareurl = $urldata;
427                                                 unset($compareurl['info']);
428                                                 unset($compareurl['site_name']);
429
430                                                 // We assume that no one will install the identical system in the root and a subfolder
431                                                 if (!empty(array_diff($comparebase, $compareurl))) {
432                                                         $serverdata = $urldata;
433                                                 }
434                                         }
435                                 }
436                         }
437
438                         if (empty($serverdata['network']) || ($serverdata['network'] == Protocol::ACTIVITYPUB)) {
439                                 $serverdata = self::detectMastodonAlikes($url, $serverdata);
440                         }
441
442                         // All following checks are done for systems that always have got a "host-meta" endpoint.
443                         // With this check we don't have to waste time and ressources for dead systems.
444                         // Also this hopefully prevents us from receiving abuse messages.
445                         if (empty($serverdata['network']) && !self::validHostMeta($url)) {
446                                 self::setFailure($url);
447                                 return false;
448                         }
449
450                         if (empty($serverdata['network']) || in_array($serverdata['network'], [Protocol::DFRN, Protocol::ACTIVITYPUB])) {
451                                 $serverdata = self::detectFriendica($url, $serverdata);
452                         }
453
454                         // the 'siteinfo.json' is some specific endpoint of Hubzilla and Red
455                         if (empty($serverdata['network']) || ($serverdata['network'] == Protocol::ZOT)) {
456                                 $serverdata = self::fetchSiteinfo($url, $serverdata);
457                         }
458
459                         // The 'siteinfo.json' doesn't seem to be present on older Hubzilla installations
460                         if (empty($serverdata['network'])) {
461                                 $serverdata = self::detectHubzilla($url, $serverdata);
462                         }
463
464                         if (empty($serverdata['network'])) {
465                                 $serverdata = self::detectNextcloud($url, $serverdata);
466                         }
467
468                         if (empty($serverdata['network'])) {
469                                 $serverdata = self::detectGNUSocial($url, $serverdata);
470                         }
471
472                         $serverdata = array_merge($nodeinfo, $serverdata);
473                 } else {
474                         $serverdata = $nodeinfo;
475                 }
476
477                 // Detect the directory type
478                 $serverdata['directory-type'] = self::DT_NONE;
479                 $serverdata = self::checkPoCo($url, $serverdata);
480                 $serverdata = self::checkMastodonDirectory($url, $serverdata);
481
482                 // We can't detect the network type. Possibly it is some system that we don't know yet
483                 if (empty($serverdata['network'])) {
484                         $serverdata['network'] = Protocol::PHANTOM;
485                 }
486
487                 // When we hadn't been able to detect the network type, we use the hint from the parameter
488                 if (($serverdata['network'] == Protocol::PHANTOM) && !empty($network)) {
489                         $serverdata['network'] = $network;
490                 }
491
492                 $serverdata['url'] = $url;
493                 $serverdata['nurl'] = Strings::normaliseLink($url);
494
495                 // We take the highest number that we do find
496                 $registeredUsers = $serverdata['registered-users'] ?? 0;
497
498                 // On an active server there has to be at least a single user
499                 if (($serverdata['network'] != Protocol::PHANTOM) && ($registeredUsers == 0)) {
500                         $registeredUsers = 1;
501                 }
502
503                 if ($serverdata['network'] == Protocol::PHANTOM) {
504                         $serverdata['registered-users'] = $registeredUsers;
505                         $serverdata = self::detectNetworkViaContacts($url, $serverdata);
506                 }
507
508                 $serverdata['next_contact'] = self::getNextUpdateDate(true);
509
510                 $serverdata['last_contact'] = DateTimeFormat::utcNow();
511                 $serverdata['failed'] = false;
512
513                 $gserver = DBA::selectFirst('gserver', ['network'], ['nurl' => Strings::normaliseLink($url)]);
514                 if (!DBA::isResult($gserver)) {
515                         $serverdata['created'] = DateTimeFormat::utcNow();
516                         $ret = DBA::insert('gserver', $serverdata);
517                         $id = DBA::lastInsertId();
518                 } else {
519                         // Don't override the network with 'unknown' when there had been a valid entry before
520                         if (($serverdata['network'] == Protocol::PHANTOM) && !empty($gserver['network'])) {
521                                 unset($serverdata['network']);
522                         }
523
524                         $ret = DBA::update('gserver', $serverdata, ['nurl' => $serverdata['nurl']]);
525                         $gserver = DBA::selectFirst('gserver', ['id'], ['nurl' => $serverdata['nurl']]);
526                         if (DBA::isResult($gserver)) {
527                                 $id = $gserver['id'];
528                         }
529                 }
530
531                 if (!empty($serverdata['network']) && !empty($id) && ($serverdata['network'] != Protocol::PHANTOM)) {
532                         $apcontacts = DBA::count('apcontact', ['gsid' => $id]);
533                         $contacts = DBA::count('contact', ['uid' => 0, 'gsid' => $id]);
534                         $max_users = max($apcontacts, $contacts, $registeredUsers);
535                         if ($max_users > $registeredUsers) {
536                                 Logger::info('Update registered users', ['id' => $id, 'url' => $serverdata['nurl'], 'registered-users' => $max_users]);
537                                 DBA::update('gserver', ['registered-users' => $max_users], ['id' => $id]);
538                         }
539                 }
540
541                 if (!empty($serverdata['network']) && in_array($serverdata['network'], [Protocol::DFRN, Protocol::DIASPORA])) {
542                         self::discoverRelay($url);
543                 }
544
545                 return $ret;
546         }
547
548         /**
549          * Fetch relay data from a given server url
550          *
551          * @param string $server_url address of the server
552          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
553          */
554         private static function discoverRelay(string $server_url)
555         {
556                 Logger::info('Discover relay data', ['server' => $server_url]);
557
558                 $curlResult = DI::httpRequest()->get($server_url . '/.well-known/x-social-relay');
559                 if (!$curlResult->isSuccess()) {
560                         return;
561                 }
562
563                 $data = json_decode($curlResult->getBody(), true);
564                 if (!is_array($data)) {
565                         return;
566                 }
567
568                 // Sanitize incoming data, see https://github.com/friendica/friendica/issues/8565
569                 $data['subscribe'] = (bool)$data['subscribe'] ?? false;
570
571                 if (!$data['subscribe'] || empty($data['scope']) || !in_array(strtolower($data['scope']), ['all', 'tags'])) {
572                         $data['scope'] = '';
573                         $data['subscribe'] = false;
574                         $data['tags'] = [];
575                 }
576
577                 $gserver = DBA::selectFirst('gserver', ['id', 'url', 'network', 'relay-subscribe', 'relay-scope'], ['nurl' => Strings::normaliseLink($server_url)]);
578                 if (!DBA::isResult($gserver)) {
579                         return;
580                 }
581
582                 if (($gserver['relay-subscribe'] != $data['subscribe']) || ($gserver['relay-scope'] != $data['scope'])) {
583                         $fields = ['relay-subscribe' => $data['subscribe'], 'relay-scope' => $data['scope']];
584                         DBA::update('gserver', $fields, ['id' => $gserver['id']]);
585                 }
586
587                 DBA::delete('gserver-tag', ['gserver-id' => $gserver['id']]);
588
589                 if ($data['scope'] == 'tags') {
590                         // Avoid duplicates
591                         $tags = [];
592                         foreach ($data['tags'] as $tag) {
593                                 $tag = mb_strtolower($tag);
594                                 if (strlen($tag) < 100) {
595                                         $tags[$tag] = $tag;
596                                 }
597                         }
598
599                         foreach ($tags as $tag) {
600                                 DBA::insert('gserver-tag', ['gserver-id' => $gserver['id'], 'tag' => $tag], Database::INSERT_IGNORE);
601                         }
602                 }
603
604                 // Create or update the relay contact
605                 $fields = [];
606                 if (isset($data['protocols'])) {
607                         if (isset($data['protocols']['diaspora'])) {
608                                 $fields['network'] = Protocol::DIASPORA;
609
610                                 if (isset($data['protocols']['diaspora']['receive'])) {
611                                         $fields['batch'] = $data['protocols']['diaspora']['receive'];
612                                 } elseif (is_string($data['protocols']['diaspora'])) {
613                                         $fields['batch'] = $data['protocols']['diaspora'];
614                                 }
615                         }
616
617                         if (isset($data['protocols']['dfrn'])) {
618                                 $fields['network'] = Protocol::DFRN;
619
620                                 if (isset($data['protocols']['dfrn']['receive'])) {
621                                         $fields['batch'] = $data['protocols']['dfrn']['receive'];
622                                 } elseif (is_string($data['protocols']['dfrn'])) {
623                                         $fields['batch'] = $data['protocols']['dfrn'];
624                                 }
625                         }
626
627                         if (isset($data['protocols']['activitypub'])) {
628                                 $fields['network'] = Protocol::ACTIVITYPUB;
629
630                                 if (!empty($data['protocols']['activitypub']['actor'])) {
631                                         $fields['url'] = $data['protocols']['activitypub']['actor'];
632                                 }
633                                 if (!empty($data['protocols']['activitypub']['receive'])) {
634                                         $fields['batch'] = $data['protocols']['activitypub']['receive'];
635                                 }
636                         }
637                 }
638
639                 Logger::info('Discovery ended', ['server' => $server_url, 'data' => $fields]);
640
641                 Relay::updateContact($gserver, $fields);
642         }
643
644         /**
645          * Fetch server data from '/statistics.json' on the given server
646          *
647          * @param string $url URL of the given server
648          *
649          * @return array server data
650          */
651         private static function fetchStatistics(string $url)
652         {
653                 $curlResult = DI::httpRequest()->get($url . '/statistics.json');
654                 if (!$curlResult->isSuccess()) {
655                         return [];
656                 }
657
658                 $data = json_decode($curlResult->getBody(), true);
659                 if (empty($data)) {
660                         return [];
661                 }
662
663                 $serverdata = ['detection-method' => self::DETECT_STATISTICS_JSON];
664
665                 if (!empty($data['version'])) {
666                         $serverdata['version'] = $data['version'];
667                         // Version numbers on statistics.json are presented with additional info, e.g.:
668                         // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
669                         $serverdata['version'] = preg_replace('=(.+)-(.{4,})=ism', '$1', $serverdata['version']);
670                 }
671
672                 if (!empty($data['name'])) {
673                         $serverdata['site_name'] = $data['name'];
674                 }
675
676                 if (!empty($data['network'])) {
677                         $serverdata['platform'] = strtolower($data['network']);
678
679                         if ($serverdata['platform'] == 'diaspora') {
680                                 $serverdata['network'] = Protocol::DIASPORA;
681                         } elseif ($serverdata['platform'] == 'friendica') {
682                                 $serverdata['network'] = Protocol::DFRN;
683                         } elseif ($serverdata['platform'] == 'hubzilla') {
684                                 $serverdata['network'] = Protocol::ZOT;
685                         } elseif ($serverdata['platform'] == 'redmatrix') {
686                                 $serverdata['network'] = Protocol::ZOT;
687                         }
688                 }
689
690
691                 if (!empty($data['registrations_open'])) {
692                         $serverdata['register_policy'] = Register::OPEN;
693                 } else {
694                         $serverdata['register_policy'] = Register::CLOSED;
695                 }
696
697                 return $serverdata;
698         }
699
700         /**
701          * Detect server type by using the nodeinfo data
702          *
703          * @param string     $url        address of the server
704          * @param CurlResult $curlResult
705          * @return array Server data
706          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
707          */
708         private static function fetchNodeinfo(string $url, CurlResult $curlResult)
709         {
710                 if (!$curlResult->isSuccess()) {
711                         return [];
712                 }
713
714                 $nodeinfo = json_decode($curlResult->getBody(), true);
715
716                 if (!is_array($nodeinfo) || empty($nodeinfo['links'])) {
717                         return [];
718                 }
719
720                 $nodeinfo1_url = '';
721                 $nodeinfo2_url = '';
722
723                 foreach ($nodeinfo['links'] as $link) {
724                         if (!is_array($link) || empty($link['rel']) || empty($link['href'])) {
725                                 Logger::info('Invalid nodeinfo format', ['url' => $url]);
726                                 continue;
727                         }
728                         if ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/1.0') {
729                                 $nodeinfo1_url = $link['href'];
730                         } elseif ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/2.0') {
731                                 $nodeinfo2_url = $link['href'];
732                         }
733                 }
734
735                 if ($nodeinfo1_url . $nodeinfo2_url == '') {
736                         return [];
737                 }
738
739                 $server = [];
740
741                 // When the nodeinfo url isn't on the same host, then there is obviously something wrong
742                 if (!empty($nodeinfo2_url) && (parse_url($url, PHP_URL_HOST) == parse_url($nodeinfo2_url, PHP_URL_HOST))) {
743                         $server = self::parseNodeinfo2($nodeinfo2_url);
744                 }
745
746                 // When the nodeinfo url isn't on the same host, then there is obviously something wrong
747                 if (empty($server) && !empty($nodeinfo1_url) && (parse_url($url, PHP_URL_HOST) == parse_url($nodeinfo1_url, PHP_URL_HOST))) {
748                         $server = self::parseNodeinfo1($nodeinfo1_url);
749                 }
750
751                 return $server;
752         }
753
754         /**
755          * Parses Nodeinfo 1
756          *
757          * @param string $nodeinfo_url address of the nodeinfo path
758          * @return array Server data
759          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
760          */
761         private static function parseNodeinfo1(string $nodeinfo_url)
762         {
763                 $curlResult = DI::httpRequest()->get($nodeinfo_url);
764
765                 if (!$curlResult->isSuccess()) {
766                         return [];
767                 }
768
769                 $nodeinfo = json_decode($curlResult->getBody(), true);
770
771                 if (!is_array($nodeinfo)) {
772                         return [];
773                 }
774
775                 $server = ['detection-method' => self::DETECT_NODEINFO_1,
776                         'register_policy' => Register::CLOSED];
777
778                 if (!empty($nodeinfo['openRegistrations'])) {
779                         $server['register_policy'] = Register::OPEN;
780                 }
781
782                 if (is_array($nodeinfo['software'])) {
783                         if (!empty($nodeinfo['software']['name'])) {
784                                 $server['platform'] = strtolower($nodeinfo['software']['name']);
785                         }
786
787                         if (!empty($nodeinfo['software']['version'])) {
788                                 $server['version'] = $nodeinfo['software']['version'];
789                                 // Version numbers on Nodeinfo are presented with additional info, e.g.:
790                                 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
791                                 $server['version'] = preg_replace('=(.+)-(.{4,})=ism', '$1', $server['version']);
792                         }
793                 }
794
795                 if (!empty($nodeinfo['metadata']['nodeName'])) {
796                         $server['site_name'] = $nodeinfo['metadata']['nodeName'];
797                 }
798
799                 if (!empty($nodeinfo['usage']['users']['total'])) {
800                         $server['registered-users'] = $nodeinfo['usage']['users']['total'];
801                 }
802
803                 if (!empty($nodeinfo['protocols']['inbound']) && is_array($nodeinfo['protocols']['inbound'])) {
804                         $protocols = [];
805                         foreach ($nodeinfo['protocols']['inbound'] as $protocol) {
806                                 $protocols[$protocol] = true;
807                         }
808
809                         if (!empty($protocols['friendica'])) {
810                                 $server['network'] = Protocol::DFRN;
811                         } elseif (!empty($protocols['activitypub'])) {
812                                 $server['network'] = Protocol::ACTIVITYPUB;
813                         } elseif (!empty($protocols['diaspora'])) {
814                                 $server['network'] = Protocol::DIASPORA;
815                         } elseif (!empty($protocols['ostatus'])) {
816                                 $server['network'] = Protocol::OSTATUS;
817                         } elseif (!empty($protocols['gnusocial'])) {
818                                 $server['network'] = Protocol::OSTATUS;
819                         } elseif (!empty($protocols['zot'])) {
820                                 $server['network'] = Protocol::ZOT;
821                         }
822                 }
823
824                 if (empty($server)) {
825                         return [];
826                 }
827
828                 return $server;
829         }
830
831         /**
832          * Parses Nodeinfo 2
833          *
834          * @param string $nodeinfo_url address of the nodeinfo path
835          * @return array Server data
836          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
837          */
838         private static function parseNodeinfo2(string $nodeinfo_url)
839         {
840                 $curlResult = DI::httpRequest()->get($nodeinfo_url);
841                 if (!$curlResult->isSuccess()) {
842                         return [];
843                 }
844
845                 $nodeinfo = json_decode($curlResult->getBody(), true);
846
847                 if (!is_array($nodeinfo)) {
848                         return [];
849                 }
850
851                 $server = ['detection-method' => self::DETECT_NODEINFO_2,
852                         'register_policy' => Register::CLOSED];
853
854                 if (!empty($nodeinfo['openRegistrations'])) {
855                         $server['register_policy'] = Register::OPEN;
856                 }
857
858                 if (is_array($nodeinfo['software'])) {
859                         if (!empty($nodeinfo['software']['name'])) {
860                                 $server['platform'] = strtolower($nodeinfo['software']['name']);
861                         }
862
863                         if (!empty($nodeinfo['software']['version'])) {
864                                 $server['version'] = $nodeinfo['software']['version'];
865                                 // Version numbers on Nodeinfo are presented with additional info, e.g.:
866                                 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
867                                 $server['version'] = preg_replace('=(.+)-(.{4,})=ism', '$1', $server['version']);
868                         }
869                 }
870
871                 if (!empty($nodeinfo['metadata']['nodeName'])) {
872                         $server['site_name'] = $nodeinfo['metadata']['nodeName'];
873                 }
874
875                 if (!empty($nodeinfo['usage']['users']['total'])) {
876                         $server['registered-users'] = $nodeinfo['usage']['users']['total'];
877                 }
878
879                 if (!empty($nodeinfo['protocols'])) {
880                         $protocols = [];
881                         foreach ($nodeinfo['protocols'] as $protocol) {
882                                 $protocols[$protocol] = true;
883                         }
884
885                         if (!empty($protocols['dfrn'])) {
886                                 $server['network'] = Protocol::DFRN;
887                         } elseif (!empty($protocols['activitypub'])) {
888                                 $server['network'] = Protocol::ACTIVITYPUB;
889                         } elseif (!empty($protocols['diaspora'])) {
890                                 $server['network'] = Protocol::DIASPORA;
891                         } elseif (!empty($protocols['ostatus'])) {
892                                 $server['network'] = Protocol::OSTATUS;
893                         } elseif (!empty($protocols['gnusocial'])) {
894                                 $server['network'] = Protocol::OSTATUS;
895                         } elseif (!empty($protocols['zot'])) {
896                                 $server['network'] = Protocol::ZOT;
897                         }
898                 }
899
900                 if (empty($server)) {
901                         return [];
902                 }
903
904                 return $server;
905         }
906
907         /**
908          * Fetch server information from a 'siteinfo.json' file on the given server
909          *
910          * @param string $url        URL of the given server
911          * @param array  $serverdata array with server data
912          *
913          * @return array server data
914          */
915         private static function fetchSiteinfo(string $url, array $serverdata)
916         {
917                 $curlResult = DI::httpRequest()->get($url . '/siteinfo.json');
918                 if (!$curlResult->isSuccess()) {
919                         return $serverdata;
920                 }
921
922                 $data = json_decode($curlResult->getBody(), true);
923                 if (empty($data)) {
924                         return $serverdata;
925                 }
926
927                 if (in_array($serverdata['detection-method'], [self::DETECT_HEADER, self::DETECT_BODY, self::DETECT_MANUAL])) {
928                         $serverdata['detection-method'] = self::DETECT_SITEINFO_JSON;
929                 }
930
931                 if (!empty($data['url'])) {
932                         $serverdata['platform'] = strtolower($data['platform']);
933                         $serverdata['version'] = $data['version'];
934                 }
935
936                 if (!empty($data['plugins'])) {
937                         if (in_array('pubcrawl', $data['plugins'])) {
938                                 $serverdata['network'] = Protocol::ACTIVITYPUB;
939                         } elseif (in_array('diaspora', $data['plugins'])) {
940                                 $serverdata['network'] = Protocol::DIASPORA;
941                         } elseif (in_array('gnusoc', $data['plugins'])) {
942                                 $serverdata['network'] = Protocol::OSTATUS;
943                         } else {
944                                 $serverdata['network'] = Protocol::ZOT;
945                         }
946                 }
947
948                 if (!empty($data['site_name'])) {
949                         $serverdata['site_name'] = $data['site_name'];
950                 }
951
952                 if (!empty($data['channels_total'])) {
953                         $serverdata['registered-users'] = $data['channels_total'];
954                 }
955
956                 if (!empty($data['register_policy'])) {
957                         switch ($data['register_policy']) {
958                                 case 'REGISTER_OPEN':
959                                         $serverdata['register_policy'] = Register::OPEN;
960                                         break;
961
962                                 case 'REGISTER_APPROVE':
963                                         $serverdata['register_policy'] = Register::APPROVE;
964                                         break;
965
966                                 case 'REGISTER_CLOSED':
967                                 default:
968                                         $serverdata['register_policy'] = Register::CLOSED;
969                                         break;
970                         }
971                 }
972
973                 return $serverdata;
974         }
975
976         /**
977          * Checks if the server contains a valid host meta file
978          *
979          * @param string $url URL of the given server
980          *
981          * @return boolean 'true' if the server seems to be vital
982          */
983         private static function validHostMeta(string $url)
984         {
985                 $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
986                 $curlResult = DI::httpRequest()->get($url . '/.well-known/host-meta', ['timeout' => $xrd_timeout]);
987                 if (!$curlResult->isSuccess()) {
988                         return false;
989                 }
990
991                 $xrd = XML::parseString($curlResult->getBody());
992                 if (!is_object($xrd)) {
993                         return false;
994                 }
995
996                 $elements = XML::elementToArray($xrd);
997                 if (empty($elements) || empty($elements['xrd']) || empty($elements['xrd']['link'])) {
998                         return false;
999                 }
1000
1001                 $valid = false;
1002                 foreach ($elements['xrd']['link'] as $link) {
1003                         // When there is more than a single "link" element, the array looks slightly different
1004                         if (!empty($link['@attributes'])) {
1005                                 $link = $link['@attributes'];
1006                         }
1007
1008                         if (empty($link['rel']) || empty($link['template'])) {
1009                                 continue;
1010                         }
1011
1012                         if ($link['rel'] == 'lrdd') {
1013                                 // When the webfinger host is the same like the system host, it should be ok.
1014                                 $valid = (parse_url($url, PHP_URL_HOST) == parse_url($link['template'], PHP_URL_HOST));
1015                         }
1016                 }
1017
1018                 return $valid;
1019         }
1020
1021         /**
1022          * Detect the network of the given server via their known contacts
1023          *
1024          * @param string $url        URL of the given server
1025          * @param array  $serverdata array with server data
1026          *
1027          * @return array server data
1028          */
1029         private static function detectNetworkViaContacts(string $url, array $serverdata)
1030         {
1031                 $contacts = [];
1032
1033                 $apcontacts = DBA::select('apcontact', ['url'], ['baseurl' => [$url, $serverdata['nurl']]]);
1034                 while ($apcontact = DBA::fetch($apcontacts)) {
1035                         $contacts[Strings::normaliseLink($apcontact['url'])] = $apcontact['url'];
1036                 }
1037                 DBA::close($apcontacts);
1038
1039                 $pcontacts = DBA::select('contact', ['url', 'nurl'], ['uid' => 0, 'baseurl' => [$url, $serverdata['nurl']]]);
1040                 while ($pcontact = DBA::fetch($pcontacts)) {
1041                         $contacts[$pcontact['nurl']] = $pcontact['url'];
1042                 }
1043                 DBA::close($pcontacts);
1044
1045                 if (empty($contacts)) {
1046                         return $serverdata;
1047                 }
1048
1049                 foreach ($contacts as $contact) {
1050                         $probed = Contact::getByURL($contact);
1051                         if (!empty($probed) && in_array($probed['network'], Protocol::FEDERATED)) {
1052                                 $serverdata['network'] = $probed['network'];
1053                                 break;
1054                         }
1055                 }
1056
1057                 $serverdata['registered-users'] = max($serverdata['registered-users'], count($contacts));
1058
1059                 return $serverdata;
1060         }
1061
1062         /**
1063          * Checks if the given server does have a '/poco' endpoint.
1064          * This is used for the 'PortableContact' functionality,
1065          * which is used by both Friendica and Hubzilla.
1066          *
1067          * @param string $url        URL of the given server
1068          * @param array  $serverdata array with server data
1069          *
1070          * @return array server data
1071          */
1072         private static function checkPoCo(string $url, array $serverdata)
1073         {
1074                 $serverdata['poco'] = '';
1075
1076                 $curlResult = DI::httpRequest()->get($url . '/poco');
1077                 if (!$curlResult->isSuccess()) {
1078                         return $serverdata;
1079                 }
1080
1081                 $data = json_decode($curlResult->getBody(), true);
1082                 if (empty($data)) {
1083                         return $serverdata;
1084                 }
1085
1086                 if (!empty($data['totalResults'])) {
1087                         $registeredUsers = $serverdata['registered-users'] ?? 0;
1088                         $serverdata['registered-users'] = max($data['totalResults'], $registeredUsers);
1089                         $serverdata['directory-type'] = self::DT_POCO;
1090                         $serverdata['poco'] = $url . '/poco';
1091                 }
1092
1093                 return $serverdata;
1094         }
1095
1096         /**
1097          * Checks if the given server does have a Mastodon style directory endpoint.
1098          *
1099          * @param string $url        URL of the given server
1100          * @param array  $serverdata array with server data
1101          *
1102          * @return array server data
1103          */
1104         public static function checkMastodonDirectory(string $url, array $serverdata)
1105         {
1106                 $curlResult = DI::httpRequest()->get($url . '/api/v1/directory?limit=1');
1107                 if (!$curlResult->isSuccess()) {
1108                         return $serverdata;
1109                 }
1110
1111                 $data = json_decode($curlResult->getBody(), true);
1112                 if (empty($data)) {
1113                         return $serverdata;
1114                 }
1115
1116                 if (count($data) == 1) {
1117                         $serverdata['directory-type'] = self::DT_MASTODON;
1118                 }
1119
1120                 return $serverdata;
1121         }
1122
1123         /**
1124          * Detects the version number of a given server when it was a NextCloud installation
1125          *
1126          * @param string $url        URL of the given server
1127          * @param array  $serverdata array with server data
1128          *
1129          * @return array server data
1130          */
1131         private static function detectNextcloud(string $url, array $serverdata)
1132         {
1133                 $curlResult = DI::httpRequest()->get($url . '/status.php');
1134
1135                 if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
1136                         return $serverdata;
1137                 }
1138
1139                 $data = json_decode($curlResult->getBody(), true);
1140                 if (empty($data)) {
1141                         return $serverdata;
1142                 }
1143
1144                 if (!empty($data['version'])) {
1145                         $serverdata['platform'] = 'nextcloud';
1146                         $serverdata['version'] = $data['version'];
1147                         $serverdata['network'] = Protocol::ACTIVITYPUB;
1148
1149                         if (in_array($serverdata['detection-method'], [self::DETECT_HEADER, self::DETECT_BODY, self::DETECT_MANUAL])) {
1150                                 $serverdata['detection-method'] = self::DETECT_STATUS_PHP;
1151                         }
1152                 }
1153
1154                 return $serverdata;
1155         }
1156
1157         /**
1158          * Detects data from a given server url if it was a mastodon alike system
1159          *
1160          * @param string $url        URL of the given server
1161          * @param array  $serverdata array with server data
1162          *
1163          * @return array server data
1164          */
1165         private static function detectMastodonAlikes(string $url, array $serverdata)
1166         {
1167                 $curlResult = DI::httpRequest()->get($url . '/api/v1/instance');
1168
1169                 if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
1170                         return $serverdata;
1171                 }
1172
1173                 $data = json_decode($curlResult->getBody(), true);
1174                 if (empty($data)) {
1175                         return $serverdata;
1176                 }
1177
1178                 if (in_array($serverdata['detection-method'], [self::DETECT_HEADER, self::DETECT_BODY, self::DETECT_MANUAL])) {
1179                         $serverdata['detection-method'] = self::DETECT_MASTODON_API;
1180                 }
1181
1182                 if (!empty($data['version'])) {
1183                         $serverdata['platform'] = 'mastodon';
1184                         $serverdata['version'] = $data['version'] ?? '';
1185                         $serverdata['network'] = Protocol::ACTIVITYPUB;
1186                 }
1187
1188                 if (!empty($data['title'])) {
1189                         $serverdata['site_name'] = $data['title'];
1190                 }
1191
1192                 if (!empty($data['title']) && empty($serverdata['platform']) && empty($serverdata['network'])) {
1193                         $serverdata['platform'] = 'mastodon';
1194                         $serverdata['network'] = Protocol::ACTIVITYPUB;
1195                 }
1196
1197                 if (!empty($data['description'])) {
1198                         $serverdata['info'] = trim($data['description']);
1199                 }
1200
1201                 if (!empty($data['stats']['user_count'])) {
1202                         $serverdata['registered-users'] = $data['stats']['user_count'];
1203                 }
1204
1205                 if (!empty($serverdata['version']) && preg_match('/.*?\(compatible;\s(.*)\s(.*)\)/ism', $serverdata['version'], $matches)) {
1206                         $serverdata['platform'] = strtolower($matches[1]);
1207                         $serverdata['version'] = $matches[2];
1208                 }
1209
1210                 if (!empty($serverdata['version']) && strstr(strtolower($serverdata['version']), 'pleroma')) {
1211                         $serverdata['platform'] = 'pleroma';
1212                         $serverdata['version'] = trim(str_ireplace('pleroma', '', $serverdata['version']));
1213                 }
1214
1215                 if (!empty($serverdata['platform']) && strstr($serverdata['platform'], 'pleroma')) {
1216                         $serverdata['version'] = trim(str_ireplace('pleroma', '', $serverdata['platform']));
1217                         $serverdata['platform'] = 'pleroma';
1218                 }
1219
1220                 return $serverdata;
1221         }
1222
1223         /**
1224          * Detects data from typical Hubzilla endpoints
1225          *
1226          * @param string $url        URL of the given server
1227          * @param array  $serverdata array with server data
1228          *
1229          * @return array server data
1230          */
1231         private static function detectHubzilla(string $url, array $serverdata)
1232         {
1233                 $curlResult = DI::httpRequest()->get($url . '/api/statusnet/config.json');
1234                 if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
1235                         return $serverdata;
1236                 }
1237
1238                 $data = json_decode($curlResult->getBody(), true);
1239                 if (empty($data) || empty($data['site'])) {
1240                         return $serverdata;
1241                 }
1242
1243                 if (!empty($data['site']['name'])) {
1244                         $serverdata['site_name'] = $data['site']['name'];
1245                 }
1246
1247                 if (!empty($data['site']['platform'])) {
1248                         $serverdata['platform'] = strtolower($data['site']['platform']['PLATFORM_NAME']);
1249                         $serverdata['version'] = $data['site']['platform']['STD_VERSION'];
1250                         $serverdata['network'] = Protocol::ZOT;
1251                 }
1252
1253                 if (!empty($data['site']['hubzilla'])) {
1254                         $serverdata['platform'] = strtolower($data['site']['hubzilla']['PLATFORM_NAME']);
1255                         $serverdata['version'] = $data['site']['hubzilla']['RED_VERSION'];
1256                         $serverdata['network'] = Protocol::ZOT;
1257                 }
1258
1259                 if (!empty($data['site']['redmatrix'])) {
1260                         if (!empty($data['site']['redmatrix']['PLATFORM_NAME'])) {
1261                                 $serverdata['platform'] = strtolower($data['site']['redmatrix']['PLATFORM_NAME']);
1262                         } elseif (!empty($data['site']['redmatrix']['RED_PLATFORM'])) {
1263                                 $serverdata['platform'] = strtolower($data['site']['redmatrix']['RED_PLATFORM']);
1264                         }
1265
1266                         $serverdata['version'] = $data['site']['redmatrix']['RED_VERSION'];
1267                         $serverdata['network'] = Protocol::ZOT;
1268                 }
1269
1270                 $private = false;
1271                 $inviteonly = false;
1272                 $closed = false;
1273
1274                 if (!empty($data['site']['closed'])) {
1275                         $closed = self::toBoolean($data['site']['closed']);
1276                 }
1277
1278                 if (!empty($data['site']['private'])) {
1279                         $private = self::toBoolean($data['site']['private']);
1280                 }
1281
1282                 if (!empty($data['site']['inviteonly'])) {
1283                         $inviteonly = self::toBoolean($data['site']['inviteonly']);
1284                 }
1285
1286                 if (!$closed && !$private and $inviteonly) {
1287                         $serverdata['register_policy'] = Register::APPROVE;
1288                 } elseif (!$closed && !$private) {
1289                         $serverdata['register_policy'] = Register::OPEN;
1290                 } else {
1291                         $serverdata['register_policy'] = Register::CLOSED;
1292                 }
1293
1294                 if (!empty($serverdata['network']) && in_array($serverdata['detection-method'],
1295                         [self::DETECT_HEADER, self::DETECT_BODY, self::DETECT_MANUAL])) {
1296                         $serverdata['detection-method'] = self::DETECT_CONFIG_JSON;
1297                 }
1298
1299                 return $serverdata;
1300         }
1301
1302         /**
1303          * Converts input value to a boolean value
1304          *
1305          * @param string|integer $val
1306          *
1307          * @return boolean
1308          */
1309         private static function toBoolean($val)
1310         {
1311                 if (($val == 'true') || ($val == 1)) {
1312                         return true;
1313                 } elseif (($val == 'false') || ($val == 0)) {
1314                         return false;
1315                 }
1316
1317                 return $val;
1318         }
1319
1320         /**
1321          * Detect if the URL belongs to a GNU Social server
1322          *
1323          * @param string $url        URL of the given server
1324          * @param array  $serverdata array with server data
1325          *
1326          * @return array server data
1327          */
1328         private static function detectGNUSocial(string $url, array $serverdata)
1329         {
1330                 // Test for GNU Social
1331                 $curlResult = DI::httpRequest()->get($url . '/api/gnusocial/version.json');
1332                 if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') &&
1333                         ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) {
1334                         $serverdata['platform'] = 'gnusocial';
1335                         // Remove junk that some GNU Social servers return
1336                         $serverdata['version'] = str_replace(chr(239) . chr(187) . chr(191), '', $curlResult->getBody());
1337                         $serverdata['version'] = str_replace(["\r", "\n", "\t"], '', $serverdata['version']);
1338                         $serverdata['version'] = trim($serverdata['version'], '"');
1339                         $serverdata['network'] = Protocol::OSTATUS;
1340
1341                         if (in_array($serverdata['detection-method'], [self::DETECT_HEADER, self::DETECT_BODY, self::DETECT_MANUAL])) {
1342                                 $serverdata['detection-method'] = self::DETECT_GNUSOCIAL;
1343                         }
1344         
1345                         return $serverdata;
1346                 }
1347
1348                 // Test for Statusnet
1349                 $curlResult = DI::httpRequest()->get($url . '/api/statusnet/version.json');
1350                 if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') &&
1351                         ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) {
1352
1353                         // Remove junk that some GNU Social servers return
1354                         $serverdata['version'] = str_replace(chr(239).chr(187).chr(191), '', $curlResult->getBody());
1355                         $serverdata['version'] = str_replace(["\r", "\n", "\t"], '', $serverdata['version']);
1356                         $serverdata['version'] = trim($serverdata['version'], '"');
1357
1358                         if (!empty($serverdata['version']) && strtolower(substr($serverdata['version'], 0, 7)) == 'pleroma') {
1359                                 $serverdata['platform'] = 'pleroma';
1360                                 $serverdata['version'] = trim(str_ireplace('pleroma', '', $serverdata['version']));
1361                                 $serverdata['network'] = Protocol::ACTIVITYPUB;
1362                         } else {
1363                                 $serverdata['platform'] = 'statusnet';
1364                                 $serverdata['network'] = Protocol::OSTATUS;
1365                         }
1366
1367                         if (in_array($serverdata['detection-method'], [self::DETECT_HEADER, self::DETECT_BODY, self::DETECT_MANUAL])) {
1368                                 $serverdata['detection-method'] = self::DETECT_STATUSNET;
1369                         }
1370                 }
1371
1372                 return $serverdata;
1373         }
1374
1375         /**
1376          * Detect if the URL belongs to a Friendica server
1377          *
1378          * @param string $url        URL of the given server
1379          * @param array  $serverdata array with server data
1380          *
1381          * @return array server data
1382          */
1383         private static function detectFriendica(string $url, array $serverdata)
1384         {
1385                 $curlResult = DI::httpRequest()->get($url . '/friendica/json');
1386                 if (!$curlResult->isSuccess()) {
1387                         $curlResult = DI::httpRequest()->get($url . '/friendika/json');
1388                         $friendika = true;
1389                         $platform = 'Friendika';
1390                 } else {
1391                         $friendika = false;
1392                         $platform = 'Friendica';
1393                 }
1394
1395                 if (!$curlResult->isSuccess()) {
1396                         return $serverdata;
1397                 }
1398
1399                 $data = json_decode($curlResult->getBody(), true);
1400                 if (empty($data) || empty($data['version'])) {
1401                         return $serverdata;
1402                 }
1403
1404                 if (in_array($serverdata['detection-method'], [self::DETECT_HEADER, self::DETECT_BODY, self::DETECT_MANUAL])) {                 
1405                         $serverdata['detection-method'] = $friendika ? self::DETECT_FRIENDIKA : self::DETECT_FRIENDICA;
1406                 }
1407
1408                 $serverdata['network'] = Protocol::DFRN;
1409                 $serverdata['version'] = $data['version'];
1410
1411                 if (!empty($data['no_scrape_url'])) {
1412                         $serverdata['noscrape'] = $data['no_scrape_url'];
1413                 }
1414
1415                 if (!empty($data['site_name'])) {
1416                         $serverdata['site_name'] = $data['site_name'];
1417                 }
1418
1419                 if (!empty($data['info'])) {
1420                         $serverdata['info'] = trim($data['info']);
1421                 }
1422
1423                 $register_policy = ($data['register_policy'] ?? '') ?: 'REGISTER_CLOSED';
1424                 switch ($register_policy) {
1425                         case 'REGISTER_OPEN':
1426                                 $serverdata['register_policy'] = Register::OPEN;
1427                                 break;
1428
1429                         case 'REGISTER_APPROVE':
1430                                 $serverdata['register_policy'] = Register::APPROVE;
1431                                 break;
1432
1433                         case 'REGISTER_CLOSED':
1434                         case 'REGISTER_INVITATION':
1435                                 $serverdata['register_policy'] = Register::CLOSED;
1436                                 break;
1437                         default:
1438                                 Logger::info('Register policy is invalid', ['policy' => $register_policy, 'server' => $url]);
1439                                 $serverdata['register_policy'] = Register::CLOSED;
1440                                 break;
1441                 }
1442
1443                 $serverdata['platform'] = strtolower($data['platform'] ?? $platform);
1444
1445                 return $serverdata;
1446         }
1447
1448         /**
1449          * Analyses the landing page of a given server for hints about type and system of that server
1450          *
1451          * @param object $curlResult result of curl execution
1452          * @param array  $serverdata array with server data
1453          * @param string $url        Server URL
1454          *
1455          * @return array server data
1456          */
1457         private static function analyseRootBody($curlResult, array $serverdata, string $url)
1458         {
1459                 $doc = new DOMDocument();
1460                 @$doc->loadHTML($curlResult->getBody());
1461                 $xpath = new DOMXPath($doc);
1462
1463                 $title = trim(XML::getFirstNodeValue($xpath, '//head/title/text()'));
1464                 if (!empty($title)) {
1465                         $serverdata['site_name'] = $title;
1466                 }
1467
1468                 $list = $xpath->query('//meta[@name]');
1469
1470                 foreach ($list as $node) {
1471                         $attr = [];
1472                         if ($node->attributes->length) {
1473                                 foreach ($node->attributes as $attribute) {
1474                                         $value = trim($attribute->value);
1475                                         if (empty($value)) {
1476                                                 continue;
1477                                         }
1478
1479                                         $attr[$attribute->name] = $value;
1480                                 }
1481
1482                                 if (empty($attr['name']) || empty($attr['content'])) {
1483                                         continue;
1484                                 }
1485                         }
1486
1487                         if ($attr['name'] == 'description') {
1488                                 $serverdata['info'] = $attr['content'];
1489                         }
1490
1491                         if (in_array($attr['name'], ['application-name', 'al:android:app_name', 'al:ios:app_name',
1492                                 'twitter:app:name:googleplay', 'twitter:app:name:iphone', 'twitter:app:name:ipad'])) {
1493                                 $serverdata['platform'] = strtolower($attr['content']);
1494                                 if (in_array($attr['content'], ['Misskey', 'Write.as'])) {
1495                                         $serverdata['network'] = Protocol::ACTIVITYPUB;
1496                                 }
1497                         }
1498                         if (($attr['name'] == 'generator') && (empty($serverdata['platform']) || (substr(strtolower($attr['content']), 0, 9) == 'wordpress'))) {
1499                                 $serverdata['platform'] = strtolower($attr['content']);
1500                                 $version_part = explode(' ', $attr['content']);
1501
1502                                 if (count($version_part) == 2) {
1503                                         if (in_array($version_part[0], ['WordPress'])) {
1504                                                 $serverdata['platform'] = strtolower($version_part[0]);
1505                                                 $serverdata['version'] = $version_part[1];
1506
1507                                                 // We still do need a reliable test if some AP plugin is activated
1508                                                 if (DBA::exists('apcontact', ['baseurl' => $url])) {
1509                                                         $serverdata['network'] = Protocol::ACTIVITYPUB;
1510                                                 } else {
1511                                                         $serverdata['network'] = Protocol::FEED;
1512                                                 }
1513
1514                                                 if ($serverdata['detection-method'] == self::DETECT_MANUAL) {
1515                                                         $serverdata['detection-method'] = self::DETECT_BODY;
1516                                                 }
1517                                         }
1518                                         if (in_array($version_part[0], ['Friendika', 'Friendica'])) {
1519                                                 $serverdata['platform'] = strtolower($version_part[0]);
1520                                                 $serverdata['version'] = $version_part[1];
1521                                                 $serverdata['network'] = Protocol::DFRN;
1522                                         }
1523                                 }
1524                         }
1525                 }
1526
1527                 $list = $xpath->query('//meta[@property]');
1528
1529                 foreach ($list as $node) {
1530                         $attr = [];
1531                         if ($node->attributes->length) {
1532                                 foreach ($node->attributes as $attribute) {
1533                                         $value = trim($attribute->value);
1534                                         if (empty($value)) {
1535                                                 continue;
1536                                         }
1537
1538                                         $attr[$attribute->name] = $value;
1539                                 }
1540
1541                                 if (empty($attr['property']) || empty($attr['content'])) {
1542                                         continue;
1543                                 }
1544                         }
1545
1546                         if ($attr['property'] == 'og:site_name') {
1547                                 $serverdata['site_name'] = $attr['content'];
1548                         }
1549
1550                         if ($attr['property'] == 'og:description') {
1551                                 $serverdata['info'] = $attr['content'];
1552                         }
1553
1554                         if ($attr['property'] == 'og:platform') {
1555                                 $serverdata['platform'] = strtolower($attr['content']);
1556
1557                                 if (in_array($attr['content'], ['PeerTube'])) {
1558                                         $serverdata['network'] = Protocol::ACTIVITYPUB;
1559                                 }
1560                         }
1561
1562                         if ($attr['property'] == 'generator') {
1563                                 $serverdata['platform'] = strtolower($attr['content']);
1564
1565                                 if (in_array($attr['content'], ['hubzilla'])) {
1566                                         // We later check which compatible protocol modules are loaded.
1567                                         $serverdata['network'] = Protocol::ZOT;
1568                                 }
1569                         }
1570                 }
1571
1572                 if (!empty($serverdata['network']) && ($serverdata['detection-method'] == self::DETECT_MANUAL)) {
1573                         $serverdata['detection-method'] = self::DETECT_BODY;
1574                 }
1575
1576                 return $serverdata;
1577         }
1578
1579         /**
1580          * Analyses the header data of a given server for hints about type and system of that server
1581          *
1582          * @param object $curlResult result of curl execution
1583          * @param array  $serverdata array with server data
1584          *
1585          * @return array server data
1586          */
1587         private static function analyseRootHeader($curlResult, array $serverdata)
1588         {
1589                 if ($curlResult->getHeader('server') == 'Mastodon') {
1590                         $serverdata['platform'] = 'mastodon';
1591                         $serverdata['network'] = Protocol::ACTIVITYPUB;
1592                 } elseif ($curlResult->inHeader('x-diaspora-version')) {
1593                         $serverdata['platform'] = 'diaspora';
1594                         $serverdata['network'] = Protocol::DIASPORA;
1595                         $serverdata['version'] = $curlResult->getHeader('x-diaspora-version');
1596                 } elseif ($curlResult->inHeader('x-friendica-version')) {
1597                         $serverdata['platform'] = 'friendica';
1598                         $serverdata['network'] = Protocol::DFRN;
1599                         $serverdata['version'] = $curlResult->getHeader('x-friendica-version');
1600                 } else {
1601                         return $serverdata;
1602                 }
1603
1604                 if ($serverdata['detection-method'] == self::DETECT_MANUAL) {
1605                         $serverdata['detection-method'] = self::DETECT_HEADER;
1606                 }
1607
1608                 return $serverdata;
1609         }
1610
1611         /**
1612          * Test if the body contains valid content
1613          *
1614          * @param string $body
1615          * @return boolean
1616          */
1617         private static function invalidBody(string $body)
1618         {
1619                 // Currently we only test for a HTML element.
1620                 // Possibly we enhance this in the future.
1621                 return !strpos($body, '>');
1622         }
1623
1624         /**
1625          * Update GServer entries
1626          */
1627         public static function discover()
1628         {
1629                 // Update the server list
1630                 self::discoverFederation();
1631
1632                 $no_of_queries = 5;
1633
1634                 $requery_days = intval(DI::config()->get('system', 'poco_requery_days'));
1635
1636                 if ($requery_days == 0) {
1637                         $requery_days = 7;
1638                 }
1639
1640                 $last_update = date('c', time() - (60 * 60 * 24 * $requery_days));
1641
1642                 $gservers = DBA::p("SELECT `id`, `url`, `nurl`, `network`, `poco`, `directory-type`
1643                         FROM `gserver`
1644                         WHERE NOT `failed`
1645                         AND `directory-type` != ?
1646                         AND `last_poco_query` < ?
1647                         ORDER BY RAND()", self::DT_NONE, $last_update
1648                 );
1649
1650                 while ($gserver = DBA::fetch($gservers)) {
1651                         Logger::info('Update peer list', ['server' => $gserver['url'], 'id' => $gserver['id']]);
1652                         Worker::add(PRIORITY_LOW, 'UpdateServerPeers', $gserver['url']);
1653
1654                         Logger::info('Update directory', ['server' => $gserver['url'], 'id' => $gserver['id']]);
1655                         Worker::add(PRIORITY_LOW, 'UpdateServerDirectory', $gserver);
1656
1657                         $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
1658                         DBA::update('gserver', $fields, ['nurl' => $gserver['nurl']]);
1659         
1660                         if (--$no_of_queries == 0) {
1661                                 break;
1662                         }
1663                 }
1664
1665                 DBA::close($gservers);
1666         }
1667
1668         /**
1669          * Discover federated servers
1670          */
1671         private static function discoverFederation()
1672         {
1673                 $last = DI::config()->get('poco', 'last_federation_discovery');
1674
1675                 if ($last) {
1676                         $next = $last + (24 * 60 * 60);
1677
1678                         if ($next > time()) {
1679                                 return;
1680                         }
1681                 }
1682
1683                 // Discover federated servers
1684                 $protocols = ['activitypub', 'diaspora', 'dfrn', 'ostatus'];
1685                 foreach ($protocols as $protocol) {
1686                         $query = '{nodes(protocol:"' . $protocol . '"){host}}';
1687                         $curlResult = DI::httpRequest()->fetch('https://the-federation.info/graphql?query=' . urlencode($query));
1688                         if (!empty($curlResult)) {
1689                                 $data = json_decode($curlResult, true);
1690                                 if (!empty($data['data']['nodes'])) {
1691                                         foreach ($data['data']['nodes'] as $server) {
1692                                                 // Using "only_nodeinfo" since servers that are listed on that page should always have it.
1693                                                 Worker::add(PRIORITY_LOW, 'UpdateGServer', 'https://' . $server['host'], true);
1694                                         }
1695                                 }
1696                         }
1697                 }
1698
1699                 // Disvover Mastodon servers
1700                 $accesstoken = DI::config()->get('system', 'instances_social_key');
1701
1702                 if (!empty($accesstoken)) {
1703                         $api = 'https://instances.social/api/1.0/instances/list?count=0';
1704                         $header = ['Authorization: Bearer '.$accesstoken];
1705                         $curlResult = DI::httpRequest()->get($api, ['header' => $header]);
1706
1707                         if ($curlResult->isSuccess()) {
1708                                 $servers = json_decode($curlResult->getBody(), true);
1709
1710                                 foreach ($servers['instances'] as $server) {
1711                                         $url = (is_null($server['https_score']) ? 'http' : 'https') . '://' . $server['name'];
1712                                         Worker::add(PRIORITY_LOW, 'UpdateGServer', $url);
1713                                 }
1714                         }
1715                 }
1716
1717                 DI::config()->set('poco', 'last_federation_discovery', time());
1718         }
1719 }