]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Diaspora.php
Fix: Events on Diaspora now should look fine
[friendica.git] / src / Protocol / Diaspora.php
index a8fe194470fa4b558bb6f03308687070b0c57763..cc4b230831de489328c8d3ff6bc1b93b97726526 100644 (file)
@@ -9,6 +9,7 @@
  */
 namespace Friendica\Protocol;
 
+use Friendica\Content\Text\BBCode;
 use Friendica\Core\Cache;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
@@ -28,6 +29,7 @@ use Friendica\Util\Crypto;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
 use Friendica\Util\XML;
+use Friendica\Util\Map;
 use dba;
 use SimpleXMLElement;
 
@@ -220,11 +222,20 @@ class Diaspora
 
                $signable_data = $msg.".".base64url_encode($type).".".base64url_encode($encoding).".".base64url_encode($alg);
 
+               if ($handle == '') {
+                       logger('No author could be decoded. Discarding. Message: ' . $envelope);
+                       return false;
+               }
+
                $key = self::key($handle);
+               if ($key == '') {
+                       logger("Couldn't get a key for handle " . $handle . ". Discarding.");
+                       return false;
+               }
 
                $verify = Crypto::rsaVerify($signable_data, $sig, $key);
                if (!$verify) {
-                       logger('Message did not verify. Discarding.');
+                       logger('Message from ' . $handle . ' did not verify. Discarding.');
                        return false;
                }
 
@@ -320,7 +331,16 @@ class Diaspora
                // Get the senders' public key
                $key_id = $base->sig[0]->attributes()->key_id[0];
                $author_addr = base64_decode($key_id);
+               if ($author_addr == '') {
+                       logger('No author could be decoded. Discarding. Message: ' . $xml);
+                       System::httpExit(400);
+               }
+
                $key = self::key($author_addr);
+               if ($key == '') {
+                       logger("Couldn't get a key for handle " . $author_addr . ". Discarding.");
+                       System::httpExit(400);
+               }
 
                $verify = Crypto::rsaVerify($signed_data, $signature, $key);
                if (!$verify) {
@@ -825,7 +845,12 @@ class Diaspora
                        // if Diaspora connectivity is enabled on their server
                        if ($r && ($r["network"] === NETWORK_DIASPORA)) {
                                self::addFContact($r, $update);
-                               $person = $r;
+
+                               // Fetch the updated or added contact
+                               $person = dba::selectFirst('fcontact', [], ['network' => NETWORK_DIASPORA, 'addr' => $handle]);
+                               if (!DBM::is_result($person)) {
+                                       $person = $r;
+                               }
                        }
                }
 
@@ -902,23 +927,23 @@ class Diaspora
        }
 
        /**
-        * @brief get a handle (user@domain.tld) from a given contact id or gcontact id
+        * @brief get a handle (user@domain.tld) from a given contact id
         *
         * @param int $contact_id  The id in the contact table
-        * @param int $gcontact_id The id in the gcontact table
+        * @param int $pcontact_id The id in the contact table (Used for the public contact)
         *
         * @return string the handle
         */
