]> 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 59ca2757f367ddfae9235bd954f11060304454a1..cc4b230831de489328c8d3ff6bc1b93b97726526 100644 (file)
@@ -9,30 +9,33 @@
  */
 namespace Friendica\Protocol;
 
-use Friendica\App;
-use Friendica\Core\System;
+use Friendica\Content\Text\BBCode;
 use Friendica\Core\Cache;
 use Friendica\Core\Config;
+use Friendica\Core\L10n;
 use Friendica\Core\PConfig;
+use Friendica\Core\System;
 use Friendica\Core\Worker;
 use Friendica\Database\DBM;
 use Friendica\Model\Contact;
 use Friendica\Model\GContact;
 use Friendica\Model\Group;
+use Friendica\Model\Item;
 use Friendica\Model\Profile;
+use Friendica\Model\Queue;
 use Friendica\Model\User;
 use Friendica\Network\Probe;
 use Friendica\Util\Crypto;
+use Friendica\Util\DateTimeFormat;
+use Friendica\Util\Network;
 use Friendica\Util\XML;
-
+use Friendica\Util\Map;
 use dba;
 use SimpleXMLElement;
 
 require_once 'include/dba.php';
 require_once 'include/items.php';
 require_once 'include/bb2diaspora.php';
-require_once 'include/datetime.php';
-require_once 'include/queue_fn.php';
 
 /**
  * @brief This class contain functions to create and send Diaspora XML files
@@ -51,10 +54,10 @@ class Diaspora
        {
                $serverdata = Config::get("system", "relay_server");
                if ($serverdata == "") {
-                       return array();
+                       return [];
                }
 
-               $relay = array();
+               $relay = [];
 
                $servers = explode(",", $serverdata);
 
@@ -74,7 +77,7 @@ class Diaspora
                                $r = q(
                                        "INSERT INTO `contact` (`uid`, `created`, `name`, `nick`, `addr`, `url`, `nurl`, `batch`, `network`, `rel`, `blocked`, `pending`, `writable`, `name-date`, `uri-date`, `avatar-date`)
                                        VALUES (0, '%s', '%s', 'relay', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 1, '%s', '%s', '%s')",
-                                       datetime_convert(),
+                                       DateTimeFormat::utcNow(),
                                        dbesc($addr),
                                        dbesc($addr),
                                        dbesc($server),
@@ -82,9 +85,9 @@ class Diaspora
                                        dbesc($batch),
                                        dbesc(NETWORK_DIASPORA),
                                        intval(CONTACT_IS_FOLLOWER),
-                                       dbesc(datetime_convert()),
-                                       dbesc(datetime_convert()),
-                                       dbesc(datetime_convert())
+                                       dbesc(DateTimeFormat::utcNow()),
+                                       dbesc(DateTimeFormat::utcNow()),
+                                       dbesc(DateTimeFormat::utcNow())
                                );
 
                                $relais = q("SELECT `batch`, `id`, `name`,`network` FROM `contact` WHERE `uid` = 0 AND `batch` = '%s' LIMIT 1", dbesc($batch));
@@ -99,6 +102,53 @@ class Diaspora
                return $relay;
        }
 
+       /**
+        * @brief Return a list of participating contacts for a thread
+        *
+        * This is used for the participation feature.
+        * One of the parameters is a contact array.
+        * This is done to avoid duplicates.
+        *
+        * @param integer $thread   The id of the thread
+        * @param array   $contacts The previously fetched contacts
+        *
+        * @return array of relay servers
+        */
+       public static function participantsForThread($thread, $contacts)
+       {
+               $r = dba::p("SELECT `contact`.`batch`, `contact`.`id`, `contact`.`name`, `contact`.`network`,
+                               `fcontact`.`batch` AS `fbatch`, `fcontact`.`network` AS `fnetwork` FROM `participation`
+                               INNER JOIN `contact` ON `contact`.`id` = `participation`.`cid`
+                               INNER JOIN `fcontact` ON `fcontact`.`id` = `participation`.`fid`
+                               WHERE `participation`.`iid` = ?", $thread);
+
+               while ($contact = dba::fetch($r)) {
+                       if (!empty($contact['fnetwork'])) {
+                               $contact['network'] = $contact['fnetwork'];
+                       }
+                       unset($contact['fnetwork']);
+
+                       if (empty($contact['batch']) && !empty($contact['fbatch'])) {
+                               $contact['batch'] = $contact['fbatch'];
+                       }
+                       unset($contact['fbatch']);
+
+                       $exists = false;
+                       foreach ($contacts as $entry) {
+                               if ($entry['batch'] == $contact['batch']) {
+                                       $exists = true;
+                               }
+                       }
+
+                       if (!$exists) {
+                               $contacts[] = $contact;
+                       }
+               }
+               dba::close($r);
+
+               return $contacts;
+       }
+
        /**
         * @brief repairs a signature that was double encoded
         *
@@ -138,7 +188,7 @@ class Diaspora
         */
        private static function verifyMagicEnvelope($envelope)
        {
-               $basedom = parse_xml_string($envelope);
+               $basedom = XML::parseString($envelope);
 
                if (!is_object($basedom)) {
                        logger("Envelope is no XML file");
@@ -168,15 +218,24 @@ class Diaspora
                }
 
                $b64url_data = base64url_encode($data);
-               $msg = str_replace(array("\n", "\r", " ", "\t"), array("", "", "", ""), $b64url_data);
+               $msg = str_replace(["\n", "\r", " ", "\t"], ["", "", "", ""], $b64url_data);
 
                $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;
                }
 
@@ -237,7 +296,7 @@ class Diaspora
 
                        if (!is_object($j_outer_key_bundle)) {
                                logger('Outer Salmon did not verify. Discarding.');
-                               http_status_exit(400);
+                               System::httpExit(400);
                        }
 
                        $outer_iv = base64_decode($j_outer_key_bundle->iv);
@@ -248,17 +307,17 @@ class Diaspora
                        $xml = $raw;
                }
 
-               $basedom = parse_xml_string($xml);
+               $basedom = XML::parseString($xml);
 
                if (!is_object($basedom)) {
                        logger('Received data does not seem to be an XML. Discarding. '.$xml);
-                       http_status_exit(400);
+                       System::httpExit(400);
                }
 
                $base = $basedom->children(NAMESPACE_SALMON_ME);
 
                // Not sure if this cleaning is needed
-               $data = str_replace(array(" ", "\t", "\r", "\n"), array("", "", "", ""), $base->data);
+               $data = str_replace([" ", "\t", "\r", "\n"], ["", "", "", ""], $base->data);
 
                // Build the signed data
                $type = $base->data[0]->attributes()->type[0];
@@ -272,17 +331,26 @@ 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) {
                        logger('Message did not verify. Discarding.');
-                       http_status_exit(400);
+                       System::httpExit(400);
                }
 
