]> git.mxchange.org Git - friendica.git/blobdiff - src/Network/Probe.php
Renamed 'http_client' -> 'performance'
[friendica.git] / src / Network / Probe.php
index b1931ae6d36bb6d5cbf8b43bd693cf92ae3024ba..fc5125f2594fed0cc4cda96429d4ad429f1468c4 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2023, the Friendica project
+ * @copyright Copyright (C) 2010-2024, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -27,7 +27,6 @@ use Exception;
 use Friendica\Core\Hook;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
-use Friendica\Core\System;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Contact;
@@ -44,6 +43,7 @@ use Friendica\Protocol\Feed;
 use Friendica\Protocol\Salmon;
 use Friendica\Util\Crypto;
 use Friendica\Util\DateTimeFormat;
+use Friendica\Util\HTTPSignature;
 use Friendica\Util\Network;
 use Friendica\Util\Strings;
 use Friendica\Util\XML;
@@ -219,13 +219,13 @@ class Probe
 
                $xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20);
 
-               Logger::info('Probing', ['host' => $host, 'ssl_url' => $ssl_url, 'url' => $url, 'callstack' => System::callstack(20)]);
+               Logger::info('Probing', ['host' => $host, 'ssl_url' => $ssl_url, 'url' => $url]);
                $xrd = null;
 
                $curlResult = DI::httpClient()->get($ssl_url, HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
                $ssl_connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
                if ($curlResult->isSuccess()) {
-                       $xml = $curlResult->getBody();
+                       $xml = $curlResult->getBodyString();
                        $xrd = XML::parseString($xml, true);
                        if (!empty($url)) {
                                $host_url = 'https://' . $host;
@@ -250,7 +250,7 @@ class Probe
                                return [];
                        }
 
-                       $xml = $curlResult->getBody();
+                       $xml = $curlResult->getBodyString();
                        $xrd = XML::parseString($xml, true);
                        $host_url = 'http://'.$host;
                }
@@ -341,7 +341,6 @@ class Probe
         * @param string  $uri     Address that should be probed
         * @param string  $network Test for this specific network
         * @param integer $uid     User ID for the probe (only used for mails)
-        * @param boolean $cache   Use cached values?
         *
         * @return array uri data
         * @throws HTTPException\InternalServerErrorException
@@ -416,14 +415,19 @@ class Probe
                        }
                }
 
-               if (!empty($data['baseurl']) && empty($data['gsid'])) {
-                       $data['gsid'] = GServer::getID($data['baseurl']);
-               }
-
                if (empty($data['network'])) {
                        $data['network'] = Protocol::PHANTOM;
                }
 
+               $baseurl = parse_url($data['url'], PHP_URL_SCHEME) . '://' . parse_url($data['url'], PHP_URL_HOST);
+               if (empty($data['baseurl']) && ($data['network'] == Protocol::ACTIVITYPUB) && (rtrim($data['url'], '/') == $baseurl)) {
+                       $data['baseurl'] = $baseurl;
+               }
+
+               if (!empty($data['baseurl']) && empty($data['gsid'])) {
+                       $data['gsid'] = GServer::getID($data['baseurl']);
+               }
+
                // Ensure that local connections always are DFRN
                if (($network == '') && ($data['network'] != Protocol::PHANTOM) && (self::ownHost($data['baseurl'] ?? '') || self::ownHost($data['url']))) {
                        $data['network'] = Protocol::DFRN;
@@ -455,7 +459,7 @@ class Probe
                        return false;
                }
 
-               $body = $curlResult->getBody();
+               $body = $curlResult->getBodyString();
                if (empty($body)) {
                        return false;
                }
@@ -861,7 +865,7 @@ class Probe
                if ($curlResult->isTimeout()) {
                        return $data;
                }
-               $content = $curlResult->getBody();
+               $content = $curlResult->getBodyString();
                if (!$content) {
                        return $data;
                }
@@ -967,7 +971,7 @@ class Probe
                        self::$isTimeout = true;
                        return [];
                }
-               $data = $curlResult->getBody();
+               $data = $curlResult->getBodyString();
 
                $webfinger = json_decode($data, true);
                if (!empty($webfinger)) {
@@ -1036,7 +1040,7 @@ class Probe
                        self::$isTimeout = true;
                        return $data;
                }
-               $content = $curlResult->getBody();
+               $content = $curlResult->getBodyString();
                if (!$content) {
                        Logger::info('Empty body', ['url' => $noscrape_url]);
                        return $data;
@@ -1181,7 +1185,6 @@ class Probe
                        $data = self::pollHcard($profile_link, $data, true);
                }
 
-               $prof_data = [];
 
                if (empty($data['addr']) || empty($data['nick'])) {
                        $probe_data = self::uri($profile_link);
@@ -1189,15 +1192,17 @@ class Probe
                        $data['nick'] = ($data['nick'] ?? '') ?: $probe_data['nick'];
                }
 
