]> git.mxchange.org Git - friendica.git/blobdiff - src/Network/Probe.php
Merge pull request #7090 from nupplaphil/task/mod_like
[friendica.git] / src / Network / Probe.php
index deec3cf2dcb22f3bb0d51b6ee7a08c9492bf1caa..b0d2630103beaed42328d4aa61cee75a08a3d57f 100644 (file)
@@ -10,6 +10,7 @@ namespace Friendica\Network;
  */
 
 use DOMDocument;
+use DomXPath;
 use Friendica\Core\Cache;
 use Friendica\Core\Config;
 use Friendica\Core\Logger;
@@ -18,15 +19,14 @@ use Friendica\Core\System;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Model\Profile;
+use Friendica\Protocol\ActivityPub;
 use Friendica\Protocol\Email;
 use Friendica\Protocol\Feed;
-use Friendica\Protocol\ActivityPub;
 use Friendica\Util\Crypto;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
 use Friendica\Util\Strings;
 use Friendica\Util\XML;
-use DomXPath;
 
 /**
  * @brief This class contain functions for probing URL
@@ -35,6 +35,7 @@ use DomXPath;
 class Probe
 {
        private static $baseurl;
+       private static $istimeout;
 
        /**
         * @brief Rearrange the array so that it always has the same order
@@ -124,6 +125,7 @@ class Probe
                        $curlResult = Network::curl($url, false, $redirects, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']);
                        if ($curlResult->isTimeout()) {
                                Logger::log("Probing timeout for " . $url, Logger::DEBUG);
+                               self::$istimeout = true;
                                return false;
                        }
                        $xml = $curlResult->getBody();
@@ -156,7 +158,7 @@ class Probe
                                continue;
                        }
 
-                       if (($attributes["rel"] == "lrdd") && !empty($attributes["template"])) {
+                       if (!empty($attributes["rel"]) && $attributes["rel"] == "lrdd" && !empty($attributes["template"])) {
                                $type = (empty($attributes["type"]) ? '' : $attributes["type"]);
 
                                $lrdd[$type] = $attributes["template"];
@@ -334,16 +336,24 @@ class Probe
                        $uid = local_user();
                }
 
+               self::$istimeout = false;
+
                if ($network != Protocol::ACTIVITYPUB) {
                        $data = self::detect($uri, $network, $uid);
                } else {
                        $data = null;
                }
 
-               $ap_profile = ActivityPub::probeProfile($uri);
+               // When the previous detection process had got a time out
+               // we could falsely detect a Friendica profile as AP profile.
+               if (!self::$istimeout) {
+                       $ap_profile = ActivityPub::probeProfile($uri);
 
-               if (!empty($ap_profile) && empty($network) && (defaults($data, 'network', '') != Protocol::DFRN)) {
-                       $data = $ap_profile;
+                       if (!empty($ap_profile) && empty($network) && (defaults($data, 'network', '') != Protocol::DFRN)) {
+                               $data = $ap_profile;
+                       }
+               } else {
+                       Logger::notice('Time out detected. AP will not be probed.', ['uri' => $uri]);
                }
 
                if (!isset($data['url'])) {
@@ -500,30 +510,6 @@ class Probe
                return $data;
        }
 
-       /**
-        * @brief Switch the scheme of an url between http and https
-        *
-        * @param string $url URL
-        *
-        * @return string switched URL
-        */
-       private static function switchScheme($url)
-       {
-               $parts = parse_url($url);
-
-               if (!isset($parts['scheme'])) {
-                       return $url;
-               }
-
-               if ($parts['scheme'] == 'http') {
-                       $url = str_replace('http://', 'https://', $url);
-               } elseif ($parts['scheme'] == 'https') {
-                       $url = str_replace('https://', 'http://', $url);
-               }
-
-               return $url;
-       }
-
        /**
         * @brief Checks if a profile url should be OStatus but only provides partial information
         *
@@ -556,7 +542,7 @@ class Probe
                        return $webfinger;
                }
 
-               $url = self::switchScheme($webfinger['subject']);
+               $url = Network::switchScheme($webfinger['subject']);
                $path = str_replace('{uri}', urlencode($url), $lrdd);
                $webfinger2 = self::webfinger($path, $type);
 
@@ -756,6 +742,7 @@ class Probe
 
                $curlResult = Network::curl($url, false, $redirects, ['timeout' => $xrd_timeout, 'accept_content' => $type]);
                if ($curlResult->isTimeout()) {
+                       self::$istimeout = true;
                        return false;
                }
                $data = $curlResult->getBody();
@@ -824,6 +811,7 @@ class Probe
        {
                $curlResult = Network::curl($noscrape_url);
                if ($curlResult->isTimeout()) {
+                       self::$istimeout = true;
                        return false;
                }
                $content = $curlResult->getBody();
@@ -1075,6 +1063,7 @@ class Probe
        {
                $curlResult = Network::curl($hcard_url);
                if ($curlResult->isTimeout()) {
+                       self::$istimeout = true;
                        return false;
                }
                $content = $curlResult->getBody();
@@ -1327,6 +1316,7 @@ class Probe
                                        } elseif (Strings::normaliseLink($pubkey) == 'http://') {
                                                $curlResult = Network::curl($pubkey);
                                                if ($curlResult->isTimeout()) {
+                                                       self::$istimeout = true;
                                                        return false;
                                                }
                                                $pubkey = $curlResult->getBody();
@@ -1359,6 +1349,7 @@ class Probe
                // Fetch all additional data from the feed
                $curlResult = Network::curl($data["poll"]);
                if ($curlResult->isTimeout()) {
+                       self::$istimeout = true;
                        return false;
                }
                $feed = $curlResult->getBody();
@@ -1573,6 +1564,7 @@ class Probe
        {
                $curlResult = Network::curl($url);
                if ($curlResult->isTimeout()) {
+                       self::$istimeout = true;
                        return false;
                }
                $feed = $curlResult->getBody();