]> git.mxchange.org Git - friendica.git/blobdiff - src/Network/Probe.php
Add Contact Object
[friendica.git] / src / Network / Probe.php
index 8e0bc1ab4d9adf22d897b5c16a2509b1cbf0bab0..b758956123826cbca02dbd7b1c07af196017044f 100644 (file)
@@ -9,7 +9,16 @@ namespace Friendica\Network;
  */
 
 use Friendica\App;
+use Friendica\Core\System;
+use Friendica\Core\Cache;
 use Friendica\Core\Config;
+use Friendica\Database\DBM;
+use Friendica\Object\Profile;
+use Friendica\Util\XML;
+
+use dba;
+use DomXPath;
+use DOMDocument;
 
 require_once 'include/feed.php';
 require_once 'include/email.php';
@@ -30,7 +39,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",
@@ -52,16 +61,35 @@ class Probe {
        }
 
        /**
-        * @brief Probes for XRD data
+        * @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 static 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 webfinger path via "host-meta"
         *
         * @param string $host The host part of an url
         *
-        * @return array
-        *      'lrdd' => Link to LRDD endpoint
-        *      'lrdd-xml' => Link to LRDD endpoint in XML format
-        *      'lrdd-json' => Link to LRDD endpoint in JSON format
+        * @return array with template and type of the webfinger template for JSON or XML
         */
-       private function xrd($host) {
+       private static function hostMeta($host) {
 
                // Reset the static variable
                self::$baseurl = '';
@@ -72,35 +100,44 @@ 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) {
-                       return false;
+               if ($ret['success']) {
+                       $xml = $ret['body'];
+                       $xrd = parse_xml_string($xml, false);
+                       $host_url = 'https://'.$host;
                }
-               $xml = $ret['body'];
-
-               $xrd = parse_xml_string($xml, false);
 
                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);
+                       $host_url = 'http://'.$host;
                }
                if (!is_object($xrd)) {
-                       return false;
+                       logger("No xrd object found for ".$host, LOGGER_DEBUG);
+                       return array();
                }
 
-               $links = \xml::element_to_array($xrd);
+               $links = XML::element_to_array($xrd);
                if (!isset($links["xrd"]["link"])) {
-                       return false;
+                       logger("No xrd data found for ".$host, LOGGER_DEBUG);
+                       return array();
                }
 
