]> git.mxchange.org Git - friendica.git/blobdiff - src/Network/Probe.php
Merge pull request #11379 from annando/accept
[friendica.git] / src / Network / Probe.php
index e9e8478524606fdce36abb76d9db420dc3b7b34e..810885323df40873630a12b839bfa934c82f0a87 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -34,6 +34,7 @@ use Friendica\Model\Contact;
 use Friendica\Model\GServer;
 use Friendica\Model\Profile;
 use Friendica\Model\User;
+use Friendica\Network\HTTPClient\Client\HttpClient;
 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
 use Friendica\Protocol\ActivityNamespace;
 use Friendica\Protocol\ActivityPub;
@@ -44,6 +45,7 @@ use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
 use Friendica\Util\Strings;
 use Friendica\Util\XML;
+use GuzzleHttp\Psr7\Uri;
 
 /**
  * This class contain functions for probing URL
@@ -58,26 +60,23 @@ class Probe
        /**
         * Remove stuff from an URI that doesn't belong there
         *
-        * @param string $URI
+        * @param string $rawUri
         * @return string Cleaned URI
         */
-       public static function cleanURI(string $URI)
+       public static function cleanURI(string $rawUri): string
        {
                // At first remove leading and trailing junk
-               $URI = trim($URI, "@#?:/ \t\n\r\0\x0B");
+               $rawUri = trim($rawUri, "@#?:/ \t\n\r\0\x0B");
 
-               $parts = parse_url($URI);
-
-               if (empty($parts['scheme'])) {
-                       return $URI;
+               $uri = new Uri($rawUri);
+               if (!$uri->getScheme()) {
+                       return $uri->__toString();
                }
 
                // Remove the URL fragment, since these shouldn't be part of any profile URL
-               unset($parts['fragment']);
-
-               $URI = Network::unparseURL($parts);
+               $uri = $uri->withFragment('');
 
-               return $URI;
+               return $uri->__toString();
        }
 
        /**
@@ -529,15 +528,10 @@ class Probe
                        $path_parts = explode('/', trim($parts['path'] ?? '', '/'));
                        if (!empty($path_parts)) {
                                $nick = ltrim(end($path_parts), '@');
-                               // When the last part of the URI is numeric then it is most likely an ID and not a nick name
-                               if (!is_numeric($nick)) {
-                                       $addr = $nick . '@' . $host;
-                               } else {
-                                       $nick = '';
-                               }
+                               $addr = $nick . '@' . $host;
                        }
 
-                       $webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, 'application/jrd+json', $uri, $addr);
+                       $webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, HttpClient::ACCEPT_JRD_JSON, $uri, $addr);
                        if (empty($webfinger)) {
                                $lrdd = self::hostMeta($host);
                        }
@@ -551,7 +545,7 @@ class Probe
                                                $addr = $nick . '@' . $host;
                                        }
 
-                                       $webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, 'application/jrd+json', $uri, $addr);
+                                       $webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, HttpClient::ACCEPT_JRD_JSON, $uri, $addr);
                                        if (empty($webfinger)) {
                                                $lrdd = self::hostMeta($host);
                                        }
@@ -569,13 +563,13 @@ class Probe
                        $nick = substr($uri, 0, strpos($uri, '@'));
                        $addr = $uri;
 
-                       $webfinger = self::getWebfinger('https://' . $host . self::WEBFINGER, 'application/jrd+json', $uri, $addr);
+                       $webfinger = self::getWebfinger('https://' . $host . self::WEBFINGER, HttpClient::ACCEPT_JRD_JSON, $uri, $addr);
                        if (self::$istimeout) {
                                return [];
                        }
 
                        if (empty($webfinger)) {
-                               $webfinger = self::getWebfinger('http://' . $host . self::WEBFINGER, 'application/jrd+json', $uri, $addr);
+                               $webfinger = self::getWebfinger('http://' . $host . self::WEBFINGER, HttpClient::ACCEPT_JRD_JSON, $uri, $addr);
                                if (self::$istimeout) {
                                        return [];
                                }
@@ -693,22 +687,21 @@ class Probe
                }
 
                $parts = parse_url($uri);
-
-               if (empty($parts['scheme']) || !empty($parts['host']) && strstr($uri, '@')) {
-                       // If the URI starts with "mailto:" then jump directly to the mail detection
-                       if (strpos($uri, 'mailto:') !== false) {
-                               $uri = str_replace('mailto:', '', $uri);
-                               return self::mail($uri, $uid);
-                       }
-
-                       if ($network == Protocol::MAIL) {
-                               return self::mail($uri, $uid);
-                       }
-               } else {
+               if (empty($parts['scheme']) && empty($parts['host']) && !strstr($parts['path'], '@')) {
                        Logger::info('URI was not detectable', ['uri' => $uri]);
                        return [];
                }
 
+               // If the URI starts with "mailto:" then jump directly to the mail detection
+               if (strpos($uri, 'mailto:') !== false) {
+                       $uri = str_replace('mailto:', '', $uri);
+                       return self::mail($uri, $uid);
+               }
+
+               if ($network == Protocol::MAIL) {
+                       return self::mail($uri, $uid);
+               }
+
                Logger::info('Probing start', ['uri' => $uri]);
 
                if (!empty($ap_profile['addr']) && ($ap_profile['addr'] != $uri)) {
@@ -2006,7 +1999,7 @@ class Probe
        /**
         * Fetch the last date that the contact had posted something (publically)
         *
-        * @param string $data  probing result
+        * @param array $data  probing result
         * @return string last activity
         */
        public static function getLastUpdate(array $data)