<?php
/**
- * @copyright Copyright (C) 2010-2023, the Friendica project
+ * @copyright Copyright (C) 2010-2024, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
use Exception;
use Friendica\Core\Logger;
use Friendica\Core\Protocol;
-use Friendica\Core\System;
use Friendica\Core\Worker;
use Friendica\Database\Database;
use Friendica\Database\DBA;
const DETECT_MASTODON_API = 16;
const DETECT_STATUS_PHP = 17; // Nextcloud
const DETECT_V1_CONFIG = 18;
- const DETECT_PUMPIO = 19; // Deprecated
const DETECT_SYSTEM_ACTOR = 20; // Mistpark, Osada, Roadhouse, Zap
+ const DETECT_THREADS = 21;
// Standardized endpoints
const DETECT_STATISTICS_JSON = 100;
const DETECT_NODEINFO_210 = 103;
/**
- * Check for the existance of a server and adds it in the background if not existant
+ * Check for the existence of a server and adds it in the background if not existant
*
* @param string $url
* @param boolean $only_nodeinfo
$gserver = DBA::selectFirst('gserver', ['id'], ['nurl' => Strings::normaliseLink($url)]);
if (DBA::isResult($gserver)) {
- Logger::debug('Got ID for URL', ['id' => $gserver['id'], 'url' => $url, 'callstack' => System::callstack(20)]);
+ Logger::debug('Got ID for URL', ['id' => $gserver['id'], 'url' => $url]);
if (Network::isUrlBlocked($url)) {
self::setBlockedById($gserver['id']);
* @param string $pattern
*
* @return array
- *
* @throws Exception
*/
public static function listByDomainPattern(string $pattern): array
} elseif (!empty($contact['baseurl'])) {
$server = $contact['baseurl'];
} elseif ($contact['network'] == Protocol::DIASPORA) {
- $parts = parse_url($contact['url']);
+ $parts = (array)parse_url($contact['url']);
unset($parts['path']);
$server = (string)Uri::fromParts($parts);
} else {
* @param bool $undetected
*
* @return string
- *
* @throws Exception
*/
public static function getNextUpdateDate(bool $success, string $created = '', string $last_contact = '', bool $undetected = false): string
return DateTimeFormat::utc('now +1 month');
}
- // The system hadn't been successul contacted for more than a month, so try again in three months
+ // The system hadn't been successful contacted for more than a month, so try again in three months
return DateTimeFormat::utc('now +3 month');
}
* Set failed server status
*
* @param string $url
+ * @return void
*/
public static function setFailureByUrl(string $url)
{
- $gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($url)]);
+ $nurl = Strings::normaliseLink($url);
+
+ $gserver = DBA::selectFirst('gserver', [], ['nurl' => $nurl]);
if (DBA::isResult($gserver)) {
$next_update = self::getNextUpdateDate(false, $gserver['created'], $gserver['last_contact']);
self::update(['url' => $url, 'failed' => true, 'blocked' => Network::isUrlBlocked($url), 'last_failure' => DateTimeFormat::utcNow(),
'next_contact' => $next_update, 'network' => Protocol::PHANTOM, 'detection-method' => null],
- ['nurl' => Strings::normaliseLink($url)]);
+ ['nurl' => $nurl]);
Logger::info('Set failed status for existing server', ['url' => $url]);
if (self::isDefunct($gserver)) {
self::archiveContacts($gserver['id']);
}
return;
}
- self::insert(['url' => $url, 'nurl' => Strings::normaliseLink($url),
+
+ self::insert(['url' => $url, 'nurl' => $nurl,
'network' => Protocol::PHANTOM, 'created' => DateTimeFormat::utcNow(),
'failed' => true, 'last_failure' => DateTimeFormat::utcNow()]);
Logger::info('Set failed status for new server', ['url' => $url]);
return false;
}
- // If the URL missmatches, then we mark the old entry as failure
+ // If the URL mismatches, then we mark the old entry as failure
if (!Strings::compareLink($url, $original_url)) {
self::setFailureByUrl($original_url);
- if (!self::getID($url, true)) {
+ if (!self::getID($url, true) && !Network::isUrlBlocked($url)) {
self::detect($url, $network, $only_nodeinfo);
}
return false;
(((parse_url($url, PHP_URL_HOST) != parse_url($valid_url, PHP_URL_HOST)) || (parse_url($url, PHP_URL_PATH) != parse_url($valid_url, PHP_URL_PATH))) && empty(parse_url($valid_url, PHP_URL_PATH)))) {
Logger::debug('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $valid_url]);
self::setFailureByUrl($url);
- if (!self::getID($valid_url, true)) {
+ if (!self::getID($valid_url, true) && !Network::isUrlBlocked($valid_url)) {
self::detect($valid_url, $network, $only_nodeinfo);
}
return false;
if ((parse_url($url, PHP_URL_HOST) != parse_url($valid_url, PHP_URL_HOST)) && (parse_url($url, PHP_URL_PATH) != parse_url($valid_url, PHP_URL_PATH)) &&
(parse_url($url, PHP_URL_PATH) == '')) {
Logger::debug('Found redirect. Mark old entry as failure and redirect to the basepath.', ['old' => $url, 'new' => $valid_url]);
- $parts = parse_url($valid_url);
+ $parts = (array)parse_url($valid_url);
unset($parts['path']);
$valid_url = (string)Uri::fromParts($parts);
self::setFailureByUrl($url);
- if (!self::getID($valid_url, true)) {
+ if (!self::getID($valid_url, true) && !Network::isUrlBlocked($valid_url)) {
self::detect($valid_url, $network, $only_nodeinfo);
}
return false;
return false;
}
- $serverdata = self::parseNodeinfo210($curlResult);
- if (empty($serverdata)) {
- $curlResult = DI::httpClient()->get($url . '/.well-known/nodeinfo', HttpClientAccept::JSON);
- $serverdata = self::fetchNodeinfo($url, $curlResult);
+ if (!empty($network) && !in_array($network, Protocol::NATIVE_SUPPORT)) {
+ $serverdata = ['detection-method' => self::DETECT_MANUAL, 'network' => $network, 'platform' => '', 'version' => '', 'site_name' => '', 'info' => ''];
+ } else {
+ $serverdata = self::parseNodeinfo210($curlResult);
+ if (empty($serverdata)) {
+ $curlResult = DI::httpClient()->get($url . '/.well-known/nodeinfo', HttpClientAccept::JSON);
+ $serverdata = self::fetchNodeinfo($url, $curlResult);
+ }
}
if ($only_nodeinfo && empty($serverdata)) {
}
if ($curlResult->isSuccess()) {
- $json = json_decode($curlResult->getBody(), true);
+ $json = json_decode($curlResult->getBodyString(), true);
if (!empty($json) && is_array($json)) {
$data = self::fetchDataFromSystemActor($json, $serverdata);
$serverdata = $data['server'];
if (!$html_fetched && !in_array($serverdata['detection-method'], [self::DETECT_SYSTEM_ACTOR, self::DETECT_AP_COLLECTION])) {
$curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML);
}
- } elseif (!$html_fetched && (strlen($curlResult->getBody()) < 1000)) {
+ } elseif (!$html_fetched && (strlen($curlResult->getBodyString()) < 1000)) {
$curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML);
}
}
}
- if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
+ if (!$curlResult->isSuccess() || empty($curlResult->getBodyString())) {
self::setFailureByUrl($url);
return false;
}
+ if (in_array($url, ['https://www.threads.net', 'https://threads.net'])) {
+ $serverdata['detection-method'] = self::DETECT_THREADS;
+ $serverdata['network'] = Protocol::ACTIVITYPUB;
+ $serverdata['platform'] = 'threads';
+ }
+
if (($serverdata['network'] == Protocol::PHANTOM) || in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
$serverdata = self::detectMastodonAlikes($url, $serverdata);
}
}
// All following checks are done for systems that always have got a "host-meta" endpoint.
- // With this check we don't have to waste time and ressources for dead systems.
+ // With this check we don't have to waste time and resources for dead systems.
// Also this hopefully prevents us from receiving abuse messages.
if (($serverdata['network'] == Protocol::PHANTOM) || in_array($serverdata['detection-method'], self::DETECT_UNSPECIFIC)) {
$validHostMeta = self::validHostMeta($url);
return false;
}
+ if (empty($serverdata['version']) && in_array($serverdata['platform'], ['osada']) && in_array($serverdata['detection-method'], [self::DETECT_CONTACTS, self::DETECT_BODY])) {
+ $serverdata['version'] = self::getNomadVersion($url);
+ }
+
// Detect the directory type
$serverdata['directory-type'] = self::DT_NONE;
}
$serverdata['registered-users'] = $serverdata['registered-users'] ?? 0;
+ $serverdata['next_contact'] = self::getNextUpdateDate(true, '', '', in_array($serverdata['network'], [Protocol::PHANTOM, Protocol::FEED]));
+ $serverdata['last_contact'] = DateTimeFormat::utcNow();
+ $serverdata['failed'] = false;
// Numbers above a reasonable value (10 millions) are ignored
if ($serverdata['registered-users'] > 10000000) {
* @param string $server_url address of the server
*
* @return void
- *
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
private static function discoverRelay(string $server_url)
return;
}
- $data = json_decode($curlResult->getBody(), true);
+ $data = json_decode($curlResult->getBodyString(), true);
if (!is_array($data)) {
return;
}
// Sanitize incoming data, see https://github.com/friendica/friendica/issues/8565
- $data['subscribe'] = (bool)$data['subscribe'] ?? false;
+ $data['subscribe'] = (bool)($data['subscribe'] ?? false);
if (!$data['subscribe'] || empty($data['scope']) || !in_array(strtolower($data['scope']), ['all', 'tags'])) {
$data['scope'] = '';
return $serverdata;
}
- $data = json_decode($curlResult->getBody(), true);
+ $data = json_decode($curlResult->getBodyString(), true);
if (empty($data)) {
return $serverdata;
}
return [];
}
- $nodeinfo = json_decode($httpResult->getBody(), true);
+ $nodeinfo = json_decode($httpResult->getBodyString(), true);
if (!is_array($nodeinfo) || empty($nodeinfo['links'])) {
return [];
Logger::info('Invalid nodeinfo format', ['url' => $url]);
continue;
}
+
if ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/1.0') {
- $nodeinfo1_url = $link['href'];
+ $nodeinfo1_url = Network::addBasePath($link['href'], $httpResult->getUrl());
} elseif ($link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/2.0') {
- $nodeinfo2_url = $link['href'];
+ $nodeinfo2_url = Network::addBasePath($link['href'], $httpResult->getUrl());
}
}
return [];
}
- $nodeinfo = json_decode($curlResult->getBody(), true);
+ $nodeinfo = json_decode($curlResult->getBodyString(), true);
if (!is_array($nodeinfo)) {
return [];
return [];
}
- $nodeinfo = json_decode($curlResult->getBody(), true);
+ $nodeinfo = json_decode($curlResult->getBodyString(), true);
if (!is_array($nodeinfo)) {
return [];
}
}
}
+ // Special treatment for NextCloud, since there you can freely define your software name
+ if (!empty($nodeinfo['rootUrl']) && in_array(parse_url($nodeinfo['rootUrl'], PHP_URL_PATH), ['/index.php/apps/social', '/apps/social'])) {
+ $server['platform'] = 'nextcloud';
+ }
+
if (!empty($nodeinfo['metadata']['nodeName'])) {
$server['site_name'] = $nodeinfo['metadata']['nodeName'];
}
if (!empty($nodeinfo['protocols'])) {
$protocols = [];
- foreach ($nodeinfo['protocols'] as $protocol) {
- if (is_string($protocol)) {
- $protocols[$protocol] = true;
+ if (is_string($nodeinfo['protocols'])) {
+ $protocols[$nodeinfo['protocols']] = true;
+ } else {
+ foreach ($nodeinfo['protocols'] as $protocol) {
+ if (is_string($protocol)) {
+ $protocols[$protocol] = true;
+ }
}
}
/**
* Parses NodeInfo2 protocol 1.0
*
- * @see https://github.com/jaywink/nodeinfo2/blob/master/PROTOCOL.md
- *
- * @param string $nodeinfo_url address of the nodeinfo path
+ * @param ICanHandleHttpResponses $httpResult
*
* @return array Server data
- *
+ * @see https://github.com/jaywink/nodeinfo2/blob/master/PROTOCOL.md
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
private static function parseNodeinfo210(ICanHandleHttpResponses $httpResult): array
return [];
}
- $nodeinfo = json_decode($httpResult->getBody(), true);
+ $nodeinfo = json_decode($httpResult->getBodyString(), true);
if (!is_array($nodeinfo)) {
return [];
if (!empty($nodeinfo['protocols'])) {
$protocols = [];
- foreach ($nodeinfo['protocols'] as $protocol) {
- if (is_string($protocol)) {
- $protocols[$protocol] = true;
+ if (is_string($nodeinfo['protocols'])) {
+ $protocols[$nodeinfo['protocols']] = true;
+ } else {
+ foreach ($nodeinfo['protocols'] as $protocol) {
+ if (is_string($protocol)) {
+ $protocols[$protocol] = true;
+ }
}
}
return $serverdata;
}
- $data = json_decode($curlResult->getBody(), true);
+ $data = json_decode($curlResult->getBodyString(), true);
if (empty($data)) {
return $serverdata;
}
$serverdata['detection-method'] = self::DETECT_SITEINFO_JSON;
}
- if (!empty($data['url'])) {
+ if (!empty($data['platform'])) {
$serverdata['platform'] = strtolower($data['platform']);
$serverdata['version'] = $data['version'] ?? 'N/A';
}
$serverdata['network'] = Protocol::ACTIVITYPUB;
$serverdata['site_name'] = JsonLD::fetchElement($actor, 'as:name', '@value');
$serverdata['info'] = JsonLD::fetchElement($actor, 'as:summary', '@value');
- if (!empty($actor['as:generator'])) {
+ if (self::isNomad($actor)) {
+ $serverdata['platform'] = self::getNomadName($actor['@id']);
+ $serverdata['version'] = self::getNomadVersion($actor['@id']);
+ $serverdata['detection-method'] = self::DETECT_SYSTEM_ACTOR;
+ } elseif (!empty($actor['as:generator'])) {
$generator = explode(' ', JsonLD::fetchElement($actor['as:generator'], 'as:name', '@value'));
$serverdata['platform'] = strtolower(array_shift($generator));
+ $serverdata['version'] = self::getNomadVersion($actor['@id']);
$serverdata['detection-method'] = self::DETECT_SYSTEM_ACTOR;
} else {
$serverdata['detection-method'] = self::DETECT_AP_ACTOR;
return ['server' => $serverdata, 'actor' => ''];
}
+ /**
+ * Detect if the given actor is a nomad account
+ *
+ * @param array $actor
+ * @return boolean
+ */
+ private static function isNomad(array $actor): bool
+ {
+ $tags = JsonLD::fetchElementArray($actor, 'as:tag');
+ if (empty($tags)) {
+ return false;
+ }
+
+ foreach ($tags as $tag) {
+ if ((($tag['as:name'] ?? '') == 'Protocol') && in_array('nomad', [$tag['sc:value'] ?? '', $tag['as:content'] ?? ''])) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Fetch the name of Nomad implementation
+ *
+ * @param string $url
+ * @return string
+ */
+ private static function getNomadName(string $url): string
+ {
+ $name = 'nomad';
+ $curlResult = DI::httpClient()->get($url . '/manifest', 'application/manifest+json');
+ if (!$curlResult->isSuccess() || ($curlResult->getBodyString() == '')) {
+ return $name;
+ }
+
+ $data = json_decode($curlResult->getBodyString(), true);
+ if (empty($data)) {
+ return $name;
+ }
+
+ return $data['name'] ?? $name;
+ }
+
+ /**
+ * Fetch the version of the Nomad installation
+ *
+ * @param string $url
+ * @return string
+ */
+ private static function getNomadVersion(string $url): string
+ {
+ $curlResult = DI::httpClient()->get($url . '/api/z/1.0/version', HttpClientAccept::JSON);
+ if (!$curlResult->isSuccess() || ($curlResult->getBodyString() == '')) {
+ return '';
+ }
+
+ $data = json_decode($curlResult->getBodyString(), true);
+ if (empty($data)) {
+ return '';
+ }
+ return $data ?? '';
+ }
+
/**
* Checks if the server contains a valid host meta file
*
return false;
}
- $xrd = XML::parseString($curlResult->getBody(), true);
+ $xrd = XML::parseString($curlResult->getBodyString(), true);
if (!is_object($xrd)) {
return false;
}
return $serverdata;
}
- $data = json_decode($curlResult->getBody(), true);
+ $data = json_decode($curlResult->getBodyString(), true);
if (empty($data)) {
return $serverdata;
}
return $serverdata;
}
- $data = json_decode($curlResult->getBody(), true);
+ $data = json_decode($curlResult->getBodyString(), true);
if (empty($data)) {
return $serverdata;
}
private static function detectPeertube(string $url, array $serverdata): array
{
$curlResult = DI::httpClient()->get($url . '/api/v1/config', HttpClientAccept::JSON);
- if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
+ if (!$curlResult->isSuccess() || ($curlResult->getBodyString() == '')) {
return $serverdata;
}
- $data = json_decode($curlResult->getBody(), true);
+ $data = json_decode($curlResult->getBodyString(), true);
if (empty($data)) {
return $serverdata;
}
private static function detectNextcloud(string $url, array $serverdata, bool $validHostMeta): array
{
$curlResult = DI::httpClient()->get($url . '/status.php', HttpClientAccept::JSON);
- if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
+ if (!$curlResult->isSuccess() || ($curlResult->getBodyString() == '')) {
return $serverdata;
}
- $data = json_decode($curlResult->getBody(), true);
+ $data = json_decode($curlResult->getBodyString(), true);
if (empty($data)) {
return $serverdata;
}
private static function fetchWeeklyUsage(string $url, array $serverdata): array
{
$curlResult = DI::httpClient()->get($url . '/api/v1/instance/activity', HttpClientAccept::JSON);
- if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
+ if (!$curlResult->isSuccess() || ($curlResult->getBodyString() == '')) {
return $serverdata;
}
- $data = json_decode($curlResult->getBody(), true);
+ $data = json_decode($curlResult->getBodyString(), true);
if (empty($data)) {
return $serverdata;
}
private static function detectMastodonAlikes(string $url, array $serverdata): array
{
$curlResult = DI::httpClient()->get($url . '/api/v1/instance', HttpClientAccept::JSON);
- if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
+ if (!$curlResult->isSuccess() || ($curlResult->getBodyString() == '')) {
return $serverdata;
}
- $data = json_decode($curlResult->getBody(), true);
+ $data = json_decode($curlResult->getBodyString(), true);
if (empty($data)) {
return $serverdata;
}
private static function detectHubzilla(string $url, array $serverdata): array
{
$curlResult = DI::httpClient()->get($url . '/api/statusnet/config.json', HttpClientAccept::JSON);
- if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
+ if (!$curlResult->isSuccess() || ($curlResult->getBodyString() == '')) {
return $serverdata;
}
- $data = json_decode($curlResult->getBody(), true);
+ $data = json_decode($curlResult->getBodyString(), true);
if (empty($data) || empty($data['site'])) {
return $serverdata;
}
{
// Test for GNU Social
$curlResult = DI::httpClient()->get($url . '/api/gnusocial/version.json', HttpClientAccept::JSON);
- if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') &&
- ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) {
+ if ($curlResult->isSuccess() && ($curlResult->getBodyString() != '{"error":"not implemented"}') &&
+ ($curlResult->getBodyString() != '') && (strlen($curlResult->getBodyString()) < 30)) {
$serverdata['platform'] = 'gnusocial';
// Remove junk that some GNU Social servers return
- $serverdata['version'] = str_replace(chr(239) . chr(187) . chr(191), '', $curlResult->getBody());
+ $serverdata['version'] = str_replace(chr(239) . chr(187) . chr(191), '', $curlResult->getBodyString());
$serverdata['version'] = str_replace(["\r", "\n", "\t"], '', $serverdata['version']);
$serverdata['version'] = trim($serverdata['version'], '"');
$serverdata['network'] = Protocol::OSTATUS;
// Test for Statusnet
$curlResult = DI::httpClient()->get($url . '/api/statusnet/version.json', HttpClientAccept::JSON);
- if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') &&
- ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) {
+ if ($curlResult->isSuccess() && ($curlResult->getBodyString() != '{"error":"not implemented"}') &&
+ ($curlResult->getBodyString() != '') && (strlen($curlResult->getBodyString()) < 30)) {
// Remove junk that some GNU Social servers return
- $serverdata['version'] = str_replace(chr(239).chr(187).chr(191), '', $curlResult->getBody());
+ $serverdata['version'] = str_replace(chr(239).chr(187).chr(191), '', $curlResult->getBodyString());
$serverdata['version'] = str_replace(["\r", "\n", "\t"], '', $serverdata['version']);
$serverdata['version'] = trim($serverdata['version'], '"');
return $serverdata;
}
- $data = json_decode($curlResult->getBody(), true);
+ $data = json_decode($curlResult->getBodyString(), true);
if (empty($data) || empty($data['version'])) {
return $serverdata;
}
*/
public static function discover()
{
+ if (!DI::config('system', 'discover_servers')) {
+ return;
+ }
+
// Update the server list
self::discoverFederation();
$requery_days = intval(DI::config()->get('system', 'poco_requery_days'));
- if ($requery_days == 0) {
- $requery_days = 7;
- }
-
$last_update = date('c', time() - (60 * 60 * 24 * $requery_days));
$gservers = DBA::select('gserver', ['id', 'url', 'nurl', 'network', 'poco', 'directory-type'],
}
}
- // Disvover Mastodon servers
+ // Discover Mastodon servers
$accesstoken = DI::config()->get('system', 'instances_social_key');
if (!empty($accesstoken)) {
$api = 'https://instances.social/api/1.0/instances/list?count=0';
$curlResult = DI::httpClient()->get($api, HttpClientAccept::JSON, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $accesstoken]]]);
if ($curlResult->isSuccess()) {
- $servers = json_decode($curlResult->getBody(), true);
+ $servers = json_decode($curlResult->getBodyString(), true);
if (!empty($servers['instances'])) {
foreach ($servers['instances'] as $server) {
}
}
- Logger::info('Protocol for server', ['protocol' => $protocol, 'old' => $old, 'id' => $gsid, 'url' => $gserver['url'], 'callstack' => System::callstack(20)]);
+ Logger::info('Protocol for server', ['protocol' => $protocol, 'old' => $old, 'id' => $gsid, 'url' => $gserver['url']]);
self::update(['protocol' => $protocol], ['id' => $gsid]);
}