]> git.mxchange.org Git - friendica.git/blobdiff - src/Network/Probe.php
Fix unliking liked
[friendica.git] / src / Network / Probe.php
index dc533d18de7f464480083f7baed14271bfb28f27..5f665814b4d3cda1c48e924e2c47e6c80dd04b0e 100644 (file)
@@ -20,7 +20,7 @@ use Friendica\Protocol\Feed;
 use Friendica\Util\Crypto;
 use Friendica\Util\Network;
 use Friendica\Util\XML;
-
+use Friendica\Util\DateTimeFormat;
 use dba;
 use DOMXPath;
 use DOMDocument;
@@ -357,11 +357,11 @@ class Probe
                        }
                }
 
-               if (self::$baseurl != "") {
+               if (!empty(self::$baseurl)) {
                        $data["baseurl"] = self::$baseurl;
                }
 
-               if (!isset($data["network"])) {
+               if (empty($data["network"])) {
                        $data["network"] = NETWORK_PHANTOM;
                }
 
@@ -395,6 +395,12 @@ class Probe
                                                'network' => $data['network'],
                                                'server_url' => $data['baseurl']];
 
+                               // 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;
+                               }
+
                                $fieldnames = [];
 
                                foreach ($fields as $key => $val) {
@@ -411,6 +417,17 @@ class Probe
 
                                $old_fields = dba::selectFirst('gcontact', $fieldnames, $condition);
 
+                               // When the gcontact doesn't exist, the value "true" will trigger an insert.
+                               // In difference to the public contacts we want to have every contact
+                               // in the world in our global contacts.
+                               if (!$old_fields) {
+                                       $old_fields = true;
+
+                                       // These values have to be set only on insert
+                                       $fields['photo'] = $data['photo'];
+                                       $fields['created'] = DateTimeFormat::utcNow();
+                               }
+
                                dba::update('gcontact', $fields, $condition, $old_fields);
 
                                $fields = ['name' => $data['name'],
@@ -428,7 +445,10 @@ class Probe
                                                'confirm' => $data['confirm'],
                                                'poco' => $data['poco'],
                                                'network' => $data['network'],
-                                               'success_update' => DBM::date()];
+                                               'pubkey' => $data['pubkey'],
+                                               'priority' => $data['priority'],
+                                               'writable' => true,
+                                               'rel' => CONTACT_IS_SHARING];
 
                                $fieldnames = [];
 
@@ -442,8 +462,16 @@ class Probe
 
                                $condition = ['nurl' => normalise_link($data["url"]), 'self' => false, 'uid' => 0];
 
+                               // "$old_fields" will return a "false" when the contact doesn't exist.
+                               // This won't trigger an insert. This is intended, since we only need
+                               // public contacts for everyone we store items from.
+                               // We don't need to store every contact on the planet.
                                $old_fields = dba::selectFirst('contact', $fieldnames, $condition);
 
+                               $fields['name-date'] = DateTimeFormat::utcNow();
+                               $fields['uri-date'] = DateTimeFormat::utcNow();
+                               $fields['success_update'] = DateTimeFormat::utcNow();
+
                                dba::update('contact', $fields, $condition, $old_fields);
                        }
                }
@@ -652,7 +680,7 @@ class Probe
                        $result = self::ostatus($webfinger);
                }
                if ((!$result && ($network == "")) || ($network == NETWORK_PUMPIO)) {
-                       $result = self::pumpio($webfinger);
+                       $result = self::pumpio($webfinger, $addr);
                }
                if ((!$result && ($network == "")) || ($network == NETWORK_FEED)) {
                        $result = self::feed($uri);
@@ -676,7 +704,6 @@ class Probe
                                $result["baseurl"] = substr($result["url"], 0, $pos).$host;
                        }
                }
-
                return $result;
        }
 
@@ -962,6 +989,10 @@ class Probe
                        }
                }
 