-               $prof_data['addr']         = $data['addr'];
-               $prof_data['nick']         = $data['nick'];
-               $prof_data['dfrn-request'] = $data['request'] ?? null;
-               $prof_data['dfrn-confirm'] = $data['confirm'] ?? null;
-               $prof_data['dfrn-notify']  = $data['notify']  ?? null;
-               $prof_data['dfrn-poll']    = $data['poll']    ?? null;
-               $prof_data['photo']        = $data['photo']   ?? null;
-               $prof_data['fn']           = $data['name']    ?? null;
-               $prof_data['key']          = $data['pubkey']  ?? null;
+               $prof_data = [
+                       'addr'         => $data['addr'],
+                       'nick'         => $data['nick'],
+                       'dfrn-request' => $data['request'] ?? null,
+                       'dfrn-confirm' => $data['confirm'] ?? null,
+                       'dfrn-notify'  => $data['notify']  ?? null,
+                       'dfrn-poll'    => $data['poll']    ?? null,
+                       'photo'        => $data['photo']   ?? null,
+                       'fn'           => $data['name']    ?? null,
+                       'key'          => $data['pubkey']  ?? null,
+               ];
 
                Logger::debug('Result', ['link' => $profile_link, 'data' => $prof_data]);
 
@@ -1298,7 +1303,7 @@ class Probe
                        self::$isTimeout = true;
                        return [];
                }
-               $content = $curlResult->getBody();
+               $content = $curlResult->getBodyString();
                if (empty($content)) {
                        return [];
                }
@@ -1575,7 +1580,7 @@ class Probe
                                                        return $short ? false : [];
                                                }
                                                Logger::debug('Fetched public key', ['Content-Type' => $curlResult->getHeader('Content-Type'), 'url' => $pubkey]);
-                                               $pubkey = $curlResult->getBody();
+                                               $pubkey = $curlResult->getBodyString();
                                        }
 
                                        try {
@@ -1607,7 +1612,7 @@ class Probe
                        self::$isTimeout = true;
                        return [];
                }
-               $feed = $curlResult->getBody();
+               $feed = $curlResult->getBodyString();
                $feed_data = Feed::import($feed);
                if (!$feed_data) {
                        return [];
@@ -1655,12 +1660,12 @@ class Probe
        private static function pumpioProfileData(string $profile_link, string $baseurl): array
        {
                $curlResult = DI::httpClient()->get($profile_link, HttpClientAccept::HTML);
-               if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
+               if (!$curlResult->isSuccess() || empty($curlResult->getBodyString())) {
                        return [];
                }
 
                $doc = new DOMDocument();
-               if (!@$doc->loadHTML($curlResult->getBody())) {
+               if (!@$doc->loadHTML($curlResult->getBodyString())) {
                        return [];
                }
 
@@ -1856,7 +1861,7 @@ class Probe
                unset($baseParts['query']);
                unset($baseParts['fragment']);
 
-               return Network::unparseURL($baseParts);
+               return Network::unparseURL((array)$baseParts);
        }
 
        /**
@@ -1882,7 +1887,7 @@ class Probe
                        return [];
                }
 
-               $feed = $curlResult->getBody();
+               $feed = $curlResult->getBodyString();
                $feed_data = Feed::import($feed);
 
                if (!$feed_data) {
@@ -2107,8 +2112,8 @@ class Probe
 
                $curlResult = DI::httpClient()->get($gserver['noscrape'] . '/' . $data['nick'], HttpClientAccept::JSON);
 
-               if ($curlResult->isSuccess() && !empty($curlResult->getBody())) {
-                       $noscrape = json_decode($curlResult->getBody(), true);
+               if ($curlResult->isSuccess() && !empty($curlResult->getBodyString())) {
+                       $noscrape = json_decode($curlResult->getBodyString(), true);
                        if (!empty($noscrape) && !empty($noscrape['updated'])) {
                                return DateTimeFormat::utc($noscrape['updated'], DateTimeFormat::MYSQL);
                        }
@@ -2128,7 +2133,7 @@ class Probe
         */
        private static function updateFromOutbox(string $feed, array $data): string
        {
-               $outbox = ActivityPub::fetchContent($feed);
+               $outbox = HTTPSignature::fetch($feed);
                if (empty($outbox)) {
                        return '';
                }
@@ -2182,12 +2187,12 @@ class Probe
        {
                // Search for the newest entry in the feed
                $curlResult = DI::httpClient()->get($data['poll'], HttpClientAccept::ATOM_XML);
-               if (!$curlResult->isSuccess() || !$curlResult->getBody()) {
+               if (!$curlResult->isSuccess() || !$curlResult->getBodyString()) {
                        return '';
                }
 
                $doc = new DOMDocument();
-               @$doc->loadXML($curlResult->getBody());
+               @$doc->loadXML($curlResult->getBodyString());
 
                $xpath = new DOMXPath($doc);
                $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');