]> git.mxchange.org Git - friendica.git/blobdiff - src/Network/Probe.php
Merge pull request #3609 from rabuzarus/20170802_-_probe_camelcase
[friendica.git] / src / Network / Probe.php
index 9e5f1b8f89c57ee85ead1a99bd7ff4eb40833b84..f54db70a6249d2ca64d6dc8272d86d40b6d7d494 100644 (file)
@@ -37,7 +37,7 @@ class Probe {
         *
         * @return array Ordered data
         */
-       private function rearrangeData($data) {
+       private static function rearrangeData($data) {
                $fields = array("name", "nick", "guid", "url", "addr", "alias",
                                "photo", "community", "keywords", "location", "about",
                                "batch", "notify", "poll", "request", "confirm", "poco",
@@ -65,7 +65,7 @@ class Probe {
         *
         * @return bool Does the testes hostname belongs to the own server?
         */
-       private function ownHost($host) {
+       private static function ownHost($host) {
                $own_host = get_app()->get_hostname();
 
                $parts = parse_url($host);
@@ -90,7 +90,7 @@ class Probe {
         *      'lrdd-xml' => Link to LRDD endpoint in XML format
         *      'lrdd-json' => Link to LRDD endpoint in JSON format
         */
-       private function xrd($host) {
+       private static function xrd($host) {
 
                // Reset the static variable
                self::$baseurl = '';
@@ -183,7 +183,6 @@ class Probe {
         *
         * @return string profile link
         */
-
        public static function webfingerDfrn($webbie, &$hcard_url) {
 
                $profile_link = '';
@@ -403,6 +402,70 @@ 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
+        *
+        * @param array $webfinger Webfinger data
+        * @param string $lrdd Path template for webfinger request
+        *
+        * @return array fixed webfinger data
+        */
+       private static function fixOstatus($webfinger, $lrdd) {
+               if (empty($webfinger['links']) || empty($webfinger['subject'])) {
+                       return $webfinger;
+               }
+
+               $is_ostatus = false;
+               $has_key = false;
+
+               foreach ($webfinger['links'] as $link) {
+                       if ($link['rel'] == NAMESPACE_OSTATUSSUB) {
+                               $is_ostatus = true;
+                       }
+                       if ($link['rel'] == 'magic-public-key') {
+                               $has_key = true;
+                       }
+               }
+
+               if (!$is_ostatus || $has_key) {
+                       return $webfinger;
+               }
+
+               $url = self::switchScheme($webfinger['subject']);
+               $path = str_replace('{uri}', urlencode($url), $lrdd);
+               $webfinger2 = self::webfinger($path);
+
+               // Is the new webfinger detectable as OStatus?
+               if (self::ostatus($webfinger2, true)) {
+                       $webfinger = $webfinger2;
+               }
+
+               return $webfinger;
+       }
+
        /**
         * @brief Fetch information (protocol endpoints and user information) about a given uri
         *
@@ -414,7 +477,7 @@ class Probe {
         *
         * @return array uri data
         */
-       private function detect($uri, $network, $uid) {
+       private static function detect($uri, $network, $uid) {
                $parts = parse_url($uri);
 
                if (isset($parts["scheme"]) && isset($parts["host"]) && isset($parts["path"])) {
@@ -500,6 +563,9 @@ class Probe {
                        $path = str_replace('{uri}', urlencode($uri), $link);
                        $webfinger = self::webfinger($path);
 
+                       // Fix possible problems with GNU Social probing to wrong scheme
+                       $webfinger = self::fixOstatus($webfinger, $link);
+
                        // We cannot be sure that the detected address was correct, so we don't use the values
                        if ($webfinger && ($uri != $addr)) {
                                $nick = "";
@@ -573,7 +639,7 @@ class Probe {
         *
         * @return array webfinger data
         */
-       private function webfinger($url) {
+       private static function webfinger($url) {
 
                $xrd_timeout = Config::get('system', 'xrd_timeout', 20);
                $redirects = 0;
@@ -641,7 +707,7 @@ class Probe {
         *
         * @return array noscrape data
         */
-       private function pollNoscrape($noscrape_url, $data) {
+       private static function pollNoscrape($noscrape_url, $data) {
                $ret = z_fetch_url($noscrape_url);
                if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
                        return false;
@@ -796,7 +862,7 @@ class Probe {
         *
         * @return array DFRN data
         */
-       private function dfrn($webfinger) {
+       private static function dfrn($webfinger) {
 
                $hcard_url = "";
                $data = array();
@@ -867,7 +933,7 @@ class Probe {
         *
         * @return array hcard data
         */
-       private function pollHcard($hcard_url, $data, $dfrn = false) {
+       private static function pollHcard($hcard_url, $data, $dfrn = false) {
                $ret = z_fetch_url($hcard_url);
                if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
                        return false;
@@ -984,7 +1050,7 @@ class Probe {
         *
         * @return array Diaspora data
         */
-       private function diaspora($webfinger) {
+       private static function diaspora($webfinger) {
 
                $hcard_url = "";
                $data = array();
@@ -1057,10 +1123,11 @@ class Probe {
         * @brief Check for OStatus contact
         *
         * @param array $webfinger Webfinger data
+        * @param bool $short Short detection mode
         *
-        * @return array OStatus data
+        * @return array|bool OStatus data or "false" on error or "true" on short mode
         */
-       private function ostatus($webfinger) {
+       private static function ostatus($webfinger, $short = false) {
                $data = array();
                if (is_array($webfinger["aliases"])) {
                        foreach ($webfinger["aliases"] as $alias) {
@@ -1119,6 +1186,11 @@ class Probe {
                } else {
                        return false;
                }
+
+               if ($short) {
+                       return true;
+               }
+
                // Fetch all additional data from the feed
                $ret = z_fetch_url($data["poll"]);
                if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
@@ -1163,7 +1235,7 @@ class Probe {
         *
         * @return array profile data
         */
-       private function pumpioProfileData($profile_link) {
+       private static function pumpioProfileData($profile_link) {
 
                $doc = new DOMDocument();
                if (!@$doc->loadHTMLFile($profile_link)) {
@@ -1203,7 +1275,7 @@ class Probe {
         *
         * @return array pump.io data
         */
-       private function pumpio($webfinger) {
+       private static function pumpio($webfinger) {
 
                $data = array();
                foreach ($webfinger["links"] as $link) {
@@ -1251,7 +1323,7 @@ class Probe {
         *
         * @return string feed link
         */
-       private function getFeedLink($url) {
+       private static function getFeedLink($url) {
                $doc = new DOMDocument();
 
                if (!@$doc->loadHTMLFile($url)) {
@@ -1294,7 +1366,7 @@ class Probe {
         *
         * @return array feed data
         */
-       private function feed($url, $probe = true) {
+       private static function feed($url, $probe = true) {
                $ret = z_fetch_url($url);
                if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
                        return false;
@@ -1354,7 +1426,7 @@ class Probe {
         *
         * @return array mail data
         */
-       private function mail($uri, $uid) {
+       private static function mail($uri, $uid) {
 
                if (!validate_email($uri)) {
                        return false;