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
*/
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);
}
/**
return false;
} else {
if (strtotime($gserver['next_contact']) < time()) {
- Worker::add(Worker::PRIORITY_LOW, 'UpdateGServer', $gserver['url'], false);
+ UpdateGServer::add(Worker::PRIORITY_LOW, $gserver['url']);
}
+
return self::isDefunct($gserver);
}
}
return true;
} else {
if (strtotime($gserver['next_contact']) < time()) {
- Worker::add(Worker::PRIORITY_LOW, 'UpdateGServer', $gserver['url'], false);
+ UpdateGServer::add(Worker::PRIORITY_LOW, $gserver['url']);
}
+
return !$gserver['failed'] && in_array($gserver['network'], Protocol::FEDERATED);
}
}
}
if (!empty($server) && (empty($gserver) || strtotime($gserver['next_contact']) < time())) {
- Worker::add(Worker::PRIORITY_LOW, 'UpdateGServer', $server, false);
+ UpdateGServer::add(Worker::PRIORITY_LOW, $server);
}
return $reachable;
/**
* Reset failed server status by gserver id
*
- * @param int $gsid
+ * @param int $gsid
+ * @param string $network
*/
- public static function setReachableById(int $gsid)
+ public static function setReachableById(int $gsid, string $network)
{
- $gserver = DBA::selectFirst('gserver', ['url', 'failed', 'next_contact'], ['id' => $gsid]);
+ $gserver = DBA::selectFirst('gserver', ['url', 'failed', 'next_contact', 'network'], ['id' => $gsid]);
if (DBA::isResult($gserver) && $gserver['failed']) {
- self::update(['failed' => false, 'last_contact' => DateTimeFormat::utcNow()], ['id' => $gsid]);
+ $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()) {
- Worker::add(Worker::PRIORITY_LOW, 'UpdateGServer', $gserver['url'], false);
+ UpdateGServer::add(Worker::PRIORITY_LOW, $gserver['url']);
}
}
}
Logger::info('Set failed status for server', ['url' => $gserver['url']]);
if (strtotime($gserver['next_contact']) < time()) {
- Worker::add(Worker::PRIORITY_LOW, 'UpdateGServer', $gserver['url'], false);
+ UpdateGServer::add(Worker::PRIORITY_LOW, $gserver['url']);
}
}
}
['nurl' => Strings::normaliseLink($url)]);
Logger::info('Set failed status for existing server', ['url' => $url]);
if (self::isDefunct($gserver)) {
- Contact::update(['archive' => true], ['gsid' => $gserver['id']]);
+ self::archiveContacts($gserver['id']);
}
return;
}
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
*
*
* @return string cleaned URL
* @throws Exception
+ * @deprecated since 2023.03 Use cleanUri instead
*/
public static function cleanURL(string $dirtyUrl): string
{
try {
- $url = str_replace('/index.php', '', trim($dirtyUrl, '/'));
- return (string)(new Uri($url))->withUserInfo('')->withQuery('')->withFragment('');
+ return (string)self::cleanUri(new Uri($dirtyUrl));
} catch (\Throwable $e) {
- Logger::warning('Invalid URL', ['dirtyUrl' => $dirtyUrl, 'url' => $url]);
+ Logger::warning('Invalid URL', ['dirtyUrl' => $dirtyUrl]);
return '';
}
}
+ /**
+ * 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(), '/')
+ )
+ );
+ }
+
/**
* Detect server data (type, protocol, version number, ...)
* The detected data is then updated or inserted in the gserver table.