]> git.mxchange.org Git - friendica.git/blobdiff - src/Network/Probe.php
Fix missing mentions
[friendica.git] / src / Network / Probe.php
index 3ad33662a79211b83d052ee3812c9bf40b8a3d5e..2c84ddb85a8312f42a6e3f5d5723051e56045161 100644 (file)
@@ -12,11 +12,14 @@ namespace Friendica\Network;
 use DOMDocument;
 use Friendica\Core\Cache;
 use Friendica\Core\Config;
+use Friendica\Core\Protocol;
 use Friendica\Core\System;
 use Friendica\Database\DBA;
+use Friendica\Model\Contact;
 use Friendica\Model\Profile;
 use Friendica\Protocol\Email;
 use Friendica\Protocol\Feed;
+use Friendica\Protocol\ActivityPub;
 use Friendica\Util\Crypto;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
@@ -230,7 +233,7 @@ class Probe
 
                if (!$lrdd) {
                        $parts = @parse_url($uri);
-                       if (!$parts) {
+                       if (!$parts || empty($parts["host"]) || empty($parts["path"])) {
                                return [];
                        }
 
@@ -326,7 +329,17 @@ class Probe
                        $uid = local_user();
                }
 
-               $data = self::detect($uri, $network, $uid);
+               if ($network != Protocol::ACTIVITYPUB) {
+                       $data = self::detect($uri, $network, $uid);
+               } else {
+                       $data = null;
+               }
+
+               $ap_profile = ActivityPub::probeProfile($uri);
+
+               if (!empty($ap_profile) && (defaults($data, 'network', '') != Protocol::DFRN)) {
+                       $data = $ap_profile;
+               }
 
                if (!isset($data["url"])) {
                        $data["url"] = $uri;
@@ -361,20 +374,20 @@ class Probe
                }
 
                if (empty($data["network"])) {
-                       $data["network"] = NETWORK_PHANTOM;
+                       $data["network"] = Protocol::PHANTOM;
                }
 
                $data = self::rearrangeData($data);
 
                // Only store into the cache if the value seems to be valid
-               if (!in_array($data['network'], [NETWORK_PHANTOM, NETWORK_MAIL])) {
+               if (!in_array($data['network'], [Protocol::PHANTOM, Protocol::MAIL])) {
                        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)
+                       if (($data['network'] != Protocol::FEED)
                                && ($network == "")
                                && $data["name"]
                                && $data["nick"]
@@ -397,7 +410,7 @@ class Probe
                                // This doesn't cover the case when a community isn't a community anymore
                                if (!empty($data['community']) && $data['community']) {
                                        $fields['community'] = $data['community'];
-                                       $fields['contact-type'] = ACCOUNT_TYPE_COMMUNITY;
+                                       $fields['contact-type'] = Contact::ACCOUNT_TYPE_COMMUNITY;
                                }
 
                                $fieldnames = [];
@@ -447,7 +460,7 @@ class Probe
                                                'pubkey' => $data['pubkey'],
                                                'priority' => $data['priority'],
                                                'writable' => true,
-                                               'rel' => CONTACT_IS_SHARING];
+                                               'rel' => Contact::SHARING];
 
                                $fieldnames = [];
 
@@ -567,7 +580,7 @@ class Probe
                        }
 
                        if ($host == 'twitter.com') {
-                               return ["network" => NETWORK_TWITTER];
+                               return ["network" => Protocol::TWITTER];
                        }
                        $lrdd = self::hostMeta($host);
 
@@ -598,7 +611,7 @@ class Probe
                                return self::mail($uri, $uid);
                        }
 
