Replace Logger with DI::logger() in Network classes

This commit is contained in:
Art4 2025-01-13 11:58:29 +00:00
parent c6cef6bbce
commit 0b20a9c923
2 changed files with 39 additions and 40 deletions

View file

@ -7,7 +7,7 @@
namespace Friendica\Network\HTTPClient\Response; namespace Friendica\Network\HTTPClient\Response;
use Friendica\Core\Logger; use Friendica\DI;
use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses; use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Response;
use GuzzleHttp\RedirectMiddleware; use GuzzleHttp\RedirectMiddleware;
@ -65,7 +65,7 @@ class GuzzleResponse extends Response implements ICanHandleHttpResponses, Respon
} }
if (!$this->isSuccess) { if (!$this->isSuccess) {
Logger::debug('debug', ['info' => $this->getHeaders()]); DI::logger()->debug('debug', ['info' => $this->getHeaders()]);
} }
if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) { if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {

View file

@ -12,7 +12,6 @@ use DomXPath;
use Exception; use Exception;
use Friendica\Content\Text\HTML; use Friendica\Content\Text\HTML;
use Friendica\Core\Hook; use Friendica\Core\Hook;
use Friendica\Core\Logger;
use Friendica\Core\Protocol; use Friendica\Core\Protocol;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
@ -116,7 +115,7 @@ class Probe
$data['photo'] = Network::addBasePath($data['photo'], $data['url']); $data['photo'] = Network::addBasePath($data['photo'], $data['url']);
if (!Network::isValidHttpUrl($data['photo'])) { if (!Network::isValidHttpUrl($data['photo'])) {
Logger::warning('Invalid URL for photo', ['url' => $data['url'], 'photo' => $data['photo']]); DI::logger()->warning('Invalid URL for photo', ['url' => $data['url'], 'photo' => $data['photo']]);
unset($data['photo']); unset($data['photo']);
} }
} }
@ -213,13 +212,13 @@ class Probe
$xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20); $xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20);
Logger::info('Probing', ['host' => $host, 'ssl_url' => $ssl_url, 'url' => $url]); DI::logger()->info('Probing', ['host' => $host, 'ssl_url' => $ssl_url, 'url' => $url]);
$xrd = null; $xrd = null;
try { try {
$curlResult = DI::httpClient()->get($ssl_url, HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]); $curlResult = DI::httpClient()->get($ssl_url, HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
} catch (\Throwable $th) { } catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]); DI::logger()->notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return []; return [];
} }
@ -234,7 +233,7 @@ class Probe
$host_url = 'https://' . $host; $host_url = 'https://' . $host;
} }
} elseif ($curlResult->isTimeout()) { } elseif ($curlResult->isTimeout()) {
Logger::info('Probing timeout', ['url' => $ssl_url]); DI::logger()->info('Probing timeout', ['url' => $ssl_url]);
self::$isTimeout = true; self::$isTimeout = true;
return []; return [];
} }
@ -243,12 +242,12 @@ class Probe
try { try {
$curlResult = DI::httpClient()->get($url, HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]); $curlResult = DI::httpClient()->get($url, HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
} catch (\Throwable $th) { } catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]); DI::logger()->notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return []; return [];
} }
$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]); DI::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) {
@ -261,13 +260,13 @@ class Probe
$host_url = 'http://' . $host; $host_url = 'http://' . $host;
} }
if (!is_object($xrd)) { if (!is_object($xrd)) {
Logger::info('No xrd object found', ['host' => $host]); DI::logger()->info('No xrd object found', ['host' => $host]);
return []; return [];
} }
$links = XML::elementToArray($xrd); $links = XML::elementToArray($xrd);
if (!isset($links['xrd']['link'])) { if (!isset($links['xrd']['link'])) {
Logger::info('No xrd data found', ['host' => $host]); DI::logger()->info('No xrd data found', ['host' => $host]);
return []; return [];
} }
@ -290,13 +289,13 @@ class Probe
} }
if (Network::isUrlBlocked($host_url)) { if (Network::isUrlBlocked($host_url)) {
Logger::info('Domain is blocked', ['url' => $host]); DI::logger()->info('Domain is blocked', ['url' => $host]);
return []; return [];
} }
self::$baseurl = $host_url; self::$baseurl = $host_url;
Logger::info('Probing successful', ['host' => $host]); DI::logger()->info('Probing successful', ['host' => $host]);
return $lrdd; return $lrdd;
} }
@ -317,7 +316,7 @@ class Probe
$webfinger = $data['webfinger']; $webfinger = $data['webfinger'];
if (empty($webfinger['links'])) { if (empty($webfinger['links'])) {
Logger::info('No webfinger links found', ['uri' => $uri]); DI::logger()->info('No webfinger links found', ['uri' => $uri]);
return []; return [];
} }
@ -464,7 +463,7 @@ class Probe
try { try {
$curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML, [HttpClientOptions::CONTENT_LENGTH => 1000000, HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]); $curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML, [HttpClientOptions::CONTENT_LENGTH => 1000000, HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
} catch (\Throwable $th) { } catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]); DI::logger()->notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return false; return false;
} }
if (!$curlResult->isSuccess()) { if (!$curlResult->isSuccess()) {
@ -631,7 +630,7 @@ class Probe
$baseurl = self::$baseurl; $baseurl = self::$baseurl;
} }
} else { } else {
Logger::info('URI was not detectable', ['uri' => $uri]); DI::logger()->info('URI was not detectable', ['uri' => $uri]);
return []; return [];
} }
@ -672,7 +671,7 @@ class Probe
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
{ {
if (Network::isUrlBlocked($template)) { if (Network::isUrlBlocked($template)) {
Logger::info('Domain is blocked', ['url' => $template]); DI::logger()->info('Domain is blocked', ['url' => $template]);
return null; return null;
} }
@ -734,7 +733,7 @@ class Probe
$parts = parse_url($uri); $parts = parse_url($uri);
if (empty($parts['scheme']) && empty($parts['host']) && (empty($parts['path']) || strpos($parts['path'], '@') === false)) { if (empty($parts['scheme']) && empty($parts['host']) && (empty($parts['path']) || strpos($parts['path'], '@') === false)) {
Logger::info('URI was not detectable, probe for AT Protocol now', ['uri' => $uri]); DI::logger()->info('URI was not detectable, probe for AT Protocol now', ['uri' => $uri]);
return self::atProtocol($uri); return self::atProtocol($uri);
} }
@ -748,7 +747,7 @@ class Probe
return self::mail($uri, $uid); return self::mail($uri, $uid);
} }
Logger::info('Probing start', ['uri' => $uri]); DI::logger()->info('Probing start', ['uri' => $uri]);
if (!empty($ap_profile['addr']) && ($ap_profile['addr'] != $uri)) { if (!empty($ap_profile['addr']) && ($ap_profile['addr'] != $uri)) {
$data = self::getWebfingerArray($ap_profile['addr']); $data = self::getWebfingerArray($ap_profile['addr']);
@ -821,7 +820,7 @@ class Probe
$result['url'] = $uri; $result['url'] = $uri;
} }
Logger::info('Probing done', ['uri' => $uri, 'network' => $result['network']]); DI::logger()->info('Probing done', ['uri' => $uri, 'network' => $result['network']]);
return $result; return $result;
} }
@ -906,7 +905,7 @@ class Probe
try { try {
$curlResult = DI::httpClient()->get($url, 'application/x-zot+json', [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]); $curlResult = DI::httpClient()->get($url, 'application/x-zot+json', [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
} catch (\Throwable $th) { } catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]); DI::logger()->notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return $data; return $data;
} }
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
@ -1017,7 +1016,7 @@ class Probe
[HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout', 20), HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO] [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout', 20), HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]
); );
} catch (\Throwable $e) { } catch (\Throwable $e) {
Logger::notice($e->getMessage(), ['url' => $url, 'type' => $type, 'class' => get_class($e)]); DI::logger()->notice($e->getMessage(), ['url' => $url, 'type' => $type, 'class' => get_class($e)]);
return null; return null;
} }
@ -1030,7 +1029,7 @@ class Probe
$webfinger = json_decode($data, true); $webfinger = json_decode($data, true);
if (!empty($webfinger)) { if (!empty($webfinger)) {
if (!isset($webfinger['links'])) { if (!isset($webfinger['links'])) {
Logger::info('No json webfinger links', ['url' => $url]); DI::logger()->info('No json webfinger links', ['url' => $url]);
return []; return [];
} }
return $webfinger; return $webfinger;
@ -1039,13 +1038,13 @@ class Probe
// If it is not JSON, maybe it is XML // If it is not JSON, maybe it is XML
$xrd = XML::parseString($data, true); $xrd = XML::parseString($data, true);
if (!is_object($xrd)) { if (!is_object($xrd)) {
Logger::info('No webfinger data retrievable', ['url' => $url]); DI::logger()->info('No webfinger data retrievable', ['url' => $url]);
return []; return [];
} }
$xrd_arr = XML::elementToArray($xrd); $xrd_arr = XML::elementToArray($xrd);
if (!isset($xrd_arr['xrd']['link'])) { if (!isset($xrd_arr['xrd']['link'])) {
Logger::info('No XML webfinger links', ['url' => $url]); DI::logger()->info('No XML webfinger links', ['url' => $url]);
return []; return [];
} }
@ -1092,7 +1091,7 @@ class Probe
try { try {
$curlResult = DI::httpClient()->get($noscrape_url, HttpClientAccept::JSON, [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]); $curlResult = DI::httpClient()->get($noscrape_url, HttpClientAccept::JSON, [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
} catch (\Throwable $th) { } catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]); DI::logger()->notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return $data; return $data;
} }
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
@ -1101,13 +1100,13 @@ class Probe
} }
$content = $curlResult->getBodyString(); $content = $curlResult->getBodyString();
if (!$content) { if (!$content) {
Logger::info('Empty body', ['url' => $noscrape_url]); DI::logger()->info('Empty body', ['url' => $noscrape_url]);
return $data; return $data;
} }
$json = json_decode($content, true); $json = json_decode($content, true);
if (!is_array($json)) { if (!is_array($json)) {
Logger::info('No json data', ['url' => $noscrape_url]); DI::logger()->info('No json data', ['url' => $noscrape_url]);
return $data; return $data;
} }
@ -1257,7 +1256,7 @@ class Probe
try { try {
$curlResult = DI::httpClient()->get($hcard_url, HttpClientAccept::HTML, [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]); $curlResult = DI::httpClient()->get($hcard_url, HttpClientAccept::HTML, [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
} catch (\Throwable $th) { } catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]); DI::logger()->notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return []; return [];
} }
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
@ -1521,7 +1520,7 @@ class Probe
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]); DI::logger()->debug('Fetched public key', ['Content-Type' => $curlResult->getHeader('Content-Type'), 'url' => $pubkey]);
$pubkey = $curlResult->getBodyString(); $pubkey = $curlResult->getBodyString();
} }
@ -1552,7 +1551,7 @@ class Probe
try { try {
$curlResult = DI::httpClient()->get($data['poll'], HttpClientAccept::FEED_XML, [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]); $curlResult = DI::httpClient()->get($data['poll'], HttpClientAccept::FEED_XML, [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
} catch (\Throwable $th) { } catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]); DI::logger()->notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return []; return [];
} }
if ($curlResult->isTimeout()) { if ($curlResult->isTimeout()) {
@ -1866,7 +1865,7 @@ class Probe
} }
$msgs = Email::poll($mbox, $uri); $msgs = Email::poll($mbox, $uri);
Logger::info('Messages found', ['uri' => $uri, 'count' => count($msgs)]); DI::logger()->info('Messages found', ['uri' => $uri, 'count' => count($msgs)]);
if (!count($msgs)) { if (!count($msgs)) {
return []; return [];
@ -1953,7 +1952,7 @@ class Probe
$fixed = $scheme . $host . $port . $path . $query . $fragment; $fixed = $scheme . $host . $port . $path . $query . $fragment;
Logger::debug('Avatar fixed', ['base' => $base, 'avatar' => $avatar, 'fixed' => $fixed]); DI::logger()->debug('Avatar fixed', ['base' => $base, 'avatar' => $avatar, 'fixed' => $fixed]);
return $fixed; return $fixed;
} }
@ -2015,7 +2014,7 @@ class Probe
try { try {
$curlResult = DI::httpClient()->get($gserver['noscrape'] . '/' . $data['nick'], HttpClientAccept::JSON, [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]); $curlResult = DI::httpClient()->get($gserver['noscrape'] . '/' . $data['nick'], HttpClientAccept::JSON, [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
} catch (\Throwable $th) { } catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]); DI::logger()->notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return ''; return '';
} }
@ -2055,7 +2054,7 @@ class Probe
if (is_string($outbox['first']) && ($outbox['first'] != $feed)) { if (is_string($outbox['first']) && ($outbox['first'] != $feed)) {
return self::updateFromOutbox($outbox['first'], $data); return self::updateFromOutbox($outbox['first'], $data);
} else { } else {
Logger::warning('Unexpected data', ['outbox' => $outbox]); DI::logger()->warning('Unexpected data', ['outbox' => $outbox]);
} }
return ''; return '';
} else { } else {
@ -2096,7 +2095,7 @@ class Probe
try { try {
$curlResult = DI::httpClient()->get($data['poll'], HttpClientAccept::ATOM_XML, [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]); $curlResult = DI::httpClient()->get($data['poll'], HttpClientAccept::ATOM_XML, [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
} catch (\Throwable $th) { } catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]); DI::logger()->notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return ''; return '';
} }
if (!$curlResult->isSuccess() || !$curlResult->getBodyString()) { if (!$curlResult->isSuccess() || !$curlResult->getBodyString()) {
@ -2120,7 +2119,7 @@ class Probe
$updated = !empty($updated_item->nodeValue) ? DateTimeFormat::utc($updated_item->nodeValue) : null; $updated = !empty($updated_item->nodeValue) ? DateTimeFormat::utc($updated_item->nodeValue) : null;
if (empty($published) || empty($updated)) { if (empty($published) || empty($updated)) {
Logger::notice('Invalid entry for XPath.', ['entry' => $entry, 'url' => $data['url']]); DI::logger()->notice('Invalid entry for XPath.', ['entry' => $entry, 'url' => $data['url']]);
continue; continue;
} }