-       public static function handleFromContact($contact_id, $gcontact_id = 0)
+       private static function handleFromContact($contact_id, $pcontact_id = 0)
        {
                $handle = false;
 
-               logger("contact id is ".$contact_id." - gcontact id is ".$gcontact_id, LOGGER_DEBUG);
+               logger("contact id is ".$contact_id." - pcontact id is ".$pcontact_id, LOGGER_DEBUG);
 
-               if ($gcontact_id != 0) {
+               if ($pcontact_id != 0) {
                        $r = q(
-                               "SELECT `addr` FROM `gcontact` WHERE `id` = %d AND `addr` != ''",
-                               intval($gcontact_id)
+                               "SELECT `addr` FROM `contact` WHERE `id` = %d AND `addr` != ''",
+                               intval($pcontact_id)
                        );
 
                        if (DBM::is_result($r)) {
@@ -1059,7 +1084,7 @@ class Diaspora
                //}
 
                // We don't seem to like that person
-               if ($contact["blocked"] || $contact["readonly"] || $contact["archive"]) {
+               if ($contact["blocked"] || $contact["readonly"]) {
                        // Maybe blocked, don't accept.
                        return false;
                        // We are following this person?
@@ -1376,26 +1401,23 @@ class Diaspora
        /**
         * @brief returns contact details
         *
-        * @param array $contact The default contact if the person isn't found
-        * @param array $person  The record of the person
-        * @param int   $uid     The user id
+        * @param array $def_contact The default contact if the person isn't found
+        * @param array $person      The record of the person
+        * @param int   $uid         The user id
         *
         * @return array
         *      'cid' => contact id
         *      'network' => network type
         */
-       private static function authorContactByUrl($contact, $person, $uid)
+       private static function authorContactByUrl($def_contact, $person, $uid)
        {
-               $r = q(
-                       "SELECT `id`, `network`, `url` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d LIMIT 1",
-                       dbesc(normalise_link($person["url"])),
-                       intval($uid)
-               );
-               if ($r) {
-                       $cid = $r[0]["id"];
-                       $network = $r[0]["network"];
-               } else {
+               $condition = ['nurl' => normalise_link($person["url"]), 'uid' => $uid];
+               $contact = dba::selectFirst('contact', ['id', 'network'], $condition);
+               if (DBM::is_result($contact)) {
                        $cid = $contact["id"];
+                       $network = $contact["network"];
+               } else {
+                       $cid = $def_contact["id"];
                        $network = NETWORK_DIASPORA;
                }
 
@@ -2208,7 +2230,10 @@ class Diaspora
                }
 
                logger('Received participation for ID: '.$item['id'].' - Contact: '.$contact_id.' - Server: '.$server, LOGGER_DEBUG);
-               dba::insert('participation', ['iid' => $item['id'], 'cid' => $contact_id, 'fid' => $person['id'], 'server' => $server]);
+
+               if (!dba::exists('participation', ['iid' => $item['id'], 'server' => $server])) {
+                       dba::insert('participation', ['iid' => $item['id'], 'cid' => $contact_id, 'fid' => $person['id'], 'server' => $server]);
+               }
 
                // Send all existing comments and likes to the requesting server
                $comments = dba::p("SELECT `item`.`id`, `item`.`verb`, `contact`.`self`
@@ -3193,13 +3218,14 @@ class Diaspora
                }
 
                $logid = random_string(4);
-               $dest_url = ($public_batch ? $contact["batch"] : $contact["notify"]);
 
-               // Fetch the fcontact entry when there is missing data
-               // Will possibly happen when data is transmitted to a DFRN contact
-               if (empty($dest_url) && !empty($contact['addr'])) {
+               // We always try to use the data from the fcontact table.
+               // This is important for transmitting data to Friendica servers.
+               if (!empty($contact['addr'])) {
                        $fcontact = self::personByHandle($contact['addr']);
                        $dest_url = ($public_batch ? $fcontact["batch"] : $fcontact["notify"]);
+               } else {
+                       $dest_url = ($public_batch ? $contact["batch"] : $contact["notify"]);
                }
 
                if (!$dest_url) {
@@ -3239,7 +3265,7 @@ class Diaspora
                        Contact::unmarkForArchival($contact);
                }
 
-               return(($return_code) ? $return_code : (-1));
+               return $return_code ? $return_code : -1;
        }
 
 
@@ -3593,10 +3619,18 @@ class Diaspora
                        $eventdata['description'] = html_entity_decode(bb2diaspora($event['desc']));
                }
                if ($event['location']) {
+                       $event['location'] = preg_replace("/\[map\](.*?)\[\/map\]/ism", '$1', $event['location']);
+                       $coord = Map::getCoordinates($event['location']);
+
                        $location = [];
                        $location["address"] = html_entity_decode(bb2diaspora($event['location']));
-                       $location["lat"] = 0;
-                       $location["lng"] = 0;
+                       if (!empty($coord['lat']) && !empty($coord['lon'])) {
+                               $location["lat"] = $coord['lat'];
+                               $location["lng"] = $coord['lon'];
+                       } else {
+                               $location["lat"] = 0;
+                               $location["lng"] = 0;
+                       }
                        $eventdata['location'] = $location;
                }
 
@@ -3690,7 +3724,13 @@ class Diaspora
                                if (count($event)) {
                                        $message['event'] = $event;
 
-                                       /// @todo Once Diaspora supports it, we will remove the body
+                                       if (!empty($event['location']['address']) &&
+                                               !empty($event['location']['lat']) &&
+                                               !empty($event['location']['lng'])) {
+                                               $message['location'] = $event['location'];
+                                       }
+
+                                       /// @todo Once Diaspora supports it, we will remove the body and the location hack above
                                        // $message['text'] = '';
                                }
                        }
@@ -4011,7 +4051,7 @@ class Diaspora
         */
        public static function sendRetraction($item, $owner, $contact, $public_batch = false, $relay = false)
        {
-               $itemaddr = self::handleFromContact($item["contact-id"], $item["gcontact-id"]);
+               $itemaddr = self::handleFromContact($item["contact-id"], $item["author-id"]);
 
                $msg_type = "retraction";
 
@@ -4200,7 +4240,7 @@ class Diaspora
                        }
 
                        $about = $profile['about'];
-                       $about = strip_tags(bbcode($about));
+                       $about = strip_tags(BBCode::convert($about));
 
                        $location = Profile::formatLocation($profile);
                        $tags = '';