Merge pull request #12063 from Quix0r/fixes/type-hints-reformatting

Added more type-hints + reformatted code
This commit is contained in:
Hypolite Petovan 2022-10-25 14:54:22 -04:00 committed by GitHub
commit db82bdfc44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 101 additions and 64 deletions

View File

@ -1359,9 +1359,10 @@ class Contact
'writable' => 1, 'writable' => 1,
'blocked' => 0, 'blocked' => 0,
'readonly' => 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. // Before inserting we do check if the entry does exist now.
$contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]); $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);

View File

@ -1325,7 +1325,7 @@ class GServer
private static function validHostMeta(string $url): bool private static function validHostMeta(string $url): bool
{ {
$xrd_timeout = DI::config()->get('system', 'xrd_timeout'); $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()) { if (!$curlResult->isSuccess()) {
return false; return false;
} }

View File

@ -35,6 +35,7 @@ use Friendica\Model\Register;
use Friendica\Module\BaseAdmin; use Friendica\Module\BaseAdmin;
use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientAccept;
use Friendica\Network\HTTPException\ServiceUnavailableException; use Friendica\Network\HTTPException\ServiceUnavailableException;
use Friendica\Network\Probe;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
class Summary extends BaseAdmin class Summary extends BaseAdmin
@ -115,7 +116,7 @@ class Summary extends BaseAdmin
// Check server vitality // Check server vitality
if (!self::checkSelfHostMeta()) { 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.', $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'); $well_known, $well_known, DI::baseUrl()->get() . '/help/Install');
} }
@ -243,14 +244,14 @@ class Summary extends BaseAdmin
'$build' => DI::config()->get('system', 'build'), '$build' => DI::config()->get('system', 'build'),
'$addons' => [DI::l10n()->t('Active addons'), Addon::getEnabledList()], '$addons' => [DI::l10n()->t('Active addons'), Addon::getEnabledList()],
'$serversettings' => $server_settings, '$serversettings' => $server_settings,
'$warningtext' => $warningtext '$warningtext' => $warningtext,
]); ]);
} }
private static function checkSelfHostMeta() private static function checkSelfHostMeta()
{ {
// Fetch the host-meta to check if this really is a vital server // 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 class Probe
{ {
const HOST_META = '/.well-known/host-meta';
const WEBFINGER = '/.well-known/webfinger?resource={uri}'; const WEBFINGER = '/.well-known/webfinger?resource={uri}';
/**
* @var string Base URL
*/
private static $baseurl; private static $baseurl;
private static $istimeout;
/**
* @var boolean Whether a timeout has occured
*/
private static $isTimeout;
/** /**
* Checks if the provided network can be probed * Checks if the provided network can be probed
* *
* @param string $network * @param string $network
*
* @return boolean * @return boolean
*/ */
public static function isProbable(string $network): bool public static function isProbable(string $network): bool
@ -158,6 +167,7 @@ class Probe
* It seems as if it was dropped from the standard. * It seems as if it was dropped from the standard.
* *
* @param string $host The host part of an url * @param string $host The host part of an url
*
* @return array with template and type of the webfinger template for JSON or XML * @return array with template and type of the webfinger template for JSON or XML
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
*/ */
@ -168,10 +178,10 @@ class Probe
// Handles the case when the hostname contains the scheme // Handles the case when the hostname contains the scheme
if (!parse_url($host, PHP_URL_SCHEME)) { if (!parse_url($host, PHP_URL_SCHEME)) {
$ssl_url = 'https://' . $host . '/.well-known/host-meta'; $ssl_url = 'https://' . $host . self::HOST_META;
$url = 'http://' . $host . '/.well-known/host-meta'; $url = 'http://' . $host . self::HOST_META;
} else { } else {
$ssl_url = $host . '/.well-known/host-meta'; $ssl_url = $host . self::HOST_META;
$url = ''; $url = '';
} }
@ -192,7 +202,7 @@ class Probe
} }
} elseif ($curlResult->isTimeout()) { } elseif ($curlResult->isTimeout()) {
Logger::info('Probing timeout', ['url' => $ssl_url]); Logger::info('Probing timeout', ['url' => $ssl_url]);
self::$istimeout = true; self::$isTimeout = true;
return []; return [];
} }
@ -201,10 +211,10 @@ class Probe
$connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0); $connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
Logger::info('Probing timeout', ['url' => $url]); Logger::info('Probing timeout', ['url' => $url]);
self::$istimeout = true; self::$isTimeout = true;
return []; return [];
} elseif ($connection_error && $ssl_connection_error) { } elseif ($connection_error && $ssl_connection_error) {
self::$istimeout = true; self::$isTimeout = true;
return []; return [];
} }
@ -305,7 +315,7 @@ class Probe
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws \ImagickException * @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 // Local profiles aren't probed via network
if (empty($network) && Contact::isLocal($uri)) { if (empty($network) && Contact::isLocal($uri)) {
@ -325,7 +335,7 @@ class Probe
$ap_profile = []; $ap_profile = [];
} }
self::$istimeout = false; self::$isTimeout = false;
if ($network != Protocol::ACTIVITYPUB) { if ($network != Protocol::ACTIVITYPUB) {
$data = self::detect($uri, $network, $uid, $ap_profile); $data = self::detect($uri, $network, $uid, $ap_profile);
@ -454,6 +464,7 @@ class Probe
* *
* @param array $result Result array * @param array $result Result array
* @param array $webfinger Webfinger data * @param array $webfinger Webfinger data
*
* @return array result Altered/unaltered result array * @return array result Altered/unaltered result array
*/ */
private static function getSubscribeLink(array $result, array $webfinger): array private static function getSubscribeLink(array $result, array $webfinger): array
@ -475,6 +486,7 @@ class Probe
* Get webfinger data from a given URI * Get webfinger data from a given URI
* *
* @param string $uri URI * @param string $uri URI
*
* @return array Webfinger data * @return array Webfinger data
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
*/ */
@ -532,13 +544,13 @@ class Probe
$addr = $uri; $addr = $uri;
$webfinger = self::getWebfinger('https://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr); $webfinger = self::getWebfinger('https://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr);
if (self::$istimeout) { if (self::$isTimeout) {
return []; return [];
} }
if (empty($webfinger)) { if (empty($webfinger)) {
$webfinger = self::getWebfinger('http://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr); $webfinger = self::getWebfinger('http://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr);
if (self::$istimeout) { if (self::$isTimeout) {
return []; return [];
} }
} else { } else {
@ -547,7 +559,7 @@ class Probe
if (empty($webfinger)) { if (empty($webfinger)) {
$lrdd = self::hostMeta($host); $lrdd = self::hostMeta($host);
if (self::$istimeout) { if (self::$isTimeout) {
return []; return [];
} }
$baseurl = self::$baseurl; $baseurl = self::$baseurl;
@ -590,6 +602,7 @@ class Probe
* @param string $type * @param string $type
* @param string $uri * @param string $uri
* @param string $addr * @param string $addr
*
* @return array webfinger results * @return array webfinger results
*/ */
private static function getWebfinger(string $template, string $type, string $uri, string $addr): array private static function getWebfinger(string $template, string $type, string $uri, string $addr): array
@ -604,7 +617,7 @@ class Probe
$detected = $addr; $detected = $addr;
$path = str_replace('{uri}', urlencode('acct:' . $addr), $template); $path = str_replace('{uri}', urlencode('acct:' . $addr), $template);
$webfinger = self::webfinger($path, $type); $webfinger = self::webfinger($path, $type);
if (self::$istimeout) { if (self::$isTimeout) {
return []; return [];
} }
} }
@ -614,7 +627,7 @@ class Probe
$detected = $uri; $detected = $uri;
$path = str_replace('{uri}', urlencode($uri), $template); $path = str_replace('{uri}', urlencode($uri), $template);
$webfinger = self::webfinger($path, $type); $webfinger = self::webfinger($path, $type);
if (self::$istimeout) { if (self::$isTimeout) {
return []; return [];
} }
} }
@ -750,6 +763,7 @@ class Probe
* @param array $webfinger Webfinger data * @param array $webfinger Webfinger data
* @param array $data previously probed data * @param array $data previously probed data
* @param string $baseUrl Base URL * @param string $baseUrl Base URL
*
* @return array Zot data * @return array Zot data
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
*/ */
@ -890,6 +904,7 @@ class Probe
* *
* @param string $url Address that should be probed * @param string $url Address that should be probed
* @param string $type type * @param string $type type
*
* @return array webfinger data * @return array webfinger data
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
*/ */
@ -899,7 +914,7 @@ class Probe
$curlResult = DI::httpClient()->get($url, $type, [HttpClientOptions::TIMEOUT => $xrd_timeout]); $curlResult = DI::httpClient()->get($url, $type, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
self::$istimeout = true; self::$isTimeout = true;
return []; return [];
} }
$data = $curlResult->getBody(); $data = $curlResult->getBody();
@ -960,6 +975,7 @@ class Probe
* *
* @param string $noscrape_url Link to the noscrape page * @param string $noscrape_url Link to the noscrape page
* @param array $data The already fetched data * @param array $data The already fetched data
*
* @return array noscrape data * @return array noscrape data
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
*/ */
@ -967,7 +983,7 @@ class Probe
{ {
$curlResult = DI::httpClient()->get($noscrape_url, HttpClientAccept::JSON); $curlResult = DI::httpClient()->get($noscrape_url, HttpClientAccept::JSON);
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
self::$istimeout = true; self::$isTimeout = true;
return $data; return $data;
} }
$content = $curlResult->getBody(); $content = $curlResult->getBody();
@ -1066,7 +1082,7 @@ class Probe
* *
* @return int Number of errors * @return int Number of errors
*/ */
public static function validDfrn($data) public static function validDfrn(array $data): int
{ {
$errors = 0; $errors = 0;
if (!isset($data['key'])) { if (!isset($data['key'])) {
@ -1229,7 +1245,7 @@ class Probe
{ {
$curlResult = DI::httpClient()->get($hcard_url, HttpClientAccept::HTML); $curlResult = DI::httpClient()->get($hcard_url, HttpClientAccept::HTML);
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
self::$istimeout = true; self::$isTimeout = true;
return []; return [];
} }
$content = $curlResult->getBody(); $content = $curlResult->getBody();
@ -1347,6 +1363,7 @@ class Probe
* Check for Diaspora contact * Check for Diaspora contact
* *
* @param array $webfinger Webfinger data * @param array $webfinger Webfinger data
*
* @return array Diaspora data * @return array Diaspora data
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
*/ */
@ -1441,6 +1458,7 @@ class Probe
* *
* @param array $webfinger Webfinger data * @param array $webfinger Webfinger data
* @param bool $short Short detection mode * @param bool $short Short detection mode
*
* @return array|bool OStatus data or "false" on error or "true" on short mode * @return array|bool OStatus data or "false" on error or "true" on short mode
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
*/ */
@ -1487,7 +1505,7 @@ class Probe
} elseif (Strings::normaliseLink($pubkey) == 'http://') { } elseif (Strings::normaliseLink($pubkey) == 'http://') {
$curlResult = DI::httpClient()->get($pubkey, HttpClientAccept::MAGIC_KEY); $curlResult = DI::httpClient()->get($pubkey, HttpClientAccept::MAGIC_KEY);
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
self::$istimeout = true; self::$isTimeout = true;
return $short ? false : []; return $short ? false : [];
} }
Logger::debug('Fetched public key', ['Content-Type' => $curlResult->getHeader('Content-Type'), 'url' => $pubkey]); 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 // Fetch all additional data from the feed
$curlResult = DI::httpClient()->get($data['poll'], HttpClientAccept::FEED_XML); $curlResult = DI::httpClient()->get($data['poll'], HttpClientAccept::FEED_XML);
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
self::$istimeout = true; self::$isTimeout = true;
return []; return [];
} }
$feed = $curlResult->getBody(); $feed = $curlResult->getBody();
@ -1567,6 +1585,7 @@ class Probe
* Fetch data from a pump.io profile page * Fetch data from a pump.io profile page
* *
* @param string $profile_link Link to the profile page * @param string $profile_link Link to the profile page
*
* @return array Profile data * @return array Profile data
*/ */
private static function pumpioProfileData(string $profile_link): array private static function pumpioProfileData(string $profile_link): array
@ -1627,6 +1646,7 @@ class Probe
* *
* @param array $webfinger Webfinger data * @param array $webfinger Webfinger data
* @param string $addr * @param string $addr
*
* @return array pump.io data * @return array pump.io data
*/ */
private static function pumpio(array $webfinger, string $addr): array private static function pumpio(array $webfinger, string $addr): array
@ -1684,6 +1704,7 @@ class Probe
* *
* @param string $url Page link * @param string $url Page link
* @param string $body Page body string * @param string $body Page body string
*
* @return string|false Feed link or false if body was invalid HTML document * @return string|false Feed link or false if body was invalid HTML document
*/ */
public static function getFeedLink(string $url, string $body) 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 $href The potential relative href found in the HTML document
* @param string $base The HTML document URL * @param string $base The HTML document URL
* @param DOMXPath $xpath The HTML document XPath * @param DOMXPath $xpath The HTML document XPath
*
* @return string Absolute URL * @return string Absolute URL
*/ */
private static function ensureAbsoluteLinkFromHTMLDoc(string $href, string $base, DOMXPath $xpath): string private static function ensureAbsoluteLinkFromHTMLDoc(string $href, string $base, DOMXPath $xpath): string
@ -1777,6 +1799,7 @@ class Probe
* *
* @param string $url Profile link * @param string $url Profile link
* @param boolean $probe Do a probe if the page contains a feed link * @param boolean $probe Do a probe if the page contains a feed link
*
* @return array feed data * @return array feed data
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
*/ */
@ -1784,7 +1807,7 @@ class Probe
{ {
$curlResult = DI::httpClient()->get($url, HttpClientAccept::FEED_XML); $curlResult = DI::httpClient()->get($url, HttpClientAccept::FEED_XML);
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
self::$istimeout = true; self::$isTimeout = true;
return []; return [];
} }
$feed = $curlResult->getBody(); $feed = $curlResult->getBody();
@ -1833,6 +1856,7 @@ class Probe
* *
* @param string $uri Profile link * @param string $uri Profile link
* @param integer $uid User ID * @param integer $uid User ID
*
* @return array mail data * @return array mail data
* @throws \Exception * @throws \Exception
*/ */
@ -1873,22 +1897,26 @@ class Probe
$phost = substr($uri, strpos($uri, '@') + 1); $phost = substr($uri, strpos($uri, '@') + 1);
$data = []; $data = [
$data['addr'] = $uri; 'addr' => $uri,
$data['network'] = Protocol::MAIL; 'network' => Protocol::MAIL,
$data['name'] = substr($uri, 0, strpos($uri, '@')); '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['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]); $x = Email::messageMeta($mbox, $msgs[0]);
if (stristr($x[0]->from, $uri)) { if (stristr($x[0]->from, $uri)) {
$adr = imap_rfc822_parse_adrlist($x[0]->from, ''); $adr = imap_rfc822_parse_adrlist($x[0]->from, '');
} elseif (stristr($x[0]->to, $uri)) { } elseif (stristr($x[0]->to, $uri)) {
$adr = imap_rfc822_parse_adrlist($x[0]->to, ''); $adr = imap_rfc822_parse_adrlist($x[0]->to, '');
} }
if (isset($adr)) { if (isset($adr)) {
foreach ($adr as $feadr) { foreach ($adr as $feadr) {
if ((strcasecmp($feadr->mailbox, $data['name']) == 0) if ((strcasecmp($feadr->mailbox, $data['name']) == 0)
@ -1907,9 +1935,11 @@ class Probe
} }
} }
} }
if (!empty($mbox)) { if (!empty($mbox)) {
imap_close($mbox); imap_close($mbox);
} }
return $data; return $data;
} }
@ -1918,6 +1948,7 @@ class Probe
* *
* @param string $avatar Path to the avatar * @param string $avatar Path to the avatar
* @param string $base Another path that is hopefully complete * @param string $base Another path that is hopefully complete
*
* @return string fixed avatar path * @return string fixed avatar path
* @throws \Exception * @throws \Exception
*/ */
@ -1954,6 +1985,7 @@ class Probe
* Fetch the last date that the contact had posted something (publically) * Fetch the last date that the contact had posted something (publically)
* *
* @param array $data probing result * @param array $data probing result
*
* @return string last activity * @return string last activity
*/ */
public static function getLastUpdate(array $data): string public static function getLastUpdate(array $data): string
@ -1985,6 +2017,7 @@ class Probe
* Fetch the last activity date from the "noscrape" endpoint * Fetch the last activity date from the "noscrape" endpoint
* *
* @param array $data Probing result * @param array $data Probing result
*
* @return string last activity or true if update was successful or the server was unreachable * @return string last activity or true if update was successful or the server was unreachable
*/ */
private static function updateFromNoScrape(array $data): string private static function updateFromNoScrape(array $data): string
@ -2017,6 +2050,7 @@ class Probe
* *
* @param string $feed * @param string $feed
* @param array $data Probing result * @param array $data Probing result
*
* @return string last activity * @return string last activity
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
@ -2121,6 +2155,7 @@ class Probe
* Probe data from local profiles without network traffic * Probe data from local profiles without network traffic
* *
* @param string $url * @param string $url
*
* @return array probed data * @return array probed data
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws HTTPException\NotFoundException * @throws HTTPException\NotFoundException