mirror of
https://github.com/friendica/friendica
synced 2025-04-25 21:50:12 +00:00
Merge remote-tracking branch 'upstream/develop' into server-detection
This commit is contained in:
commit
8b7cb5d9ef
328 changed files with 13242 additions and 10211 deletions
|
@ -30,7 +30,6 @@ use Friendica\Core\System;
|
|||
use Friendica\Core\Worker;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\Database\DBStructure;
|
||||
use Friendica\DI;
|
||||
use Friendica\Module\Register;
|
||||
use Friendica\Network\HTTPClient\Client\HttpClientAccept;
|
||||
|
@ -110,7 +109,7 @@ class GServer
|
|||
*
|
||||
* @param string $url
|
||||
* @param boolean $no_check Don't check if the server hadn't been found
|
||||
* @return int gserver id
|
||||
* @return int|null gserver id or NULL on empty URL or failed check
|
||||
*/
|
||||
public static function getID(string $url, bool $no_check = false)
|
||||
{
|
||||
|
@ -170,7 +169,7 @@ class GServer
|
|||
*
|
||||
* @return boolean 'true' if server seems vital
|
||||
*/
|
||||
public static function reachable(string $profile, string $server = '', string $network = '', bool $force = false)
|
||||
public static function reachable(string $profile, string $server = '', string $network = '', bool $force = false): bool
|
||||
{
|
||||
if ($server == '') {
|
||||
$contact = Contact::getByURL($profile, null, ['baseurl']);
|
||||
|
@ -246,7 +245,7 @@ class GServer
|
|||
*
|
||||
* @return boolean 'true' if server seems vital
|
||||
*/
|
||||
public static function check(string $server_url, string $network = '', bool $force = false, bool $only_nodeinfo = false)
|
||||
public static function check(string $server_url, string $network = '', bool $force = false, bool $only_nodeinfo = false): bool
|
||||
{
|
||||
$server_url = self::cleanURL($server_url);
|
||||
if ($server_url == '') {
|
||||
|
@ -258,7 +257,7 @@ class GServer
|
|||
if ($gserver['created'] <= DBA::NULL_DATETIME) {
|
||||
$fields = ['created' => DateTimeFormat::utcNow()];
|
||||
$condition = ['nurl' => Strings::normaliseLink($server_url)];
|
||||
DBA::update('gserver', $fields, $condition);
|
||||
self::update($fields, $condition);
|
||||
}
|
||||
|
||||
if (!$force && (strtotime($gserver['next_contact']) > time())) {
|
||||
|
@ -283,7 +282,7 @@ class GServer
|
|||
$gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($url)]);
|
||||
if (DBA::isResult($gserver)) {
|
||||
$next_update = self::getNextUpdateDate(false, $gserver['created'], $gserver['last_contact']);
|
||||
DBA::update('gserver', ['url' => $url, 'failed' => true, 'last_failure' => DateTimeFormat::utcNow(),
|
||||
self::update(['url' => $url, 'failed' => true, 'last_failure' => DateTimeFormat::utcNow(),
|
||||
'next_contact' => $next_update, 'network' => Protocol::PHANTOM, 'detection-method' => null],
|
||||
['nurl' => Strings::normaliseLink($url)]);
|
||||
Logger::info('Set failed status for existing server', ['url' => $url]);
|
||||
|
@ -301,7 +300,7 @@ class GServer
|
|||
* @param string $url
|
||||
* @return string cleaned URL
|
||||
*/
|
||||
public static function cleanURL(string $url)
|
||||
public static function cleanURL(string $url): string
|
||||
{
|
||||
$url = trim($url, '/');
|
||||
$url = str_replace('/index.php', '', $url);
|
||||
|
@ -324,7 +323,7 @@ class GServer
|
|||
*
|
||||
* @return boolean 'true' if server could be detected
|
||||
*/
|
||||
public static function detect(string $url, string $network = '', bool $only_nodeinfo = false)
|
||||
public static function detect(string $url, string $network = '', bool $only_nodeinfo = false): bool
|
||||
{
|
||||
Logger::info('Detect server type', ['server' => $url]);
|
||||
|
||||
|
@ -559,16 +558,13 @@ class GServer
|
|||
$serverdata['last_contact'] = DateTimeFormat::utcNow();
|
||||
$serverdata['failed'] = false;
|
||||
|
||||
// Limit the length on incoming fields
|
||||
$serverdata = DBStructure::getFieldsForTable('gserver', $serverdata);
|
||||
|
||||
$gserver = DBA::selectFirst('gserver', ['network'], ['nurl' => Strings::normaliseLink($url)]);
|
||||
if (!DBA::isResult($gserver)) {
|
||||
$serverdata['created'] = DateTimeFormat::utcNow();
|
||||
$ret = DBA::insert('gserver', $serverdata);
|
||||
$id = DBA::lastInsertId();
|
||||
} else {
|
||||
$ret = DBA::update('gserver', $serverdata, ['nurl' => $serverdata['nurl']]);
|
||||
$ret = self::update($serverdata, ['nurl' => $serverdata['nurl']]);
|
||||
$gserver = DBA::selectFirst('gserver', ['id'], ['nurl' => $serverdata['nurl']]);
|
||||
if (DBA::isResult($gserver)) {
|
||||
$id = $gserver['id'];
|
||||
|
@ -582,14 +578,14 @@ class GServer
|
|||
$max_users = max($apcontacts, $contacts);
|
||||
if ($max_users > $serverdata['registered-users']) {
|
||||
Logger::info('Update registered users', ['id' => $id, 'url' => $serverdata['nurl'], 'registered-users' => $max_users]);
|
||||
DBA::update('gserver', ['registered-users' => $max_users], ['id' => $id]);
|
||||
self::update(['registered-users' => $max_users], ['id' => $id]);
|
||||
}
|
||||
|
||||
if (empty($serverdata['active-month-users'])) {
|
||||
$contacts = DBA::count('contact', ["`uid` = ? AND `gsid` = ? AND NOT `failed` AND `last-item` > ?", 0, $id, DateTimeFormat::utc('now - 30 days')]);
|
||||
if ($contacts > 0) {
|
||||
Logger::info('Update monthly users', ['id' => $id, 'url' => $serverdata['nurl'], 'monthly-users' => $contacts]);
|
||||
DBA::update('gserver', ['active-month-users' => $contacts], ['id' => $id]);
|
||||
self::update(['active-month-users' => $contacts], ['id' => $id]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -597,7 +593,7 @@ class GServer
|
|||
$contacts = DBA::count('contact', ["`uid` = ? AND `gsid` = ? AND NOT `failed` AND `last-item` > ?", 0, $id, DateTimeFormat::utc('now - 180 days')]);
|
||||
if ($contacts > 0) {
|
||||
Logger::info('Update halfyear users', ['id' => $id, 'url' => $serverdata['nurl'], 'halfyear-users' => $contacts]);
|
||||
DBA::update('gserver', ['active-halfyear-users' => $contacts], ['id' => $id]);
|
||||
self::update(['active-halfyear-users' => $contacts], ['id' => $id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -618,6 +614,7 @@ class GServer
|
|||
* Fetch relay data from a given server url
|
||||
*
|
||||
* @param string $server_url address of the server
|
||||
* @return void
|
||||
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
||||
*/
|
||||
private static function discoverRelay(string $server_url)
|
||||
|
@ -650,7 +647,7 @@ class GServer
|
|||
|
||||
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']]);
|
||||
self::update($fields, ['id' => $gserver['id']]);
|
||||
}
|
||||
|
||||
DBA::delete('gserver-tag', ['gserver-id' => $gserver['id']]);
|
||||
|
@ -714,7 +711,6 @@ class GServer
|
|||
* 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, array $serverdata)
|
||||
|
@ -809,7 +805,7 @@ class GServer
|
|||
* @return array Server data
|
||||
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
||||
*/
|
||||
private static function fetchNodeinfo(string $url, ICanHandleHttpResponses $httpResult)
|
||||
private static function fetchNodeinfo(string $url, ICanHandleHttpResponses $httpResult): array
|
||||
{
|
||||
if (!$httpResult->isSuccess()) {
|
||||
return [];
|
||||
|
@ -860,7 +856,7 @@ class GServer
|
|||
* @return array Server data
|
||||
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
||||
*/
|
||||
private static function parseNodeinfo1(string $nodeinfo_url)
|
||||
private static function parseNodeinfo1(string $nodeinfo_url): array
|
||||
{
|
||||
$curlResult = DI::httpClient()->get($nodeinfo_url, HttpClientAccept::JSON);
|
||||
if (!$curlResult->isSuccess()) {
|
||||
|
@ -957,7 +953,7 @@ class GServer
|
|||
* @return array Server data
|
||||
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
||||
*/
|
||||
private static function parseNodeinfo2(string $nodeinfo_url)
|
||||
private static function parseNodeinfo2(string $nodeinfo_url): array
|
||||
{
|
||||
$curlResult = DI::httpClient()->get($nodeinfo_url, HttpClientAccept::JSON);
|
||||
if (!$curlResult->isSuccess()) {
|
||||
|
@ -969,8 +965,11 @@ class GServer
|
|||
return [];
|
||||
}
|
||||
|
||||
$server = ['detection-method' => self::DETECT_NODEINFO_2,
|
||||
'register_policy' => Register::CLOSED];
|
||||
$server = [
|
||||
'detection-method' => self::DETECT_NODEINFO_2,
|
||||
'register_policy' => Register::CLOSED,
|
||||
'platform' => 'unknown',
|
||||
];
|
||||
|
||||
if (!empty($nodeinfo['openRegistrations'])) {
|
||||
$server['register_policy'] = Register::OPEN;
|
||||
|
@ -1155,10 +1154,9 @@ class GServer
|
|||
*
|
||||
* @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)
|
||||
private static function fetchSiteinfo(string $url, array $serverdata): array
|
||||
{
|
||||
$curlResult = DI::httpClient()->get($url . '/siteinfo.json', HttpClientAccept::JSON);
|
||||
if (!$curlResult->isSuccess()) {
|
||||
|
@ -1276,10 +1274,9 @@ class GServer
|
|||
* 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)
|
||||
private static function validHostMeta(string $url): bool
|
||||
{
|
||||
$xrd_timeout = DI::config()->get('system', 'xrd_timeout');
|
||||
$curlResult = DI::httpClient()->get($url . '/.well-known/host-meta', HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
|
||||
|
@ -1322,10 +1319,9 @@ class GServer
|
|||
*
|
||||
* @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)
|
||||
private static function detectNetworkViaContacts(string $url, array $serverdata): array
|
||||
{
|
||||
$contacts = [];
|
||||
|
||||
|
@ -1374,10 +1370,9 @@ class GServer
|
|||
*
|
||||
* @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)
|
||||
private static function checkPoCo(string $url, array $serverdata): array
|
||||
{
|
||||
$serverdata['poco'] = '';
|
||||
|
||||
|
@ -1406,10 +1401,9 @@ class GServer
|
|||
*
|
||||
* @param string $url URL of the given server
|
||||
* @param array $serverdata array with server data
|
||||
*
|
||||
* @return array server data
|
||||
*/
|
||||
public static function checkMastodonDirectory(string $url, array $serverdata)
|
||||
public static function checkMastodonDirectory(string $url, array $serverdata): array
|
||||
{
|
||||
$curlResult = DI::httpClient()->get($url . '/api/v1/directory?limit=1', HttpClientAccept::JSON);
|
||||
if (!$curlResult->isSuccess()) {
|
||||
|
@ -1436,7 +1430,7 @@ class GServer
|
|||
*
|
||||
* @return array server data
|
||||
*/
|
||||
private static function detectPeertube(string $url, array $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() == '')) {
|
||||
|
@ -1512,7 +1506,15 @@ class GServer
|
|||
return $serverdata;
|
||||
}
|
||||
|
||||
private static function fetchWeeklyUsage(string $url, array $serverdata) {
|
||||
/**
|
||||
* Fetches weekly usage data
|
||||
*
|
||||
* @param string $url URL of the given server
|
||||
* @param array $serverdata array with server data
|
||||
* @return array server data
|
||||
*/
|
||||
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() == '')) {
|
||||
return $serverdata;
|
||||
|
@ -1548,10 +1550,9 @@ class GServer
|
|||
*
|
||||
* @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)
|
||||
private static function detectMastodonAlikes(string $url, array $serverdata): array
|
||||
{
|
||||
$curlResult = DI::httpClient()->get($url . '/api/v1/instance', HttpClientAccept::JSON);
|
||||
if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
|
||||
|
@ -1620,10 +1621,9 @@ class GServer
|
|||
*
|
||||
* @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)
|
||||
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() == '')) {
|
||||
|
@ -1697,10 +1697,9 @@ class GServer
|
|||
* Converts input value to a boolean value
|
||||
*
|
||||
* @param string|integer $val
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
private static function toBoolean($val)
|
||||
private static function toBoolean($val): bool
|
||||
{
|
||||
if (($val == 'true') || ($val == 1)) {
|
||||
return true;
|
||||
|
@ -1716,10 +1715,9 @@ class GServer
|
|||
*
|
||||
* @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)
|
||||
private static function detectGNUSocial(string $url, array $serverdata): array
|
||||
{
|
||||
// Test for GNU Social
|
||||
$curlResult = DI::httpClient()->get($url . '/api/gnusocial/version.json', HttpClientAccept::JSON);
|
||||
|
@ -1771,10 +1769,9 @@ class GServer
|
|||
*
|
||||
* @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)
|
||||
private static function detectFriendica(string $url, array $serverdata): array
|
||||
{
|
||||
// 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.
|
||||
|
@ -1849,7 +1846,7 @@ class GServer
|
|||
*
|
||||
* @return array server data
|
||||
*/
|
||||
private static function analyseRootBody($curlResult, array $serverdata)
|
||||
private static function analyseRootBody($curlResult, array $serverdata): array
|
||||
{
|
||||
if (empty($curlResult->getBody())) {
|
||||
return $serverdata;
|
||||
|
@ -2008,7 +2005,7 @@ class GServer
|
|||
*
|
||||
* @return array server data
|
||||
*/
|
||||
private static function analyseRootHeader($curlResult, array $serverdata)
|
||||
private static function analyseRootHeader($curlResult, array $serverdata): array
|
||||
{
|
||||
if ($curlResult->getHeader('server') == 'Mastodon') {
|
||||
$serverdata['platform'] = 'mastodon';
|
||||
|
@ -2062,7 +2059,7 @@ class GServer
|
|||
Worker::add(PRIORITY_LOW, 'UpdateServerDirectory', $gserver);
|
||||
|
||||
$fields = ['last_poco_query' => DateTimeFormat::utcNow()];
|
||||
DBA::update('gserver', $fields, ['nurl' => $gserver['nurl']]);
|
||||
self::update($fields, ['nurl' => $gserver['nurl']]);
|
||||
|
||||
if (--$no_of_queries == 0) {
|
||||
break;
|
||||
|
@ -2180,17 +2177,17 @@ class GServer
|
|||
}
|
||||
|
||||
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]);
|
||||
self::update(['protocol' => $protocol], ['id' => $gsid]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the protocol of the given server
|
||||
*
|
||||
* @param int $gsid Server id
|
||||
* @return int
|
||||
* @return ?int One of Post\DeliveryData protocol constants or null if unknown or gserver is missing
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function getProtocol(int $gsid)
|
||||
public static function getProtocol(int $gsid): ?int
|
||||
{
|
||||
if (empty($gsid)) {
|
||||
return null;
|
||||
|
@ -2203,4 +2200,19 @@ class GServer
|
|||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enforces gserver table field maximum sizes to avoid "Data too long" database errors
|
||||
*
|
||||
* @param array $fields
|
||||
* @param array $condition
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function update(array $fields, array $condition): bool
|
||||
{
|
||||
$fields = DI::dbaDefinition()->truncateFieldsForTable('gserver', $fields);
|
||||
|
||||
return DBA::update('gserver', $fields, $condition);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue