From 9cbe5c5e0ef41030cd022eb314a07d236c767818 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 3 Oct 2019 09:20:36 +0000 Subject: [PATCH 01/13] New class for handling gserver entries --- src/Model/GServer.php | 665 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 665 insertions(+) create mode 100644 src/Model/GServer.php diff --git a/src/Model/GServer.php b/src/Model/GServer.php new file mode 100644 index 0000000000..352ec75307 --- /dev/null +++ b/src/Model/GServer.php @@ -0,0 +1,665 @@ + Strings::normaliseLink($url)]); + $serverdata = []; + + // When a nodeinfo is present, we don't need to dig further + $xrd_timeout = Config::get('system', 'xrd_timeout'); + $curlResult = Network::curl($url . '/.well-known/nodeinfo', false, ['timeout' => $xrd_timeout]); + if ($curlResult->isTimeout()) { + DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($url)]); + return false; + } + + $nodeinfo = self::fetchNodeinfo($url, $curlResult); + + if (empty($nodeinfo) || ($nodeinfo['network'] == Protocol::DFRN)) { + // Fetch the landing page, possibly it reveals some data + $curlResult = Network::curl($url, false, ['timeout' => $xrd_timeout]); + if ($curlResult->isSuccess()) { + $serverdata = self::analyseRootHeader($curlResult, $serverdata); + $serverdata = self::analyseRootBody($curlResult, $serverdata); + } + + if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { + DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($url)]); + return false; + } + + if (empty($serverdata['network']) || ($serverdata['network'] == Protocol::DFRN)) { + $serverdata = self::detectFriendica($url, $serverdata); + } + + if (empty($serverdata['network']) || ($serverdata['network'] == Protocol::ACTIVITYPUB)) { + $serverdata = self::detectMastodonAlikes($url, $serverdata); + } + + // the "siteinfo.json" is some specific endpoint of Hubzilla and Red + if (empty($serverdata['network']) || (($serverdata['network'] == Protocol::DIASPORA) && ($serverdata['platform'] != 'diaspora'))) { + $serverdata = self::fetchSiteinfo($url, $serverdata); + } + + if (empty($serverdata['network'])) { + $serverdata = self::detectNextcloud($url, $serverdata); + } + + if (empty($serverdata['network'])) { + $serverdata = self::detectGNUSocial($url, $serverdata); + } + } else { + $serverdata = $nodeinfo; + } + + // We can't detect the network type. Possibly it is some system that we don't know yet + if (empty($serverdata['network'])) { + $serverdata['network'] = Protocol::PHANTOM; + } + + $serverdata['url'] = $url; + $serverdata['nurl'] = Strings::normaliseLink($url); + + $fields = array_keys($serverdata); + $old_data = DBA::selectFirst('gserver', $fields, ['nurl' => Strings::normaliseLink($url)]); + if (!DBA::isResult($old_data)) { +die('Möööp'); + $serverdata['created'] = DateTimeFormat::utcNow(); + DBA::insert('gserver', $serverdata); + } else { + $serverdata['last_contact'] = DateTimeFormat::utcNow(); + DBA::update('gserver', $serverdata, ['nurl' => $serverdata['nurl']], $old_data); + } + + return $serverdata; + } + + /** + * @brief Detect server type by using the nodeinfo data + * + * @param string $url address of the server + * @return array Server data + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + private static function fetchNodeinfo($url, $curlResult) + { + $nodeinfo = json_decode($curlResult->getBody(), true); + + if (!is_array($nodeinfo) || !isset($nodeinfo['links'])) { + return []; + } + + $nodeinfo1_url = ''; + $nodeinfo2_url = ''; + + foreach ($nodeinfo['links'] as $link) { + if (!is_array($link) || empty($link['rel']) || empty($link['href'])) { + Logger::info('Invalid nodeinfo format', ['url' => $url]); + continue; + } + if ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/1.0') { + $nodeinfo1_url = $link['href']; + } elseif ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/2.0') { + $nodeinfo2_url = $link['href']; + } + } + + if ($nodeinfo1_url . $nodeinfo2_url == '') { + return []; + } + + $server = []; + + // When the nodeinfo url isn't on the same host, then there is obviously something wrong + if (!empty($nodeinfo2_url) && (parse_url($url, PHP_URL_HOST) == parse_url($nodeinfo2_url, PHP_URL_HOST))) { + $server = self::parseNodeinfo2($nodeinfo2_url); + } + + // When the nodeinfo url isn't on the same host, then there is obviously something wrong + if (empty($server) && !empty($nodeinfo1_url) && (parse_url($url, PHP_URL_HOST) == parse_url($nodeinfo1_url, PHP_URL_HOST))) { + $server = self::parseNodeinfo1($nodeinfo1_url); + } + + return $server; + } + + /** + * @brief Parses Nodeinfo 1 + * + * @param string $nodeinfo_url address of the nodeinfo path + * @return array Server data + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + private static function parseNodeinfo1($nodeinfo_url) + { + $curlResult = Network::curl($nodeinfo_url); + + if (!$curlResult->isSuccess()) { + return false; + } + + $nodeinfo = json_decode($curlResult->getBody(), true); + + if (!is_array($nodeinfo)) { + return false; + } + + $server = []; + + $server['register_policy'] = Register::CLOSED; + + if (!empty($nodeinfo['openRegistrations'])) { + $server['register_policy'] = Register::OPEN; + } + + if (is_array($nodeinfo['software'])) { + if (isset($nodeinfo['software']['name'])) { + $server['platform'] = $nodeinfo['software']['name']; + } + + if (isset($nodeinfo['software']['version'])) { + $server['version'] = $nodeinfo['software']['version']; + // Version numbers on Nodeinfo are presented with additional info, e.g.: + // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191. + $server['version'] = preg_replace('=(.+)-(.{4,})=ism', '$1', $server['version']); + } + } + + if (isset($nodeinfo['metadata']['nodeName'])) { + $server['site_name'] = $nodeinfo['metadata']['nodeName']; + } + + if (!empty($nodeinfo['usage']['users']['total'])) { + $server['registered-users'] = $nodeinfo['usage']['users']['total']; + } + + if (!empty($nodeinfo['protocols']['inbound']) && is_array($nodeinfo['protocols']['inbound'])) { + $protocols = []; + foreach ($nodeinfo['protocols']['inbound'] as $protocol) { + $protocols[$protocol] = true; + } + + if (!empty($protocols['friendica'])) { + $server['network'] = Protocol::DFRN; + } elseif (!empty($protocols['activitypub'])) { + $server['network'] = Protocol::ACTIVITYPUB; + } elseif (!empty($protocols['diaspora'])) { + $server['network'] = Protocol::DIASPORA; + } elseif (!empty($protocols['ostatus'])) { + $server['network'] = Protocol::OSTATUS; + } elseif (!empty($protocols['gnusocial'])) { + $server['network'] = Protocol::OSTATUS; + } else { + print_r($protocols); + die('Protocol 1'); + } + } + + if (!$server) { + return false; + } + + return $server; + } + + /** + * @brief Parses Nodeinfo 2 + * + * @param string $nodeinfo_url address of the nodeinfo path + * @return array Server data + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + private static function parseNodeinfo2($nodeinfo_url) + { + $curlResult = Network::curl($nodeinfo_url); + if (!$curlResult->isSuccess()) { + return false; + } + + $nodeinfo = json_decode($curlResult->getBody(), true); + + if (!is_array($nodeinfo)) { + return false; + } + + $server = []; + + $server['register_policy'] = Register::CLOSED; + + if (!empty($nodeinfo['openRegistrations'])) { + $server['register_policy'] = Register::OPEN; + } + + if (is_array($nodeinfo['software'])) { + if (isset($nodeinfo['software']['name'])) { + $server['platform'] = $nodeinfo['software']['name']; + } + + if (isset($nodeinfo['software']['version'])) { + $server['version'] = $nodeinfo['software']['version']; + // Version numbers on Nodeinfo are presented with additional info, e.g.: + // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191. + $server['version'] = preg_replace('=(.+)-(.{4,})=ism', '$1', $server['version']); + } + } + + if (isset($nodeinfo['metadata']['nodeName'])) { + $server['site_name'] = $nodeinfo['metadata']['nodeName']; + } + + if (!empty($nodeinfo['usage']['users']['total'])) { + $server['registered-users'] = $nodeinfo['usage']['users']['total']; + } + + if (!empty($nodeinfo['protocols'])) { + $protocols = []; + foreach ($nodeinfo['protocols'] as $protocol) { + $protocols[$protocol] = true; + } + + if (!empty($protocols['friendica'])) { + $server['network'] = Protocol::DFRN; + } elseif (!empty($protocols['activitypub'])) { + $server['network'] = Protocol::ACTIVITYPUB; + } elseif (!empty($protocols['diaspora'])) { + $server['network'] = Protocol::DIASPORA; + } elseif (!empty($protocols['ostatus'])) { + $server['network'] = Protocol::OSTATUS; + } elseif (!empty($protocols['gnusocial'])) { + $server['network'] = Protocol::OSTATUS; + } else { + print_r($protocols); + die('Protocol 2'); + } + } + + if (empty($server)) { + return false; + } + + return $server; + } + + private static function fetchSiteinfo($url, $serverdata) + { + $curlResult = Network::curl($url . '/siteinfo.json'); + if (!$curlResult->isSuccess()) { + return $serverdata; + } + + $data = json_decode($curlResult->getBody(), true); + if (empty($data)) { + return $serverdata; + } + + if (isset($data['url'])) { + $serverdata['platform'] = $data['platform']; + $serverdata['version'] = $data['version']; + } + + if (!empty($data['plugins'])) { + if (in_array('pubcrawl', $data['plugins'])) { + $serverdata['network'] = Protocol::ACTIVITYPUB; + } elseif (in_array('diaspora', $data['plugins'])) { + $serverdata['network'] = Protocol::DIASPORA; + } elseif (in_array('gnusoc', $data['plugins'])) { + $serverdata['network'] = Protocol::OSTATUS; + } else { + $serverdata['network'] = Protocol::ZOT; + } + } + + if (!empty($data['site_name'])) { + $serverdata['site_name'] = $data['site_name']; + } + + if (!empty($data['channels_total'])) { + $serverdata['registered-users'] = $data['channels_total']; + } + + if (!empty($data['register_policy'])) { + switch ($data['register_policy']) { + case "REGISTER_OPEN": + $serverdata['register_policy'] = Register::OPEN; + break; + + case "REGISTER_APPROVE": + $serverdata['register_policy'] = Register::APPROVE; + break; + + case "REGISTER_CLOSED": + default: + $serverdata['register_policy'] = Register::CLOSED; + break; + } + } + + return $serverdata; + } + + private static function detectNextcloud($url, $serverdata) + { + $curlResult = Network::curl($url . '/status.php'); + + if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { + return $serverdata; + } + + $data = json_decode($curlResult->getBody(), true); + if (empty($data)) { + return $serverdata; + } + + if (!empty($data['version'])) { + $serverdata['platform'] = 'nextcloud'; + $serverdata['version'] = $data['version']; + $serverdata['network'] = Protocol::ACTIVITYPUB; + } + + return $serverdata; + } + + private static function detectMastodonAlikes($url, $serverdata) + { + $curlResult = Network::curl($url . '/api/v1/instance'); + + if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { + return $serverdata; + } + + $data = json_decode($curlResult->getBody(), true); + if (empty($data)) { + return $serverdata; + } + + if (isset($data['version'])) { + $serverdata['platform'] = 'mastodon'; + $serverdata['version'] = defaults($data, 'version', ''); + if (!empty($data['title'])) { + $serverdata['site_name'] = $data['title']; + } + if (!empty($data['description'])) { + $serverdata['info'] = trim($data['description']); + } + $serverdata['network'] = Protocol::ACTIVITYPUB; + } + + if (!empty($data['stats']['user_count'])) { + $serverdata['registered-users'] = $data['stats']['user_count']; + } + + if (strstr($serverdata['version'], 'Pleroma')) { + $serverdata['platform'] = 'pleroma'; + $serverdata['version'] = trim(str_replace('Pleroma', '', $serverdata['version'])); // 2.7.2 (compatible; Pleroma 1.0.0-1225-gf31ad554-develop) + } + + if (strstr($serverdata['version'], 'Pixelfed')) { + print_r($serverdata); + die(); +// $serverdata['platform'] = 'pixelfed'; +// $serverdata['version'] = trim(str_replace('Pixelfed', '', $serverdata['version'])); // 2.7.2 (compatible; Pixelfed 0.10.5) + } + + return $serverdata; + } + + private static function detectGNUSocial($url, $serverdata) + { + $curlResult = Network::curl($url . '/api/statusnet/version.json'); + + if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') && + ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) { + $serverdata['platform'] = 'StatusNet'; + // Remove junk that some GNU Social servers return + $serverdata['version'] = str_replace(chr(239).chr(187).chr(191), '', $curlResult->getBody()); + $serverdata['version'] = trim($serverdata['version'], '"'); + $serverdata['network'] = Protocol::OSTATUS; + } + + // Test for GNU Social + $curlResult = Network::curl($url . '/api/gnusocial/version.json'); + + if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') && + ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) { + $serverdata['platform'] = 'GNU Social'; + // Remove junk that some GNU Social servers return + $serverdata['version'] = str_replace(chr(239) . chr(187) . chr(191), '', $curlResult->getBody()); + $serverdata['version'] = trim($serverdata['version'], '"'); + $serverdata['network'] = Protocol::OSTATUS; + } + + return $serverdata; + } + + private static function detectFriendica($url, $serverdata) + { + $curlResult = Network::curl($url . '/friendica/json'); + if (!$curlResult->isSuccess()) { + $curlResult = Network::curl($url . '/friendika/json'); + } + + if (!$curlResult->isSuccess()) { + return $serverdata; + } + + $data = json_decode($curlResult->getBody(), true); + if (empty($data) || empty($data['version'])) { + return $serverdata; + } + + $serverdata['network'] = Protocol::DFRN; + $serverdata['version'] = $data['version']; + + if (!empty($data['no_scrape_url'])) { + $serverdata['noscrape'] = $data['no_scrape_url']; + } + + if (!empty($data['site_name'])) { + $serverdata['site_name'] = $data['site_name']; + } + + if (!empty($data['info'])) { + $serverdata['info'] = trim($data['info']); + } + + $register_policy = defaults($data, 'register_policy', 'REGISTER_CLOSED'); + switch ($register_policy) { + case 'REGISTER_OPEN': + $serverdata['register_policy'] = Register::OPEN; + break; + + case 'REGISTER_APPROVE': + $serverdata['register_policy'] = Register::APPROVE; + break; + + case 'REGISTER_CLOSED': + case 'REGISTER_INVITATION': + $serverdata['register_policy'] = Register::CLOSED; + break; + default: + Logger::info('Register policy is invalid', ['policy' => $register_policy, 'server' => $url]); + $serverdata['register_policy'] = Register::CLOSED; + break; + } + + $serverdata['platform'] = defaults($data, 'platform', ''); + + return $serverdata; + } + + private static function analyseRootBody($curlResult, $serverdata) + { + $doc = new DOMDocument(); + @$doc->loadHTML($curlResult->getBody()); + $xpath = new DOMXPath($doc); + + $title = trim(XML::getFirstNodeValue($xpath, '//head/title/text()')); + if (!empty($title)) { + $serverdata['site_name'] = $title; + } + + $list = $xpath->query('//meta[@name]'); + + foreach ($list as $node) { + $attr = []; + if ($node->attributes->length) { + foreach ($node->attributes as $attribute) { + $attribute->value = trim($attribute->value); + if (empty($attribute->value)) { + continue; + } + + $attr[$attribute->name] = $attribute->value; + } + } +//print_r($attr); + if ($attr['name'] == 'description') { + $serverdata['info'] = $attr['content']; + } + + if ($attr['name'] == 'application-name') { + $serverdata['platform'] = $attr['content']; + if (in_array($attr['content'], ['Misskey', 'Write.as'])) { + $serverdata['network'] = Protocol::ACTIVITYPUB; + } + } +// + if (($attr['name'] == 'generator') && in_array($attr['content'], ['Write.as'])) { +die('as'); +// $serverdata['platform'] = $attr['content']; +// $serverdata['network'] = Protocol::ACTIVITYPUB; + } elseif ($attr['name'] == 'generator') { + $serverdata['platform'] = $attr['content']; + + $version_part = explode(' ', $attr['content']); + + if (count($version_part) == 3) { + if (($version_part[0] == 'Red') && ($version_part[1] == 'Matrix')) { +// $serverdata['platform'] = $version_part[0] . ' ' . $version_part[1]; +// $serverdata['version'] = $version_part[2]; +// $serverdata['network'] = Protocol::DIASPORA; + } + } elseif (count($version_part) == 2) { + if (in_array($version_part[0], ['WordPress'])) { + $serverdata['platform'] = $version_part[0]; + $serverdata['version'] = $version_part[1]; + $serverdata['network'] = Protocol::ACTIVITYPUB; + } + if (in_array($version_part[0], ['Friendika', 'Friendica'])) { + $serverdata['platform'] = $version_part[0]; + $serverdata['version'] = $version_part[1]; + $serverdata['network'] = Protocol::DFRN; + } + } + } + } + + $list = $xpath->query('//meta[@property]'); + + foreach ($list as $node) { + $attr = []; + if ($node->attributes->length) { + foreach ($node->attributes as $attribute) { + $attribute->value = trim($attribute->value); + if (empty($attribute->value)) { + continue; + } + + $attr[$attribute->name] = $attribute->value; + } + } +//print_r($attr); + + if ($attr['property'] == 'og:site_name') { + $serverdata['site_name'] = $attr['content']; + } + + if ($attr['property'] == 'og:description') { + $serverdata['info'] = $attr['content']; + } + +// if (($attr['property'] == 'og:title') && in_array($attr['content'], ['pixelfed', 'Socialhome'])) { +// if (($attr['property'] == 'og:title') && in_array($attr['content'], ['Nextcloud'])) { +// $serverdata['platform'] = $attr['content']; +// $serverdata['network'] = Protocol::ACTIVITYPUB; +// } + + if (($attr['property'] == 'og:platform') && in_array($attr['content'], ['PeerTube'])) { +die('Peertube'); +// $serverdata['platform'] = $attr['content']; +// $serverdata['network'] = Protocol::ACTIVITYPUB; + } + + if (($attr['property'] == 'generator') && in_array($attr['content'], ['hubzilla'])) { + $serverdata['platform'] = $attr['content']; + $serverdata['network'] = Protocol::DIASPORA; + } + } + + return $serverdata; + } + + private static function analyseRootHeader($curlResult, $serverdata) + { + if ($curlResult->getHeader('server') == 'Mastodon') { + $serverdata['platform'] = 'mastodon'; + $serverdata['network'] = $network = Protocol::ACTIVITYPUB; + } elseif ($curlResult->inHeader('x-diaspora-version')) { + $serverdata['platform'] = 'diaspora'; + $serverdata['network'] = $network = Protocol::DIASPORA; + $serverdata['version'] = $curlResult->getHeader('x-diaspora-version'); + + } elseif ($curlResult->inHeader('x-friendica-version')) { + $serverdata['platform'] = 'friendica'; + $serverdata['network'] = $network = Protocol::DFRN; + $serverdata['version'] = $curlResult->getHeader('x-friendica-version'); + + } else { +//print_r($curlResult->getHeaderArray()); + } + return $serverdata; + } +} From 25f93cb03ae44b4d4498a035c923b482c8ad10db Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 3 Oct 2019 10:44:29 +0000 Subject: [PATCH 02/13] Count known users, check for empty attribute values --- src/Model/GServer.php | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/Model/GServer.php b/src/Model/GServer.php index 352ec75307..4eaee9dc4f 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -80,7 +80,7 @@ class GServer } // the "siteinfo.json" is some specific endpoint of Hubzilla and Red - if (empty($serverdata['network']) || (($serverdata['network'] == Protocol::DIASPORA) && ($serverdata['platform'] != 'diaspora'))) { + if (empty($serverdata['network']) || ($serverdata['network'] == Protocol::ZOT)) { $serverdata = self::fetchSiteinfo($url, $serverdata); } @@ -103,6 +103,14 @@ class GServer $serverdata['url'] = $url; $serverdata['nurl'] = Strings::normaliseLink($url); + // When we don't have the registered users, we simply count what we know + if (empty($serverdata['registered-users'])) { + $gcontacts = DBA::count('gcontact', ['server_url' => [$url, $serverdata['nurl']]]); + $apcontacts = DBA::count('apcontact', ['baseurl' => [$url, $serverdata['nurl']]]); + $contacts = DBA::count('contact', ['uid' => 0, 'baseurl' => [$url, $serverdata['nurl']]]); + $serverdata['registered-users'] = max($gcontacts, $apcontacts, $contacts); + } + $fields = array_keys($serverdata); $old_data = DBA::selectFirst('gserver', $fields, ['nurl' => Strings::normaliseLink($url)]); if (!DBA::isResult($old_data)) { @@ -554,6 +562,10 @@ die('Möööp'); $attr[$attribute->name] = $attribute->value; } + + if (empty($attr['name']) || empty($attr['content'])) { + continue; + } } //print_r($attr); if ($attr['name'] == 'description') { @@ -610,6 +622,10 @@ die('as'); $attr[$attribute->name] = $attribute->value; } + + if (empty($attr['property']) || empty($attr['content'])) { + continue; + } } //print_r($attr); @@ -627,15 +643,21 @@ die('as'); // $serverdata['network'] = Protocol::ACTIVITYPUB; // } - if (($attr['property'] == 'og:platform') && in_array($attr['content'], ['PeerTube'])) { -die('Peertube'); -// $serverdata['platform'] = $attr['content']; -// $serverdata['network'] = Protocol::ACTIVITYPUB; + if ($attr['property'] == 'og:platform') { + $serverdata['platform'] = $attr['content']; + + if (in_array($attr['content'], ['PeerTube'])) { + $serverdata['network'] = Protocol::ACTIVITYPUB; + } } - if (($attr['property'] == 'generator') && in_array($attr['content'], ['hubzilla'])) { + if ($attr['property'] == 'generator') { $serverdata['platform'] = $attr['content']; - $serverdata['network'] = Protocol::DIASPORA; + + if (in_array($attr['content'], ['hubzilla'])) { + // We later check which compatible protocol modules are loaded. + $serverdata['network'] = Protocol::ZOT; + } } } From ae55029028a880ad7dc96ce9e4a38310ed873c66 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 3 Oct 2019 13:02:48 +0000 Subject: [PATCH 03/13] Additional endpoints added --- src/Model/GServer.php | 190 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 176 insertions(+), 14 deletions(-) diff --git a/src/Model/GServer.php b/src/Model/GServer.php index 4eaee9dc4f..44bf304be8 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -42,7 +42,7 @@ class GServer { /// @Todo: // - Update Check - // - poco + // - poco endpoint // - Pleroma version number // $gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($url)]); @@ -58,6 +58,10 @@ class GServer $nodeinfo = self::fetchNodeinfo($url, $curlResult); + if (empty($nodeinfo)) { + $nodeinfo = self::fetchStatistics($url); + } + if (empty($nodeinfo) || ($nodeinfo['network'] == Protocol::DFRN)) { // Fetch the landing page, possibly it reveals some data $curlResult = Network::curl($url, false, ['timeout' => $xrd_timeout]); @@ -79,11 +83,15 @@ class GServer $serverdata = self::detectMastodonAlikes($url, $serverdata); } - // the "siteinfo.json" is some specific endpoint of Hubzilla and Red + // the 'siteinfo.json' is some specific endpoint of Hubzilla and Red if (empty($serverdata['network']) || ($serverdata['network'] == Protocol::ZOT)) { $serverdata = self::fetchSiteinfo($url, $serverdata); } + if (empty($serverdata['network'])) { + $serverdata = self::detectHubzilla($url, $serverdata); + } + if (empty($serverdata['network'])) { $serverdata = self::detectNextcloud($url, $serverdata); } @@ -125,6 +133,56 @@ die('Möööp'); return $serverdata; } + private static function fetchStatistics($url) + { + $curlResult = Network::curl($url . '/statistics.json'); + if (!$curlResult->isSuccess()) { + return []; + } + + $data = json_decode($curlResult->getBody(), true); + if (empty($data)) { + return []; + } + + $serverdata = []; + + if (!empty($data['version'])) { + $serverdata['version'] = $data['version']; + // Version numbers on statistics.json are presented with additional info, e.g.: + // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191. + $serverdata['version'] = preg_replace('=(.+)-(.{4,})=ism', '$1', $serverdata['version']); + } + + if (!empty($data['name'])) { + $serverdata['site_name'] = $data['name']; + } + + if (!empty($data['network'])) { + $serverdata['platform'] = $data['network']; + + if ($serverdata['platform'] == 'Diaspora') { + $serverdata['network'] = Protocol::DIASPORA; + } elseif ($serverdata['platform'] == 'Friendica') { + $serverdata['network'] = Protocol::DFRN; + } elseif ($serverdata['platform'] == 'redmatrix') { + $serverdata['network'] = Protocol::ZOT; + } else { + print_r($serverdata); + die('aaa'); + } + } + + + if (!empty($data['registrations_open'])) { + $serverdata['register_policy'] = Register::OPEN; + } else { + $serverdata['register_policy'] = Register::CLOSED; + } + + return $serverdata; + } + /** * @brief Detect server type by using the nodeinfo data * @@ -240,6 +298,8 @@ die('Möööp'); $server['network'] = Protocol::OSTATUS; } elseif (!empty($protocols['gnusocial'])) { $server['network'] = Protocol::OSTATUS; + } elseif (!empty($protocols['zot'])) { + $server['network'] = Protocol::ZOT; } else { print_r($protocols); die('Protocol 1'); @@ -318,6 +378,8 @@ die('Möööp'); $server['network'] = Protocol::OSTATUS; } elseif (!empty($protocols['gnusocial'])) { $server['network'] = Protocol::OSTATUS; + } elseif (!empty($protocols['zot'])) { + $server['network'] = Protocol::ZOT; } else { print_r($protocols); die('Protocol 2'); @@ -370,15 +432,15 @@ die('Möööp'); if (!empty($data['register_policy'])) { switch ($data['register_policy']) { - case "REGISTER_OPEN": + case 'REGISTER_OPEN': $serverdata['register_policy'] = Register::OPEN; break; - case "REGISTER_APPROVE": + case 'REGISTER_APPROVE': $serverdata['register_policy'] = Register::APPROVE; break; - case "REGISTER_CLOSED": + case 'REGISTER_CLOSED': default: $serverdata['register_policy'] = Register::CLOSED; break; @@ -423,28 +485,30 @@ die('Möööp'); return $serverdata; } - if (isset($data['version'])) { + if (!empty($data['version'])) { $serverdata['platform'] = 'mastodon'; $serverdata['version'] = defaults($data, 'version', ''); - if (!empty($data['title'])) { - $serverdata['site_name'] = $data['title']; - } - if (!empty($data['description'])) { - $serverdata['info'] = trim($data['description']); - } $serverdata['network'] = Protocol::ACTIVITYPUB; } + if (!empty($data['title'])) { + $serverdata['site_name'] = $data['title']; + } + + if (!empty($data['description'])) { + $serverdata['info'] = trim($data['description']); + } + if (!empty($data['stats']['user_count'])) { $serverdata['registered-users'] = $data['stats']['user_count']; } - if (strstr($serverdata['version'], 'Pleroma')) { + if (!empty($serverdata['version']) && strstr($serverdata['version'], 'Pleroma')) { $serverdata['platform'] = 'pleroma'; $serverdata['version'] = trim(str_replace('Pleroma', '', $serverdata['version'])); // 2.7.2 (compatible; Pleroma 1.0.0-1225-gf31ad554-develop) } - if (strstr($serverdata['version'], 'Pixelfed')) { + if (!empty($serverdata['version']) && strstr($serverdata['version'], 'Pixelfed')) { print_r($serverdata); die(); // $serverdata['platform'] = 'pixelfed'; @@ -454,6 +518,104 @@ die('Möööp'); return $serverdata; } + private static function detectHubzilla($url, $serverdata) + { + $curlResult = Network::curl($url . '/api/statusnet/config.json'); + if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { + return $serverdata; + } + + $data = json_decode($curlResult->getBody(), true); + if (empty($data)) { + return $serverdata; + } + + if (!empty($data['site']['name'])) { + $serverdata['site_name'] = $data['site']['name']; + } + + if (!empty($data['site']['platform'])) { +print_r($data); +die('1'); + $serverdata['platform'] = $data['site']['platform']['PLATFORM_NAME']; + $serverdata['version'] = $data['site']['platform']['STD_VERSION']; + $serverdata['network'] = Protocol::ZOT; + } + + if (isset($data['site']['BlaBlaNet'])) { +print_r($data); +die('2'); + $serverdata['platform'] = $data['site']['BlaBlaNet']['PLATFORM_NAME']; + $serverdata['version'] = $data['site']['BlaBlaNet']['STD_VERSION']; + $serverdata['network'] = Protocol::ZOT; + } + + if (isset($data['site']['hubzilla'])) { +print_r($data); +die('3'); + $serverdata['platform'] = $data['site']['hubzilla']['PLATFORM_NAME']; + $serverdata['version'] = $data['site']['hubzilla']['RED_VERSION']; + $serverdata['network'] = Protocol::ZOT; + } + + if (isset($data['site']['redmatrix'])) { + if (isset($data['site']['redmatrix']['PLATFORM_NAME'])) { + $serverdata['platform'] = $data['site']['redmatrix']['PLATFORM_NAME']; + } elseif (isset($data['site']['redmatrix']['RED_PLATFORM'])) { + $serverdata['platform'] = $data['site']['redmatrix']['RED_PLATFORM']; + } + + $serverdata['version'] = $data['site']['redmatrix']['RED_VERSION']; + $serverdata['network'] = Protocol::ZOT; + } + + if (isset($data['site']['friendica'])) { +print_r($data); +print_r($serverdata); +die('5'); + $serverdata['platform'] = $data['site']['friendica']['FRIENDICA_PLATFORM']; + $serverdata['version'] = $data['site']['friendica']['FRIENDICA_VERSION']; + $serverdata['network'] = Protocol::DFRN; + } + + $private = false; + $inviteonly = false; + $closed = false; + + if (!empty($data['site']['closed'])) { + $closed = self::toBoolean($data['site']['closed']); + } + + if (!empty($data['site']['private'])) { + $private = self::toBoolean($data['site']['private']); + } + + if (!empty($data['site']['inviteonly'])) { + $inviteonly = self::toBoolean($data['site']['inviteonly']); + } + + if (!$closed && !$private and $inviteonly) { + $register_policy = Register::APPROVE; + } elseif (!$closed && !$private) { + $register_policy = Register::OPEN; + } else { + $register_policy = Register::CLOSED; + } + + return $serverdata; + } + + private static function toBoolean($val) + { + if (($val == 'true') || ($val == 1)) { + return true; + } elseif (($val == 'false') || ($val == 0)) { + return false; + } + + return $val; + } + private static function detectGNUSocial($url, $serverdata) { $curlResult = Network::curl($url . '/api/statusnet/version.json'); From 3eb24d1b5c4c507113f29dd98b07944a8fdaf830 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 3 Oct 2019 14:48:46 +0000 Subject: [PATCH 04/13] Nost test code is removed --- src/Model/GServer.php | 119 +++++++++++------------------------------- 1 file changed, 31 insertions(+), 88 deletions(-) diff --git a/src/Model/GServer.php b/src/Model/GServer.php index 44bf304be8..a1f194ed9a 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -18,34 +18,25 @@ use Friendica\Util\Strings; use Friendica\Util\XML; use Friendica\Core\Logger; -/* -use Exception; -use Friendica\Core\System; -use Friendica\Core\Worker; -use Friendica\Network\Probe; -use Friendica\Protocol\PortableContact; -*/ /** * @brief This class handles GServer related functions */ class GServer { /** - * Detect server type + * Detect server data (type, protocol, version number, ...) + * The detected data is then updated or inserted in the gserver table. * * @param string $url Server url - * @param boolean $force Force update * * @return boolean 'true' if server could be detected */ - public static function detect($url, $force = false) + public static function detect($url) { /// @Todo: - // - Update Check // - poco endpoint // - Pleroma version number -// $gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($url)]); $serverdata = []; // When a nodeinfo is present, we don't need to dig further @@ -58,10 +49,12 @@ class GServer $nodeinfo = self::fetchNodeinfo($url, $curlResult); + // When nodeinfo isn't present, we use the older 'statistics.json' endpoint if (empty($nodeinfo)) { $nodeinfo = self::fetchStatistics($url); } + // If that didn't work out well, we use some protocol specific endpoints if (empty($nodeinfo) || ($nodeinfo['network'] == Protocol::DFRN)) { // Fetch the landing page, possibly it reveals some data $curlResult = Network::curl($url, false, ['timeout' => $xrd_timeout]); @@ -88,6 +81,7 @@ class GServer $serverdata = self::fetchSiteinfo($url, $serverdata); } + // The 'siteinfo.json' doesn't seem to be present on older Hubzilla installations if (empty($serverdata['network'])) { $serverdata = self::detectHubzilla($url, $serverdata); } @@ -119,18 +113,18 @@ class GServer $serverdata['registered-users'] = max($gcontacts, $apcontacts, $contacts); } - $fields = array_keys($serverdata); - $old_data = DBA::selectFirst('gserver', $fields, ['nurl' => Strings::normaliseLink($url)]); - if (!DBA::isResult($old_data)) { -die('Möööp'); + $serverdata['last_contact'] = DateTimeFormat::utcNow(); + + if (!DBA::exists('gserver', ['nurl' => Strings::normaliseLink($url)])) { $serverdata['created'] = DateTimeFormat::utcNow(); - DBA::insert('gserver', $serverdata); + $ret = DBA::insert('gserver', $serverdata); } else { - $serverdata['last_contact'] = DateTimeFormat::utcNow(); - DBA::update('gserver', $serverdata, ['nurl' => $serverdata['nurl']], $old_data); + $ret = DBA::update('gserver', $serverdata, ['nurl' => $serverdata['nurl']]); } - return $serverdata; + print_r($serverdata); + + return $ret; } private static function fetchStatistics($url) @@ -165,11 +159,10 @@ die('Möööp'); $serverdata['network'] = Protocol::DIASPORA; } elseif ($serverdata['platform'] == 'Friendica') { $serverdata['network'] = Protocol::DFRN; + } elseif ($serverdata['platform'] == 'hubzilla') { + $serverdata['network'] = Protocol::ZOT; } elseif ($serverdata['platform'] == 'redmatrix') { $serverdata['network'] = Protocol::ZOT; - } else { - print_r($serverdata); - die('aaa'); } } @@ -194,7 +187,7 @@ die('Möööp'); { $nodeinfo = json_decode($curlResult->getBody(), true); - if (!is_array($nodeinfo) || !isset($nodeinfo['links'])) { + if (!is_array($nodeinfo) || empty($nodeinfo['links'])) { return []; } @@ -262,11 +255,11 @@ die('Möööp'); } if (is_array($nodeinfo['software'])) { - if (isset($nodeinfo['software']['name'])) { + if (!empty($nodeinfo['software']['name'])) { $server['platform'] = $nodeinfo['software']['name']; } - if (isset($nodeinfo['software']['version'])) { + if (!empty($nodeinfo['software']['version'])) { $server['version'] = $nodeinfo['software']['version']; // Version numbers on Nodeinfo are presented with additional info, e.g.: // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191. @@ -274,7 +267,7 @@ die('Möööp'); } } - if (isset($nodeinfo['metadata']['nodeName'])) { + if (!empty($nodeinfo['metadata']['nodeName'])) { $server['site_name'] = $nodeinfo['metadata']['nodeName']; } @@ -300,9 +293,6 @@ die('Möööp'); $server['network'] = Protocol::OSTATUS; } elseif (!empty($protocols['zot'])) { $server['network'] = Protocol::ZOT; - } else { - print_r($protocols); - die('Protocol 1'); } } @@ -342,11 +332,11 @@ die('Möööp'); } if (is_array($nodeinfo['software'])) { - if (isset($nodeinfo['software']['name'])) { + if (!empty($nodeinfo['software']['name'])) { $server['platform'] = $nodeinfo['software']['name']; } - if (isset($nodeinfo['software']['version'])) { + if (!empty($nodeinfo['software']['version'])) { $server['version'] = $nodeinfo['software']['version']; // Version numbers on Nodeinfo are presented with additional info, e.g.: // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191. @@ -354,7 +344,7 @@ die('Möööp'); } } - if (isset($nodeinfo['metadata']['nodeName'])) { + if (!empty($nodeinfo['metadata']['nodeName'])) { $server['site_name'] = $nodeinfo['metadata']['nodeName']; } @@ -380,9 +370,6 @@ die('Möööp'); $server['network'] = Protocol::OSTATUS; } elseif (!empty($protocols['zot'])) { $server['network'] = Protocol::ZOT; - } else { - print_r($protocols); - die('Protocol 2'); } } @@ -405,7 +392,7 @@ die('Möööp'); return $serverdata; } - if (isset($data['url'])) { + if (!empty($data['url'])) { $serverdata['platform'] = $data['platform']; $serverdata['version'] = $data['version']; } @@ -508,13 +495,6 @@ die('Möööp'); $serverdata['version'] = trim(str_replace('Pleroma', '', $serverdata['version'])); // 2.7.2 (compatible; Pleroma 1.0.0-1225-gf31ad554-develop) } - if (!empty($serverdata['version']) && strstr($serverdata['version'], 'Pixelfed')) { - print_r($serverdata); - die(); -// $serverdata['platform'] = 'pixelfed'; -// $serverdata['version'] = trim(str_replace('Pixelfed', '', $serverdata['version'])); // 2.7.2 (compatible; Pixelfed 0.10.5) - } - return $serverdata; } @@ -535,33 +515,21 @@ die('Möööp'); } if (!empty($data['site']['platform'])) { -print_r($data); -die('1'); $serverdata['platform'] = $data['site']['platform']['PLATFORM_NAME']; $serverdata['version'] = $data['site']['platform']['STD_VERSION']; $serverdata['network'] = Protocol::ZOT; } - if (isset($data['site']['BlaBlaNet'])) { -print_r($data); -die('2'); - $serverdata['platform'] = $data['site']['BlaBlaNet']['PLATFORM_NAME']; - $serverdata['version'] = $data['site']['BlaBlaNet']['STD_VERSION']; - $serverdata['network'] = Protocol::ZOT; - } - - if (isset($data['site']['hubzilla'])) { -print_r($data); -die('3'); + if (!empty($data['site']['hubzilla'])) { $serverdata['platform'] = $data['site']['hubzilla']['PLATFORM_NAME']; $serverdata['version'] = $data['site']['hubzilla']['RED_VERSION']; $serverdata['network'] = Protocol::ZOT; } - if (isset($data['site']['redmatrix'])) { - if (isset($data['site']['redmatrix']['PLATFORM_NAME'])) { + if (!empty($data['site']['redmatrix'])) { + if (!empty($data['site']['redmatrix']['PLATFORM_NAME'])) { $serverdata['platform'] = $data['site']['redmatrix']['PLATFORM_NAME']; - } elseif (isset($data['site']['redmatrix']['RED_PLATFORM'])) { + } elseif (!empty($data['site']['redmatrix']['RED_PLATFORM'])) { $serverdata['platform'] = $data['site']['redmatrix']['RED_PLATFORM']; } @@ -569,15 +537,6 @@ die('3'); $serverdata['network'] = Protocol::ZOT; } - if (isset($data['site']['friendica'])) { -print_r($data); -print_r($serverdata); -die('5'); - $serverdata['platform'] = $data['site']['friendica']['FRIENDICA_PLATFORM']; - $serverdata['version'] = $data['site']['friendica']['FRIENDICA_VERSION']; - $serverdata['network'] = Protocol::DFRN; - } - $private = false; $inviteonly = false; $closed = false; @@ -740,23 +699,13 @@ die('5'); $serverdata['network'] = Protocol::ACTIVITYPUB; } } -// - if (($attr['name'] == 'generator') && in_array($attr['content'], ['Write.as'])) { -die('as'); -// $serverdata['platform'] = $attr['content']; -// $serverdata['network'] = Protocol::ACTIVITYPUB; - } elseif ($attr['name'] == 'generator') { + + if ($attr['name'] == 'generator') { $serverdata['platform'] = $attr['content']; $version_part = explode(' ', $attr['content']); - if (count($version_part) == 3) { - if (($version_part[0] == 'Red') && ($version_part[1] == 'Matrix')) { -// $serverdata['platform'] = $version_part[0] . ' ' . $version_part[1]; -// $serverdata['version'] = $version_part[2]; -// $serverdata['network'] = Protocol::DIASPORA; - } - } elseif (count($version_part) == 2) { + if (count($version_part) == 2) { if (in_array($version_part[0], ['WordPress'])) { $serverdata['platform'] = $version_part[0]; $serverdata['version'] = $version_part[1]; @@ -799,12 +748,6 @@ die('as'); $serverdata['info'] = $attr['content']; } -// if (($attr['property'] == 'og:title') && in_array($attr['content'], ['pixelfed', 'Socialhome'])) { -// if (($attr['property'] == 'og:title') && in_array($attr['content'], ['Nextcloud'])) { -// $serverdata['platform'] = $attr['content']; -// $serverdata['network'] = Protocol::ACTIVITYPUB; -// } - if ($attr['property'] == 'og:platform') { $serverdata['platform'] = $attr['content']; From 59b464ade308672161c25361fcb04e4ecf7dd517 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 3 Oct 2019 15:30:07 +0000 Subject: [PATCH 05/13] PoCo endpoint added --- src/Model/GServer.php | 45 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/src/Model/GServer.php b/src/Model/GServer.php index a1f194ed9a..11dd9b5257 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -27,14 +27,13 @@ class GServer * Detect server data (type, protocol, version number, ...) * The detected data is then updated or inserted in the gserver table. * - * @param string $url Server url + * @param string $url Server url * * @return boolean 'true' if server could be detected */ public static function detect($url) { /// @Todo: - // - poco endpoint // - Pleroma version number $serverdata = []; @@ -97,6 +96,8 @@ class GServer $serverdata = $nodeinfo; } + $serverdata = self::checkPoCo($url, $serverdata); + // We can't detect the network type. Possibly it is some system that we don't know yet if (empty($serverdata['network'])) { $serverdata['network'] = Protocol::PHANTOM; @@ -105,14 +106,19 @@ class GServer $serverdata['url'] = $url; $serverdata['nurl'] = Strings::normaliseLink($url); - // When we don't have the registered users, we simply count what we know - if (empty($serverdata['registered-users'])) { - $gcontacts = DBA::count('gcontact', ['server_url' => [$url, $serverdata['nurl']]]); - $apcontacts = DBA::count('apcontact', ['baseurl' => [$url, $serverdata['nurl']]]); - $contacts = DBA::count('contact', ['uid' => 0, 'baseurl' => [$url, $serverdata['nurl']]]); - $serverdata['registered-users'] = max($gcontacts, $apcontacts, $contacts); + // We take the highest number that we do find + $registeredUsers = $serverdata['registered-users'] ?? 0; + + // On an active server there has to be at least a single user + if (($serverdata['network'] != Protocol::PHANTOM) && ($registeredUsers == 0)) { + $registeredUsers = 1; } + $gcontacts = DBA::count('gcontact', ['server_url' => [$url, $serverdata['nurl']]]); + $apcontacts = DBA::count('apcontact', ['baseurl' => [$url, $serverdata['nurl']]]); + $contacts = DBA::count('contact', ['uid' => 0, 'baseurl' => [$url, $serverdata['nurl']]]); + $serverdata['registered-users'] = max($gcontacts, $apcontacts, $contacts, $registeredUsers); + $serverdata['last_contact'] = DateTimeFormat::utcNow(); if (!DBA::exists('gserver', ['nurl' => Strings::normaliseLink($url)])) { @@ -437,6 +443,29 @@ class GServer return $serverdata; } + private static function checkPoCo($url, $serverdata) + { + $curlResult = Network::curl($url. '/poco'); + if (!$curlResult->isSuccess()) { + return $serverdata; + } + + $data = json_decode($curlResult->getBody(), true); + if (empty($data)) { + return $serverdata; + } + + if (!empty($data['totalResults'])) { + $registeredUsers = $serverdata['registered-users'] ?? 0; + $serverdata['registered-users'] = max($data['totalResults'], $registeredUsers); + $serverdata['poco'] = $url . '/poco'; + } else { + $serverdata['poco'] = ''; + } + + return $serverdata; + } + private static function detectNextcloud($url, $serverdata) { $curlResult = Network::curl($url . '/status.php'); From 832c765ad50b71920eb32453606892dfed8e78c7 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 3 Oct 2019 20:52:04 +0000 Subject: [PATCH 06/13] Hostmeta check, Pleroma version number cleaning --- src/Model/GServer.php | 51 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/src/Model/GServer.php b/src/Model/GServer.php index 11dd9b5257..9a224aecd8 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -54,7 +54,7 @@ class GServer } // If that didn't work out well, we use some protocol specific endpoints - if (empty($nodeinfo) || ($nodeinfo['network'] == Protocol::DFRN)) { + if (empty($nodeinfo) || empty($nodeinfo['network']) || ($nodeinfo['network'] == Protocol::DFRN)) { // Fetch the landing page, possibly it reveals some data $curlResult = Network::curl($url, false, ['timeout' => $xrd_timeout]); if ($curlResult->isSuccess()) { @@ -103,6 +103,14 @@ class GServer $serverdata['network'] = Protocol::PHANTOM; } + // Check host-meta for phantom networks. + // Although this is not needed, it is a good indicator for a living system, + // since most systems had implemented it. + if (($serverdata['network'] == Protocol::PHANTOM) && !self::validHostMeta($url)) { + DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($url)]); + return false; + } + $serverdata['url'] = $url; $serverdata['nurl'] = Strings::normaliseLink($url); @@ -443,6 +451,39 @@ class GServer return $serverdata; } + private static function validHostMeta($url) + { + $xrd_timeout = Config::get('system', 'xrd_timeout'); + $curlResult = Network::curl($url . '/.well-known/host-meta', false, ['timeout' => $xrd_timeout]); + if (!$curlResult->isSuccess()) { + return false; + } + + $xrd = XML::parseString($curlResult->getBody(), false); + if (!is_object($xrd)) { + return false; + } + + $elements = XML::elementToArray($xrd); + if (empty($elements) || empty($elements['xrd']) || empty($elements['xrd']['link'])) { + return false; + } + + $valid = false; + foreach ($elements['xrd']['link'] as $link) { + if (empty($link['rel']) || empty($link['type']) || empty($link['template'])) { + continue; + } + + if ($link['type'] == 'application/xrd+xml') { + // When the webfinger host is the same like the system host, it should be ok. + $valid = (parse_url($url, PHP_URL_HOST) == parse_url($link['template'], PHP_URL_HOST)); + } + } + + return $valid; + } + private static function checkPoCo($url, $serverdata) { $curlResult = Network::curl($url. '/poco'); @@ -519,9 +560,15 @@ class GServer $serverdata['registered-users'] = $data['stats']['user_count']; } + if (!empty($serverdata['version']) && preg_match('/.*?\(compatible;\s(.*)\s(.*)\)/ism', $serverdata['version'], $matches)) { + $serverdata['platform'] = $matches[1]; + $serverdata['version'] = $matches[2]; +print_r($serverdata); + } + if (!empty($serverdata['version']) && strstr($serverdata['version'], 'Pleroma')) { $serverdata['platform'] = 'pleroma'; - $serverdata['version'] = trim(str_replace('Pleroma', '', $serverdata['version'])); // 2.7.2 (compatible; Pleroma 1.0.0-1225-gf31ad554-develop) + $serverdata['version'] = trim(str_replace('Pleroma', '', $serverdata['version'])); } return $serverdata; From 200908032710835c60bbbfb207d511a912c8354a Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 3 Oct 2019 21:39:48 +0000 Subject: [PATCH 07/13] Remove more test data --- src/Model/GServer.php | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/Model/GServer.php b/src/Model/GServer.php index 9a224aecd8..e665df35c0 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -33,9 +33,6 @@ class GServer */ public static function detect($url) { - /// @Todo: - // - Pleroma version number - $serverdata = []; // When a nodeinfo is present, we don't need to dig further @@ -136,8 +133,6 @@ class GServer $ret = DBA::update('gserver', $serverdata, ['nurl' => $serverdata['nurl']]); } - print_r($serverdata); - return $ret; } @@ -563,7 +558,6 @@ class GServer if (!empty($serverdata['version']) && preg_match('/.*?\(compatible;\s(.*)\s(.*)\)/ism', $serverdata['version'], $matches)) { $serverdata['platform'] = $matches[1]; $serverdata['version'] = $matches[2]; -print_r($serverdata); } if (!empty($serverdata['version']) && strstr($serverdata['version'], 'Pleroma')) { @@ -764,7 +758,7 @@ print_r($serverdata); continue; } } -//print_r($attr); + if ($attr['name'] == 'description') { $serverdata['info'] = $attr['content']; } @@ -814,7 +808,6 @@ print_r($serverdata); continue; } } -//print_r($attr); if ($attr['property'] == 'og:site_name') { $serverdata['site_name'] = $attr['content']; @@ -859,9 +852,6 @@ print_r($serverdata); $serverdata['platform'] = 'friendica'; $serverdata['network'] = $network = Protocol::DFRN; $serverdata['version'] = $curlResult->getHeader('x-friendica-version'); - - } else { -//print_r($curlResult->getHeaderArray()); } return $serverdata; } From d9a74bd5dd061a1f98d5527d8d6011264d710121 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 3 Oct 2019 23:33:41 +0000 Subject: [PATCH 08/13] Code transitioning from PortableContacts.php to GServer.php --- src/Model/Contact.php | 2 +- src/Model/GContact.php | 4 +- src/Model/GServer.php | 120 ++++ src/Protocol/PortableContact.php | 953 +------------------------------ src/Worker/DiscoverPoCo.php | 7 +- src/Worker/PubSubPublish.php | 3 +- 6 files changed, 135 insertions(+), 954 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 74534fc371..5ae9955223 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1481,7 +1481,7 @@ class Contact extends BaseObject $data = Probe::uri($url, "", $uid); // Ensure that there is a gserver entry if (!empty($data['baseurl']) && ($data['network'] != Protocol::PHANTOM)) { - PortableContact::checkServer($data['baseurl']); + GServer::check($data['baseurl']); } } diff --git a/src/Model/GContact.php b/src/Model/GContact.php index 5aaf0f2c98..89737cc084 100644 --- a/src/Model/GContact.php +++ b/src/Model/GContact.php @@ -231,7 +231,7 @@ class GContact } // The server URL doesn't seem to be valid, so we don't store it. - if (!PortableContact::checkServer($gcontact['server_url'], $gcontact['network'])) { + if (!GServer::check($gcontact['server_url'])) { $gcontact['server_url'] = ""; } @@ -541,7 +541,7 @@ class GContact $j = json_decode($x); if (!empty($j->entries)) { foreach ($j->entries as $entry) { - PortableContact::checkServer($entry->url); + GServer::check($entry->url); $url = $entry->url . '/poco'; if (!in_array($url, $done)) { diff --git a/src/Model/GServer.php b/src/Model/GServer.php index e665df35c0..b3cc6fc41b 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -17,12 +17,55 @@ use Friendica\Util\DateTimeFormat; use Friendica\Util\Strings; use Friendica\Util\XML; use Friendica\Core\Logger; +use Friendica\Protocol\PortableContact; +use Friendica\Protocol\Diaspora; /** * @brief This class handles GServer related functions */ class GServer { + public static function check($server_url, $force = false) + { + // Unify the server address + $server_url = trim($server_url, '/'); + $server_url = str_replace('/index.php', '', $server_url); + + if ($server_url == '') { + return false; + } + + $gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($server_url)]); + if (DBA::isResult($gserver)) { + if ($gserver['created'] <= DBA::NULL_DATETIME) { + $fields = ['created' => DateTimeFormat::utcNow()]; + $condition = ['nurl' => Strings::normaliseLink($server_url)]; + DBA::update('gserver', $fields, $condition); + } + + $last_contact = $gserver["last_contact"]; + $last_failure = $gserver["last_failure"]; + + // See discussion under https://forum.friendi.ca/display/0b6b25a8135aabc37a5a0f5684081633 + // It can happen that a zero date is in the database, but storing it again is forbidden. + if ($last_contact < DBA::NULL_DATETIME) { + $last_contact = DBA::NULL_DATETIME; + } + + if ($last_failure < DBA::NULL_DATETIME) { + $last_failure = DBA::NULL_DATETIME; + } + + if (!$force && !PortableContact::updateNeeded($gserver['created'], '', $last_failure, $last_contact)) { + Logger::info('Use cached data', ['server' => $server_url]); + return ($last_contact >= $last_failure); + } + } + Logger::info('Server is outdated or unknown. Start discovery.', ['Server' => $server_url, 'Force' => $force, 'Created' => $gserver['created'], 'Failure' => $last_failure, 'Contact' => $last_contact]); + + return self::detect($server_url); + } + /** * Detect server data (type, protocol, version number, ...) * The detected data is then updated or inserted in the gserver table. @@ -133,9 +176,86 @@ class GServer $ret = DBA::update('gserver', $serverdata, ['nurl' => $serverdata['nurl']]); } + if (in_array($serverdata['network'], [Protocol::DFRN, Protocol::DIASPORA])) { + self::discoverRelay($url); + } + return $ret; } + /** + * @brief Fetch relay data from a given server url + * + * @param string $server_url address of the server + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + private static function discoverRelay($server_url) + { + Logger::info('Discover relay data', ['server' => $server_url]); + + $curlResult = Network::curl($server_url . '/.well-known/x-social-relay'); + if (!$curlResult->isSuccess()) { + return; + } + + $data = json_decode($curlResult->getBody(), true); + if (!is_array($data)) { + return; + } + + $gserver = DBA::selectFirst('gserver', ['id', 'relay-subscribe', 'relay-scope'], ['nurl' => Strings::normaliseLink($server_url)]); + if (!DBA::isResult($gserver)) { + return; + } + + if (($gserver['relay-subscribe'] != $data['subscribe']) || ($gserver['relay-scope'] != $data['scope'])) { + $fields = ['relay-subscribe' => $data['subscribe'], 'relay-scope' => $data['scope']]; + DBA::update('gserver', $fields, ['id' => $gserver['id']]); + } + + DBA::delete('gserver-tag', ['gserver-id' => $gserver['id']]); + + if ($data['scope'] == 'tags') { + // Avoid duplicates + $tags = []; + foreach ($data['tags'] as $tag) { + $tag = mb_strtolower($tag); + if (strlen($tag) < 100) { + $tags[$tag] = $tag; + } + } + + foreach ($tags as $tag) { + DBA::insert('gserver-tag', ['gserver-id' => $gserver['id'], 'tag' => $tag], true); + } + } + + // Create or update the relay contact + $fields = []; + if (isset($data['protocols'])) { + if (isset($data['protocols']['diaspora'])) { + $fields['network'] = Protocol::DIASPORA; + + if (isset($data['protocols']['diaspora']['receive'])) { + $fields['batch'] = $data['protocols']['diaspora']['receive']; + } elseif (is_string($data['protocols']['diaspora'])) { + $fields['batch'] = $data['protocols']['diaspora']; + } + } + + if (isset($data['protocols']['dfrn'])) { + $fields['network'] = Protocol::DFRN; + + if (isset($data['protocols']['dfrn']['receive'])) { + $fields['batch'] = $data['protocols']['dfrn']['receive']; + } elseif (is_string($data['protocols']['dfrn'])) { + $fields['batch'] = $data['protocols']['dfrn']; + } + } + } + Diaspora::setRelayContact($server_url, $fields); + } + private static function fetchStatistics($url) { $curlResult = Network::curl($url . '/statistics.json'); diff --git a/src/Protocol/PortableContact.php b/src/Protocol/PortableContact.php index c7ca1c7429..826fc108c8 100644 --- a/src/Protocol/PortableContact.php +++ b/src/Protocol/PortableContact.php @@ -20,6 +20,7 @@ use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\Model\Contact; use Friendica\Model\GContact; +use Friendica\Model\GServer; use Friendica\Model\Profile; use Friendica\Module\Register; use Friendica\Network\Probe; @@ -223,7 +224,7 @@ class PortableContact return true; } - return self::checkServer($server, $network, $force); + return GServer::check($server, $force); } public static function alternateOStatusUrl($url) @@ -267,7 +268,7 @@ class PortableContact } if ($server_url != "") { - if (!self::checkServer($server_url, $gcontacts[0]["network"], $force)) { + if (!GServer::check($server_url, $force)) { if ($force) { $fields = ['last_failure' => DateTimeFormat::utcNow()]; DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]); @@ -523,948 +524,6 @@ class PortableContact return true; } - /// @TODO Maybe move this out to an utilities class? - private static function toBoolean($val) - { - if (($val == "true") || ($val == 1)) { - return true; - } elseif (($val == "false") || ($val == 0)) { - return false; - } - - return $val; - } - - /** - * @brief Detect server type (Hubzilla or Friendica) via the poco data - * - * @param array $data POCO data - * @return array Server data - */ - private static function detectPocoData(array $data) - { - if (!isset($data['entry'])) { - return false; - } - - if (count($data['entry']) == 0) { - return false; - } - - if (!isset($data['entry'][0]['urls'])) { - return false; - } - - if (count($data['entry'][0]['urls']) == 0) { - return false; - } - - foreach ($data['entry'][0]['urls'] as $url) { - if ($url['type'] == 'zot') { - $server = []; - $server["platform"] = 'Hubzilla'; - $server["network"] = Protocol::DIASPORA; - return $server; - } - } - return false; - } - - /** - * @brief Detect server type by using the nodeinfo data - * - * @param string $server_url address of the server - * @return array Server data - * @throws \Friendica\Network\HTTPException\InternalServerErrorException - */ - private static function fetchNodeinfo($server_url) - { - $curlResult = Network::curl($server_url."/.well-known/nodeinfo"); - if (!$curlResult->isSuccess()) { - return false; - } - - $nodeinfo = json_decode($curlResult->getBody(), true); - - if (!is_array($nodeinfo) || !isset($nodeinfo['links'])) { - return false; - } - - $nodeinfo1_url = ''; - $nodeinfo2_url = ''; - - foreach ($nodeinfo['links'] as $link) { - if (!is_array($link) || empty($link['rel']) || empty($link['href'])) { - Logger::log('Invalid nodeinfo format for ' . $server_url, Logger::DEBUG); - continue; - } - if ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/1.0') { - $nodeinfo1_url = $link['href']; - } elseif ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/2.0') { - $nodeinfo2_url = $link['href']; - } - } - - if ($nodeinfo1_url . $nodeinfo2_url == '') { - return false; - } - - $server = []; - - // When the nodeinfo url isn't on the same host, then there is obviously something wrong - if (!empty($nodeinfo2_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo2_url, PHP_URL_HOST))) { - $server = self::parseNodeinfo2($nodeinfo2_url); - } - - // When the nodeinfo url isn't on the same host, then there is obviously something wrong - if (empty($server) && !empty($nodeinfo1_url) && (parse_url($server_url, PHP_URL_HOST) == parse_url($nodeinfo1_url, PHP_URL_HOST))) { - $server = self::parseNodeinfo1($nodeinfo1_url); - } - - return $server; - } - - /** - * @brief Parses Nodeinfo 1 - * - * @param string $nodeinfo_url address of the nodeinfo path - * @return array Server data - * @throws \Friendica\Network\HTTPException\InternalServerErrorException - */ - private static function parseNodeinfo1($nodeinfo_url) - { - $curlResult = Network::curl($nodeinfo_url); - - if (!$curlResult->isSuccess()) { - return false; - } - - $nodeinfo = json_decode($curlResult->getBody(), true); - - if (!is_array($nodeinfo)) { - return false; - } - - $server = []; - - $server['register_policy'] = Register::CLOSED; - - if (isset($nodeinfo['openRegistrations']) && is_bool($nodeinfo['openRegistrations']) && $nodeinfo['openRegistrations']) { - $server['register_policy'] = Register::OPEN; - } - - if (is_array($nodeinfo['software'])) { - if (isset($nodeinfo['software']['name'])) { - $server['platform'] = $nodeinfo['software']['name']; - } - - if (isset($nodeinfo['software']['version'])) { - $server['version'] = $nodeinfo['software']['version']; - // Version numbers on Nodeinfo are presented with additional info, e.g.: - // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191. - $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']); - } - } - - if (isset($nodeinfo['metadata']['nodeName'])) { - $server['site_name'] = $nodeinfo['metadata']['nodeName']; - } - - if (!empty($nodeinfo['usage']['users']['total'])) { - $server['registered-users'] = $nodeinfo['usage']['users']['total']; - } - - $diaspora = false; - $friendica = false; - $gnusocial = false; - - if (!empty($nodeinfo['protocols']['inbound']) && is_array($nodeinfo['protocols']['inbound'])) { - foreach ($nodeinfo['protocols']['inbound'] as $inbound) { - if ($inbound == 'diaspora') { - $diaspora = true; - } - if ($inbound == 'friendica') { - $friendica = true; - } - if ($inbound == 'gnusocial') { - $gnusocial = true; - } - } - } - - if ($gnusocial) { - $server['network'] = Protocol::OSTATUS; - } - if ($diaspora) { - $server['network'] = Protocol::DIASPORA; - } - if ($friendica) { - $server['network'] = Protocol::DFRN; - } - - if (!$server) { - return false; - } - - return $server; - } - - /** - * @brief Parses Nodeinfo 2 - * - * @param string $nodeinfo_url address of the nodeinfo path - * @return array Server data - * @throws \Friendica\Network\HTTPException\InternalServerErrorException - */ - private static function parseNodeinfo2($nodeinfo_url) - { - $curlResult = Network::curl($nodeinfo_url); - if (!$curlResult->isSuccess()) { - return false; - } - - $nodeinfo = json_decode($curlResult->getBody(), true); - - if (!is_array($nodeinfo)) { - return false; - } - - $server = []; - - $server['register_policy'] = Register::CLOSED; - - if (is_bool($nodeinfo['openRegistrations']) && $nodeinfo['openRegistrations']) { - $server['register_policy'] = Register::OPEN; - } - - if (is_array($nodeinfo['software'])) { - if (isset($nodeinfo['software']['name'])) { - $server['platform'] = $nodeinfo['software']['name']; - } - - if (isset($nodeinfo['software']['version'])) { - $server['version'] = $nodeinfo['software']['version']; - // Version numbers on Nodeinfo are presented with additional info, e.g.: - // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191. - $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']); - } - } - - if (isset($nodeinfo['metadata']['nodeName'])) { - $server['site_name'] = $nodeinfo['metadata']['nodeName']; - } - - if (!empty($nodeinfo['usage']['users']['total'])) { - $server['registered-users'] = $nodeinfo['usage']['users']['total']; - } - - $diaspora = false; - $friendica = false; - $gnusocial = false; - - if (!empty($nodeinfo['protocols'])) { - foreach ($nodeinfo['protocols'] as $protocol) { - if ($protocol == 'diaspora') { - $diaspora = true; - } elseif ($protocol == 'friendica') { - $friendica = true; - } elseif ($protocol == 'gnusocial') { - $gnusocial = true; - } - } - } - - if ($gnusocial) { - $server['network'] = Protocol::OSTATUS; - } elseif ($diaspora) { - $server['network'] = Protocol::DIASPORA; - } elseif ($friendica) { - $server['network'] = Protocol::DFRN; - } - - if (empty($server)) { - return false; - } - - return $server; - } - - /** - * @brief Detect server type (Hubzilla or Friendica) via the front page body - * - * @param string $body Front page of the server - * @return array Server data - */ - private static function detectServerType($body) - { - $server = false; - - $doc = new DOMDocument(); - /// @TODO Acoid supressing error - @$doc->loadHTML($body); - $xpath = new DOMXPath($doc); - - $list = $xpath->query("//meta[@name]"); - - foreach ($list as $node) { - $attr = []; - if ($node->attributes->length) { - foreach ($node->attributes as $attribute) { - $attr[$attribute->name] = $attribute->value; - } - } - if ($attr['name'] == 'generator') { - $version_part = explode(" ", $attr['content']); - if (count($version_part) == 2) { - if (in_array($version_part[0], ["Friendika", "Friendica"])) { - $server = []; - $server["platform"] = $version_part[0]; - $server["version"] = $version_part[1]; - $server["network"] = Protocol::DFRN; - } - } - } - } - - if (!$server) { - $list = $xpath->query("//meta[@property]"); - - foreach ($list as $node) { - $attr = []; - if ($node->attributes->length) { - foreach ($node->attributes as $attribute) { - $attr[$attribute->name] = $attribute->value; - } - } - if ($attr['property'] == 'generator' && in_array($attr['content'], ["hubzilla", "BlaBlaNet"])) { - $server = []; - $server["platform"] = $attr['content']; - $server["version"] = ""; - $server["network"] = Protocol::DIASPORA; - } - } - } - - if (!$server) { - return false; - } - - $server["site_name"] = XML::getFirstNodeValue($xpath, '//head/title/text()'); - - return $server; - } - - public static function checkServer($server_url, $network = "", $force = false) - { - // Unify the server address - $server_url = trim($server_url, "/"); - $server_url = str_replace("/index.php", "", $server_url); - - if ($server_url == "") { - return false; - } - - $gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($server_url)]); - if (DBA::isResult($gserver)) { - if ($gserver["created"] <= DBA::NULL_DATETIME) { - $fields = ['created' => DateTimeFormat::utcNow()]; - $condition = ['nurl' => Strings::normaliseLink($server_url)]; - DBA::update('gserver', $fields, $condition); - } - $poco = $gserver["poco"]; - $noscrape = $gserver["noscrape"]; - - if ($network == "") { - $network = $gserver["network"]; - } - - $last_contact = $gserver["last_contact"]; - $last_failure = $gserver["last_failure"]; - $version = $gserver["version"]; - $platform = $gserver["platform"]; - $site_name = $gserver["site_name"]; - $info = $gserver["info"]; - $register_policy = $gserver["register_policy"]; - $registered_users = $gserver["registered-users"]; - - // See discussion under https://forum.friendi.ca/display/0b6b25a8135aabc37a5a0f5684081633 - // It can happen that a zero date is in the database, but storing it again is forbidden. - if ($last_contact < DBA::NULL_DATETIME) { - $last_contact = DBA::NULL_DATETIME; - } - - if ($last_failure < DBA::NULL_DATETIME) { - $last_failure = DBA::NULL_DATETIME; - } - - if (!$force && !self::updateNeeded($gserver["created"], "", $last_failure, $last_contact)) { - Logger::log("Use cached data for server ".$server_url, Logger::DEBUG); - return ($last_contact >= $last_failure); - } - } else { - $poco = ""; - $noscrape = ""; - $version = ""; - $platform = ""; - $site_name = ""; - $info = ""; - $register_policy = -1; - $registered_users = 0; - - $last_contact = DBA::NULL_DATETIME; - $last_failure = DBA::NULL_DATETIME; - } - Logger::log("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$gserver["created"]." Failure: ".$last_failure." Contact: ".$last_contact, Logger::DEBUG); - - $failure = false; - $possible_failure = false; - $orig_last_failure = $last_failure; - $orig_last_contact = $last_contact; - - // Mastodon uses the "@" for user profiles. - // But this can be misunderstood. - if (parse_url($server_url, PHP_URL_USER) != '') { - DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($server_url)]); - return false; - } - - // Check if the page is accessible via SSL. - $orig_server_url = $server_url; - $server_url = str_replace("http://", "https://", $server_url); - - // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital - $curlResult = Network::curl($server_url."/.well-known/host-meta", false, ['timeout' => 20]); - - // Quit if there is a timeout. - // But we want to make sure to only quit if we are mostly sure that this server url fits. - if (DBA::isResult($gserver) && ($orig_server_url == $server_url) && - ($curlResult->isTimeout())) { - Logger::log("Connection to server ".$server_url." timed out.", Logger::DEBUG); - DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($server_url)]); - return false; - } - - // Maybe the page is unencrypted only? - $xmlobj = @simplexml_load_string($curlResult->getBody(), 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0"); - if (!$curlResult->isSuccess() || ($curlResult->getBody() == "") || empty($xmlobj) || !is_object($xmlobj)) { - $server_url = str_replace("https://", "http://", $server_url); - - // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital - $curlResult = Network::curl($server_url."/.well-known/host-meta", false, ['timeout' => 20]); - - // Quit if there is a timeout - if ($curlResult->isTimeout()) { - Logger::log("Connection to server " . $server_url . " timed out.", Logger::DEBUG); - DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($server_url)]); - return false; - } - - $xmlobj = @simplexml_load_string($curlResult->getBody(), 'SimpleXMLElement', 0, "http://docs.oasis-open.org/ns/xri/xrd-1.0"); - } - - if (!$curlResult->isSuccess() || ($curlResult->getBody() == "") || empty($xmlobj) || !is_object($xmlobj)) { - // Workaround for bad configured servers (known nginx problem) - if (!empty($curlResult->getInfo()) && !in_array($curlResult->getInfo()["http_code"], ["403", "404"])) { - $failure = true; - } - - $possible_failure = true; - } - - // If the server has no possible failure we reset the cached data - if (!$possible_failure) { - $version = ""; - $platform = ""; - $site_name = ""; - $info = ""; - $register_policy = -1; - } - - if (!$failure) { - // This will be too low, but better than no value at all. - $registered_users = DBA::count('gcontact', ['server_url' => Strings::normaliseLink($server_url)]); - } - - // Look for poco - if (!$failure) { - $curlResult = Network::curl($server_url."/poco"); - - if ($curlResult->isSuccess()) { - $data = json_decode($curlResult->getBody(), true); - - if (isset($data['totalResults'])) { - $registered_users = $data['totalResults']; - $poco = $server_url . "/poco"; - $server = self::detectPocoData($data); - - if (!empty($server)) { - $platform = $server['platform']; - $network = $server['network']; - $version = ''; - $site_name = ''; - } - } - - /* - * There are servers out there who don't return 404 on a failure - * We have to be sure that don't misunderstand this - */ - if (is_null($data)) { - $poco = ""; - $noscrape = ""; - $network = ""; - } - } - } - - if (!$failure) { - // Test for Diaspora, Hubzilla, Mastodon or older Friendica servers - $curlResult = Network::curl($server_url); - - if (!$curlResult->isSuccess() || ($curlResult->getBody() == "")) { - $failure = true; - } else { - $server = self::detectServerType($curlResult->getBody()); - - if (!empty($server)) { - $platform = $server['platform']; - $network = $server['network']; - $version = $server['version']; - $site_name = $server['site_name']; - } - - $lines = explode("\n", $curlResult->getHeader()); - - if (count($lines)) { - foreach ($lines as $line) { - $line = trim($line); - - if (stristr($line, 'X-Diaspora-Version:')) { - $platform = "Diaspora"; - $version = trim(str_replace("X-Diaspora-Version:", "", $line)); - $version = trim(str_replace("x-diaspora-version:", "", $version)); - $network = Protocol::DIASPORA; - $versionparts = explode("-", $version); - $version = $versionparts[0]; - } - - if (stristr($line, 'Server: Mastodon')) { - $platform = "Mastodon"; - $network = Protocol::OSTATUS; - } - } - } - } - } - - if (!$failure && ($poco == "")) { - // Test for Statusnet - // Will also return data for Friendica and GNU Social - but it will be overwritten later - // The "not implemented" is a special treatment for really, really old Friendica versions - $curlResult = Network::curl($server_url."/api/statusnet/version.json"); - - if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') && - ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) { - $platform = "StatusNet"; - // Remove junk that some GNU Social servers return - $version = str_replace(chr(239).chr(187).chr(191), "", $curlResult->getBody()); - $version = trim($version, '"'); - $network = Protocol::OSTATUS; - } - - // Test for GNU Social - $curlResult = Network::curl($server_url."/api/gnusocial/version.json"); - - if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') && - ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) { - $platform = "GNU Social"; - // Remove junk that some GNU Social servers return - $version = str_replace(chr(239) . chr(187) . chr(191), "", $curlResult->getBody()); - $version = trim($version, '"'); - $network = Protocol::OSTATUS; - } - - // Test for Mastodon - $orig_version = $version; - $curlResult = Network::curl($server_url . "/api/v1/instance"); - - if ($curlResult->isSuccess() && ($curlResult->getBody() != '')) { - $data = json_decode($curlResult->getBody(), true); - - if (isset($data['version'])) { - $platform = "Mastodon"; - $version = defaults($data, 'version', ''); - $site_name = defaults($data, 'title', ''); - $info = defaults($data, 'description', ''); - $network = Protocol::OSTATUS; - } - - if (!empty($data['stats']['user_count'])) { - $registered_users = $data['stats']['user_count']; - } - } - - if (strstr($orig_version . $version, 'Pleroma')) { - $platform = 'Pleroma'; - $version = trim(str_replace('Pleroma', '', $version)); - } - } - - if (!$failure) { - // Test for Hubzilla and Red - $curlResult = Network::curl($server_url . "/siteinfo.json"); - - if ($curlResult->isSuccess()) { - $data = json_decode($curlResult->getBody(), true); - - if (isset($data['url'])) { - $platform = $data['platform']; - $version = $data['version']; - $network = Protocol::DIASPORA; - } - - if (!empty($data['site_name'])) { - $site_name = $data['site_name']; - } - - if (!empty($data['channels_total'])) { - $registered_users = $data['channels_total']; - } - - if (!empty($data['register_policy'])) { - switch ($data['register_policy']) { - case "REGISTER_OPEN": - $register_policy = Register::OPEN; - break; - - case "REGISTER_APPROVE": - $register_policy = Register::APPROVE; - break; - - case "REGISTER_CLOSED": - default: - $register_policy = Register::CLOSED; - break; - } - } - } else { - // Test for Hubzilla, Redmatrix or Friendica - $curlResult = Network::curl($server_url."/api/statusnet/config.json"); - - if ($curlResult->isSuccess()) { - $data = json_decode($curlResult->getBody(), true); - - if (isset($data['site']['server'])) { - if (isset($data['site']['platform'])) { - $platform = $data['site']['platform']['PLATFORM_NAME']; - $version = $data['site']['platform']['STD_VERSION']; - $network = Protocol::DIASPORA; - } - - if (isset($data['site']['BlaBlaNet'])) { - $platform = $data['site']['BlaBlaNet']['PLATFORM_NAME']; - $version = $data['site']['BlaBlaNet']['STD_VERSION']; - $network = Protocol::DIASPORA; - } - - if (isset($data['site']['hubzilla'])) { - $platform = $data['site']['hubzilla']['PLATFORM_NAME']; - $version = $data['site']['hubzilla']['RED_VERSION']; - $network = Protocol::DIASPORA; - } - - if (isset($data['site']['redmatrix'])) { - if (isset($data['site']['redmatrix']['PLATFORM_NAME'])) { - $platform = $data['site']['redmatrix']['PLATFORM_NAME']; - } elseif (isset($data['site']['redmatrix']['RED_PLATFORM'])) { - $platform = $data['site']['redmatrix']['RED_PLATFORM']; - } - - $version = $data['site']['redmatrix']['RED_VERSION']; - $network = Protocol::DIASPORA; - } - - if (isset($data['site']['friendica'])) { - $platform = $data['site']['friendica']['FRIENDICA_PLATFORM']; - $version = $data['site']['friendica']['FRIENDICA_VERSION']; - $network = Protocol::DFRN; - } - - $site_name = $data['site']['name']; - - $private = false; - $inviteonly = false; - $closed = false; - - if (!empty($data['site']['closed'])) { - $closed = self::toBoolean($data['site']['closed']); - } - - if (!empty($data['site']['private'])) { - $private = self::toBoolean($data['site']['private']); - } - - if (!empty($data['site']['inviteonly'])) { - $inviteonly = self::toBoolean($data['site']['inviteonly']); - } - - if (!$closed && !$private and $inviteonly) { - $register_policy = Register::APPROVE; - } elseif (!$closed && !$private) { - $register_policy = Register::OPEN; - } else { - $register_policy = Register::CLOSED; - } - } - } - } - } - - // Query statistics.json. Optional package for Diaspora, Friendica and Redmatrix - if (!$failure) { - $curlResult = Network::curl($server_url . "/statistics.json"); - - if ($curlResult->isSuccess()) { - $data = json_decode($curlResult->getBody(), true); - - if (isset($data['version'])) { - $version = $data['version']; - // Version numbers on statistics.json are presented with additional info, e.g.: - // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191. - $version = preg_replace("=(.+)-(.{4,})=ism", "$1", $version); - } - - if (!empty($data['name'])) { - $site_name = $data['name']; - } - - if (!empty($data['network'])) { - $platform = $data['network']; - } - - if ($platform == "Diaspora") { - $network = Protocol::DIASPORA; - } - - if (!empty($data['registrations_open']) && $data['registrations_open']) { - $register_policy = Register::OPEN; - } else { - $register_policy = Register::CLOSED; - } - } - } - - // Query nodeinfo. Working for (at least) Diaspora and Friendica. - if (!$failure) { - $server = self::fetchNodeinfo($server_url); - - if (!empty($server)) { - $register_policy = $server['register_policy']; - - if (isset($server['platform'])) { - $platform = $server['platform']; - } - - if (isset($server['network'])) { - $network = $server['network']; - } - - if (isset($server['version'])) { - $version = $server['version']; - } - - if (isset($server['site_name'])) { - $site_name = $server['site_name']; - } - - if (isset($server['registered-users'])) { - $registered_users = $server['registered-users']; - } - } - } - - // Check for noscrape - // Friendica servers could be detected as OStatus servers - if (!$failure && in_array($network, [Protocol::DFRN, Protocol::OSTATUS])) { - $curlResult = Network::curl($server_url . "/friendica/json"); - - if (!$curlResult->isSuccess()) { - $curlResult = Network::curl($server_url . "/friendika/json"); - } - - if ($curlResult->isSuccess()) { - $data = json_decode($curlResult->getBody(), true); - - if (isset($data['version'])) { - $network = Protocol::DFRN; - - if (!empty($data['no_scrape_url'])) { - $noscrape = $data['no_scrape_url']; - } - - $version = $data['version']; - - if (!empty($data['site_name'])) { - $site_name = $data['site_name']; - } - - $info = defaults($data, 'info', ''); - - $register_policy = defaults($data, 'register_policy', 'REGISTER_CLOSED'); - switch ($register_policy) { - case 'REGISTER_OPEN': - $register_policy = Register::OPEN; - break; - - case 'REGISTER_APPROVE': - $register_policy = Register::APPROVE; - break; - - default: - Logger::log("Register policy '$register_policy' from $server_url is invalid."); - // Defaulting to closed - - case 'REGISTER_CLOSED': - case 'REGISTER_INVITATION': - $register_policy = Register::CLOSED; - break; - } - - $platform = defaults($data, 'platform', ''); - } - } - } - - // Every server has got at least an admin account - if (!$failure && ($registered_users == 0)) { - $registered_users = 1; - } - - if ($possible_failure && !$failure) { - $failure = true; - } - - if ($failure) { - $last_contact = $orig_last_contact; - $last_failure = DateTimeFormat::utcNow(); - } else { - $last_contact = DateTimeFormat::utcNow(); - $last_failure = $orig_last_failure; - } - - if (($last_contact <= $last_failure) && !$failure) { - Logger::log("Server ".$server_url." seems to be alive, but last contact wasn't set - could be a bug", Logger::DEBUG); - } elseif (($last_contact >= $last_failure) && $failure) { - Logger::log("Server ".$server_url." seems to be dead, but last failure wasn't set - could be a bug", Logger::DEBUG); - } - - // Check again if the server exists - $found = DBA::exists('gserver', ['nurl' => Strings::normaliseLink($server_url)]); - - $version = strip_tags($version); - $site_name = strip_tags($site_name); - $info = strip_tags($info); - $platform = strip_tags($platform); - - $fields = ['url' => $server_url, 'version' => $version, - 'site_name' => $site_name, 'info' => $info, 'register_policy' => $register_policy, - 'poco' => $poco, 'noscrape' => $noscrape, 'network' => $network, - 'platform' => $platform, 'registered-users' => $registered_users, - 'last_contact' => $last_contact, 'last_failure' => $last_failure]; - - if ($found) { - DBA::update('gserver', $fields, ['nurl' => Strings::normaliseLink($server_url)]); - } elseif (!$failure) { - $fields['nurl'] = Strings::normaliseLink($server_url); - $fields['created'] = DateTimeFormat::utcNow(); - DBA::insert('gserver', $fields); - } - - if (!$failure && in_array($fields['network'], [Protocol::DFRN, Protocol::DIASPORA])) { - self::discoverRelay($server_url); - } - - Logger::log("End discovery for server " . $server_url, Logger::DEBUG); - - return !$failure; - } - - /** - * @brief Fetch relay data from a given server url - * - * @param string $server_url address of the server - * @throws \Friendica\Network\HTTPException\InternalServerErrorException - */ - private static function discoverRelay($server_url) - { - Logger::log("Discover relay data for server " . $server_url, Logger::DEBUG); - - $curlResult = Network::curl($server_url . "/.well-known/x-social-relay"); - - if (!$curlResult->isSuccess()) { - return; - } - - $data = json_decode($curlResult->getBody(), true); - - if (!is_array($data)) { - return; - } - - $gserver = DBA::selectFirst('gserver', ['id', 'relay-subscribe', 'relay-scope'], ['nurl' => Strings::normaliseLink($server_url)]); - - if (!DBA::isResult($gserver)) { - return; - } - - if (($gserver['relay-subscribe'] != $data['subscribe']) || ($gserver['relay-scope'] != $data['scope'])) { - $fields = ['relay-subscribe' => $data['subscribe'], 'relay-scope' => $data['scope']]; - DBA::update('gserver', $fields, ['id' => $gserver['id']]); - } - - DBA::delete('gserver-tag', ['gserver-id' => $gserver['id']]); - - if ($data['scope'] == 'tags') { - // Avoid duplicates - $tags = []; - foreach ($data['tags'] as $tag) { - $tag = mb_strtolower($tag); - if (strlen($tag) < 100) { - $tags[$tag] = $tag; - } - } - - foreach ($tags as $tag) { - DBA::insert('gserver-tag', ['gserver-id' => $gserver['id'], 'tag' => $tag], true); - } - } - - // Create or update the relay contact - $fields = []; - if (isset($data['protocols'])) { - if (isset($data['protocols']['diaspora'])) { - $fields['network'] = Protocol::DIASPORA; - - if (isset($data['protocols']['diaspora']['receive'])) { - $fields['batch'] = $data['protocols']['diaspora']['receive']; - } elseif (is_string($data['protocols']['diaspora'])) { - $fields['batch'] = $data['protocols']['diaspora']; - } - } - - if (isset($data['protocols']['dfrn'])) { - $fields['network'] = Protocol::DFRN; - - if (isset($data['protocols']['dfrn']['receive'])) { - $fields['batch'] = $data['protocols']['dfrn']['receive']; - } elseif (is_string($data['protocols']['dfrn'])) { - $fields['batch'] = $data['protocols']['dfrn']; - } - } - } - Diaspora::setRelayContact($server_url, $fields); - } - /** * @brief Returns a list of all known servers * @return array List of server urls @@ -1577,7 +636,7 @@ class PortableContact // $servers = json_decode($result->getBody(), true); // foreach($servers['data'] as $server) - // self::checkServer($server['instance_address']); + // GServer::check($server['instance_address']); // } //} @@ -1646,7 +705,7 @@ class PortableContact return true; } else { // If the server hadn't replied correctly, then force a sanity check - self::checkServer($server["url"], $server["network"], true); + GServer::check($server["url"], true); // If we couldn't reach the server, we will try it some time later $fields = ['last_poco_query' => DateTimeFormat::utcNow()]; @@ -1681,7 +740,7 @@ class PortableContact if (DBA::isResult($gservers)) { foreach ($gservers as $gserver) { - if (!self::checkServer($gserver['url'], $gserver['network'])) { + if (!GServer::check($gserver['url'])) { // The server is not reachable? Okay, then we will try it later $fields = ['last_poco_query' => DateTimeFormat::utcNow()]; DBA::update('gserver', $fields, ['nurl' => $gserver['nurl']]); diff --git a/src/Worker/DiscoverPoCo.php b/src/Worker/DiscoverPoCo.php index 8a783e1865..bee94a77d1 100644 --- a/src/Worker/DiscoverPoCo.php +++ b/src/Worker/DiscoverPoCo.php @@ -12,6 +12,7 @@ use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\Model\GContact; use Friendica\Model\Contact; +use Friendica\Model\GServer; use Friendica\Network\Probe; use Friendica\Protocol\PortableContact; use Friendica\Util\DateTimeFormat; @@ -86,7 +87,7 @@ class DiscoverPoCo return; } $result = "Checking server ".$server_url." - "; - $ret = PortableContact::checkServer($server_url); + $ret = GServer::check($server_url); if ($ret) { $result .= "success"; } else { @@ -186,7 +187,7 @@ class DiscoverPoCo $server_url = $user["server_url"]; } - if ((($server_url == "") && ($user["network"] == Protocol::FEED)) || $force_update || PortableContact::checkServer($server_url, $user["network"])) { + if ((($server_url == "") && ($user["network"] == Protocol::FEED)) || $force_update || GServer::check($server_url)) { Logger::log('Check profile '.$user["url"]); Worker::add(PRIORITY_LOW, "DiscoverPoCo", "check_profile", $user["url"]); @@ -237,7 +238,7 @@ class DiscoverPoCo $server_url = Contact::getBasepath($jj->url); if ($server_url != '') { - if (!PortableContact::checkServer($server_url)) { + if (!GServer::check($server_url)) { Logger::log("Friendica server ".$server_url." doesn't answer.", Logger::DEBUG); continue; } diff --git a/src/Worker/PubSubPublish.php b/src/Worker/PubSubPublish.php index b6061f8199..df3776b48f 100644 --- a/src/Worker/PubSubPublish.php +++ b/src/Worker/PubSubPublish.php @@ -9,6 +9,7 @@ use Friendica\Core\Logger; use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\Model\PushSubscriber; +use Friendica\Model\GServer; use Friendica\Protocol\OStatus; use Friendica\Util\Network; @@ -30,7 +31,7 @@ class PubSubPublish return; } - /// @todo Check server status with PortableContact::checkServer() + /// @todo Check server status with GServer::check() // Before this can be done we need a way to safely detect the server url. Logger::log("Generate feed of user " . $subscriber['nickname']. " to " . $subscriber['callback_url']. " - last updated " . $subscriber['last_update'], Logger::DEBUG); From a7e518acbd8ecc39276f24f74113b18529fcebb1 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 4 Oct 2019 05:42:54 +0000 Subject: [PATCH 09/13] Readded the network parameter --- src/Model/GContact.php | 2 +- src/Model/GServer.php | 12 +++++++----- src/Protocol/PortableContact.php | 8 ++++---- src/Worker/DiscoverPoCo.php | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Model/GContact.php b/src/Model/GContact.php index 89737cc084..23415d1078 100644 --- a/src/Model/GContact.php +++ b/src/Model/GContact.php @@ -231,7 +231,7 @@ class GContact } // The server URL doesn't seem to be valid, so we don't store it. - if (!GServer::check($gcontact['server_url'])) { + if (!GServer::check($gcontact['server_url'], $gcontact['network'])) { $gcontact['server_url'] = ""; } diff --git a/src/Model/GServer.php b/src/Model/GServer.php index b3cc6fc41b..05ea0baacc 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -25,7 +25,7 @@ use Friendica\Protocol\Diaspora; */ class GServer { - public static function check($server_url, $force = false) + public static function check($server_url, $network = '', $force = false) { // Unify the server address $server_url = trim($server_url, '/'); @@ -57,13 +57,15 @@ class GServer } if (!$force && !PortableContact::updateNeeded($gserver['created'], '', $last_failure, $last_contact)) { - Logger::info('Use cached data', ['server' => $server_url]); + Logger::info('No update needed', ['server' => $server_url]); return ($last_contact >= $last_failure); } + Logger::info('Server is outdated. Start discovery.', ['Server' => $server_url, 'Force' => $force, 'Created' => $gserver['created'], 'Failure' => $last_failure, 'Contact' => $last_contact]); + } else { + Logger::info('Server is unknown. Start discovery.', ['Server' => $server_url]); } - Logger::info('Server is outdated or unknown. Start discovery.', ['Server' => $server_url, 'Force' => $force, 'Created' => $gserver['created'], 'Failure' => $last_failure, 'Contact' => $last_contact]); - return self::detect($server_url); + return self::detect($server_url, $network); } /** @@ -74,7 +76,7 @@ class GServer * * @return boolean 'true' if server could be detected */ - public static function detect($url) + public static function detect($url, $network = '') { $serverdata = []; diff --git a/src/Protocol/PortableContact.php b/src/Protocol/PortableContact.php index 826fc108c8..235c17ff59 100644 --- a/src/Protocol/PortableContact.php +++ b/src/Protocol/PortableContact.php @@ -224,7 +224,7 @@ class PortableContact return true; } - return GServer::check($server, $force); + return GServer::check($server, $network, $force); } public static function alternateOStatusUrl($url) @@ -268,7 +268,7 @@ class PortableContact } if ($server_url != "") { - if (!GServer::check($server_url, $force)) { + if (!GServer::check($server_url, $gcontacts[0]["network"], $force)) { if ($force) { $fields = ['last_failure' => DateTimeFormat::utcNow()]; DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($profile)]); @@ -705,7 +705,7 @@ class PortableContact return true; } else { // If the server hadn't replied correctly, then force a sanity check - GServer::check($server["url"], true); + GServer::check($server["url"], $server["network"], true); // If we couldn't reach the server, we will try it some time later $fields = ['last_poco_query' => DateTimeFormat::utcNow()]; @@ -740,7 +740,7 @@ class PortableContact if (DBA::isResult($gservers)) { foreach ($gservers as $gserver) { - if (!GServer::check($gserver['url'])) { + if (!GServer::check($gserver['url'], $gserver['network'])) { // The server is not reachable? Okay, then we will try it later $fields = ['last_poco_query' => DateTimeFormat::utcNow()]; DBA::update('gserver', $fields, ['nurl' => $gserver['nurl']]); diff --git a/src/Worker/DiscoverPoCo.php b/src/Worker/DiscoverPoCo.php index bee94a77d1..5ce4c97d6f 100644 --- a/src/Worker/DiscoverPoCo.php +++ b/src/Worker/DiscoverPoCo.php @@ -187,7 +187,7 @@ class DiscoverPoCo $server_url = $user["server_url"]; } - if ((($server_url == "") && ($user["network"] == Protocol::FEED)) || $force_update || GServer::check($server_url)) { + if ((($server_url == "") && ($user["network"] == Protocol::FEED)) || $force_update || GServer::check($server_url, $user["network"])) { Logger::log('Check profile '.$user["url"]); Worker::add(PRIORITY_LOW, "DiscoverPoCo", "check_profile", $user["url"]); From cd3dada39cd01cad8cf2801291f0f0603fc35d32 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 4 Oct 2019 06:33:16 +0000 Subject: [PATCH 10/13] Detect network type via the contacts --- src/Model/GServer.php | 69 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 6 deletions(-) diff --git a/src/Model/GServer.php b/src/Model/GServer.php index 05ea0baacc..64fa3dfcf4 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -19,6 +19,7 @@ use Friendica\Util\XML; use Friendica\Core\Logger; use Friendica\Protocol\PortableContact; use Friendica\Protocol\Diaspora; +use Friendica\Network\Probe; /** * @brief This class handles GServer related functions @@ -145,6 +146,11 @@ class GServer $serverdata['network'] = Protocol::PHANTOM; } + // When we hadn't been able to detect the network type, we use the hint from the parameter + if (($serverdata['network'] == Protocol::PHANTOM) && !empty($network)) { + $serverdata['network'] = $network; + } + // Check host-meta for phantom networks. // Although this is not needed, it is a good indicator for a living system, // since most systems had implemented it. @@ -164,21 +170,32 @@ class GServer $registeredUsers = 1; } - $gcontacts = DBA::count('gcontact', ['server_url' => [$url, $serverdata['nurl']]]); - $apcontacts = DBA::count('apcontact', ['baseurl' => [$url, $serverdata['nurl']]]); - $contacts = DBA::count('contact', ['uid' => 0, 'baseurl' => [$url, $serverdata['nurl']]]); - $serverdata['registered-users'] = max($gcontacts, $apcontacts, $contacts, $registeredUsers); + if ($serverdata['network'] != Protocol::PHANTOM) { + $gcontacts = DBA::count('gcontact', ['server_url' => [$url, $serverdata['nurl']]]); + $apcontacts = DBA::count('apcontact', ['baseurl' => [$url, $serverdata['nurl']]]); + $contacts = DBA::count('contact', ['uid' => 0, 'baseurl' => [$url, $serverdata['nurl']]]); + $serverdata['registered-users'] = max($gcontacts, $apcontacts, $contacts, $registeredUsers); + } else { + $serverdata['registered-users'] = $registeredUsers; + $serverdata = self::detectNetworkViaContacts($url, $serverdata); + } $serverdata['last_contact'] = DateTimeFormat::utcNow(); - if (!DBA::exists('gserver', ['nurl' => Strings::normaliseLink($url)])) { + $gserver = DBA::selectFirst('gserver', ['network'], ['nurl' => Strings::normaliseLink($url)]); + if (!DBA::isResult($gserver)) { $serverdata['created'] = DateTimeFormat::utcNow(); $ret = DBA::insert('gserver', $serverdata); } else { + // Don't override the network with "unknown" when there had been a valid entry before + if (($serverdata['network'] == Protocol::PHANTOM) && !empty($gserver['network'])) { + unset($serverdata['network']); + } + $ret = DBA::update('gserver', $serverdata, ['nurl' => $serverdata['nurl']]); } - if (in_array($serverdata['network'], [Protocol::DFRN, Protocol::DIASPORA])) { + if (!empty($serverdata['network']) && in_array($serverdata['network'], [Protocol::DFRN, Protocol::DIASPORA])) { self::discoverRelay($url); } @@ -601,6 +618,46 @@ class GServer return $valid; } + private static function detectNetworkViaContacts($url, $serverdata) + { + $contacts = ''; + $fields = ['nurl', 'url']; + + $gcontacts = DBA::select('gcontact', $fields, ['server_url' => [$url, $serverdata['nurl']]]); + while ($gcontact = DBA::fetch($gcontacts)) { + $contacts[$gcontact['nurl']] = $gcontact['url']; + } + DBA::close($gcontacts); + + $apcontacts = DBA::select('apcontact', $fields, ['baseurl' => [$url, $serverdata['nurl']]]); + while ($gcontact = DBA::fetch($gcontacts)) { + $contacts[$apcontact['nurl']] = $apcontact['url']; + } + DBA::close($apcontacts); + + $pcontacts = DBA::select('contact', $fields, ['uid' => 0, 'baseurl' => [$url, $serverdata['nurl']]]); + while ($gcontact = DBA::fetch($gcontacts)) { + $contacts[$pcontact['nurl']] = $pcontact['url']; + } + DBA::close($pcontacts); + + if (empty($contacts)) { + return $serverdata; + } + + foreach ($contacts as $contact) { + $probed = Probe::uri($contact); + if (in_array($probed['network'], Protocol::FEDERATED)) { + $serverdata['network'] = $probed['network']; + break; + } + } + + $serverdata['registered-users'] = max($serverdata['registered-users'], count($contacts)); + + return $serverdata; + } + private static function checkPoCo($url, $serverdata) { $curlResult = Network::curl($url. '/poco'); From 4d1cc44151490fa8ae9b9029be9da2d57d9bbfb9 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 4 Oct 2019 17:29:21 +0000 Subject: [PATCH 11/13] Added documentation --- src/Model/GServer.php | 161 +++++++++++++++++++++++++++++++++++------- 1 file changed, 136 insertions(+), 25 deletions(-) diff --git a/src/Model/GServer.php b/src/Model/GServer.php index 64fa3dfcf4..729ae9d3d0 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -2,7 +2,7 @@ /** * @file src/Model/GServer.php - * @brief This file includes the GServer class to handle with servers + * This file includes the GServer class to handle with servers */ namespace Friendica\Model; @@ -22,11 +22,20 @@ use Friendica\Protocol\Diaspora; use Friendica\Network\Probe; /** - * @brief This class handles GServer related functions + * This class handles GServer related functions */ class GServer { - public static function check($server_url, $network = '', $force = false) + /** + * Checks the state of the given server. + * + * @param string $server_url URL of the given server + * @param string $network Network value that is used, when detection failed + * @param boolean $force Force an update. + * + * @return boolean "true" if server seems vital + */ + public static function check(string $server_url, string $network = '', bool $force = false) { // Unify the server address $server_url = trim($server_url, '/'); @@ -73,11 +82,12 @@ class GServer * Detect server data (type, protocol, version number, ...) * The detected data is then updated or inserted in the gserver table. * - * @param string $url Server url + * @param string $url URL of the given server + * @param string $network Network value that is used, when detection failed * * @return boolean 'true' if server could be detected */ - public static function detect($url, $network = '') + public static function detect(string $url, string $network = '') { $serverdata = []; @@ -203,12 +213,12 @@ class GServer } /** - * @brief Fetch relay data from a given server url + * Fetch relay data from a given server url * * @param string $server_url address of the server * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - private static function discoverRelay($server_url) + private static function discoverRelay(string $server_url) { Logger::info('Discover relay data', ['server' => $server_url]); @@ -275,7 +285,14 @@ class GServer Diaspora::setRelayContact($server_url, $fields); } - private static function fetchStatistics($url) + /** + * Fetch server data from '/statistics.json' on the given server + * + * @param string $url URL of the given server + * + * @return array server data + */ + private static function fetchStatistics(string $url) { $curlResult = Network::curl($url . '/statistics.json'); if (!$curlResult->isSuccess()) { @@ -325,13 +342,13 @@ class GServer } /** - * @brief Detect server type by using the nodeinfo data + * Detect server type by using the nodeinfo data * * @param string $url address of the server * @return array Server data * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - private static function fetchNodeinfo($url, $curlResult) + private static function fetchNodeinfo(string $url, $curlResult) { $nodeinfo = json_decode($curlResult->getBody(), true); @@ -374,13 +391,13 @@ class GServer } /** - * @brief Parses Nodeinfo 1 + * Parses Nodeinfo 1 * * @param string $nodeinfo_url address of the nodeinfo path * @return array Server data * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - private static function parseNodeinfo1($nodeinfo_url) + private static function parseNodeinfo1(string $nodeinfo_url) { $curlResult = Network::curl($nodeinfo_url); @@ -452,13 +469,13 @@ class GServer } /** - * @brief Parses Nodeinfo 2 + * Parses Nodeinfo 2 * * @param string $nodeinfo_url address of the nodeinfo path * @return array Server data * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - private static function parseNodeinfo2($nodeinfo_url) + private static function parseNodeinfo2(string $nodeinfo_url) { $curlResult = Network::curl($nodeinfo_url); if (!$curlResult->isSuccess()) { @@ -528,7 +545,15 @@ class GServer return $server; } - private static function fetchSiteinfo($url, $serverdata) + /** + * Fetch server information from a "siteinfo.json" file on the given server + * + * @param string $url URL of the given server + * @param array $serverdata array with server data + * + * @return array server data + */ + private static function fetchSiteinfo(string $url, array $serverdata) { $curlResult = Network::curl($url . '/siteinfo.json'); if (!$curlResult->isSuccess()) { @@ -585,7 +610,14 @@ class GServer return $serverdata; } - private static function validHostMeta($url) + /** + * Checks if the server contains a valid host meta file + * + * @param string $url URL of the given server + * + * @return boolean "true" if the server seems to be vital + */ + private static function validHostMeta(string $url) { $xrd_timeout = Config::get('system', 'xrd_timeout'); $curlResult = Network::curl($url . '/.well-known/host-meta', false, ['timeout' => $xrd_timeout]); @@ -618,7 +650,15 @@ class GServer return $valid; } - private static function detectNetworkViaContacts($url, $serverdata) + /** + * Detect the network of the given server via their known contacts + * + * @param string $url URL of the given server + * @param array $serverdata array with server data + * + * @return array server data + */ + private static function detectNetworkViaContacts(string $url, array $serverdata) { $contacts = ''; $fields = ['nurl', 'url']; @@ -658,7 +698,15 @@ class GServer return $serverdata; } - private static function checkPoCo($url, $serverdata) + /** + * Checks if the given server does have a "poco" endpoint + * + * @param string $url URL of the given server + * @param array $serverdata array with server data + * + * @return array server data + */ + private static function checkPoCo(string $url, array $serverdata) { $curlResult = Network::curl($url. '/poco'); if (!$curlResult->isSuccess()) { @@ -681,7 +729,15 @@ class GServer return $serverdata; } - private static function detectNextcloud($url, $serverdata) + /** + * Detects the version number of a given server when it was a NextCloud installation + * + * @param string $url URL of the given server + * @param array $serverdata array with server data + * + * @return array server data + */ + private static function detectNextcloud(string $url, array $serverdata) { $curlResult = Network::curl($url . '/status.php'); @@ -703,7 +759,15 @@ class GServer return $serverdata; } - private static function detectMastodonAlikes($url, $serverdata) + /** + * Detects data from a given server url if it was a mastodon alike system + * + * @param string $url URL of the given server + * @param array $serverdata array with server data + * + * @return array server data + */ + private static function detectMastodonAlikes(string $url, array $serverdata) { $curlResult = Network::curl($url . '/api/v1/instance'); @@ -747,7 +811,15 @@ class GServer return $serverdata; } - private static function detectHubzilla($url, $serverdata) + /** + * Detects data from typical Hubzilla endpoints + * + * @param string $url URL of the given server + * @param array $serverdata array with server data + * + * @return array server data + */ + private static function detectHubzilla(string $url, array $serverdata) { $curlResult = Network::curl($url . '/api/statusnet/config.json'); if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { @@ -813,6 +885,13 @@ class GServer return $serverdata; } + /** + * Converts input value to a boolean value + * + * @param string|integer $val + * + * @return boolean + */ private static function toBoolean($val) { if (($val == 'true') || ($val == 1)) { @@ -824,7 +903,15 @@ class GServer return $val; } - private static function detectGNUSocial($url, $serverdata) + /** + * Detect if the URL belongs to a GNU Social server + * + * @param string $url URL of the given server + * @param array $serverdata array with server data + * + * @return array server data + */ + private static function detectGNUSocial(string $url, array $serverdata) { $curlResult = Network::curl($url . '/api/statusnet/version.json'); @@ -852,7 +939,15 @@ class GServer return $serverdata; } - private static function detectFriendica($url, $serverdata) + /** + * Detect if the URL belongs to a Friendica server + * + * @param string $url URL of the given server + * @param array $serverdata array with server data + * + * @return array server data + */ + private static function detectFriendica(string $url, array $serverdata) { $curlResult = Network::curl($url . '/friendica/json'); if (!$curlResult->isSuccess()) { @@ -908,7 +1003,15 @@ class GServer return $serverdata; } - private static function analyseRootBody($curlResult, $serverdata) + /** + * Analyses the landing page of a given server for hints about type and system of that server + * + * @param object $curlResult result of curl execution + * @param array $serverdata array with server data + * + * @return array server data + */ + private static function analyseRootBody($curlResult, array $serverdata) { $doc = new DOMDocument(); @$doc->loadHTML($curlResult->getBody()); @@ -1017,7 +1120,15 @@ class GServer return $serverdata; } - private static function analyseRootHeader($curlResult, $serverdata) + /** + * Analyses the header data of a given server for hints about type and system of that server + * + * @param object $curlResult result of curl execution + * @param array $serverdata array with server data + * + * @return array server data + */ + private static function analyseRootHeader($curlResult, array $serverdata) { if ($curlResult->getHeader('server') == 'Mastodon') { $serverdata['platform'] = 'mastodon'; From aa75e749be04d8443add271b7e9c70b3e54bfee4 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 5 Oct 2019 04:22:16 +0000 Subject: [PATCH 12/13] Enhanced documentation --- src/Model/GServer.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Model/GServer.php b/src/Model/GServer.php index 729ae9d3d0..26824a237b 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -33,7 +33,7 @@ class GServer * @param string $network Network value that is used, when detection failed * @param boolean $force Force an update. * - * @return boolean "true" if server seems vital + * @return boolean 'true' if server seems vital */ public static function check(string $server_url, string $network = '', bool $force = false) { @@ -53,8 +53,8 @@ class GServer DBA::update('gserver', $fields, $condition); } - $last_contact = $gserver["last_contact"]; - $last_failure = $gserver["last_failure"]; + $last_contact = $gserver['last_contact']; + $last_failure = $gserver['last_failure']; // See discussion under https://forum.friendi.ca/display/0b6b25a8135aabc37a5a0f5684081633 // It can happen that a zero date is in the database, but storing it again is forbidden. @@ -197,7 +197,7 @@ class GServer $serverdata['created'] = DateTimeFormat::utcNow(); $ret = DBA::insert('gserver', $serverdata); } else { - // Don't override the network with "unknown" when there had been a valid entry before + // Don't override the network with 'unknown' when there had been a valid entry before if (($serverdata['network'] == Protocol::PHANTOM) && !empty($gserver['network'])) { unset($serverdata['network']); } @@ -546,7 +546,7 @@ class GServer } /** - * Fetch server information from a "siteinfo.json" file on the given server + * Fetch server information from a 'siteinfo.json' file on the given server * * @param string $url URL of the given server * @param array $serverdata array with server data @@ -615,7 +615,7 @@ class GServer * * @param string $url URL of the given server * - * @return boolean "true" if the server seems to be vital + * @return boolean 'true' if the server seems to be vital */ private static function validHostMeta(string $url) { @@ -699,7 +699,9 @@ class GServer } /** - * Checks if the given server does have a "poco" endpoint + * Checks if the given server does have a '/poco' endpoint. + * This is used for the 'PortableContact' functionality, + * which is used by both Friendica and Hubzilla. * * @param string $url URL of the given server * @param array $serverdata array with server data From af5527f8968e2990aefba993560cc6c23ea88c02 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 5 Oct 2019 04:50:29 +0000 Subject: [PATCH 13/13] Unified return value types --- src/Model/GServer.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Model/GServer.php b/src/Model/GServer.php index 26824a237b..77d5563575 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -402,13 +402,13 @@ class GServer $curlResult = Network::curl($nodeinfo_url); if (!$curlResult->isSuccess()) { - return false; + return []; } $nodeinfo = json_decode($curlResult->getBody(), true); if (!is_array($nodeinfo)) { - return false; + return []; } $server = []; @@ -461,8 +461,8 @@ class GServer } } - if (!$server) { - return false; + if (empty($server)) { + return []; } return $server; @@ -479,13 +479,13 @@ class GServer { $curlResult = Network::curl($nodeinfo_url); if (!$curlResult->isSuccess()) { - return false; + return []; } $nodeinfo = json_decode($curlResult->getBody(), true); if (!is_array($nodeinfo)) { - return false; + return []; } $server = []; @@ -539,7 +539,7 @@ class GServer } if (empty($server)) { - return false; + return []; } return $server;