use DOMXPath;
use Friendica\Core\Config;
use Friendica\Core\Protocol;
+use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\Module\Register;
+use Friendica\Network\CurlResult;
use Friendica\Util\Network;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Strings;
*/
class GServer
{
+ // Directory types
+ const DT_NONE = 0;
+ const DT_POCO = 1;
+ const DT_MASTODON = 2;
/**
* Checks if the given server is reachable
*
public static function reachable(string $profile, string $server = '', string $network = '', bool $force = false)
{
if ($server == '') {
- $server = Contact::getBasepath($profile);
+ $server = GContact::getBasepath($profile);
}
if ($server == '') {
return self::check($server, $network, $force);
}
+ /**
+ * Decides if a server needs to be updated, based upon several date fields
+ *
+ * @param date $created Creation date of that server entry
+ * @param date $updated When had the server entry be updated
+ * @param date $last_failure Last failure when contacting that server
+ * @param date $last_contact Last time the server had been contacted
+ *
+ * @return boolean Does the server record needs an update?
+ */
+ public static function updateNeeded($created, $updated, $last_failure, $last_contact)
+ {
+ $now = strtotime(DateTimeFormat::utcNow());
+
+ if ($updated > $last_contact) {
+ $contact_time = strtotime($updated);
+ } else {
+ $contact_time = strtotime($last_contact);
+ }
+
+ $failure_time = strtotime($last_failure);
+ $created_time = strtotime($created);
+
+ // If there is no "created" time then use the current time
+ if ($created_time <= 0) {
+ $created_time = $now;
+ }
+
+ // If the last contact was less than 24 hours then don't update
+ if (($now - $contact_time) < (60 * 60 * 24)) {
+ return false;
+ }
+
+ // If the last failure was less than 24 hours then don't update
+ if (($now - $failure_time) < (60 * 60 * 24)) {
+ return false;
+ }
+
+ // If the last contact was less than a week ago and the last failure is older than a week then don't update
+ //if ((($now - $contact_time) < (60 * 60 * 24 * 7)) && ($contact_time > $failure_time))
+ // return false;
+
+ // If the last contact time was more than a week ago and the contact was created more than a week ago, then only try once a week
+ if ((($now - $contact_time) > (60 * 60 * 24 * 7)) && (($now - $created_time) > (60 * 60 * 24 * 7)) && (($now - $failure_time) < (60 * 60 * 24 * 7))) {
+ return false;
+ }
+
+ // If the last contact time was more than a month ago and the contact was created more than a month ago, then only try once a month
+ if ((($now - $contact_time) > (60 * 60 * 24 * 30)) && (($now - $created_time) > (60 * 60 * 24 * 30)) && (($now - $failure_time) < (60 * 60 * 24 * 30))) {
+ return false;
+ }
+
+ return true;
+ }
+
/**
* Checks the state of the given server.
*
$last_failure = DBA::NULL_DATETIME;
}
- if (!$force && !PortableContact::updateNeeded($gserver['created'], '', $last_failure, $last_contact)) {
+ if (!$force && !self::updateNeeded($gserver['created'], '', $last_failure, $last_contact)) {
Logger::info('No update needed', ['server' => $server_url]);
return ($last_contact >= $last_failure);
}
*/
public static function detect(string $url, string $network = '')
{
+ Logger::info('Detect server type', ['server' => $url]);
$serverdata = [];
+ $original_url = $url;
+
+ // Remove URL content that is not supposed to exist for a server url
+ $urlparts = parse_url($url);
+ unset($urlparts['user']);
+ unset($urlparts['pass']);
+ unset($urlparts['query']);
+ unset($urlparts['fragment']);
+ $url = Network::unparseURL($urlparts);
+
+ // If the URL missmatches, then we mark the old entry as failure
+ if ($url != $original_url) {
+ DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($original_url)]);
+ }
+
// When a nodeinfo is present, we don't need to dig further
$xrd_timeout = Config::get('system', 'xrd_timeout');
$curlResult = Network::curl($url . '/.well-known/nodeinfo', false, ['timeout' => $xrd_timeout]);
$serverdata = $nodeinfo;
}
+ // Detect the directory type
+ $serverdata['directory-type'] = self::DT_NONE;
$serverdata = self::checkPoCo($url, $serverdata);
+ $serverdata = self::checkMastodonDirectory($url, $serverdata);
// We can't detect the network type. Possibly it is some system that we don't know yet
if (empty($serverdata['network'])) {
/**
* Detect server type by using the nodeinfo data
*
- * @param string $url address of the server
+ * @param string $url address of the server
+ * @param CurlResult $curlResult
* @return array Server data
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
- private static function fetchNodeinfo(string $url, $curlResult)
+ private static function fetchNodeinfo(string $url, CurlResult $curlResult)
{
$nodeinfo = json_decode($curlResult->getBody(), true);
$protocols[$protocol] = true;
}
- if (!empty($protocols['friendica'])) {
+ if (!empty($protocols['dfrn'])) {
$server['network'] = Protocol::DFRN;
} elseif (!empty($protocols['activitypub'])) {
$server['network'] = Protocol::ACTIVITYPUB;
*/
private static function checkPoCo(string $url, array $serverdata)
{
+ $serverdata['poco'] = '';
+
$curlResult = Network::curl($url. '/poco');
if (!$curlResult->isSuccess()) {
return $serverdata;
if (!empty($data['totalResults'])) {
$registeredUsers = $serverdata['registered-users'] ?? 0;
$serverdata['registered-users'] = max($data['totalResults'], $registeredUsers);
+ $serverdata['directory-type'] = self::DT_POCO;
$serverdata['poco'] = $url . '/poco';
- } else {
- $serverdata['poco'] = '';
+ }
+
+ return $serverdata;
+ }
+
+ /**
+ * Checks if the given server does have a Mastodon style directory endpoint.
+ *
+ * @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)
+ {
+ $curlResult = Network::curl($url . '/api/v1/directory?limit=1');
+ if (!$curlResult->isSuccess()) {
+ return $serverdata;
+ }
+
+ $data = json_decode($curlResult->getBody(), true);
+ if (empty($data)) {
+ return $serverdata;
+ }
+
+ if (count($data) == 1) {
+ $serverdata['directory-type'] = self::DT_MASTODON;
}
return $serverdata;
$attr = [];
if ($node->attributes->length) {
foreach ($node->attributes as $attribute) {
- $attribute->value = @trim($attribute->value);
- if (empty($attribute->value)) {
+ $value = trim($attribute->value);
+ if (empty($value)) {
continue;
}
- $attr[$attribute->name] = $attribute->value;
+ $attr[$attribute->name] = $value;
}
if (empty($attr['name']) || empty($attr['content'])) {
$attr = [];
if ($node->attributes->length) {
foreach ($node->attributes as $attribute) {
- $attribute->value = @trim($attribute->value);
- if (empty($attribute->value)) {
+ $value = trim($attribute->value);
+ if (empty($value)) {
continue;
}
- $attr[$attribute->name] = $attribute->value;
+ $attr[$attribute->name] = $value;
}
if (empty($attr['property']) || empty($attr['content'])) {
}
return $serverdata;
}
+
+ /**
+ * Update the user directory of a given gserver record
+ *
+ * @param array $gserver gserver record
+ */
+ public static function updateDirectory(array $gserver)
+ {
+ /// @todo Add Mastodon API directory
+
+ if (!empty($gserver['poco'])) {
+ PortableContact::discoverSingleServer($gserver['id']);
+ }
+ }
+
+ /**
+ * Update GServer entries
+ */
+ public static function discover()
+ {
+ // Update the server list
+ self::discoverFederation();
+
+ $no_of_queries = 5;
+
+ $requery_days = intval(Config::get('system', 'poco_requery_days'));
+
+ if ($requery_days == 0) {
+ $requery_days = 7;
+ }
+
+ $last_update = date('c', time() - (60 * 60 * 24 * $requery_days));
+
+ $gservers = DBA::p("SELECT `id`, `url`, `nurl`, `network`, `poco`
+ FROM `gserver`
+ WHERE `last_contact` >= `last_failure`
+ AND `poco` != ''
+ AND `last_poco_query` < ?
+ ORDER BY RAND()", $last_update
+ );
+
+ while ($gserver = DBA::fetch($gservers)) {
+ if (!GServer::check($gserver['url'], $gserver['network'])) {
+ // The server is not reachable? Okay, then we will try it later
+ $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
+ DBA::update('gserver', $fields, ['nurl' => $gserver['nurl']]);
+ continue;
+ }
+
+ Logger::info('Update directory', ['server' => $gserver['url'], 'id' => $gserver['id']]);
+ Worker::add(PRIORITY_LOW, 'UpdateServerDirectory', $gserver);
+
+ if (--$no_of_queries == 0) {
+ break;
+ }
+ }
+
+ DBA::close($gservers);
+ }
+
+ /**
+ * Discover federated servers
+ */
+ private static function discoverFederation()
+ {
+ $last = Config::get('poco', 'last_federation_discovery');
+
+ if ($last) {
+ $next = $last + (24 * 60 * 60);
+
+ if ($next > time()) {
+ return;
+ }
+ }
+
+ // Discover federated servers
+ $curlResult = Network::fetchUrl("http://the-federation.info/pods.json");
+
+ if (!empty($curlResult)) {
+ $servers = json_decode($curlResult, true);
+
+ if (!empty($servers['pods'])) {
+ foreach ($servers['pods'] as $server) {
+ Worker::add(PRIORITY_LOW, 'UpdateGServer', 'https://' . $server['host']);
+ }
+ }
+ }
+
+ // Disvover Mastodon servers
+ $accesstoken = Config::get('system', 'instances_social_key');
+
+ if (!empty($accesstoken)) {
+ $api = 'https://instances.social/api/1.0/instances/list?count=0';
+ $header = ['Authorization: Bearer '.$accesstoken];
+ $curlResult = Network::curl($api, false, ['headers' => $header]);
+
+ if ($curlResult->isSuccess()) {
+ $servers = json_decode($curlResult->getBody(), true);
+
+ foreach ($servers['instances'] as $server) {
+ $url = (is_null($server['https_score']) ? 'http' : 'https') . '://' . $server['name'];
+ Worker::add(PRIORITY_LOW, 'UpdateGServer', $url);
+ }
+ }
+ }
+
+ Config::set('poco', 'last_federation_discovery', time());
+ }
}