-                       if ($network == NETWORK_MAIL) {
+                       if ($network == Protocol::MAIL) {
                                return self::mail($uri, $uid);
                        }
                        // Remove "acct:" from the URI
@@ -608,7 +621,7 @@ class Probe
                        $nick = substr($uri, 0, strpos($uri, '@'));
 
                        if (strpos($uri, '@twitter.com')) {
-                               return ["network" => NETWORK_TWITTER];
+                               return ["network" => Protocol::TWITTER];
                        }
                        $lrdd = self::hostMeta($host);
 
@@ -669,19 +682,19 @@ class Probe
 
                logger("Probing ".$uri, LOGGER_DEBUG);
 
-               if (in_array($network, ["", NETWORK_DFRN])) {
+               if (in_array($network, ["", Protocol::DFRN])) {
                        $result = self::dfrn($webfinger);
                }
-               if ((!$result && ($network == "")) || ($network == NETWORK_DIASPORA)) {
+               if ((!$result && ($network == "")) || ($network == Protocol::DIASPORA)) {
                        $result = self::diaspora($webfinger);
                }
-               if ((!$result && ($network == "")) || ($network == NETWORK_OSTATUS)) {
+               if ((!$result && ($network == "")) || ($network == Protocol::OSTATUS)) {
                        $result = self::ostatus($webfinger);
                }
-               if ((!$result && ($network == "")) || ($network == NETWORK_PUMPIO)) {
+               if ((!$result && ($network == "")) || ($network == Protocol::PUMPIO)) {
                        $result = self::pumpio($webfinger, $addr);
                }
-               if ((!$result && ($network == "")) || ($network == NETWORK_FEED)) {
+               if ((!$result && ($network == "")) || ($network == Protocol::FEED)) {
                        $result = self::feed($uri);
                } else {
                        // We overwrite the detected nick with our try if the previois routines hadn't detected it.
@@ -696,7 +709,7 @@ class Probe
                }
 
                if (empty($result["network"])) {
-                       $result["network"] = NETWORK_PHANTOM;
+                       $result["network"] = Protocol::PHANTOM;
                }
 
                if (empty($result["url"])) {
@@ -924,7 +937,6 @@ class Probe
                        || !isset($data["confirm"])
                        || !isset($data["request"])
                        || !isset($data["poll"])
-                       || !isset($data["poco"])
                        || !isset($data["name"])
                        || !isset($data["photo"])
                ) {
@@ -932,13 +944,19 @@ class Probe
                }
 
                $prof_data = [];
+
+               if (empty($data["addr"]) || empty($data["nick"])) {
+                       $probe_data = self::uri($profile_link);
+                       $data["addr"] = defaults($data, "addr", $probe_data["addr"]);
+                       $data["nick"] = defaults($data, "nick", $probe_data["nick"]);
+               }
+
                $prof_data["addr"]         = $data["addr"];
                $prof_data["nick"]         = $data["nick"];
                $prof_data["dfrn-request"] = $data["request"];
                $prof_data["dfrn-confirm"] = $data["confirm"];
                $prof_data["dfrn-notify"]  = $data["notify"];
                $prof_data["dfrn-poll"]    = $data["poll"];
-               $prof_data["dfrn-poco"]    = $data["poco"];
                $prof_data["photo"]        = $data["photo"];
                $prof_data["fn"]           = $data["name"];
                $prof_data["key"]          = $data["pubkey"];
@@ -960,23 +978,23 @@ class Probe
                $hcard_url = "";
                $data = [];
                foreach ($webfinger["links"] as $link) {
-                       if (($link["rel"] == NAMESPACE_DFRN) && ($link["href"] != "")) {
-                               $data["network"] = NETWORK_DFRN;
-                       } elseif (($link["rel"] == NAMESPACE_FEED) && ($link["href"] != "")) {
+                       if (($link["rel"] == NAMESPACE_DFRN) && !empty($link["href"])) {
+                               $data["network"] = Protocol::DFRN;
+                       } elseif (($link["rel"] == NAMESPACE_FEED) && !empty($link["href"])) {
                                $data["poll"] = $link["href"];
-                       } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") && ($link["type"] == "text/html") && ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") && (defaults($link, "type", "") == "text/html") && !empty($link["href"])) {
                                $data["url"] = $link["href"];
-                       } elseif (($link["rel"] == "http://microformats.org/profile/hcard") && ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "http://microformats.org/profile/hcard") && !empty($link["href"])) {
                                $hcard_url = $link["href"];
-                       } elseif (($link["rel"] == NAMESPACE_POCO) && ($link["href"] != "")) {
+                       } elseif (($link["rel"] == NAMESPACE_POCO) && !empty($link["href"])) {
                                $data["poco"] = $link["href"];
-                       } elseif (($link["rel"] == "http://webfinger.net/rel/avatar") && ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "http://webfinger.net/rel/avatar") && !empty($link["href"])) {
                                $data["photo"] = $link["href"];
-                       } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") && ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") && !empty($link["href"])) {
                                $data["baseurl"] = trim($link["href"], '/');
-                       } elseif (($link["rel"] == "http://joindiaspora.com/guid") && ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "http://joindiaspora.com/guid") && !empty($link["href"])) {
                                $data["guid"] = $link["href"];
-                       } elseif (($link["rel"] == "diaspora-public-key") && ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "diaspora-public-key") && !empty($link["href"])) {
                                $data["pubkey"] = base64_decode($link["href"]);
 
                                //if (strstr($data["pubkey"], 'RSA ') || ($link["type"] == "RSA"))
@@ -988,7 +1006,9 @@ class Probe
 
                if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) {
                        foreach ($webfinger["aliases"] as $alias) {
-                               if (normalise_link($alias) != normalise_link($data["url"]) && ! strstr($alias, "@")) {
+                               if (empty($data["url"]) && !strstr($alias, "@")) {
+                                       $data["url"] = $alias;
+                               } elseif (!strstr($alias, "@") && normalise_link($alias) != normalise_link($data["url"])) {
                                        $data["alias"] = $alias;
                                } elseif (substr($alias, 0, 5) == 'acct:') {
                                        $data["addr"] = substr($alias, 5);
@@ -1161,21 +1181,21 @@ class Probe
                $hcard_url = "";
                $data = [];
                foreach ($webfinger["links"] as $link) {
-                       if (($link["rel"] == "http://microformats.org/profile/hcard") && ($link["href"] != "")) {
+                       if (($link["rel"] == "http://microformats.org/profile/hcard") && !empty($link["href"])) {
                                $hcard_url = $link["href"];
-                       } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") && ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") && !empty($link["href"])) {
                                $data["baseurl"] = trim($link["href"], '/');
-                       } elseif (($link["rel"] == "http://joindiaspora.com/guid") && ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "http://joindiaspora.com/guid") && !empty($link["href"])) {
                                $data["guid"] = $link["href"];
-                       } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") && ($link["type"] == "text/html") && ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") && (defaults($link, "type", "") == "text/html") && !empty($link["href"])) {
                                $data["url"] = $link["href"];
-                       } elseif (($link["rel"] == NAMESPACE_FEED) && ($link["href"] != "")) {
+                       } elseif (($link["rel"] == NAMESPACE_FEED) && !empty($link["href"])) {
                                $data["poll"] = $link["href"];
-                       } elseif (($link["rel"] == NAMESPACE_POCO) && ($link["href"] != "")) {
+                       } elseif (($link["rel"] == NAMESPACE_POCO) && !empty($link["href"])) {
                                $data["poco"] = $link["href"];
-                       } elseif (($link["rel"] == "salmon") && ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "salmon") && !empty($link["href"])) {
                                $data["notify"] = $link["href"];
-                       } elseif (($link["rel"] == "diaspora-public-key") && ($link["href"] != "")) {
+                       } elseif (($link["rel"] == "diaspora-public-key") && !empty($link["href"])) {
                                $data["pubkey"] = base64_decode($link["href"]);
 
                                //if (strstr($data["pubkey"], 'RSA ') || ($link["type"] == "RSA"))
@@ -1216,7 +1236,7 @@ class Probe
                        && isset($data["pubkey"])
                        && ($hcard_url != "")
                ) {
-                       $data["network"] = NETWORK_DIASPORA;
+                       $data["network"] = Protocol::DIASPORA;
 
                        // The Diaspora handle must always be lowercase
                        if (!empty($data["addr"])) {
@@ -1263,15 +1283,15 @@ class Probe
                if (is_array($webfinger["links"])) {
                        foreach ($webfinger["links"] as $link) {
                                if (($link["rel"] == "http://webfinger.net/rel/profile-page")
-                                       && ($link["type"] == "text/html")
+                                       && (defaults($link, "type", "") == "text/html")
                                        && ($link["href"] != "")
                                ) {
                                        $data["url"] = $link["href"];
-                               } elseif (($link["rel"] == "salmon") && ($link["href"] != "")) {
+                               } elseif (($link["rel"] == "salmon") && !empty($link["href"])) {
                                        $data["notify"] = $link["href"];
-                               } elseif (($link["rel"] == NAMESPACE_FEED) && ($link["href"] != "")) {
+                               } elseif (($link["rel"] == NAMESPACE_FEED) && !empty($link["href"])) {
                                        $data["poll"] = $link["href"];
-                               } elseif (($link["rel"] == "magic-public-key") && ($link["href"] != "")) {
+                               } elseif (($link["rel"] == "magic-public-key") && !empty($link["href"])) {
                                        $pubkey = $link["href"];
 
                                        if (substr($pubkey, 0, 5) === 'data:') {
@@ -1303,7 +1323,7 @@ class Probe
                        && isset($data["poll"])
                        && isset($data["url"])
                ) {
-                       $data["network"] = NETWORK_OSTATUS;
+                       $data["network"] = Protocol::OSTATUS;
                } else {
                        return false;
                }
@@ -1427,7 +1447,7 @@ class Probe
                $data = [];
                foreach ($webfinger["links"] as $link) {
                        if (($link["rel"] == "http://webfinger.net/rel/profile-page")
-                               && ($link["type"] == "text/html")
+                               && (defaults($link, "type", "") == "text/html")
                                && ($link["href"] != "")
                        ) {
                                $data["url"] = $link["href"];
@@ -1447,7 +1467,7 @@ class Probe
                        // So we unset all data that isn't used at the moment
                        unset($data["dialback"]);
 
-                       $data["network"] = NETWORK_PUMPIO;
+                       $data["network"] = Protocol::PUMPIO;
                } else {
                        return false;
                }
@@ -1570,7 +1590,7 @@ class Probe
                        $data["baseurl"] = $data["url"];
                }
 
-               $data["network"] = NETWORK_FEED;
+               $data["network"] = Protocol::FEED;
 
                return $data;
        }
@@ -1593,18 +1613,22 @@ class Probe
                        return false;
                }
 
-               $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
+               $user = DBA::selectFirst('user', ['prvkey'], ['uid' => $uid]);
 
-               $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1", intval($uid));
+               $condition = ["`uid` = ? AND `server` != ''", $uid];
+               $fields = ['pass', 'user', 'server', 'port', 'ssltype', 'mailbox'];
+               $mailacct = DBA::selectFirst('mailacct', $fields, $condition);
 
-               if (DBA::isResult($x) && DBA::isResult($r)) {
-                       $mailbox = Email::constructMailboxName($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 (!DBA::isResult($user) || !DBA::isResult($mailacct)) {
+                       return false;
+               }
+
+               $mailbox = Email::constructMailboxName($mailacct);
+               $password = '';
+               openssl_private_decrypt(hex2bin($mailacct['pass']), $password, $user['prvkey']);
+               $mbox = Email::connect($mailbox, $mailacct['user'], $password);
+               if (!$mbox) {
+                       return false;
                }
 
                $msgs = Email::poll($mbox, $uri);
@@ -1618,7 +1642,7 @@ class Probe
 
                $data = [];
                $data["addr"]    = $uri;
-               $data["network"] = NETWORK_MAIL;
+               $data["network"] = Protocol::MAIL;
                $data["name"]    = substr($uri, 0, strpos($uri, '@'));
                $data["nick"]    = $data["name"];
                $data["photo"]   = Network::lookupAvatarByEmail($uri);
@@ -1655,7 +1679,6 @@ class Probe
                if (!empty($mbox)) {
                        imap_close($mbox);
                }
-
                return $data;
        }