]> git.mxchange.org Git - friendica.git/blobdiff - src/Network/Probe.php
Merge remote-tracking branch 'upstream/3.5.2rc' into 1705-dbclean-advanced
[friendica.git] / src / Network / Probe.php
index 8e0bc1ab4d9adf22d897b5c16a2509b1cbf0bab0..94fa2733befa7eba890c82e457d3a84d27f4da40 100644 (file)
@@ -11,6 +11,13 @@ namespace Friendica\Network;
 use Friendica\App;
 use Friendica\Core\Config;
 
+use dbm;
+use Cache;
+use xml;
+
+use DomXPath;
+use DOMDocument;
+
 require_once 'include/feed.php';
 require_once 'include/email.php';
 require_once 'include/network.php';
@@ -51,6 +58,28 @@ class Probe {
                return $newdata;
        }
 
+       /**
+        * @brief Check if the hostname belongs to the own server
+        *
+        * @param string $host The hostname that is to be checked
+        *
+        * @return bool Does the testes hostname belongs to the own server?
+        */
+       private function ownHost($host) {
+               $own_host = get_app()->get_hostname();
+
+               $parts = parse_url($host);
+
+               if (!isset($parts['scheme'])) {
+                       $parts = parse_url('http://'.$host);
+               }
+
+               if (!isset($parts['host'])) {
+                       return false;
+               }
+               return $parts['host'] == $own_host;
+       }
+
        /**
         * @brief Probes for XRD data
         *
@@ -72,8 +101,11 @@ class Probe {
                $xrd_timeout = Config::get('system', 'xrd_timeout', 20);
                $redirects = 0;
 
+               logger("Probing for ".$host, LOGGER_DEBUG);
+
                $ret = z_fetch_url($ssl_url, false, $redirects, array('timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml'));
-               if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
+               if (($ret['errno'] == CURLE_OPERATION_TIMEDOUT) AND !self::ownHost($ssl_url)) {
+                       logger("Probing timeout for ".$ssl_url, LOGGER_DEBUG);
                        return false;
                }
                $xml = $ret['body'];
@@ -83,17 +115,20 @@ class Probe {
                if (!is_object($xrd)) {
                        $ret = z_fetch_url($url, false, $redirects, array('timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml'));
                        if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
+                               logger("Probing timeout for ".$url, LOGGER_DEBUG);
                                return false;
                        }
                        $xml = $ret['body'];
                        $xrd = parse_xml_string($xml, false);
                }
                if (!is_object($xrd)) {
+                       logger("No xrd object found for ".$host, LOGGER_DEBUG);
                        return false;
                }
 
-               $links = \xml::element_to_array($xrd);
+               $links = xml::element_to_array($xrd);
                if (!isset($links["xrd"]["link"])) {
+                       logger("No xrd data found for ".$host, LOGGER_DEBUG);
                        return false;
                }
 
@@ -123,6 +158,8 @@ class Probe {
 
                self::$baseurl = "http://".$host;
 
+               logger("Probing successful for ".$host, LOGGER_DEBUG);
+
                return $xrd_data;
        }
 
@@ -207,6 +244,7 @@ class Probe {
                }
 
                if (!$lrdd) {
+                       logger("No lrdd data found for ".$uri, LOGGER_DEBUG);
                        return array();
                }
 
@@ -242,6 +280,7 @@ class Probe {
                }
 
                if (!is_array($webfinger["links"])) {
+                       logger("No webfinger links found for ".$uri, LOGGER_DEBUG);
                        return false;
                }
 
@@ -275,7 +314,7 @@ class Probe {
        public static function uri($uri, $network = "", $uid = 0, $cache = true) {
 
                if ($cache) {
-                       $result = \Cache::get("probe_url:".$network.":".$uri);
+                       $result = Cache::get("probe_url:".$network.":".$uri);
                        if (!is_null($result)) {
                                return $result;
                        }
@@ -327,7 +366,7 @@ class Probe {
 
                // Only store into the cache if the value seems to be valid
                if (!in_array($data['network'], array(NETWORK_PHANTOM, NETWORK_MAIL))) {
-                       \Cache::set("probe_url:".$network.":".$uri, $data, CACHE_DAY);
+                       Cache::set("probe_url:".$network.":".$uri, $data, CACHE_DAY);
 
                        /// @todo temporary fix - we need a real contact update function that updates only changing fields
                        /// The biggest problem is the avatar picture that could have a reduced image size.
@@ -392,6 +431,7 @@ class Probe {
                                $lrdd = self::xrd($host);
                        }
                        if (!$lrdd) {
+                               logger('No XRD data was found for '.$uri, LOGGER_DEBUG);
                                return self::feed($uri);
                        }
                        $nick = array_pop($path_parts);
@@ -423,11 +463,13 @@ class Probe {
                        $lrdd = self::xrd($host);
 
                        if (!$lrdd) {
+                               logger('No XRD data was found for '.$uri, LOGGER_DEBUG);
                                return self::mail($uri, $uid);
                        }
                        $addr = $uri;
 
                } else {
+                       logger("Uri ".$uri." was not detectable", LOGGER_DEBUG);
                        return false;
                }
 
@@ -537,14 +579,16 @@ class Probe {
                        $webfinger = json_decode($data, true);
 
                        if (!isset($webfinger["links"])) {
+                               logger("No json webfinger links for ".$url, LOGGER_DEBUG);
                                return false;
                        }
 
                        return $webfinger;
                }
 
-               $xrd_arr = \xml::element_to_array($xrd);
+               $xrd_arr = xml::element_to_array($xrd);
                if (!isset($xrd_arr["xrd"]["link"])) {
+                       logger("No XML webfinger links for ".$url, LOGGER_DEBUG);
                        return false;
                }
 
@@ -592,11 +636,13 @@ class Probe {
                }
                $content = $ret['body'];
                if (!$content) {
+                       logger("Empty body for ".$noscrape_url, LOGGER_DEBUG);
                        return false;
                }
 
                $json = json_decode($content, true);
                if (!is_array($json)) {
+                       logger("No json data for ".$noscrape_url, LOGGER_DEBUG);
                        return false;
                }
 
@@ -811,12 +857,12 @@ class Probe {
                        return false;
                }
 
-               $doc = new \DOMDocument();
+               $doc = new DOMDocument();
                if (!@$doc->loadHTML($content)) {
                        return false;
                }
 
-               $xpath = new \DomXPath($doc);
+               $xpath = new DomXPath($doc);
 
                $vcards = $xpath->query("//div[contains(concat(' ', @class, ' '), ' vcard ')]");
                if (!is_object($vcards)) {
@@ -1099,12 +1145,12 @@ class Probe {
         */
        private function pumpioProfileData($profile_link) {
 
-               $doc = new \DOMDocument();
+               $doc = new DOMDocument();
                if (!@$doc->loadHTMLFile($profile_link)) {
                        return false;
                }
 
-               $xpath = new \DomXPath($doc);
+               $xpath = new DomXPath($doc);
 
                $data = array();
 
@@ -1186,13 +1232,13 @@ class Probe {
         * @return string feed link
         */
        private function getFeedLink($url) {
-               $doc = new \DOMDocument();
+               $doc = new DOMDocument();
 
                if (!@$doc->loadHTMLFile($url)) {
                        return false;
                }
 
-               $xpath = new \DomXPath($doc);
+               $xpath = new DomXPath($doc);
 
                //$feeds = $xpath->query("/html/head/link[@type='application/rss+xml']");
                $feeds = $xpath->query("/html/head/link[@type='application/rss+xml' and @rel='alternate']");
@@ -1298,7 +1344,7 @@ class Probe {
 
                $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1", intval($uid));
 
-               if (\dbm::is_result($x) && \dbm::is_result($r)) {
+               if (dbm::is_result($x) && dbm::is_result($r)) {
                        $mailbox = construct_mailbox_name($r[0]);
                        $password = '';
                        openssl_private_decrypt(hex2bin($r[0]['pass']), $password, $x[0]['prvkey']);