]> git.mxchange.org Git - friendica.git/blob - src/Model/GServer.php
Merge remote-tracking branch 'upstream/develop' into server-detection
[friendica.git] / src / Model / GServer.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
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 Exception;
27 use Friendica\Core\Logger;
28 use Friendica\Core\Protocol;
29 use Friendica\Core\System;
30 use Friendica\Core\Worker;
31 use Friendica\Database\Database;
32 use Friendica\Database\DBA;
33 use Friendica\DI;
34 use Friendica\Module\Register;
35 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
36 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
37 use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
38 use Friendica\Network\Probe;
39 use Friendica\Protocol\ActivityPub;
40 use Friendica\Protocol\Relay;
41 use Friendica\Util\DateTimeFormat;
42 use Friendica\Util\JsonLD;
43 use Friendica\Util\Network;
44 use Friendica\Util\Strings;
45 use Friendica\Util\XML;
46 use Friendica\Network\HTTPException;
47 use GuzzleHttp\Psr7\Uri;
48
49 /**
50  * This class handles GServer related functions
51  */
52 class GServer
53 {
54         // Directory types
55         const DT_NONE = 0;
56         const DT_POCO = 1;
57         const DT_MASTODON = 2;
58
59         // Methods to detect server types
60
61         // Non endpoint specific methods
62         const DETECT_MANUAL = 0;
63         const DETECT_HEADER = 1;
64         const DETECT_BODY = 2;
65         const DETECT_HOST_META = 3;
66         const DETECT_CONTACTS = 4;
67         const DETECT_AP_ACTOR = 5;
68         const DETECT_AP_COLLECTION = 6;
69
70         const DETECT_UNSPECIFIC = [self::DETECT_MANUAL, self::DETECT_HEADER, self::DETECT_BODY, self::DETECT_HOST_META, self::DETECT_CONTACTS, self::DETECT_AP_ACTOR];
71
72         // Implementation specific endpoints
73         const DETECT_FRIENDIKA = 10;
74         const DETECT_FRIENDICA = 11;
75         const DETECT_STATUSNET = 12;
76         const DETECT_GNUSOCIAL = 13;
77         const DETECT_CONFIG_JSON = 14; // Statusnet, GNU Social, Older Hubzilla/Redmatrix
78         const DETECT_SITEINFO_JSON = 15; // Newer Hubzilla
79         const DETECT_MASTODON_API = 16;
80         const DETECT_STATUS_PHP = 17; // Nextcloud
81         const DETECT_V1_CONFIG = 18;
82         const DETECT_PUMPIO = 19; // Deprecated
83         const DETECT_SYSTEM_ACTOR = 20; // Mistpark, Osada, Roadhouse, Zap
84
85         // Standardized endpoints
86         const DETECT_STATISTICS_JSON = 100;
87         const DETECT_NODEINFO_1 = 101;
88         const DETECT_NODEINFO_2 = 102;
89         const DETECT_NODEINFO_210 = 103;
90
91         /**
92          * Check for the existance of a server and adds it in the background if not existant
93          *
94          * @param string $url
95          * @param boolean $only_nodeinfo
96          * @return void
97          */
98         public static function add(string $url, bool $only_nodeinfo = false)
99         {
100                 if (self::getID($url, false)) {
101                         return;
102                 }
103
104                 Worker::add(PRIORITY_LOW, 'UpdateGServer', $url, $only_nodeinfo);
105         }
106
107         /**
108          * Get the ID for the given server URL
109          *
110          * @param string $url
111          * @param boolean $no_check Don't check if the server hadn't been found
112          * @return int|null gserver id or NULL on empty URL or failed check
113          */
114         public static function getID(string $url, bool $no_check = false)
115         {
116                 if (empty($url)) {
117                         return null;
118                 }
119
120                 $url = self::cleanURL($url);
121
122                 $gserver = DBA::selectFirst('gserver', ['id'], ['nurl' => Strings::normaliseLink($url)]);
123                 if (DBA::isResult($gserver)) {
124                         Logger::debug('Got ID for URL', ['id' => $gserver['id'], 'url' => $url, 'callstack' => System::callstack(20)]);
125                         return $gserver['id'];
126                 }
127
128                 if ($no_check || !self::check($url)) {
129                         return null;
130                 }
131
132                 return self::getID($url, true);
133         }
134
135         /**
136          * Retrieves all the servers which base domain are matching the provided domain pattern
137          *
138          * The pattern is a simple fnmatch() pattern with ? for single wildcard and * for multiple wildcard
139          *
140          * @param string $pattern
141          * @return array
142          * @throws Exception
143          */
144         public static function listByDomainPattern(string $pattern): array
145         {
146                 $likePattern = 'http://' . strtr($pattern, ['_' => '\_', '%' => '\%', '?' => '_', '*' => '%']);
147
148                 // The SUBSTRING_INDEX returns everything before the eventual third /, which effectively trims an
149                 // eventual server path and keep only the server domain which we're matching against the pattern.
150                 $sql = "SELECT `gserver`.*, COUNT(*) AS `contacts`
151                         FROM `gserver`
152                         LEFT JOIN `contact` ON `gserver`.`id` = `contact`.`gsid`
153                         WHERE SUBSTRING_INDEX(`gserver`.`nurl`, '/', 3) LIKE ?
154                         AND NOT `gserver`.`failed`
155                         GROUP BY `gserver`.`id`";
156
157                 $stmt = DI::dba()->p($sql, $likePattern);
158
159                 return DI::dba()->toArray($stmt);
160         }
161
162         /**
163          * Checks if the given server is reachable
164          *
165          * @param string  $profile URL of the given profile
166          * @param string  $server  URL of the given server (If empty, taken from profile)
167          * @param string  $network Network value that is used, when detection failed
168          * @param boolean $force   Force an update.
169          *
170          * @return boolean 'true' if server seems vital
171          */
172         public static function reachable(string $profile, string $server = '', string $network = '', bool $force = false): bool
173         {
174                 if ($server == '') {
175                         $contact = Contact::getByURL($profile, null, ['baseurl']);
176                         if (!empty($contact['baseurl'])) {
177                                 $server = $contact['baseurl'];
178                         }
179                 }
180
181                 if ($server == '') {
182                         return true;
183                 }
184
185                 return self::check($server, $network, $force);
186         }
187
188         public static function getNextUpdateDate(bool $success, string $created = '', string $last_contact = '', bool $undetected = false)
189         {
190                 // On successful contact process check again next week when it is a detected system.
191                 // When we haven't detected the system, it could be a static website or a really old system.
192                 if ($success) {
193                         return DateTimeFormat::utc($undetected ? 'now +1 month' : 'now +7 day');
194                 }
195
196                 $now = strtotime(DateTimeFormat::utcNow());
197
198                 if ($created > $last_contact) {
199                         $contact_time = strtotime($created);
200                 } else {
201                         $contact_time = strtotime($last_contact);
202                 }
203
204                 // If the last contact was less than 6 hours before then try again in 6 hours
205                 if (($now - $contact_time) < (60 * 60 * 6)) {
206                         return DateTimeFormat::utc('now +6 hour');
207                 }
208
209                 // If the last contact was less than 12 hours before then try again in 12 hours
210                 if (($now - $contact_time) < (60 * 60 * 12)) {
211                         return DateTimeFormat::utc('now +12 hour');
212                 }
213
214                 // If the last contact was less than 24 hours before then try tomorrow again
215                 if (($now - $contact_time) < (60 * 60 * 24)) {
216                         return DateTimeFormat::utc('now +1 day');
217                 }
218
219                 // If the last contact was less than a week before then try again in a week
220                 if (($now - $contact_time) < (60 * 60 * 24 * 7)) {
221                         return DateTimeFormat::utc('now +1 week');
222                 }
223
224                 // If the last contact was less than two weeks before then try again in two week
225                 if (($now - $contact_time) < (60 * 60 * 24 * 14)) {
226                         return DateTimeFormat::utc('now +2 week');
227                 }
228
229                 // If the last contact was less than a month before then try again in a month
230                 if (($now - $contact_time) < (60 * 60 * 24 * 30)) {
231                         return DateTimeFormat::utc('now +1 month');
232                 }
233
234                 // The system hadn't been successul contacted for more than a month, so try again in three months
235                 return DateTimeFormat::utc('now +3 month');
236         }
237
238         /**
239          * Checks the state of the given server.
240          *
241          * @param string  $server_url    URL of the given server
242          * @param string  $network       Network value that is used, when detection failed
243          * @param boolean $force         Force an update.
244          * @param boolean $only_nodeinfo Only use nodeinfo for server detection
245          *
246          * @return boolean 'true' if server seems vital
247          */
248         public static function check(string $server_url, string $network = '', bool $force = false, bool $only_nodeinfo = false): bool
249         {
250                 $server_url = self::cleanURL($server_url);
251                 if ($server_url == '') {
252                         return false;
253                 }
254
255                 $gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($server_url)]);
256                 if (DBA::isResult($gserver)) {
257                         if ($gserver['created'] <= DBA::NULL_DATETIME) {
258                                 $fields = ['created' => DateTimeFormat::utcNow()];
259                                 $condition = ['nurl' => Strings::normaliseLink($server_url)];
260                                 self::update($fields, $condition);
261                         }
262
263                         if (!$force && (strtotime($gserver['next_contact']) > time())) {
264                                 Logger::info('No update needed', ['server' => $server_url]);
265                                 return (!$gserver['failed']);
266                         }
267                         Logger::info('Server is outdated. Start discovery.', ['Server' => $server_url, 'Force' => $force]);
268                 } else {
269                         Logger::info('Server is unknown. Start discovery.', ['Server' => $server_url]);
270                 }
271
272                 return self::detect($server_url, $network, $only_nodeinfo);
273         }
274
275         /**
276          * Set failed server status
277          *
278          * @param string $url
279          */
280         public static function setFailure(string $url)
281         {
282                 $gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($url)]);
283                 if (DBA::isResult($gserver)) {
284                         $next_update = self::getNextUpdateDate(false, $gserver['created'], $gserver['last_contact']);
285                         self::update(['url' => $url, 'failed' => true, 'last_failure' => DateTimeFormat::utcNow(),
286                         'next_contact' => $next_update, 'network' => Protocol::PHANTOM, 'detection-method' => null],
287                         ['nurl' => Strings::normaliseLink($url)]);
288                         Logger::info('Set failed status for existing server', ['url' => $url]);
289                         return;
290                 }
291                 DBA::insert('gserver', ['url' => $url, 'nurl' => Strings::normaliseLink($url),
292                         'network' => Protocol::PHANTOM, 'created' => DateTimeFormat::utcNow(),
293                         'failed' => true, 'last_failure' => DateTimeFormat::utcNow()]);
294                 Logger::info('Set failed status for new server', ['url' => $url]);
295         }
296
297         /**
298          * Remove unwanted content from the given URL
299          *
300          * @param string $url
301          * @return string cleaned URL
302          */
303         public static function cleanURL(string $url): string
304         {
305                 $url = trim($url, '/');
306                 $url = str_replace('/index.php', '', $url);
307
308                 $urlparts = parse_url($url);
309                 unset($urlparts['user']);
310                 unset($urlparts['pass']);
311                 unset($urlparts['query']);
312                 unset($urlparts['fragment']);
313                 return (string)Uri::fromParts($urlparts);
314         }
315
316         /**
317          * Detect server data (type, protocol, version number, ...)
318          * The detected data is then updated or inserted in the gserver table.
319          *
320          * @param string  $url           URL of the given server
321          * @param string  $network       Network value that is used, when detection failed
322          * @param boolean $only_nodeinfo Only use nodeinfo for server detection
323          *
324          * @return boolean 'true' if server could be detected
325          */
326         public static function detect(string $url, string $network = '', bool $only_nodeinfo = false): bool
327         {
328                 Logger::info('Detect server type', ['server' => $url]);
329
330                 $original_url = $url;
331
332                 // Remove URL content that is not supposed to exist for a server url
333                 $url = rtrim(self::cleanURL($url), '/');
334                 if (empty($url)) {
335                         Logger::notice('Empty URL.');
336                         return false;
337                 }
338
339                 // If the URL missmatches, then we mark the old entry as failure
340                 if (!Strings::compareLink($url, $original_url)) {
341                         self::setFailure($original_url);
342                         if (!self::getID($url, true)) {
343                                 self::detect($url, $network, $only_nodeinfo);
344                         }
345                         return false;
346                 }
347
348                 $valid_url = Network::isUrlValid($url);
349                 if (!$valid_url) {
350                         self::setFailure($url);
351                         return false;
352                 } else {
353                         $valid_url = rtrim($valid_url, '/');
354                 }
355
356                 if (!Strings::compareLink($url, $valid_url)) {
357                         // We only follow redirects when the path stays the same or the target url has no path.
358                         // Some systems have got redirects on their landing page to a single account page. This check handles it.
359                         if (((parse_url($url, PHP_URL_HOST) != parse_url($valid_url, PHP_URL_HOST)) && (parse_url($url, PHP_URL_PATH) == parse_url($valid_url, PHP_URL_PATH))) ||
360                                 (((parse_url($url, PHP_URL_HOST) != parse_url($valid_url, PHP_URL_HOST)) || (parse_url($url, PHP_URL_PATH) != parse_url($valid_url, PHP_URL_PATH))) && empty(parse_url($valid_url, PHP_URL_PATH)))) {
361                                 Logger::debug('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $valid_url]);
362                                 self::setFailure($url);
363                                 if (!self::getID($valid_url, true)) {
364                                         self::detect($valid_url, $network, $only_nodeinfo);
365                                 }
366                                 return false;
367                         }
368
369                         if ((parse_url($url, PHP_URL_HOST) != parse_url($valid_url, PHP_URL_HOST)) && (parse_url($url, PHP_URL_PATH) != parse_url($valid_url, PHP_URL_PATH)) &&
370                                 (parse_url($url, PHP_URL_PATH) == '')) {
371                                 Logger::debug('Found redirect. Mark old entry as failure and redirect to the basepath.', ['old' => $url, 'new' => $valid_url]);
372                                 $parts = parse_url($valid_url);
373                                 unset($parts['path']);
374                                 $valid_url = (string)Uri::fromParts($parts);
375
376                                 self::setFailure($url);
377                                 if (!self::getID($valid_url, true)) {
378                                         self::detect($valid_url, $network, $only_nodeinfo);
379                                 }
380                                 return false;   
381                         }
382                         Logger::debug('Found redirect, but ignore it.', ['old' => $url, 'new' => $valid_url]);
383                 }
384
385                 if ((parse_url($url, PHP_URL_HOST) == parse_url($valid_url, PHP_URL_HOST)) &&
386                         (parse_url($url, PHP_URL_PATH) == parse_url($valid_url, PHP_URL_PATH)) &&
387                         (parse_url($url, PHP_URL_SCHEME) != parse_url($valid_url, PHP_URL_SCHEME))) {
388                         $url = $valid_url;
389                 }
390
391                 $in_webroot = empty(parse_url($url, PHP_URL_PATH));
392
393                 // When a nodeinfo is present, we don't need to dig further
394                 $curlResult = DI::httpClient()->get($url . '/.well-known/x-nodeinfo2', HttpClientAccept::JSON);
395                 if ($curlResult->isTimeout()) {
396                         self::setFailure($url);
397                         return false;
398                 }
399
400                 $serverdata = self::parseNodeinfo210($curlResult);
401                 if (empty($serverdata)) {
402                         $curlResult = DI::httpClient()->get($url . '/.well-known/nodeinfo', HttpClientAccept::JSON);
403                         $serverdata = self::fetchNodeinfo($url, $curlResult);
404                 }
405
406                 if ($only_nodeinfo && empty($serverdata)) {
407                         Logger::info('Invalid nodeinfo in nodeinfo-mode, server is marked as failure', ['url' => $url]);
408                         self::setFailure($url);
409                         return false;
410                 } elseif (empty($serverdata)) {
411                         $serverdata = ['detection-method' => self::DETECT_MANUAL, 'network' => Protocol::PHANTOM, 'platform' => '', 'version' => '', 'site_name' => '', 'info' => ''];
412                 }
413
414                 // When there is no Nodeinfo, then use some protocol specific endpoints
415                 if ($serverdata['network'] == Protocol::PHANTOM) {
416                         if ($in_webroot) {
417                                 // Fetch the landing page, possibly it reveals some data
418                                 $accept = 'application/activity+json,application/ld+json,application/json,*/*;q=0.9';
419                                 $curlResult = DI::httpClient()->get($url, $accept);
420                                 if (!$curlResult->isSuccess() && $curlResult->getReturnCode() == '406') {
421                                         $curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML);
422                                         $html_fetched = true;
423                                 } else {
424                                         $html_fetched = false;
425                                 }
426
427                                 if ($curlResult->isSuccess()) {
428                                         $json = json_decode($curlResult->getBody(), true);
429                                         if (!empty($json) && is_array($json)) {
430                                                 $data = self::fetchDataFromSystemActor($json, $serverdata);
431                                                 $serverdata = $data['server'];
432                                                 $systemactor = $data['actor'];
433                                                 if (!$html_fetched && !in_array($serverdata['detection-method'], [self::DETECT_SYSTEM_ACTOR, self::DETECT_AP_COLLECTION])) {
434                                                         $curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML);
435                                                 }
436                                         } elseif (!$html_fetched && (strlen($curlResult->getBody()) < 1000)) {
437                                                 $curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML);
438                                         }
439
440                                         if ($serverdata['detection-method'] != self::DETECT_SYSTEM_ACTOR) {
441                                                 $serverdata = self::analyseRootHeader($curlResult, $serverdata);
442                                                 $serverdata = self::analyseRootBody($curlResult, $serverdata);
443                                         }
444                                 }
445
446                                 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
447                                         self::setFailure($url);
448                                         return false;
449                                 }
450
451                                 if (($serverdata['network'] == Protocol::PHANTOM) || in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
452                                         $serverdata = self::detectMastodonAlikes($url, $serverdata);
453                                 }
454                         }
455
456                         // All following checks are done for systems that always have got a "host-meta" endpoint.
457                         // With this check we don't have to waste time and ressources for dead systems.
458                         // Also this hopefully prevents us from receiving abuse messages.
459                         if (($serverdata['network'] == Protocol::PHANTOM) || in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
460                                 $validHostMeta = self::validHostMeta($url);
461                         } else {
462                                 $validHostMeta = false;
463                         }
464
465                         if ($validHostMeta) {
466                                 if (in_array($serverdata['detection-method'], [self::DETECT_MANUAL, self::DETECT_HEADER, self::DETECT_BODY])) {
467                                         $serverdata['detection-method'] = self::DETECT_HOST_META;
468                                 }
469
470                                 if (($serverdata['network'] == Protocol::PHANTOM) || in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
471                                         $serverdata = self::detectFriendica($url, $serverdata);
472                                 }
473
474                                 // The following systems have to be installed in the root directory.
475                                 if ($in_webroot) {
476                                         // the 'siteinfo.json' is some specific endpoint of Hubzilla and Red
477                                         if (($serverdata['network'] == Protocol::PHANTOM) || in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
478                                                 $serverdata = self::fetchSiteinfo($url, $serverdata);
479                                         }
480
481                                         // The 'siteinfo.json' doesn't seem to be present on older Hubzilla installations, so we check other endpoints as well
482                                         if (($serverdata['network'] == Protocol::PHANTOM) || in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
483                                                 $serverdata = self::detectHubzilla($url, $serverdata);
484                                         }
485
486                                         if (($serverdata['network'] == Protocol::PHANTOM) || in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
487                                                 $serverdata = self::detectPeertube($url, $serverdata);
488                                         }
489
490                                         if (($serverdata['network'] == Protocol::PHANTOM) || in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
491                                                 $serverdata = self::detectGNUSocial($url, $serverdata);
492                                         }
493                                 }
494                         } elseif (in_array($serverdata['platform'], ['friendica', 'friendika']) && in_array($serverdata['detection-method'], array_merge(self::DETECT_UNSPECIFIC, [self::DETECT_SYSTEM_ACTOR]))) {
495                                 $serverdata = self::detectFriendica($url, $serverdata);
496                         }
497
498                         if (($serverdata['network'] == Protocol::PHANTOM) || in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
499                                 $serverdata = self::detectNextcloud($url, $serverdata, $validHostMeta);
500                         }
501
502                         // When nodeinfo isn't present, we use the older 'statistics.json' endpoint
503                         // Since this endpoint is only rarely used, we query it at a later time
504                         if (in_array($serverdata['detection-method'], array_merge(self::DETECT_UNSPECIFIC, [self::DETECT_FRIENDICA, self::DETECT_CONFIG_JSON]))) {
505                                 $serverdata = self::fetchStatistics($url, $serverdata);
506                         }
507                 }
508
509                 // When we hadn't been able to detect the network type, we use the hint from the parameter
510                 if (($serverdata['network'] == Protocol::PHANTOM) && !empty($network)) {
511                         $serverdata['network'] = $network;
512                 }
513
514                 // Most servers aren't installed in a subdirectory, so we declare this entry as failed
515                 if (($serverdata['network'] == Protocol::PHANTOM) && !empty(parse_url($url, PHP_URL_PATH)) && in_array($serverdata['detection-method'], [self::DETECT_MANUAL])) {
516                         self::setFailure($url);
517                         return false;
518                 }
519
520                 $serverdata['url'] = $url;
521                 $serverdata['nurl'] = Strings::normaliseLink($url);
522
523                 // We have to prevent an endless loop here.
524                 // When a server is new, then there is no gserver entry yet.
525                 // But in "detectNetworkViaContacts" it could happen that a contact is updated,
526                 // and this can call this function here as well.
527                 if (self::getID($url, true) && (in_array($serverdata['network'], [Protocol::PHANTOM, Protocol::FEED]) ||
528                         in_array($serverdata['detection-method'], [self::DETECT_MANUAL, self::DETECT_HEADER, self::DETECT_BODY, self::DETECT_HOST_META]))) {
529                         $serverdata = self::detectNetworkViaContacts($url, $serverdata);
530                 }
531
532                 // Detect the directory type
533                 $serverdata['directory-type'] = self::DT_NONE;
534
535                 if (in_array($serverdata['network'], Protocol::FEDERATED)) {
536                         $serverdata = self::checkMastodonDirectory($url, $serverdata);
537
538                         if ($serverdata['directory-type'] == self::DT_NONE) {
539                                 $serverdata = self::checkPoCo($url, $serverdata);
540                         }
541                 }
542
543                 if ($serverdata['network'] == Protocol::ACTIVITYPUB) {
544                         $serverdata = self::fetchWeeklyUsage($url, $serverdata);
545                 }
546
547                 $serverdata['registered-users'] = $serverdata['registered-users'] ?? 0;
548
549                 // On an active server there has to be at least a single user
550                 if (!in_array($serverdata['network'], [Protocol::PHANTOM, Protocol::FEED]) && ($serverdata['registered-users'] == 0)) {
551                         $serverdata['registered-users'] = 1;
552                 } elseif (in_array($serverdata['network'], [Protocol::PHANTOM, Protocol::FEED])) {
553                         $serverdata['registered-users'] = 0;
554                 }
555
556                 $serverdata['next_contact'] = self::getNextUpdateDate(true, '', '', in_array($serverdata['network'], [Protocol::PHANTOM, Protocol::FEED]));
557
558                 $serverdata['last_contact'] = DateTimeFormat::utcNow();
559                 $serverdata['failed'] = false;
560
561                 $gserver = DBA::selectFirst('gserver', ['network'], ['nurl' => Strings::normaliseLink($url)]);
562                 if (!DBA::isResult($gserver)) {
563                         $serverdata['created'] = DateTimeFormat::utcNow();
564                         $ret = DBA::insert('gserver', $serverdata);
565                         $id = DBA::lastInsertId();
566                 } else {
567                         $ret = self::update($serverdata, ['nurl' => $serverdata['nurl']]);
568                         $gserver = DBA::selectFirst('gserver', ['id'], ['nurl' => $serverdata['nurl']]);
569                         if (DBA::isResult($gserver)) {
570                                 $id = $gserver['id'];
571                         }
572                 }
573
574                 // Count the number of known contacts from this server
575                 if (!empty($id) && !in_array($serverdata['network'], [Protocol::PHANTOM, Protocol::FEED])) {
576                         $apcontacts = DBA::count('apcontact', ['gsid' => $id]);
577                         $contacts = DBA::count('contact', ['uid' => 0, 'gsid' => $id, 'failed' => false]);
578                         $max_users = max($apcontacts, $contacts);
579                         if ($max_users > $serverdata['registered-users']) {
580                                 Logger::info('Update registered users', ['id' => $id, 'url' => $serverdata['nurl'], 'registered-users' => $max_users]);
581                                 self::update(['registered-users' => $max_users], ['id' => $id]);
582                         }
583
584                         if (empty($serverdata['active-month-users'])) {
585                                 $contacts = DBA::count('contact', ["`uid` = ? AND `gsid` = ? AND NOT `failed` AND `last-item` > ?", 0, $id, DateTimeFormat::utc('now - 30 days')]);
586                                 if ($contacts > 0) {
587                                         Logger::info('Update monthly users', ['id' => $id, 'url' => $serverdata['nurl'], 'monthly-users' => $contacts]);
588                                         self::update(['active-month-users' => $contacts], ['id' => $id]);
589                                 }
590                         }
591
592                         if (empty($serverdata['active-halfyear-users'])) {
593                                 $contacts = DBA::count('contact', ["`uid` = ? AND `gsid` = ? AND NOT `failed` AND `last-item` > ?", 0, $id, DateTimeFormat::utc('now - 180 days')]);
594                                 if ($contacts > 0) {
595                                         Logger::info('Update halfyear users', ['id' => $id, 'url' => $serverdata['nurl'], 'halfyear-users' => $contacts]);
596                                         self::update(['active-halfyear-users' => $contacts], ['id' => $id]);
597                                 }
598                         }
599                 }
600
601                 if (in_array($serverdata['network'], [Protocol::DFRN, Protocol::DIASPORA])) {
602                         self::discoverRelay($url);
603                 }
604
605                 if (!empty($systemactor)) {
606                         $contact = Contact::getByURL($systemactor, true, ['gsid', 'baseurl', 'id', 'network', 'url', 'name']);
607                         Logger::debug('Fetched system actor',  ['url' => $url, 'gsid' => $id, 'contact' => $contact]);
608                 }
609
610                 return $ret;
611         }
612
613         /**
614          * Fetch relay data from a given server url
615          *
616          * @param string $server_url address of the server
617          * @return void
618          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
619          */
620         private static function discoverRelay(string $server_url)
621         {
622                 Logger::info('Discover relay data', ['server' => $server_url]);
623
624                 $curlResult = DI::httpClient()->get($server_url . '/.well-known/x-social-relay', HttpClientAccept::JSON);
625                 if (!$curlResult->isSuccess()) {
626                         return;
627                 }
628
629                 $data = json_decode($curlResult->getBody(), true);
630                 if (!is_array($data)) {
631                         return;
632                 }
633
634                 // Sanitize incoming data, see https://github.com/friendica/friendica/issues/8565
635                 $data['subscribe'] = (bool)$data['subscribe'] ?? false;
636
637                 if (!$data['subscribe'] || empty($data['scope']) || !in_array(strtolower($data['scope']), ['all', 'tags'])) {
638                         $data['scope'] = '';
639                         $data['subscribe'] = false;
640                         $data['tags'] = [];
641                 }
642
643                 $gserver = DBA::selectFirst('gserver', ['id', 'url', 'network', 'relay-subscribe', 'relay-scope'], ['nurl' => Strings::normaliseLink($server_url)]);
644                 if (!DBA::isResult($gserver)) {
645                         return;
646                 }
647
648                 if (($gserver['relay-subscribe'] != $data['subscribe']) || ($gserver['relay-scope'] != $data['scope'])) {
649                         $fields = ['relay-subscribe' => $data['subscribe'], 'relay-scope' => $data['scope']];
650                         self::update($fields, ['id' => $gserver['id']]);
651                 }
652
653                 DBA::delete('gserver-tag', ['gserver-id' => $gserver['id']]);
654
655                 if ($data['scope'] == 'tags') {
656                         // Avoid duplicates
657                         $tags = [];
658                         foreach ($data['tags'] as $tag) {
659                                 $tag = mb_strtolower($tag);
660                                 if (strlen($tag) < 100) {
661                                         $tags[$tag] = $tag;
662                                 }
663                         }
664
665                         foreach ($tags as $tag) {
666                                 DBA::insert('gserver-tag', ['gserver-id' => $gserver['id'], 'tag' => $tag], Database::INSERT_IGNORE);
667                         }
668                 }
669
670                 // Create or update the relay contact
671                 $fields = [];
672                 if (isset($data['protocols'])) {
673                         if (isset($data['protocols']['diaspora'])) {
674                                 $fields['network'] = Protocol::DIASPORA;
675
676                                 if (isset($data['protocols']['diaspora']['receive'])) {
677                                         $fields['batch'] = $data['protocols']['diaspora']['receive'];
678                                 } elseif (is_string($data['protocols']['diaspora'])) {
679                                         $fields['batch'] = $data['protocols']['diaspora'];
680                                 }
681                         }
682
683                         if (isset($data['protocols']['dfrn'])) {
684                                 $fields['network'] = Protocol::DFRN;
685
686                                 if (isset($data['protocols']['dfrn']['receive'])) {
687                                         $fields['batch'] = $data['protocols']['dfrn']['receive'];
688                                 } elseif (is_string($data['protocols']['dfrn'])) {
689                                         $fields['batch'] = $data['protocols']['dfrn'];
690                                 }
691                         }
692
693                         if (isset($data['protocols']['activitypub'])) {
694                                 $fields['network'] = Protocol::ACTIVITYPUB;
695
696                                 if (!empty($data['protocols']['activitypub']['actor'])) {
697                                         $fields['url'] = $data['protocols']['activitypub']['actor'];
698                                 }
699                                 if (!empty($data['protocols']['activitypub']['receive'])) {
700                                         $fields['batch'] = $data['protocols']['activitypub']['receive'];
701                                 }
702                         }
703                 }
704
705                 Logger::info('Discovery ended', ['server' => $server_url, 'data' => $fields]);
706
707                 Relay::updateContact($gserver, $fields);
708         }
709
710         /**
711          * Fetch server data from '/statistics.json' on the given server
712          *
713          * @param string $url URL of the given server
714          * @return array server data
715          */
716         private static function fetchStatistics(string $url, array $serverdata)
717         {
718                 $curlResult = DI::httpClient()->get($url . '/statistics.json', HttpClientAccept::JSON);
719                 if (!$curlResult->isSuccess()) {
720                         return $serverdata;
721                 }
722
723                 $data = json_decode($curlResult->getBody(), true);
724                 if (empty($data)) {
725                         return $serverdata;
726                 }
727
728                 // Some AP enabled systems return activity data that we don't expect here.
729                 if (strpos($curlResult->getContentType(), 'application/activity+json') !== false) {
730                         return $serverdata;
731                 }
732
733                 $valid = false;
734                 $old_serverdata = $serverdata;
735
736                 $serverdata['detection-method'] = self::DETECT_STATISTICS_JSON;
737
738                 if (!empty($data['version'])) {
739                         $valid = true;
740                         $serverdata['version'] = $data['version'];
741                         // Version numbers on statistics.json are presented with additional info, e.g.:
742                         // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
743                         $serverdata['version'] = preg_replace('=(.+)-(.{4,})=ism', '$1', $serverdata['version']);
744                 }
745
746                 if (!empty($data['name'])) {
747                         $valid = true;
748                         $serverdata['site_name'] = $data['name'];
749                 }
750
751                 if (!empty($data['network'])) {
752                         $valid = true;
753                         $serverdata['platform'] = strtolower($data['network']);
754
755                         if ($serverdata['platform'] == 'diaspora') {
756                                 $serverdata['network'] = Protocol::DIASPORA;
757                         } elseif ($serverdata['platform'] == 'friendica') {
758                                 $serverdata['network'] = Protocol::DFRN;
759                         } elseif ($serverdata['platform'] == 'hubzilla') {
760                                 $serverdata['network'] = Protocol::ZOT;
761                         } elseif ($serverdata['platform'] == 'redmatrix') {
762                                 $serverdata['network'] = Protocol::ZOT;
763                         }
764                 }
765
766                 if (!empty($data['total_users'])) {
767                         $valid = true;
768                         $serverdata['registered-users'] = max($data['total_users'], 1);
769                 }
770
771                 if (!empty($data['active_users_monthly'])) {
772                         $valid = true;
773                         $serverdata['active-month-users'] = max($data['active_users_monthly'], 0);
774                 }
775
776                 if (!empty($data['active_users_halfyear'])) {
777                         $valid = true;
778                         $serverdata['active-halfyear-users'] = max($data['active_users_halfyear'], 0);
779                 }
780
781                 if (!empty($data['local_posts'])) {
782                         $valid = true;
783                         $serverdata['local-posts'] = max($data['local_posts'], 0);
784                 }
785
786                 if (!empty($data['registrations_open'])) {
787                         $serverdata['register_policy'] = Register::OPEN;
788                 } else {
789                         $serverdata['register_policy'] = Register::CLOSED;
790                 }
791
792                 if (!$valid) {
793                         return $old_serverdata;
794                 }
795
796                 return $serverdata;
797         }
798
799         /**
800          * Detect server type by using the nodeinfo data
801          *
802          * @param string                  $url        address of the server
803          * @param ICanHandleHttpResponses $httpResult
804          *
805          * @return array Server data
806          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
807          */
808         private static function fetchNodeinfo(string $url, ICanHandleHttpResponses $httpResult): array
809         {
810                 if (!$httpResult->isSuccess()) {
811                         return [];
812                 }
813
814                 $nodeinfo = json_decode($httpResult->getBody(), true);
815
816                 if (!is_array($nodeinfo) || empty($nodeinfo['links'])) {
817                         return [];
818                 }
819
820                 $nodeinfo1_url = '';
821                 $nodeinfo2_url = '';
822
823                 foreach ($nodeinfo['links'] as $link) {
824                         if (!is_array($link) || empty($link['rel']) || empty($link['href'])) {
825                                 Logger::info('Invalid nodeinfo format', ['url' => $url]);
826                                 continue;
827                         }
828                         if ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/1.0') {
829                                 $nodeinfo1_url = $link['href'];
830                         } elseif ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/2.0') {
831                                 $nodeinfo2_url = $link['href'];
832                         }
833                 }
834
835                 if ($nodeinfo1_url . $nodeinfo2_url == '') {
836                         return [];
837                 }
838
839                 $server = [];
840
841                 if (!empty($nodeinfo2_url)) {
842                         $server = self::parseNodeinfo2($nodeinfo2_url);
843                 }
844
845                 if (empty($server) && !empty($nodeinfo1_url)) {
846                         $server = self::parseNodeinfo1($nodeinfo1_url);
847                 }
848
849                 return $server;
850         }
851
852         /**
853          * Parses Nodeinfo 1
854          *
855          * @param string $nodeinfo_url address of the nodeinfo path
856          * @return array Server data
857          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
858          */
859         private static function parseNodeinfo1(string $nodeinfo_url): array
860         {
861                 $curlResult = DI::httpClient()->get($nodeinfo_url, HttpClientAccept::JSON);
862                 if (!$curlResult->isSuccess()) {
863                         return [];
864                 }
865
866                 $nodeinfo = json_decode($curlResult->getBody(), true);
867
868                 if (!is_array($nodeinfo)) {
869                         return [];
870                 }
871
872                 $server = ['detection-method' => self::DETECT_NODEINFO_1,
873                         'register_policy' => Register::CLOSED];
874
875                 if (!empty($nodeinfo['openRegistrations'])) {
876                         $server['register_policy'] = Register::OPEN;
877                 }
878
879                 if (is_array($nodeinfo['software'])) {
880                         if (!empty($nodeinfo['software']['name'])) {
881                                 $server['platform'] = strtolower($nodeinfo['software']['name']);
882                         }
883
884                         if (!empty($nodeinfo['software']['version'])) {
885                                 $server['version'] = $nodeinfo['software']['version'];
886                                 // Version numbers on Nodeinfo are presented with additional info, e.g.:
887                                 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
888                                 $server['version'] = preg_replace('=(.+)-(.{4,})=ism', '$1', $server['version']);
889                         }
890                 }
891
892                 if (!empty($nodeinfo['metadata']['nodeName'])) {
893                         $server['site_name'] = $nodeinfo['metadata']['nodeName'];
894                 }
895
896                 if (!empty($nodeinfo['usage']['users']['total'])) {
897                         $server['registered-users'] = max($nodeinfo['usage']['users']['total'], 1);
898                 }
899
900                 if (!empty($nodeinfo['usage']['users']['activeMonth'])) {
901                         $server['active-month-users'] = max($nodeinfo['usage']['users']['activeMonth'], 0);
902                 }
903
904                 if (!empty($nodeinfo['usage']['users']['activeHalfyear'])) {
905                         $server['active-halfyear-users'] = max($nodeinfo['usage']['users']['activeHalfyear'], 0);
906                 }
907
908                 if (!empty($nodeinfo['usage']['localPosts'])) {
909                         $server['local-posts'] = max($nodeinfo['usage']['localPosts'], 0);
910                 }
911
912                 if (!empty($nodeinfo['usage']['localComments'])) {
913                         $server['local-comments'] = max($nodeinfo['usage']['localComments'], 0);
914                 }
915
916                 if (!empty($nodeinfo['protocols']['inbound']) && is_array($nodeinfo['protocols']['inbound'])) {
917                         $protocols = [];
918                         foreach ($nodeinfo['protocols']['inbound'] as $protocol) {
919                                 $protocols[$protocol] = true;
920                         }
921
922                         if (!empty($protocols['friendica'])) {
923                                 $server['network'] = Protocol::DFRN;
924                         } elseif (!empty($protocols['activitypub'])) {
925                                 $server['network'] = Protocol::ACTIVITYPUB;
926                         } elseif (!empty($protocols['diaspora'])) {
927                                 $server['network'] = Protocol::DIASPORA;
928                         } elseif (!empty($protocols['ostatus'])) {
929                                 $server['network'] = Protocol::OSTATUS;
930                         } elseif (!empty($protocols['gnusocial'])) {
931                                 $server['network'] = Protocol::OSTATUS;
932                         } elseif (!empty($protocols['zot'])) {
933                                 $server['network'] = Protocol::ZOT;
934                         }
935                 }
936
937                 if (empty($server)) {
938                         return [];
939                 }
940
941                 if (empty($server['network'])) {
942                         $server['network'] = Protocol::PHANTOM;
943                 }
944
945                 return $server;
946         }
947
948         /**
949          * Parses Nodeinfo 2
950          *
951          * @see https://git.feneas.org/jaywink/nodeinfo2
952          * @param string $nodeinfo_url address of the nodeinfo path
953          * @return array Server data
954          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
955          */
956         private static function parseNodeinfo2(string $nodeinfo_url): array
957         {
958                 $curlResult = DI::httpClient()->get($nodeinfo_url, HttpClientAccept::JSON);
959                 if (!$curlResult->isSuccess()) {
960                         return [];
961                 }
962
963                 $nodeinfo = json_decode($curlResult->getBody(), true);
964                 if (!is_array($nodeinfo)) {
965                         return [];
966                 }
967
968                 $server = [
969                         'detection-method' => self::DETECT_NODEINFO_2,
970                         'register_policy' => Register::CLOSED,
971                         'platform' => 'unknown',
972                 ];
973
974                 if (!empty($nodeinfo['openRegistrations'])) {
975                         $server['register_policy'] = Register::OPEN;
976                 }
977
978                 if (!empty($nodeinfo['software'])) {
979                         if (isset($nodeinfo['software']['name'])) {
980                                 $server['platform'] = strtolower($nodeinfo['software']['name']);
981                         }
982
983                         if (!empty($nodeinfo['software']['version']) && isset($server['platform'])) {
984                                 $server['version'] = $nodeinfo['software']['version'];
985                                 // Version numbers on Nodeinfo are presented with additional info, e.g.:
986                                 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
987                                 $server['version'] = preg_replace('=(.+)-(.{4,})=ism', '$1', $server['version']);
988
989                                 // qoto advertises itself as Mastodon
990                                 if (($server['platform'] == 'mastodon') && substr($nodeinfo['software']['version'], -5) == '-qoto') {
991                                         $server['platform'] = 'qoto';
992                                 }
993                         }
994                 }
995
996                 if (!empty($nodeinfo['metadata']['nodeName'])) {
997                         $server['site_name'] = $nodeinfo['metadata']['nodeName'];
998                 }
999
1000                 if (!empty($nodeinfo['usage']['users']['total'])) {
1001                         $server['registered-users'] = max($nodeinfo['usage']['users']['total'], 1);
1002                 }
1003
1004                 if (!empty($nodeinfo['usage']['users']['activeMonth'])) {
1005                         $server['active-month-users'] = max($nodeinfo['usage']['users']['activeMonth'], 0);
1006                 }
1007
1008                 if (!empty($nodeinfo['usage']['users']['activeHalfyear'])) {
1009                         $server['active-halfyear-users'] = max($nodeinfo['usage']['users']['activeHalfyear'], 0);
1010                 }
1011
1012                 if (!empty($nodeinfo['usage']['localPosts'])) {
1013                         $server['local-posts'] = max($nodeinfo['usage']['localPosts'], 0);
1014                 }
1015
1016                 if (!empty($nodeinfo['usage']['localComments'])) {
1017                         $server['local-comments'] = max($nodeinfo['usage']['localComments'], 0);
1018                 }
1019
1020                 if (!empty($nodeinfo['protocols'])) {
1021                         $protocols = [];
1022                         foreach ($nodeinfo['protocols'] as $protocol) {
1023                                 if (is_string($protocol)) {
1024                                         $protocols[$protocol] = true;
1025                                 }
1026                         }
1027
1028                         if (!empty($protocols['dfrn'])) {
1029                                 $server['network'] = Protocol::DFRN;
1030                         } elseif (!empty($protocols['activitypub'])) {
1031                                 $server['network'] = Protocol::ACTIVITYPUB;
1032                         } elseif (!empty($protocols['diaspora'])) {
1033                                 $server['network'] = Protocol::DIASPORA;
1034                         } elseif (!empty($protocols['ostatus'])) {
1035                                 $server['network'] = Protocol::OSTATUS;
1036                         } elseif (!empty($protocols['gnusocial'])) {
1037                                 $server['network'] = Protocol::OSTATUS;
1038                         } elseif (!empty($protocols['zot'])) {
1039                                 $server['network'] = Protocol::ZOT;
1040                         }
1041                 }
1042
1043                 if (empty($server)) {
1044                         return [];
1045                 }
1046
1047                 if (empty($server['network'])) {
1048                         $server['network'] = Protocol::PHANTOM;
1049                 }
1050
1051                 return $server;
1052         }
1053
1054         /**
1055          * Parses NodeInfo2 protocol 1.0
1056          *
1057          * @see https://github.com/jaywink/nodeinfo2/blob/master/PROTOCOL.md
1058          * @param string $nodeinfo_url address of the nodeinfo path
1059          * @return array Server data
1060          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1061          */
1062         private static function parseNodeinfo210(ICanHandleHttpResponses $httpResult)
1063         {
1064                 if (!$httpResult->isSuccess()) {
1065                         return [];
1066                 }
1067
1068                 $nodeinfo = json_decode($httpResult->getBody(), true);
1069
1070                 if (!is_array($nodeinfo)) {
1071                         return [];
1072                 }
1073
1074                 $server = ['detection-method' => self::DETECT_NODEINFO_210,
1075                         'register_policy' => Register::CLOSED];
1076
1077                 if (!empty($nodeinfo['openRegistrations'])) {
1078                         $server['register_policy'] = Register::OPEN;
1079                 }
1080
1081                 if (!empty($nodeinfo['server'])) {
1082                         if (!empty($nodeinfo['server']['software'])) {
1083                                 $server['platform'] = strtolower($nodeinfo['server']['software']);
1084                         }
1085
1086                         if (!empty($nodeinfo['server']['version'])) {
1087                                 $server['version'] = $nodeinfo['server']['version'];
1088                                 // Version numbers on Nodeinfo are presented with additional info, e.g.:
1089                                 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
1090                                 $server['version'] = preg_replace('=(.+)-(.{4,})=ism', '$1', $server['version']);
1091                         }
1092
1093                         if (!empty($nodeinfo['server']['name'])) {
1094                                 $server['site_name'] = $nodeinfo['server']['name'];
1095                         }
1096                 }
1097
1098                 if (!empty($nodeinfo['usage']['users']['total'])) {
1099                         $server['registered-users'] = max($nodeinfo['usage']['users']['total'], 1);
1100                 }
1101
1102                 if (!empty($nodeinfo['usage']['users']['activeMonth'])) {
1103                         $server['active-month-users'] = max($nodeinfo['usage']['users']['activeMonth'], 0);
1104                 }
1105
1106                 if (!empty($nodeinfo['usage']['users']['activeHalfyear'])) {
1107                         $server['active-halfyear-users'] = max($nodeinfo['usage']['users']['activeHalfyear'], 0);
1108                 }
1109
1110                 if (!empty($nodeinfo['usage']['localPosts'])) {
1111                         $server['local-posts'] = max($nodeinfo['usage']['localPosts'], 0);
1112                 }
1113
1114                 if (!empty($nodeinfo['usage']['localComments'])) {
1115                         $server['local-comments'] = max($nodeinfo['usage']['localComments'], 0);
1116                 }
1117
1118                 if (!empty($nodeinfo['protocols'])) {
1119                         $protocols = [];
1120                         foreach ($nodeinfo['protocols'] as $protocol) {
1121                                 if (is_string($protocol)) {
1122                                         $protocols[$protocol] = true;
1123                                 }
1124                         }
1125
1126                         if (!empty($protocols['dfrn'])) {
1127                                 $server['network'] = Protocol::DFRN;
1128                         } elseif (!empty($protocols['activitypub'])) {
1129                                 $server['network'] = Protocol::ACTIVITYPUB;
1130                         } elseif (!empty($protocols['diaspora'])) {
1131                                 $server['network'] = Protocol::DIASPORA;
1132                         } elseif (!empty($protocols['ostatus'])) {
1133                                 $server['network'] = Protocol::OSTATUS;
1134                         } elseif (!empty($protocols['gnusocial'])) {
1135                                 $server['network'] = Protocol::OSTATUS;
1136                         } elseif (!empty($protocols['zot'])) {
1137                                 $server['network'] = Protocol::ZOT;
1138                         }
1139                 }
1140
1141                 if (empty($server) || empty($server['platform'])) {
1142                         return [];
1143                 }
1144
1145                 if (empty($server['network'])) {
1146                         $server['network'] = Protocol::PHANTOM;
1147                 }
1148
1149                 return $server;
1150         }
1151
1152         /**
1153          * Fetch server information from a 'siteinfo.json' file on the given server
1154          *
1155          * @param string $url        URL of the given server
1156          * @param array  $serverdata array with server data
1157          * @return array server data
1158          */
1159         private static function fetchSiteinfo(string $url, array $serverdata): array
1160         {
1161                 $curlResult = DI::httpClient()->get($url . '/siteinfo.json', HttpClientAccept::JSON);
1162                 if (!$curlResult->isSuccess()) {
1163                         return $serverdata;
1164                 }
1165
1166                 $data = json_decode($curlResult->getBody(), true);
1167                 if (empty($data)) {
1168                         return $serverdata;
1169                 }
1170
1171                 if (in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
1172                         $serverdata['detection-method'] = self::DETECT_SITEINFO_JSON;
1173                 }
1174
1175                 if (!empty($data['url'])) {
1176                         $serverdata['platform'] = strtolower($data['platform']);
1177                         $serverdata['version'] = $data['version'];
1178                 }
1179
1180                 if (!empty($data['plugins'])) {
1181                         if (in_array('pubcrawl', $data['plugins'])) {
1182                                 $serverdata['network'] = Protocol::ACTIVITYPUB;
1183                         } elseif (in_array('diaspora', $data['plugins'])) {
1184                                 $serverdata['network'] = Protocol::DIASPORA;
1185                         } elseif (in_array('gnusoc', $data['plugins'])) {
1186                                 $serverdata['network'] = Protocol::OSTATUS;
1187                         } else {
1188                                 $serverdata['network'] = Protocol::ZOT;
1189                         }
1190                 }
1191
1192                 if (!empty($data['site_name'])) {
1193                         $serverdata['site_name'] = $data['site_name'];
1194                 }
1195
1196                 if (!empty($data['channels_total'])) {
1197                         $serverdata['registered-users'] = max($data['channels_total'], 1);
1198                 }
1199
1200                 if (!empty($data['channels_active_monthly'])) {
1201                         $serverdata['active-month-users'] = max($data['channels_active_monthly'], 0);
1202                 }
1203
1204                 if (!empty($data['channels_active_halfyear'])) {
1205                         $serverdata['active-halfyear-users'] = max($data['channels_active_halfyear'], 0);
1206                 }
1207
1208                 if (!empty($data['local_posts'])) {
1209                         $serverdata['local-posts'] = max($data['local_posts'], 0);
1210                 }
1211
1212                 if (!empty($data['local_comments'])) {
1213                         $serverdata['local-comments'] = max($data['local_comments'], 0);
1214                 }
1215
1216                 if (!empty($data['register_policy'])) {
1217                         switch ($data['register_policy']) {
1218                                 case 'REGISTER_OPEN':
1219                                         $serverdata['register_policy'] = Register::OPEN;
1220                                         break;
1221
1222                                 case 'REGISTER_APPROVE':
1223                                         $serverdata['register_policy'] = Register::APPROVE;
1224                                         break;
1225
1226                                 case 'REGISTER_CLOSED':
1227                                 default:
1228                                         $serverdata['register_policy'] = Register::CLOSED;
1229                                         break;
1230                         }
1231                 }
1232
1233                 return $serverdata;
1234         }
1235
1236         private static function fetchDataFromSystemActor(array $data, array $serverdata)
1237         {
1238                 if (empty($data)) {
1239                         return ['server' => $serverdata, 'actor' => ''];
1240                 }
1241
1242                 $actor = JsonLD::compact($data, false);
1243                 if (in_array(JsonLD::fetchElement($actor, '@type'), ActivityPub\Receiver::ACCOUNT_TYPES)) {
1244                         $serverdata['network'] = Protocol::ACTIVITYPUB;
1245                         $serverdata['site_name'] = JsonLD::fetchElement($actor, 'as:name', '@value');
1246                         $serverdata['info'] = JsonLD::fetchElement($actor, 'as:summary', '@value');
1247                         if (!empty($actor['as:generator'])) {
1248                                 $generator = explode(' ', JsonLD::fetchElement($actor['as:generator'], 'as:name', '@value'));
1249                                 $serverdata['platform'] = strtolower(array_shift($generator));
1250                                 $serverdata['detection-method'] = self::DETECT_SYSTEM_ACTOR;
1251                         } else {
1252                                 $serverdata['detection-method'] = self::DETECT_AP_ACTOR;
1253                         }
1254                         return ['server' => $serverdata, 'actor' => $actor['@id']];
1255                 } elseif ((JsonLD::fetchElement($actor, '@type') == 'as:Collection')) {
1256                         // By now only Ktistec seems to provide collections this way
1257                         $serverdata['platform'] = 'ktistec';
1258                         $serverdata['network'] = Protocol::ACTIVITYPUB;
1259                         $serverdata['detection-method'] = self::DETECT_AP_COLLECTION;
1260
1261                         $actors = JsonLD::fetchElementArray($actor, 'as:items');
1262                         if (!empty($actors) && !empty($actors[0]['@id'])) {
1263                                 $actor_url = $actor['@id'] . $actors[0]['@id'];
1264                         } else {
1265                                 $actor_url = '';
1266                         }
1267
1268                         return ['server' => $serverdata, 'actor' => $actor_url];
1269                 }
1270                 return ['server' => $serverdata, 'actor' => ''];
1271         }
1272
1273         /**
1274          * Checks if the server contains a valid host meta file
1275          *
1276          * @param string $url URL of the given server
1277          * @return boolean 'true' if the server seems to be vital
1278          */
1279         private static function validHostMeta(string $url): bool
1280         {
1281                 $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
1282                 $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta', HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
1283                 if (!$curlResult->isSuccess()) {
1284                         return false;
1285                 }
1286
1287                 $xrd = XML::parseString($curlResult->getBody());
1288                 if (!is_object($xrd)) {
1289                         return false;
1290                 }
1291
1292                 $elements = XML::elementToArray($xrd);
1293                 if (empty($elements) || empty($elements['xrd']) || empty($elements['xrd']['link'])) {
1294                         return false;
1295                 }
1296
1297                 $valid = false;
1298                 foreach ($elements['xrd']['link'] as $link) {
1299                         // When there is more than a single "link" element, the array looks slightly different
1300                         if (!empty($link['@attributes'])) {
1301                                 $link = $link['@attributes'];
1302                         }
1303
1304                         if (empty($link['rel']) || empty($link['template'])) {
1305                                 continue;
1306                         }
1307
1308                         if ($link['rel'] == 'lrdd') {
1309                                 // When the webfinger host is the same like the system host, it should be ok.
1310                                 $valid = (parse_url($url, PHP_URL_HOST) == parse_url($link['template'], PHP_URL_HOST));
1311                         }
1312                 }
1313
1314                 return $valid;
1315         }
1316
1317         /**
1318          * Detect the network of the given server via their known contacts
1319          *
1320          * @param string $url        URL of the given server
1321          * @param array  $serverdata array with server data
1322          * @return array server data
1323          */
1324         private static function detectNetworkViaContacts(string $url, array $serverdata): array
1325         {
1326                 $contacts = [];
1327
1328                 $nurl = Strings::normaliseLink($url);
1329
1330                 $apcontacts = DBA::select('apcontact', ['url'], ['baseurl' => [$url, $nurl]]);
1331                 while ($apcontact = DBA::fetch($apcontacts)) {
1332                         $contacts[Strings::normaliseLink($apcontact['url'])] = $apcontact['url'];
1333                 }
1334                 DBA::close($apcontacts);
1335
1336                 $pcontacts = DBA::select('contact', ['url', 'nurl'], ['uid' => 0, 'baseurl' => [$url, $nurl]]);
1337                 while ($pcontact = DBA::fetch($pcontacts)) {
1338                         $contacts[$pcontact['nurl']] = $pcontact['url'];
1339                 }
1340                 DBA::close($pcontacts);
1341
1342                 if (empty($contacts)) {
1343                         return $serverdata;
1344                 }
1345
1346                 $time = time();
1347                 foreach ($contacts as $contact) {
1348                         // Endlosschleife verhindern wegen gsid!
1349                         $data = Probe::uri($contact);
1350                         if (in_array($data['network'], Protocol::FEDERATED)) {
1351                                 $serverdata['network'] = $data['network'];
1352
1353                                 if (in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
1354                                         $serverdata['detection-method'] = self::DETECT_CONTACTS;
1355                                 }
1356                                 break;
1357                         } elseif ((time() - $time) > 10) {
1358                                 // To reduce the stress on remote systems we probe a maximum of 10 seconds
1359                                 break;
1360                         }
1361                 }
1362
1363                 return $serverdata;
1364         }
1365
1366         /**
1367          * Checks if the given server does have a '/poco' endpoint.
1368          * This is used for the 'PortableContact' functionality,
1369          * which is used by both Friendica and Hubzilla.
1370          *
1371          * @param string $url        URL of the given server
1372          * @param array  $serverdata array with server data
1373          * @return array server data
1374          */
1375         private static function checkPoCo(string $url, array $serverdata): array
1376         {
1377                 $serverdata['poco'] = '';
1378
1379                 $curlResult = DI::httpClient()->get($url . '/poco', HttpClientAccept::JSON);
1380                 if (!$curlResult->isSuccess()) {
1381                         return $serverdata;
1382                 }
1383
1384                 $data = json_decode($curlResult->getBody(), true);
1385                 if (empty($data)) {
1386                         return $serverdata;
1387                 }
1388
1389                 if (!empty($data['totalResults'])) {
1390                         $registeredUsers = $serverdata['registered-users'] ?? 0;
1391                         $serverdata['registered-users'] = max($data['totalResults'], $registeredUsers, 1);
1392                         $serverdata['directory-type'] = self::DT_POCO;
1393                         $serverdata['poco'] = $url . '/poco';
1394                 }
1395
1396                 return $serverdata;
1397         }
1398
1399         /**
1400          * Checks if the given server does have a Mastodon style directory endpoint.
1401          *
1402          * @param string $url        URL of the given server
1403          * @param array  $serverdata array with server data
1404          * @return array server data
1405          */
1406         public static function checkMastodonDirectory(string $url, array $serverdata): array
1407         {
1408                 $curlResult = DI::httpClient()->get($url . '/api/v1/directory?limit=1', HttpClientAccept::JSON);
1409                 if (!$curlResult->isSuccess()) {
1410                         return $serverdata;
1411                 }
1412
1413                 $data = json_decode($curlResult->getBody(), true);
1414                 if (empty($data)) {
1415                         return $serverdata;
1416                 }
1417
1418                 if (count($data) == 1) {
1419                         $serverdata['directory-type'] = self::DT_MASTODON;
1420                 }
1421
1422                 return $serverdata;
1423         }
1424
1425         /**
1426          * Detects Peertube via their known endpoint
1427          *
1428          * @param string $url        URL of the given server
1429          * @param array  $serverdata array with server data
1430          *
1431          * @return array server data
1432          */
1433         private static function detectPeertube(string $url, array $serverdata): array
1434         {
1435                 $curlResult = DI::httpClient()->get($url . '/api/v1/config', HttpClientAccept::JSON);
1436                 if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
1437                         return $serverdata;
1438                 }
1439
1440                 $data = json_decode($curlResult->getBody(), true);
1441                 if (empty($data)) {
1442                         return $serverdata;
1443                 }
1444
1445                 if (!empty($data['instance']) && !empty($data['serverVersion'])) {
1446                         $serverdata['platform'] = 'peertube';
1447                         $serverdata['version'] = $data['serverVersion'];
1448                         $serverdata['network'] = Protocol::ACTIVITYPUB;
1449
1450                         if (!empty($data['instance']['name'])) {
1451                                 $serverdata['site_name'] = $data['instance']['name'];
1452                         }
1453
1454                         if (!empty($data['instance']['shortDescription'])) {
1455                                 $serverdata['info'] = $data['instance']['shortDescription'];
1456                         }
1457
1458                         if (!empty($data['signup'])) {
1459                                 if (!empty($data['signup']['allowed'])) {
1460                                         $serverdata['register_policy'] = Register::OPEN;
1461                                 }
1462                         }
1463
1464                         if (in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
1465                                 $serverdata['detection-method'] = self::DETECT_V1_CONFIG;
1466                         }
1467                 }
1468
1469                 return $serverdata;
1470         }
1471
1472         /**
1473          * Detects the version number of a given server when it was a NextCloud installation
1474          *
1475          * @param string $url        URL of the given server
1476          * @param array  $serverdata array with server data
1477          * @param bool   $validHostMeta
1478          *
1479          * @return array server data
1480          */
1481         private static function detectNextcloud(string $url, array $serverdata, bool $validHostMeta)
1482         {
1483                 $curlResult = DI::httpClient()->get($url . '/status.php', HttpClientAccept::JSON);
1484                 if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
1485                         return $serverdata;
1486                 }
1487
1488                 $data = json_decode($curlResult->getBody(), true);
1489                 if (empty($data)) {
1490                         return $serverdata;
1491                 }
1492
1493                 if (!empty($data['version'])) {
1494                         $serverdata['platform'] = 'nextcloud';
1495                         $serverdata['version'] = $data['version'];
1496
1497                         if ($validHostMeta) {
1498                                 $serverdata['network'] = Protocol::ACTIVITYPUB;
1499                         }
1500
1501                         if (in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
1502                                 $serverdata['detection-method'] = self::DETECT_STATUS_PHP;
1503                         }
1504                 }
1505
1506                 return $serverdata;
1507         }
1508
1509         /**
1510          * Fetches weekly usage data
1511          *
1512          * @param string $url        URL of the given server
1513          * @param array  $serverdata array with server data
1514          * @return array server data
1515          */
1516         private static function fetchWeeklyUsage(string $url, array $serverdata): array
1517         {
1518                 $curlResult = DI::httpClient()->get($url . '/api/v1/instance/activity', HttpClientAccept::JSON);
1519                 if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
1520                         return $serverdata;
1521                 }
1522
1523                 $data = json_decode($curlResult->getBody(), true);
1524                 if (empty($data)) {
1525                         return $serverdata;
1526                 }
1527
1528                 $current_week = [];
1529                 foreach ($data as $week) {
1530                         // Use only data from a full week
1531                         if (empty($week['week']) || (time() - $week['week']) < 7 * 24 * 60 * 60) {
1532                                 continue;
1533                         }
1534
1535                         // Most likely the data is sorted correctly. But we better are safe than sorry
1536                         if (empty($current_week['week']) || ($current_week['week'] < $week['week'])) {
1537                                 $current_week = $week;
1538                         }
1539                 }
1540
1541                 if (!empty($current_week['logins'])) {
1542                         $serverdata['active-week-users'] = max($current_week['logins'], 0);
1543                 }
1544
1545                 return $serverdata;
1546         }
1547
1548         /**
1549          * Detects data from a given server url if it was a mastodon alike system
1550          *
1551          * @param string $url        URL of the given server
1552          * @param array  $serverdata array with server data
1553          * @return array server data
1554          */
1555         private static function detectMastodonAlikes(string $url, array $serverdata): array
1556         {
1557                 $curlResult = DI::httpClient()->get($url . '/api/v1/instance', HttpClientAccept::JSON);
1558                 if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
1559                         return $serverdata;
1560                 }
1561
1562                 $data = json_decode($curlResult->getBody(), true);
1563                 if (empty($data)) {
1564                         return $serverdata;
1565                 }
1566
1567                 $valid = false;
1568
1569                 if (!empty($data['version'])) {
1570                         $serverdata['platform'] = 'mastodon';
1571                         $serverdata['version'] = $data['version'] ?? '';
1572                         $serverdata['network'] = Protocol::ACTIVITYPUB;
1573                         $valid = true;
1574                 }
1575
1576                 if (!empty($data['title'])) {
1577                         $serverdata['site_name'] = $data['title'];
1578                 }
1579
1580                 if (!empty($data['title']) && empty($serverdata['platform']) && ($serverdata['network'] == Protocol::PHANTOM)) {
1581                         $serverdata['platform'] = 'mastodon';
1582                         $serverdata['network'] = Protocol::ACTIVITYPUB;
1583                         $valid = true;
1584                 }
1585
1586                 if (!empty($data['description'])) {
1587                         $serverdata['info'] = trim($data['description']);
1588                 }
1589
1590                 if (!empty($data['stats']['user_count'])) {
1591                         $serverdata['registered-users'] = max($data['stats']['user_count'], 1);
1592                 }
1593
1594                 if (!empty($serverdata['version']) && preg_match('/.*?\(compatible;\s(.*)\s(.*)\)/ism', $serverdata['version'], $matches)) {
1595                         $serverdata['platform'] = strtolower($matches[1]);
1596                         $serverdata['version'] = $matches[2];
1597                         $valid = true;
1598                 }
1599
1600                 if (!empty($serverdata['version']) && strstr(strtolower($serverdata['version']), 'pleroma')) {
1601                         $serverdata['platform'] = 'pleroma';
1602                         $serverdata['version'] = trim(str_ireplace('pleroma', '', $serverdata['version']));
1603                         $valid = true;
1604                 }
1605
1606                 if (!empty($serverdata['platform']) && strstr($serverdata['platform'], 'pleroma')) {
1607                         $serverdata['version'] = trim(str_ireplace('pleroma', '', $serverdata['platform']));
1608                         $serverdata['platform'] = 'pleroma';
1609                         $valid = true;
1610                 }
1611
1612                 if ($valid && in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
1613                         $serverdata['detection-method'] = self::DETECT_MASTODON_API;
1614                 }
1615
1616                 return $serverdata;
1617         }
1618
1619         /**
1620          * Detects data from typical Hubzilla endpoints
1621          *
1622          * @param string $url        URL of the given server
1623          * @param array  $serverdata array with server data
1624          * @return array server data
1625          */
1626         private static function detectHubzilla(string $url, array $serverdata): array
1627         {
1628                 $curlResult = DI::httpClient()->get($url . '/api/statusnet/config.json', HttpClientAccept::JSON);
1629                 if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
1630                         return $serverdata;
1631                 }
1632
1633                 $data = json_decode($curlResult->getBody(), true);
1634                 if (empty($data) || empty($data['site'])) {
1635                         return $serverdata;
1636                 }
1637
1638                 if (!empty($data['site']['name'])) {
1639                         $serverdata['site_name'] = $data['site']['name'];
1640                 }
1641
1642                 if (!empty($data['site']['platform'])) {
1643                         $serverdata['platform'] = strtolower($data['site']['platform']['PLATFORM_NAME']);
1644                         $serverdata['version'] = $data['site']['platform']['STD_VERSION'];
1645                         $serverdata['network'] = Protocol::ZOT;
1646                 }
1647
1648                 if (!empty($data['site']['hubzilla'])) {
1649                         $serverdata['platform'] = strtolower($data['site']['hubzilla']['PLATFORM_NAME']);
1650                         $serverdata['version'] = $data['site']['hubzilla']['RED_VERSION'];
1651                         $serverdata['network'] = Protocol::ZOT;
1652                 }
1653
1654                 if (!empty($data['site']['redmatrix'])) {
1655                         if (!empty($data['site']['redmatrix']['PLATFORM_NAME'])) {
1656                                 $serverdata['platform'] = strtolower($data['site']['redmatrix']['PLATFORM_NAME']);
1657                         } elseif (!empty($data['site']['redmatrix']['RED_PLATFORM'])) {
1658                                 $serverdata['platform'] = strtolower($data['site']['redmatrix']['RED_PLATFORM']);
1659                         }
1660
1661                         $serverdata['version'] = $data['site']['redmatrix']['RED_VERSION'];
1662                         $serverdata['network'] = Protocol::ZOT;
1663                 }
1664
1665                 $private = false;
1666                 $inviteonly = false;
1667                 $closed = false;
1668
1669                 if (!empty($data['site']['closed'])) {
1670                         $closed = self::toBoolean($data['site']['closed']);
1671                 }
1672
1673                 if (!empty($data['site']['private'])) {
1674                         $private = self::toBoolean($data['site']['private']);
1675                 }
1676
1677                 if (!empty($data['site']['inviteonly'])) {
1678                         $inviteonly = self::toBoolean($data['site']['inviteonly']);
1679                 }
1680
1681                 if (!$closed && !$private and $inviteonly) {
1682                         $serverdata['register_policy'] = Register::APPROVE;
1683                 } elseif (!$closed && !$private) {
1684                         $serverdata['register_policy'] = Register::OPEN;
1685                 } else {
1686                         $serverdata['register_policy'] = Register::CLOSED;
1687                 }
1688
1689                 if (($serverdata['network'] != Protocol::PHANTOM) && in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
1690                         $serverdata['detection-method'] = self::DETECT_CONFIG_JSON;
1691                 }
1692
1693                 return $serverdata;
1694         }
1695
1696         /**
1697          * Converts input value to a boolean value
1698          *
1699          * @param string|integer $val
1700          * @return boolean
1701          */
1702         private static function toBoolean($val): bool
1703         {
1704                 if (($val == 'true') || ($val == 1)) {
1705                         return true;
1706                 } elseif (($val == 'false') || ($val == 0)) {
1707                         return false;
1708                 }
1709
1710                 return $val;
1711         }
1712
1713         /**
1714          * Detect if the URL belongs to a GNU Social server
1715          *
1716          * @param string $url        URL of the given server
1717          * @param array  $serverdata array with server data
1718          * @return array server data
1719          */
1720         private static function detectGNUSocial(string $url, array $serverdata): array
1721         {
1722                 // Test for GNU Social
1723                 $curlResult = DI::httpClient()->get($url . '/api/gnusocial/version.json', HttpClientAccept::JSON);
1724                 if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') &&
1725                         ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) {
1726                         $serverdata['platform'] = 'gnusocial';
1727                         // Remove junk that some GNU Social servers return
1728                         $serverdata['version'] = str_replace(chr(239) . chr(187) . chr(191), '', $curlResult->getBody());
1729                         $serverdata['version'] = str_replace(["\r", "\n", "\t"], '', $serverdata['version']);
1730                         $serverdata['version'] = trim($serverdata['version'], '"');
1731                         $serverdata['network'] = Protocol::OSTATUS;
1732
1733                         if (in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
1734                                 $serverdata['detection-method'] = self::DETECT_GNUSOCIAL;
1735                         }
1736
1737                         return $serverdata;
1738                 }
1739
1740                 // Test for Statusnet
1741                 $curlResult = DI::httpClient()->get($url . '/api/statusnet/version.json', HttpClientAccept::JSON);
1742                 if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') &&
1743                         ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) {
1744
1745                         // Remove junk that some GNU Social servers return
1746                         $serverdata['version'] = str_replace(chr(239).chr(187).chr(191), '', $curlResult->getBody());
1747                         $serverdata['version'] = str_replace(["\r", "\n", "\t"], '', $serverdata['version']);
1748                         $serverdata['version'] = trim($serverdata['version'], '"');
1749
1750                         if (!empty($serverdata['version']) && strtolower(substr($serverdata['version'], 0, 7)) == 'pleroma') {
1751                                 $serverdata['platform'] = 'pleroma';
1752                                 $serverdata['version'] = trim(str_ireplace('pleroma', '', $serverdata['version']));
1753                                 $serverdata['network'] = Protocol::ACTIVITYPUB;
1754                         } else {
1755                                 $serverdata['platform'] = 'statusnet';
1756                                 $serverdata['network'] = Protocol::OSTATUS;
1757                         }
1758
1759                         if (in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
1760                                 $serverdata['detection-method'] = self::DETECT_STATUSNET;
1761                         }
1762                 }
1763
1764                 return $serverdata;
1765         }
1766
1767         /**
1768          * Detect if the URL belongs to a Friendica server
1769          *
1770          * @param string $url        URL of the given server
1771          * @param array  $serverdata array with server data
1772          * @return array server data
1773          */
1774         private static function detectFriendica(string $url, array $serverdata): array
1775         {
1776                 // There is a bug in some versions of Friendica that will return an ActivityStream actor when the content type "application/json" is requested.
1777                 // Because of this me must not use ACCEPT_JSON here.
1778                 $curlResult = DI::httpClient()->get($url . '/friendica/json');
1779                 if (!$curlResult->isSuccess()) {
1780                         $curlResult = DI::httpClient()->get($url . '/friendika/json');
1781                         $friendika = true;
1782                         $platform = 'Friendika';
1783                 } else {
1784                         $friendika = false;
1785                         $platform = 'Friendica';
1786                 }
1787
1788                 if (!$curlResult->isSuccess()) {
1789                         return $serverdata;
1790                 }
1791
1792                 $data = json_decode($curlResult->getBody(), true);
1793                 if (empty($data) || empty($data['version'])) {
1794                         return $serverdata;
1795                 }
1796
1797                 if (in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
1798                         $serverdata['detection-method'] = $friendika ? self::DETECT_FRIENDIKA : self::DETECT_FRIENDICA;
1799                 }
1800
1801                 $serverdata['network'] = Protocol::DFRN;
1802                 $serverdata['version'] = $data['version'];
1803
1804                 if (!empty($data['no_scrape_url'])) {
1805                         $serverdata['noscrape'] = $data['no_scrape_url'];
1806                 }
1807
1808                 if (!empty($data['site_name'])) {
1809                         $serverdata['site_name'] = $data['site_name'];
1810                 }
1811
1812                 if (!empty($data['info'])) {
1813                         $serverdata['info'] = trim($data['info']);
1814                 }
1815
1816                 $register_policy = ($data['register_policy'] ?? '') ?: 'REGISTER_CLOSED';
1817                 switch ($register_policy) {
1818                         case 'REGISTER_OPEN':
1819                                 $serverdata['register_policy'] = Register::OPEN;
1820                                 break;
1821
1822                         case 'REGISTER_APPROVE':
1823                                 $serverdata['register_policy'] = Register::APPROVE;
1824                                 break;
1825
1826                         case 'REGISTER_CLOSED':
1827                         case 'REGISTER_INVITATION':
1828                                 $serverdata['register_policy'] = Register::CLOSED;
1829                                 break;
1830                         default:
1831                                 Logger::info('Register policy is invalid', ['policy' => $register_policy, 'server' => $url]);
1832                                 $serverdata['register_policy'] = Register::CLOSED;
1833                                 break;
1834                 }
1835
1836                 $serverdata['platform'] = strtolower($data['platform'] ?? $platform);
1837
1838                 return $serverdata;
1839         }
1840
1841         /**
1842          * Analyses the landing page of a given server for hints about type and system of that server
1843          *
1844          * @param object $curlResult result of curl execution
1845          * @param array  $serverdata array with server data
1846          *
1847          * @return array server data
1848          */
1849          private static function analyseRootBody($curlResult, array $serverdata): array
1850         {
1851                 if (empty($curlResult->getBody())) {
1852                         return $serverdata;
1853                 }
1854
1855                 if (file_exists(__DIR__ . '/../../static/generator.config.php')) {
1856                         require __DIR__ . '/../../static/generator.config.php';
1857                 } else {
1858                         throw new HTTPException\InternalServerErrorException('Invalid generator file');
1859                 }
1860
1861                 $platforms = array_merge($ap_platforms, $dfrn_platforms, $zap_platforms, $platforms);
1862
1863                 $doc = new DOMDocument();
1864                 @$doc->loadHTML($curlResult->getBody());
1865                 $xpath = new DOMXPath($doc);
1866                 $assigned = false;
1867
1868                 // We can only detect honk via some HTML element on their page
1869                 if ($xpath->query('//div[@id="honksonpage"]')->count() == 1) {
1870                         $serverdata['platform'] = 'honk';
1871                         $serverdata['network'] = Protocol::ACTIVITYPUB;
1872                         $assigned = true;
1873                 }
1874
1875                 $title = trim(XML::getFirstNodeValue($xpath, '//head/title/text()'));
1876                 if (!empty($title)) {
1877                         $serverdata['site_name'] = $title;
1878                 }
1879
1880                 $list = $xpath->query('//meta[@name]');
1881
1882                 foreach ($list as $node) {
1883                         $attr = [];
1884                         if ($node->attributes->length) {
1885                                 foreach ($node->attributes as $attribute) {
1886                                         $value = trim($attribute->value);
1887                                         if (empty($value)) {
1888                                                 continue;
1889                                         }
1890
1891                                         $attr[$attribute->name] = $value;
1892                                 }
1893
1894                                 if (empty($attr['name']) || empty($attr['content'])) {
1895                                         continue;
1896                                 }
1897                         }
1898
1899                         if ($attr['name'] == 'description') {
1900                                 $serverdata['info'] = $attr['content'];
1901                         }
1902
1903                         if (in_array($attr['name'], ['application-name', 'al:android:app_name', 'al:ios:app_name',
1904                                 'twitter:app:name:googleplay', 'twitter:app:name:iphone', 'twitter:app:name:ipad', 'generator'])) {
1905                                 $platform = str_ireplace(array_keys($platforms), array_values($platforms), $attr['content']);
1906                                 $platform = str_replace('/', ' ', $platform);
1907                                 $platform_parts = explode(' ', $platform);
1908                                 if ((count($platform_parts) >= 2) && in_array(strtolower($platform_parts[0]), array_values($platforms))) {
1909                                         $platform = $platform_parts[0];
1910                                         $serverdata['version'] = $platform_parts[1];
1911                                 }
1912                                 if (in_array($platform, array_values($dfrn_platforms))) {
1913                                         $serverdata['network'] = Protocol::DFRN;
1914                                 } elseif (in_array($platform, array_values($ap_platforms))) {
1915                                         $serverdata['network'] = Protocol::ACTIVITYPUB;
1916                                 } elseif (in_array($platform, array_values($zap_platforms))) {
1917                                         $serverdata['network'] = Protocol::ZOT;
1918                                 }
1919                                 if (in_array($platform, array_values($platforms))) {
1920                                         $serverdata['platform'] = $platform;
1921                                         $assigned = true;
1922                                 }
1923                         }
1924                 }
1925
1926                 $list = $xpath->query('//meta[@property]');
1927
1928                 foreach ($list as $node) {
1929                         $attr = [];
1930                         if ($node->attributes->length) {
1931                                 foreach ($node->attributes as $attribute) {
1932                                         $value = trim($attribute->value);
1933                                         if (empty($value)) {
1934                                                 continue;
1935                                         }
1936
1937                                         $attr[$attribute->name] = $value;
1938                                 }
1939
1940                                 if (empty($attr['property']) || empty($attr['content'])) {
1941                                         continue;
1942                                 }
1943                         }
1944
1945                         if ($attr['property'] == 'og:site_name') {
1946                                 $serverdata['site_name'] = $attr['content'];
1947                         }
1948
1949                         if ($attr['property'] == 'og:description') {
1950                                 $serverdata['info'] = $attr['content'];
1951                         }
1952
1953                         if (in_array($attr['property'], ['og:platform', 'generator'])) {
1954                                 if (in_array($attr['content'], array_keys($platforms))) {
1955                                         $serverdata['platform'] = $platforms[$attr['content']];
1956                                         $assigned = true;
1957                                 }
1958
1959                                 if (in_array($attr['content'], array_keys($ap_platforms))) {
1960                                         $serverdata['network'] = Protocol::ACTIVITYPUB;
1961                                 } elseif (in_array($attr['content'], array_values($zap_platforms))) {
1962                                         $serverdata['network'] = Protocol::ZOT;
1963                                 }
1964                         }
1965                 }
1966
1967                 $list = $xpath->query('//link[@rel="me"]');
1968                 foreach ($list as $node) {
1969                         foreach ($node->attributes as $attribute) {
1970                                 if (parse_url(trim($attribute->value), PHP_URL_HOST) == 'micro.blog') {
1971                                         $serverdata['version'] = trim($serverdata['platform'] . ' ' . $serverdata['version']);
1972                                         $serverdata['platform'] = 'microblog';
1973                                         $serverdata['network'] = Protocol::ACTIVITYPUB;
1974                                         $assigned = true;
1975                                 }
1976                         }
1977                 }
1978
1979                 if ($serverdata['platform'] != 'microblog') {
1980                         $list = $xpath->query('//link[@rel="micropub"]');
1981                         foreach ($list as $node) {
1982                                 foreach ($node->attributes as $attribute) {
1983                                         if (trim($attribute->value) == 'https://micro.blog/micropub') {
1984                                                 $serverdata['version'] = trim($serverdata['platform'] . ' ' . $serverdata['version']);
1985                                                 $serverdata['platform'] = 'microblog';
1986                                                 $serverdata['network'] = Protocol::ACTIVITYPUB;
1987                                                 $assigned = true;
1988                                         }
1989                                 }
1990                         }
1991                 }
1992
1993                 if ($assigned && in_array($serverdata['detection-method'], [self::DETECT_MANUAL, self::DETECT_HEADER])) {
1994                         $serverdata['detection-method'] = self::DETECT_BODY;
1995                 }
1996
1997                 return $serverdata;
1998         }
1999
2000         /**
2001          * Analyses the header data of a given server for hints about type and system of that server
2002          *
2003          * @param object $curlResult result of curl execution
2004          * @param array  $serverdata array with server data
2005          *
2006          * @return array server data
2007          */
2008         private static function analyseRootHeader($curlResult, array $serverdata): array
2009         {
2010                 if ($curlResult->getHeader('server') == 'Mastodon') {
2011                         $serverdata['platform'] = 'mastodon';
2012                         $serverdata['network'] = Protocol::ACTIVITYPUB;
2013                 } elseif ($curlResult->inHeader('x-diaspora-version')) {
2014                         $serverdata['platform'] = 'diaspora';
2015                         $serverdata['network'] = Protocol::DIASPORA;
2016                         $serverdata['version'] = $curlResult->getHeader('x-diaspora-version')[0] ?? '';
2017                 } elseif ($curlResult->inHeader('x-friendica-version')) {
2018                         $serverdata['platform'] = 'friendica';
2019                         $serverdata['network'] = Protocol::DFRN;
2020                         $serverdata['version'] = $curlResult->getHeader('x-friendica-version')[0] ?? '';
2021                 } else {
2022                         return $serverdata;
2023                 }
2024
2025                 if ($serverdata['detection-method'] == self::DETECT_MANUAL) {
2026                         $serverdata['detection-method'] = self::DETECT_HEADER;
2027                 }
2028
2029                 return $serverdata;
2030         }
2031
2032         /**
2033          * Update GServer entries
2034          */
2035         public static function discover()
2036         {
2037                 // Update the server list
2038                 self::discoverFederation();
2039
2040                 $no_of_queries = 5;
2041
2042                 $requery_days = intval(DI::config()->get('system', 'poco_requery_days'));
2043
2044                 if ($requery_days == 0) {
2045                         $requery_days = 7;
2046                 }
2047
2048                 $last_update = date('c', time() - (60 * 60 * 24 * $requery_days));
2049
2050                 $gservers = DBA::select('gserver', ['id', 'url', 'nurl', 'network', 'poco', 'directory-type'],
2051                         ["NOT `failed` AND `directory-type` != ? AND `last_poco_query` < ?", GServer::DT_NONE, $last_update],
2052                         ['order' => ['RAND()']]);
2053
2054                 while ($gserver = DBA::fetch($gservers)) {
2055                         Logger::info('Update peer list', ['server' => $gserver['url'], 'id' => $gserver['id']]);
2056                         Worker::add(PRIORITY_LOW, 'UpdateServerPeers', $gserver['url']);
2057
2058                         Logger::info('Update directory', ['server' => $gserver['url'], 'id' => $gserver['id']]);
2059                         Worker::add(PRIORITY_LOW, 'UpdateServerDirectory', $gserver);
2060
2061                         $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
2062                         self::update($fields, ['nurl' => $gserver['nurl']]);
2063
2064                         if (--$no_of_queries == 0) {
2065                                 break;
2066                         }
2067                 }
2068
2069                 DBA::close($gservers);
2070         }
2071
2072         /**
2073          * Discover federated servers
2074          */
2075         private static function discoverFederation()
2076         {
2077                 $last = DI::config()->get('poco', 'last_federation_discovery');
2078
2079                 if ($last) {
2080                         $next = $last + (24 * 60 * 60);
2081
2082                         if ($next > time()) {
2083                                 return;
2084                         }
2085                 }
2086
2087                 // Discover federated servers
2088                 $protocols = ['activitypub', 'diaspora', 'dfrn', 'ostatus'];
2089                 foreach ($protocols as $protocol) {
2090                         $query = '{nodes(protocol:"' . $protocol . '"){host}}';
2091                         $curlResult = DI::httpClient()->fetch('https://the-federation.info/graphql?query=' . urlencode($query), HttpClientAccept::JSON);
2092                         if (!empty($curlResult)) {
2093                                 $data = json_decode($curlResult, true);
2094                                 if (!empty($data['data']['nodes'])) {
2095                                         foreach ($data['data']['nodes'] as $server) {
2096                                                 // Using "only_nodeinfo" since servers that are listed on that page should always have it.
2097                                                 self::add('https://' . $server['host'], true);
2098                                         }
2099                                 }
2100                         }
2101                 }
2102
2103                 // Disvover Mastodon servers
2104                 $accesstoken = DI::config()->get('system', 'instances_social_key');
2105
2106                 if (!empty($accesstoken)) {
2107                         $api = 'https://instances.social/api/1.0/instances/list?count=0';
2108                         $curlResult = DI::httpClient()->get($api, HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $accesstoken]]]);
2109                         if ($curlResult->isSuccess()) {
2110                                 $servers = json_decode($curlResult->getBody(), true);
2111
2112                                 foreach ($servers['instances'] as $server) {
2113                                         $url = (is_null($server['https_score']) ? 'http' : 'https') . '://' . $server['name'];
2114                                         self::add($url);
2115                                 }
2116                         }
2117                 }
2118
2119                 DI::config()->set('poco', 'last_federation_discovery', time());
2120         }
2121
2122         /**
2123          * Set the protocol for the given server
2124          *
2125          * @param int $gsid     Server id
2126          * @param int $protocol Protocol id
2127          * @return void
2128          * @throws Exception
2129          */
2130         public static function setProtocol(int $gsid, int $protocol)
2131         {
2132                 if (empty($gsid)) {
2133                         return;
2134                 }
2135
2136                 $gserver = DBA::selectFirst('gserver', ['protocol', 'url'], ['id' => $gsid]);
2137                 if (!DBA::isResult($gserver)) {
2138                         return;
2139                 }
2140
2141                 $old = $gserver['protocol'];
2142
2143                 if (!is_null($old)) {
2144                         /*
2145                         The priority for the protocols is:
2146                                 1. ActivityPub
2147                                 2. DFRN via Diaspora
2148                                 3. Legacy DFRN
2149                                 4. Diaspora
2150                                 5. OStatus
2151                         */
2152
2153                         // We don't need to change it when nothing is to be changed
2154                         if ($old == $protocol) {
2155                                 return;
2156                         }
2157
2158                         // We don't want to mark a server as OStatus when it had been marked with any other protocol before
2159                         if ($protocol == Post\DeliveryData::OSTATUS) {
2160                                 return;
2161                         }
2162
2163                         // If the server is marked as ActivityPub then we won't change it to anything different
2164                         if ($old == Post\DeliveryData::ACTIVITYPUB) {
2165                                 return;
2166                         }
2167
2168                         // Don't change it to anything lower than DFRN if the new one wasn't ActivityPub
2169                         if (($old == Post\DeliveryData::DFRN) && ($protocol != Post\DeliveryData::ACTIVITYPUB)) {
2170                                 return;
2171                         }
2172
2173                         // Don't change it to Diaspora when it is a legacy DFRN server
2174                         if (($old == Post\DeliveryData::LEGACY_DFRN) && ($protocol == Post\DeliveryData::DIASPORA)) {
2175                                 return;
2176                         }
2177                 }
2178
2179                 Logger::info('Protocol for server', ['protocol' => $protocol, 'old' => $old, 'id' => $gsid, 'url' => $gserver['url'], 'callstack' => System::callstack(20)]);
2180                 self::update(['protocol' => $protocol], ['id' => $gsid]);
2181         }
2182
2183         /**
2184          * Fetch the protocol of the given server
2185          *
2186          * @param int $gsid Server id
2187          * @return ?int One of Post\DeliveryData protocol constants or null if unknown or gserver is missing
2188          * @throws Exception
2189          */
2190         public static function getProtocol(int $gsid): ?int
2191         {
2192                 if (empty($gsid)) {
2193                         return null;
2194                 }
2195
2196                 $gserver = DBA::selectFirst('gserver', ['protocol'], ['id' => $gsid]);
2197                 if (DBA::isResult($gserver)) {
2198                         return $gserver['protocol'];
2199                 }
2200
2201                 return null;
2202         }
2203
2204         /**
2205          * Enforces gserver table field maximum sizes to avoid "Data too long" database errors
2206          *
2207          * @param array $fields
2208          * @param array $condition
2209          * @return bool
2210          * @throws Exception
2211          */
2212         public static function update(array $fields, array $condition): bool
2213         {
2214                 $fields = DI::dbaDefinition()->truncateFieldsForTable('gserver', $fields);
2215
2216                 return DBA::update('gserver', $fields, $condition);
2217         }
2218 }