]> git.mxchange.org Git - friendica.git/blobdiff - src/Network/Probe.php
Merge pull request #8036 from nupplaphil/task/redundant_system_call
[friendica.git] / src / Network / Probe.php
index 514cd6989391ad5a43d565741f01b2770f4d9adf..dac0822a94af7c1dfafec75350fe6b46b9551e14 100644 (file)
@@ -15,8 +15,8 @@ use Friendica\Core\Cache;
 use Friendica\Core\Config;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
-use Friendica\Core\System;
 use Friendica\Database\DBA;
+use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Profile;
 use Friendica\Protocol\ActivityNamespace;
@@ -76,7 +76,7 @@ class Probe
         */
        private static function ownHost($host)
        {
-               $own_host = \get_app()->getHostName();
+               $own_host = DI::baseUrl()->getHostname();
 
                $parts = parse_url($host);
 
@@ -115,19 +115,29 @@ class Probe
                $xrd = null;
 
                $curlResult = Network::curl($ssl_url, false, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']);
+               $ssl_connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
                if ($curlResult->isSuccess()) {
                        $xml = $curlResult->getBody();
                        $xrd = XML::parseString($xml, false);
                        $host_url = 'https://'.$host;
+               } elseif ($curlResult->isTimeout()) {
+                       Logger::info('Probing timeout', ['url' => $ssl_url], Logger::DEBUG);
+                       self::$istimeout = true;
+                       return false;
                }
 
                if (!is_object($xrd)) {
                        $curlResult = Network::curl($url, false, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']);
+                       $connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
                        if ($curlResult->isTimeout()) {
-                               Logger::log("Probing timeout for " . $url, Logger::DEBUG);
+                               Logger::info('Probing timeout', ['url' => $url], Logger::DEBUG);
+                               self::$istimeout = true;
+                               return false;
+                       } elseif ($connection_error && $ssl_connection_error) {
                                self::$istimeout = true;
                                return false;
                        }
+
                        $xml = $curlResult->getBody();
                        $xrd = XML::parseString($xml, false);
                        $host_url = 'http://'.$host;
@@ -359,10 +369,10 @@ class Probe
                        $data['url'] = $uri;
                }
 
-               if (!empty($data['photo'])) {
-                       $data['baseurl'] = Network::getUrlMatch(Strings::normaliseLink($data['baseurl'] ?? ''), Strings::normaliseLink($data['photo']));
-               } else {
-                       $data['photo'] = System::baseUrl() . '/images/person-300.jpg';
+               if (!empty($data['photo']) && !empty($data['baseurl'])) {
+                       $data['baseurl'] = Network::getUrlMatch(Strings::normaliseLink($data['baseurl']), Strings::normaliseLink($data['photo']));
+               } elseif (empty($data['photo'])) {
+                       $data['photo'] = DI::baseUrl() . '/images/person-300.jpg';
                }
 
                if (empty($data['name'])) {
@@ -641,20 +651,20 @@ class Probe
                if (in_array($network, ["", Protocol::DFRN])) {
                        $result = self::dfrn($webfinger);
                }
-               if ((empty($result['network']) && ($network == "")) || ($network == Protocol::DIASPORA)) {
-                       $result = self::diaspora($webfinger, $result);
+               if ((!$result && ($network == "")) || ($network == Protocol::DIASPORA)) {
+                       $result = self::diaspora($webfinger);
                }
-               if ((empty($result['network']) && ($network == "")) || ($network == Protocol::OSTATUS)) {
-                       $result = self::ostatus($webfinger, false, $result);
+               if ((!$result && ($network == "")) || ($network == Protocol::OSTATUS)) {
+                       $result = self::ostatus($webfinger);
                }
-               if ((empty($result['network']) && ($network == "")) || ($network == Protocol::PUMPIO)) {
-                       $result = self::pumpio($webfinger, $addr, $result);
+               if (in_array($network, ['', Protocol::ZOT])) {
+                       $result = self::zot($webfinger, $result);
                }
-               if ((empty($result['network']) && ($network == "")) || ($network == Protocol::ZOT)) {
-                       $result = self::hubzilla($webfinger, $result);
+               if ((!$result && ($network == "")) || ($network == Protocol::PUMPIO)) {
+                       $result = self::pumpio($webfinger, $addr);
                }
-               if ((empty($result['network']) && ($network == "")) || ($network == Protocol::FEED)) {
-                       $result = self::feed($uri, true, $result);
+               if ((!$result && ($network == "")) || ($network == Protocol::FEED)) {
+                       $result = self::feed($uri);
                } else {
                        // We overwrite the detected nick with our try if the previois routines hadn't detected it.
                        // Additionally it is overwritten when the nickname doesn't make sense (contains spaces).
@@ -677,7 +687,7 @@ class Probe
 
                Logger::log($uri." is ".$result["network"], Logger::DEBUG);
 
-               if (empty($result["baseurl"])) {
+               if (empty($result["baseurl"]) && ($result["network"] != Protocol::PHANTOM)) {
                        $pos = strpos($result["url"], $host);
                        if ($pos) {
                                $result["baseurl"] = substr($result["url"], 0, $pos).$host;
@@ -686,42 +696,56 @@ class Probe
                return $result;
        }
 
-       private static function hubzilla($webfinger, $data)
+       /**
+        * Check for Zot contact
+        *
+        * @param array $webfinger Webfinger data
+        * @param array $data      previously probed data
+        *
+        * @return array Zot data
+        * @throws HTTPException\InternalServerErrorException
+        */
+       private static function zot($webfinger, $data)
        {
-               if (strstr($webfinger['properties']['http://purl.org/zot/federation'] ?? '', 'zot')) {
-                       $data['network'] = Protocol::ZOT;
-               }
-               if (!empty($webfinger['properties']['http://webfinger.net/ns/name'])) {
-                       $data['name'] = $webfinger['properties']['http://webfinger.net/ns/name'];
+               if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) {
+                       foreach ($webfinger["aliases"] as $alias) {
+                               if (substr($alias, 0, 5) == 'acct:') {
+                                       $data["addr"] = substr($alias, 5);
+                               }
+                       }
                }
-               if (!empty($webfinger['properties']['https://w3id.org/security/v1#publicKeyPem'])) {
-                       $data['pubkey'] = $webfinger['properties']['https://w3id.org/security/v1#publicKeyPem'];
+
+               if (!empty($webfinger["subject"]) && (substr($webfinger["subject"], 0, 5) == "acct:")) {
+                       $data["addr"] = substr($webfinger["subject"], 5);
                }
-//print_r($webfinger);
-               $hcard_url = '';
+
                $zot_url = '';
                foreach ($webfinger['links'] as $link) {
-                       if (($link['rel'] == 'http://microformats.org/profile/hcard') && !empty($link['href'])) {
-                               $hcard_url = $link['href'];
-                       } elseif (($link['rel'] == 'http://purl.org/zot/protocol') && !empty($link['href'])) {
+                       if (($link['rel'] == 'http://purl.org/zot/protocol') && !empty($link['href'])) {
                                $zot_url = $link['href'];
-                       } elseif (($link["rel"] == "http://purl.org/zot/protocol/6.0") && !empty($link["href"])) {
-                               $data["url"] = $link["href"];
-                       } elseif (($link["rel"] == "http://webfinger.net/rel/blog") && !empty($link["href"]) && empty($data["url"])) {
-                               $data["url"] = $link["href"];
                        }
                }
 
                if (empty($zot_url) && !empty($data['addr']) && !empty(self::$baseurl)) {
+                       $condition = ['nurl' => Strings::normaliseLink(self::$baseurl), 'platform' => ['hubzilla']];
+                       if (!DBA::exists('gserver', $condition)) {
+                               return $data;
+                       }
                        $zot_url = self::$baseurl . '/.well-known/zot-info?address=' . $data['addr'];
                }
 
-               if (!empty($hcard_url)) {
-                       $data = self::pollHcard($hcard_url, $data, false);
+               if (empty($zot_url)) {
+                       return $data;
                }
 
-               if (!empty($zot_url)) {
-                       $data = self::pollZot($zot_url, $data);
+               $data = self::pollZot($zot_url, $data);
+
+               if (!empty($data['url']) && !empty($webfinger['aliases']) && is_array($webfinger['aliases'])) {
+                       foreach ($webfinger['aliases'] as $alias) {
+                               if (!strstr($alias, '@') && Strings::normaliseLink($alias) != Strings::normaliseLink($data['url'])) {
+                                       $data['alias'] = $alias;
+                               }
+                       }
                }
 
                return $data;
@@ -743,19 +767,29 @@ class Probe
                        return $data;
                }
 
-               if (!empty($json['protocols']) && in_array('zot', $json['protocols'])) {
-                       $data['network'] = Protocol::ZOT;
+               if (empty($data['network'])) {
+                       if (!empty($json['protocols']) && in_array('zot', $json['protocols'])) {
+                               $data['network'] = Protocol::ZOT;
+                       } elseif (!isset($json['protocols'])) {
+                               $data['network'] = Protocol::ZOT;
+                       }
                }
 
-               if (!empty($json['guid'])) {
+               if (!empty($json['guid']) && empty($data['guid'])) {
                        $data['guid'] = $json['guid'];
                }
-               if (!empty($json['key'])) {
+               if (!empty($json['key']) && empty($data['pubkey'])) {
                        $data['pubkey'] = $json['key'];
                }
                if (!empty($json['name'])) {
                        $data['name'] = $json['name'];
                }
+               if (!empty($json['photo'])) {
+                       $data['photo'] = $json['photo'];
+                       if (!empty($json['photo_updated'])) {
+                               $data['photo'] .= '?rev=' . urlencode($json['photo_updated']);
+                       }
+               }
                if (!empty($json['address'])) {
                        $data['addr'] = $json['address'];
                }
@@ -1119,7 +1153,7 @@ class Probe
                }
 
                if (!isset($data["network"]) || ($hcard_url == "")) {
-                       return $data;
+                       return false;
                }
 
                // Fetch data via noscrape - this is faster
@@ -1277,12 +1311,10 @@ class Probe
         * @return array Diaspora data
         * @throws HTTPException\InternalServerErrorException
         */
-       private static function diaspora($webfinger, $data)
+       private static function diaspora($webfinger)
        {
                $hcard_url = "";
-
-               unset($data["guid"]);
-               unset($data["pubkey"]);
+               $data = [];
 
                // The array is reversed to take into account the order of preference for same-rel links
                // See: https://tools.ietf.org/html/rfc7033#section-4.4.4
@@ -1312,7 +1344,7 @@ class Probe
                }
 
                if (empty($data["url"]) || empty($hcard_url)) {
-                       return $data;
+                       return false;
                }
 
                if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) {
@@ -1332,6 +1364,10 @@ class Probe
                // Fetch further information from the hcard
                $data = self::pollHcard($hcard_url, $data);
 
+               if (!$data) {
+                       return false;
+               }
+
                if (!empty($data["url"])
                        && !empty($data["guid"])
                        && !empty($data["baseurl"])
@@ -1348,6 +1384,8 @@ class Probe
                        // We have to overwrite the detected value for "notify" since Hubzilla doesn't send it
                        $data["notify"] = $data["baseurl"] . "/receive/users/" . $data["guid"];
                        $data["batch"]  = $data["baseurl"] . "/receive/public";
+               } else {
+                       return false;
                }
 
                return $data;
@@ -1362,8 +1400,10 @@ class Probe
         * @return array|bool OStatus data or "false" on error or "true" on short mode
         * @throws HTTPException\InternalServerErrorException
         */
-       private static function ostatus($webfinger, $short = false, $data = [])
+       private static function ostatus($webfinger, $short = false)
        {
+               $data = [];
+
                if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) {
                        foreach ($webfinger["aliases"] as $alias) {
                                if (strstr($alias, "@") && !strstr(Strings::normaliseLink($alias), "http://")) {
@@ -1404,11 +1444,7 @@ class Probe
                                                $curlResult = Network::curl($pubkey);
                                                if ($curlResult->isTimeout()) {
                                                        self::$istimeout = true;
-                                                       if ($short) {
-                                                               return false;
-                                                       } else {
-                                                               return $data;
-                                                       }
+                                                       return false;
                                                }
                                                $pubkey = $curlResult->getBody();
                                        }
@@ -1429,10 +1465,8 @@ class Probe
                        && isset($data["url"])
                ) {
                        $data["network"] = Protocol::OSTATUS;
-               } elseif ($short) {
-                       return false;
                } else {
-                       return $data;
+                       return false;
                }
 
                if ($short) {
@@ -1443,7 +1477,7 @@ class Probe
                $curlResult = Network::curl($data["poll"]);
                if ($curlResult->isTimeout()) {
                        self::$istimeout = true;
-                       return $data;
+                       return false;
                }
                $feed = $curlResult->getBody();
                $dummy1 = null;
@@ -1451,7 +1485,7 @@ class Probe
                $dummy2 = null;
                $feed_data = Feed::import($feed, $dummy1, $dummy2, $dummy3, true);
                if (!$feed_data) {
-                       return $data;
+                       return false;
                }
 
                if (!empty($feed_data["header"]["author-name"])) {
@@ -1496,8 +1530,13 @@ class Probe
         */
        private static function pumpioProfileData($profile_link)
        {
+               $curlResult = Network::curl($profile_link);
+               if (!$curlResult->isSuccess()) {
+                       return false;
+               }
+
                $doc = new DOMDocument();
-               if (!@$doc->loadHTMLFile($profile_link)) {
+               if (!@$doc->loadHTML($curlResult->getBody())) {
                        return false;
                }
 
@@ -1546,13 +1585,13 @@ class Probe
        /**
         * @brief Check for pump.io contact
         *
-        * @param array $webfinger Webfinger data
-        *
-        * @param       $addr
+        * @param array  $webfinger Webfinger data
+        * @param string $addr
         * @return array pump.io data
         */
-       private static function pumpio($webfinger, $addr, $data)
+       private static function pumpio($webfinger, $addr)
        {
+               $data = [];
                // The array is reversed to take into account the order of preference for same-rel links
                // See: https://tools.ietf.org/html/rfc7033#section-4.4.4
                foreach (array_reverse($webfinger["links"]) as $link) {
@@ -1579,13 +1618,13 @@ class Probe
 
                        $data["network"] = Protocol::PUMPIO;
                } else {
-                       return $data;
+                       return false;
                }
 
                $profile_data = self::pumpioProfileData($data["url"]);
 
                if (!$profile_data) {
-                       return $data;
+                       return false;
                }
 
                $data = array_merge($data, $profile_data);
@@ -1669,9 +1708,13 @@ class Probe
         */
        private static function getFeedLink($url)
        {
-               $doc = new DOMDocument();
+               $curlResult = Network::curl($url);
+               if (!$curlResult->isSuccess()) {
+                       return false;
+               }
 
-               if (!@$doc->loadHTMLFile($url)) {
+               $doc = new DOMDocument();
+               if (!@$doc->loadHTML($curlResult->getBody())) {
                        return false;
                }
 
@@ -1712,12 +1755,12 @@ class Probe
         * @return array feed data
         * @throws HTTPException\InternalServerErrorException
         */
-       private static function feed($url, $probe = true, $data = [])
+       private static function feed($url, $probe = true)
        {
                $curlResult = Network::curl($url);
                if ($curlResult->isTimeout()) {
                        self::$istimeout = true;
-                       return $data;
+                       return false;
                }
                $feed = $curlResult->getBody();
                $dummy1 = $dummy2 = $dummy3 = null;
@@ -1725,13 +1768,13 @@ class Probe
 
                if (!$feed_data) {
                        if (!$probe) {
-                               return $data;
+                               return false;
                        }
 
                        $feed_url = self::getFeedLink($url);
 
                        if (!$feed_url) {
-                               return $data;
+                               return false;
                        }
 
                        return self::feed($feed_url, false);