<?php
/**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
use Friendica\Util\Strings;
use Friendica\Util\XML;
use Friendica\Network\HTTPException;
+use Friendica\Worker\UpdateGServer;
use GuzzleHttp\Psr7\Uri;
+use Psr\Http\Message\UriInterface;
/**
* This class handles GServer related functions
const DETECT_UNSPECIFIC = [self::DETECT_MANUAL, self::DETECT_HEADER, self::DETECT_BODY, self::DETECT_HOST_META, self::DETECT_CONTACTS, self::DETECT_AP_ACTOR];
// Implementation specific endpoints
+ // @todo Possibly add Lemmy detection via the endpoint /api/v3/site
const DETECT_FRIENDIKA = 10;
const DETECT_FRIENDICA = 11;
const DETECT_STATUSNET = 12;
*/
public static function add(string $url, bool $only_nodeinfo = false)
{
- if (self::getID($url, false)) {
+ if (self::getID($url)) {
return;
}
- Worker::add(Worker::PRIORITY_LOW, 'UpdateGServer', $url, $only_nodeinfo);
+ UpdateGServer::add(Worker::PRIORITY_LOW, $url, $only_nodeinfo);
}
/**
}
/**
- * Checks if the given server is reachable
+ * Checks if the given server array is unreachable for a long time now
*
- * @param string $profile URL of the given profile
- * @param string $server URL of the given server (If empty, taken from profile)
- * @param string $network Network value that is used, when detection failed
- * @param boolean $force Force an update.
+ * @param integer $gsid
+ * @return boolean
+ */
+ private static function isDefunct(array $gserver): bool
+ {
+ return ($gserver['failed'] || in_array($gserver['network'], Protocol::FEDERATED)) &&
+ ($gserver['last_contact'] >= $gserver['created']) &&
+ ($gserver['last_contact'] < $gserver['last_failure']) &&
+ ($gserver['last_contact'] < DateTimeFormat::utc('now - 90 days'));
+ }
+
+ /**
+ * Checks if the given server id is unreachable for a long time now
*
- * @return boolean 'true' if server seems vital
+ * @param integer $gsid
+ * @return boolean
*/
- public static function reachable(string $profile, string $server = '', string $network = '', bool $force = false): bool
+ public static function isDefunctById(int $gsid): bool
{
- if ($server == '') {
- $contact = Contact::getByURL($profile, null, ['baseurl']);
- if (!empty($contact['baseurl'])) {
- $server = $contact['baseurl'];
+ $gserver = DBA::selectFirst('gserver', ['url', 'next_contact', 'last_contact', 'last_failure', 'created', 'failed', 'network'], ['id' => $gsid]);
+ if (empty($gserver)) {
+ return false;
+ } else {
+ if (strtotime($gserver['next_contact']) < time()) {
+ UpdateGServer::add(Worker::PRIORITY_LOW, $gserver['url']);
}
+
+ return self::isDefunct($gserver);
}
+ }
- if ($server == '') {
+ /**
+ * Checks if the given server id is reachable
+ *
+ * @param integer $gsid
+ * @return boolean
+ */
+ public static function isReachableById(int $gsid): bool
+ {
+ $gserver = DBA::selectFirst('gserver', ['url', 'next_contact', 'failed', 'network'], ['id' => $gsid]);
+ if (empty($gserver)) {
return true;
+ } else {
+ if (strtotime($gserver['next_contact']) < time()) {
+ UpdateGServer::add(Worker::PRIORITY_LOW, $gserver['url']);
+ }
+
+ return !$gserver['failed'] && in_array($gserver['network'], Protocol::FEDERATED);
+ }
+ }
+
+ /**
+ * Checks if the given server is reachable
+ *
+ * @param array $contact Contact that should be checked
+ *
+ * @return boolean 'true' if server seems vital
+ */
+ public static function reachable(array $contact): bool
+ {
+ if (!empty($contact['gsid'])) {
+ $gsid = $contact['gsid'];
+ } elseif (!empty($contact['baseurl'])) {
+ $server = $contact['baseurl'];
+ } elseif ($contact['network'] == Protocol::DIASPORA) {
+ $parts = parse_url($contact['url']);
+ unset($parts['path']);
+ $server = (string)Uri::fromParts($parts);
+ } else {
+ return true;
+ }
+
+ if (!empty($gsid)) {
+ $condition = ['id' => $gsid];
+ } else {
+ $condition = ['nurl' => Strings::normaliseLink($server)];
+ }
+
+ $gserver = DBA::selectFirst('gserver', ['url', 'next_contact', 'failed', 'network'], $condition);
+ if (empty($gserver)) {
+ $reachable = true;
+ } else {
+ $reachable = !$gserver['failed'] && in_array($gserver['network'], Protocol::FEDERATED);
+ $server = $gserver['url'];
+ }
+
+ if (!empty($server) && (empty($gserver) || strtotime($gserver['next_contact']) < time())) {
+ UpdateGServer::add(Worker::PRIORITY_LOW, $server);
}
- return self::check($server, $network, $force);
+ return $reachable;
}
/**
return self::detect($server_url, $network, $only_nodeinfo);
}
+ /**
+ * Reset failed server status by gserver id
+ *
+ * @param int $gsid
+ * @param string $network
+ */
+ public static function setReachableById(int $gsid, string $network)
+ {
+ $gserver = DBA::selectFirst('gserver', ['url', 'failed', 'next_contact', 'network'], ['id' => $gsid]);
+ if (DBA::isResult($gserver) && $gserver['failed']) {
+ $fields = ['failed' => false, 'last_contact' => DateTimeFormat::utcNow()];
+ if (!empty($network) && !in_array($gserver['network'], Protocol::FEDERATED)) {
+ $fields['network'] = $network;
+ }
+ self::update($fields, ['id' => $gsid]);
+ Logger::info('Reset failed status for server', ['url' => $gserver['url']]);
+
+ if (strtotime($gserver['next_contact']) < time()) {
+ UpdateGServer::add(Worker::PRIORITY_LOW, $gserver['url']);
+ }
+ }
+ }
+
+ /**
+ * Set failed server status by gserver id
+ *
+ * @param int $gsid
+ */
+ public static function setFailureById(int $gsid)
+ {
+ $gserver = DBA::selectFirst('gserver', ['url', 'failed', 'next_contact'], ['id' => $gsid]);
+ if (DBA::isResult($gserver) && !$gserver['failed']) {
+ self::update(['failed' => true, 'last_failure' => DateTimeFormat::utcNow()], ['id' => $gsid]);
+ Logger::info('Set failed status for server', ['url' => $gserver['url']]);
+
+ if (strtotime($gserver['next_contact']) < time()) {
+ UpdateGServer::add(Worker::PRIORITY_LOW, $gserver['url']);
+ }
+ }
+ }
+
/**
* Set failed server status
*
* @param string $url
*/
- public static function setFailure(string $url)
+ public static function setFailureByUrl(string $url)
{
$gserver = DBA::selectFirst('gserver', [], ['nurl' => Strings::normaliseLink($url)]);
if (DBA::isResult($gserver)) {
'next_contact' => $next_update, 'network' => Protocol::PHANTOM, 'detection-method' => null],
['nurl' => Strings::normaliseLink($url)]);
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),
Logger::info('Set failed status for new server', ['url' => $url]);
}
+ /**
+ * Archive server related contacts and inboxes
+ *
+ * @param integer $gsid
+ * @return void
+ */
+ private static function archiveContacts(int $gsid)
+ {
+ Contact::update(['archive' => true], ['gsid' => $gsid]);
+ DBA::update('inbox-status', ['archive' => true], ['gsid' => $gsid]);
+ }
+
/**
* Remove unwanted content from the given URL
*
- * @param string $url
+ * @param string $dirtyUrl
*
* @return string cleaned URL
+ * @throws Exception
+ * @deprecated since 2023.03 Use cleanUri instead
*/
- public static function cleanURL(string $url): string
+ public static function cleanURL(string $dirtyUrl): string
{
- $url = trim($url, '/');
- $url = str_replace('/index.php', '', $url);
-
- $urlparts = parse_url($url);
- if (empty($urlparts)) {
+ try {
+ return (string)self::cleanUri(new Uri($dirtyUrl));
+ } catch (\Throwable $e) {
+ Logger::warning('Invalid URL', ['dirtyUrl' => $dirtyUrl]);
return '';
}
+ }
- unset($urlparts['user']);
- unset($urlparts['pass']);
- unset($urlparts['query']);
- unset($urlparts['fragment']);
- return (string)Uri::fromParts($urlparts);
+ /**
+ * Remove unwanted content from the given URI
+ *
+ * @param UriInterface $dirtyUri
+ *
+ * @return UriInterface cleaned URI
+ * @throws Exception
+ */
+ public static function cleanUri(UriInterface $dirtyUri): string
+ {
+ return $dirtyUri
+ ->withUserInfo('')
+ ->withQuery('')
+ ->withFragment('')
+ ->withPath(
+ preg_replace(
+ '#(?:^|/)index\.php#',
+ '',
+ rtrim($dirtyUri->getPath(), '/')
+ )
+ );
}
/**
// If the URL missmatches, then we mark the old entry as failure
if (!Strings::compareLink($url, $original_url)) {
- self::setFailure($original_url);
+ self::setFailureByUrl($original_url);
if (!self::getID($url, true)) {
self::detect($url, $network, $only_nodeinfo);
}
$valid_url = Network::isUrlValid($url);
if (!$valid_url) {
- self::setFailure($url);
+ self::setFailureByUrl($url);
return false;
} else {
$valid_url = rtrim($valid_url, '/');
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_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::setFailure($url);
+ self::setFailureByUrl($url);
if (!self::getID($valid_url, true)) {
self::detect($valid_url, $network, $only_nodeinfo);
}
unset($parts['path']);
$valid_url = (string)Uri::fromParts($parts);
- self::setFailure($url);
+ self::setFailureByUrl($url);
if (!self::getID($valid_url, true)) {
self::detect($valid_url, $network, $only_nodeinfo);
}
// When a nodeinfo is present, we don't need to dig further
$curlResult = DI::httpClient()->get($url . '/.well-known/x-nodeinfo2', HttpClientAccept::JSON);
if ($curlResult->isTimeout()) {
- self::setFailure($url);
+ self::setFailureByUrl($url);
return false;
}
if ($only_nodeinfo && empty($serverdata)) {
Logger::info('Invalid nodeinfo in nodeinfo-mode, server is marked as failure', ['url' => $url]);
- self::setFailure($url);
+ self::setFailureByUrl($url);
return false;
} elseif (empty($serverdata)) {
$serverdata = ['detection-method' => self::DETECT_MANUAL, 'network' => Protocol::PHANTOM, 'platform' => '', 'version' => '', 'site_name' => '', 'info' => ''];
}
if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
- self::setFailure($url);
+ self::setFailureByUrl($url);
return false;
}
// Most servers aren't installed in a subdirectory, so we declare this entry as failed
if (($serverdata['network'] == Protocol::PHANTOM) && !empty(parse_url($url, PHP_URL_PATH)) && in_array($serverdata['detection-method'], [self::DETECT_MANUAL])) {
- self::setFailure($url);
+ self::setFailureByUrl($url);
return false;
}
$serverdata = self::detectNetworkViaContacts($url, $serverdata);
}
+ if (($serverdata['network'] == Protocol::PHANTOM) && in_array($serverdata['detection-method'], [self::DETECT_MANUAL, self::DETECT_BODY])) {
+ self::setFailureByUrl($url);
+ return false;
+ }
+
// Detect the directory type
$serverdata['directory-type'] = self::DT_NONE;
*/
private static function discoverFederation()
{
- $last = DI::config()->get('poco', 'last_federation_discovery');
+ $last = DI::keyValue()->get('poco_last_federation_discovery');
if ($last) {
$next = $last + (24 * 60 * 60);
}
}
- DI::config()->set('poco', 'last_federation_discovery', time());
+ DI::keyValue()->set('poco_last_federation_discovery', time());
}
/**