From 7aec520b32e7b36aeb00dfbb2fb3c10e3e72bf95 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 24 Mar 2022 11:29:50 +0000 Subject: [PATCH 1/2] Don't display Peertube groups as forums --- database.sql | 20 ++++++++++++-------- src/Content/ForumManager.php | 22 ++++++++++++++-------- static/dbstructure.config.php | 2 +- static/dbview.config.php | 14 +++++++++----- 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/database.sql b/database.sql index 39e40c044..ffe020203 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ -- ------------------------------------------ -- Friendica 2022.05-dev (Siberian Iris) --- DB_UPDATE_VERSION 1452 +-- DB_UPDATE_VERSION 1453 -- ------------------------------------------ @@ -2412,10 +2412,8 @@ CREATE VIEW `account-view` AS SELECT `contact`.`archive` AS `archive`, `contact`.`deleted` AS `deleted`, `contact`.`blocked` AS `blocked`, - `contact`.`request` AS `dfrn-request`, `contact`.`notify` AS `dfrn-notify`, `contact`.`poll` AS `dfrn-poll`, - `contact`.`confirm` AS `dfrn-confirm`, `fcontact`.`guid` AS `diaspora-guid`, `fcontact`.`batch` AS `diaspora-batch`, `fcontact`.`notify` AS `diaspora-notify`, @@ -2431,11 +2429,15 @@ CREATE VIEW `account-view` AS SELECT `apcontact`.`generator` AS `ap-generator`, `apcontact`.`following_count` AS `ap-following_count`, `apcontact`.`followers_count` AS `ap-followers_count`, - `apcontact`.`statuses_count` AS `ap-statuses_count` + `apcontact`.`statuses_count` AS `ap-statuses_count`, + `gserver`.`site_name` AS `site_name`, + `gserver`.`platform` AS `platform`, + `gserver`.`version` AS `version` FROM `contact` LEFT JOIN `item-uri` ON `item-uri`.`id` = `contact`.`uri-id` LEFT JOIN `apcontact` ON `apcontact`.`uri-id` = `contact`.`uri-id` LEFT JOIN `fcontact` ON `fcontact`.`uri-id` = contact.`uri-id` + LEFT JOIN `gserver` ON `gserver`.`id` = contact.`gsid` WHERE `contact`.`uid` = 0; -- @@ -2511,10 +2513,8 @@ CREATE VIEW `account-user-view` AS SELECT `ucontact`.`subhub` AS `subhub`, `ucontact`.`hub-verify` AS `hub-verify`, `ucontact`.`reason` AS `reason`, - `contact`.`request` AS `dfrn-request`, `contact`.`notify` AS `dfrn-notify`, `contact`.`poll` AS `dfrn-poll`, - `contact`.`confirm` AS `dfrn-confirm`, `fcontact`.`guid` AS `diaspora-guid`, `fcontact`.`batch` AS `diaspora-batch`, `fcontact`.`notify` AS `diaspora-notify`, @@ -2530,12 +2530,16 @@ CREATE VIEW `account-user-view` AS SELECT `apcontact`.`generator` AS `ap-generator`, `apcontact`.`following_count` AS `ap-following_count`, `apcontact`.`followers_count` AS `ap-followers_count`, - `apcontact`.`statuses_count` AS `ap-statuses_count` + `apcontact`.`statuses_count` AS `ap-statuses_count`, + `gserver`.`site_name` AS `site_name`, + `gserver`.`platform` AS `platform`, + `gserver`.`version` AS `version` FROM `contact` AS `ucontact` INNER JOIN `contact` ON `contact`.`uri-id` = `ucontact`.`uri-id` AND `contact`.`uid` = 0 LEFT JOIN `item-uri` ON `item-uri`.`id` = `ucontact`.`uri-id` LEFT JOIN `apcontact` ON `apcontact`.`uri-id` = `ucontact`.`uri-id` - LEFT JOIN `fcontact` ON `fcontact`.`uri-id` = `ucontact`.`uri-id` AND `fcontact`.`network` = 'dspr'; + LEFT JOIN `fcontact` ON `fcontact`.`uri-id` = `ucontact`.`uri-id` AND `fcontact`.`network` = 'dspr' + LEFT JOIN `gserver` ON `gserver`.`id` = contact.`gsid`; -- -- VIEW pending-view diff --git a/src/Content/ForumManager.php b/src/Content/ForumManager.php index 0104d388f..51abaa49d 100644 --- a/src/Content/ForumManager.php +++ b/src/Content/ForumManager.php @@ -57,23 +57,29 @@ class ForumManager $params = ['order' => ['name']]; } - $condition_str = "`network` IN (?, ?) AND `uid` = ? AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND "; + $condition = [ + 'contact-type' => Contact::TYPE_COMMUNITY, + 'network' => [Protocol::DFRN, Protocol::ACTIVITYPUB], + 'uid' => $uid, + 'blocked' => false, + 'pending' => false, + 'archive' => false, + ]; - if ($showprivate) { - $condition_str .= '(`forum` OR `prv`)'; - } else { - $condition_str .= '`forum`'; + $condition = DBA::mergeConditions($condition, ["`platform` != ?", 'peertube']); + + if (!$showprivate) { + $condition = DBA::mergeConditions($condition, ['manually-approve' => false]); } if (!$showhidden) { - $condition_str .= ' AND NOT `hidden`'; + $condition = DBA::mergeConditions($condition, ['hidden' => false]); } $forumlist = []; $fields = ['id', 'url', 'name', 'micro', 'thumb', 'avatar', 'network', 'uid']; - $condition = [$condition_str, Protocol::DFRN, Protocol::ACTIVITYPUB, $uid]; - $contacts = DBA::select('contact', $fields, $condition, $params); + $contacts = DBA::select('account-user-view', $fields, $condition, $params); if (!$contacts) { return($forumlist); } diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index 8963b1df6..fd1e39cc2 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -55,7 +55,7 @@ use Friendica\Database\DBA; if (!defined('DB_UPDATE_VERSION')) { - define('DB_UPDATE_VERSION', 1452); + define('DB_UPDATE_VERSION', 1453); } return [ diff --git a/static/dbview.config.php b/static/dbview.config.php index 014973de2..a5c52c9b1 100644 --- a/static/dbview.config.php +++ b/static/dbview.config.php @@ -873,10 +873,8 @@ "archive" => ["contact", "archive"], "deleted" => ["contact", "deleted"], "blocked" => ["contact", "blocked"], - "dfrn-request" => ["contact", "request"], "dfrn-notify" => ["contact", "notify"], "dfrn-poll" => ["contact", "poll"], - "dfrn-confirm" => ["contact", "confirm"], "diaspora-guid" => ["fcontact", "guid"], "diaspora-batch" => ["fcontact", "batch"], "diaspora-notify" => ["fcontact", "notify"], @@ -893,11 +891,15 @@ "ap-following_count" => ["apcontact", "following_count"], "ap-followers_count" => ["apcontact", "followers_count"], "ap-statuses_count" => ["apcontact", "statuses_count"], + "site_name" => ["gserver", "site_name"], + "platform" => ["gserver", "platform"], + "version" => ["gserver", "version"], ], "query" => "FROM `contact` LEFT JOIN `item-uri` ON `item-uri`.`id` = `contact`.`uri-id` LEFT JOIN `apcontact` ON `apcontact`.`uri-id` = `contact`.`uri-id` LEFT JOIN `fcontact` ON `fcontact`.`uri-id` = contact.`uri-id` + LEFT JOIN `gserver` ON `gserver`.`id` = contact.`gsid` WHERE `contact`.`uid` = 0" ], "account-user-view" => [ @@ -970,10 +972,8 @@ "subhub" => ["ucontact", "subhub"], "hub-verify" => ["ucontact", "hub-verify"], "reason" => ["ucontact", "reason"], - "dfrn-request" => ["contact", "request"], "dfrn-notify" => ["contact", "notify"], "dfrn-poll" => ["contact", "poll"], - "dfrn-confirm" => ["contact", "confirm"], "diaspora-guid" => ["fcontact", "guid"], "diaspora-batch" => ["fcontact", "batch"], "diaspora-notify" => ["fcontact", "notify"], @@ -990,12 +990,16 @@ "ap-following_count" => ["apcontact", "following_count"], "ap-followers_count" => ["apcontact", "followers_count"], "ap-statuses_count" => ["apcontact", "statuses_count"], + "site_name" => ["gserver", "site_name"], + "platform" => ["gserver", "platform"], + "version" => ["gserver", "version"], ], "query" => "FROM `contact` AS `ucontact` INNER JOIN `contact` ON `contact`.`uri-id` = `ucontact`.`uri-id` AND `contact`.`uid` = 0 LEFT JOIN `item-uri` ON `item-uri`.`id` = `ucontact`.`uri-id` LEFT JOIN `apcontact` ON `apcontact`.`uri-id` = `ucontact`.`uri-id` - LEFT JOIN `fcontact` ON `fcontact`.`uri-id` = `ucontact`.`uri-id` AND `fcontact`.`network` = 'dspr'" + LEFT JOIN `fcontact` ON `fcontact`.`uri-id` = `ucontact`.`uri-id` AND `fcontact`.`network` = 'dspr' + LEFT JOIN `gserver` ON `gserver`.`id` = contact.`gsid`" ], "pending-view" => [ "fields" => [ From ff70f74952c4c4e341b0a339b30baa347acfe0ac Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 28 Mar 2022 10:33:06 +0000 Subject: [PATCH 2/2] "accept" constants introduced and used --- src/Model/APContact.php | 5 +- src/Model/Contact.php | 4 - src/Model/GServer.php | 91 +++++++++++++------- src/Network/HTTPClient/Client/HttpClient.php | 14 ++- src/Network/Probe.php | 9 +- src/Util/HTTPSignature.php | 3 +- src/Worker/OnePoll.php | 5 +- 7 files changed, 86 insertions(+), 45 deletions(-) diff --git a/src/Model/APContact.php b/src/Model/APContact.php index 55dd75663..9759876fa 100644 --- a/src/Model/APContact.php +++ b/src/Model/APContact.php @@ -28,6 +28,7 @@ use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\Database\DBStructure; use Friendica\DI; +use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Network\HTTPException; use Friendica\Network\Probe; use Friendica\Protocol\ActivityNamespace; @@ -71,10 +72,10 @@ class APContact $data = ['addr' => $addr]; $template = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr); - $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), 'application/jrd+json'); + $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), HttpClient::ACCEPT_JRD_JSON); if (empty($webfinger['links'])) { $template = 'http://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr); - $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), 'application/jrd+json'); + $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), HttpClient::ACCEPT_JRD_JSON); if (empty($webfinger['links'])) { return []; } diff --git a/src/Model/Contact.php b/src/Model/Contact.php index e1152b408..2c88e798d 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -38,10 +38,6 @@ use Friendica\DI; use Friendica\Network\HTTPException; use Friendica\Network\Probe; use Friendica\Protocol\Activity; -use Friendica\Protocol\ActivityPub; -use Friendica\Protocol\Diaspora; -use Friendica\Protocol\OStatus; -use Friendica\Protocol\Salmon; use Friendica\Util\DateTimeFormat; use Friendica\Util\Images; use Friendica\Util\Network; diff --git a/src/Model/GServer.php b/src/Model/GServer.php index b739c7420..3cb4dc5f1 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -34,6 +34,7 @@ use Friendica\DI; use Friendica\Module\Register; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses; +use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Protocol\Relay; use Friendica\Util\DateTimeFormat; use Friendica\Util\Network; @@ -343,7 +344,7 @@ class GServer // When a nodeinfo is present, we don't need to dig further $xrd_timeout = DI::config()->get('system', 'xrd_timeout'); - $curlResult = DI::httpClient()->get($url . '/.well-known/nodeinfo', [HttpClientOptions::TIMEOUT => $xrd_timeout]); + $curlResult = DI::httpClient()->get($url . '/.well-known/nodeinfo', [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if ($curlResult->isTimeout()) { self::setFailure($url); return false; @@ -351,7 +352,7 @@ class GServer // On a redirect follow the new host but mark the old one as failure if ($curlResult->isSuccess() && !empty($curlResult->getRedirectUrl()) && (parse_url($url, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST))) { - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout]); + $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_HTML]); if (!empty($curlResult->getRedirectUrl()) && parse_url($url, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST)) { Logger::info('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $curlResult->getRedirectUrl()]); self::setFailure($url); @@ -393,7 +394,7 @@ class GServer $basedata = ['detection-method' => self::DETECT_MANUAL]; } - $curlResult = DI::httpClient()->get($baseurl, [HttpClientOptions::TIMEOUT => $xrd_timeout]); + $curlResult = DI::httpClient()->get($baseurl, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_HTML]); if ($curlResult->isSuccess()) { if (!empty($curlResult->getRedirectUrl()) && (parse_url($baseurl, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST))) { Logger::info('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $curlResult->getRedirectUrl()]); @@ -417,7 +418,7 @@ class GServer // When the base path doesn't seem to contain a social network we try the complete path. // Most detectable system have to be installed in the root directory. // We checked the base to avoid false positives. - $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout]); + $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_HTML]); if ($curlResult->isSuccess()) { $urldata = self::analyseRootHeader($curlResult, $serverdata); $urldata = self::analyseRootBody($curlResult, $urldata, $url); @@ -444,7 +445,13 @@ class GServer // 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. // Also this hopefully prevents us from receiving abuse messages. - if (empty($serverdata['network']) && !self::validHostMeta($url)) { + $validHostMeta = self::validHostMeta($url); + + if (empty($serverdata['network']) && !$validHostMeta) { + $serverdata = self::detectFromContacts($url, $serverdata); + } + + if (empty($serverdata['network']) && !$validHostMeta) { self::setFailure($url); return false; } @@ -581,7 +588,7 @@ class GServer { Logger::info('Discover relay data', ['server' => $server_url]); - $curlResult = DI::httpClient()->get($server_url . '/.well-known/x-social-relay'); + $curlResult = DI::httpClient()->get($server_url . '/.well-known/x-social-relay', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if (!$curlResult->isSuccess()) { return; } @@ -676,7 +683,7 @@ class GServer */ private static function fetchStatistics(string $url) { - $curlResult = DI::httpClient()->get($url . '/statistics.json'); + $curlResult = DI::httpClient()->get($url . '/statistics.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if (!$curlResult->isSuccess()) { return []; } @@ -802,8 +809,7 @@ class GServer */ private static function parseNodeinfo1(string $nodeinfo_url) { - $curlResult = DI::httpClient()->get($nodeinfo_url); - + $curlResult = DI::httpClient()->get($nodeinfo_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if (!$curlResult->isSuccess()) { return []; } @@ -896,7 +902,7 @@ class GServer */ private static function parseNodeinfo2(string $nodeinfo_url) { - $curlResult = DI::httpClient()->get($nodeinfo_url); + $curlResult = DI::httpClient()->get($nodeinfo_url, [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if (!$curlResult->isSuccess()) { return []; } @@ -991,7 +997,7 @@ class GServer */ private static function fetchSiteinfo(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/siteinfo.json'); + $curlResult = DI::httpClient()->get($url . '/siteinfo.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if (!$curlResult->isSuccess()) { return $serverdata; } @@ -1076,7 +1082,7 @@ class GServer private static function validHostMeta(string $url) { $xrd_timeout = DI::config()->get('system', 'xrd_timeout'); - $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta', [HttpClientOptions::TIMEOUT => $xrd_timeout]); + $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta', [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_XRD_XML]); if (!$curlResult->isSuccess()) { return false; } @@ -1168,7 +1174,7 @@ class GServer { $serverdata['poco'] = ''; - $curlResult = DI::httpClient()->get($url . '/poco'); + $curlResult = DI::httpClient()->get($url . '/poco', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if (!$curlResult->isSuccess()) { return $serverdata; } @@ -1198,7 +1204,7 @@ class GServer */ public static function checkMastodonDirectory(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/api/v1/directory?limit=1'); + $curlResult = DI::httpClient()->get($url . '/api/v1/directory?limit=1', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if (!$curlResult->isSuccess()) { return $serverdata; } @@ -1225,8 +1231,7 @@ class GServer */ private static function detectPeertube(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/api/v1/config'); - + $curlResult = DI::httpClient()->get($url . '/api/v1/config', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { return $serverdata; } @@ -1273,8 +1278,7 @@ class GServer */ private static function detectNextcloud(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/status.php'); - + $curlResult = DI::httpClient()->get($url . '/status.php', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { return $serverdata; } @@ -1298,8 +1302,7 @@ class GServer } private static function fetchWeeklyUsage(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/api/v1/instance/activity'); - + $curlResult = DI::httpClient()->get($url . '/api/v1/instance/activity', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { return $serverdata; } @@ -1328,6 +1331,34 @@ class GServer return $serverdata; } + + /** + * Detects the server network type from contacts of that server + * + * @param string $url URL of the given server + * @param array $serverdata array with server data + * + * @return array server data + */ + private static function detectFromContacts(string $url, array $serverdata) + { + $gserver = DBA::selectFirst('gserver', ['id'], ['nurl' => Strings::normaliseLink($url)]); + if (empty($gserver)) { + return $serverdata; + } + + $contact = Contact::selectFirst(['id'], ['uid' => 0, 'failed' => false, 'gsid' => $gserver['id']]); + + // Via probing we can be sure that the server is responding + if (Contact::updateFromProbe($contact['id'])) { + $contact = Contact::selectFirst(['network', 'failed'], ['id' => $contact['id']]); + if (!$contact['failed'] && in_array($contact['network'], Protocol::FEDERATED)) { + $serverdata['network'] = $contact['network']; + } + } + + return $serverdata; + } /** * Detects data from a given server url if it was a mastodon alike system @@ -1339,8 +1370,7 @@ class GServer */ private static function detectMastodonAlikes(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/api/v1/instance'); - + $curlResult = DI::httpClient()->get($url . '/api/v1/instance', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { return $serverdata; } @@ -1405,7 +1435,7 @@ class GServer */ private static function detectHubzilla(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/api/statusnet/config.json'); + $curlResult = DI::httpClient()->get($url . '/api/statusnet/config.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { return $serverdata; } @@ -1502,7 +1532,7 @@ class GServer */ private static function detectPumpIO(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta.json'); + $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if (!$curlResult->isSuccess()) { return $serverdata; } @@ -1553,7 +1583,7 @@ class GServer private static function detectGNUSocial(string $url, array $serverdata) { // Test for GNU Social - $curlResult = DI::httpClient()->get($url . '/api/gnusocial/version.json'); + $curlResult = DI::httpClient()->get($url . '/api/gnusocial/version.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') && ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) { $serverdata['platform'] = 'gnusocial'; @@ -1571,7 +1601,7 @@ class GServer } // Test for Statusnet - $curlResult = DI::httpClient()->get($url . '/api/statusnet/version.json'); + $curlResult = DI::httpClient()->get($url . '/api/statusnet/version.json', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') && ($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) { @@ -1607,9 +1637,11 @@ class GServer */ private static function detectFriendica(string $url, array $serverdata) { - $curlResult = DI::httpClient()->get($url . '/friendica/json'); + // There is a bug in some versions of Friendica that will return an ActivityStream actor when the content type "application/json" is requested. + // Because of this me must not use ACCEPT_JSON here. + $curlResult = DI::httpClient()->get($url . '/friendica/json', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); if (!$curlResult->isSuccess()) { - $curlResult = DI::httpClient()->get($url . '/friendika/json'); + $curlResult = DI::httpClient()->get($url . '/friendika/json', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_DEFAULT]); $friendika = true; $platform = 'Friendika'; } else { @@ -1931,8 +1963,7 @@ class GServer if (!empty($accesstoken)) { $api = 'https://instances.social/api/1.0/instances/list?count=0'; - $curlResult = DI::httpClient()->get($api, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $accesstoken]]]); - + $curlResult = DI::httpClient()->get($api, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $accesstoken]], HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_JSON]); if ($curlResult->isSuccess()) { $servers = json_decode($curlResult->getBody(), true); diff --git a/src/Network/HTTPClient/Client/HttpClient.php b/src/Network/HTTPClient/Client/HttpClient.php index ae34defc4..1f6839762 100644 --- a/src/Network/HTTPClient/Client/HttpClient.php +++ b/src/Network/HTTPClient/Client/HttpClient.php @@ -44,7 +44,17 @@ use Psr\Log\LoggerInterface; class HttpClient implements ICanSendHttpRequests { /** @var string Default value for "Accept" header */ - const DEFAULT_ACCEPT = '*/*'; + const ACCEPT_DEFAULT = '*/*'; + const ACCEPT_ATOM_XML = 'application/atom+xml,text/xml;q=0.9,*/*;q=0.8'; + const ACCEPT_FEED_XML = 'application/atom+xml,application/rss+xml;q=0.9,application/rdf+xml;q=0.8,text/xml;q=0.7,*/*;q=0.6'; + const ACCEPT_HTML = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'; + const ACCEPT_IMAGE = 'image/png,image/jpeg,image/gif,image/*;q=0.9,*/*;q=0.8'; + const ACCEPT_JRD_JSON = 'application/jrd+json,application/json;q=0.9'; + const ACCEPT_JSON = 'application/json,*/*;q=0.9'; + const ACCEPT_JSON_AS = 'application/activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams"'; + const ACCEPT_RSS_XML = 'application/rss+xml,text/xml;q=0.9,*/*;q=0.8'; + const ACCEPT_VIDEO = 'video/mp4,video/*;q=0.9,*/*;q=0.8'; + const ACCEPT_XRD_XML = 'application/xrd+xml,text/xml;q=0.9,*/*;q=0.8'; /** @var LoggerInterface */ private $logger; @@ -144,7 +154,7 @@ class HttpClient implements ICanSendHttpRequests }; if (empty($conf[HttpClientOptions::HEADERS]['Accept'])) { - $conf[HttpClientOptions::HEADERS]['Accept'] = static::DEFAULT_ACCEPT; + $conf[HttpClientOptions::HEADERS]['Accept'] = static::ACCEPT_DEFAULT; } try { diff --git a/src/Network/Probe.php b/src/Network/Probe.php index a6987d0a6..810885323 100644 --- a/src/Network/Probe.php +++ b/src/Network/Probe.php @@ -34,6 +34,7 @@ use Friendica\Model\Contact; use Friendica\Model\GServer; use Friendica\Model\Profile; use Friendica\Model\User; +use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Protocol\ActivityNamespace; use Friendica\Protocol\ActivityPub; @@ -530,7 +531,7 @@ class Probe $addr = $nick . '@' . $host; } - $webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, 'application/jrd+json', $uri, $addr); + $webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, HttpClient::ACCEPT_JRD_JSON, $uri, $addr); if (empty($webfinger)) { $lrdd = self::hostMeta($host); } @@ -544,7 +545,7 @@ class Probe $addr = $nick . '@' . $host; } - $webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, 'application/jrd+json', $uri, $addr); + $webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, HttpClient::ACCEPT_JRD_JSON, $uri, $addr); if (empty($webfinger)) { $lrdd = self::hostMeta($host); } @@ -562,13 +563,13 @@ class Probe $nick = substr($uri, 0, strpos($uri, '@')); $addr = $uri; - $webfinger = self::getWebfinger('https://' . $host . self::WEBFINGER, 'application/jrd+json', $uri, $addr); + $webfinger = self::getWebfinger('https://' . $host . self::WEBFINGER, HttpClient::ACCEPT_JRD_JSON, $uri, $addr); if (self::$istimeout) { return []; } if (empty($webfinger)) { - $webfinger = self::getWebfinger('http://' . $host . self::WEBFINGER, 'application/jrd+json', $uri, $addr); + $webfinger = self::getWebfinger('http://' . $host . self::WEBFINGER, HttpClient::ACCEPT_JRD_JSON, $uri, $addr); if (self::$istimeout) { return []; } diff --git a/src/Util/HTTPSignature.php b/src/Util/HTTPSignature.php index 68f6cb1dd..faad39dde 100644 --- a/src/Util/HTTPSignature.php +++ b/src/Util/HTTPSignature.php @@ -28,6 +28,7 @@ use Friendica\DI; use Friendica\Model\APContact; use Friendica\Model\Contact; use Friendica\Model\User; +use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Network\HTTPClient\Client\HttpClientOptions; /** @@ -415,7 +416,7 @@ class HTTPSignature * @return \Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses CurlResult * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function fetchRaw($request, $uid = 0, $opts = ['accept_content' => ['application/activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams"']]) + public static function fetchRaw($request, $uid = 0, $opts = ['accept_content' => [HttpClient::ACCEPT_JSON_AS]]) { $header = []; diff --git a/src/Worker/OnePoll.php b/src/Worker/OnePoll.php index fbc00e1d5..40295f0c9 100644 --- a/src/Worker/OnePoll.php +++ b/src/Worker/OnePoll.php @@ -30,6 +30,7 @@ use Friendica\Model\Contact; use Friendica\Model\Item; use Friendica\Model\Post; use Friendica\Model\User; +use Friendica\Network\HTTPClient\Client\HttpClient; use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Protocol\Activity; use Friendica\Protocol\ActivityPub; @@ -154,7 +155,7 @@ class OnePoll } $cookiejar = tempnam(System::getTempPath(), 'cookiejar-onepoll-'); - $curlResult = DI::httpClient()->get($contact['poll'], [HttpClientOptions::COOKIEJAR => $cookiejar]); + $curlResult = DI::httpClient()->get($contact['poll'], [HttpClientOptions::COOKIEJAR => $cookiejar], [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_FEED_XML]); unlink($cookiejar); if ($curlResult->isTimeout()) { @@ -173,7 +174,7 @@ class OnePoll return false; } - Logger::notice('Consume feed of contact', ['id' => $contact['id'], 'url' => $contact['poll']]); + Logger::notice('Consume feed of contact', ['id' => $contact['id'], 'url' => $contact['poll'], 'Content-Type' => $curlResult->getHeader('Content-Type')]); return !empty(Feed::import($xml, $importer, $contact)); }