3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Model;
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;
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;
50 * This class handles GServer related functions
57 const DT_MASTODON = 2;
59 // Methods to detect server types
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;
70 const DETECT_UNSPECIFIC = [self::DETECT_MANUAL, self::DETECT_HEADER, self::DETECT_BODY, self::DETECT_HOST_META, self::DETECT_CONTACTS, self::DETECT_AP_ACTOR];
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
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;
92 * Check for the existance of a server and adds it in the background if not existant
95 * @param boolean $only_nodeinfo
98 public static function add(string $url, bool $only_nodeinfo = false)
100 if (self::getID($url, false)) {
104 Worker::add(PRIORITY_LOW, 'UpdateGServer', $url, $only_nodeinfo);
108 * Get the ID for the given server 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
114 public static function getID(string $url, bool $no_check = false)
120 $url = self::cleanURL($url);
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'];
128 if ($no_check || !self::check($url)) {
132 return self::getID($url, true);
136 * Retrieves all the servers which base domain are matching the provided domain pattern
138 * The pattern is a simple fnmatch() pattern with ? for single wildcard and * for multiple wildcard
140 * @param string $pattern
144 public static function listByDomainPattern(string $pattern): array
146 $likePattern = 'http://' . strtr($pattern, ['_' => '\_', '%' => '\%', '?' => '_', '*' => '%']);
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`
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`";
157 $stmt = DI::dba()->p($sql, $likePattern);
159 return DI::dba()->toArray($stmt);
163 * Checks if the given server is reachable
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.
170 * @return boolean 'true' if server seems vital
172 public static function reachable(string $profile, string $server = '', string $network = '', bool $force = false): bool
175 $contact = Contact::getByURL($profile, null, ['baseurl']);
176 if (!empty($contact['baseurl'])) {
177 $server = $contact['baseurl'];
185 return self::check($server, $network, $force);
188 public static function getNextUpdateDate(bool $success, string $created = '', string $last_contact = '', bool $undetected = false)
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.
193 return DateTimeFormat::utc($undetected ? 'now +1 month' : 'now +7 day');
196 $now = strtotime(DateTimeFormat::utcNow());
198 if ($created > $last_contact) {
199 $contact_time = strtotime($created);
201 $contact_time = strtotime($last_contact);
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');
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');
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');
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');
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');
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');
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');
239 * Checks the state of the given server.
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
246 * @return boolean 'true' if server seems vital
248 public static function check(string $server_url, string $network = '', bool $force = false, bool $only_nodeinfo = false): bool
250 $server_url = self::cleanURL($server_url);
251 if ($server_url == '') {
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);
263 if (!$force && (strtotime($gserver['next_contact']) > time())) {
264 Logger::info('No update needed', ['server' => $server_url]);
265 return (!$gserver['failed']);
267 Logger::info('Server is outdated. Start discovery.', ['Server' => $server_url, 'Force' => $force]);
269 Logger::info('Server is unknown. Start discovery.', ['Server' => $server_url]);
272 return self::detect($server_url, $network, $only_nodeinfo);
276 * Set failed server status
280 public static function setFailure(string $url)
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]);
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]);
298 * Remove unwanted content from the given URL
301 * @return string cleaned URL
303 public static function cleanURL(string $url): string
305 $url = trim($url, '/');
306 $url = str_replace('/index.php', '', $url);
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);
317 * Detect server data (type, protocol, version number, ...)
318 * The detected data is then updated or inserted in the gserver table.
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
324 * @return boolean 'true' if server could be detected
326 public static function detect(string $url, string $network = '', bool $only_nodeinfo = false): bool
328 Logger::info('Detect server type', ['server' => $url]);
330 $original_url = $url;
332 // Remove URL content that is not supposed to exist for a server url
333 $url = rtrim(self::cleanURL($url), '/');
335 Logger::notice('Empty URL.');
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);
348 $valid_url = Network::isUrlValid($url);
350 self::setFailure($url);
353 $valid_url = rtrim($valid_url, '/');
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);
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);
376 self::setFailure($url);
377 if (!self::getID($valid_url, true)) {
378 self::detect($valid_url, $network, $only_nodeinfo);
382 Logger::debug('Found redirect, but ignore it.', ['old' => $url, 'new' => $valid_url]);
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))) {
391 $in_webroot = empty(parse_url($url, PHP_URL_PATH));
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);
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);
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);
410 } elseif (empty($serverdata)) {
411 $serverdata = ['detection-method' => self::DETECT_MANUAL, 'network' => Protocol::PHANTOM, 'platform' => '', 'version' => '', 'site_name' => '', 'info' => ''];
414 // When there is no Nodeinfo, then use some protocol specific endpoints
415 if ($serverdata['network'] == Protocol::PHANTOM) {
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;
424 $html_fetched = false;
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);
436 } elseif (!$html_fetched && (strlen($curlResult->getBody()) < 1000)) {
437 $curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML);
440 if ($serverdata['detection-method'] != self::DETECT_SYSTEM_ACTOR) {
441 $serverdata = self::analyseRootHeader($curlResult, $serverdata);
442 $serverdata = self::analyseRootBody($curlResult, $serverdata);
446 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
447 self::setFailure($url);
451 if (($serverdata['network'] == Protocol::PHANTOM) || in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
452 $serverdata = self::detectMastodonAlikes($url, $serverdata);
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);
462 $validHostMeta = false;
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;
470 if (($serverdata['network'] == Protocol::PHANTOM) || in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
471 $serverdata = self::detectFriendica($url, $serverdata);
474 // The following systems have to be installed in the root directory.
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);
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);
486 if (($serverdata['network'] == Protocol::PHANTOM) || in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
487 $serverdata = self::detectPeertube($url, $serverdata);
490 if (($serverdata['network'] == Protocol::PHANTOM) || in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
491 $serverdata = self::detectGNUSocial($url, $serverdata);
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);
498 if (($serverdata['network'] == Protocol::PHANTOM) || in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
499 $serverdata = self::detectNextcloud($url, $serverdata, $validHostMeta);
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);
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;
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);
520 $serverdata['url'] = $url;
521 $serverdata['nurl'] = Strings::normaliseLink($url);
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);
532 // Detect the directory type
533 $serverdata['directory-type'] = self::DT_NONE;
535 if (in_array($serverdata['network'], Protocol::FEDERATED)) {
536 $serverdata = self::checkMastodonDirectory($url, $serverdata);
538 if ($serverdata['directory-type'] == self::DT_NONE) {
539 $serverdata = self::checkPoCo($url, $serverdata);
543 if ($serverdata['network'] == Protocol::ACTIVITYPUB) {
544 $serverdata = self::fetchWeeklyUsage($url, $serverdata);
547 $serverdata['registered-users'] = $serverdata['registered-users'] ?? 0;
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;
556 $serverdata['next_contact'] = self::getNextUpdateDate(true, '', '', in_array($serverdata['network'], [Protocol::PHANTOM, Protocol::FEED]));
558 $serverdata['last_contact'] = DateTimeFormat::utcNow();
559 $serverdata['failed'] = false;
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();
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'];
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]);
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')]);
587 Logger::info('Update monthly users', ['id' => $id, 'url' => $serverdata['nurl'], 'monthly-users' => $contacts]);
588 self::update(['active-month-users' => $contacts], ['id' => $id]);
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')]);
595 Logger::info('Update halfyear users', ['id' => $id, 'url' => $serverdata['nurl'], 'halfyear-users' => $contacts]);
596 self::update(['active-halfyear-users' => $contacts], ['id' => $id]);
601 if (in_array($serverdata['network'], [Protocol::DFRN, Protocol::DIASPORA])) {
602 self::discoverRelay($url);
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]);
614 * Fetch relay data from a given server url
616 * @param string $server_url address of the server
618 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
620 private static function discoverRelay(string $server_url)
622 Logger::info('Discover relay data', ['server' => $server_url]);
624 $curlResult = DI::httpClient()->get($server_url . '/.well-known/x-social-relay', HttpClientAccept::JSON);
625 if (!$curlResult->isSuccess()) {
629 $data = json_decode($curlResult->getBody(), true);
630 if (!is_array($data)) {
634 // Sanitize incoming data, see https://github.com/friendica/friendica/issues/8565
635 $data['subscribe'] = (bool)$data['subscribe'] ?? false;
637 if (!$data['subscribe'] || empty($data['scope']) || !in_array(strtolower($data['scope']), ['all', 'tags'])) {
639 $data['subscribe'] = false;
643 $gserver = DBA::selectFirst('gserver', ['id', 'url', 'network', 'relay-subscribe', 'relay-scope'], ['nurl' => Strings::normaliseLink($server_url)]);
644 if (!DBA::isResult($gserver)) {
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']]);
653 DBA::delete('gserver-tag', ['gserver-id' => $gserver['id']]);
655 if ($data['scope'] == 'tags') {
658 foreach ($data['tags'] as $tag) {
659 $tag = mb_strtolower($tag);
660 if (strlen($tag) < 100) {
665 foreach ($tags as $tag) {
666 DBA::insert('gserver-tag', ['gserver-id' => $gserver['id'], 'tag' => $tag], Database::INSERT_IGNORE);
670 // Create or update the relay contact
672 if (isset($data['protocols'])) {
673 if (isset($data['protocols']['diaspora'])) {
674 $fields['network'] = Protocol::DIASPORA;
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'];
683 if (isset($data['protocols']['dfrn'])) {
684 $fields['network'] = Protocol::DFRN;
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'];
693 if (isset($data['protocols']['activitypub'])) {
694 $fields['network'] = Protocol::ACTIVITYPUB;
696 if (!empty($data['protocols']['activitypub']['actor'])) {
697 $fields['url'] = $data['protocols']['activitypub']['actor'];
699 if (!empty($data['protocols']['activitypub']['receive'])) {
700 $fields['batch'] = $data['protocols']['activitypub']['receive'];
705 Logger::info('Discovery ended', ['server' => $server_url, 'data' => $fields]);
707 Relay::updateContact($gserver, $fields);
711 * Fetch server data from '/statistics.json' on the given server
713 * @param string $url URL of the given server
714 * @return array server data
716 private static function fetchStatistics(string $url, array $serverdata)
718 $curlResult = DI::httpClient()->get($url . '/statistics.json', HttpClientAccept::JSON);
719 if (!$curlResult->isSuccess()) {
723 $data = json_decode($curlResult->getBody(), true);
728 // Some AP enabled systems return activity data that we don't expect here.
729 if (strpos($curlResult->getContentType(), 'application/activity+json') !== false) {
734 $old_serverdata = $serverdata;
736 $serverdata['detection-method'] = self::DETECT_STATISTICS_JSON;
738 if (!empty($data['version'])) {
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']);
746 if (!empty($data['name'])) {
748 $serverdata['site_name'] = $data['name'];
751 if (!empty($data['network'])) {
753 $serverdata['platform'] = strtolower($data['network']);
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;
766 if (!empty($data['total_users'])) {
768 $serverdata['registered-users'] = max($data['total_users'], 1);
771 if (!empty($data['active_users_monthly'])) {
773 $serverdata['active-month-users'] = max($data['active_users_monthly'], 0);
776 if (!empty($data['active_users_halfyear'])) {
778 $serverdata['active-halfyear-users'] = max($data['active_users_halfyear'], 0);
781 if (!empty($data['local_posts'])) {
783 $serverdata['local-posts'] = max($data['local_posts'], 0);
786 if (!empty($data['registrations_open'])) {
787 $serverdata['register_policy'] = Register::OPEN;
789 $serverdata['register_policy'] = Register::CLOSED;
793 return $old_serverdata;
800 * Detect server type by using the nodeinfo data
802 * @param string $url address of the server
803 * @param ICanHandleHttpResponses $httpResult
805 * @return array Server data
806 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
808 private static function fetchNodeinfo(string $url, ICanHandleHttpResponses $httpResult): array
810 if (!$httpResult->isSuccess()) {
814 $nodeinfo = json_decode($httpResult->getBody(), true);
816 if (!is_array($nodeinfo) || empty($nodeinfo['links'])) {
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]);
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'];
835 if ($nodeinfo1_url . $nodeinfo2_url == '') {
841 if (!empty($nodeinfo2_url)) {
842 $server = self::parseNodeinfo2($nodeinfo2_url);
845 if (empty($server) && !empty($nodeinfo1_url)) {
846 $server = self::parseNodeinfo1($nodeinfo1_url);
855 * @param string $nodeinfo_url address of the nodeinfo path
856 * @return array Server data
857 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
859 private static function parseNodeinfo1(string $nodeinfo_url): array
861 $curlResult = DI::httpClient()->get($nodeinfo_url, HttpClientAccept::JSON);
862 if (!$curlResult->isSuccess()) {
866 $nodeinfo = json_decode($curlResult->getBody(), true);
868 if (!is_array($nodeinfo)) {
872 $server = ['detection-method' => self::DETECT_NODEINFO_1,
873 'register_policy' => Register::CLOSED];
875 if (!empty($nodeinfo['openRegistrations'])) {
876 $server['register_policy'] = Register::OPEN;
879 if (is_array($nodeinfo['software'])) {
880 if (!empty($nodeinfo['software']['name'])) {
881 $server['platform'] = strtolower($nodeinfo['software']['name']);
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']);
892 if (!empty($nodeinfo['metadata']['nodeName'])) {
893 $server['site_name'] = $nodeinfo['metadata']['nodeName'];
896 if (!empty($nodeinfo['usage']['users']['total'])) {
897 $server['registered-users'] = max($nodeinfo['usage']['users']['total'], 1);
900 if (!empty($nodeinfo['usage']['users']['activeMonth'])) {
901 $server['active-month-users'] = max($nodeinfo['usage']['users']['activeMonth'], 0);
904 if (!empty($nodeinfo['usage']['users']['activeHalfyear'])) {
905 $server['active-halfyear-users'] = max($nodeinfo['usage']['users']['activeHalfyear'], 0);
908 if (!empty($nodeinfo['usage']['localPosts'])) {
909 $server['local-posts'] = max($nodeinfo['usage']['localPosts'], 0);
912 if (!empty($nodeinfo['usage']['localComments'])) {
913 $server['local-comments'] = max($nodeinfo['usage']['localComments'], 0);
916 if (!empty($nodeinfo['protocols']['inbound']) && is_array($nodeinfo['protocols']['inbound'])) {
918 foreach ($nodeinfo['protocols']['inbound'] as $protocol) {
919 $protocols[$protocol] = true;
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;
937 if (empty($server)) {
941 if (empty($server['network'])) {
942 $server['network'] = Protocol::PHANTOM;
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
956 private static function parseNodeinfo2(string $nodeinfo_url): array
958 $curlResult = DI::httpClient()->get($nodeinfo_url, HttpClientAccept::JSON);
959 if (!$curlResult->isSuccess()) {
963 $nodeinfo = json_decode($curlResult->getBody(), true);
964 if (!is_array($nodeinfo)) {
969 'detection-method' => self::DETECT_NODEINFO_2,
970 'register_policy' => Register::CLOSED,
971 'platform' => 'unknown',
974 if (!empty($nodeinfo['openRegistrations'])) {
975 $server['register_policy'] = Register::OPEN;
978 if (!empty($nodeinfo['software'])) {
979 if (isset($nodeinfo['software']['name'])) {
980 $server['platform'] = strtolower($nodeinfo['software']['name']);
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']);
989 // qoto advertises itself as Mastodon
990 if (($server['platform'] == 'mastodon') && substr($nodeinfo['software']['version'], -5) == '-qoto') {
991 $server['platform'] = 'qoto';
996 if (!empty($nodeinfo['metadata']['nodeName'])) {
997 $server['site_name'] = $nodeinfo['metadata']['nodeName'];
1000 if (!empty($nodeinfo['usage']['users']['total'])) {
1001 $server['registered-users'] = max($nodeinfo['usage']['users']['total'], 1);
1004 if (!empty($nodeinfo['usage']['users']['activeMonth'])) {
1005 $server['active-month-users'] = max($nodeinfo['usage']['users']['activeMonth'], 0);
1008 if (!empty($nodeinfo['usage']['users']['activeHalfyear'])) {
1009 $server['active-halfyear-users'] = max($nodeinfo['usage']['users']['activeHalfyear'], 0);
1012 if (!empty($nodeinfo['usage']['localPosts'])) {
1013 $server['local-posts'] = max($nodeinfo['usage']['localPosts'], 0);
1016 if (!empty($nodeinfo['usage']['localComments'])) {
1017 $server['local-comments'] = max($nodeinfo['usage']['localComments'], 0);
1020 if (!empty($nodeinfo['protocols'])) {
1022 foreach ($nodeinfo['protocols'] as $protocol) {
1023 if (is_string($protocol)) {
1024 $protocols[$protocol] = true;
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;
1043 if (empty($server)) {
1047 if (empty($server['network'])) {
1048 $server['network'] = Protocol::PHANTOM;
1055 * Parses NodeInfo2 protocol 1.0
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
1062 private static function parseNodeinfo210(ICanHandleHttpResponses $httpResult)
1064 if (!$httpResult->isSuccess()) {
1068 $nodeinfo = json_decode($httpResult->getBody(), true);
1070 if (!is_array($nodeinfo)) {
1074 $server = ['detection-method' => self::DETECT_NODEINFO_210,
1075 'register_policy' => Register::CLOSED];
1077 if (!empty($nodeinfo['openRegistrations'])) {
1078 $server['register_policy'] = Register::OPEN;
1081 if (!empty($nodeinfo['server'])) {
1082 if (!empty($nodeinfo['server']['software'])) {
1083 $server['platform'] = strtolower($nodeinfo['server']['software']);
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']);
1093 if (!empty($nodeinfo['server']['name'])) {
1094 $server['site_name'] = $nodeinfo['server']['name'];
1098 if (!empty($nodeinfo['usage']['users']['total'])) {
1099 $server['registered-users'] = max($nodeinfo['usage']['users']['total'], 1);
1102 if (!empty($nodeinfo['usage']['users']['activeMonth'])) {
1103 $server['active-month-users'] = max($nodeinfo['usage']['users']['activeMonth'], 0);
1106 if (!empty($nodeinfo['usage']['users']['activeHalfyear'])) {
1107 $server['active-halfyear-users'] = max($nodeinfo['usage']['users']['activeHalfyear'], 0);
1110 if (!empty($nodeinfo['usage']['localPosts'])) {
1111 $server['local-posts'] = max($nodeinfo['usage']['localPosts'], 0);
1114 if (!empty($nodeinfo['usage']['localComments'])) {
1115 $server['local-comments'] = max($nodeinfo['usage']['localComments'], 0);
1118 if (!empty($nodeinfo['protocols'])) {
1120 foreach ($nodeinfo['protocols'] as $protocol) {
1121 if (is_string($protocol)) {
1122 $protocols[$protocol] = true;
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;
1141 if (empty($server) || empty($server['platform'])) {
1145 if (empty($server['network'])) {
1146 $server['network'] = Protocol::PHANTOM;
1153 * Fetch server information from a 'siteinfo.json' file on the given server
1155 * @param string $url URL of the given server
1156 * @param array $serverdata array with server data
1157 * @return array server data
1159 private static function fetchSiteinfo(string $url, array $serverdata): array
1161 $curlResult = DI::httpClient()->get($url . '/siteinfo.json', HttpClientAccept::JSON);
1162 if (!$curlResult->isSuccess()) {
1166 $data = json_decode($curlResult->getBody(), true);
1171 if (in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
1172 $serverdata['detection-method'] = self::DETECT_SITEINFO_JSON;
1175 if (!empty($data['url'])) {
1176 $serverdata['platform'] = strtolower($data['platform']);
1177 $serverdata['version'] = $data['version'];
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;
1188 $serverdata['network'] = Protocol::ZOT;
1192 if (!empty($data['site_name'])) {
1193 $serverdata['site_name'] = $data['site_name'];
1196 if (!empty($data['channels_total'])) {
1197 $serverdata['registered-users'] = max($data['channels_total'], 1);
1200 if (!empty($data['channels_active_monthly'])) {
1201 $serverdata['active-month-users'] = max($data['channels_active_monthly'], 0);
1204 if (!empty($data['channels_active_halfyear'])) {
1205 $serverdata['active-halfyear-users'] = max($data['channels_active_halfyear'], 0);
1208 if (!empty($data['local_posts'])) {
1209 $serverdata['local-posts'] = max($data['local_posts'], 0);
1212 if (!empty($data['local_comments'])) {
1213 $serverdata['local-comments'] = max($data['local_comments'], 0);
1216 if (!empty($data['register_policy'])) {
1217 switch ($data['register_policy']) {
1218 case 'REGISTER_OPEN':
1219 $serverdata['register_policy'] = Register::OPEN;
1222 case 'REGISTER_APPROVE':
1223 $serverdata['register_policy'] = Register::APPROVE;
1226 case 'REGISTER_CLOSED':
1228 $serverdata['register_policy'] = Register::CLOSED;
1236 private static function fetchDataFromSystemActor(array $data, array $serverdata)
1239 return ['server' => $serverdata, 'actor' => ''];
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;
1252 $serverdata['detection-method'] = self::DETECT_AP_ACTOR;
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;
1261 $actors = JsonLD::fetchElementArray($actor, 'as:items');
1262 if (!empty($actors) && !empty($actors[0]['@id'])) {
1263 $actor_url = $actor['@id'] . $actors[0]['@id'];
1268 return ['server' => $serverdata, 'actor' => $actor_url];
1270 return ['server' => $serverdata, 'actor' => ''];
1274 * Checks if the server contains a valid host meta file
1276 * @param string $url URL of the given server
1277 * @return boolean 'true' if the server seems to be vital
1279 private static function validHostMeta(string $url): bool
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()) {
1287 $xrd = XML::parseString($curlResult->getBody());
1288 if (!is_object($xrd)) {
1292 $elements = XML::elementToArray($xrd);
1293 if (empty($elements) || empty($elements['xrd']) || empty($elements['xrd']['link'])) {
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'];
1304 if (empty($link['rel']) || empty($link['template'])) {
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));
1318 * Detect the network of the given server via their known contacts
1320 * @param string $url URL of the given server
1321 * @param array $serverdata array with server data
1322 * @return array server data
1324 private static function detectNetworkViaContacts(string $url, array $serverdata): array
1328 $nurl = Strings::normaliseLink($url);
1330 $apcontacts = DBA::select('apcontact', ['url'], ['baseurl' => [$url, $nurl]]);
1331 while ($apcontact = DBA::fetch($apcontacts)) {
1332 $contacts[Strings::normaliseLink($apcontact['url'])] = $apcontact['url'];
1334 DBA::close($apcontacts);
1336 $pcontacts = DBA::select('contact', ['url', 'nurl'], ['uid' => 0, 'baseurl' => [$url, $nurl]]);
1337 while ($pcontact = DBA::fetch($pcontacts)) {
1338 $contacts[$pcontact['nurl']] = $pcontact['url'];
1340 DBA::close($pcontacts);
1342 if (empty($contacts)) {
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'];
1353 if (in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
1354 $serverdata['detection-method'] = self::DETECT_CONTACTS;
1357 } elseif ((time() - $time) > 10) {
1358 // To reduce the stress on remote systems we probe a maximum of 10 seconds
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.
1371 * @param string $url URL of the given server
1372 * @param array $serverdata array with server data
1373 * @return array server data
1375 private static function checkPoCo(string $url, array $serverdata): array
1377 $serverdata['poco'] = '';
1379 $curlResult = DI::httpClient()->get($url . '/poco', HttpClientAccept::JSON);
1380 if (!$curlResult->isSuccess()) {
1384 $data = json_decode($curlResult->getBody(), true);
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';
1400 * Checks if the given server does have a Mastodon style directory endpoint.
1402 * @param string $url URL of the given server
1403 * @param array $serverdata array with server data
1404 * @return array server data
1406 public static function checkMastodonDirectory(string $url, array $serverdata): array
1408 $curlResult = DI::httpClient()->get($url . '/api/v1/directory?limit=1', HttpClientAccept::JSON);
1409 if (!$curlResult->isSuccess()) {
1413 $data = json_decode($curlResult->getBody(), true);
1418 if (count($data) == 1) {
1419 $serverdata['directory-type'] = self::DT_MASTODON;
1426 * Detects Peertube via their known endpoint
1428 * @param string $url URL of the given server
1429 * @param array $serverdata array with server data
1431 * @return array server data
1433 private static function detectPeertube(string $url, array $serverdata): array
1435 $curlResult = DI::httpClient()->get($url . '/api/v1/config', HttpClientAccept::JSON);
1436 if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
1440 $data = json_decode($curlResult->getBody(), true);
1445 if (!empty($data['instance']) && !empty($data['serverVersion'])) {
1446 $serverdata['platform'] = 'peertube';
1447 $serverdata['version'] = $data['serverVersion'];
1448 $serverdata['network'] = Protocol::ACTIVITYPUB;
1450 if (!empty($data['instance']['name'])) {
1451 $serverdata['site_name'] = $data['instance']['name'];
1454 if (!empty($data['instance']['shortDescription'])) {
1455 $serverdata['info'] = $data['instance']['shortDescription'];
1458 if (!empty($data['signup'])) {
1459 if (!empty($data['signup']['allowed'])) {
1460 $serverdata['register_policy'] = Register::OPEN;
1464 if (in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
1465 $serverdata['detection-method'] = self::DETECT_V1_CONFIG;
1473 * Detects the version number of a given server when it was a NextCloud installation
1475 * @param string $url URL of the given server
1476 * @param array $serverdata array with server data
1477 * @param bool $validHostMeta
1479 * @return array server data
1481 private static function detectNextcloud(string $url, array $serverdata, bool $validHostMeta)
1483 $curlResult = DI::httpClient()->get($url . '/status.php', HttpClientAccept::JSON);
1484 if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
1488 $data = json_decode($curlResult->getBody(), true);
1493 if (!empty($data['version'])) {
1494 $serverdata['platform'] = 'nextcloud';
1495 $serverdata['version'] = $data['version'];
1497 if ($validHostMeta) {
1498 $serverdata['network'] = Protocol::ACTIVITYPUB;
1501 if (in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
1502 $serverdata['detection-method'] = self::DETECT_STATUS_PHP;
1510 * Fetches weekly usage data
1512 * @param string $url URL of the given server
1513 * @param array $serverdata array with server data
1514 * @return array server data
1516 private static function fetchWeeklyUsage(string $url, array $serverdata): array
1518 $curlResult = DI::httpClient()->get($url . '/api/v1/instance/activity', HttpClientAccept::JSON);
1519 if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
1523 $data = json_decode($curlResult->getBody(), true);
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) {
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;
1541 if (!empty($current_week['logins'])) {
1542 $serverdata['active-week-users'] = max($current_week['logins'], 0);
1549 * Detects data from a given server url if it was a mastodon alike system
1551 * @param string $url URL of the given server
1552 * @param array $serverdata array with server data
1553 * @return array server data
1555 private static function detectMastodonAlikes(string $url, array $serverdata): array
1557 $curlResult = DI::httpClient()->get($url . '/api/v1/instance', HttpClientAccept::JSON);
1558 if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
1562 $data = json_decode($curlResult->getBody(), true);
1569 if (!empty($data['version'])) {
1570 $serverdata['platform'] = 'mastodon';
1571 $serverdata['version'] = $data['version'] ?? '';
1572 $serverdata['network'] = Protocol::ACTIVITYPUB;
1576 if (!empty($data['title'])) {
1577 $serverdata['site_name'] = $data['title'];
1580 if (!empty($data['title']) && empty($serverdata['platform']) && ($serverdata['network'] == Protocol::PHANTOM)) {
1581 $serverdata['platform'] = 'mastodon';
1582 $serverdata['network'] = Protocol::ACTIVITYPUB;
1586 if (!empty($data['description'])) {
1587 $serverdata['info'] = trim($data['description']);
1590 if (!empty($data['stats']['user_count'])) {
1591 $serverdata['registered-users'] = max($data['stats']['user_count'], 1);
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];
1600 if (!empty($serverdata['version']) && strstr(strtolower($serverdata['version']), 'pleroma')) {
1601 $serverdata['platform'] = 'pleroma';
1602 $serverdata['version'] = trim(str_ireplace('pleroma', '', $serverdata['version']));
1606 if (!empty($serverdata['platform']) && strstr($serverdata['platform'], 'pleroma')) {
1607 $serverdata['version'] = trim(str_ireplace('pleroma', '', $serverdata['platform']));
1608 $serverdata['platform'] = 'pleroma';
1612 if ($valid && in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
1613 $serverdata['detection-method'] = self::DETECT_MASTODON_API;
1620 * Detects data from typical Hubzilla endpoints
1622 * @param string $url URL of the given server
1623 * @param array $serverdata array with server data
1624 * @return array server data
1626 private static function detectHubzilla(string $url, array $serverdata): array
1628 $curlResult = DI::httpClient()->get($url . '/api/statusnet/config.json', HttpClientAccept::JSON);
1629 if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
1633 $data = json_decode($curlResult->getBody(), true);
1634 if (empty($data) || empty($data['site'])) {
1638 if (!empty($data['site']['name'])) {
1639 $serverdata['site_name'] = $data['site']['name'];
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;
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;
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']);
1661 $serverdata['version'] = $data['site']['redmatrix']['RED_VERSION'];
1662 $serverdata['network'] = Protocol::ZOT;
1666 $inviteonly = false;
1669 if (!empty($data['site']['closed'])) {
1670 $closed = self::toBoolean($data['site']['closed']);
1673 if (!empty($data['site']['private'])) {
1674 $private = self::toBoolean($data['site']['private']);
1677 if (!empty($data['site']['inviteonly'])) {
1678 $inviteonly = self::toBoolean($data['site']['inviteonly']);
1681 if (!$closed && !$private and $inviteonly) {
1682 $serverdata['register_policy'] = Register::APPROVE;
1683 } elseif (!$closed && !$private) {
1684 $serverdata['register_policy'] = Register::OPEN;
1686 $serverdata['register_policy'] = Register::CLOSED;
1689 if (($serverdata['network'] != Protocol::PHANTOM) && in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
1690 $serverdata['detection-method'] = self::DETECT_CONFIG_JSON;
1697 * Converts input value to a boolean value
1699 * @param string|integer $val
1702 private static function toBoolean($val): bool
1704 if (($val == 'true') || ($val == 1)) {
1706 } elseif (($val == 'false') || ($val == 0)) {
1714 * Detect if the URL belongs to a GNU Social server
1716 * @param string $url URL of the given server
1717 * @param array $serverdata array with server data
1718 * @return array server data
1720 private static function detectGNUSocial(string $url, array $serverdata): array
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;
1733 if (in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
1734 $serverdata['detection-method'] = self::DETECT_GNUSOCIAL;
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)) {
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'], '"');
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;
1755 $serverdata['platform'] = 'statusnet';
1756 $serverdata['network'] = Protocol::OSTATUS;
1759 if (in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
1760 $serverdata['detection-method'] = self::DETECT_STATUSNET;
1768 * Detect if the URL belongs to a Friendica server
1770 * @param string $url URL of the given server
1771 * @param array $serverdata array with server data
1772 * @return array server data
1774 private static function detectFriendica(string $url, array $serverdata): array
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');
1782 $platform = 'Friendika';
1785 $platform = 'Friendica';
1788 if (!$curlResult->isSuccess()) {
1792 $data = json_decode($curlResult->getBody(), true);
1793 if (empty($data) || empty($data['version'])) {
1797 if (in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
1798 $serverdata['detection-method'] = $friendika ? self::DETECT_FRIENDIKA : self::DETECT_FRIENDICA;
1801 $serverdata['network'] = Protocol::DFRN;
1802 $serverdata['version'] = $data['version'];
1804 if (!empty($data['no_scrape_url'])) {
1805 $serverdata['noscrape'] = $data['no_scrape_url'];
1808 if (!empty($data['site_name'])) {
1809 $serverdata['site_name'] = $data['site_name'];
1812 if (!empty($data['info'])) {
1813 $serverdata['info'] = trim($data['info']);
1816 $register_policy = ($data['register_policy'] ?? '') ?: 'REGISTER_CLOSED';
1817 switch ($register_policy) {
1818 case 'REGISTER_OPEN':
1819 $serverdata['register_policy'] = Register::OPEN;
1822 case 'REGISTER_APPROVE':
1823 $serverdata['register_policy'] = Register::APPROVE;
1826 case 'REGISTER_CLOSED':
1827 case 'REGISTER_INVITATION':
1828 $serverdata['register_policy'] = Register::CLOSED;
1831 Logger::info('Register policy is invalid', ['policy' => $register_policy, 'server' => $url]);
1832 $serverdata['register_policy'] = Register::CLOSED;
1836 $serverdata['platform'] = strtolower($data['platform'] ?? $platform);
1842 * Analyses the landing page of a given server for hints about type and system of that server
1844 * @param object $curlResult result of curl execution
1845 * @param array $serverdata array with server data
1847 * @return array server data
1849 private static function analyseRootBody($curlResult, array $serverdata): array
1851 if (empty($curlResult->getBody())) {
1855 if (file_exists(__DIR__ . '/../../static/generator.config.php')) {
1856 require __DIR__ . '/../../static/generator.config.php';
1858 throw new HTTPException\InternalServerErrorException('Invalid generator file');
1861 $platforms = array_merge($ap_platforms, $dfrn_platforms, $zap_platforms, $platforms);
1863 $doc = new DOMDocument();
1864 @$doc->loadHTML($curlResult->getBody());
1865 $xpath = new DOMXPath($doc);
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;
1875 $title = trim(XML::getFirstNodeValue($xpath, '//head/title/text()'));
1876 if (!empty($title)) {
1877 $serverdata['site_name'] = $title;
1880 $list = $xpath->query('//meta[@name]');
1882 foreach ($list as $node) {
1884 if ($node->attributes->length) {
1885 foreach ($node->attributes as $attribute) {
1886 $value = trim($attribute->value);
1887 if (empty($value)) {
1891 $attr[$attribute->name] = $value;
1894 if (empty($attr['name']) || empty($attr['content'])) {
1899 if ($attr['name'] == 'description') {
1900 $serverdata['info'] = $attr['content'];
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];
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;
1919 if (in_array($platform, array_values($platforms))) {
1920 $serverdata['platform'] = $platform;
1926 $list = $xpath->query('//meta[@property]');
1928 foreach ($list as $node) {
1930 if ($node->attributes->length) {
1931 foreach ($node->attributes as $attribute) {
1932 $value = trim($attribute->value);
1933 if (empty($value)) {
1937 $attr[$attribute->name] = $value;
1940 if (empty($attr['property']) || empty($attr['content'])) {
1945 if ($attr['property'] == 'og:site_name') {
1946 $serverdata['site_name'] = $attr['content'];
1949 if ($attr['property'] == 'og:description') {
1950 $serverdata['info'] = $attr['content'];
1953 if (in_array($attr['property'], ['og:platform', 'generator'])) {
1954 if (in_array($attr['content'], array_keys($platforms))) {
1955 $serverdata['platform'] = $platforms[$attr['content']];
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;
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;
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;
1993 if ($assigned && in_array($serverdata['detection-method'], [self::DETECT_MANUAL, self::DETECT_HEADER])) {
1994 $serverdata['detection-method'] = self::DETECT_BODY;
2001 * Analyses the header data of a given server for hints about type and system of that server
2003 * @param object $curlResult result of curl execution
2004 * @param array $serverdata array with server data
2006 * @return array server data
2008 private static function analyseRootHeader($curlResult, array $serverdata): array
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] ?? '';
2025 if ($serverdata['detection-method'] == self::DETECT_MANUAL) {
2026 $serverdata['detection-method'] = self::DETECT_HEADER;
2033 * Update GServer entries
2035 public static function discover()
2037 // Update the server list
2038 self::discoverFederation();
2042 $requery_days = intval(DI::config()->get('system', 'poco_requery_days'));
2044 if ($requery_days == 0) {
2048 $last_update = date('c', time() - (60 * 60 * 24 * $requery_days));
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()']]);
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']);
2058 Logger::info('Update directory', ['server' => $gserver['url'], 'id' => $gserver['id']]);
2059 Worker::add(PRIORITY_LOW, 'UpdateServerDirectory', $gserver);
2061 $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
2062 self::update($fields, ['nurl' => $gserver['nurl']]);
2064 if (--$no_of_queries == 0) {
2069 DBA::close($gservers);
2073 * Discover federated servers
2075 private static function discoverFederation()
2077 $last = DI::config()->get('poco', 'last_federation_discovery');
2080 $next = $last + (24 * 60 * 60);
2082 if ($next > time()) {
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);
2103 // Disvover Mastodon servers
2104 $accesstoken = DI::config()->get('system', 'instances_social_key');
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);
2112 foreach ($servers['instances'] as $server) {
2113 $url = (is_null($server['https_score']) ? 'http' : 'https') . '://' . $server['name'];
2119 DI::config()->set('poco', 'last_federation_discovery', time());
2123 * Set the protocol for the given server
2125 * @param int $gsid Server id
2126 * @param int $protocol Protocol id
2130 public static function setProtocol(int $gsid, int $protocol)
2136 $gserver = DBA::selectFirst('gserver', ['protocol', 'url'], ['id' => $gsid]);
2137 if (!DBA::isResult($gserver)) {
2141 $old = $gserver['protocol'];
2143 if (!is_null($old)) {
2145 The priority for the protocols is:
2147 2. DFRN via Diaspora
2153 // We don't need to change it when nothing is to be changed
2154 if ($old == $protocol) {
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) {
2163 // If the server is marked as ActivityPub then we won't change it to anything different
2164 if ($old == Post\DeliveryData::ACTIVITYPUB) {
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)) {
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)) {
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]);
2184 * Fetch the protocol of the given server
2186 * @param int $gsid Server id
2187 * @return ?int One of Post\DeliveryData protocol constants or null if unknown or gserver is missing
2190 public static function getProtocol(int $gsid): ?int
2196 $gserver = DBA::selectFirst('gserver', ['protocol'], ['id' => $gsid]);
2197 if (DBA::isResult($gserver)) {
2198 return $gserver['protocol'];
2205 * Enforces gserver table field maximum sizes to avoid "Data too long" database errors
2207 * @param array $fields
2208 * @param array $condition
2212 public static function update(array $fields, array $condition): bool
2214 $fields = DI::dbaDefinition()->truncateFieldsForTable('gserver', $fields);
2216 return DBA::update('gserver', $fields, $condition);