-               return array('message' => (string)base64url_decode($base->data),
+               return ['message' => (string)base64url_decode($base->data),
                                'author' => unxmlify($author_addr),
-                               'key' => (string)$key);
+                               'key' => (string)$key];
        }
 
        /**
@@ -299,7 +367,7 @@ class Diaspora
        public static function decode($importer, $xml)
        {
                $public = false;
-               $basedom = parse_xml_string($xml);
+               $basedom = XML::parseString($xml);
 
                if (!is_object($basedom)) {
                        logger("XML is not parseable.");
@@ -307,6 +375,9 @@ class Diaspora
                }
                $children = $basedom->children('https://joindiaspora.com/protocol');
 
+               $inner_aes_key = null;
+               $inner_iv = null;
+
                if ($children->header) {
                        $public = true;
                        $author_link = str_replace('acct:', '', $children->header->author_id);
@@ -333,7 +404,7 @@ class Diaspora
                        $decrypted = self::aesDecrypt($outer_key, $outer_iv, $ciphertext);
 
                        logger('decrypted: '.$decrypted, LOGGER_DEBUG);
-                       $idom = parse_xml_string($decrypted);
+                       $idom = XML::parseString($decrypted);
 
                        $inner_iv = base64_decode($idom->iv);
                        $inner_aes_key = base64_decode($idom->aes_key);
@@ -345,6 +416,7 @@ class Diaspora
 
                // figure out where in the DOM tree our data is hiding
 
+               $base = null;
                if ($dom->provenance->data) {
                        $base = $dom->provenance;
                } elseif ($dom->env->data) {
@@ -355,7 +427,7 @@ class Diaspora
 
                if (!$base) {
                        logger('unable to locate salmon data in xml');
-                       http_status_exit(400);
+                       System::httpExit(400);
                }
 
 
@@ -365,7 +437,7 @@ class Diaspora
                // unpack the  data
 
                // strip whitespace so our data element will return to one big base64 blob
-               $data = str_replace(array(" ", "\t", "\r", "\n"), array("", "", "", ""), $base->data);
+               $data = str_replace([" ", "\t", "\r", "\n"], ["", "", "", ""], $base->data);
 
 
                // stash away some other stuff for later
@@ -393,7 +465,7 @@ class Diaspora
 
                if (!$author_link) {
                        logger('Could not retrieve author URI.');
-                       http_status_exit(400);
+                       System::httpExit(400);
                }
                // Once we have the author URI, go to the web and try to find their public key
                // (first this will look it up locally if it is in the fcontact cache)
@@ -404,21 +476,21 @@ class Diaspora
 
                if (!$key) {
                        logger('Could not retrieve author key.');
-                       http_status_exit(400);
+                       System::httpExit(400);
                }
 
                $verify = Crypto::rsaVerify($signed_data, $signature, $key);
 
                if (!$verify) {
                        logger('Message did not verify. Discarding.');
-                       http_status_exit(400);
+                       System::httpExit(400);
                }
 
                logger('Message verified.');
 
-               return array('message' => (string)$inner_decrypted,
+               return ['message' => (string)$inner_decrypted,
                                'author' => unxmlify($author_link),
-                               'key' => (string)$key);
+                               'key' => (string)$key];
        }
 
 
@@ -457,11 +529,11 @@ class Diaspora
 
                // Process item retractions. This has to be done separated from the other stuff,
                // since retractions for comments could come even from non followers.
-               if (!empty($fields) && in_array($fields->getName(), array('retraction'))) {
+               if (!empty($fields) && in_array($fields->getName(), ['retraction'])) {
                        $target = notags(unxmlify($fields->target_type));
-                       if (in_array($target, array("Comment", "Like", "Post", "Reshare", "StatusMessage"))) {
+                       if (in_array($target, ["Comment", "Like", "Post", "Reshare", "StatusMessage"])) {
                                logger('processing retraction for '.$target, LOGGER_DEBUG);
-                               $importer = array("uid" => 0, "page-flags" => PAGE_FREELOVE);
+                               $importer = ["uid" => 0, "page-flags" => PAGE_FREELOVE];
                                $message_id = self::dispatch($importer, $msg, $fields);
                                return $message_id;
                        }
@@ -485,7 +557,7 @@ class Diaspora
                        logger("Unwanted message from ".$msg["author"]." send by ".$_SERVER["REMOTE_ADDR"]." with ".$_SERVER["HTTP_USER_AGENT"].": ".print_r($msg, true), LOGGER_DEBUG);
                } else {
                        // Use a dummy importer to import the data for the public copy
-                       $importer = array("uid" => 0, "page-flags" => PAGE_FREELOVE);
+                       $importer = ["uid" => 0, "page-flags" => PAGE_FREELOVE];
                        $message_id = self::dispatch($importer, $msg, $fields);
                }
 
@@ -542,7 +614,7 @@ class Diaspora
                        case "message":
                                return self::receiveMessage($importer, $fields);
 
-                       case "participation": // Not implemented
+                       case "participation":
                                return self::receiveParticipation($importer, $fields);
 
                        case "photo": // Not implemented
@@ -583,15 +655,13 @@ class Diaspora
         */
        private static function validPosting($msg)
        {
-               $data = parse_xml_string($msg["message"]);
+               $data = XML::parseString($msg["message"]);
 
                if (!is_object($data)) {
                        logger("No valid XML ".$msg["message"], LOGGER_DEBUG);
                        return false;
                }
 
-               $first_child = $data->getName();
-
                // Is this the new or the old version?
                if ($data->getName() == "XML") {
                        $oldXML = true;
@@ -610,7 +680,7 @@ class Diaspora
 
                // All retractions are handled identically from now on.
                // In the new version there will only be "retraction".
-               if (in_array($type, array("signed_retraction", "relayable_retraction")))
+               if (in_array($type, ["signed_retraction", "relayable_retraction"]))
                        $type = "retraction";
 
                if ($type == "request") {
@@ -620,6 +690,8 @@ class Diaspora
                $fields = new SimpleXMLElement("<".$type."/>");
 
                $signed_data = "";
+               $author_signature = null;
+               $parent_author_signature = null;
 
                foreach ($element->children() as $fieldname => $entry) {
                        if ($oldXML) {
@@ -630,7 +702,7 @@ class Diaspora
                                if ($fieldname == "participant_handles") {
                                        $fieldname = "participants";
                                }
-                               if (in_array($type, array("like", "participation"))) {
+                               if (in_array($type, ["like", "participation"])) {
                                        if ($fieldname == "target_type") {
                                                $fieldname = "parent_type";
                                        }
@@ -663,14 +735,14 @@ class Diaspora
                                $author_signature = base64_decode($entry);
                        } elseif (($fieldname == "parent_author_signature") && ($entry != "")) {
                                $parent_author_signature = base64_decode($entry);
-                       } elseif (!in_array($fieldname, array("author_signature", "parent_author_signature", "target_author_signature"))) {
+                       } elseif (!in_array($fieldname, ["author_signature", "parent_author_signature", "target_author_signature"])) {
                                if ($signed_data != "") {
                                        $signed_data .= ";";
                                }
 
                                $signed_data .= $entry;
                        }
-                       if (!in_array($fieldname, array("parent_author_signature", "target_author_signature"))
+                       if (!in_array($fieldname, ["parent_author_signature", "target_author_signature"])
                                || ($orig_type == "relayable_retraction")
                        ) {
                                XML::copy($entry, $fields, $fieldname);
@@ -678,7 +750,7 @@ class Diaspora
                }
 
                // This is something that shouldn't happen at all.
-               if (in_array($type, array("status_message", "reshare", "profile"))) {
+               if (in_array($type, ["status_message", "reshare", "profile"])) {
                        if ($msg["author"] != $fields->author) {
                                logger("Message handle is not the same as envelope sender. Quitting this message.");
                                return false;
@@ -686,8 +758,8 @@ class Diaspora
                }
 
                // Only some message types have signatures. So we quit here for the other types.
-               if (!in_array($type, array("comment", "like"))) {
-                       return array("fields" => $fields, "relayed" => false);
+               if (!in_array($type, ["comment", "like"])) {
+                       return ["fields" => $fields, "relayed" => false];
                }
                // No author_signature? This is a must, so we quit.
                if (!isset($author_signature)) {
@@ -714,7 +786,7 @@ class Diaspora
                        logger("No valid author signature for author ".$fields->author. " in type ".$type." - signed data: ".$signed_data." - Message: ".$msg["message"]." - Signature ".$author_signature, LOGGER_DEBUG);
                        return false;
                } else {
-                       return array("fields" => $fields, "relayed" => $relayed);
+                       return ["fields" => $fields, "relayed" => $relayed];
                }
        }
 
@@ -748,14 +820,11 @@ class Diaspora
         */
        public static function personByHandle($handle)
        {
-               $r = q(
-                       "SELECT * FROM `fcontact` WHERE `network` = '%s' AND `addr` = '%s' LIMIT 1",
-                       dbesc(NETWORK_DIASPORA),
-                       dbesc($handle)
-               );
-               if ($r) {
-                       $person = $r[0];
-                       logger("In cache " . print_r($r, true), LOGGER_DEBUG);
+               $update = false;
+
+               $person = dba::selectFirst('fcontact', [], ['network' => NETWORK_DIASPORA, 'addr' => $handle]);
+               if (DBM::is_result($person)) {
+                       logger("In cache " . print_r($person, true), LOGGER_DEBUG);
 
                        // update record occasionally so it doesn't get stale
                        $d = strtotime($person["updated"]." +00:00");
@@ -768,7 +837,7 @@ class Diaspora
                        }
                }
 
-               if (!$person || $update) {
+               if (!DBM::is_result($person) || $update) {
                        logger("create or refresh", LOGGER_DEBUG);
                        $r = Probe::uri($handle, NETWORK_DIASPORA);
 
@@ -776,9 +845,15 @@ 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;
+                               }
                        }
                }
+
                return $person;
        }
 
@@ -821,7 +896,7 @@ class Diaspora
                                dbesc($arr["confirm"]),
                                dbesc($arr["alias"]),
                                dbesc($arr["pubkey"]),
-                               dbesc(datetime_convert()),
+                               dbesc(DateTimeFormat::utcNow()),
                                dbesc($arr["url"]),
                                dbesc($arr["network"])
                        );
@@ -844,7 +919,7 @@ class Diaspora
                                dbesc($arr["network"]),
                                dbesc($arr["alias"]),
                                dbesc($arr["pubkey"]),
-                               dbesc(datetime_convert())
+                               dbesc(DateTimeFormat::utcNow())
                        );
                }
 
@@ -852,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)) {
@@ -1009,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?
@@ -1209,7 +1284,7 @@ class Diaspora
 
                logger("Fetch post from ".$source_url, LOGGER_DEBUG);
 
-               $envelope = fetch_url($source_url);
+               $envelope = Network::fetchUrl($source_url);
                if ($envelope) {
                        logger("Envelope was fetched.", LOGGER_DEBUG);
                        $x = self::verifyMagicEnvelope($envelope);
@@ -1227,13 +1302,13 @@ class Diaspora
                        $source_url = $server."/p/".urlencode($guid).".xml";
                        logger("Fetch post from ".$source_url, LOGGER_DEBUG);
 
-                       $x = fetch_url($source_url);
+                       $x = Network::fetchUrl($source_url);
                        if (!$x) {
                                return false;
                        }
                }
 
-               $source_xml = parse_xml_string($x);
+               $source_xml = XML::parseString($x);
 
                if (!is_object($source_xml)) {
                        return false;
@@ -1264,7 +1339,7 @@ class Diaspora
                        return false;
                }
 
-               $msg = array("message" => $x, "author" => $author);
+               $msg = ["message" => $x, "author" => $author];
 
                $msg["key"] = self::key($msg["author"]);
 
@@ -1326,30 +1401,27 @@ 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;
                }
 
-               return array("cid" => $cid, "network" => $network);
+               return ["cid" => $cid, "network" => $network];
        }
 
        /**
@@ -1448,30 +1520,30 @@ class Diaspora
                        return false;
                }
 
-               $fields = array('url' => $data['url'], 'nurl' => normalise_link($data['url']),
+               $fields = ['url' => $data['url'], 'nurl' => normalise_link($data['url']),
                                'name' => $data['name'], 'nick' => $data['nick'],
                                'addr' => $data['addr'], 'batch' => $data['batch'],
                                'notify' => $data['notify'], 'poll' => $data['poll'],
-                               'network' => $data['network']);
+                               'network' => $data['network']];
 
-               dba::update('contact', $fields, array('addr' => $old_handle));
+               dba::update('contact', $fields, ['addr' => $old_handle]);
 
-               $fields = array('url' => $data['url'], 'nurl' => normalise_link($data['url']),
+               $fields = ['url' => $data['url'], 'nurl' => normalise_link($data['url']),
                                'name' => $data['name'], 'nick' => $data['nick'],
                                'addr' => $data['addr'], 'connect' => $data['addr'],
                                'notify' => $data['notify'], 'photo' => $data['photo'],
-                               'server_url' => $data['baseurl'], 'network' => $data['network']);
+                               'server_url' => $data['baseurl'], 'network' => $data['network']];
 
-               dba::update('gcontact', $fields, array('addr' => $old_handle));
+               dba::update('gcontact', $fields, ['addr' => $old_handle]);
 
                logger('Contacts are updated.');
 
                // update items
                /// @todo This is an extreme performance killer
-               $fields = array(
-                       'owner-link' => array($contact["url"], $data["url"]),
-                       'author-link' => array($contact["url"], $data["url"]),
-               );
+               $fields = [
+                       'owner-link' => [$contact["url"], $data["url"]],
+                       'author-link' => [$contact["url"], $data["url"]],
+               ];
                foreach ($fields as $n => $f) {
                        $r = q(
                                "SELECT `id` FROM `item` WHERE `%s` = '%s' AND `uid` = %d LIMIT 1",
@@ -1604,9 +1676,9 @@ class Diaspora
                $text = unxmlify($data->text);
 
                if (isset($data->created_at)) {
-                       $created_at = datetime_convert("UTC", "UTC", notags(unxmlify($data->created_at)));
+                       $created_at = DateTimeFormat::utc(notags(unxmlify($data->created_at)));
                } else {
-                       $created_at = datetime_convert();
+                       $created_at = DateTimeFormat::utcNow();
                }
 
                if (isset($data->thread_parent_guid)) {
@@ -1640,7 +1712,7 @@ class Diaspora
                // Fetch the contact id - if we know this contact
                $author_contact = self::authorContactByUrl($contact, $person, $importer["uid"]);
 
-               $datarray = array();
+               $datarray = [];
 
                $datarray["uid"] = $importer["uid"];
                $datarray["contact-id"] = $author_contact["cid"];
@@ -1682,7 +1754,7 @@ class Diaspora
 
                self::fetchGuid($datarray);
 
-               $message_id = item_store($datarray);
+               $message_id = Item::insert($datarray);
 
                if ($message_id <= 0) {
                        return false;
@@ -1696,7 +1768,7 @@ class Diaspora
                if ($message_id && $parent_item["origin"]) {
                        // Formerly we stored the signed text, the signature and the author in different fields.
                        // We now store the raw data so that we are more flexible.
-                       dba::insert('sign', array('iid' => $message_id, 'signed_text' => json_encode($data)));
+                       dba::insert('sign', ['iid' => $message_id, 'signed_text' => json_encode($data)]);
 
                        // notify others
                        Worker::add(PRIORITY_HIGH, "Notifier", "comment-import", $message_id);
@@ -1736,7 +1808,7 @@ class Diaspora
                $msg_guid = notags(unxmlify($mesg->guid));
                $msg_conversation_guid = notags(unxmlify($mesg->conversation_guid));
                $msg_text = unxmlify($mesg->text);
-               $msg_created_at = datetime_convert("UTC", "UTC", notags(unxmlify($mesg->created_at)));
+               $msg_created_at = DateTimeFormat::utc(notags(unxmlify($mesg->created_at)));
 
                if ($msg_conversation_guid != $guid) {
                        logger("message conversation guid does not belong to the current conversation.");
@@ -1781,22 +1853,22 @@ class Diaspora
 
                dba::unlock();
 
-               dba::update('conv', array('updated' => datetime_convert()), array('id' => $conversation["id"]));
+               dba::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $conversation["id"]]);
 
                notification(
-                       array(
+                       [
                        "type" => NOTIFY_MAIL,
                        "notify_flags" => $importer["notify-flags"],
                        "language" => $importer["language"],
                        "to_name" => $importer["username"],
                        "to_email" => $importer["email"],
                        "uid" =>$importer["uid"],
-                       "item" => array("subject" => $subject, "body" => $body),
+                       "item" => ["subject" => $subject, "body" => $body],
                        "source_name" => $person["name"],
                        "source_link" => $person["url"],
                        "source_photo" => $person["thumb"],
                        "verb" => ACTIVITY_POST,
-                       "otype" => "mail")
+                       "otype" => "mail"]
                );
                return true;
        }
@@ -1815,7 +1887,7 @@ class Diaspora
                $author = notags(unxmlify($data->author));
                $guid = notags(unxmlify($data->guid));
                $subject = notags(unxmlify($data->subject));
-               $created_at = datetime_convert("UTC", "UTC", notags(unxmlify($data->created_at)));
+               $created_at = DateTimeFormat::utc(notags(unxmlify($data->created_at)));
                $participants = notags(unxmlify($data->participants));
 
                $messages = $data->message;
@@ -1847,7 +1919,7 @@ class Diaspora
                                dbesc($guid),
                                dbesc($author),
                                dbesc($created_at),
-                               dbesc(datetime_convert()),
+                               dbesc(DateTimeFormat::utcNow()),
                                dbesc($subject),
                                dbesc($participants)
                        );
@@ -1886,11 +1958,11 @@ class Diaspora
         */
        private static function constructLikeBody($contact, $parent_item, $guid)
        {
-               $bodyverb = t('%1$s likes %2$s\'s %3$s');
+               $bodyverb = L10n::t('%1$s likes %2$s\'s %3$s');
 
                $ulink = "[url=".$contact["url"]."]".$contact["name"]."[/url]";
                $alink = "[url=".$parent_item["author-link"]."]".$parent_item["author-name"]."[/url]";
-               $plink = "[url=".System::baseUrl()."/display/".urlencode($guid)."]".t("status")."[/url]";
+               $plink = "[url=".System::baseUrl()."/display/".urlencode($guid)."]".L10n::t("status")."[/url]";
 
                return sprintf($bodyverb, $ulink, $alink, $plink);
        }
@@ -1909,12 +1981,12 @@ class Diaspora
                $link = '<link rel="alternate" type="text/html" href="'.System::baseUrl()."/display/".$importer["nickname"]."/".$parent_item["id"].'" />';
                $parent_body = $parent_item["body"];
 
-               $xmldata = array("object" => array("type" => $objtype,
+               $xmldata = ["object" => ["type" => $objtype,
                                                "local" => "1",
                                                "id" => $parent_item["uri"],
                                                "link" => $link,
                                                "title" => "",
-                                               "content" => $parent_body));
+                                               "content" => $parent_body]];
 
                return XML::fromArray($xmldata, $xml, true);
        }
@@ -1938,7 +2010,7 @@ class Diaspora
 
                // likes on comments aren't supported by Diaspora - only on posts
                // But maybe this will be supported in the future, so we will accept it.
-               if (!in_array($parent_type, array("Post", "Comment"))) {
+               if (!in_array($parent_type, ["Post", "Comment"])) {
                        return false;
                }
 
@@ -1974,7 +2046,7 @@ class Diaspora
                        $verb = ACTIVITY_DISLIKE;
                }
 
-               $datarray = array();
+               $datarray = [];
 
                $datarray["protocol"] = PROTOCOL_DIASPORA;
 
@@ -2003,7 +2075,7 @@ class Diaspora
 
                $datarray["body"] = self::constructLikeBody($contact, $parent_item, $guid);
 
-               $message_id = item_store($datarray);
+               $message_id = Item::insert($datarray);
 
                if ($message_id <= 0) {
                        return false;
@@ -2015,7 +2087,7 @@ class Diaspora
 
                // like on comments have the comment as parent. So we need to fetch the toplevel parent
                if ($parent_item["id"] != $parent_item["parent"]) {
-                       $toplevel = dba::select('item', array('origin'), array('id' => $parent_item["parent"]), array('limit' => 1));
+                       $toplevel = dba::selectFirst('item', ['origin'], ['id' => $parent_item["parent"]]);
                        $origin = $toplevel["origin"];
                } else {
                        $origin = $parent_item["origin"];
@@ -2025,7 +2097,7 @@ class Diaspora
                if ($message_id && $origin) {
                        // Formerly we stored the signed text, the signature and the author in different fields.
                        // We now store the raw data so that we are more flexible.
-                       dba::insert('sign', array('iid' => $message_id, 'signed_text' => json_encode($data)));
+                       dba::insert('sign', ['iid' => $message_id, 'signed_text' => json_encode($data)]);
 
                        // notify others
                        Worker::add(PRIORITY_HIGH, "Notifier", "comment-import", $message_id);
@@ -2048,7 +2120,7 @@ class Diaspora
                $guid = notags(unxmlify($data->guid));
                $conversation_guid = notags(unxmlify($data->conversation_guid));
                $text = unxmlify($data->text);
-               $created_at = datetime_convert("UTC", "UTC", notags(unxmlify($data->created_at)));
+               $created_at = DateTimeFormat::utc(notags(unxmlify($data->created_at)));
 
                $contact = self::allowedContactByHandle($importer, $author, true);
                if (!$contact) {
@@ -2114,7 +2186,7 @@ class Diaspora
 
                dba::unlock();
 
-               dba::update('conv', array('updated' => datetime_convert()), array('id' => $conversation["id"]));
+               dba::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $conversation["id"]]);
                return true;
        }
 
@@ -2128,7 +2200,57 @@ class Diaspora
         */
        private static function receiveParticipation($importer, $data)
        {
-               // I'm not sure if we can fully support this message type
+               $author = strtolower(notags(unxmlify($data->author)));
+               $parent_guid = notags(unxmlify($data->parent_guid));
+
+               $contact_id = Contact::getIdForURL($author);
+               if (!$contact_id) {
+                       logger('Contact not found: '.$author);
+                       return false;
+               }
+
+               $person = self::personByHandle($author);
+               if (!is_array($person)) {
+                       logger("Person not found: ".$author);
+                       return false;
+               }
+
+               $item = dba::selectFirst('item', ['id'], ['guid' => $parent_guid, 'origin' => true, 'private' => false]);
+               if (!DBM::is_result($item)) {
+                       logger('Item not found, no origin or private: '.$parent_guid);
+                       return false;
+               }
+
+               $author_parts = explode('@', $author);
+               if (isset($author_parts[1])) {
+                       $server = $author_parts[1];
+               } else {
+                       // Should never happen
+                       $server = $author;
+               }
+
+               logger('Received participation for ID: '.$item['id'].' - Contact: '.$contact_id.' - Server: '.$server, LOGGER_DEBUG);
+
+               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`
+                               FROM `item`
+                               INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
+                               WHERE `item`.`parent` = ? AND `item`.`id` != `item`.`parent`", $item['id']);
+               while ($comment = dba::fetch($comments)) {
+                       if ($comment['verb'] == ACTIVITY_POST) {
+                               $cmd = $comment['self'] ? 'comment-new' : 'comment-import';
+                       } else {
+                               $cmd = $comment['self'] ? 'like' : 'comment-import';
+                       }
+                       logger("Send ".$cmd." for item ".$comment['id']." to contact ".$contact_id, LOGGER_DEBUG);
+                       Worker::add(PRIORITY_HIGH, 'Delivery', $cmd, $comment['id'], $contact_id);
+               }
+               dba::close($comments);
+
                return true;
        }
 
@@ -2190,7 +2312,7 @@ class Diaspora
 
                $tags = explode("#", $tags);
 
-               $keywords = array();
+               $keywords = [];
                foreach ($tags as $tag) {
                        $tag = trim(strtolower($tag));
                        if ($tag != "") {
@@ -2218,7 +2340,7 @@ class Diaspora
                $birthday = str_replace("1000", "1901", $birthday);
 
                if ($birthday != "") {
-                       $birthday = datetime_convert("UTC", "UTC", $birthday, "Y-m-d");
+                       $birthday = DateTimeFormat::utc($birthday, "Y-m-d");
                }
 
                // this is to prevent multiple birthday notifications in a single year
@@ -2234,7 +2356,7 @@ class Diaspora
                        dbesc($name),
                        dbesc($nick),
                        dbesc($author),
-                       dbesc(datetime_convert()),
+                       dbesc(DateTimeFormat::utcNow()),
                        dbesc($birthday),
                        dbesc($location),
                        dbesc($about),
@@ -2244,11 +2366,11 @@ class Diaspora
                        intval($importer["uid"])
                );
 
-               $gcontact = array("url" => $contact["url"], "network" => NETWORK_DIASPORA, "generation" => 2,
+               $gcontact = ["url" => $contact["url"], "network" => NETWORK_DIASPORA, "generation" => 2,
                                        "photo" => $image_url, "name" => $name, "location" => $location,
                                        "about" => $about, "birthday" => $birthday, "gender" => $gender,
                                        "addr" => $author, "nick" => $nick, "keywords" => $keywords,
-                                       "hide" => !$searchable, "nsfw" => $nsfw);
+                                       "hide" => !$searchable, "nsfw" => $nsfw];
 
                $gcid = GContact::update($gcontact);
 
@@ -2273,8 +2395,8 @@ class Diaspora
                if ($contact["rel"] == CONTACT_IS_SHARING) {
                        dba::update(
                                'contact',
-                               array('rel' => CONTACT_IS_FRIEND, 'writable' => true),
-                               array('id' => $contact["id"], 'uid' => $importer["uid"])
+                               ['rel' => CONTACT_IS_FRIEND, 'writable' => true],
+                               ['id' => $contact["id"], 'uid' => $importer["uid"]]
                        );
                }
                // send notification
@@ -2293,7 +2415,7 @@ class Diaspora
                        // they are not CONTACT_IS_FOLLOWER anymore but that's what we have in the array
 
                        if ($self && $contact["rel"] == CONTACT_IS_FOLLOWER) {
-                               $arr = array();
+                               $arr = [];
                                $arr["protocol"] = PROTOCOL_DIASPORA;
                                $arr["uri"] = $arr["parent-uri"] = item_new_uri($a->get_hostname(), $importer["uid"]);
                                $arr["uid"] = $importer["uid"];
@@ -2311,20 +2433,18 @@ class Diaspora
                                $A = "[url=".$self[0]["url"]."]".$self[0]["name"]."[/url]";
                                $B = "[url=".$contact["url"]."]".$contact["name"]."[/url]";
                                $BPhoto = "[url=".$contact["url"]."][img]".$contact["thumb"]."[/img][/url]";
-                               $arr["body"] = sprintf(t('%1$s is now friends with %2$s'), $A, $B)."\n\n\n".$BPhoto;
+                               $arr["body"] = L10n::t('%1$s is now friends with %2$s', $A, $B)."\n\n\n".$BPhoto;
 
                                $arr["object"] = self::constructNewFriendObject($contact);
 
-                               $arr["last-child"] = 1;
-
-                               $user = dba::select('user', ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'], ['uid' => $importer["uid"]], ['limit' => 1]);
+                               $user = dba::selectFirst('user', ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'], ['uid' => $importer["uid"]]);
 
                                $arr["allow_cid"] = $user["allow_cid"];
                                $arr["allow_gid"] = $user["allow_gid"];
                                $arr["deny_cid"]  = $user["deny_cid"];
                                $arr["deny_gid"]  = $user["deny_gid"];
 
-                               $i = item_store($arr);
+                               $i = Item::insert($arr);
                                if ($i) {
                                        Worker::add(PRIORITY_HIGH, "Notifier", "activity", $i);
                                }
@@ -2345,10 +2465,10 @@ class Diaspora
                $link = '<link rel="alternate" type="text/html" href="'.$contact["url"].'" />'."\n".
                        '<link rel="photo" type="image/jpeg" href="'.$contact["thumb"].'" />'."\n";
 
-               $xmldata = array("object" => array("type" => $objtype,
+               $xmldata = ["object" => ["type" => $objtype,
                                                "title" => $contact["name"],
                                                "id" => $contact["url"]."/".$contact["name"],
-                                               "link" => $link));
+                                               "link" => $link]];
 
                return XML::fromArray($xmldata, $xml, true);
        }
@@ -2398,7 +2518,7 @@ class Diaspora
 
                                // If we are now friends, we are sending a share message.
                                // Normally we needn't to do so, but the first message could have been vanished.
-                               if (in_array($contact["rel"], array(CONTACT_IS_FRIEND))) {
+                               if (in_array($contact["rel"], [CONTACT_IS_FRIEND])) {
                                        $u = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($importer["uid"]));
                                        if ($u) {
                                                logger("Sending share message to author ".$author." - Contact: ".$contact["id"]." - User: ".$importer["uid"], LOGGER_DEBUG);
@@ -2408,12 +2528,12 @@ class Diaspora
                                return true;
                        } else {
                                logger("Author ".$author." doesn't want to follow us anymore.", LOGGER_DEBUG);
-                               lose_follower($importer, $contact);
+                               Contact::removeFollower($importer, $contact);
                                return true;
                        }
                }
 
-               if (!$following && $sharing && in_array($importer["page-flags"], array(PAGE_SOAPBOX, PAGE_NORMAL))) {
+               if (!$following && $sharing && in_array($importer["page-flags"], [PAGE_SOAPBOX, PAGE_NORMAL])) {
                        logger("Author ".$author." wants to share with us - but doesn't want to listen. Request is ignored.", LOGGER_DEBUG);
                        return false;
                } elseif (!$following && !$sharing) {
@@ -2442,7 +2562,7 @@ class Diaspora
                        intval($importer["uid"]),
                        dbesc($ret["network"]),
                        dbesc($ret["addr"]),
-                       datetime_convert(),
+                       DateTimeFormat::utcNow(),
                        dbesc($ret["url"]),
                        dbesc(normalise_link($ret["url"])),
                        dbesc($batch),
@@ -2483,9 +2603,9 @@ class Diaspora
                                intval($contact_record["id"]),
                                0,
                                0,
-                               dbesc(t("Sharing notification from Diaspora network")),
+                               dbesc(L10n::t("Sharing notification from Diaspora network")),
                                dbesc($hash),
-                               dbesc(datetime_convert())
+                               dbesc(DateTimeFormat::utcNow())
                        );
                } else {
                        // automatic friend approval
@@ -2516,8 +2636,8 @@ class Diaspora
                                WHERE `id` = %d
                                ",
                                intval($new_relation),
-                               dbesc(datetime_convert()),
-                               dbesc(datetime_convert()),
+                               dbesc(DateTimeFormat::utcNow()),
+                               dbesc(DateTimeFormat::utcNow()),
                                intval($contact_record["id"])
                        );
 
@@ -2527,7 +2647,7 @@ class Diaspora
                                $ret = self::sendShare($u[0], $contact_record);
 
                                // Send the profile data, maybe it weren't transmitted before
-                               self::sendProfile($importer["uid"], array($contact_record));
+                               self::sendProfile($importer["uid"], [$contact_record]);
                        }
                }
 
@@ -2560,7 +2680,7 @@ class Diaspora
                        // Then refetch the content, if it is a reshare from a reshare.
                        // If it is a reshared post from another network then reformat to avoid display problems with two share elements
                        if (self::isReshare($r[0]["body"], true)) {
-                               $r = array();
+                               $r = [];
                        } elseif (self::isReshare($r[0]["body"], false) || strstr($r[0]["body"], "[share")) {
                                $r[0]["body"] = diaspora2bb(bb2diaspora($r[0]["body"]));
 
@@ -2621,7 +2741,7 @@ class Diaspora
        {
                $author = notags(unxmlify($data->author));
                $guid = notags(unxmlify($data->guid));
-               $created_at = datetime_convert("UTC", "UTC", notags(unxmlify($data->created_at)));
+               $created_at = DateTimeFormat::utc(notags(unxmlify($data->created_at)));
                $root_author = notags(unxmlify($data->root_author));
                $root_guid = notags(unxmlify($data->root_guid));
                /// @todo handle unprocessed property "provider_display_name"
@@ -2644,7 +2764,7 @@ class Diaspora
 
                $orig_url = System::baseUrl()."/display/".$original_item["guid"];
 
-               $datarray = array();
+               $datarray = [];
 
                $datarray["uid"] = $importer["uid"];
                $datarray["contact-id"] = $contact["id"];
@@ -2687,7 +2807,7 @@ class Diaspora
                $datarray["object-type"] = $original_item["object-type"];
 
                self::fetchGuid($datarray);
-               $message_id = item_store($datarray);
+               $message_id = Item::insert($datarray);
 
                self::sendParticipation($contact, $datarray);
 
@@ -2725,13 +2845,13 @@ class Diaspora
                }
 
                // Fetch items that are about to be deleted
-               $fields = array('uid', 'id', 'parent', 'parent-uri', 'author-link');
+               $fields = ['uid', 'id', 'parent', 'parent-uri', 'author-link'];
 
                // When we receive a public retraction, we delete every item that we find.
                if ($importer['uid'] == 0) {
-                       $condition = array("`guid` = ? AND NOT `file` LIKE '%%[%%' AND NOT `deleted`", $target_guid);
+                       $condition = ["`guid` = ? AND NOT `file` LIKE '%%[%%' AND NOT `deleted`", $target_guid];
                } else {
-                       $condition = array("`guid` = ? AND `uid` = ? AND NOT `file` LIKE '%%[%%' AND NOT `deleted`", $target_guid, $importer['uid']);
+                       $condition = ["`guid` = ? AND `uid` = ? AND NOT `file` LIKE '%%[%%' AND NOT `deleted`", $target_guid, $importer['uid']];
                }
                $r = dba::select('item', $fields, $condition);
                if (!DBM::is_result($r)) {
@@ -2741,7 +2861,7 @@ class Diaspora
 
                while ($item = dba::fetch($r)) {
                        // Fetch the parent item
-                       $parent = dba::select('item', array('author-link', 'origin'), array('id' => $item["parent"]), array('limit' => 1));
+                       $parent = dba::selectFirst('item', ['author-link', 'origin'], ['id' => $item["parent"]]);
 
                        // Only delete it if the parent author really fits
                        if (!link_compare($parent["author-link"], $contact["url"]) && !link_compare($item["author-link"], $contact["url"])) {
@@ -2749,23 +2869,7 @@ class Diaspora
                                continue;
                        }
 
-                       // Currently we don't have a central deletion function that we could use in this case.
-                       // The function "item_drop" doesn't work for that case
-                       dba::update(
-                               'item',
-                               array(
-                                       'deleted' => true,
-                                       'title' => '',
-                                       'body' => '',
-                                       'edited' => datetime_convert(),
-                                       'changed' => datetime_convert()),
-                               array('id' => $item["id"])
-                       );
-
-                       // Delete the thread - if it is a starting post and not a comment
-                       if ($target_type != 'Comment') {
-                               delete_thread($item["id"], $item["parent-uri"]);
-                       }
+                       Item::deleteById($item["id"]);
 
                        logger("Deleted target ".$target_guid." (".$item["id"].") from user ".$item["uid"]." parent: ".$item["parent"], LOGGER_DEBUG);
 
@@ -2793,7 +2897,7 @@ class Diaspora
                $target_type = notags(unxmlify($data->target_type));
 
                $contact = self::contactByHandle($importer["uid"], $sender);
-               if (!$contact && (in_array($target_type, array("Contact", "Person")))) {
+               if (!$contact && (in_array($target_type, ["Contact", "Person"]))) {
                        logger("cannot find contact for sender: ".$sender." and user ".$importer["uid"]);
                        return false;
                }
@@ -2835,7 +2939,7 @@ class Diaspora
        {
                $author = notags(unxmlify($data->author));
                $guid = notags(unxmlify($data->guid));
-               $created_at = datetime_convert("UTC", "UTC", notags(unxmlify($data->created_at)));
+               $created_at = DateTimeFormat::utc(notags(unxmlify($data->created_at)));
                $public = notags(unxmlify($data->public));
                $text = unxmlify($data->text);
                $provider_display_name = notags(unxmlify($data->provider_display_name));
@@ -2850,7 +2954,7 @@ class Diaspora
                        return true;
                }
 
-               $address = array();
+               $address = [];
                if ($data->location) {
                        foreach ($data->location->children() as $fieldname => $data) {
                                $address[$fieldname] = notags(unxmlify($data));
@@ -2859,7 +2963,7 @@ class Diaspora
 
                $body = diaspora2bb($text);
 
-               $datarray = array();
+               $datarray = [];
 
                // Attach embedded pictures to the body
                if ($data->photo) {
@@ -2927,7 +3031,7 @@ class Diaspora
                }
 
                self::fetchGuid($datarray);
-               $message_id = item_store($datarray);
+               $message_id = Item::insert($datarray);
 
                self::sendParticipation($contact, $datarray);
 
@@ -2996,14 +3100,14 @@ class Diaspora
 
                $ciphertext = self::aesEncrypt($aes_key, $iv, $msg);
 
-               $json = json_encode(array("iv" => $b_iv, "key" => $b_aes_key));
+               $json = json_encode(["iv" => $b_iv, "key" => $b_aes_key]);
 
                $encrypted_key_bundle = "";
                openssl_public_encrypt($json, $encrypted_key_bundle, $pubkey);
 
                $json_object = json_encode(
-                       array("aes_key" => base64_encode($encrypted_key_bundle),
-                                       "encrypted_magic_envelope" => base64_encode($ciphertext))
+                       ["aes_key" => base64_encode($encrypted_key_bundle),
+                                       "encrypted_magic_envelope" => base64_encode($ciphertext)]
                );
 
                return $json_object;
@@ -3020,7 +3124,7 @@ class Diaspora
        public static function buildMagicEnvelope($msg, $user)
        {
                $b64url_data = base64url_encode($msg);
-               $data = str_replace(array("\n", "\r", " ", "\t"), array("", "", "", ""), $b64url_data);
+               $data = str_replace(["\n", "\r", " ", "\t"], ["", "", "", ""], $b64url_data);
 
                $key_id = base64url_encode(self::myHandle($user));
                $type = "application/xml";
@@ -3036,14 +3140,14 @@ class Diaspora
                $signature = Crypto::rsaSign($signable_data, $user["uprvkey"]);
                $sig = base64url_encode($signature);
 
-               $xmldata = array("me:env" => array("me:data" => $data,
-                                                       "@attributes" => array("type" => $type),
+               $xmldata = ["me:env" => ["me:data" => $data,
+                                                       "@attributes" => ["type" => $type],
                                                        "me:encoding" => $encoding,
                                                        "me:alg" => $alg,
                                                        "me:sig" => $sig,
-                                                       "@attributes2" => array("key_id" => $key_id)));
+                                                       "@attributes2" => ["key_id" => $key_id]]];
 
-               $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
+               $namespaces = ["me" => "http://salmon-protocol.org/ns/magic-env"];
 
                return XML::fromArray($xmldata, $xml, false, $namespaces);
        }
@@ -3104,7 +3208,7 @@ class Diaspora
         *
         * @return int Result of the transmission
         */
-       public static function transmit($owner, $contact, $envelope, $public_batch, $queue_run = false, $guid = "")
+       public static function transmit($owner, $contact, $envelope, $public_batch, $queue_run = false, $guid = "", $no_queue = false)
        {
                $a = get_app();
 
@@ -3114,7 +3218,16 @@ class Diaspora
                }
 
                $logid = random_string(4);
-               $dest_url = (($public_batch) ? $contact["batch"] : $contact["notify"]);
+
+               // 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) {
                        logger("no url for contact: ".$contact["id"]." batch mode =".$public_batch);
                        return 0;
@@ -3122,13 +3235,13 @@ class Diaspora
 
                logger("transmit: ".$logid."-".$guid." ".$dest_url);
 
-               if (!$queue_run && was_recently_delayed($contact["id"])) {
+               if (!$queue_run && Queue::wasDelayed($contact["id"])) {
                        $return_code = 0;
                } else {
                        if (!intval(Config::get("system", "diaspora_test"))) {
                                $content_type = (($public_batch) ? "application/magic-envelope+xml" : "application/json");
 
-                               post_url($dest_url."/", $envelope, array("Content-Type: ".$content_type));
+                               Network::post($dest_url."/", $envelope, ["Content-Type: ".$content_type]);
                                $return_code = $a->get_curl_code();
                        } else {
                                logger("test_mode");
@@ -3139,30 +3252,20 @@ class Diaspora
                logger("transmit: ".$logid."-".$guid." returns: ".$return_code);
 
                if (!$return_code || (($return_code == 503) && (stristr($a->get_curl_headers(), "retry-after")))) {
-                       logger("queue message");
-
-                       $r = q(
-                               "SELECT `id` FROM `queue` WHERE `cid` = %d AND `network` = '%s' AND `content` = '%s' AND `batch` = %d LIMIT 1",
-                               intval($contact["id"]),
-                               dbesc(NETWORK_DIASPORA),
-                               dbesc($envelope),
-                               intval($public_batch)
-                       );
-                       if ($r) {
-                               logger("add_to_queue ignored - identical item already in queue");
-                       } else {
+                       if (!$no_queue) {
+                               logger("queue message");
                                // queue message for redelivery
-                               add_to_queue($contact["id"], NETWORK_DIASPORA, $envelope, $public_batch);
-
-                               // The message could not be delivered. We mark the contact as "dead"
-                               Contact::markForArchival($contact);
+                               Queue::add($contact["id"], NETWORK_DIASPORA, $envelope, $public_batch, $guid);
                        }
+
+                       // The message could not be delivered. We mark the contact as "dead"
+                       Contact::markForArchival($contact);
                } elseif (($return_code >= 200) && ($return_code <= 299)) {
                        // We successfully delivered a message, the contact is alive
                        Contact::unmarkForArchival($contact);
                }
 
-               return(($return_code) ? $return_code : (-1));
+               return $return_code ? $return_code : -1;
        }
 
 
@@ -3176,7 +3279,7 @@ class Diaspora
         */
        public static function buildPostXml($type, $message)
        {
-               $data = array($type => $message);
+               $data = [$type => $message];
 
                return XML::fromArray($data, $xml);
        }
@@ -3209,7 +3312,7 @@ class Diaspora
                $envelope = self::buildMessage($msg, $owner, $contact, $owner['uprvkey'], $contact['pubkey'], $public_batch);
 
                if ($spool) {
-                       add_to_queue($contact['id'], NETWORK_DIASPORA, $envelope, $public_batch);
+                       Queue::add($contact['id'], NETWORK_DIASPORA, $envelope, $public_batch, $guid);
                        return true;
                } else {
                        $return_code = self::transmit($owner, $contact, $envelope, $public_batch, false, $guid);
@@ -3247,7 +3350,7 @@ class Diaspora
                // If the item belongs to a user, we take this user id.
                if ($item['uid'] == 0) {
                        $condition = ['verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false];
-                       $first_user = dba::select('user', ['uid'], $condition, ['limit' => 1]);
+                       $first_user = dba::selectFirst('user', ['uid'], $condition);
                        $owner = User::getOwnerDataById($first_user['uid']);
                } else {
                        $owner = User::getOwnerDataById($item['uid']);
@@ -3255,10 +3358,10 @@ class Diaspora
 
                $author = self::myHandle($owner);
 
-               $message = array("author" => $author,
+               $message = ["author" => $author,
                                "guid" => get_guid(32),
                                "parent_type" => "Post",
-                               "parent_guid" => $item["guid"]);
+                               "parent_guid" => $item["guid"]];
 
                logger("Send participation for ".$item["guid"]." by ".$author, LOGGER_DEBUG);
 
@@ -3285,9 +3388,9 @@ class Diaspora
                $signed_text = 'AccountMigration:'.$old_handle.':'.$profile['author'];
                $signature = base64_encode(Crypto::rsaSign($signed_text, $owner["uprvkey"], "sha256"));
 
-               $message = array("author" => $old_handle,
+               $message = ["author" => $old_handle,
                                "profile" => $profile,
-                               "signature" => $signature);
+                               "signature" => $signature];
 
                logger("Send account migration ".print_r($message, true), LOGGER_DEBUG);
 
@@ -3325,10 +3428,10 @@ class Diaspora
                }
                */
 
-               $message = array("author" => self::myHandle($owner),
+               $message = ["author" => self::myHandle($owner),
                                "recipient" => $contact["addr"],
                                "following" => "true",
-                               "sharing" => "true");
+                               "sharing" => "true"];
 
                logger("Send share ".print_r($message, true), LOGGER_DEBUG);
 
@@ -3345,10 +3448,10 @@ class Diaspora
         */
        public static function sendUnshare($owner, $contact)
        {
-               $message = array("author" => self::myHandle($owner),
+               $message = ["author" => self::myHandle($owner),
                                "recipient" => $contact["addr"],
                                "following" => "false",
-                               "sharing" => "false");
+                               "sharing" => "false"];
 
                logger("Send unshare ".print_r($message, true), LOGGER_DEBUG);
 
@@ -3408,7 +3511,7 @@ class Diaspora
                                NETWORK_DIASPORA
                        );
                        if ($r) {
-                               $ret= array();
+                               $ret= [];
                                $ret["root_handle"] = self::handleFromContact($r[0]["contact-id"]);
                                $ret["root_guid"] = $guid;
                                return($ret);
@@ -3426,7 +3529,7 @@ class Diaspora
                        $profile = $matches[1];
                }
 
-               $ret= array();
+               $ret= [];
 
                $ret["root_handle"] = preg_replace("=https?://(.*)/u/(.*)=ism", "$2@$1", $profile);
                if (($ret["root_handle"] == $profile) || ($ret["root_handle"] == "")) {
@@ -3463,23 +3566,23 @@ class Diaspora
        {
                $r = q("SELECT `guid`, `uid`, `start`, `finish`, `nofinish`, `summary`, `desc`, `location`, `adjust` FROM `event` WHERE `id` = %d", intval($event_id));
                if (!DBM::is_result($r)) {
-                       return array();
+                       return [];
                }
 
                $event = $r[0];
 
-               $eventdata = array();
+               $eventdata = [];
 
                $r = q("SELECT `timezone` FROM `user` WHERE `uid` = %d", intval($event['uid']));
                if (!DBM::is_result($r)) {
-                       return array();
+                       return [];
                }
 
                $user = $r[0];
 
                $r = q("SELECT `addr`, `nick` FROM `contact` WHERE `uid` = %d AND `self`", intval($event['uid']));
                if (!DBM::is_result($r)) {
-                       return array();
+                       return [];
                }
 
                $owner = $r[0];
@@ -3490,7 +3593,7 @@ class Diaspora
                        $eventdata['guid'] = $event['guid'];
                }
 
-               $mask = 'Y-m-d\TH:i:s\Z';
+               $mask = DateTimeFormat::ATOM;
 
                /// @todo - establish "all day" events in Friendica
                $eventdata["all_day"] = "false";
@@ -3504,10 +3607,10 @@ class Diaspora
                }
 
                if ($event['start']) {
-                       $eventdata['start'] = datetime_convert($eventdata['timezone'], "UTC", $event['start'], $mask);
+                       $eventdata['start'] = DateTimeFormat::convert($event['start'], "UTC", $eventdata['timezone'], $mask);
                }
                if ($event['finish'] && !$event['nofinish']) {
-                       $eventdata['end'] = datetime_convert($eventdata['timezone'], "UTC", $event['finish'], $mask);
+                       $eventdata['end'] = DateTimeFormat::convert($event['finish'], "UTC", $eventdata['timezone'], $mask);
                }
                if ($event['summary']) {
                        $eventdata['summary'] = html_entity_decode(bb2diaspora($event['summary']));
@@ -3516,10 +3619,18 @@ class Diaspora
                        $eventdata['description'] = html_entity_decode(bb2diaspora($event['desc']));
                }
                if ($event['location']) {
-                       $location = array();
+                       $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;
                }
 
@@ -3549,17 +3660,17 @@ class Diaspora
 
                $public = (($item["private"]) ? "false" : "true");
 
-               $created = datetime_convert("UTC", "UTC", $item["created"], 'Y-m-d\TH:i:s\Z');
+               $created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM);
 
                // Detect a share element and do a reshare
                if (!$item['private'] && ($ret = self::isReshare($item["body"]))) {
-                       $message = array("author" => $myaddr,
+                       $message = ["author" => $myaddr,
                                        "guid" => $item["guid"],
                                        "created_at" => $created,
                                        "root_author" => $ret["root_handle"],
                                        "root_guid" => $ret["root_guid"],
                                        "provider_display_name" => $item["app"],
-                                       "public" => $public);
+                                       "public" => $public];
 
                        $type = "reshare";
                } else {
@@ -3577,14 +3688,14 @@ class Diaspora
                        if ($item["attach"]) {
                                $cnt = preg_match_all('/href=\"(.*?)\"(.*?)title=\"(.*?)\"/ism', $item["attach"], $matches, PREG_SET_ORDER);
                                if (cnt) {
-                                       $body .= "\n".t("Attachments:")."\n";
+                                       $body .= "\n".L10n::t("Attachments:")."\n";
                                        foreach ($matches as $mtch) {
                                                $body .= "[".$mtch[3]."](".$mtch[1].")\n";
                                        }
                                }
                        }
 
-                       $location = array();
+                       $location = [];
 
                        if ($item["location"] != "")
                                $location["address"] = $item["location"];
@@ -3595,13 +3706,13 @@ class Diaspora
                                $location["lng"] = $coord[1];
                        }
 
-                       $message = array("author" => $myaddr,
+                       $message = ["author" => $myaddr,
                                        "guid" => $item["guid"],
                                        "created_at" => $created,
                                        "public" => $public,
                                        "text" => $body,
                                        "provider_display_name" => $item["app"],
-                                       "location" => $location);
+                                       "location" => $location];
 
                        // Diaspora rejects messages when they contain a location without "lat" or "lng"
                        if (!isset($location["lat"]) || !isset($location["lng"])) {
@@ -3613,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'] = '';
                                }
                        }
@@ -3621,7 +3738,7 @@ class Diaspora
                        $type = "status_message";
                }
 
-               $msg = array("type" => $type, "message" => $message);
+               $msg = ["type" => $type, "message" => $message];
 
                Cache::set($cachekey, $msg, CACHE_QUARTER_HOUR);
 
@@ -3666,18 +3783,19 @@ class Diaspora
                $parent = $p[0];
 
                $target_type = ($parent["uri"] === $parent["parent-uri"] ? "Post" : "Comment");
+               $positive = null;
                if ($item['verb'] === ACTIVITY_LIKE) {
                        $positive = "true";
                } elseif ($item['verb'] === ACTIVITY_DISLIKE) {
                        $positive = "false";
                }
 
-               return(array("author" => self::myHandle($owner),
+               return(["author" => self::myHandle($owner),
                                "guid" => $item["guid"],
                                "parent_guid" => $parent["guid"],
                                "parent_type" => $target_type,
                                "positive" => $positive,
-                               "author_signature" => ""));
+                               "author_signature" => ""]);
        }
 
        /**
@@ -3715,11 +3833,11 @@ class Diaspora
                                return false;
                }
 
-               return(array("author" => self::myHandle($owner),
+               return(["author" => self::myHandle($owner),
                                "guid" => $item["guid"],
                                "parent_guid" => $parent["guid"],
                                "status" => $attend_answer,
-                               "author_signature" => ""));
+                               "author_signature" => ""]);
        }
 
        /**
@@ -3752,14 +3870,14 @@ class Diaspora
                $parent = $p[0];
 
                $text = html_entity_decode(bb2diaspora($item["body"]));
-               $created = datetime_convert("UTC", "UTC", $item["created"], 'Y-m-d\TH:i:s\Z');
+               $created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM);
 
-               $comment = array("author" => self::myHandle($owner),
+               $comment = ["author" => self::myHandle($owner),
                                "guid" => $item["guid"],
                                "created_at" => $created,
                                "parent_guid" => $parent["guid"],
                                "text" => $text,
-                               "author_signature" => "");
+                               "author_signature" => ""];
 
                // Send the thread parent guid only if it is a threaded comment
                if ($item['thr-parent'] != $item['parent-uri']) {
@@ -3783,10 +3901,10 @@ class Diaspora
         */
        public static function sendFollowup($item, $owner, $contact, $public_batch = false)
        {
-               if (in_array($item['verb'], array(ACTIVITY_ATTEND, ACTIVITY_ATTENDNO, ACTIVITY_ATTENDMAYBE))) {
+               if (in_array($item['verb'], [ACTIVITY_ATTEND, ACTIVITY_ATTENDNO, ACTIVITY_ATTENDMAYBE])) {
                        $message = self::constructAttend($item, $owner);
                        $type = "event_participation";
-               } elseif (in_array($item["verb"], array(ACTIVITY_LIKE, ACTIVITY_DISLIKE))) {
+               } elseif (in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
                        $message = self::constructLike($item, $owner);
                        $type = "like";
                } else {
@@ -3817,17 +3935,17 @@ class Diaspora
                $signed_parts = explode(";", $signature['signed_text']);
 
                if ($item["deleted"]) {
-                       $message = array("author" => $signature['signer'],
+                       $message = ["author" => $signature['signer'],
                                        "target_guid" => $signed_parts[0],
-                                       "target_type" => $signed_parts[1]);
-               } elseif (in_array($item["verb"], array(ACTIVITY_LIKE, ACTIVITY_DISLIKE))) {
-                       $message = array("author" => $signed_parts[4],
+                                       "target_type" => $signed_parts[1]];
+               } elseif (in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
+                       $message = ["author" => $signed_parts[4],
                                        "guid" => $signed_parts[1],
                                        "parent_guid" => $signed_parts[3],
                                        "parent_type" => $signed_parts[2],
                                        "positive" => $signed_parts[0],
                                        "author_signature" => $signature['signature'],
-                                       "parent_author_signature" => "");
+                                       "parent_author_signature" => ""];
                } else {
                        // Remove the comment guid
                        $guid = array_shift($signed_parts);
@@ -3841,12 +3959,12 @@ class Diaspora
                        // Glue the parts together
                        $text = implode(";", $signed_parts);
 
-                       $message = array("author" => $handle,
+                       $message = ["author" => $handle,
                                        "guid" => $guid,
                                        "parent_guid" => $parent_guid,
                                        "text" => implode(";", $signed_parts),
                                        "author_signature" => $signature['signature'],
-                                       "parent_author_signature" => "");
+                                       "parent_author_signature" => ""];
                }
                return $message;
        }
@@ -3865,7 +3983,7 @@ class Diaspora
        {
                if ($item["deleted"]) {
                        return self::sendRetraction($item, $owner, $contact, $public_batch, true);
-               } elseif (in_array($item["verb"], array(ACTIVITY_LIKE, ACTIVITY_DISLIKE))) {
+               } elseif (in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
                        $type = "like";
                } else {
                        $type = "comment";
@@ -3894,7 +4012,7 @@ class Diaspora
                } else {// New way
                        $msg = json_decode($signature['signed_text'], true);
 
-                       $message = array();
+                       $message = [];
                        if (is_array($msg)) {
                                foreach ($msg as $field => $data) {
                                        if (!$item["deleted"]) {
@@ -3933,21 +4051,21 @@ 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";
 
                if ($item['id'] == $item['parent']) {
                        $target_type = "Post";
-               } elseif (in_array($item["verb"], array(ACTIVITY_LIKE, ACTIVITY_DISLIKE))) {
+               } elseif (in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
                        $target_type = "Like";
                } else {
                        $target_type = "Comment";
                }
 
-               $message = array("author" => $itemaddr,
+               $message = ["author" => $itemaddr,
                                "target_guid" => $item['guid'],
-                               "target_type" => $target_type);
+                               "target_type" => $target_type];
 
                logger("Got message ".print_r($message, true), LOGGER_DEBUG);
 
@@ -3979,36 +4097,36 @@ class Diaspora
                }
                $cnv = $r[0];
 
-               $conv = array(
+               $conv = [
                        "author" => $cnv["creator"],
                        "guid" => $cnv["guid"],
                        "subject" => $cnv["subject"],
-                       "created_at" => datetime_convert("UTC", "UTC", $cnv['created'], 'Y-m-d\TH:i:s\Z'),
+                       "created_at" => DateTimeFormat::utc($cnv['created'], DateTimeFormat::ATOM),
                        "participants" => $cnv["recips"]
-               );
+               ];
 
                $body = bb2diaspora($item["body"]);
-               $created = datetime_convert("UTC", "UTC", $item["created"], 'Y-m-d\TH:i:s\Z');
+               $created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM);
 
-               $msg = array(
+               $msg = [
                        "author" => $myaddr,
                        "guid" => $item["guid"],
                        "conversation_guid" => $cnv["guid"],
                        "text" => $body,
                        "created_at" => $created,
-               );
+               ];
 
                if ($item["reply"]) {
                        $message = $msg;
                        $type = "message";
                } else {
-                       $message = array(
+                       $message = [
                                        "author" => $cnv["creator"],
                                        "guid" => $cnv["guid"],
                                        "subject" => $cnv["subject"],
-                                       "created_at" => datetime_convert("UTC", "UTC", $cnv['created'], 'Y-m-d\TH:i:s\Z'),
+                                       "created_at" => DateTimeFormat::utc($cnv['created'], DateTimeFormat::ATOM),
                                        "participants" => $cnv["recips"],
-                                       "message" => $msg);
+                                       "message" => $msg];
 
                        $type = "conversation";
                }
@@ -4091,7 +4209,7 @@ class Diaspora
                );
 
                if (!$r) {
-                       return array();
+                       return [];
                }
 
                $profile = $r[0];
@@ -4106,15 +4224,23 @@ class Diaspora
                $small = System::baseUrl().'/photo/custom/50/'  .$profile['uid'].'.jpg';
                $searchable = (($profile['publish'] && $profile['net-publish']) ? 'true' : 'false');
 
+               $dob = null;
+               $about = null;
+               $location = null;
+               $tags = null;
                if ($searchable === 'true') {
-                       $dob = '1000-00-00';
+                       $dob = '';
 
-                       if (($profile['dob']) && ($profile['dob'] > '0001-01-01')) {
-                               $dob = ((intval($profile['dob'])) ? intval($profile['dob']) : '1000') .'-'. datetime_convert('UTC', 'UTC', $profile['dob'],'m-d');
+                       if ($profile['dob'] && ($profile['dob'] > '0000-00-00')) {
+                               list($year, $month, $day) = sscanf($profile['dob'], '%4d-%2d-%2d');
+                               if ($year < 1004) {
+                                       $year = 1004;
+                               }
+                               $dob = DateTimeFormat::utc($year . '-' . $month . '-'. $day, 'Y-m-d');
                        }
 
                        $about = $profile['about'];
-                       $about = strip_tags(bbcode($about));
+                       $about = strip_tags(BBCode::convert($about));
 
                        $location = Profile::formatLocation($profile);
                        $tags = '';
@@ -4133,7 +4259,7 @@ class Diaspora
                        $tags = trim($tags);
                }
 
-               return array("author" => $handle,
+               return ["author" => $handle,
                                "first_name" => $first,
                                "last_name" => $last,
                                "image_url" => $large,
@@ -4145,7 +4271,7 @@ class Diaspora
                                "location" => $location,
                                "searchable" => $searchable,
                                "nsfw" => "false",
-                               "tag_string" => $tags);
+                               "tag_string" => $tags];
        }
 
        /**
@@ -4184,7 +4310,7 @@ class Diaspora
 
                foreach ($recips as $recip) {
                        logger("Send updated profile data for user ".$uid." to contact ".$recip["id"], LOGGER_DEBUG);
-                       self::buildAndTransmit($owner, $recip, "profile", $message, false, "", true);
+                       self::buildAndTransmit($owner, $recip, "profile", $message, false, "", false);
                }
        }
 
@@ -4216,18 +4342,22 @@ class Diaspora
                        return false;
                }
 
-               if (!in_array($r[0]["verb"], array(ACTIVITY_LIKE, ACTIVITY_DISLIKE))) {
+               if (!in_array($r[0]["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
                        return false;
                }
 
                $message = self::constructLike($r[0], $contact);
+               if ($message === false) {
+                       return false;
+               }
+
                $message["author_signature"] = self::signature($contact, $message);
 
                /*
                 * Now store the signature more flexible to dynamically support new fields.
                 * This will break Diaspora compatibility with Friendica versions prior to 3.5.
                 */
-               dba::insert('sign', array('iid' => $post_id, 'signed_text' => json_encode($message)));
+               dba::insert('sign', ['iid' => $post_id, 'signed_text' => json_encode($message)]);
 
                logger('Stored diaspora like signature');
                return true;
@@ -4253,13 +4383,17 @@ class Diaspora
                $contact["uprvkey"] = $uprvkey;
 
                $message = self::constructComment($item, $contact);
+               if ($message === false) {
+                       return false;
+               }
+
                $message["author_signature"] = self::signature($contact, $message);
 
                /*
                 * Now store the signature more flexible to dynamically support new fields.
                 * This will break Diaspora compatibility with Friendica versions prior to 3.5.
                 */
-               dba::insert('sign', array('iid' => $message_id, 'signed_text' => json_encode($message)));
+               dba::insert('sign', ['iid' => $message_id, 'signed_text' => json_encode($message)]);
 
                logger('Stored diaspora comment signature');
                return true;