4 * @file src/Model/GServer.php
5 * @brief This file includes the GServer class to handle with servers
7 namespace Friendica\Model;
11 use Friendica\Core\Config;
12 use Friendica\Core\Protocol;
13 use Friendica\Database\DBA;
14 use Friendica\Module\Register;
15 use Friendica\Util\Network;
16 use Friendica\Util\DateTimeFormat;
17 use Friendica\Util\Strings;
18 use Friendica\Util\XML;
19 use Friendica\Core\Logger;
23 use Friendica\Core\System;
24 use Friendica\Core\Worker;
25 use Friendica\Network\Probe;
26 use Friendica\Protocol\PortableContact;
29 * @brief This class handles GServer related functions
36 * @param string $url Server url
37 * @param boolean $force Force update
39 * @return boolean 'true' if server could be detected
41 public static function detect($url, $force = false)
46 // - Pleroma version number
48 // $gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($url)]);
51 // When a nodeinfo is present, we don't need to dig further
52 $xrd_timeout = Config::get('system', 'xrd_timeout');
53 $curlResult = Network::curl($url . '/.well-known/nodeinfo', false, ['timeout' => $xrd_timeout]);
54 if ($curlResult->isTimeout()) {
55 DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($url)]);
59 $nodeinfo = self::fetchNodeinfo($url, $curlResult);
61 if (empty($nodeinfo) || ($nodeinfo['network'] == Protocol::DFRN)) {
62 // Fetch the landing page, possibly it reveals some data
63 $curlResult = Network::curl($url, false, ['timeout' => $xrd_timeout]);
64 if ($curlResult->isSuccess()) {
65 $serverdata = self::analyseRootHeader($curlResult, $serverdata);
66 $serverdata = self::analyseRootBody($curlResult, $serverdata);
69 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
70 DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($url)]);
74 if (empty($serverdata['network']) || ($serverdata['network'] == Protocol::DFRN)) {
75 $serverdata = self::detectFriendica($url, $serverdata);
78 if (empty($serverdata['network']) || ($serverdata['network'] == Protocol::ACTIVITYPUB)) {
79 $serverdata = self::detectMastodonAlikes($url, $serverdata);
82 // the "siteinfo.json" is some specific endpoint of Hubzilla and Red
83 if (empty($serverdata['network']) || ($serverdata['network'] == Protocol::ZOT)) {
84 $serverdata = self::fetchSiteinfo($url, $serverdata);
87 if (empty($serverdata['network'])) {
88 $serverdata = self::detectNextcloud($url, $serverdata);
91 if (empty($serverdata['network'])) {
92 $serverdata = self::detectGNUSocial($url, $serverdata);
95 $serverdata = $nodeinfo;
98 // We can't detect the network type. Possibly it is some system that we don't know yet
99 if (empty($serverdata['network'])) {
100 $serverdata['network'] = Protocol::PHANTOM;
103 $serverdata['url'] = $url;
104 $serverdata['nurl'] = Strings::normaliseLink($url);
106 // When we don't have the registered users, we simply count what we know
107 if (empty($serverdata['registered-users'])) {
108 $gcontacts = DBA::count('gcontact', ['server_url' => [$url, $serverdata['nurl']]]);
109 $apcontacts = DBA::count('apcontact', ['baseurl' => [$url, $serverdata['nurl']]]);
110 $contacts = DBA::count('contact', ['uid' => 0, 'baseurl' => [$url, $serverdata['nurl']]]);
111 $serverdata['registered-users'] = max($gcontacts, $apcontacts, $contacts);
114 $fields = array_keys($serverdata);
115 $old_data = DBA::selectFirst('gserver', $fields, ['nurl' => Strings::normaliseLink($url)]);
116 if (!DBA::isResult($old_data)) {
118 $serverdata['created'] = DateTimeFormat::utcNow();
119 DBA::insert('gserver', $serverdata);
121 $serverdata['last_contact'] = DateTimeFormat::utcNow();
122 DBA::update('gserver', $serverdata, ['nurl' => $serverdata['nurl']], $old_data);
129 * @brief Detect server type by using the nodeinfo data
131 * @param string $url address of the server
132 * @return array Server data
133 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
135 private static function fetchNodeinfo($url, $curlResult)
137 $nodeinfo = json_decode($curlResult->getBody(), true);
139 if (!is_array($nodeinfo) || !isset($nodeinfo['links'])) {
146 foreach ($nodeinfo['links'] as $link) {
147 if (!is_array($link) || empty($link['rel']) || empty($link['href'])) {
148 Logger::info('Invalid nodeinfo format', ['url' => $url]);
151 if ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/1.0') {
152 $nodeinfo1_url = $link['href'];
153 } elseif ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/2.0') {
154 $nodeinfo2_url = $link['href'];
158 if ($nodeinfo1_url . $nodeinfo2_url == '') {
164 // When the nodeinfo url isn't on the same host, then there is obviously something wrong
165 if (!empty($nodeinfo2_url) && (parse_url($url, PHP_URL_HOST) == parse_url($nodeinfo2_url, PHP_URL_HOST))) {
166 $server = self::parseNodeinfo2($nodeinfo2_url);
169 // When the nodeinfo url isn't on the same host, then there is obviously something wrong
170 if (empty($server) && !empty($nodeinfo1_url) && (parse_url($url, PHP_URL_HOST) == parse_url($nodeinfo1_url, PHP_URL_HOST))) {
171 $server = self::parseNodeinfo1($nodeinfo1_url);
178 * @brief Parses Nodeinfo 1
180 * @param string $nodeinfo_url address of the nodeinfo path
181 * @return array Server data
182 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
184 private static function parseNodeinfo1($nodeinfo_url)
186 $curlResult = Network::curl($nodeinfo_url);
188 if (!$curlResult->isSuccess()) {
192 $nodeinfo = json_decode($curlResult->getBody(), true);
194 if (!is_array($nodeinfo)) {
200 $server['register_policy'] = Register::CLOSED;
202 if (!empty($nodeinfo['openRegistrations'])) {
203 $server['register_policy'] = Register::OPEN;
206 if (is_array($nodeinfo['software'])) {
207 if (isset($nodeinfo['software']['name'])) {
208 $server['platform'] = $nodeinfo['software']['name'];
211 if (isset($nodeinfo['software']['version'])) {
212 $server['version'] = $nodeinfo['software']['version'];
213 // Version numbers on Nodeinfo are presented with additional info, e.g.:
214 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
215 $server['version'] = preg_replace('=(.+)-(.{4,})=ism', '$1', $server['version']);
219 if (isset($nodeinfo['metadata']['nodeName'])) {
220 $server['site_name'] = $nodeinfo['metadata']['nodeName'];
223 if (!empty($nodeinfo['usage']['users']['total'])) {
224 $server['registered-users'] = $nodeinfo['usage']['users']['total'];
227 if (!empty($nodeinfo['protocols']['inbound']) && is_array($nodeinfo['protocols']['inbound'])) {
229 foreach ($nodeinfo['protocols']['inbound'] as $protocol) {
230 $protocols[$protocol] = true;
233 if (!empty($protocols['friendica'])) {
234 $server['network'] = Protocol::DFRN;
235 } elseif (!empty($protocols['activitypub'])) {
236 $server['network'] = Protocol::ACTIVITYPUB;
237 } elseif (!empty($protocols['diaspora'])) {
238 $server['network'] = Protocol::DIASPORA;
239 } elseif (!empty($protocols['ostatus'])) {
240 $server['network'] = Protocol::OSTATUS;
241 } elseif (!empty($protocols['gnusocial'])) {
242 $server['network'] = Protocol::OSTATUS;
257 * @brief Parses Nodeinfo 2
259 * @param string $nodeinfo_url address of the nodeinfo path
260 * @return array Server data
261 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
263 private static function parseNodeinfo2($nodeinfo_url)
265 $curlResult = Network::curl($nodeinfo_url);
266 if (!$curlResult->isSuccess()) {
270 $nodeinfo = json_decode($curlResult->getBody(), true);
272 if (!is_array($nodeinfo)) {
278 $server['register_policy'] = Register::CLOSED;
280 if (!empty($nodeinfo['openRegistrations'])) {
281 $server['register_policy'] = Register::OPEN;
284 if (is_array($nodeinfo['software'])) {
285 if (isset($nodeinfo['software']['name'])) {
286 $server['platform'] = $nodeinfo['software']['name'];
289 if (isset($nodeinfo['software']['version'])) {
290 $server['version'] = $nodeinfo['software']['version'];
291 // Version numbers on Nodeinfo are presented with additional info, e.g.:
292 // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
293 $server['version'] = preg_replace('=(.+)-(.{4,})=ism', '$1', $server['version']);
297 if (isset($nodeinfo['metadata']['nodeName'])) {
298 $server['site_name'] = $nodeinfo['metadata']['nodeName'];
301 if (!empty($nodeinfo['usage']['users']['total'])) {
302 $server['registered-users'] = $nodeinfo['usage']['users']['total'];
305 if (!empty($nodeinfo['protocols'])) {
307 foreach ($nodeinfo['protocols'] as $protocol) {
308 $protocols[$protocol] = true;
311 if (!empty($protocols['friendica'])) {
312 $server['network'] = Protocol::DFRN;
313 } elseif (!empty($protocols['activitypub'])) {
314 $server['network'] = Protocol::ACTIVITYPUB;
315 } elseif (!empty($protocols['diaspora'])) {
316 $server['network'] = Protocol::DIASPORA;
317 } elseif (!empty($protocols['ostatus'])) {
318 $server['network'] = Protocol::OSTATUS;
319 } elseif (!empty($protocols['gnusocial'])) {
320 $server['network'] = Protocol::OSTATUS;
327 if (empty($server)) {
334 private static function fetchSiteinfo($url, $serverdata)
336 $curlResult = Network::curl($url . '/siteinfo.json');
337 if (!$curlResult->isSuccess()) {
341 $data = json_decode($curlResult->getBody(), true);
346 if (isset($data['url'])) {
347 $serverdata['platform'] = $data['platform'];
348 $serverdata['version'] = $data['version'];
351 if (!empty($data['plugins'])) {
352 if (in_array('pubcrawl', $data['plugins'])) {
353 $serverdata['network'] = Protocol::ACTIVITYPUB;
354 } elseif (in_array('diaspora', $data['plugins'])) {
355 $serverdata['network'] = Protocol::DIASPORA;
356 } elseif (in_array('gnusoc', $data['plugins'])) {
357 $serverdata['network'] = Protocol::OSTATUS;
359 $serverdata['network'] = Protocol::ZOT;
363 if (!empty($data['site_name'])) {
364 $serverdata['site_name'] = $data['site_name'];
367 if (!empty($data['channels_total'])) {
368 $serverdata['registered-users'] = $data['channels_total'];
371 if (!empty($data['register_policy'])) {
372 switch ($data['register_policy']) {
373 case "REGISTER_OPEN":
374 $serverdata['register_policy'] = Register::OPEN;
377 case "REGISTER_APPROVE":
378 $serverdata['register_policy'] = Register::APPROVE;
381 case "REGISTER_CLOSED":
383 $serverdata['register_policy'] = Register::CLOSED;
391 private static function detectNextcloud($url, $serverdata)
393 $curlResult = Network::curl($url . '/status.php');
395 if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
399 $data = json_decode($curlResult->getBody(), true);
404 if (!empty($data['version'])) {
405 $serverdata['platform'] = 'nextcloud';
406 $serverdata['version'] = $data['version'];
407 $serverdata['network'] = Protocol::ACTIVITYPUB;
413 private static function detectMastodonAlikes($url, $serverdata)
415 $curlResult = Network::curl($url . '/api/v1/instance');
417 if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
421 $data = json_decode($curlResult->getBody(), true);
426 if (isset($data['version'])) {
427 $serverdata['platform'] = 'mastodon';
428 $serverdata['version'] = defaults($data, 'version', '');
429 if (!empty($data['title'])) {
430 $serverdata['site_name'] = $data['title'];
432 if (!empty($data['description'])) {
433 $serverdata['info'] = trim($data['description']);
435 $serverdata['network'] = Protocol::ACTIVITYPUB;
438 if (!empty($data['stats']['user_count'])) {
439 $serverdata['registered-users'] = $data['stats']['user_count'];
442 if (strstr($serverdata['version'], 'Pleroma')) {
443 $serverdata['platform'] = 'pleroma';
444 $serverdata['version'] = trim(str_replace('Pleroma', '', $serverdata['version'])); // 2.7.2 (compatible; Pleroma 1.0.0-1225-gf31ad554-develop)
447 if (strstr($serverdata['version'], 'Pixelfed')) {
448 print_r($serverdata);
450 // $serverdata['platform'] = 'pixelfed';
451 // $serverdata['version'] = trim(str_replace('Pixelfed', '', $serverdata['version'])); // 2.7.2 (compatible; Pixelfed 0.10.5)
457 private static function detectGNUSocial($url, $serverdata)
459 $curlResult = Network::curl($url . '/api/statusnet/version.json');
461 if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') &&
462 ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) {
463 $serverdata['platform'] = 'StatusNet';
464 // Remove junk that some GNU Social servers return
465 $serverdata['version'] = str_replace(chr(239).chr(187).chr(191), '', $curlResult->getBody());
466 $serverdata['version'] = trim($serverdata['version'], '"');
467 $serverdata['network'] = Protocol::OSTATUS;
470 // Test for GNU Social
471 $curlResult = Network::curl($url . '/api/gnusocial/version.json');
473 if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') &&
474 ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) {
475 $serverdata['platform'] = 'GNU Social';
476 // Remove junk that some GNU Social servers return
477 $serverdata['version'] = str_replace(chr(239) . chr(187) . chr(191), '', $curlResult->getBody());
478 $serverdata['version'] = trim($serverdata['version'], '"');
479 $serverdata['network'] = Protocol::OSTATUS;
485 private static function detectFriendica($url, $serverdata)
487 $curlResult = Network::curl($url . '/friendica/json');
488 if (!$curlResult->isSuccess()) {
489 $curlResult = Network::curl($url . '/friendika/json');
492 if (!$curlResult->isSuccess()) {
496 $data = json_decode($curlResult->getBody(), true);
497 if (empty($data) || empty($data['version'])) {
501 $serverdata['network'] = Protocol::DFRN;
502 $serverdata['version'] = $data['version'];
504 if (!empty($data['no_scrape_url'])) {
505 $serverdata['noscrape'] = $data['no_scrape_url'];
508 if (!empty($data['site_name'])) {
509 $serverdata['site_name'] = $data['site_name'];
512 if (!empty($data['info'])) {
513 $serverdata['info'] = trim($data['info']);
516 $register_policy = defaults($data, 'register_policy', 'REGISTER_CLOSED');
517 switch ($register_policy) {
518 case 'REGISTER_OPEN':
519 $serverdata['register_policy'] = Register::OPEN;
522 case 'REGISTER_APPROVE':
523 $serverdata['register_policy'] = Register::APPROVE;
526 case 'REGISTER_CLOSED':
527 case 'REGISTER_INVITATION':
528 $serverdata['register_policy'] = Register::CLOSED;
531 Logger::info('Register policy is invalid', ['policy' => $register_policy, 'server' => $url]);
532 $serverdata['register_policy'] = Register::CLOSED;
536 $serverdata['platform'] = defaults($data, 'platform', '');
541 private static function analyseRootBody($curlResult, $serverdata)
543 $doc = new DOMDocument();
544 @$doc->loadHTML($curlResult->getBody());
545 $xpath = new DOMXPath($doc);
547 $title = trim(XML::getFirstNodeValue($xpath, '//head/title/text()'));
548 if (!empty($title)) {
549 $serverdata['site_name'] = $title;
552 $list = $xpath->query('//meta[@name]');
554 foreach ($list as $node) {
556 if ($node->attributes->length) {
557 foreach ($node->attributes as $attribute) {
558 $attribute->value = trim($attribute->value);
559 if (empty($attribute->value)) {
563 $attr[$attribute->name] = $attribute->value;
566 if (empty($attr['name']) || empty($attr['content'])) {
571 if ($attr['name'] == 'description') {
572 $serverdata['info'] = $attr['content'];
575 if ($attr['name'] == 'application-name') {
576 $serverdata['platform'] = $attr['content'];
577 if (in_array($attr['content'], ['Misskey', 'Write.as'])) {
578 $serverdata['network'] = Protocol::ACTIVITYPUB;
582 if (($attr['name'] == 'generator') && in_array($attr['content'], ['Write.as'])) {
584 // $serverdata['platform'] = $attr['content'];
585 // $serverdata['network'] = Protocol::ACTIVITYPUB;
586 } elseif ($attr['name'] == 'generator') {
587 $serverdata['platform'] = $attr['content'];
589 $version_part = explode(' ', $attr['content']);
591 if (count($version_part) == 3) {
592 if (($version_part[0] == 'Red') && ($version_part[1] == 'Matrix')) {
593 // $serverdata['platform'] = $version_part[0] . ' ' . $version_part[1];
594 // $serverdata['version'] = $version_part[2];
595 // $serverdata['network'] = Protocol::DIASPORA;
597 } elseif (count($version_part) == 2) {
598 if (in_array($version_part[0], ['WordPress'])) {
599 $serverdata['platform'] = $version_part[0];
600 $serverdata['version'] = $version_part[1];
601 $serverdata['network'] = Protocol::ACTIVITYPUB;
603 if (in_array($version_part[0], ['Friendika', 'Friendica'])) {
604 $serverdata['platform'] = $version_part[0];
605 $serverdata['version'] = $version_part[1];
606 $serverdata['network'] = Protocol::DFRN;
612 $list = $xpath->query('//meta[@property]');
614 foreach ($list as $node) {
616 if ($node->attributes->length) {
617 foreach ($node->attributes as $attribute) {
618 $attribute->value = trim($attribute->value);
619 if (empty($attribute->value)) {
623 $attr[$attribute->name] = $attribute->value;
626 if (empty($attr['property']) || empty($attr['content'])) {
632 if ($attr['property'] == 'og:site_name') {
633 $serverdata['site_name'] = $attr['content'];
636 if ($attr['property'] == 'og:description') {
637 $serverdata['info'] = $attr['content'];
640 // if (($attr['property'] == 'og:title') && in_array($attr['content'], ['pixelfed', 'Socialhome'])) {
641 // if (($attr['property'] == 'og:title') && in_array($attr['content'], ['Nextcloud'])) {
642 // $serverdata['platform'] = $attr['content'];
643 // $serverdata['network'] = Protocol::ACTIVITYPUB;
646 if ($attr['property'] == 'og:platform') {
647 $serverdata['platform'] = $attr['content'];
649 if (in_array($attr['content'], ['PeerTube'])) {
650 $serverdata['network'] = Protocol::ACTIVITYPUB;
654 if ($attr['property'] == 'generator') {
655 $serverdata['platform'] = $attr['content'];
657 if (in_array($attr['content'], ['hubzilla'])) {
658 // We later check which compatible protocol modules are loaded.
659 $serverdata['network'] = Protocol::ZOT;
667 private static function analyseRootHeader($curlResult, $serverdata)
669 if ($curlResult->getHeader('server') == 'Mastodon') {
670 $serverdata['platform'] = 'mastodon';
671 $serverdata['network'] = $network = Protocol::ACTIVITYPUB;
672 } elseif ($curlResult->inHeader('x-diaspora-version')) {
673 $serverdata['platform'] = 'diaspora';
674 $serverdata['network'] = $network = Protocol::DIASPORA;
675 $serverdata['version'] = $curlResult->getHeader('x-diaspora-version');
677 } elseif ($curlResult->inHeader('x-friendica-version')) {
678 $serverdata['platform'] = 'friendica';
679 $serverdata['network'] = $network = Protocol::DFRN;
680 $serverdata['version'] = $curlResult->getHeader('x-friendica-version');
683 //print_r($curlResult->getHeaderArray());