- added more missing type-hints (checked them in body of the method)
- reformatted code a little (code-style)
- moved '/.well-known/host-meta' to constant Probe::HOST_META
This commit is contained in:
Roland Häder 2022-10-25 20:22:19 +02:00
parent a7e9457ef2
commit 5306bb3e31
No known key found for this signature in database
GPG Key ID: C82EDE5DDFA0BA77
4 changed files with 101 additions and 64 deletions

View File

@ -1359,9 +1359,10 @@ class Contact
'writable' => 1,
'blocked' => 0,
'readonly' => 0,
'pending' => 0];
'pending' => 0,
];
$condition = ['nurl' => Strings::normaliseLink($data["url"]), 'uid' => $uid, 'deleted' => false];
$condition = ['nurl' => Strings::normaliseLink($data['url']), 'uid' => $uid, 'deleted' => false];
// Before inserting we do check if the entry does exist now.
$contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);

View File

@ -1325,7 +1325,7 @@ class GServer
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]);
$curlResult = DI::httpClient()->get($url . Probe::HOST_META, HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
if (!$curlResult->isSuccess()) {
return false;
}

View File

@ -35,6 +35,7 @@ use Friendica\Model\Register;
use Friendica\Module\BaseAdmin;
use Friendica\Network\HTTPClient\Client\HttpClientAccept;
use Friendica\Network\HTTPException\ServiceUnavailableException;
use Friendica\Network\Probe;
use Friendica\Util\DateTimeFormat;
class Summary extends BaseAdmin
@ -115,7 +116,7 @@ class Summary extends BaseAdmin
// Check server vitality
if (!self::checkSelfHostMeta()) {
$well_known = DI::baseUrl()->get() . '/.well-known/host-meta';
$well_known = DI::baseUrl()->get() . Probe::HOST_META;
$warningtext[] = DI::l10n()->t('<a href="%s">%s</a> is not reachable on your system. This is a severe configuration issue that prevents server to server communication. See <a href="%s">the installation page</a> for help.',
$well_known, $well_known, DI::baseUrl()->get() . '/help/Install');
}
@ -219,10 +220,10 @@ class Summary extends BaseAdmin
$server_settings = [
'label' => DI::l10n()->t('Server Settings'),
'php' => [
'php' => [
'upload_max_filesize' => ini_get('upload_max_filesize'),
'post_max_size' => ini_get('post_max_size'),
'memory_limit' => ini_get('memory_limit')
'post_max_size' => ini_get('post_max_size'),
'memory_limit' => ini_get('memory_limit')
],
'mysql' => [
'max_allowed_packet' => $max_allowed_packet
@ -231,26 +232,26 @@ class Summary extends BaseAdmin
$t = Renderer::getMarkupTemplate('admin/summary.tpl');
return Renderer::replaceMacros($t, [
'$title' => DI::l10n()->t('Administration'),
'$page' => DI::l10n()->t('Summary'),
'$queues' => $queues,
'$users' => [DI::l10n()->t('Registered users'), $users],
'$accounts' => $accounts,
'$pending' => [DI::l10n()->t('Pending registrations'), $pending],
'$version' => [DI::l10n()->t('Version'), App::VERSION],
'$platform' => App::PLATFORM,
'$codename' => App::CODENAME,
'$build' => DI::config()->get('system', 'build'),
'$addons' => [DI::l10n()->t('Active addons'), Addon::getEnabledList()],
'$title' => DI::l10n()->t('Administration'),
'$page' => DI::l10n()->t('Summary'),
'$queues' => $queues,
'$users' => [DI::l10n()->t('Registered users'), $users],
'$accounts' => $accounts,
'$pending' => [DI::l10n()->t('Pending registrations'), $pending],
'$version' => [DI::l10n()->t('Version'), App::VERSION],
'$platform' => App::PLATFORM,
'$codename' => App::CODENAME,
'$build' => DI::config()->get('system', 'build'),
'$addons' => [DI::l10n()->t('Active addons'), Addon::getEnabledList()],
'$serversettings' => $server_settings,
'$warningtext' => $warningtext
'$warningtext' => $warningtext,
]);
}
private static function checkSelfHostMeta()
{
// Fetch the host-meta to check if this really is a vital server
return DI::httpClient()->get(DI::baseUrl()->get() . '/.well-known/host-meta', HttpClientAccept::XRD_XML)->isSuccess();
return DI::httpClient()->get(DI::baseUrl()->get() . Probe::HOST_META, HttpClientAccept::XRD_XML)->isSuccess();
}
}

View File

@ -52,15 +52,24 @@ use GuzzleHttp\Psr7\Uri;
*/
class Probe
{
const HOST_META = '/.well-known/host-meta';
const WEBFINGER = '/.well-known/webfinger?resource={uri}';
/**
* @var string Base URL
*/
private static $baseurl;
private static $istimeout;
/**
* @var boolean Whether a timeout has occured
*/
private static $isTimeout;
/**
* Checks if the provided network can be probed
*
* @param string $network
*
* @return boolean
*/
public static function isProbable(string $network): bool
@ -142,7 +151,7 @@ class Probe
$parts = parse_url($host);
if (!isset($parts['scheme'])) {
$parts = parse_url('http://'.$host);
$parts = parse_url('http://' . $host);
}
if (!isset($parts['host'])) {
@ -158,6 +167,7 @@ class Probe
* It seems as if it was dropped from the standard.
*
* @param string $host The host part of an url
*
* @return array with template and type of the webfinger template for JSON or XML
* @throws HTTPException\InternalServerErrorException
*/
@ -168,10 +178,10 @@ class Probe
// Handles the case when the hostname contains the scheme
if (!parse_url($host, PHP_URL_SCHEME)) {
$ssl_url = 'https://' . $host . '/.well-known/host-meta';
$url = 'http://' . $host . '/.well-known/host-meta';
$ssl_url = 'https://' . $host . self::HOST_META;
$url = 'http://' . $host . self::HOST_META;
} else {
$ssl_url = $host . '/.well-known/host-meta';
$ssl_url = $host . self::HOST_META;
$url = '';
}
@ -192,7 +202,7 @@ class Probe
}
} elseif ($curlResult->isTimeout()) {
Logger::info('Probing timeout', ['url' => $ssl_url]);
self::$istimeout = true;
self::$isTimeout = true;
return [];
}
@ -201,10 +211,10 @@ class Probe
$connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
if ($curlResult->isTimeout()) {
Logger::info('Probing timeout', ['url' => $url]);
self::$istimeout = true;
self::$isTimeout = true;
return [];
} elseif ($connection_error && $ssl_connection_error) {
self::$istimeout = true;
self::$isTimeout = true;
return [];
}
@ -305,7 +315,7 @@ class Probe
* @throws HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public static function uri($uri, $network = '', $uid = -1)
public static function uri(string $uri, string $network = '', int $uid = -1): array
{
// Local profiles aren't probed via network
if (empty($network) && Contact::isLocal($uri)) {
@ -325,7 +335,7 @@ class Probe
$ap_profile = [];
}
self::$istimeout = false;
self::$isTimeout = false;
if ($network != Protocol::ACTIVITYPUB) {
$data = self::detect($uri, $network, $uid, $ap_profile);
@ -454,6 +464,7 @@ class Probe
*
* @param array $result Result array
* @param array $webfinger Webfinger data
*
* @return array result Altered/unaltered result array
*/
private static function getSubscribeLink(array $result, array $webfinger): array
@ -475,6 +486,7 @@ class Probe
* Get webfinger data from a given URI
*
* @param string $uri URI
*
* @return array Webfinger data
* @throws HTTPException\InternalServerErrorException
*/
@ -532,13 +544,13 @@ class Probe
$addr = $uri;
$webfinger = self::getWebfinger('https://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr);
if (self::$istimeout) {
if (self::$isTimeout) {
return [];
}
if (empty($webfinger)) {
$webfinger = self::getWebfinger('http://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr);
if (self::$istimeout) {
if (self::$isTimeout) {
return [];
}
} else {
@ -547,7 +559,7 @@ class Probe
if (empty($webfinger)) {
$lrdd = self::hostMeta($host);
if (self::$istimeout) {
if (self::$isTimeout) {
return [];
}
$baseurl = self::$baseurl;
@ -590,6 +602,7 @@ class Probe
* @param string $type
* @param string $uri
* @param string $addr
*
* @return array webfinger results
*/
private static function getWebfinger(string $template, string $type, string $uri, string $addr): array
@ -604,7 +617,7 @@ class Probe
$detected = $addr;
$path = str_replace('{uri}', urlencode('acct:' . $addr), $template);
$webfinger = self::webfinger($path, $type);
if (self::$istimeout) {
if (self::$isTimeout) {
return [];
}
}
@ -614,7 +627,7 @@ class Probe
$detected = $uri;
$path = str_replace('{uri}', urlencode($uri), $template);
$webfinger = self::webfinger($path, $type);
if (self::$istimeout) {
if (self::$isTimeout) {
return [];
}
}
@ -750,6 +763,7 @@ class Probe
* @param array $webfinger Webfinger data
* @param array $data previously probed data
* @param string $baseUrl Base URL
*
* @return array Zot data
* @throws HTTPException\InternalServerErrorException
*/
@ -890,6 +904,7 @@ class Probe
*
* @param string $url Address that should be probed
* @param string $type type
*
* @return array webfinger data
* @throws HTTPException\InternalServerErrorException
*/
@ -899,7 +914,7 @@ class Probe
$curlResult = DI::httpClient()->get($url, $type, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
if ($curlResult->isTimeout()) {
self::$istimeout = true;
self::$isTimeout = true;
return [];
}
$data = $curlResult->getBody();
@ -960,6 +975,7 @@ class Probe
*
* @param string $noscrape_url Link to the noscrape page
* @param array $data The already fetched data
*
* @return array noscrape data
* @throws HTTPException\InternalServerErrorException
*/
@ -967,7 +983,7 @@ class Probe
{
$curlResult = DI::httpClient()->get($noscrape_url, HttpClientAccept::JSON);
if ($curlResult->isTimeout()) {
self::$istimeout = true;
self::$isTimeout = true;
return $data;
}
$content = $curlResult->getBody();
@ -1066,7 +1082,7 @@ class Probe
*
* @return int Number of errors
*/
public static function validDfrn($data)
public static function validDfrn(array $data): int
{
$errors = 0;
if (!isset($data['key'])) {
@ -1229,7 +1245,7 @@ class Probe
{
$curlResult = DI::httpClient()->get($hcard_url, HttpClientAccept::HTML);
if ($curlResult->isTimeout()) {
self::$istimeout = true;
self::$isTimeout = true;
return [];
}
$content = $curlResult->getBody();
@ -1347,6 +1363,7 @@ class Probe
* Check for Diaspora contact
*
* @param array $webfinger Webfinger data
*
* @return array Diaspora data
* @throws HTTPException\InternalServerErrorException
*/
@ -1441,6 +1458,7 @@ class Probe
*
* @param array $webfinger Webfinger data
* @param bool $short Short detection mode
*
* @return array|bool OStatus data or "false" on error or "true" on short mode
* @throws HTTPException\InternalServerErrorException
*/
@ -1487,7 +1505,7 @@ class Probe
} elseif (Strings::normaliseLink($pubkey) == 'http://') {
$curlResult = DI::httpClient()->get($pubkey, HttpClientAccept::MAGIC_KEY);
if ($curlResult->isTimeout()) {
self::$istimeout = true;
self::$isTimeout = true;
return $short ? false : [];
}
Logger::debug('Fetched public key', ['Content-Type' => $curlResult->getHeader('Content-Type'), 'url' => $pubkey]);
@ -1522,7 +1540,7 @@ class Probe
// Fetch all additional data from the feed
$curlResult = DI::httpClient()->get($data['poll'], HttpClientAccept::FEED_XML);
if ($curlResult->isTimeout()) {
self::$istimeout = true;
self::$isTimeout = true;
return [];
}
$feed = $curlResult->getBody();
@ -1567,6 +1585,7 @@ class Probe
* Fetch data from a pump.io profile page
*
* @param string $profile_link Link to the profile page
*
* @return array Profile data
*/
private static function pumpioProfileData(string $profile_link): array
@ -1627,6 +1646,7 @@ class Probe
*
* @param array $webfinger Webfinger data
* @param string $addr
*
* @return array pump.io data
*/
private static function pumpio(array $webfinger, string $addr): array
@ -1684,6 +1704,7 @@ class Probe
*
* @param string $url Page link
* @param string $body Page body string
*
* @return string|false Feed link or false if body was invalid HTML document
*/
public static function getFeedLink(string $url, string $body)
@ -1716,6 +1737,7 @@ class Probe
* @param string $href The potential relative href found in the HTML document
* @param string $base The HTML document URL
* @param DOMXPath $xpath The HTML document XPath
*
* @return string Absolute URL
*/
private static function ensureAbsoluteLinkFromHTMLDoc(string $href, string $base, DOMXPath $xpath): string
@ -1777,6 +1799,7 @@ class Probe
*
* @param string $url Profile link
* @param boolean $probe Do a probe if the page contains a feed link
*
* @return array feed data
* @throws HTTPException\InternalServerErrorException
*/
@ -1784,7 +1807,7 @@ class Probe
{
$curlResult = DI::httpClient()->get($url, HttpClientAccept::FEED_XML);
if ($curlResult->isTimeout()) {
self::$istimeout = true;
self::$isTimeout = true;
return [];
}
$feed = $curlResult->getBody();
@ -1833,6 +1856,7 @@ class Probe
*
* @param string $uri Profile link
* @param integer $uid User ID
*
* @return array mail data
* @throws \Exception
*/
@ -1873,22 +1897,26 @@ class Probe
$phost = substr($uri, strpos($uri, '@') + 1);
$data = [];
$data['addr'] = $uri;
$data['network'] = Protocol::MAIL;
$data['name'] = substr($uri, 0, strpos($uri, '@'));
$data = [
'addr' => $uri,
'network' => Protocol::MAIL,
'name' => substr($uri, 0, strpos($uri, '@')),
'photo' => Network::lookupAvatarByEmail($uri),
'url' => 'mailto:' . $uri,
'notify' => 'smtp ' . Strings::getRandomHex(),
'poll' => 'email ' . Strings::getRandomHex(),
];
$data['nick'] = $data['name'];
$data['photo'] = Network::lookupAvatarByEmail($uri);
$data['url'] = 'mailto:'.$uri;
$data['notify'] = 'smtp ' . Strings::getRandomHex();
$data['poll'] = 'email ' . Strings::getRandomHex();
$x = Email::messageMeta($mbox, $msgs[0]);
if (stristr($x[0]->from, $uri)) {
$adr = imap_rfc822_parse_adrlist($x[0]->from, '');
} elseif (stristr($x[0]->to, $uri)) {
$adr = imap_rfc822_parse_adrlist($x[0]->to, '');
}
if (isset($adr)) {
foreach ($adr as $feadr) {
if ((strcasecmp($feadr->mailbox, $data['name']) == 0)
@ -1907,9 +1935,11 @@ class Probe
}
}
}
if (!empty($mbox)) {
imap_close($mbox);
}
return $data;
}
@ -1918,6 +1948,7 @@ class Probe
*
* @param string $avatar Path to the avatar
* @param string $base Another path that is hopefully complete
*
* @return string fixed avatar path
* @throws \Exception
*/
@ -1954,6 +1985,7 @@ class Probe
* Fetch the last date that the contact had posted something (publically)
*
* @param array $data probing result
*
* @return string last activity
*/
public static function getLastUpdate(array $data): string
@ -1985,6 +2017,7 @@ class Probe
* Fetch the last activity date from the "noscrape" endpoint
*
* @param array $data Probing result
*
* @return string last activity or true if update was successful or the server was unreachable
*/
private static function updateFromNoScrape(array $data): string
@ -2017,6 +2050,7 @@ class Probe
*
* @param string $feed
* @param array $data Probing result
*
* @return string last activity
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
@ -2121,6 +2155,7 @@ class Probe
* Probe data from local profiles without network traffic
*
* @param string $url
*
* @return array probed data
* @throws HTTPException\InternalServerErrorException
* @throws HTTPException\NotFoundException
@ -2141,20 +2176,20 @@ class Probe
}
$data = [
'name' => $owner['name'], 'nick' => $owner['nick'], 'guid' => $approfile['diaspora:guid'] ?? '',
'url' => $owner['url'], 'addr' => $owner['addr'], 'alias' => $owner['alias'],
'photo' => User::getAvatarUrl($owner),
'header' => $owner['header'] ? Contact::getHeaderUrlForId($owner['id'], $owner['updated']) : '',
'account-type' => $owner['contact-type'], 'community' => ($owner['contact-type'] == User::ACCOUNT_TYPE_COMMUNITY),
'keywords' => $owner['keywords'], 'location' => $owner['location'], 'about' => $owner['about'],
'xmpp' => $owner['xmpp'], 'matrix' => $owner['matrix'],
'hide' => !$owner['net-publish'], 'batch' => '', 'notify' => $owner['notify'],
'poll' => $owner['poll'], 'request' => $owner['request'], 'confirm' => $owner['confirm'],
'subscribe' => $approfile['generator']['url'] . '/follow?url={uri}', 'poco' => $owner['poco'],
'following' => $approfile['following'], 'followers' => $approfile['followers'],
'inbox' => $approfile['inbox'], 'outbox' => $approfile['outbox'],
'sharedinbox' => $approfile['endpoints']['sharedInbox'], 'network' => Protocol::DFRN,
'pubkey' => $owner['upubkey'], 'baseurl' => $approfile['generator']['url'], 'gsid' => $owner['gsid'],
'name' => $owner['name'], 'nick' => $owner['nick'], 'guid' => $approfile['diaspora:guid'] ?? '',
'url' => $owner['url'], 'addr' => $owner['addr'], 'alias' => $owner['alias'],
'photo' => User::getAvatarUrl($owner),
'header' => $owner['header'] ? Contact::getHeaderUrlForId($owner['id'], $owner['updated']) : '',
'account-type' => $owner['contact-type'], 'community' => ($owner['contact-type'] == User::ACCOUNT_TYPE_COMMUNITY),
'keywords' => $owner['keywords'], 'location' => $owner['location'], 'about' => $owner['about'],
'xmpp' => $owner['xmpp'], 'matrix' => $owner['matrix'],
'hide' => !$owner['net-publish'], 'batch' => '', 'notify' => $owner['notify'],
'poll' => $owner['poll'], 'request' => $owner['request'], 'confirm' => $owner['confirm'],
'subscribe' => $approfile['generator']['url'] . '/follow?url={uri}', 'poco' => $owner['poco'],
'following' => $approfile['following'], 'followers' => $approfile['followers'],
'inbox' => $approfile['inbox'], 'outbox' => $approfile['outbox'],
'sharedinbox' => $approfile['endpoints']['sharedInbox'], 'network' => Protocol::DFRN,
'pubkey' => $owner['upubkey'], 'baseurl' => $approfile['generator']['url'], 'gsid' => $owner['gsid'],
'manually-approve' => in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP])
];
} catch (Exception $e) {