-               $xrd_data = array();
+               $lrdd = array();
+               // The following webfinger path is defined in RFC 7033 https://tools.ietf.org/html/rfc7033
+               // Problem is that Hubzilla currently doesn't provide all data in the JSON webfinger
+               // compared to the XML webfinger. So this is commented out by now.
+               // $lrdd = array("application/jrd+json" => $host_url.'/.well-known/webfinger?resource={uri}');
 
                foreach ($links["xrd"]["link"] as $value => $link) {
-                       if (isset($link["@attributes"])) {
+                       if (!empty($link["@attributes"])) {
                                $attributes = $link["@attributes"];
                        } elseif ($value == "@attributes") {
                                $attributes = $link;
@@ -108,22 +145,18 @@ class Probe {
                                continue;
                        }
 
-                       if (($attributes["rel"] == "lrdd")
-                               AND ($attributes["type"] == "application/xrd+xml")
-                       ) {
-                               $xrd_data["lrdd-xml"] = $attributes["template"];
-                       } elseif (($attributes["rel"] == "lrdd")
-                               AND ($attributes["type"] == "application/json")
-                       ) {
-                               $xrd_data["lrdd-json"] = $attributes["template"];
-                       } elseif ($attributes["rel"] == "lrdd") {
-                               $xrd_data["lrdd"] = $attributes["template"];
+                       if (($attributes["rel"] == "lrdd") && !empty($attributes["template"])) {
+                               $type = (empty($attributes["type"]) ? '' : $attributes["type"]);
+
+                               $lrdd[$type] = $attributes["template"];
                        }
                }
 
                self::$baseurl = "http://".$host;
 
-               return $xrd_data;
+               logger("Probing successful for ".$host, LOGGER_DEBUG);
+
+               return $lrdd;
        }
 
        /**
@@ -146,7 +179,6 @@ class Probe {
         *
         * @return string profile link
         */
-
        public static function webfingerDfrn($webbie, &$hcard_url) {
 
                $profile_link = '';
@@ -158,7 +190,7 @@ class Probe {
                                if ($link['@attributes']['rel'] === NAMESPACE_DFRN) {
                                        $profile_link = $link['@attributes']['href'];
                                }
-                               if (($link['@attributes']['rel'] === NAMESPACE_OSTATUSSUB) AND ($profile_link == "")) {
+                               if (($link['@attributes']['rel'] === NAMESPACE_OSTATUSSUB) && ($profile_link == "")) {
                                        $profile_link = 'stat:'.$link['@attributes']['template'];
                                }
                                if ($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard') {
@@ -182,9 +214,13 @@ class Probe {
         */
        public static function lrdd($uri) {
 
-               $lrdd = self::xrd($uri);
+               $lrdd = self::hostMeta($uri);
                $webfinger = null;
 
+               if (is_bool($lrdd)) {
+                       return array();
+               }
+
                if (!$lrdd) {
                        $parts = @parse_url($uri);
                        if (!$parts) {
@@ -192,7 +228,7 @@ class Probe {
                        }
 
                        $host = $parts["host"];
-                       if (isset($parts["port"])) {
+                       if (!empty($parts["port"])) {
                                $host .= ':'.$parts["port"];
                        }
 
@@ -201,47 +237,45 @@ class Probe {
                        $nick = array_pop($path_parts);
 
                        do {
-                               $lrdd = self::xrd($host);
+                               $lrdd = self::hostMeta($host);
                                $host .= "/".array_shift($path_parts);
-                       } while (!$lrdd AND (sizeof($path_parts) > 0));
+                       } while (!$lrdd && (sizeof($path_parts) > 0));
                }
 
                if (!$lrdd) {
+                       logger("No lrdd data found for ".$uri, LOGGER_DEBUG);
                        return array();
                }
 
-               foreach ($lrdd as $key => $link) {
+               foreach ($lrdd AS $type => $template) {
                        if ($webfinger) {
                                continue;
                        }
 
-                       if (!in_array($key, array("lrdd", "lrdd-xml", "lrdd-json"))) {
-                               continue;
-                       }
-
-                       $path = str_replace('{uri}', urlencode($uri), $link);
-                       $webfinger = self::webfinger($path);
+                       $path = str_replace('{uri}', urlencode($uri), $template);
+                       $webfinger = self::webfinger($path, $type);
 
-                       if (!$webfinger AND (strstr($uri, "@"))) {
-                               $path = str_replace('{uri}', urlencode("acct:".$uri), $link);
-                               $webfinger = self::webfinger($path);
+                       if (!$webfinger && (strstr($uri, "@"))) {
+                               $path = str_replace('{uri}', urlencode("acct:".$uri), $template);
+                               $webfinger = self::webfinger($path, $type);
                        }
 
                        // Special treatment for Mastodon
                        // Problem is that Mastodon uses an URL format like http://domain.tld/@nick
                        // But the webfinger for this format fails.
-                       if (!$webfinger AND isset($nick)) {
+                       if (!$webfinger && !empty($nick)) {
                                // Mastodon uses a "@" as prefix for usernames in their url format
                                $nick = ltrim($nick, '@');
 
                                $addr = $nick."@".$host;
 
-                               $path = str_replace('{uri}', urlencode("acct:".$addr), $link);
-                               $webfinger = self::webfinger($path);
+                               $path = str_replace('{uri}', urlencode("acct:".$addr), $template);
+                               $webfinger = self::webfinger($path, $type);
                        }
                }
 
                if (!is_array($webfinger["links"])) {
+                       logger("No webfinger links found for ".$uri, LOGGER_DEBUG);
                        return false;
                }
 
@@ -272,16 +306,16 @@ class Probe {
         *
         * @return array uri data
         */
-       public static function uri($uri, $network = "", $uid = 0, $cache = true) {
+       public static function uri($uri, $network = "", $uid = -1, $cache = true) {
 
                if ($cache) {
-                       $result = \Cache::get("probe_url:".$network.":".$uri);
+                       $result = Cache::get("Probe::uri:".$network.":".$uri);
                        if (!is_null($result)) {
                                return $result;
                        }
                }
 
-               if ($uid == 0) {
+               if ($uid == -1) {
                        $uid = local_user();
                }
 
@@ -294,11 +328,11 @@ class Probe {
                if ($data["photo"] != "") {
                        $data["baseurl"] = matching_url(normalise_link($data["baseurl"]), normalise_link($data["photo"]));
                } else {
-                       $data["photo"] = App::get_baseurl().'/images/person-175.jpg';
+                       $data["photo"] = System::baseUrl().'/images/person-175.jpg';
                }
 
-               if (!isset($data["name"]) OR ($data["name"] == "")) {
-                       if (isset($data["nick"])) {
+               if (empty($data["name"])) {
+                       if (!empty($data["nick"])) {
                                $data["name"] = $data["nick"];
                        }
 
@@ -307,7 +341,7 @@ class Probe {
                        }
                }
 
-               if (!isset($data["nick"]) OR ($data["nick"] == "")) {
+               if (empty($data["nick"])) {
                        $data["nick"] = strtolower($data["name"]);
 
                        if (strpos($data['nick'], ' ')) {
@@ -327,39 +361,152 @@ 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::uri:".$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.
                        /// It should only be updated if the existing picture isn't existing anymore.
                        /// We only update the contact when it is no probing for a specific network.
                        if (($data['network'] != NETWORK_FEED)
-                               AND ($network == "")
-                               AND $data["name"]
-                               AND $data["nick"]
-                               AND $data["url"]
-                               AND $data["addr"]
-                               AND $data["poll"]
+                               && ($network == "")
+                               && $data["name"]
+                               && $data["nick"]
+                               && $data["url"]
+                               && $data["addr"]
+                               && $data["poll"]
                        ) {
-                               q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `url` = '%s', `addr` = '%s',
-                                               `notify` = '%s', `poll` = '%s', `alias` = '%s', `success_update` = '%s'
-                                       WHERE `nurl` = '%s' AND NOT `self` AND `uid` = 0",
-                                       dbesc($data["name"]),
-                                       dbesc($data["nick"]),
-                                       dbesc($data["url"]),
-                                       dbesc($data["addr"]),
-                                       dbesc($data["notify"]),
-                                       dbesc($data["poll"]),
-                                       dbesc($data["alias"]),
-                                       dbesc(datetime_convert()),
-                                       dbesc(normalise_link($data['url']))
-                               );
+                               $fields = array('name' => $data['name'],
+                                               'nick' => $data['nick'],
+                                               'url' => $data['url'],
+                                               'addr' => $data['addr'],
+                                               'photo' => $data['photo'],
+                                               'keywords' => $data['keywords'],
+                                               'location' => $data['location'],
+                                               'about' => $data['about'],
+                                               'notify' => $data['notify'],
+                                               'network' => $data['network'],
+                                               'server_url' => $data['baseurl']);
+
+                               $fieldnames = array();
+
+                               foreach ($fields AS $key => $val) {
+                                       if (empty($val)) {
+                                               unset($fields[$key]);
+                                       } else {
+                                               $fieldnames[] = $key;
+                                       }
+                               }
+
+                               $fields['updated'] = DBM::date();
+
+                               $condition = array('nurl' => normalise_link($data["url"]));
+
+                               $old_fields = dba::select('gcontact', $fieldnames, $condition, array('limit' => 1));
+
+                               dba::update('gcontact', $fields, $condition, $old_fields);
+
+                               $fields = array('name' => $data['name'],
+                                               'nick' => $data['nick'],
+                                               'url' => $data['url'],
+                                               'addr' => $data['addr'],
+                                               'alias' => $data['alias'],
+                                               'keywords' => $data['keywords'],
+                                               'location' => $data['location'],
+                                               'about' => $data['about'],
+                                               'batch' => $data['batch'],
+                                               'notify' => $data['notify'],
+                                               'poll' => $data['poll'],
+                                               'request' => $data['request'],
+                                               'confirm' => $data['confirm'],
+                                               'poco' => $data['poco'],
+                                               'network' => $data['network'],
+                                               'success_update' => DBM::date());
+
+                               $fieldnames = array();
+
+                               foreach ($fields AS $key => $val) {
+                                       if (empty($val)) {
+                                               unset($fields[$key]);
+                                       } else {
+                                               $fieldnames[] = $key;
+                                       }
+                               }
+
+                               $condition = array('nurl' => normalise_link($data["url"]), 'self' => false, 'uid' => 0);
+
+                               $old_fields = dba::select('contact', $fieldnames, $condition, array('limit' => 1));
+
+                               dba::update('contact', $fields, $condition, $old_fields);
                        }
                }
 
                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, $type) {
+               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, $type);
+
+               // 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
         *
@@ -371,27 +518,32 @@ 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"]) AND isset($parts["host"]) AND isset($parts["path"])) {
+               if (!empty($parts["scheme"]) && !empty($parts["host"]) && !empty($parts["path"])) {
                        $host = $parts["host"];
-                       if (isset($parts["port"])) {
+                       if (!empty($parts["port"])) {
                                $host .= ':'.$parts["port"];
                        }
 
                        if ($host == 'twitter.com') {
                                return array("network" => NETWORK_TWITTER);
                        }
-                       $lrdd = self::xrd($host);
+                       $lrdd = self::hostMeta($host);
+
+                       if (is_bool($lrdd)) {
+                               return array();
+                       }
 
                        $path_parts = explode("/", trim($parts["path"], "/"));
 
-                       while (!$lrdd AND (sizeof($path_parts) > 1)) {
+                       while (!$lrdd && (sizeof($path_parts) > 1)) {
                                $host .= "/".array_shift($path_parts);
-                               $lrdd = self::xrd($host);
+                               $lrdd = self::hostMeta($host);
                        }
                        if (!$lrdd) {
+                               logger('No XRD data was found for '.$uri, LOGGER_DEBUG);
                                return self::feed($uri);
                        }
                        $nick = array_pop($path_parts);
@@ -420,14 +572,20 @@ class Probe {
                        if (strpos($uri, '@twitter.com')) {
                                return array("network" => NETWORK_TWITTER);
                        }
-                       $lrdd = self::xrd($host);
+                       $lrdd = self::hostMeta($host);
+
+                       if (is_bool($lrdd)) {
+                               return array();
+                       }
 
                        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;
                }
 
@@ -435,35 +593,37 @@ class Probe {
 
                /// @todo Do we need the prefix "acct:" or "acct://"?
 
-               foreach ($lrdd as $key => $link) {
+               foreach ($lrdd AS $type => $template) {
                        if ($webfinger) {
                                continue;
                        }
-                       if (!in_array($key, array("lrdd", "lrdd-xml", "lrdd-json"))) {
-                               continue;
-                       }
+
                        // At first try it with the given uri
-                       $path = str_replace('{uri}', urlencode($uri), $link);
-                       $webfinger = self::webfinger($path);
+                       $path = str_replace('{uri}', urlencode($uri), $template);
+                       $webfinger = self::webfinger($path, $type);
+
+                       // Fix possible problems with GNU Social probing to wrong scheme
+                       $webfinger = self::fixOstatus($webfinger, $template, $type);
 
                        // We cannot be sure that the detected address was correct, so we don't use the values
-                       if ($webfinger AND ($uri != $addr)) {
+                       if ($webfinger && ($uri != $addr)) {
                                $nick = "";
                                $addr = "";
                        }
 
                        // Try webfinger with the address (user@domain.tld)
                        if (!$webfinger) {
-                               $path = str_replace('{uri}', urlencode($addr), $link);
-                               $webfinger = self::webfinger($path);
+                               $path = str_replace('{uri}', urlencode($addr), $template);
+                               $webfinger = self::webfinger($path, $type);
                        }
 
                        // Mastodon needs to have it with "acct:"
                        if (!$webfinger) {
-                               $path = str_replace('{uri}', urlencode("acct:".$addr), $link);
-                               $webfinger = self::webfinger($path);
+                               $path = str_replace('{uri}', urlencode("acct:".$addr), $template);
+                               $webfinger = self::webfinger($path, $type);
                        }
                }
+
                if (!$webfinger) {
                        return self::feed($uri);
                }
@@ -475,32 +635,32 @@ class Probe {
                if (in_array($network, array("", NETWORK_DFRN))) {
                        $result = self::dfrn($webfinger);
                }
-               if ((!$result AND ($network == "")) OR ($network == NETWORK_DIASPORA)) {
+               if ((!$result && ($network == "")) || ($network == NETWORK_DIASPORA)) {
                        $result = self::diaspora($webfinger);
                }
-               if ((!$result AND ($network == "")) OR ($network == NETWORK_OSTATUS)) {
+               if ((!$result && ($network == "")) || ($network == NETWORK_OSTATUS)) {
                        $result = self::ostatus($webfinger);
                }
-               if ((!$result AND ($network == "")) OR ($network == NETWORK_PUMPIO)) {
+               if ((!$result && ($network == "")) || ($network == NETWORK_PUMPIO)) {
                        $result = self::pumpio($webfinger);
                }
-               if ((!$result AND ($network == "")) OR ($network == NETWORK_FEED)) {
+               if ((!$result && ($network == "")) || ($network == NETWORK_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).
-                       if ((!isset($result["nick"]) OR ($result["nick"] == "") OR (strstr($result["nick"], " "))) AND ($nick != "")) {
+                       if ((empty($result["nick"]) || (strstr($result["nick"], " "))) && ($nick != "")) {
                                $result["nick"] = $nick;
                        }
 
-                       if ((!isset($result["addr"]) OR ($result["addr"] == "")) AND ($addr != "")) {
+                       if (empty($result["addr"]) && ($addr != "")) {
                                $result["addr"] = $addr;
                        }
                }
 
                logger($uri." is ".$result["network"], LOGGER_DEBUG);
 
-               if (!isset($result["baseurl"]) OR ($result["baseurl"] == "")) {
+               if (empty($result["baseurl"])) {
                        $pos = strpos($result["url"], $host);
                        if ($pos) {
                                $result["baseurl"] = substr($result["url"], 0, $pos).$host;
@@ -519,49 +679,53 @@ class Probe {
         *
         * @return array webfinger data
         */
-       private function webfinger($url) {
+       private static function webfinger($url, $type) {
 
                $xrd_timeout = Config::get('system', 'xrd_timeout', 20);
                $redirects = 0;
 
-               $ret = z_fetch_url($url, false, $redirects, array('timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml'));
-               if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
+               $ret = z_fetch_url($url, false, $redirects, array('timeout' => $xrd_timeout, 'accept_content' => $type));
+                       if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
                        return false;
                }
                $data = $ret['body'];
 
-               $xrd = parse_xml_string($data, false);
-
-               if (!is_object($xrd)) {
-                       // If it is not XML, maybe it is JSON
-                       $webfinger = json_decode($data, true);
-
+               $webfinger = json_decode($data, true);
+               if (is_array($webfinger)) {
                        if (!isset($webfinger["links"])) {
+                               logger("No json webfinger links for ".$url, LOGGER_DEBUG);
                                return false;
                        }
-
                        return $webfinger;
                }
 
-               $xrd_arr = \xml::element_to_array($xrd);
+               // If it is not JSON, maybe it is XML
+               $xrd = parse_xml_string($data, false);
+               if (!is_object($xrd)) {
+                       logger("No webfinger data retrievable for ".$url, LOGGER_DEBUG);
+                       return false;
+               }
+
+               $xrd_arr = XML::element_to_array($xrd);
                if (!isset($xrd_arr["xrd"]["link"])) {
+                       logger("No XML webfinger links for ".$url, LOGGER_DEBUG);
                        return false;
                }
 
                $webfinger = array();
 
-               if (isset($xrd_arr["xrd"]["subject"])) {
+               if (!empty($xrd_arr["xrd"]["subject"])) {
                        $webfinger["subject"] = $xrd_arr["xrd"]["subject"];
                }
 
-               if (isset($xrd_arr["xrd"]["alias"])) {
+               if (!empty($xrd_arr["xrd"]["alias"])) {
                        $webfinger["aliases"] = $xrd_arr["xrd"]["alias"];
                }
 
                $webfinger["links"] = array();
 
                foreach ($xrd_arr["xrd"]["link"] as $value => $data) {
-                       if (isset($data["@attributes"])) {
+                       if (!empty($data["@attributes"])) {
                                $attributes = $data["@attributes"];
                        } elseif ($value == "@attributes") {
                                $attributes = $data;
@@ -585,38 +749,44 @@ 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;
                }
                $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;
                }
 
-               if (isset($json["fn"])) {
+               if (!empty($json["fn"])) {
                        $data["name"] = $json["fn"];
                }
 
-               if (isset($json["addr"])) {
+               if (!empty($json["addr"])) {
                        $data["addr"] = $json["addr"];
                }
 
-               if (isset($json["nick"])) {
+               if (!empty($json["nick"])) {
                        $data["nick"] = $json["nick"];
                }
 
-               if (isset($json["comm"])) {
+               if (!empty($json["guid"])) {
+                       $data["guid"] = $json["guid"];
+               }
+
+               if (!empty($json["comm"])) {
                        $data["community"] = $json["comm"];
                }
 
-               if (isset($json["tags"])) {
+               if (!empty($json["tags"])) {
                        $keywords = implode(" ", $json["tags"]);
                        if ($keywords != "") {
                                $data["keywords"] = $keywords;
@@ -628,31 +798,31 @@ class Probe {
                        $data["location"] = $location;
                }
 
-               if (isset($json["about"])) {
+               if (!empty($json["about"])) {
                        $data["about"] = $json["about"];
                }
 
-               if (isset($json["key"])) {
+               if (!empty($json["key"])) {
                        $data["pubkey"] = $json["key"];
                }
 
-               if (isset($json["photo"])) {
+               if (!empty($json["photo"])) {
                        $data["photo"] = $json["photo"];
                }
 
-               if (isset($json["dfrn-request"])) {
+               if (!empty($json["dfrn-request"])) {
                        $data["request"] = $json["dfrn-request"];
                }
 
-               if (isset($json["dfrn-confirm"])) {
+               if (!empty($json["dfrn-confirm"])) {
                        $data["confirm"] = $json["dfrn-confirm"];
                }
 
-               if (isset($json["dfrn-notify"])) {
+               if (!empty($json["dfrn-notify"])) {
                        $data["notify"] = $json["dfrn-notify"];
                }
 
-               if (isset($json["dfrn-poll"])) {
+               if (!empty($json["dfrn-poll"])) {
                        $data["poll"] = $json["dfrn-poll"];
                }
 
@@ -704,12 +874,12 @@ class Probe {
                $data = self::pollNoscrape($noscrape_url, $data);
 
                if (!isset($data["notify"])
-                       OR !isset($data["confirm"])
-                       OR !isset($data["request"])
-                       OR !isset($data["poll"])
-                       OR !isset($data["poco"])
-                       OR !isset($data["name"])
-                       OR !isset($data["photo"])
+                       || !isset($data["confirm"])
+                       || !isset($data["request"])
+                       || !isset($data["poll"])
+                       || !isset($data["poco"])
+                       || !isset($data["name"])
+                       || !isset($data["photo"])
                ) {
                        $data = self::pollHcard($profile_link, $data, true);
                }
@@ -738,38 +908,47 @@ class Probe {
         *
         * @return array DFRN data
         */
-       private function dfrn($webfinger) {
-
+       private static function dfrn($webfinger) {
                $hcard_url = "";
                $data = array();
                foreach ($webfinger["links"] as $link) {
-                       if (($link["rel"] == NAMESPACE_DFRN) AND ($link["href"] != "")) {
+                       if (($link["rel"] == NAMESPACE_DFRN) && ($link["href"] != "")) {
                                $data["network"] = NETWORK_DFRN;
-                       } elseif (($link["rel"] == NAMESPACE_FEED) AND ($link["href"] != "")) {
+                       } elseif (($link["rel"] == NAMESPACE_FEED) && ($link["href"] != "")) {
                                $data["poll"] = $link["href"];
-                       } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") AND ($link["type"] == "text/html") AND ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") && ($link["type"] == "text/html") && ($link["href"] != "")) {
                                $data["url"] = $link["href"];
-                       } elseif (($link["rel"] == "http://microformats.org/profile/hcard") AND ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "http://microformats.org/profile/hcard") && ($link["href"] != "")) {
                                $hcard_url = $link["href"];
-                       } elseif (($link["rel"] == NAMESPACE_POCO) AND ($link["href"] != "")) {
+                       } elseif (($link["rel"] == NAMESPACE_POCO) && ($link["href"] != "")) {
                                $data["poco"] = $link["href"];
-                       } elseif (($link["rel"] == "http://webfinger.net/rel/avatar") AND ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "http://webfinger.net/rel/avatar") && ($link["href"] != "")) {
                                $data["photo"] = $link["href"];
-                       } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") AND ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") && ($link["href"] != "")) {
                                $data["baseurl"] = trim($link["href"], '/');
-                       } elseif (($link["rel"] == "http://joindiaspora.com/guid") AND ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "http://joindiaspora.com/guid") && ($link["href"] != "")) {
                                $data["guid"] = $link["href"];
-                       } elseif (($link["rel"] == "diaspora-public-key") AND ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "diaspora-public-key") && ($link["href"] != "")) {
                                $data["pubkey"] = base64_decode($link["href"]);
 
-                               //if (strstr($data["pubkey"], 'RSA ') OR ($link["type"] == "RSA"))
+                               //if (strstr($data["pubkey"], 'RSA ') || ($link["type"] == "RSA"))
                                if (strstr($data["pubkey"], 'RSA ')) {
                                        $data["pubkey"] = rsatopem($data["pubkey"]);
                                }
                        }
                }
 
-               if (!isset($data["network"]) OR ($hcard_url == "")) {
+               if (is_array($webfinger["aliases"])) {
+                       foreach ($webfinger["aliases"] as $alias) {
+                               if (normalise_link($alias) != normalise_link($data["url"]) && ! strstr($alias, "@")) {
+                                       $data["alias"] = $alias;
+                               } elseif (substr($alias, 0, 5) == 'acct:') {
+                                       $data["addr"] = substr($alias, 5);
+                               }
+                       }
+               }
+
+               if (!isset($data["network"]) || ($hcard_url == "")) {
                        return false;
                }
 
@@ -778,11 +957,11 @@ class Probe {
                $data = self::pollNoscrape($noscrape_url, $data);
 
                if (isset($data["notify"])
-                       AND isset($data["confirm"])
-                       AND isset($data["request"])
-                       AND isset($data["poll"])
-                       AND isset($data["name"])
-                       AND isset($data["photo"])
+                       && isset($data["confirm"])
+                       && isset($data["request"])
+                       && isset($data["poll"])
+                       && isset($data["name"])
+                       && isset($data["photo"])
                ) {
                        return $data;
                }
@@ -801,7 +980,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;
@@ -811,12 +990,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)) {
@@ -829,7 +1008,7 @@ class Probe {
                        // We have to discard the guid from the hcard in favour of the guid from lrdd
                        // Reason: Hubzilla doesn't use the value "uid" in the hcard like Diaspora does.
                        $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' uid ')]", $vcard); // */
-                       if (($search->length > 0) AND ($data["guid"] == "")) {
+                       if (($search->length > 0) && ($data["guid"] == "")) {
                                $data["guid"] = $search->item(0)->nodeValue;
                        }
 
@@ -870,13 +1049,13 @@ class Probe {
                                $attr[$attribute->name] = trim($attribute->value);
                        }
 
-                       if (isset($attr["src"]) AND isset($attr["width"])) {
+                       if (isset($attr["src"]) && isset($attr["width"])) {
                                $avatar[$attr["width"]] = $attr["src"];
                        }
 
                        // We don't have a width. So we just take everything that we got.
                        // This is a Hubzilla workaround which doesn't send a width.
-                       if ((sizeof($avatar) == 0) AND isset($attr["src"])) {
+                       if ((sizeof($avatar) == 0) && !empty($attr["src"])) {
                                $avatar[] = $attr["src"];
                        }
                }
@@ -918,47 +1097,52 @@ class Probe {
         *
         * @return array Diaspora data
         */
-       private function diaspora($webfinger) {
-
+       private static function diaspora($webfinger) {
                $hcard_url = "";
                $data = array();
                foreach ($webfinger["links"] as $link) {
-                       if (($link["rel"] == "http://microformats.org/profile/hcard") AND ($link["href"] != "")) {
+                       if (($link["rel"] == "http://microformats.org/profile/hcard") && ($link["href"] != "")) {
                                $hcard_url = $link["href"];
-                       } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") AND ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") && ($link["href"] != "")) {
                                $data["baseurl"] = trim($link["href"], '/');
-                       } elseif (($link["rel"] == "http://joindiaspora.com/guid") AND ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "http://joindiaspora.com/guid") && ($link["href"] != "")) {
                                $data["guid"] = $link["href"];
-                       } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") AND ($link["type"] == "text/html") AND ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") && ($link["type"] == "text/html") && ($link["href"] != "")) {
                                $data["url"] = $link["href"];
-                       } elseif (($link["rel"] == NAMESPACE_FEED) AND ($link["href"] != "")) {
+                       } elseif (($link["rel"] == NAMESPACE_FEED) && ($link["href"] != "")) {
                                $data["poll"] = $link["href"];
-                       } elseif (($link["rel"] == NAMESPACE_POCO) AND ($link["href"] != "")) {
+                       } elseif (($link["rel"] == NAMESPACE_POCO) && ($link["href"] != "")) {
                                $data["poco"] = $link["href"];
-                       } elseif (($link["rel"] == "salmon") AND ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "salmon") && ($link["href"] != "")) {
                                $data["notify"] = $link["href"];
-                       } elseif (($link["rel"] == "diaspora-public-key") AND ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "diaspora-public-key") && ($link["href"] != "")) {
                                $data["pubkey"] = base64_decode($link["href"]);
 
-                               //if (strstr($data["pubkey"], 'RSA ') OR ($link["type"] == "RSA"))
+                               //if (strstr($data["pubkey"], 'RSA ') || ($link["type"] == "RSA"))
                                if (strstr($data["pubkey"], 'RSA ')) {
                                        $data["pubkey"] = rsatopem($data["pubkey"]);
                                }
                        }
                }
 
-               if (!isset($data["url"]) OR ($hcard_url == "")) {
+               if (!isset($data["url"]) || ($hcard_url == "")) {
                        return false;
                }
 
                if (is_array($webfinger["aliases"])) {
                        foreach ($webfinger["aliases"] as $alias) {
-                               if (normalise_link($alias) != normalise_link($data["url"]) AND ! strstr($alias, "@")) {
+                               if (normalise_link($alias) != normalise_link($data["url"]) && ! strstr($alias, "@")) {
                                        $data["alias"] = $alias;
+                               } elseif (substr($alias, 0, 5) == 'acct:') {
+                                       $data["addr"] = substr($alias, 5);
                                }
                        }
                }
 
+               if (!empty($webfinger["subject"]) && (substr($webfinger["subject"], 0, 5) == 'acct:')) {
+                       $data["addr"] = substr($webfinger["subject"], 5);
+               }
+
                // Fetch further information from the hcard
                $data = self::pollHcard($hcard_url, $data);
 
@@ -967,10 +1151,10 @@ class Probe {
                }
 
                if (isset($data["url"])
-                       AND isset($data["guid"])
-                       AND isset($data["baseurl"])
-                       AND isset($data["pubkey"])
-                       AND ($hcard_url != "")
+                       && isset($data["guid"])
+                       && isset($data["baseurl"])
+                       && isset($data["pubkey"])
+                       && ($hcard_url != "")
                ) {
                        $data["network"] = NETWORK_DIASPORA;
 
@@ -991,68 +1175,79 @@ 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) {
-                               if (strstr($alias, "@")) {
+                               if (strstr($alias, "@") && !strstr(normalise_link($alias), "http://")) {
                                        $data["addr"] = str_replace('acct:', '', $alias);
                                }
                        }
                }
 
-               if (is_string($webfinger["subject"]) AND strstr($webfinger["subject"], "@")) {
+               if (is_string($webfinger["subject"]) && strstr($webfinger["subject"], "@") &&
+                       !strstr(normalise_link($webfinger["subject"]), "http://")) {
                        $data["addr"] = str_replace('acct:', '', $webfinger["subject"]);
                }
-               $pubkey = "";
-               foreach ($webfinger["links"] as $link) {
-                       if (($link["rel"] == "http://webfinger.net/rel/profile-page")
-                               AND ($link["type"] == "text/html")
-                               AND ($link["href"] != "")
-                       ) {
-                               $data["url"] = $link["href"];
-                       } elseif (($link["rel"] == "salmon") AND ($link["href"] != "")) {
-                               $data["notify"] = $link["href"];
-                       } elseif (($link["rel"] == NAMESPACE_FEED) AND ($link["href"] != "")) {
-                               $data["poll"] = $link["href"];
-                       } elseif (($link["rel"] == "magic-public-key") AND ($link["href"] != "")) {
-                               $pubkey = $link["href"];
 
-                               if (substr($pubkey, 0, 5) === 'data:') {
-                                       if (strstr($pubkey, ',')) {
-                                               $pubkey = substr($pubkey, strpos($pubkey, ',') + 1);
-                                       } else {
-                                               $pubkey = substr($pubkey, 5);
-                                       }
-                               } elseif (normalise_link($pubkey) == 'http://') {
-                                       $ret = z_fetch_url($pubkey);
-                                       if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
-                                               return false;
+               $pubkey = "";
+               if (is_array($webfinger["links"])) {
+                       foreach ($webfinger["links"] as $link) {
+                               if (($link["rel"] == "http://webfinger.net/rel/profile-page")
+                                       && ($link["type"] == "text/html")
+                                       && ($link["href"] != "")
+                               ) {
+                                       $data["url"] = $link["href"];
+                               } elseif (($link["rel"] == "salmon") && ($link["href"] != "")) {
+                                       $data["notify"] = $link["href"];
+                               } elseif (($link["rel"] == NAMESPACE_FEED) && ($link["href"] != "")) {
+                                       $data["poll"] = $link["href"];
+                               } elseif (($link["rel"] == "magic-public-key") && ($link["href"] != "")) {
+                                       $pubkey = $link["href"];
+
+                                       if (substr($pubkey, 0, 5) === 'data:') {
+                                               if (strstr($pubkey, ',')) {
+                                                       $pubkey = substr($pubkey, strpos($pubkey, ',') + 1);
+                                               } else {
+                                                       $pubkey = substr($pubkey, 5);
+                                               }
+                                       } elseif (normalise_link($pubkey) == 'http://') {
+                                               $ret = z_fetch_url($pubkey);
+                                               if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
+                                                       return false;
+                                               }
+                                               $pubkey = $ret['body'];
                                        }
-                                       $pubkey = $ret['body'];
-                               }
 
-                               $key = explode(".", $pubkey);
+                                       $key = explode(".", $pubkey);
 
-                               if (sizeof($key) >= 3) {
-                                       $m = base64url_decode($key[1]);
-                                       $e = base64url_decode($key[2]);
-                                       $data["pubkey"] = metopem($m, $e);
+                                       if (sizeof($key) >= 3) {
+                                               $m = base64url_decode($key[1]);
+                                               $e = base64url_decode($key[2]);
+                                               $data["pubkey"] = metopem($m, $e);
+                                       }
                                }
                        }
                }
 
-               if (isset($data["notify"]) AND isset($data["pubkey"])
-                       AND isset($data["poll"])
-                       AND isset($data["url"])
+               if (isset($data["notify"]) && isset($data["pubkey"])
+                       && isset($data["poll"])
+                       && isset($data["url"])
                ) {
                        $data["network"] = NETWORK_OSTATUS;
                } 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) {
@@ -1063,6 +1258,7 @@ class Probe {
                if (!$feed_data) {
                        return false;
                }
+
                if ($feed_data["header"]["author-name"] != "") {
                        $data["name"] = $feed_data["header"]["author-name"];
                }
@@ -1086,6 +1282,12 @@ class Probe {
                if ($feed_data["header"]["author-link"] != "") {
                        $data["url"] = $feed_data["header"]["author-link"];
                }
+
+               if (($data['poll'] == $data['url']) && ($data["alias"] != '')) {
+                       $data['url'] = $data["alias"];
+                       $data["alias"] = '';
+               }
+
                /// @todo Fetch location and "about" from the feed as well
                return $data;
        }
@@ -1097,14 +1299,14 @@ class Probe {
         *
         * @return array profile data
         */
-       private function pumpioProfileData($profile_link) {
+       private static 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();
 
@@ -1137,26 +1339,26 @@ class Probe {
         *
         * @return array pump.io data
         */
-       private function pumpio($webfinger) {
+       private static function pumpio($webfinger) {
 
                $data = array();
                foreach ($webfinger["links"] as $link) {
                        if (($link["rel"] == "http://webfinger.net/rel/profile-page")
-                               AND ($link["type"] == "text/html")
-                               AND ($link["href"] != "")
+                               && ($link["type"] == "text/html")
+                               && ($link["href"] != "")
                        ) {
                                $data["url"] = $link["href"];
-                       } elseif (($link["rel"] == "activity-inbox") AND ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "activity-inbox") && ($link["href"] != "")) {
                                $data["notify"] = $link["href"];
-                       } elseif (($link["rel"] == "activity-outbox") AND ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "activity-outbox") && ($link["href"] != "")) {
                                $data["poll"] = $link["href"];
-                       } elseif (($link["rel"] == "dialback") AND ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "dialback") && ($link["href"] != "")) {
                                $data["dialback"] = $link["href"];
                        }
                }
-               if (isset($data["poll"]) AND isset($data["notify"])
-                       AND isset($data["dialback"])
-                       AND isset($data["url"])
+               if (isset($data["poll"]) && isset($data["notify"])
+                       && isset($data["dialback"])
+                       && isset($data["url"])
                ) {
                        // by now we use these fields only for the network type detection
                        // So we unset all data that isn't used at the moment
@@ -1185,14 +1387,14 @@ class Probe {
         *
         * @return string feed link
         */
-       private function getFeedLink($url) {
-               $doc = new \DOMDocument();
+       private static function getFeedLink($url) {
+               $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']");
@@ -1228,7 +1430,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;
@@ -1288,31 +1490,33 @@ class Probe {
         *
         * @return array mail data
         */
-       private function mail($uri, $uid) {
+       private static function mail($uri, $uid) {
 
                if (!validate_email($uri)) {
                        return false;
                }
 
-               $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
+               if ($uid != 0) {
+                       $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
 
-               $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1", intval($uid));
+                       $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1", intval($uid));
 
-               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']);
-                       $mbox = email_connect($mailbox, $r[0]['user'], $password);
-                       if (!mbox) {
-                               return false;
+                       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']);
+                               $mbox = email_connect($mailbox, $r[0]['user'], $password);
+                               if (!mbox) {
+                                       return false;
+                               }
                        }
-               }
 
-               $msgs = email_poll($mbox, $uri);
-               logger('searching '.$uri.', '.count($msgs).' messages found.', LOGGER_DEBUG);
+                       $msgs = email_poll($mbox, $uri);
+                       logger('searching '.$uri.', '.count($msgs).' messages found.', LOGGER_DEBUG);
 
-               if (!count($msgs)) {
-                       return false;
+                       if (!count($msgs)) {
+                               return false;
+                       }
                }
 
                $phost = substr($uri, strpos($uri, '@') + 1);
@@ -1323,7 +1527,7 @@ class Probe {
                $data["name"]    = substr($uri, 0, strpos($uri, '@'));
                $data["nick"]    = $data["name"];
                $data["photo"]   = avatar_img($uri);
-               $data["url"]     = 'http://'.$phost."/".$data["nick"];
+               $data["url"]     = 'mailto:'.$uri;
                $data["notify"]  = 'smtp '.random_string();
                $data["poll"]    = 'email '.random_string();
 
@@ -1353,7 +1557,9 @@ class Probe {
                                }
                        }
                }
-               imap_close($mbox);
+               if (!empty($mbox)) {
+                       imap_close($mbox);
+               }
 
                return $data;
        }