+               if (substr($webfinger["subject"], 0, 5) == "acct:") {
+                       $data["addr"] = substr($webfinger["subject"], 5);
+               }
+
                if (!isset($data["network"]) || ($hcard_url == "")) {
                        return false;
                }
@@ -1328,14 +1359,33 @@ class Probe
 
                $data = [];
 
-               // This is ugly - but pump.io doesn't seem to know a better way for it
-               $data["name"] = trim($xpath->query("//h1[@class='media-header']")->item(0)->nodeValue);
-               $pos = strpos($data["name"], chr(10));
-               if ($pos) {
-                       $data["name"] = trim(substr($data["name"], 0, $pos));
+               $data["name"] = $xpath->query("//span[contains(@class, 'p-name')]")->item(0)->nodeValue;
+
+               if ($data["name"] == '') {
+                       // This is ugly - but pump.io doesn't seem to know a better way for it
+                       $data["name"] = trim($xpath->query("//h1[@class='media-header']")->item(0)->nodeValue);
+                       $pos = strpos($data["name"], chr(10));
+                       if ($pos) {
+                               $data["name"] = trim(substr($data["name"], 0, $pos));
+                       }
+               }
+
+               $data["location"] = $xpath->query("//p[contains(@class, 'p-locality')]")->item(0)->nodeValue;
+
+               if ($data["location"] == '') {
+                       $data["location"] = $xpath->query("//p[contains(@class, 'location')]")->item(0)->nodeValue;
                }
 
-               $avatar = $xpath->query("//img[@class='img-rounded media-object']")->item(0);
+               $data["about"] = $xpath->query("//p[contains(@class, 'p-note')]")->item(0)->nodeValue;
+
+               if ($data["about"] == '') {
+                       $data["about"] = $xpath->query("//p[contains(@class, 'summary')]")->item(0)->nodeValue;
+               }
+
+               $avatar = $xpath->query("//img[contains(@class, 'u-photo')]")->item(0);
+               if (!$avatar) {
+                       $avatar = $xpath->query("//img[@class='img-rounded media-object']")->item(0);
+               }
                if ($avatar) {
                        foreach ($avatar->attributes as $attribute) {
                                if ($attribute->name == "src") {
@@ -1344,9 +1394,6 @@ class Probe
                        }
                }
 
-               $data["location"] = $xpath->query("//p[@class='location']")->item(0)->nodeValue;
-               $data["about"] = $xpath->query("//p[@class='summary']")->item(0)->nodeValue;
-
                return $data;
        }
 
@@ -1357,7 +1404,7 @@ class Probe
         *
         * @return array pump.io data
         */
-       private static function pumpio($webfinger)
+       private static function pumpio($webfinger, $addr)
        {
                $data = [];
                foreach ($webfinger["links"] as $link) {
@@ -1395,6 +1442,13 @@ class Probe
 
                $data = array_merge($data, $profile_data);
 
+               if (($addr != '') && ($data['name'] != '')) {
+                       $name = trim(str_replace($addr, '', $data['name']));
+                       if ($name != '') {
+                               $data['name'] = $name;
+                       }
+               }
+
                return $data;
        }
 
@@ -1516,29 +1570,31 @@ class Probe
                        return false;
                }
 
-               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));
+               if ($uid == 0) {
+                       return false;
+               }
 
-                       if (DBM::is_result($x) && DBM::is_result($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;
-                               }
-                       }
+               $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
 
-                       $msgs = Email::poll($mbox, $uri);
-                       logger('searching '.$uri.', '.count($msgs).' messages found.', LOGGER_DEBUG);
+               $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1", intval($uid));
 
-                       if (!count($msgs)) {
+               if (DBM::is_result($x) && DBM::is_result($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;
                        }
                }
 
+               $msgs = Email::poll($mbox, $uri);
+               logger('searching '.$uri.', '.count($msgs).' messages found.', LOGGER_DEBUG);
+
+               if (!count($msgs)) {
+                       return false;
+               }
+
                $phost = substr($uri, strpos($uri, '@') + 1);
 
                $data = [];