<?php
/**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
use DOMDocument;
use DOMXPath;
+use Exception;
use Friendica\Core\Logger;
use Friendica\Core\Protocol;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Module\Register;
-use Friendica\Network\CurlResult;
+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');
/**
* 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 [];
/**
* 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
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 analyseRootBody($curlResult, array $serverdata, string $url)
{
+ if (empty($curlResult->getBody())) {
+ return $serverdata;
+ }
+
$doc = new DOMDocument();
@$doc->loadHTML($curlResult->getBody());
$xpath = new DOMXPath($doc);
DI::config()->set('poco', 'last_federation_discovery', time());
}
+
+ /**
+ * Set the protocol for the given server
+ *
+ * @param int $gsid Server id
+ * @param int $protocol Protocol id
+ * @return void
+ * @throws Exception
+ */
+ public static function setProtocol(int $gsid, int $protocol)
+ {
+ if (empty($gsid)) {
+ return;
+ }
+
+ $gserver = DBA::selectFirst('gserver', ['protocol', 'url'], ['id' => $gsid]);
+ if (!DBA::isResult($gserver)) {
+ return;
+ }
+
+ $old = $gserver['protocol'];
+
+ if (!is_null($old)) {
+ /*
+ The priority for the protocols is:
+ 1. ActivityPub
+ 2. DFRN via Diaspora
+ 3. Legacy DFRN
+ 4. Diaspora
+ 5. OStatus
+ */
+
+ // We don't need to change it when nothing is to be changed
+ if ($old == $protocol) {
+ return;
+ }
+
+ // We don't want to mark a server as OStatus when it had been marked with any other protocol before
+ if ($protocol == Post\DeliveryData::OSTATUS) {
+ return;
+ }
+
+ // If the server is marked as ActivityPub then we won't change it to anything different
+ if ($old == Post\DeliveryData::ACTIVITYPUB) {
+ return;
+ }
+
+ // Don't change it to anything lower than DFRN if the new one wasn't ActivityPub
+ if (($old == Post\DeliveryData::DFRN) && ($protocol != Post\DeliveryData::ACTIVITYPUB)) {
+ return;
+ }
+
+ // Don't change it to Diaspora when it is a legacy DFRN server
+ if (($old == Post\DeliveryData::LEGACY_DFRN) && ($protocol == Post\DeliveryData::DIASPORA)) {
+ return;
+ }
+ }
+
+ 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
+ */
+ public static function getProtocol(int $gsid)
+ {
+ if (empty($gsid)) {
+ return null;
+ }
+
+ $gserver = DBA::selectFirst('gserver', ['protocol'], ['id' => $gsid]);
+ if (DBA::isResult($gserver)) {
+ return $gserver['protocol'];
+ }
+
+ return null;
+ }
}