]> git.mxchange.org Git - friendica.git/blobdiff - src/Network/Probe.php
Add Contact Object
[friendica.git] / src / Network / Probe.php
index aa2e27c81d500dcfdf9514a377aaf4c2f58abba2..b758956123826cbca02dbd7b1c07af196017044f 100644 (file)
@@ -10,12 +10,13 @@ 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 dbm;
-use Cache;
-use xml;
-
+use dba;
 use DomXPath;
 use DOMDocument;
 
@@ -82,16 +83,13 @@ class Probe {
        }
 
        /**
-        * @brief Probes for XRD data
+        * @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 static function xrd($host) {
+       private static function hostMeta($host) {
 
                // Reset the static variable
                self::$baseurl = '';
@@ -108,6 +106,7 @@ class Probe {
                if ($ret['success']) {
                        $xml = $ret['body'];
                        $xrd = parse_xml_string($xml, false);
+                       $host_url = 'https://'.$host;
                }
 
                if (!is_object($xrd)) {
@@ -118,19 +117,24 @@ class Probe {
                        }
                        $xml = $ret['body'];
                        $xrd = parse_xml_string($xml, false);
+                       $host_url = 'http://'.$host;
                }
                if (!is_object($xrd)) {
                        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"])) {
                        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 (!empty($link["@attributes"])) {
@@ -141,16 +145,10 @@ class Probe {
                                continue;
                        }
 
-                       if (($attributes["rel"] == "lrdd")
-                               && ($attributes["type"] == "application/xrd+xml")
-                       ) {
-                               $xrd_data["lrdd-xml"] = $attributes["template"];
-                       } elseif (($attributes["rel"] == "lrdd")
-                               && ($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"];
                        }
                }
 
@@ -158,7 +156,7 @@ class Probe {
 
                logger("Probing successful for ".$host, LOGGER_DEBUG);
 
-               return $xrd_data;
+               return $lrdd;
        }
 
        /**
@@ -216,7 +214,7 @@ class Probe {
         */
        public static function lrdd($uri) {
 
-               $lrdd = self::xrd($uri);
+               $lrdd = self::hostMeta($uri);
                $webfinger = null;
 
                if (is_bool($lrdd)) {
@@ -239,7 +237,7 @@ class Probe {
                        $nick = array_pop($path_parts);
 
                        do {
-                               $lrdd = self::xrd($host);
+                               $lrdd = self::hostMeta($host);
                                $host .= "/".array_shift($path_parts);
                        } while (!$lrdd && (sizeof($path_parts) > 0));
                }
@@ -249,21 +247,17 @@ class Probe {
                        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 && (strstr($uri, "@"))) {
-                               $path = str_replace('{uri}', urlencode("acct:".$uri), $link);
-                               $webfinger = self::webfinger($path);
+                               $path = str_replace('{uri}', urlencode("acct:".$uri), $template);
+                               $webfinger = self::webfinger($path, $type);
                        }
 
                        // Special treatment for Mastodon
@@ -275,8 +269,8 @@ class Probe {
 
                                $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);
                        }
                }
 
@@ -312,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();
                }
 
@@ -367,7 +361,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::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.
@@ -381,19 +375,68 @@ class Probe {
                                && $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);
                        }
                }
 
@@ -431,7 +474,7 @@ class Probe {
         *
         * @return array fixed webfinger data
         */
-       private static function fixOstatus($webfinger, $lrdd) {
+       private static function fixOstatus($webfinger, $lrdd, $type) {
                if (empty($webfinger['links']) || empty($webfinger['subject'])) {
                        return $webfinger;
                }
@@ -454,7 +497,7 @@ class Probe {
 
                $url = self::switchScheme($webfinger['subject']);
                $path = str_replace('{uri}', urlencode($url), $lrdd);
-               $webfinger2 = self::webfinger($path);
+               $webfinger2 = self::webfinger($path, $type);
 
                // Is the new webfinger detectable as OStatus?
                if (self::ostatus($webfinger2, true)) {
@@ -487,7 +530,7 @@ class Probe {
                        if ($host == 'twitter.com') {
                                return array("network" => NETWORK_TWITTER);
                        }
-                       $lrdd = self::xrd($host);
+                       $lrdd = self::hostMeta($host);
 
                        if (is_bool($lrdd)) {
                                return array();
@@ -497,7 +540,7 @@ class Probe {
 
                        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);
@@ -529,7 +572,7 @@ 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();
@@ -550,19 +593,17 @@ 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, $link);
+                       $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 && ($uri != $addr)) {
@@ -572,16 +613,17 @@ class Probe {
 
                        // 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);
                }
@@ -637,32 +679,34 @@ class Probe {
         *
         * @return array webfinger data
         */
-       private static 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;
@@ -734,6 +778,10 @@ class Probe {
                        $data["nick"] = $json["nick"];
                }
 
+               if (!empty($json["guid"])) {
+                       $data["guid"] = $json["guid"];
+               }
+
                if (!empty($json["comm"])) {
                        $data["community"] = $json["comm"];
                }
@@ -861,7 +909,6 @@ class Probe {
         * @return array DFRN data
         */
        private static function dfrn($webfinger) {
-
                $hcard_url = "";
                $data = array();
                foreach ($webfinger["links"] as $link) {
@@ -893,7 +940,9 @@ class Probe {
 
                if (is_array($webfinger["aliases"])) {
                        foreach ($webfinger["aliases"] as $alias) {
-                               if (substr($alias, 0, 5) == 'acct:') {
+                               if (normalise_link($alias) != normalise_link($data["url"]) && ! strstr($alias, "@")) {
+                                       $data["alias"] = $alias;
+                               } elseif (substr($alias, 0, 5) == 'acct:') {
                                        $data["addr"] = substr($alias, 5);
                                }
                        }
@@ -1049,7 +1098,6 @@ class Probe {
         * @return array Diaspora data
         */
        private static function diaspora($webfinger) {
-
                $hcard_url = "";
                $data = array();
                foreach ($webfinger["links"] as $link) {
@@ -1085,10 +1133,16 @@ class Probe {
                        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 (!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);
 
@@ -1127,51 +1181,56 @@ class Probe {
         */
        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"]) && 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")
-                               && ($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 = "";
+               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);
+                                       }
                                }
                        }
                }
@@ -1437,25 +1496,27 @@ class Probe {
                        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);
@@ -1466,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();
 
@@ -1496,7 +1557,9 @@ class Probe {
                                }
                        }
                }
-               imap_close($mbox);
+               if (!empty($mbox)) {
+                       imap_close($mbox);
+               }
 
                return $data;
        }