<?php
/**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Module\Register;
-use Friendica\Network\CurlResult;
+use Friendica\Network\HTTPClientOptions;
+use Friendica\Network\IHTTPResult;
use Friendica\Protocol\Relay;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Network;
if (($now - $contact_time) < (60 * 60 * 24)) {
return DateTimeFormat::utc('now +1 day');
}
-
+
// If the last contact was less than a week before then try again in a week
if (($now - $contact_time) < (60 * 60 * 24 * 7)) {
return DateTimeFormat::utc('now +1 week');
// When a nodeinfo is present, we don't need to dig further
$xrd_timeout = DI::config()->get('system', 'xrd_timeout');
- $curlResult = DI::httpRequest()->get($url . '/.well-known/nodeinfo', ['timeout' => $xrd_timeout]);
+ $curlResult = DI::httpClient()->get($url . '/.well-known/nodeinfo', [HTTPClientOptions::TIMEOUT => $xrd_timeout]);
if ($curlResult->isTimeout()) {
self::setFailure($url);
return false;
// On a redirect follow the new host but mark the old one as failure
if ($curlResult->isSuccess() && (parse_url($url, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST))) {
- $curlResult = DI::httpRequest()->get($url, ['timeout' => $xrd_timeout]);
+ $curlResult = DI::httpClient()->get($url, [HTTPClientOptions::TIMEOUT => $xrd_timeout]);
if (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);
$basedata = ['detection-method' => self::DETECT_MANUAL];
}
- $curlResult = DI::httpRequest()->get($baseurl, ['timeout' => $xrd_timeout]);
+ $curlResult = DI::httpClient()->get($baseurl, [HTTPClientOptions::TIMEOUT => $xrd_timeout]);
if ($curlResult->isSuccess()) {
if ((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()]);
// 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::httpRequest()->get($url, ['timeout' => $xrd_timeout]);
+ $curlResult = DI::httpClient()->get($url, [HTTPClientOptions::TIMEOUT => $xrd_timeout]);
if ($curlResult->isSuccess()) {
$urldata = self::analyseRootHeader($curlResult, $serverdata);
$urldata = self::analyseRootBody($curlResult, $urldata, $url);
{
Logger::info('Discover relay data', ['server' => $server_url]);
- $curlResult = DI::httpRequest()->get($server_url . '/.well-known/x-social-relay');
+ $curlResult = DI::httpClient()->get($server_url . '/.well-known/x-social-relay');
if (!$curlResult->isSuccess()) {
return;
}
*/
private static function fetchStatistics(string $url)
{
- $curlResult = DI::httpRequest()->get($url . '/statistics.json');
+ $curlResult = DI::httpClient()->get($url . '/statistics.json');
if (!$curlResult->isSuccess()) {
return [];
}
/**
* Detect server type by using the nodeinfo data
*
- * @param string $url address of the server
- * @param CurlResult $curlResult
+ * @param string $url address of the server
+ * @param IHTTPResult $httpResult
+ *
* @return array Server data
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
- private static function fetchNodeinfo(string $url, CurlResult $curlResult)
+ private static function fetchNodeinfo(string $url, IHTTPResult $httpResult)
{
- if (!$curlResult->isSuccess()) {
+ if (!$httpResult->isSuccess()) {
return [];
}
- $nodeinfo = json_decode($curlResult->getBody(), true);
+ $nodeinfo = json_decode($httpResult->getBody(), true);
if (!is_array($nodeinfo) || empty($nodeinfo['links'])) {
return [];
*/
private static function parseNodeinfo1(string $nodeinfo_url)
{
- $curlResult = DI::httpRequest()->get($nodeinfo_url);
+ $curlResult = DI::httpClient()->get($nodeinfo_url);
if (!$curlResult->isSuccess()) {
return [];
/**
* Parses Nodeinfo 2
*
+ * @see https://git.feneas.org/jaywink/nodeinfo2
* @param string $nodeinfo_url address of the nodeinfo path
* @return array Server data
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
private static function parseNodeinfo2(string $nodeinfo_url)
{
- $curlResult = DI::httpRequest()->get($nodeinfo_url);
+ $curlResult = DI::httpClient()->get($nodeinfo_url);
if (!$curlResult->isSuccess()) {
return [];
}
if (!empty($nodeinfo['protocols'])) {
$protocols = [];
foreach ($nodeinfo['protocols'] as $protocol) {
- $protocols[$protocol] = true;
+ if (is_string($protocol)) {
+ $protocols[$protocol] = true;
+ }
}
if (!empty($protocols['dfrn'])) {
*/
private static function fetchSiteinfo(string $url, array $serverdata)
{
- $curlResult = DI::httpRequest()->get($url . '/siteinfo.json');
+ $curlResult = DI::httpClient()->get($url . '/siteinfo.json');
if (!$curlResult->isSuccess()) {
return $serverdata;
}
private static function validHostMeta(string $url)
{
$xrd_timeout = DI::config()->get('system', 'xrd_timeout');
- $curlResult = DI::httpRequest()->get($url . '/.well-known/host-meta', ['timeout' => $xrd_timeout]);
+ $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta', [HTTPClientOptions::TIMEOUT => $xrd_timeout]);
if (!$curlResult->isSuccess()) {
return false;
}
{
$serverdata['poco'] = '';
- $curlResult = DI::httpRequest()->get($url . '/poco');
+ $curlResult = DI::httpClient()->get($url . '/poco');
if (!$curlResult->isSuccess()) {
return $serverdata;
}
*/
public static function checkMastodonDirectory(string $url, array $serverdata)
{
- $curlResult = DI::httpRequest()->get($url . '/api/v1/directory?limit=1');
+ $curlResult = DI::httpClient()->get($url . '/api/v1/directory?limit=1');
if (!$curlResult->isSuccess()) {
return $serverdata;
}
*/
private static function detectPeertube(string $url, array $serverdata)
{
- $curlResult = DI::httpRequest()->get($url . '/api/v1/config');
+ $curlResult = DI::httpClient()->get($url . '/api/v1/config');
if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
return $serverdata;
*/
private static function detectNextcloud(string $url, array $serverdata)
{
- $curlResult = DI::httpRequest()->get($url . '/status.php');
+ $curlResult = DI::httpClient()->get($url . '/status.php');
if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
return $serverdata;
*/
private static function detectMastodonAlikes(string $url, array $serverdata)
{
- $curlResult = DI::httpRequest()->get($url . '/api/v1/instance');
+ $curlResult = DI::httpClient()->get($url . '/api/v1/instance');
if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
return $serverdata;
*/
private static function detectHubzilla(string $url, array $serverdata)
{
- $curlResult = DI::httpRequest()->get($url . '/api/statusnet/config.json');
+ $curlResult = DI::httpClient()->get($url . '/api/statusnet/config.json');
if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
return $serverdata;
}
private static function detectGNUSocial(string $url, array $serverdata)
{
// Test for GNU Social
- $curlResult = DI::httpRequest()->get($url . '/api/gnusocial/version.json');
+ $curlResult = DI::httpClient()->get($url . '/api/gnusocial/version.json');
if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') &&
($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) {
$serverdata['platform'] = 'gnusocial';
}
// Test for Statusnet
- $curlResult = DI::httpRequest()->get($url . '/api/statusnet/version.json');
+ $curlResult = DI::httpClient()->get($url . '/api/statusnet/version.json');
if ($curlResult->isSuccess() && ($curlResult->getBody() != '{"error":"not implemented"}') &&
($curlResult->getBody() != '') && (strlen($curlResult->getBody()) < 30)) {
*/
private static function detectFriendica(string $url, array $serverdata)
{
- $curlResult = DI::httpRequest()->get($url . '/friendica/json');
+ $curlResult = DI::httpClient()->get($url . '/friendica/json');
if (!$curlResult->isSuccess()) {
- $curlResult = DI::httpRequest()->get($url . '/friendika/json');
+ $curlResult = DI::httpClient()->get($url . '/friendika/json');
$friendika = true;
$platform = 'Friendika';
} else {
*/
private static function analyseRootBody($curlResult, array $serverdata, string $url)
{
+ if (empty($curlResult->getBody())) {
+ return $serverdata;
+ }
+
$doc = new DOMDocument();
@$doc->loadHTML($curlResult->getBody());
$xpath = new DOMXPath($doc);
} elseif ($curlResult->inHeader('x-diaspora-version')) {
$serverdata['platform'] = 'diaspora';
$serverdata['network'] = Protocol::DIASPORA;
- $serverdata['version'] = $curlResult->getHeader('x-diaspora-version');
+ $serverdata['version'] = $curlResult->getHeader('x-diaspora-version')[0] ?? '';
} elseif ($curlResult->inHeader('x-friendica-version')) {
$serverdata['platform'] = 'friendica';
$serverdata['network'] = Protocol::DFRN;
- $serverdata['version'] = $curlResult->getHeader('x-friendica-version');
+ $serverdata['version'] = $curlResult->getHeader('x-friendica-version')[0] ?? '';
} else {
return $serverdata;
}
$protocols = ['activitypub', 'diaspora', 'dfrn', 'ostatus'];
foreach ($protocols as $protocol) {
$query = '{nodes(protocol:"' . $protocol . '"){host}}';
- $curlResult = DI::httpRequest()->fetch('https://the-federation.info/graphql?query=' . urlencode($query));
+ $curlResult = DI::httpClient()->fetch('https://the-federation.info/graphql?query=' . urlencode($query));
if (!empty($curlResult)) {
$data = json_decode($curlResult, true);
if (!empty($data['data']['nodes'])) {
if (!empty($accesstoken)) {
$api = 'https://instances.social/api/1.0/instances/list?count=0';
- $header = ['Authorization: Bearer '.$accesstoken];
- $curlResult = DI::httpRequest()->get($api, ['header' => $header]);
+ $curlResult = DI::httpClient()->get($api, [HTTPClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $accesstoken]]]);
if ($curlResult->isSuccess()) {
$servers = json_decode($curlResult->getBody(), true);
*
* @param int $gsid Server id
* @param int $protocol Protocol id
- * @return void
- * @throws Exception
+ * @return void
+ * @throws Exception
*/
public static function setProtocol(int $gsid, int $protocol)
{
}
}
- Logger::info('Protocol for server', ['protocol' => $protocol, 'old' => $old, 'id' => $gsid, 'url' => $gserver['url']]);
+ Logger::info('Protocol for server', ['protocol' => $protocol, 'old' => $old, 'id' => $gsid, 'url' => $gserver['url'], 'callstack' => System::callstack(20)]);
DBA::update('gserver', ['protocol' => $protocol], ['id' => $gsid]);
}
* Fetch the protocol of the given server
*
* @param int $gsid Server id
- * @return int
- * @throws Exception
+ * @return int
+ * @throws Exception
*/
public static function getProtocol(int $gsid)
{