]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Diaspora.php
rename rawContent() to content()
[friendica.git] / src / Protocol / Diaspora.php
index 5036aa357b7fc999d02ada9fb2ff26b972d3bf00..41ed7e7d9f79b9db0f3d29ae37d24066b27012b8 100644 (file)
 
 namespace Friendica\Protocol;
 
+use Friendica\Content\Feature;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\Markdown;
 use Friendica\Core\Cache;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
+use Friendica\Core\Logger;
 use Friendica\Core\PConfig;
+use Friendica\Core\Protocol;
 use Friendica\Core\System;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
@@ -24,20 +27,18 @@ use Friendica\Model\Conversation;
 use Friendica\Model\GContact;
 use Friendica\Model\Group;
 use Friendica\Model\Item;
+use Friendica\Model\Mail;
 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\Map;
 use Friendica\Util\Network;
+use Friendica\Util\Strings;
 use Friendica\Util\XML;
 use SimpleXMLElement;
 
-require_once 'include/dba.php';
-require_once 'include/items.php';
-
 /**
  * @brief This class contain functions to create and send Diaspora XML files
  *
@@ -53,6 +54,7 @@ class Diaspora
         * @param array   $contacts The previously fetched contacts
         *
         * @return array of relay servers
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function relayList($item_id, array $contacts = [])
        {
@@ -110,7 +112,7 @@ class Diaspora
                // Now we are collecting all relay contacts
                foreach ($serverlist as $server_url) {
                        // We don't send messages to ourselves
-                       if (link_compare($server_url, System::baseUrl())) {
+                       if (Strings::compareLink($server_url, System::baseUrl())) {
                                continue;
                        }
                        $contact = self::getRelayContact($server_url);
@@ -138,14 +140,15 @@ class Diaspora
         *
         * @param string $server_url The url of the server
         * @return array with the contact
+        * @throws \Exception
         */
        private static function getRelayContact($server_url)
        {
                $fields = ['batch', 'id', 'name', 'network', 'archive', 'blocked'];
 
                // Fetch the relay contact
-               $condition = ['uid' => 0, 'nurl' => normalise_link($server_url),
-                       'contact-type' => Contact::ACCOUNT_TYPE_RELAY];
+               $condition = ['uid' => 0, 'nurl' => Strings::normaliseLink($server_url),
+                       'contact-type' => Contact::TYPE_RELAY];
                $contact = DBA::selectFirst('contact', $fields, $condition);
 
                if (DBA::isResult($contact)) {
@@ -169,22 +172,23 @@ class Diaspora
        /**
         * @brief Update or insert a relay contact
         *
-        * @param string $server_url The url of the server
-        * @param array $network_fields Optional network specific fields
+        * @param string $server_url     The url of the server
+        * @param array  $network_fields Optional network specific fields
+        * @throws \Exception
         */
        public static function setRelayContact($server_url, array $network_fields = [])
        {
                $fields = ['created' => DateTimeFormat::utcNow(),
                        'name' => 'relay', 'nick' => 'relay',
-                       'url' => $server_url, 'network' => NETWORK_DIASPORA,
+                       'url' => $server_url, 'network' => Protocol::DIASPORA,
                        'batch' => $server_url . '/receive/public',
                        'rel' => Contact::FOLLOWER, 'blocked' => false,
                        'pending' => false, 'writable' => true];
 
                $fields = array_merge($fields, $network_fields);
 
-               $condition = ['uid' => 0, 'nurl' => normalise_link($server_url),
-                       'contact-type' => Contact::ACCOUNT_TYPE_RELAY];
+               $condition = ['uid' => 0, 'nurl' => Strings::normaliseLink($server_url),
+                       'contact-type' => Contact::TYPE_RELAY];
 
                if (DBA::exists('contact', $condition)) {
                        unset($fields['created']);
@@ -204,10 +208,11 @@ class Diaspora
         * @param array   $contacts The previously fetched contacts
         *
         * @return array of relay servers
+        * @throws \Exception
         */
        public static function participantsForThread($thread, array $contacts)
        {
-               $r = DBA::p("SELECT `contact`.`batch`, `contact`.`id`, `contact`.`name`, `contact`.`network`,
+               $r = DBA::p("SELECT `contact`.`batch`, `contact`.`id`, `contact`.`name`, `contact`.`network`, `contact`.`protocol`,
                                `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`
@@ -219,6 +224,10 @@ class Diaspora
                        }
                        unset($contact['fnetwork']);
 
+                       if (empty($contact['protocol'])) {
+                               $contact['protocol'] = $contact['network'];
+                       }
+
                        if (empty($contact['batch']) && !empty($contact['fbatch'])) {
                                $contact['batch'] = $contact['fbatch'];
                        }
@@ -250,6 +259,7 @@ class Diaspora
         * @param integer $level     This value is only set inside this function to avoid endless loops
         *
         * @return string the repaired signature
+        * @throws \Exception
         */
        private static function repairSignature($signature, $handle = "", $level = 1)
        {
@@ -259,7 +269,7 @@ class Diaspora
 
                if (base64_encode(base64_decode(base64_decode($signature))) == base64_decode($signature)) {
                        $signature = base64_decode($signature);
-                       logger("Repaired double encoded signature from Diaspora/Hubzilla handle ".$handle." - level ".$level, LOGGER_DEBUG);
+                       Logger::log("Repaired double encoded signature from Diaspora/Hubzilla handle ".$handle." - level ".$level, Logger::DEBUG);
 
                        // Do a recursive call to be able to fix even multiple levels
                        if ($level < 10) {
@@ -276,57 +286,59 @@ class Diaspora
         * @param string $envelope The magic envelope
         *
         * @return string verified data
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        private static function verifyMagicEnvelope($envelope)
        {
                $basedom = XML::parseString($envelope);
 
                if (!is_object($basedom)) {
-                       logger("Envelope is no XML file");
+                       Logger::log("Envelope is no XML file");
                        return false;
                }
 
                $children = $basedom->children('http://salmon-protocol.org/ns/magic-env');
 
                if (sizeof($children) == 0) {
-                       logger("XML has no children");
+                       Logger::log("XML has no children");
                        return false;
                }
 
                $handle = "";
 
-               $data = base64url_decode($children->data);
+               $data = Strings::base64UrlDecode($children->data);
                $type = $children->data->attributes()->type[0];
 
                $encoding = $children->encoding;
 
                $alg = $children->alg;
 
-               $sig = base64url_decode($children->sig);
+               $sig = Strings::base64UrlDecode($children->sig);
                $key_id = $children->sig->attributes()->key_id[0];
                if ($key_id != "") {
-                       $handle = base64url_decode($key_id);
+                       $handle = Strings::base64UrlDecode($key_id);
                }
 
-               $b64url_data = base64url_encode($data);
+               $b64url_data = Strings::base64UrlEncode($data);
                $msg = str_replace(["\n", "\r", " ", "\t"], ["", "", "", ""], $b64url_data);
 
-               $signable_data = $msg.".".base64url_encode($type).".".base64url_encode($encoding).".".base64url_encode($alg);
+               $signable_data = $msg.".".Strings::base64UrlEncode($type).".".Strings::base64UrlEncode($encoding).".".Strings::base64UrlEncode($alg);
 
                if ($handle == '') {
-                       logger('No author could be decoded. Discarding. Message: ' . $envelope);
+                       Logger::log('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.");
+                       Logger::log("Couldn't get a key for handle " . $handle . ". Discarding.");
                        return false;
                }
 
                $verify = Crypto::rsaVerify($signable_data, $sig, $key);
                if (!$verify) {
-                       logger('Message from ' . $handle . ' did not verify. Discarding.');
+                       Logger::log('Message from ' . $handle . ' did not verify. Discarding.');
                        return false;
                }
 
@@ -364,15 +376,18 @@ class Diaspora
        /**
         * @brief: Decodes incoming Diaspora message in the new format
         *
-        * @param array  $importer Array of the importer user
-        * @param string $raw      raw post message
+        * @param array   $importer Array of the importer user
+        * @param string  $raw      raw post message
+        * @param boolean $no_exit  Don't do an http exit on error
         *
         * @return array
         * 'message' -> decoded Diaspora XML message
         * 'author' -> author diaspora handle
         * 'key' -> author public key (converted to pkcs#8)
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
-       public static function decodeRaw(array $importer, $raw)
+       public static function decodeRaw(array $importer, $raw, $no_exit = false)
        {
                $data = json_decode($raw);
 
@@ -386,8 +401,12 @@ class Diaspora
                        $j_outer_key_bundle = json_decode($outer_key_bundle);
 
                        if (!is_object($j_outer_key_bundle)) {
-                               logger('Outer Salmon did not verify. Discarding.');
-                               System::httpExit(400);
+                               Logger::log('Outer Salmon did not verify. Discarding.');
+                               if ($no_exit) {
+                                       return false;
+                               } else {
+                                       throw new \Friendica\Network\HTTPException\BadRequestException();
+                               }
                        }
 
                        $outer_iv = base64_decode($j_outer_key_bundle->iv);
@@ -401,8 +420,12 @@ class Diaspora
                $basedom = XML::parseString($xml);
 
                if (!is_object($basedom)) {
-                       logger('Received data does not seem to be an XML. Discarding. '.$xml);
-                       System::httpExit(400);
+                       Logger::log('Received data does not seem to be an XML. Discarding. '.$xml);
+                       if ($no_exit) {
+                               return false;
+                       } else {
+                               throw new \Friendica\Network\HTTPException\BadRequestException();
+                       }
                }
 
                $base = $basedom->children(NAMESPACE_SALMON_ME);
@@ -414,33 +437,45 @@ class Diaspora
                $type = $base->data[0]->attributes()->type[0];
                $encoding = $base->encoding;
                $alg = $base->alg;
-               $signed_data = $data.'.'.base64url_encode($type).'.'.base64url_encode($encoding).'.'.base64url_encode($alg);
+               $signed_data = $data.'.'.Strings::base64UrlEncode($type).'.'.Strings::base64UrlEncode($encoding).'.'.Strings::base64UrlEncode($alg);
 
                // This is the signature
-               $signature = base64url_decode($base->sig);
+               $signature = Strings::base64UrlDecode($base->sig);
 
                // 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);
+                       Logger::log('No author could be decoded. Discarding. Message: ' . $xml);
+                       if ($no_exit) {
+                               return false;
+                       } else {
+                               throw new \Friendica\Network\HTTPException\BadRequestException();
+                       }
                }
 
                $key = self::key($author_addr);
                if ($key == '') {
-                       logger("Couldn't get a key for handle " . $author_addr . ". Discarding.");
-                       System::httpExit(400);
+                       Logger::log("Couldn't get a key for handle " . $author_addr . ". Discarding.");
+                       if ($no_exit) {
+                               return false;
+                       } else {
+                               throw new \Friendica\Network\HTTPException\BadRequestException();
+                       }
                }
 
                $verify = Crypto::rsaVerify($signed_data, $signature, $key);
                if (!$verify) {
-                       logger('Message did not verify. Discarding.');
-                       System::httpExit(400);
+                       Logger::log('Message did not verify. Discarding.');
+                       if ($no_exit) {
+                               return false;
+                       } else {
+                               throw new \Friendica\Network\HTTPException\BadRequestException();
+                       }
                }
 
-               return ['message' => (string)base64url_decode($base->data),
-                               'author' => unxmlify($author_addr),
+               return ['message' => (string)Strings::base64UrlDecode($base->data),
+                               'author' => XML::unescape($author_addr),
                                'key' => (string)$key];
        }
 
@@ -454,6 +489,8 @@ class Diaspora
         * 'message' -> decoded Diaspora XML message
         * 'author' -> author diaspora handle
         * 'key' -> author public key (converted to pkcs#8)
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        public static function decode(array $importer, $xml)
        {
@@ -461,7 +498,7 @@ class Diaspora
                $basedom = XML::parseString($xml);
 
                if (!is_object($basedom)) {
-                       logger("XML is not parseable.");
+                       Logger::log("XML is not parseable.");
                        return false;
                }
                $children = $basedom->children('https://joindiaspora.com/protocol');
@@ -475,7 +512,7 @@ class Diaspora
                } else {
                        // This happens with posts from a relais
                        if (!$importer) {
-                               logger("This is no private post in the old format", LOGGER_DEBUG);
+                               Logger::log("This is no private post in the old format", Logger::DEBUG);
                                return false;
                        }
 
@@ -494,7 +531,7 @@ class Diaspora
 
                        $decrypted = self::aesDecrypt($outer_key, $outer_iv, $ciphertext);
 
-                       logger('decrypted: '.$decrypted, LOGGER_DEBUG);
+                       Logger::log('decrypted: '.$decrypted, Logger::DEBUG);
                        $idom = XML::parseString($decrypted);
 
                        $inner_iv = base64_decode($idom->iv);
@@ -517,13 +554,13 @@ class Diaspora
                }
 
                if (!$base) {
-                       logger('unable to locate salmon data in xml');
-                       System::httpExit(400);
+                       Logger::log('unable to locate salmon data in xml');
+                       throw new \Friendica\Network\HTTPException\BadRequestException();
                }
 
 
                // Stash the signature away for now. We have to find their key or it won't be good for anything.
-               $signature = base64url_decode($base->sig);
+               $signature = Strings::base64UrlDecode($base->sig);
 
                // unpack the  data
 
@@ -539,11 +576,11 @@ class Diaspora
                $alg = $base->alg;
 
 
-               $signed_data = $data.'.'.base64url_encode($type).'.'.base64url_encode($encoding).'.'.base64url_encode($alg);
+               $signed_data = $data.'.'.Strings::base64UrlEncode($type).'.'.Strings::base64UrlEncode($encoding).'.'.Strings::base64UrlEncode($alg);
 
 
                // decode the data
-               $data = base64url_decode($data);
+               $data = Strings::base64UrlDecode($data);
 
 
                if ($public) {
@@ -555,32 +592,32 @@ class Diaspora
                }
 
                if (!$author_link) {
-                       logger('Could not retrieve author URI.');
-                       System::httpExit(400);
+                       Logger::log('Could not retrieve author URI.');
+                       throw new \Friendica\Network\HTTPException\BadRequestException();
                }
                // 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)
                // This will also convert diaspora public key from pkcs#1 to pkcs#8
 
-               logger('Fetching key for '.$author_link);
+               Logger::log('Fetching key for '.$author_link);
                $key = self::key($author_link);
 
                if (!$key) {
-                       logger('Could not retrieve author key.');
-                       System::httpExit(400);
+                       Logger::log('Could not retrieve author key.');
+                       throw new \Friendica\Network\HTTPException\BadRequestException();
                }
 
                $verify = Crypto::rsaVerify($signed_data, $signature, $key);
 
                if (!$verify) {
-                       logger('Message did not verify. Discarding.');
-                       System::httpExit(400);
+                       Logger::log('Message did not verify. Discarding.');
+                       throw new \Friendica\Network\HTTPException\BadRequestException();
                }
 
-               logger('Message verified.');
+               Logger::log('Message verified.');
 
                return ['message' => (string)$inner_decrypted,
-                               'author' => unxmlify($author_link),
+                               'author' => XML::unescape($author_link),
                                'key' => (string)$key];
        }
 
@@ -591,21 +628,23 @@ class Diaspora
         * @param array $msg The post that will be dispatched
         *
         * @return int The message id of the generated message, "true" or "false" if there was an error
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        public static function dispatchPublic($msg)
        {
                $enabled = intval(Config::get("system", "diaspora_enabled"));
                if (!$enabled) {
-                       logger("diaspora is disabled");
+                       Logger::log("diaspora is disabled");
                        return false;
                }
 
                if (!($fields = self::validPosting($msg))) {
-                       logger("Invalid posting");
+                       Logger::log("Invalid posting");
                        return false;
                }
 
-               $importer = ["uid" => 0, "page-flags" => Contact::PAGE_FREELOVE];
+               $importer = ["uid" => 0, "page-flags" => User::PAGE_FLAGS_FREELOVE];
                $success = self::dispatch($importer, $msg, $fields);
 
                return $success;
@@ -614,13 +653,15 @@ class Diaspora
        /**
         * @brief Dispatches the different message types to the different functions
         *
-        * @param array  $importer Array of the importer user
-        * @param array  $msg      The post that will be dispatched
-        * @param object $fields   SimpleXML object that contains the message
+        * @param array            $importer Array of the importer user
+        * @param array            $msg      The post that will be dispatched
+        * @param SimpleXMLElement $fields   SimpleXML object that contains the message
         *
         * @return int The message id of the generated message, "true" or "false" if there was an error
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
-       public static function dispatch(array $importer, $msg, $fields = null)
+       public static function dispatch(array $importer, $msg, SimpleXMLElement $fields = null)
        {
                // The sender is the handle of the contact that sent the message.
                // This will often be different with relayed messages (for example "like" and "comment")
@@ -630,7 +671,7 @@ class Diaspora
                if (is_null($fields)) {
                        $private = true;
                        if (!($fields = self::validPosting($msg))) {
-                               logger("Invalid posting");
+                               Logger::log("Invalid posting");
                                return false;
                        }
                } else {
@@ -639,12 +680,12 @@ class Diaspora
 
                $type = $fields->getName();
 
-               logger("Received message type ".$type." from ".$sender." for user ".$importer["uid"], LOGGER_DEBUG);
+               Logger::log("Received message type ".$type." from ".$sender." for user ".$importer["uid"], Logger::DEBUG);
 
                switch ($type) {
                        case "account_migration":
                                if (!$private) {
-                                       logger('Message with type ' . $type . ' is not private, quitting.');
+                                       Logger::log('Message with type ' . $type . ' is not private, quitting.');
                                        return false;
                                }
                                return self::receiveAccountMigration($importer, $fields);
@@ -657,14 +698,14 @@ class Diaspora
 
                        case "contact":
                                if (!$private) {
-                                       logger('Message with type ' . $type . ' is not private, quitting.');
+                                       Logger::log('Message with type ' . $type . ' is not private, quitting.');
                                        return false;
                                }
                                return self::receiveContactRequest($importer, $fields);
 
                        case "conversation":
                                if (!$private) {
-                                       logger('Message with type ' . $type . ' is not private, quitting.');
+                                       Logger::log('Message with type ' . $type . ' is not private, quitting.');
                                        return false;
                                }
                                return self::receiveConversation($importer, $msg, $fields);
@@ -674,14 +715,14 @@ class Diaspora
 
                        case "message":
                                if (!$private) {
-                                       logger('Message with type ' . $type . ' is not private, quitting.');
+                                       Logger::log('Message with type ' . $type . ' is not private, quitting.');
                                        return false;
                                }
                                return self::receiveMessage($importer, $fields);
 
                        case "participation":
                                if (!$private) {
-                                       logger('Message with type ' . $type . ' is not private, quitting.');
+                                       Logger::log('Message with type ' . $type . ' is not private, quitting.');
                                        return false;
                                }
                                return self::receiveParticipation($importer, $fields);
@@ -694,7 +735,7 @@ class Diaspora
 
                        case "profile":
                                if (!$private) {
-                                       logger('Message with type ' . $type . ' is not private, quitting.');
+                                       Logger::log('Message with type ' . $type . ' is not private, quitting.');
                                        return false;
                                }
                                return self::receiveProfile($importer, $fields);
@@ -709,11 +750,9 @@ class Diaspora
                                return self::receiveStatusMessage($importer, $fields, $msg["message"]);
 
                        default:
-                               logger("Unknown message type ".$type);
+                               Logger::log("Unknown message type ".$type);
                                return false;
                }
-
-               return true;
        }
 
        /**
@@ -724,14 +763,16 @@ class Diaspora
         *
         * @param array $msg Array with the XML, the sender handle and the sender signature
         *
-        * @return bool|array If the posting is valid then an array with an SimpleXML object is returned
+        * @return bool|SimpleXMLElement If the posting is valid then an array with an SimpleXML object is returned
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        private static function validPosting($msg)
        {
                $data = XML::parseString($msg["message"]);
 
                if (!is_object($data)) {
-                       logger("No valid XML ".$msg["message"], LOGGER_DEBUG);
+                       Logger::log("No valid XML ".$msg["message"], Logger::DEBUG);
                        return false;
                }
 
@@ -749,7 +790,7 @@ class Diaspora
                $type = $element->getName();
                $orig_type = $type;
 
-               logger("Got message type ".$type.": ".$msg["message"], LOGGER_DATA);
+               Logger::log("Got message type ".$type.": ".$msg["message"], Logger::DATA);
 
                // All retractions are handled identically from now on.
                // In the new version there will only be "retraction".
@@ -825,7 +866,7 @@ class Diaspora
                // This is something that shouldn't happen at all.
                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.");
+                               Logger::log("Message handle is not the same as envelope sender. Quitting this message.");
                                return false;
                        }
                }
@@ -836,31 +877,31 @@ class Diaspora
                }
                // No author_signature? This is a must, so we quit.
                if (!isset($author_signature)) {
-                       logger("No author signature for type ".$type." - Message: ".$msg["message"], LOGGER_DEBUG);
+                       Logger::log("No author signature for type ".$type." - Message: ".$msg["message"], Logger::DEBUG);
                        return false;
                }
 
                if (isset($parent_author_signature)) {
                        $key = self::key($msg["author"]);
                        if (empty($key)) {
-                               logger("No key found for parent author ".$msg["author"], LOGGER_DEBUG);
+                               Logger::log("No key found for parent author ".$msg["author"], Logger::DEBUG);
                                return false;
                        }
 
                        if (!Crypto::rsaVerify($signed_data, $parent_author_signature, $key, "sha256")) {
-                               logger("No valid parent author signature for parent author ".$msg["author"]. " in type ".$type." - signed data: ".$signed_data." - Message: ".$msg["message"]." - Signature ".$parent_author_signature, LOGGER_DEBUG);
+                               Logger::log("No valid parent author signature for parent author ".$msg["author"]. " in type ".$type." - signed data: ".$signed_data." - Message: ".$msg["message"]." - Signature ".$parent_author_signature, Logger::DEBUG);
                                return false;
                        }
                }
 
                $key = self::key($fields->author);
                if (empty($key)) {
-                       logger("No key found for author ".$fields->author, LOGGER_DEBUG);
+                       Logger::log("No key found for author ".$fields->author, Logger::DEBUG);
                        return false;
                }
 
                if (!Crypto::rsaVerify($signed_data, $author_signature, $key, "sha256")) {
-                       logger("No valid author signature for author ".$fields->author. " in type ".$type." - signed data: ".$signed_data." - Message: ".$msg["message"]." - Signature ".$author_signature, LOGGER_DEBUG);
+                       Logger::log("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 $fields;
@@ -873,12 +914,14 @@ class Diaspora
         * @param string $handle The handle
         *
         * @return string The public key
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        private static function key($handle)
        {
                $handle = strval($handle);
 
-               logger("Fetching diaspora key for: ".$handle);
+               Logger::log("Fetching diaspora key for: ".$handle);
 
                $r = self::personByHandle($handle);
                if ($r) {
@@ -894,14 +937,16 @@ class Diaspora
         * @param string $handle The handle
         *
         * @return array the queried data
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        public static function personByHandle($handle)
        {
                $update = false;
 
-               $person = DBA::selectFirst('fcontact', [], ['network' => NETWORK_DIASPORA, 'addr' => $handle]);
+               $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]);
                if (DBA::isResult($person)) {
-                       logger("In cache " . print_r($person, true), LOGGER_DEBUG);
+                       Logger::debug("In cache " . print_r($person, true));
 
                        // update record occasionally so it doesn't get stale
                        $d = strtotime($person["updated"]." +00:00");
@@ -915,18 +960,19 @@ class Diaspora
                }
 
                if (!DBA::isResult($person) || $update) {
-                       logger("create or refresh", LOGGER_DEBUG);
-                       $r = Probe::uri($handle, NETWORK_DIASPORA);
+                       Logger::log("create or refresh", Logger::DEBUG);
+                       $r = Probe::uri($handle, Protocol::DIASPORA);
 
                        // Note that Friendica contacts will return a "Diaspora person"
                        // if Diaspora connectivity is enabled on their server
-                       if ($r && ($r["network"] === NETWORK_DIASPORA)) {
+                       if ($r && ($r["network"] === Protocol::DIASPORA)) {
                                self::updateFContact($r);
 
                                // Fetch the updated or added contact
-                               $person = DBA::selectFirst('fcontact', [], ['network' => NETWORK_DIASPORA, 'addr' => $handle]);
+                               $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]);
                                if (!DBA::isResult($person)) {
                                        $person = $r;
+                                       $person['id'] = 0;
                                }
                        }
                }
@@ -938,6 +984,7 @@ class Diaspora
         * @brief Updates the fcontact table
         *
         * @param array $arr The fcontact data
+        * @throws \Exception
         */
        private static function updateFContact($arr)
        {
@@ -961,21 +1008,19 @@ class Diaspora
         * @param int $pcontact_id The id in the contact table (Used for the public contact)
         *
         * @return string the handle
+        * @throws \Exception
         */
        private static function handleFromContact($contact_id, $pcontact_id = 0)
        {
                $handle = false;
 
-               logger("contact id is ".$contact_id." - pcontact id is ".$pcontact_id, LOGGER_DEBUG);
+               Logger::log("contact id is ".$contact_id." - pcontact id is ".$pcontact_id, Logger::DEBUG);
 
                if ($pcontact_id != 0) {
-                       $r = q(
-                               "SELECT `addr` FROM `contact` WHERE `id` = %d AND `addr` != ''",
-                               intval($pcontact_id)
-                       );
+                       $contact = DBA::selectFirst('contact', ['addr'], ['id' => $pcontact_id]);
 
-                       if (DBA::isResult($r)) {
-                               return strtolower($r[0]["addr"]);
+                       if (DBA::isResult($contact) && !empty($contact["addr"])) {
+                               return strtolower($contact["addr"]);
                        }
                }
 
@@ -987,7 +1032,7 @@ class Diaspora
                if (DBA::isResult($r)) {
                        $contact = $r[0];
 
-                       logger("contact 'self' = ".$contact['self']." 'url' = ".$contact['url'], LOGGER_DEBUG);
+                       Logger::log("contact 'self' = ".$contact['self']." 'url' = ".$contact['url'], Logger::DEBUG);
 
                        if ($contact['addr'] != "") {
                                $handle = $contact['addr'];
@@ -1010,14 +1055,15 @@ class Diaspora
         * @param mixed $fcontact_guid Hexadecimal string guid
         *
         * @return string the contact url or null
+        * @throws \Exception
         */
        public static function urlFromContactGuid($fcontact_guid)
        {
-               logger("fcontact guid is ".$fcontact_guid, LOGGER_DEBUG);
+               Logger::log("fcontact guid is ".$fcontact_guid, Logger::DEBUG);
 
                $r = q(
                        "SELECT `url` FROM `fcontact` WHERE `url` != '' AND `network` = '%s' AND `guid` = '%s'",
-                       DBA::escape(NETWORK_DIASPORA),
+                       DBA::escape(Protocol::DIASPORA),
                        DBA::escape($fcontact_guid)
                );
 
@@ -1031,12 +1077,14 @@ class Diaspora
        /**
         * @brief Get a contact id for a given handle
         *
-        * @todo Move to Friendica\Model\Contact
+        * @todo  Move to Friendica\Model\Contact
         *
         * @param int    $uid    The user id
         * @param string $handle The handle in the format user@domain.tld
         *
-        * @return int Contact id
+        * @return array Contact data
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        private static function contactByHandle($uid, $handle)
        {
@@ -1048,14 +1096,14 @@ class Diaspora
                }
 
                if (!$cid) {
-                       logger("Haven't found a contact for user " . $uid . " and handle " . $handle, LOGGER_DEBUG);
+                       Logger::log("Haven't found a contact for user " . $uid . " and handle " . $handle, Logger::DEBUG);
                        return false;
                }
 
-               $contact = dba::selectFirst('contact', [], ['id' => $cid]);
+               $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
                if (!DBA::isResult($contact)) {
                        // This here shouldn't happen at all
-                       logger("Haven't found a contact for user " . $uid . " and handle " . $handle, LOGGER_DEBUG);
+                       Logger::log("Haven't found a contact for user " . $uid . " and handle " . $handle, Logger::DEBUG);
                        return false;
                }
 
@@ -1080,19 +1128,22 @@ class Diaspora
                 */
                // It is deactivated by now, due to side effects. See issue https://github.com/friendica/friendica/pull/4033
                // It is not removed by now. Possibly the code is needed?
-               //if (!$is_comment && $contact["rel"] == Contact::FOLLOWER && in_array($importer["page-flags"], array(Contact::PAGE_FREELOVE))) {
-               //      dba::update(
+               //if (!$is_comment && $contact["rel"] == Contact::FOLLOWER && in_array($importer["page-flags"], array(User::PAGE_FLAGS_FREELOVE))) {
+               //      DBA::update(
                //              'contact',
                //              array('rel' => Contact::FRIEND, 'writable' => true),
                //              array('id' => $contact["id"], 'uid' => $contact["uid"])
                //      );
                //
                //      $contact["rel"] = Contact::FRIEND;
-               //      logger("defining user ".$contact["nick"]." as friend");
+               //      Logger::log("defining user ".$contact["nick"]." as friend");
                //}
 
-               // We don't seem to like that person
-               if ($contact["blocked"]) {
+               // Contact server is blocked
+               if (Network::isUrlBlocked($contact['url'])) {
+                       return false;
+                       // We don't seem to like that person
+               } elseif ($contact["blocked"]) {
                        // Maybe blocked, don't accept.
                        return false;
                        // We are following this person?
@@ -1100,7 +1151,7 @@ class Diaspora
                        // Yes, then it is fine.
                        return true;
                        // Is it a post to a community?
-               } elseif (($contact["rel"] == Contact::FOLLOWER) && in_array($importer["page-flags"], [Contact::PAGE_COMMUNITY, Contact::PAGE_PRVGROUP])) {
+               } elseif (($contact["rel"] == Contact::FOLLOWER) && in_array($importer["page-flags"], [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP])) {
                        // That's good
                        return true;
                        // Is the message a global user or a comment?
@@ -1120,12 +1171,13 @@ class Diaspora
         * @param bool   $is_comment Is the check for a comment?
         *
         * @return array The contact data
+        * @throws \Exception
         */
        private static function allowedContactByHandle(array $importer, $handle, $is_comment = false)
        {
                $contact = self::contactByHandle($importer["uid"], $handle);
                if (!$contact) {
-                       logger("A Contact for handle ".$handle." and user ".$importer["uid"]." was not found");
+                       Logger::log("A Contact for handle ".$handle." and user ".$importer["uid"]." was not found");
                        // If a contact isn't found, we accept it anyway if it is a comment
                        if ($is_comment && ($importer["uid"] != 0)) {
                                return self::contactByHandle(0, $handle);
@@ -1137,7 +1189,7 @@ class Diaspora
                }
 
                if (!self::postAllow($importer, $contact, $is_comment)) {
-                       logger("The handle: ".$handle." is not allowed to post to user ".$importer["uid"]);
+                       Logger::log("The handle: ".$handle." is not allowed to post to user ".$importer["uid"]);
                        return false;
                }
                return $contact;
@@ -1150,12 +1202,13 @@ class Diaspora
         * @param string $guid The guid of the message
         *
         * @return int|bool message id if the message already was stored into the system - or false.
+        * @throws \Exception
         */
        private static function messageExists($uid, $guid)
        {
                $item = Item::selectFirst(['id'], ['uid' => $uid, 'guid' => $guid]);
                if (DBA::isResult($item)) {
-                       logger("message ".$guid." already exists for user ".$uid);
+                       Logger::log("message ".$guid." already exists for user ".$uid);
                        return $item["id"];
                }
 
@@ -1180,7 +1233,7 @@ class Diaspora
                );
 
                preg_replace_callback(
-                       "&\[url=/posts/([^\[\]]*)\](.*)\[\/url\]&Usi",
+                       "&\[url=/?posts/([^\[\]]*)\](.*)\[\/url\]&Usi",
                        function ($match) use ($item) {
                                self::fetchGuidSub($match, $item);
                        },
@@ -1230,6 +1283,8 @@ class Diaspora
         * @param array $match array containing a link that has to be checked for a message link
         * @param array $item  The item array
         * @return void
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        private static function fetchGuidSub($match, $item)
        {
@@ -1246,6 +1301,8 @@ class Diaspora
         * @param int    $uid    The user id of the user
         *
         * @return int the message id of the stored message or false
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        private static function storeByGuid($guid, $server, $uid = 0)
        {
@@ -1257,7 +1314,7 @@ class Diaspora
 
                $server = $serverparts["scheme"]."://".$serverparts["host"];
 
-               logger("Trying to fetch item ".$guid." from ".$server, LOGGER_DEBUG);
+               Logger::log("Trying to fetch item ".$guid." from ".$server, Logger::DEBUG);
 
                $msg = self::message($guid, $server);
 
@@ -1265,7 +1322,7 @@ class Diaspora
                        return false;
                }
 
-               logger("Successfully fetched item ".$guid." from ".$server, LOGGER_DEBUG);
+               Logger::log("Successfully fetched item ".$guid." from ".$server, Logger::DEBUG);
 
                // Now call the dispatcher
                return self::dispatchPublic($msg);
@@ -1282,6 +1339,7 @@ class Diaspora
         *      'message' => The message XML
         *      'author' => The author handle
         *      'key' => The public key of the author
+        * @throws \Exception
         */
        private static function message($guid, $server, $level = 0)
        {
@@ -1292,30 +1350,23 @@ class Diaspora
                // This will work for new Diaspora servers and Friendica servers from 3.5
                $source_url = $server."/fetch/post/".urlencode($guid);
 
-               logger("Fetch post from ".$source_url, LOGGER_DEBUG);
+               Logger::log("Fetch post from ".$source_url, Logger::DEBUG);
 
                $envelope = Network::fetchUrl($source_url);
                if ($envelope) {
-                       logger("Envelope was fetched.", LOGGER_DEBUG);
+                       Logger::log("Envelope was fetched.", Logger::DEBUG);
                        $x = self::verifyMagicEnvelope($envelope);
                        if (!$x) {
-                               logger("Envelope could not be verified.", LOGGER_DEBUG);
+                               Logger::log("Envelope could not be verified.", Logger::DEBUG);
                        } else {
-                               logger("Envelope was verified.", LOGGER_DEBUG);
+                               Logger::log("Envelope was verified.", Logger::DEBUG);
                        }
                } else {
                        $x = false;
                }
 
-               // This will work for older Diaspora and Friendica servers
                if (!$x) {
-                       $source_url = $server."/p/".urlencode($guid).".xml";
-                       logger("Fetch post from ".$source_url, LOGGER_DEBUG);
-
-                       $x = Network::fetchUrl($source_url);
-                       if (!$x) {
-                               return false;
-                       }
+                       return false;
                }
 
                $source_xml = XML::parseString($x);
@@ -1326,11 +1377,11 @@ class Diaspora
 
                if ($source_xml->post->reshare) {
                        // Reshare of a reshare - old Diaspora version
-                       logger("Message is a reshare", LOGGER_DEBUG);
+                       Logger::log("Message is a reshare", Logger::DEBUG);
                        return self::message($source_xml->post->reshare->root_guid, $server, ++$level);
                } elseif ($source_xml->getName() == "reshare") {
                        // Reshare of a reshare - new Diaspora version
-                       logger("Message is a new reshare", LOGGER_DEBUG);
+                       Logger::log("Message is a new reshare", Logger::DEBUG);
                        return self::message($source_xml->root_guid, $server, ++$level);
                }
 
@@ -1345,7 +1396,7 @@ class Diaspora
 
                // If this isn't a "status_message" then quit
                if (!$author) {
-                       logger("Message doesn't seem to be a status message", LOGGER_DEBUG);
+                       Logger::log("Message doesn't seem to be a status message", Logger::DEBUG);
                        return false;
                }
 
@@ -1365,6 +1416,7 @@ class Diaspora
         * @param array  $contact The contact of the item owner
         *
         * @return array the item record
+        * @throws \Exception
         */
        private static function parentItem($uid, $guid, $author, array $contact)
        {
@@ -1384,17 +1436,17 @@ class Diaspora
                        }
 
                        if ($result) {
-                               logger("Fetched missing item ".$guid." - result: ".$result, LOGGER_DEBUG);
+                               Logger::log("Fetched missing item ".$guid." - result: ".$result, Logger::DEBUG);
 
                                $item = Item::selectFirst($fields, $condition);
                        }
                }
 
                if (!DBA::isResult($item)) {
-                       logger("parent item not found: parent: ".$guid." - user: ".$uid);
+                       Logger::log("parent item not found: parent: ".$guid." - user: ".$uid);
                        return false;
                } else {
-                       logger("parent item found: parent: ".$guid." - user: ".$uid);
+                       Logger::log("parent item found: parent: ".$guid." - user: ".$uid);
                        return $item;
                }
        }
@@ -1409,17 +1461,18 @@ class Diaspora
         * @return array
         *      'cid' => contact id
         *      'network' => network type
+        * @throws \Exception
         */
        private static function authorContactByUrl($def_contact, $person, $uid)
        {
-               $condition = ['nurl' => normalise_link($person["url"]), 'uid' => $uid];
+               $condition = ['nurl' => Strings::normaliseLink($person["url"]), 'uid' => $uid];
                $contact = DBA::selectFirst('contact', ['id', 'network'], $condition);
                if (DBA::isResult($contact)) {
                        $cid = $contact["id"];
                        $network = $contact["network"];
                } else {
                        $cid = $def_contact["id"];
-                       $network = NETWORK_DIASPORA;
+                       $network = Protocol::DIASPORA;
                }
 
                return ["cid" => $cid, "network" => $network];
@@ -1445,6 +1498,8 @@ class Diaspora
         * @param string $parent_guid optional parent guid
         *
         * @return string the post link
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        private static function plink($addr, $guid, $parent_guid = '')
        {
@@ -1459,7 +1514,7 @@ class Diaspora
                        }
                }
 
-               if ($contact["network"] == NETWORK_DFRN) {
+               if ($contact["network"] == Protocol::DFRN) {
                        return str_replace("/profile/" . $contact["nick"] . "/", "/display/" . $guid, $contact["url"] . "/");
                }
 
@@ -1481,26 +1536,28 @@ class Diaspora
         * @param object $data     The message object
         *
         * @return bool Success
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        private static function receiveAccountMigration(array $importer, $data)
        {
-               $old_handle = notags(unxmlify($data->author));
-               $new_handle = notags(unxmlify($data->profile->author));
-               $signature = notags(unxmlify($data->signature));
+               $old_handle = Strings::escapeTags(XML::unescape($data->author));
+               $new_handle = Strings::escapeTags(XML::unescape($data->profile->author));
+               $signature = Strings::escapeTags(XML::unescape($data->signature));
 
                $contact = self::contactByHandle($importer["uid"], $old_handle);
                if (!$contact) {
-                       logger("cannot find contact for sender: ".$old_handle." and user ".$importer["uid"]);
+                       Logger::log("cannot find contact for sender: ".$old_handle." and user ".$importer["uid"]);
                        return false;
                }
 
-               logger("Got migration for ".$old_handle.", to ".$new_handle." with user ".$importer["uid"]);
+               Logger::log("Got migration for ".$old_handle.", to ".$new_handle." with user ".$importer["uid"]);
 
                // Check signature
                $signed_text = 'AccountMigration:'.$old_handle.':'.$new_handle;
                $key = self::key($old_handle);
                if (!Crypto::rsaVerify($signed_text, $signature, $key, "sha256")) {
-                       logger('No valid signature for migration.');
+                       Logger::log('No valid signature for migration.');
                        return false;
                }
 
@@ -1509,12 +1566,12 @@ class Diaspora
 
                // change the technical stuff in contact and gcontact
                $data = Probe::uri($new_handle);
-               if ($data['network'] == NETWORK_PHANTOM) {
-                       logger('Account for '.$new_handle." couldn't be probed.");
+               if ($data['network'] == Protocol::PHANTOM) {
+                       Logger::log('Account for '.$new_handle." couldn't be probed.");
                        return false;
                }
 
-               $fields = ['url' => $data['url'], 'nurl' => normalise_link($data['url']),
+               $fields = ['url' => $data['url'], 'nurl' => Strings::normaliseLink($data['url']),
                                'name' => $data['name'], 'nick' => $data['nick'],
                                'addr' => $data['addr'], 'batch' => $data['batch'],
                                'notify' => $data['notify'], 'poll' => $data['poll'],
@@ -1522,7 +1579,7 @@ class Diaspora
 
                DBA::update('contact', $fields, ['addr' => $old_handle]);
 
-               $fields = ['url' => $data['url'], 'nurl' => normalise_link($data['url']),
+               $fields = ['url' => $data['url'], 'nurl' => Strings::normaliseLink($data['url']),
                                'name' => $data['name'], 'nick' => $data['nick'],
                                'addr' => $data['addr'], 'connect' => $data['addr'],
                                'notify' => $data['notify'], 'photo' => $data['photo'],
@@ -1530,7 +1587,7 @@ class Diaspora
 
                DBA::update('gcontact', $fields, ['addr' => $old_handle]);
 
-               logger('Contacts are updated.');
+               Logger::log('Contacts are updated.');
 
                return true;
        }
@@ -1538,13 +1595,14 @@ class Diaspora
        /**
         * @brief Processes an account deletion
         *
-        * @param object $data     The message object
+        * @param object $data The message object
         *
         * @return bool Success
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        private static function receiveAccountDeletion($data)
        {
-               $author = notags(unxmlify($data->author));
+               $author = Strings::escapeTags(XML::unescape($data->author));
 
                $contacts = DBA::select('contact', ['id'], ['addr' => $author]);
                while ($contact = DBA::fetch($contacts)) {
@@ -1553,7 +1611,7 @@ class Diaspora
 
                DBA::delete('gcontact', ['addr' => $author]);
 
-               logger('Removed contacts for ' . $author);
+               Logger::log('Removed contacts for ' . $author);
 
                return true;
        }
@@ -1566,6 +1624,8 @@ class Diaspora
         * @param boolean $onlyfound Only return uri when found in the database
         *
         * @return string The constructed uri or the one from our database
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        private static function getUriFromGuid($author, $guid, $onlyfound = false)
        {
@@ -1573,17 +1633,13 @@ class Diaspora
                if (DBA::isResult($item)) {
                        return $item["uri"];
                } elseif (!$onlyfound) {
-                       $contact = Contact::getDetailsByAddr($author, 0);
-                       if (!empty($contact['network'])) {
-                               $prefix = 'urn:X-' . $contact['network'] . ':';
-                       } else {
-                               // This fallback should happen most unlikely
-                               $prefix = 'urn:X-dspr:';
-                       }
+                       $person = self::personByHandle($author);
 
-                       $author_parts = explode('@', $author);
+                       $parts = parse_url($person['url']);
+                       unset($parts['path']);
+                       $host_url = Network::unparseURL($parts);
 
-                       return $prefix . $author_parts[1] . ':' . $author_parts[0] . ':'. $guid;
+                       return $host_url . '/objects/' . $guid;
                }
 
                return "";
@@ -1596,6 +1652,7 @@ class Diaspora
         * @param string $uid Author handle
         *
         * @return string The post guid
+        * @throws \Exception
         */
        private static function getGuidFromUri($uri, $uid)
        {
@@ -1613,12 +1670,13 @@ class Diaspora
         * @param string $guid The guid of the item
         *
         * @return array|boolean the origin owner of that post - or false
+        * @throws \Exception
         */
        private static function importerForGuid($guid)
        {
                $item = Item::selectFirst(['uid'], ['origin' => true, 'guid' => $guid]);
                if (DBA::isResult($item)) {
-                       logger("Found user ".$item['uid']." as owner of item ".$guid, LOGGER_DEBUG);
+                       Logger::log("Found user ".$item['uid']." as owner of item ".$guid, Logger::DEBUG);
                        $contact = DBA::selectFirst('contact', [], ['self' => true, 'uid' => $item['uid']]);
                        if (DBA::isResult($contact)) {
                                return $contact;
@@ -1636,22 +1694,24 @@ class Diaspora
         * @param string $xml      The original XML of the message
         *
         * @return int The message id of the generated comment or "false" if there was an error
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        private static function receiveComment(array $importer, $sender, $data, $xml)
        {
-               $author = notags(unxmlify($data->author));
-               $guid = notags(unxmlify($data->guid));
-               $parent_guid = notags(unxmlify($data->parent_guid));
-               $text = unxmlify($data->text);
+               $author = Strings::escapeTags(XML::unescape($data->author));
+               $guid = Strings::escapeTags(XML::unescape($data->guid));
+               $parent_guid = Strings::escapeTags(XML::unescape($data->parent_guid));
+               $text = XML::unescape($data->text);
 
                if (isset($data->created_at)) {
-                       $created_at = DateTimeFormat::utc(notags(unxmlify($data->created_at)));
+                       $created_at = DateTimeFormat::utc(Strings::escapeTags(XML::unescape($data->created_at)));
                } else {
                        $created_at = DateTimeFormat::utcNow();
                }
 
                if (isset($data->thread_parent_guid)) {
-                       $thread_parent_guid = notags(unxmlify($data->thread_parent_guid));
+                       $thread_parent_guid = Strings::escapeTags(XML::unescape($data->thread_parent_guid));
                        $thr_uri = self::getUriFromGuid("", $thread_parent_guid, true);
                } else {
                        $thr_uri = "";
@@ -1674,7 +1734,7 @@ class Diaspora
 
                $person = self::personByHandle($author);
                if (!is_array($person)) {
-                       logger("unable to find author details");
+                       Logger::log("unable to find author details");
                        return false;
                }
 
@@ -1733,7 +1793,7 @@ class Diaspora
                }
 
                if ($message_id) {
-                       logger("Stored comment ".$datarray["guid"]." with message id ".$message_id, LOGGER_DEBUG);
+                       Logger::log("Stored comment ".$datarray["guid"]." with message id ".$message_id, Logger::DEBUG);
                        if ($datarray['uid'] == 0) {
                                Item::distribute($message_id, json_encode($data));
                        }
@@ -1753,30 +1813,31 @@ class Diaspora
         * @param array  $conversation The conversation record to which this message belongs
         *
         * @return bool "true" if it was successful
+        * @throws \Exception
         */
        private static function receiveConversationMessage(array $importer, array $contact, $data, $msg, $mesg, $conversation)
        {
-               $author = notags(unxmlify($data->author));
-               $guid = notags(unxmlify($data->guid));
-               $subject = notags(unxmlify($data->subject));
+               $author = Strings::escapeTags(XML::unescape($data->author));
+               $guid = Strings::escapeTags(XML::unescape($data->guid));
+               $subject = Strings::escapeTags(XML::unescape($data->subject));
 
                // "diaspora_handle" is the element name from the old version
                // "author" is the element name from the new version
                if ($mesg->author) {
-                       $msg_author = notags(unxmlify($mesg->author));
+                       $msg_author = Strings::escapeTags(XML::unescape($mesg->author));
                } elseif ($mesg->diaspora_handle) {
-                       $msg_author = notags(unxmlify($mesg->diaspora_handle));
+                       $msg_author = Strings::escapeTags(XML::unescape($mesg->diaspora_handle));
                } else {
                        return false;
                }
 
-               $msg_guid = notags(unxmlify($mesg->guid));
-               $msg_conversation_guid = notags(unxmlify($mesg->conversation_guid));
-               $msg_text = unxmlify($mesg->text);
-               $msg_created_at = DateTimeFormat::utc(notags(unxmlify($mesg->created_at)));
+               $msg_guid = Strings::escapeTags(XML::unescape($mesg->guid));
+               $msg_conversation_guid = Strings::escapeTags(XML::unescape($mesg->conversation_guid));
+               $msg_text = XML::unescape($mesg->text);
+               $msg_created_at = DateTimeFormat::utc(Strings::escapeTags(XML::unescape($mesg->created_at)));
 
                if ($msg_conversation_guid != $guid) {
-                       logger("message conversation guid does not belong to the current conversation.");
+                       Logger::log("message conversation guid does not belong to the current conversation.");
                        return false;
                }
 
@@ -1785,57 +1846,20 @@ class Diaspora
 
                $person = self::personByHandle($msg_author);
 
-               DBA::lock('mail');
-
-               $r = q(
-                       "SELECT `id` FROM `mail` WHERE `guid` = '%s' AND `uid` = %d LIMIT 1",
-                       DBA::escape($msg_guid),
-                       intval($importer["uid"])
-               );
-               if (DBA::isResult($r)) {
-                       logger("duplicate message already delivered.", LOGGER_DEBUG);
-                       return false;
-               }
-
-               q(
-                       "INSERT INTO `mail` (`uid`, `guid`, `convid`, `from-name`,`from-photo`,`from-url`,`contact-id`,`title`,`body`,`seen`,`reply`,`uri`,`parent-uri`,`created`)
-                       VALUES (%d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s','%s','%s')",
-                       intval($importer["uid"]),
-                       DBA::escape($msg_guid),
-                       intval($conversation["id"]),
-                       DBA::escape($person["name"]),
-                       DBA::escape($person["photo"]),
-                       DBA::escape($person["url"]),
-                       intval($contact["id"]),
-                       DBA::escape($subject),
-                       DBA::escape($body),
-                       0,
-                       0,
-                       DBA::escape($message_uri),
-                       DBA::escape($author.":".$guid),
-                       DBA::escape($msg_created_at)
-               );
-
-               DBA::unlock();
-
-               DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $conversation["id"]]);
-
-               notification(
-                       [
-                       "type" => NOTIFY_MAIL,
-                       "notify_flags" => $importer["notify-flags"],
-                       "language" => $importer["language"],
-                       "to_name" => $importer["username"],
-                       "to_email" => $importer["email"],
-                       "uid" =>$importer["uid"],
-                       "item" => ["subject" => $subject, "body" => $body],
-                       "source_name" => $person["name"],
-                       "source_link" => $person["url"],
-                       "source_photo" => $person["thumb"],
-                       "verb" => ACTIVITY_POST,
-                       "otype" => "mail"]
-               );
-               return true;
+               return Mail::insert([
+                       'uid'        => $importer['uid'],
+                       'guid'       => $msg_guid,
+                       'convid'     => $conversation['id'],
+                       'from-name'  => $person['name'],
+                       'from-photo' => $person['photo'],
+                       'from-url'   => $person['url'],
+                       'contact-id' => $contact['id'],
+                       'title'      => $subject,
+                       'body'       => $body,
+                       'uri'        => $message_uri,
+                       'parent-uri' => $author . ':' . $guid,
+                       'created'    => $msg_created_at
+               ]);
        }
 
        /**
@@ -1846,19 +1870,20 @@ class Diaspora
         * @param object $data     The message object
         *
         * @return bool Success
+        * @throws \Exception
         */
        private static function receiveConversation(array $importer, $msg, $data)
        {
-               $author = notags(unxmlify($data->author));
-               $guid = notags(unxmlify($data->guid));
-               $subject = notags(unxmlify($data->subject));
-               $created_at = DateTimeFormat::utc(notags(unxmlify($data->created_at)));
-               $participants = notags(unxmlify($data->participants));
+               $author = Strings::escapeTags(XML::unescape($data->author));
+               $guid = Strings::escapeTags(XML::unescape($data->guid));
+               $subject = Strings::escapeTags(XML::unescape($data->subject));
+               $created_at = DateTimeFormat::utc(Strings::escapeTags(XML::unescape($data->created_at)));
+               $participants = Strings::escapeTags(XML::unescape($data->participants));
 
                $messages = $data->message;
 
                if (!count($messages)) {
-                       logger("empty conversation");
+                       Logger::log("empty conversation");
                        return false;
                }
 
@@ -1867,16 +1892,8 @@ class Diaspora
                        return false;
                }
 
-               $conversation = null;
-
-               $c = q(
-                       "SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
-                       intval($importer["uid"]),
-                       DBA::escape($guid)
-               );
-               if ($c)
-                       $conversation = $c[0];
-               else {
+               $conversation = DBA::selectFirst('conv', [], ['uid' => $importer["uid"], 'guid' => $guid]);
+               if (!DBA::isResult($conversation)) {
                        $r = q(
                                "INSERT INTO `conv` (`uid`, `guid`, `creator`, `created`, `updated`, `subject`, `recips`)
                                VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s')",
@@ -1889,19 +1906,11 @@ class Diaspora
                                DBA::escape($participants)
                        );
                        if ($r) {
-                               $c = q(
-                                       "SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
-                                       intval($importer["uid"]),
-                                       DBA::escape($guid)
-                               );
-                       }
-
-                       if ($c) {
-                               $conversation = $c[0];
+                               $conversation = DBA::selectFirst('conv', [], ['uid' => $importer["uid"], 'guid' => $guid]);
                        }
                }
                if (!$conversation) {
-                       logger("unable to create conversation.");
+                       Logger::log("unable to create conversation.");
                        return false;
                }
 
@@ -1920,14 +1929,16 @@ class Diaspora
         * @param object $data     The message object
         *
         * @return int The message id of the generated like or "false" if there was an error
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        private static function receiveLike(array $importer, $sender, $data)
        {
-               $author = notags(unxmlify($data->author));
-               $guid = notags(unxmlify($data->guid));
-               $parent_guid = notags(unxmlify($data->parent_guid));
-               $parent_type = notags(unxmlify($data->parent_type));
-               $positive = notags(unxmlify($data->positive));
+               $author = Strings::escapeTags(XML::unescape($data->author));
+               $guid = Strings::escapeTags(XML::unescape($data->guid));
+               $parent_guid = Strings::escapeTags(XML::unescape($data->parent_guid));
+               $parent_type = Strings::escapeTags(XML::unescape($data->parent_type));
+               $positive = Strings::escapeTags(XML::unescape($data->positive));
 
                // 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.
@@ -1952,7 +1963,7 @@ class Diaspora
 
                $person = self::personByHandle($author);
                if (!is_array($person)) {
-                       logger("unable to find author details");
+                       Logger::log("unable to find author details");
                        return false;
                }
 
@@ -1975,11 +1986,8 @@ class Diaspora
                $datarray["contact-id"] = $author_contact["cid"];
                $datarray["network"]  = $author_contact["network"];
 
-               $datarray["author-link"] = $person["url"];
-               $datarray["author-id"] = Contact::getIdForURL($person["url"], 0);
-
-               $datarray["owner-link"] = $contact["url"];
-               $datarray["owner-id"] = Contact::getIdForURL($contact["url"], 0);
+               $datarray["owner-link"] = $datarray["author-link"] = $person["url"];
+               $datarray["owner-id"] = $datarray["author-id"] = Contact::getIdForURL($person["url"], 0);
 
                $datarray["guid"] = $guid;
                $datarray["uri"] = self::getUriFromGuid($author, $guid);
@@ -2016,7 +2024,7 @@ class Diaspora
                }
 
                if ($message_id) {
-                       logger("Stored like ".$datarray["guid"]." with message id ".$message_id, LOGGER_DEBUG);
+                       Logger::log("Stored like ".$datarray["guid"]." with message id ".$message_id, Logger::DEBUG);
                        if ($datarray['uid'] == 0) {
                                Item::distribute($message_id, json_encode($data));
                        }
@@ -2032,14 +2040,15 @@ class Diaspora
         * @param object $data     The message object
         *
         * @return bool Success?
+        * @throws \Exception
         */
        private static function receiveMessage(array $importer, $data)
        {
-               $author = notags(unxmlify($data->author));
-               $guid = notags(unxmlify($data->guid));
-               $conversation_guid = notags(unxmlify($data->conversation_guid));
-               $text = unxmlify($data->text);
-               $created_at = DateTimeFormat::utc(notags(unxmlify($data->created_at)));
+               $author = Strings::escapeTags(XML::unescape($data->author));
+               $guid = Strings::escapeTags(XML::unescape($data->guid));
+               $conversation_guid = Strings::escapeTags(XML::unescape($data->conversation_guid));
+               $text = XML::unescape($data->text);
+               $created_at = DateTimeFormat::utc(Strings::escapeTags(XML::unescape($data->created_at)));
 
                $contact = self::allowedContactByHandle($importer, $author, true);
                if (!$contact) {
@@ -2048,15 +2057,11 @@ class Diaspora
 
                $conversation = null;
 
-               $c = q(
-                       "SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
-                       intval($importer["uid"]),
-                       DBA::escape($conversation_guid)
-               );
-               if ($c) {
-                       $conversation = $c[0];
-               } else {
-                       logger("conversation not available.");
+               $condition = ['uid' => $importer["uid"], 'guid' => $conversation_guid];
+               $conversation = DBA::selectFirst('conv', [], $condition);
+
+               if (!DBA::isResult($conversation)) {
+                       Logger::log("conversation not available.");
                        return false;
                }
 
@@ -2064,7 +2069,7 @@ class Diaspora
 
                $person = self::personByHandle($author);
                if (!$person) {
-                       logger("unable to find author details");
+                       Logger::log("unable to find author details");
                        return false;
                }
 
@@ -2072,41 +2077,21 @@ class Diaspora
 
                $body = self::replacePeopleGuid($body, $person["url"]);
 
-               DBA::lock('mail');
-
-               $r = q(
-                       "SELECT `id` FROM `mail` WHERE `guid` = '%s' AND `uid` = %d LIMIT 1",
-                       DBA::escape($guid),
-                       intval($importer["uid"])
-               );
-               if (DBA::isResult($r)) {
-                       logger("duplicate message already delivered.", LOGGER_DEBUG);
-                       return false;
-               }
-
-               q(
-                       "INSERT INTO `mail` (`uid`, `guid`, `convid`, `from-name`,`from-photo`,`from-url`,`contact-id`,`title`,`body`,`seen`,`reply`,`uri`,`parent-uri`,`created`)
-                               VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s','%s','%s')",
-                       intval($importer["uid"]),
-                       DBA::escape($guid),
-                       intval($conversation["id"]),
-                       DBA::escape($person["name"]),
-                       DBA::escape($person["photo"]),
-                       DBA::escape($person["url"]),
-                       intval($contact["id"]),
-                       DBA::escape($conversation["subject"]),
-                       DBA::escape($body),
-                       0,
-                       1,
-                       DBA::escape($message_uri),
-                       DBA::escape($author.":".$conversation["guid"]),
-                       DBA::escape($created_at)
-               );
-
-               DBA::unlock();
-
-               DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $conversation["id"]]);
-               return true;
+               return Mail::insert([
+                       'uid'        => $importer['uid'],
+                       'guid'       => $guid,
+                       'convid'     => $conversation['id'],
+                       'from-name'  => $person['name'],
+                       'from-photo' => $person['photo'],
+                       'from-url'   => $person['url'],
+                       'contact-id' => $contact['id'],
+                       'title'      => $conversation['subject'],
+                       'body'       => $body,
+                       'reply'      => 1,
+                       'uri'        => $message_uri,
+                       'parent-uri' => $author.":".$conversation['guid'],
+                       'created'    => $created_at
+               ]);
        }
 
        /**
@@ -2116,27 +2101,29 @@ class Diaspora
         * @param object $data     The message object
         *
         * @return bool always true
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        private static function receiveParticipation(array $importer, $data)
        {
-               $author = strtolower(notags(unxmlify($data->author)));
-               $parent_guid = notags(unxmlify($data->parent_guid));
+               $author = strtolower(Strings::escapeTags(XML::unescape($data->author)));
+               $parent_guid = Strings::escapeTags(XML::unescape($data->parent_guid));
 
                $contact_id = Contact::getIdForURL($author);
                if (!$contact_id) {
-                       logger('Contact not found: '.$author);
+                       Logger::log('Contact not found: '.$author);
                        return false;
                }
 
                $person = self::personByHandle($author);
                if (!is_array($person)) {
-                       logger("Person not found: ".$author);
+                       Logger::log("Person not found: ".$author);
                        return false;
                }
 
                $item = Item::selectFirst(['id'], ['guid' => $parent_guid, 'origin' => true, 'private' => false]);
                if (!DBA::isResult($item)) {
-                       logger('Item not found, no origin or private: '.$parent_guid);
+                       Logger::log('Item not found, no origin or private: '.$parent_guid);
                        return false;
                }
 
@@ -2148,7 +2135,7 @@ class Diaspora
                        $server = $author;
                }
 
-               logger('Received participation for ID: '.$item['id'].' - Contact: '.$contact_id.' - Server: '.$server, LOGGER_DEBUG);
+               Logger::log('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]);
@@ -2165,7 +2152,7 @@ class Diaspora
                        } else {
                                $cmd = $comment['self'] ? 'like' : 'comment-import';
                        }
-                       logger("Send ".$cmd." for item ".$comment['id']." to contact ".$contact_id, LOGGER_DEBUG);
+                       Logger::log("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);
@@ -2209,25 +2196,27 @@ class Diaspora
         * @param object $data     The message object
         *
         * @return bool Success
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        private static function receiveProfile(array $importer, $data)
        {
-               $author = strtolower(notags(unxmlify($data->author)));
+               $author = strtolower(Strings::escapeTags(XML::unescape($data->author)));
 
                $contact = self::contactByHandle($importer["uid"], $author);
                if (!$contact) {
                        return false;
                }
 
-               $name = unxmlify($data->first_name).((strlen($data->last_name)) ? " ".unxmlify($data->last_name) : "");
-               $image_url = unxmlify($data->image_url);
-               $birthday = unxmlify($data->birthday);
-               $gender = unxmlify($data->gender);
-               $about = Markdown::toBBCode(unxmlify($data->bio));
-               $location = Markdown::toBBCode(unxmlify($data->location));
-               $searchable = (unxmlify($data->searchable) == "true");
-               $nsfw = (unxmlify($data->nsfw) == "true");
-               $tags = unxmlify($data->tag_string);
+               $name = XML::unescape($data->first_name).((strlen($data->last_name)) ? " ".XML::unescape($data->last_name) : "");
+               $image_url = XML::unescape($data->image_url);
+               $birthday = XML::unescape($data->birthday);
+               $gender = XML::unescape($data->gender);
+               $about = Markdown::toBBCode(XML::unescape($data->bio));
+               $location = Markdown::toBBCode(XML::unescape($data->location));
+               $searchable = (XML::unescape($data->searchable) == "true");
+               $nsfw = (XML::unescape($data->nsfw) == "true");
+               $tags = XML::unescape($data->tag_string);
 
                $tags = explode("#", $tags);
 
@@ -2281,7 +2270,7 @@ class Diaspora
 
                DBA::update('contact', $fields, ['id' => $contact['id']]);
 
-               $gcontact = ["url" => $contact["url"], "network" => NETWORK_DIASPORA, "generation" => 2,
+               $gcontact = ["url" => $contact["url"], "network" => Protocol::DIASPORA, "generation" => 2,
                                        "photo" => $image_url, "name" => $name, "location" => $location,
                                        "about" => $about, "birthday" => $birthday, "gender" => $gender,
                                        "addr" => $author, "nick" => $nick, "keywords" => $keywords,
@@ -2291,7 +2280,7 @@ class Diaspora
 
                GContact::link($gcid, $importer["uid"], $contact["id"]);
 
-               logger("Profile of contact ".$contact["id"]." stored for user ".$importer["uid"], LOGGER_DEBUG);
+               Logger::log("Profile of contact ".$contact["id"]." stored for user ".$importer["uid"], Logger::DEBUG);
 
                return true;
        }
@@ -2302,11 +2291,10 @@ class Diaspora
         * @param array $importer Array of the importer user
         * @param array $contact  The contact that send the request
         * @return void
+        * @throws \Exception
         */
        private static function receiveRequestMakeFriend(array $importer, array $contact)
        {
-               $a = get_app();
-
                if ($contact["rel"] == Contact::SHARING) {
                        DBA::update(
                                'contact',
@@ -2323,11 +2311,12 @@ class Diaspora
         * @param object $data     The message object
         *
         * @return bool Success
+        * @throws \Exception
         */
        private static function receiveContactRequest(array $importer, $data)
        {
-               $author = unxmlify($data->author);
-               $recipient = unxmlify($data->recipient);
+               $author = XML::unescape($data->author);
+               $recipient = XML::unescape($data->recipient);
 
                if (!$author || !$recipient) {
                        return false;
@@ -2336,13 +2325,13 @@ class Diaspora
                // the current protocol version doesn't know these fields
                // That means that we will assume their existance
                if (isset($data->following)) {
-                       $following = (unxmlify($data->following) == "true");
+                       $following = (XML::unescape($data->following) == "true");
                } else {
                        $following = true;
                }
 
                if (isset($data->sharing)) {
-                       $sharing = (unxmlify($data->sharing) == "true");
+                       $sharing = (XML::unescape($data->sharing) == "true");
                } else {
                        $sharing = true;
                }
@@ -2353,7 +2342,7 @@ class Diaspora
                // That makes us friends.
                if ($contact) {
                        if ($following) {
-                               logger("Author ".$author." (Contact ".$contact["id"].") wants to follow us.", LOGGER_DEBUG);
+                               Logger::log("Author ".$author." (Contact ".$contact["id"].") wants to follow us.", Logger::DEBUG);
                                self::receiveRequestMakeFriend($importer, $contact);
 
                                // refetch the contact array
@@ -2362,44 +2351,44 @@ 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"], [Contact::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);
-                                               $ret = self::sendShare($u[0], $contact);
+                                       $user = DBA::selectFirst('user', [], ['uid' => $importer["uid"]]);
+                                       if (DBA::isResult($user)) {
+                                               Logger::log("Sending share message to author ".$author." - Contact: ".$contact["id"]." - User: ".$importer["uid"], Logger::DEBUG);
+                                               self::sendShare($user, $contact);
                                        }
                                }
                                return true;
                        } else {
-                               logger("Author ".$author." doesn't want to follow us anymore.", LOGGER_DEBUG);
+                               Logger::log("Author ".$author." doesn't want to follow us anymore.", Logger::DEBUG);
                                Contact::removeFollower($importer, $contact);
                                return true;
                        }
                }
 
-               if (!$following && $sharing && in_array($importer["page-flags"], [Contact::PAGE_SOAPBOX, Contact::PAGE_NORMAL])) {
-                       logger("Author ".$author." wants to share with us - but doesn't want to listen. Request is ignored.", LOGGER_DEBUG);
+               if (!$following && $sharing && in_array($importer["page-flags"], [User::PAGE_FLAGS_SOAPBOX, User::PAGE_FLAGS_NORMAL])) {
+                       Logger::log("Author ".$author." wants to share with us - but doesn't want to listen. Request is ignored.", Logger::DEBUG);
                        return false;
                } elseif (!$following && !$sharing) {
-                       logger("Author ".$author." doesn't want anything - and we don't know the author. Request is ignored.", LOGGER_DEBUG);
+                       Logger::log("Author ".$author." doesn't want anything - and we don't know the author. Request is ignored.", Logger::DEBUG);
                        return false;
                } elseif (!$following && $sharing) {
-                       logger("Author ".$author." wants to share with us.", LOGGER_DEBUG);
+                       Logger::log("Author ".$author." wants to share with us.", Logger::DEBUG);
                } elseif ($following && $sharing) {
-                       logger("Author ".$author." wants to have a bidirectional conection.", LOGGER_DEBUG);
+                       Logger::log("Author ".$author." wants to have a bidirectional conection.", Logger::DEBUG);
                } elseif ($following && !$sharing) {
-                       logger("Author ".$author." wants to listen to us.", LOGGER_DEBUG);
+                       Logger::log("Author ".$author." wants to listen to us.", Logger::DEBUG);
                }
 
                $ret = self::personByHandle($author);
 
-               if (!$ret || ($ret["network"] != NETWORK_DIASPORA)) {
-                       logger("Cannot resolve diaspora handle ".$author." for ".$recipient);
+               if (!$ret || ($ret["network"] != Protocol::DIASPORA)) {
+                       Logger::log("Cannot resolve diaspora handle ".$author." for ".$recipient);
                        return false;
                }
 
                $batch = (($ret["batch"]) ? $ret["batch"] : implode("/", array_slice(explode("/", $ret["url"]), 0, 3))."/receive/public");
 
-               $r = q(
+               q(
                        "INSERT INTO `contact` (`uid`, `network`,`addr`,`created`,`url`,`nurl`,`batch`,`name`,`nick`,`photo`,`pubkey`,`notify`,`poll`,`blocked`,`priority`)
                        VALUES (%d, '%s', '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s','%s',%d,%d)",
                        intval($importer["uid"]),
@@ -2407,7 +2396,7 @@ class Diaspora
                        DBA::escape($ret["addr"]),
                        DateTimeFormat::utcNow(),
                        DBA::escape($ret["url"]),
-                       DBA::escape(normalise_link($ret["url"])),
+                       DBA::escape(Strings::normaliseLink($ret["url"])),
                        DBA::escape($batch),
                        DBA::escape($ret["name"]),
                        DBA::escape($ret["nick"]),
@@ -2424,22 +2413,22 @@ class Diaspora
                $contact_record = self::contactByHandle($importer["uid"], $author);
 
                if (!$contact_record) {
-                       logger("unable to locate newly created contact record.");
+                       Logger::log("unable to locate newly created contact record.");
                        return;
                }
 
-               logger("Author ".$author." was added as contact number ".$contact_record["id"].".", LOGGER_DEBUG);
+               Logger::log("Author ".$author." was added as contact number ".$contact_record["id"].".", Logger::DEBUG);
 
                Group::addMember(User::getDefaultGroup($importer['uid'], $ret["network"]), $contact_record['id']);
 
                Contact::updateAvatar($ret["photo"], $importer['uid'], $contact_record["id"], true);
 
-               if (in_array($importer["page-flags"], [Contact::PAGE_NORMAL, Contact::PAGE_PRVGROUP])) {
-                       logger("Sending intra message for author ".$author.".", LOGGER_DEBUG);
+               if (in_array($importer["page-flags"], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP])) {
+                       Logger::log("Sending intra message for author ".$author.".", Logger::DEBUG);
 
-                       $hash = random_string().(string)time();   // Generate a confirm_key
+                       $hash = Strings::getRandomHex().(string)time();   // Generate a confirm_key
 
-                       $ret = q(
+                       q(
                                "INSERT INTO `intro` (`uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
                                VALUES (%d, %d, %d, %d, '%s', '%s', '%s')",
                                intval($importer["uid"]),
@@ -2453,23 +2442,24 @@ class Diaspora
                } else {
                        // automatic friend approval
 
-                       logger("Does an automatic friend approval for author ".$author.".", LOGGER_DEBUG);
+                       Logger::log("Does an automatic friend approval for author ".$author.".", Logger::DEBUG);
 
                        Contact::updateAvatar($contact_record["photo"], $importer["uid"], $contact_record["id"]);
 
-                       // technically they are sharing with us (Contact::SHARING),
-                       // but if our page-type is PAGE_COMMUNITY or PAGE_SOAPBOX
-                       // we are going to change the relationship and make them a follower.
-
-                       if (($importer["page-flags"] == Contact::PAGE_FREELOVE) && $sharing && $following) {
+                       /*
+                        * technically they are sharing with us (Contact::SHARING),
+                        * but if our page-type is Profile::PAGE_COMMUNITY or Profile::PAGE_SOAPBOX
+                        * we are going to change the relationship and make them a follower.
+                        */
+                       if (($importer["page-flags"] == User::PAGE_FLAGS_FREELOVE) && $sharing && $following) {
                                $new_relation = Contact::FRIEND;
-                       } elseif (($importer["page-flags"] == Contact::PAGE_FREELOVE) && $sharing) {
+                       } elseif (($importer["page-flags"] == User::PAGE_FLAGS_FREELOVE) && $sharing) {
                                $new_relation = Contact::SHARING;
                        } else {
                                $new_relation = Contact::FOLLOWER;
                        }
 
-                       $r = q(
+                       q(
                                "UPDATE `contact` SET `rel` = %d,
                                `name-date` = '%s',
                                `uri-date` = '%s',
@@ -2484,10 +2474,10 @@ class Diaspora
                                intval($contact_record["id"])
                        );
 
-                       $u = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($importer["uid"]));
-                       if ($u) {
-                               logger("Sending share message (Relation: ".$new_relation.") to author ".$author." - Contact: ".$contact_record["id"]." - User: ".$importer["uid"], LOGGER_DEBUG);
-                               $ret = self::sendShare($u[0], $contact_record);
+                       $user = DBA::selectFirst('user', [], ['uid' => $importer["uid"]]);
+                       if (DBA::isResult($user)) {
+                               Logger::log("Sending share message (Relation: ".$new_relation.") to author ".$author." - Contact: ".$contact_record["id"]." - User: ".$importer["uid"], Logger::DEBUG);
+                               self::sendShare($user, $contact_record);
 
                                // Send the profile data, maybe it weren't transmitted before
                                self::sendProfile($importer["uid"], [$contact_record]);
@@ -2502,14 +2492,14 @@ class Diaspora
         *
         * @param string $guid        message guid
         * @param string $orig_author handle of the original post
-        * @param string $author      handle of the sharer
-        *
         * @return array The fetched item
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        public static function originalItem($guid, $orig_author)
        {
                if (empty($guid)) {
-                       logger('Empty guid. Quitting.');
+                       Logger::log('Empty guid. Quitting.');
                        return false;
                }
 
@@ -2520,7 +2510,7 @@ class Diaspora
                $item = Item::selectFirst($fields, $condition);
 
                if (DBA::isResult($item)) {
-                       logger("reshared message ".$guid." already exists on system.");
+                       Logger::log("reshared message ".$guid." already exists on system.");
 
                        // Maybe it is already a reshared item?
                        // Then refetch the content, if it is a reshare from a reshare.
@@ -2543,17 +2533,17 @@ class Diaspora
 
                if (!DBA::isResult($item)) {
                        if (empty($orig_author)) {
-                               logger('Empty author for guid ' . $guid . '. Quitting.');
+                               Logger::log('Empty author for guid ' . $guid . '. Quitting.');
                                return false;
                        }
 
                        $server = "https://".substr($orig_author, strpos($orig_author, "@") + 1);
-                       logger("1st try: reshared message ".$guid." will be fetched via SSL from the server ".$server);
+                       Logger::log("1st try: reshared message ".$guid." will be fetched via SSL from the server ".$server);
                        $stored = self::storeByGuid($guid, $server);
 
                        if (!$stored) {
                                $server = "http://".substr($orig_author, strpos($orig_author, "@") + 1);
-                               logger("2nd try: reshared message ".$guid." will be fetched without SSL from the server ".$server);
+                               Logger::log("2nd try: reshared message ".$guid." will be fetched without SSL from the server ".$server);
                                $stored = self::storeByGuid($guid, $server);
                        }
 
@@ -2577,6 +2567,54 @@ class Diaspora
                return false;
        }
 
+       /**
+        * @brief Stores a reshare activity
+        *
+        * @param array   $item              Array of reshare post
+        * @param integer $parent_message_id Id of the parent post
+        * @param string  $guid              GUID string of reshare action
+        * @param string  $author            Author handle
+        */
+       private static function addReshareActivity($item, $parent_message_id, $guid, $author)
+       {
+               $parent = Item::selectFirst(['uri', 'guid'], ['id' => $parent_message_id]);
+
+               $datarray = [];
+
+               $datarray['uid'] = $item['uid'];
+               $datarray['contact-id'] = $item['contact-id'];
+               $datarray['network'] = $item['network'];
+
+               $datarray['author-link'] = $item['author-link'];
+               $datarray['author-id'] = $item['author-id'];
+
+               $datarray['owner-link'] = $datarray['author-link'];
+               $datarray['owner-id'] = $datarray['author-id'];
+
+               $datarray['guid'] = $parent['guid'] . '-' . $guid;
+               $datarray['uri'] = self::getUriFromGuid($author, $datarray['guid']);
+               $datarray['parent-uri'] = $parent['uri'];
+
+               $datarray['verb'] = $datarray['body'] = ACTIVITY2_ANNOUNCE;
+               $datarray['gravity'] = GRAVITY_ACTIVITY;
+               $datarray['object-type'] = ACTIVITY_OBJ_NOTE;
+
+               $datarray['protocol'] = $item['protocol'];
+
+               $datarray['plink'] = self::plink($author, $datarray['guid']);
+               $datarray['private'] = $item['private'];
+               $datarray['changed'] = $datarray['created'] = $datarray['edited'] = $item['created'];
+
+               $message_id = Item::insert($datarray);
+
+               if ($message_id) {
+                       Logger::info('Stored reshare activity.', ['guid' => $guid, 'id' => $message_id]);
+                       if ($datarray['uid'] == 0) {
+                               Item::distribute($message_id);
+                       }
+               }
+       }
+
        /**
         * @brief Processes a reshare message
         *
@@ -2585,16 +2623,18 @@ class Diaspora
         * @param string $xml      The original XML of the message
         *
         * @return int the message id
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        private static function receiveReshare(array $importer, $data, $xml)
        {
-               $author = notags(unxmlify($data->author));
-               $guid = notags(unxmlify($data->guid));
-               $created_at = DateTimeFormat::utc(notags(unxmlify($data->created_at)));
-               $root_author = notags(unxmlify($data->root_author));
-               $root_guid = notags(unxmlify($data->root_guid));
+               $author = Strings::escapeTags(XML::unescape($data->author));
+               $guid = Strings::escapeTags(XML::unescape($data->guid));
+               $created_at = DateTimeFormat::utc(Strings::escapeTags(XML::unescape($data->created_at)));
+               $root_author = Strings::escapeTags(XML::unescape($data->root_author));
+               $root_guid = Strings::escapeTags(XML::unescape($data->root_guid));
                /// @todo handle unprocessed property "provider_display_name"
-               $public = notags(unxmlify($data->public));
+               $public = Strings::escapeTags(XML::unescape($data->public));
 
                $contact = self::allowedContactByHandle($importer, $author, false);
                if (!$contact) {
@@ -2617,7 +2657,7 @@ class Diaspora
 
                $datarray["uid"] = $importer["uid"];
                $datarray["contact-id"] = $contact["id"];
-               $datarray["network"]  = NETWORK_DIASPORA;
+               $datarray["network"]  = Protocol::DIASPORA;
 
                $datarray["author-link"] = $contact["url"];
                $datarray["author-id"] = Contact::getIdForURL($contact["url"], 0);
@@ -2658,8 +2698,13 @@ class Diaspora
 
                self::sendParticipation($contact, $datarray);
 
+               $root_message_id = self::messageExists($importer["uid"], $root_guid);
+               if ($root_message_id) {
+                       self::addReshareActivity($datarray, $root_message_id, $guid, $author);
+               }
+
                if ($message_id) {
-                       logger("Stored reshare ".$datarray["guid"]." with message id ".$message_id, LOGGER_DEBUG);
+                       Logger::log("Stored reshare ".$datarray["guid"]." with message id ".$message_id, Logger::DEBUG);
                        if ($datarray['uid'] == 0) {
                                Item::distribute($message_id);
                        }
@@ -2677,16 +2722,17 @@ class Diaspora
         * @param object $data     The message object
         *
         * @return bool success
+        * @throws \Exception
         */
        private static function itemRetraction(array $importer, array $contact, $data)
        {
-               $author = notags(unxmlify($data->author));
-               $target_guid = notags(unxmlify($data->target_guid));
-               $target_type = notags(unxmlify($data->target_type));
+               $author = Strings::escapeTags(XML::unescape($data->author));
+               $target_guid = Strings::escapeTags(XML::unescape($data->target_guid));
+               $target_type = Strings::escapeTags(XML::unescape($data->target_type));
 
                $person = self::personByHandle($author);
                if (!is_array($person)) {
-                       logger("unable to find author detail for ".$author);
+                       Logger::log("unable to find author detail for ".$author);
                        return false;
                }
 
@@ -2706,13 +2752,13 @@ class Diaspora
 
                $r = Item::select($fields, $condition);
                if (!DBA::isResult($r)) {
-                       logger("Target guid ".$target_guid." was not found on this system for user ".$importer['uid'].".");
+                       Logger::log("Target guid ".$target_guid." was not found on this system for user ".$importer['uid'].".");
                        return false;
                }
 
                while ($item = Item::fetch($r)) {
                        if (strstr($item['file'], '[')) {
-                               logger("Target guid " . $target_guid . " for user " . $item['uid'] . " is filed. So it won't be deleted.", LOGGER_DEBUG);
+                               Logger::log("Target guid " . $target_guid . " for user " . $item['uid'] . " is filed. So it won't be deleted.", Logger::DEBUG);
                                continue;
                        }
 
@@ -2720,14 +2766,14 @@ class Diaspora
                        $parent = Item::selectFirst(['author-link'], ['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"])) {
-                               logger("Thread author ".$parent["author-link"]." and item author ".$item["author-link"]." don't fit to expected contact ".$contact["url"], LOGGER_DEBUG);
+                       if (!Strings::compareLink($parent["author-link"], $contact["url"]) && !Strings::compareLink($item["author-link"], $contact["url"])) {
+                               Logger::log("Thread author ".$parent["author-link"]." and item author ".$item["author-link"]." don't fit to expected contact ".$contact["url"], Logger::DEBUG);
                                continue;
                        }
 
                        Item::delete(['id' => $item['id']]);
 
-                       logger("Deleted target ".$target_guid." (".$item["id"].") from user ".$item["uid"]." parent: ".$item["parent"], LOGGER_DEBUG);
+                       Logger::log("Deleted target ".$target_guid." (".$item["id"].") from user ".$item["uid"]." parent: ".$item["parent"], Logger::DEBUG);
                }
 
                return true;
@@ -2741,14 +2787,15 @@ class Diaspora
         * @param object $data     The message object
         *
         * @return bool Success
+        * @throws \Exception
         */
        private static function receiveRetraction(array $importer, $sender, $data)
        {
-               $target_type = notags(unxmlify($data->target_type));
+               $target_type = Strings::escapeTags(XML::unescape($data->target_type));
 
                $contact = self::contactByHandle($importer["uid"], $sender);
                if (!$contact && (in_array($target_type, ["Contact", "Person"]))) {
-                       logger("cannot find contact for sender: ".$sender." and user ".$importer["uid"]);
+                       Logger::log("cannot find contact for sender: ".$sender." and user ".$importer["uid"]);
                        return false;
                }
 
@@ -2756,7 +2803,7 @@ class Diaspora
                        $contact = [];
                }
 
-               logger("Got retraction for ".$target_type.", sender ".$sender." and user ".$importer["uid"], LOGGER_DEBUG);
+               Logger::log("Got retraction for ".$target_type.", sender ".$sender." and user ".$importer["uid"], Logger::DEBUG);
 
                switch ($target_type) {
                        case "Comment":
@@ -2772,7 +2819,7 @@ class Diaspora
                                break;
 
                        default:
-                               logger("Unknown target type ".$target_type);
+                               Logger::log("Unknown target type ".$target_type);
                                return false;
                }
                return true;
@@ -2781,20 +2828,22 @@ class Diaspora
        /**
         * @brief Receives status messages
         *
-        * @param array  $importer Array of the importer user
-        * @param object $data     The message object
-        * @param string $xml      The original XML of the message
+        * @param array            $importer Array of the importer user
+        * @param SimpleXMLElement $data     The message object
+        * @param string           $xml      The original XML of the message
         *
         * @return int The message id of the newly created item
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
-       private static function receiveStatusMessage(array $importer, $data, $xml)
+       private static function receiveStatusMessage(array $importer, SimpleXMLElement $data, $xml)
        {
-               $author = notags(unxmlify($data->author));
-               $guid = notags(unxmlify($data->guid));
-               $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));
+               $author = Strings::escapeTags(XML::unescape($data->author));
+               $guid = Strings::escapeTags(XML::unescape($data->guid));
+               $created_at = DateTimeFormat::utc(Strings::escapeTags(XML::unescape($data->created_at)));
+               $public = Strings::escapeTags(XML::unescape($data->public));
+               $text = XML::unescape($data->text);
+               $provider_display_name = Strings::escapeTags(XML::unescape($data->provider_display_name));
 
                $contact = self::allowedContactByHandle($importer, $author, false);
                if (!$contact) {
@@ -2809,7 +2858,7 @@ class Diaspora
                $address = [];
                if ($data->location) {
                        foreach ($data->location->children() as $fieldname => $data) {
-                               $address[$fieldname] = notags(unxmlify($data));
+                               $address[$fieldname] = Strings::escapeTags(XML::unescape($data));
                        }
                }
 
@@ -2820,8 +2869,8 @@ class Diaspora
                // Attach embedded pictures to the body
                if ($data->photo) {
                        foreach ($data->photo as $photo) {
-                               $body = "[img]".unxmlify($photo->remote_photo_path).
-                                       unxmlify($photo->remote_photo_name)."[/img]\n".$body;
+                               $body = "[img]".XML::unescape($photo->remote_photo_path).
+                                       XML::unescape($photo->remote_photo_name)."[/img]\n".$body;
                        }
 
                        $datarray["object-type"] = ACTIVITY_OBJ_IMAGE;
@@ -2845,7 +2894,7 @@ class Diaspora
 
                $datarray["uid"] = $importer["uid"];
                $datarray["contact-id"] = $contact["id"];
-               $datarray["network"] = NETWORK_DIASPORA;
+               $datarray["network"] = Protocol::DIASPORA;
 
                $datarray["author-link"] = $contact["url"];
                $datarray["author-id"] = Contact::getIdForURL($contact["url"], 0);
@@ -2886,7 +2935,7 @@ class Diaspora
                self::sendParticipation($contact, $datarray);
 
                if ($message_id) {
-                       logger("Stored item ".$datarray["guid"]." with message id ".$message_id, LOGGER_DEBUG);
+                       Logger::log("Stored item ".$datarray["guid"]." with message id ".$message_id, Logger::DEBUG);
                        if ($datarray['uid'] == 0) {
                                Item::distribute($message_id);
                        }
@@ -2906,6 +2955,7 @@ class Diaspora
         * @param array $contact contact array
         *
         * @return string the handle in the format user@domain.tld
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        private static function myHandle(array $contact)
        {
@@ -2935,14 +2985,15 @@ class Diaspora
         * @param string $pubkey  The public key of the receiver
         *
         * @return string The encrypted data
+        * @throws \Exception
         */
        public static function encodePrivateData($msg, array $user, array $contact, $prvkey, $pubkey)
        {
-               logger("Message: ".$msg, LOGGER_DATA);
+               Logger::log("Message: ".$msg, Logger::DATA);
 
                // without a public key nothing will work
                if (!$pubkey) {
-                       logger("pubkey missing: contact id: ".$contact["id"]);
+                       Logger::log("pubkey missing: contact id: ".$contact["id"]);
                        return false;
                }
 
@@ -2973,17 +3024,18 @@ class Diaspora
         * @param array  $user The record of the sender
         *
         * @return string The envelope
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function buildMagicEnvelope($msg, array $user)
        {
-               $b64url_data = base64url_encode($msg);
+               $b64url_data = Strings::base64UrlEncode($msg);
                $data = str_replace(["\n", "\r", " ", "\t"], ["", "", "", ""], $b64url_data);
 
-               $key_id = base64url_encode(self::myHandle($user));
+               $key_id = Strings::base64UrlEncode(self::myHandle($user));
                $type = "application/xml";
                $encoding = "base64url";
                $alg = "RSA-SHA256";
-               $signable_data = $data.".".base64url_encode($type).".".base64url_encode($encoding).".".base64url_encode($alg);
+               $signable_data = $data.".".Strings::base64UrlEncode($type).".".Strings::base64UrlEncode($encoding).".".Strings::base64UrlEncode($alg);
 
                // Fallback if the private key wasn't transmitted in the expected field
                if ($user['uprvkey'] == "") {
@@ -2991,7 +3043,7 @@ class Diaspora
                }
 
                $signature = Crypto::rsaSign($signable_data, $user["uprvkey"]);
-               $sig = base64url_encode($signature);
+               $sig = Strings::base64UrlEncode($signature);
 
                $xmldata = ["me:env" => ["me:data" => $data,
                                                        "@attributes" => ["type" => $type],
@@ -3016,6 +3068,7 @@ class Diaspora
         * @param bool   $public  Is the message public?
         *
         * @return string The message that will be transmitted to other servers
+        * @throws \Exception
         */
        public static function buildMessage($msg, array $user, array $contact, $prvkey, $pubkey, $public = false)
        {
@@ -3056,21 +3109,21 @@ class Diaspora
         * @param array  $contact      Target of the communication
         * @param string $envelope     The message that is to be transmitted
         * @param bool   $public_batch Is it a public post?
-        * @param bool   $queue_run    Is the transmission called from the queue?
         * @param string $guid         message guid
+        * @param bool   $no_defer     Don't defer a failing delivery
         *
         * @return int Result of the transmission
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
-       public static function transmit(array $owner, array $contact, $envelope, $public_batch, $queue_run = false, $guid = "", $no_queue = false)
+       public static function transmit(array $owner, array $contact, $envelope, $public_batch, $guid = "", $no_defer = false)
        {
-               $a = get_app();
-
                $enabled = intval(Config::get("system", "diaspora_enabled"));
                if (!$enabled) {
                        return 200;
                }
 
-               $logid = random_string(4);
+               $logid = Strings::getRandomHex(4);
 
                $dest_url = ($public_batch ? $contact["batch"] : $contact["notify"]);
 
@@ -3084,33 +3137,29 @@ class Diaspora
                }
 
                if (!$dest_url) {
-                       logger("no url for contact: ".$contact["id"]." batch mode =".$public_batch);
+                       Logger::log("no url for contact: ".$contact["id"]." batch mode =".$public_batch);
                        return 0;
                }
 
-               logger("transmit: ".$logid."-".$guid." ".$dest_url);
+               Logger::log("transmit: ".$logid."-".$guid." ".$dest_url);
 
-               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");
+               if (!intval(Config::get("system", "diaspora_test"))) {
+                       $content_type = (($public_batch) ? "application/magic-envelope+xml" : "application/json");
 
-                               Network::post($dest_url."/", $envelope, ["Content-Type: ".$content_type]);
-                               $return_code = $a->get_curl_code();
-                       } else {
-                               logger("test_mode");
-                               return 200;
-                       }
+                       $postResult = Network::post($dest_url."/", $envelope, ["Content-Type: ".$content_type]);
+                       $return_code = $postResult->getReturnCode();
+               } else {
+                       Logger::log("test_mode");
+                       return 200;
                }
 
-               logger("transmit: ".$logid."-".$guid." to ".$dest_url." returns: ".$return_code);
+               Logger::log("transmit: ".$logid."-".$guid." to ".$dest_url." returns: ".$return_code);
 
-               if (!$return_code || (($return_code == 503) && (stristr($a->get_curl_headers(), "retry-after")))) {
-                       if (!$no_queue && ($contact['contact-type'] != Contact::ACCOUNT_TYPE_RELAY)) {
-                               logger("queue message");
-                               // queue message for redelivery
-                               Queue::add($contact["id"], NETWORK_DIASPORA, $envelope, $public_batch, $guid);
+               if (!$return_code || (($return_code == 503) && (stristr($postResult->getHeader(), "retry-after")))) {
+                       if (!$no_defer && !empty($contact['contact-type']) && ($contact['contact-type'] != Contact::TYPE_RELAY)) {
+                               Logger::info('defer message', ['log' => $logid, 'guid' => $guid, 'destination' => $dest_url]);
+                               // defer message for redelivery
+                               Worker::defer();
                        }
 
                        // The message could not be delivered. We mark the contact as "dead"
@@ -3148,16 +3197,18 @@ class Diaspora
         * @param array  $message      The message data
         * @param bool   $public_batch Is it a public post?
         * @param string $guid         message guid
-        * @param bool   $spool        Should the transmission be spooled or transmitted?
+        * @param bool   $no_defer     Don't defer a failing delivery
         *
         * @return int Result of the transmission
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
-       private static function buildAndTransmit(array $owner, array $contact, $type, $message, $public_batch = false, $guid = "", $spool = false)
+       private static function buildAndTransmit(array $owner, array $contact, $type, $message, $public_batch = false, $guid = "", $no_defer = false)
        {
                $msg = self::buildPostXml($type, $message);
 
-               logger('message: '.$msg, LOGGER_DATA);
-               logger('send guid '.$guid, LOGGER_DEBUG);
+               Logger::log('message: '.$msg, Logger::DATA);
+               Logger::log('send guid '.$guid, Logger::DEBUG);
 
                // Fallback if the private key wasn't transmitted in the expected field
                if (empty($owner['uprvkey'])) {
@@ -3166,14 +3217,9 @@ class Diaspora
 
                $envelope = self::buildMessage($msg, $owner, $contact, $owner['uprvkey'], $contact['pubkey'], $public_batch);
 
-               if ($spool) {
-                       Queue::add($contact['id'], NETWORK_DIASPORA, $envelope, $public_batch, $guid);
-                       return true;
-               } else {
-                       $return_code = self::transmit($owner, $contact, $envelope, $public_batch, false, $guid);
-               }
+               $return_code = self::transmit($owner, $contact, $envelope, $public_batch, $guid, $no_defer);
 
-               logger("guid: ".$guid." result ".$return_code, LOGGER_DEBUG);
+               Logger::log("guid: ".$guid." result ".$return_code, Logger::DEBUG);
 
                return $return_code;
        }
@@ -3182,9 +3228,10 @@ class Diaspora
         * @brief sends a participation (Used to get all further updates)
         *
         * @param array $contact Target of the communication
-        * @param array $item    Item array
+        * @param array $item    Item array
         *
         * @return int The result of the transmission
+        * @throws \Exception
         */
        private static function sendParticipation(array $contact, array $item)
        {
@@ -3214,14 +3261,14 @@ class Diaspora
                $author = self::myHandle($owner);
 
                $message = ["author" => $author,
-                               "guid" => System::createGUID(32),
+                               "guid" => System::createUUID(),
                                "parent_type" => "Post",
                                "parent_guid" => $item["guid"]];
 
-               logger("Send participation for ".$item["guid"]." by ".$author, LOGGER_DEBUG);
+               Logger::log("Send participation for ".$item["guid"]." by ".$author, Logger::DEBUG);
 
                // It doesn't matter what we store, we only want to avoid sending repeated notifications for the same item
-               Cache::set($cachekey, $item["guid"], CACHE_QUARTER_HOUR);
+               Cache::set($cachekey, $item["guid"], Cache::QUARTER_HOUR);
 
                return self::buildAndTransmit($owner, $contact, "participation", $message);
        }
@@ -3231,9 +3278,11 @@ class Diaspora
         *
         * @param array $owner   the array of the item owner
         * @param array $contact Target of the communication
-        * @param int   $uid     User ID
+        * @param int   $uid     User ID
         *
         * @return int The result of the transmission
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        public static function sendAccountMigration(array $owner, array $contact, $uid)
        {
@@ -3247,7 +3296,7 @@ class Diaspora
                                "profile" => $profile,
                                "signature" => $signature];
 
-               logger("Send account migration ".print_r($message, true), LOGGER_DEBUG);
+               Logger::log("Send account migration ".print_r($message, true), Logger::DEBUG);
 
                return self::buildAndTransmit($owner, $contact, "account_migration", $message);
        }
@@ -3259,6 +3308,7 @@ class Diaspora
         * @param array $contact Target of the communication
         *
         * @return int The result of the transmission
+        * @throws \Exception
         */
        public static function sendShare(array $owner, array $contact)
        {
@@ -3290,7 +3340,7 @@ class Diaspora
                                "following" => "true",
                                "sharing" => "true"];
 
-               logger("Send share ".print_r($message, true), LOGGER_DEBUG);
+               Logger::log("Send share ".print_r($message, true), Logger::DEBUG);
 
                return self::buildAndTransmit($owner, $contact, "contact", $message);
        }
@@ -3302,6 +3352,7 @@ class Diaspora
         * @param array $contact Target of the communication
         *
         * @return int The result of the transmission
+        * @throws \Exception
         */
        public static function sendUnshare(array $owner, array $contact)
        {
@@ -3310,7 +3361,7 @@ class Diaspora
                                "following" => "false",
                                "sharing" => "false"];
 
-               logger("Send unshare ".print_r($message, true), LOGGER_DEBUG);
+               Logger::log("Send unshare ".print_r($message, true), Logger::DEBUG);
 
                return self::buildAndTransmit($owner, $contact, "contact", $message);
        }
@@ -3322,6 +3373,8 @@ class Diaspora
         * @param bool   $complete Should it be a complete check or a simple check?
         *
         * @return array|bool Reshare details or "false" if no reshare
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        public static function isReshare($body, $complete = true)
        {
@@ -3358,7 +3411,7 @@ class Diaspora
                }
 
                if (($guid != "") && $complete) {
-                       $condition = ['guid' => $guid, 'network' => [NETWORK_DFRN, NETWORK_DIASPORA]];
+                       $condition = ['guid' => $guid, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
                        $item = Item::selectFirst(['contact-id'], $condition);
                        if (DBA::isResult($item)) {
                                $ret= [];
@@ -3409,6 +3462,7 @@ class Diaspora
         * @param integer $event_id The id of the event
         *
         * @return array with event data
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        private static function buildEvent($event_id)
        {
@@ -3446,12 +3500,9 @@ class Diaspora
                /// @todo - establish "all day" events in Friendica
                $eventdata["all_day"] = "false";
 
-               if (!$event['adjust']) {
+               $eventdata['timezone'] = 'UTC';
+               if (!$event['adjust'] && $user['timezone']) {
                        $eventdata['timezone'] = $user['timezone'];
-
-                       if ($eventdata['timezone'] == "") {
-                               $eventdata['timezone'] = 'UTC';
-                       }
                }
 
                if ($event['start']) {
@@ -3494,6 +3545,8 @@ class Diaspora
         * @return array
         * 'type' -> Message type ("status_message" or "reshare")
         * 'message' -> Array of XML elements of the status
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        public static function buildStatus(array $item, array $owner)
        {
@@ -3506,7 +3559,7 @@ class Diaspora
 
                $myaddr = self::myHandle($owner);
 
-               $public = (($item["private"]) ? "false" : "true");
+               $public = ($item["private"] ? "false" : "true");
 
                $created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM);
 
@@ -3594,11 +3647,25 @@ class Diaspora
 
                $msg = ["type" => $type, "message" => $message];
 
-               Cache::set($cachekey, $msg, CACHE_QUARTER_HOUR);
+               Cache::set($cachekey, $msg, Cache::QUARTER_HOUR);
 
                return $msg;
        }
 
+       private static function prependParentAuthorMention($body, $profile_url)
+       {
+               $profile = Contact::getDetailsByURL($profile_url);
+               if (!empty($profile['addr'])
+                       && $profile['contact-type'] != Contact::TYPE_COMMUNITY
+                       && !strstr($body, $profile['addr'])
+                       && !strstr($body, $profile_url)
+               ) {
+                       $body = '@[url=' . $profile_url . ']' . $profile['name'] . '[/url] ' . $body;
+               }
+
+               return $body;
+       }
+
        /**
         * @brief Sends a post
         *
@@ -3608,6 +3675,8 @@ class Diaspora
         * @param bool  $public_batch Is it a public post?
         *
         * @return int The result of the transmission
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        public static function sendStatus(array $item, array $owner, array $contact, $public_batch = false)
        {
@@ -3623,6 +3692,7 @@ class Diaspora
         * @param array $owner the array of the item owner
         *
         * @return array The data for a "like"
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        private static function constructLike(array $item, array $owner)
        {
@@ -3654,6 +3724,7 @@ class Diaspora
         * @param array $owner the array of the item owner
         *
         * @return array The data for an "EventParticipation"
+        * @throws \Exception
         */
        private static function constructAttend(array $item, array $owner)
        {
@@ -3673,7 +3744,7 @@ class Diaspora
                                $attend_answer = 'tentative';
                                break;
                        default:
-                               logger('Unknown verb '.$item['verb'].' in item '.$item['guid']);
+                               Logger::log('Unknown verb '.$item['verb'].' in item '.$item['guid']);
                                return false;
                }
 
@@ -3690,7 +3761,8 @@ class Diaspora
         * @param array $item  The item that will be exported
         * @param array $owner the array of the item owner
         *
-        * @return array The data for a comment
+        * @return array|false The data for a comment
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        private static function constructComment(array $item, array $owner)
        {
@@ -3701,27 +3773,43 @@ class Diaspora
                        return $result;
                }
 
-               $parent = Item::selectFirst(['guid'], ['id' => $item["parent"], 'parent' => $item["parent"]]);
-               if (!DBA::isResult($parent)) {
+               $toplevel_item = Item::selectFirst(['guid', 'author-link'], ['id' => $item["parent"], 'parent' => $item["parent"]]);
+               if (!DBA::isResult($toplevel_item)) {
+                       Logger::error('Missing parent conversation item', ['parent' => $item["parent"]]);
                        return false;
                }
 
-               $text = html_entity_decode(BBCode::toMarkdown($item["body"]));
+               $thread_parent_item = $toplevel_item;
+               if ($item['thr-parent'] != $item['parent-uri']) {
+                       $thread_parent_item = Item::selectFirst(['guid', 'author-link'], ['uri' => $item['thr-parent'], 'uid' => $item['uid']]);
+               }
+
+               $body = $item["body"];
+
+               if ((empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions'))
+                       && !Config::get('system', 'disable_implicit_mentions')
+               ) {
+                       $body = self::prependParentAuthorMention($body, $thread_parent_item['author-link']);
+               }
+
+               $text = html_entity_decode(BBCode::toMarkdown($body));
                $created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM);
 
-               $comment = ["author" => self::myHandle($owner),
-                               "guid" => $item["guid"],
-                               "created_at" => $created,
-                               "parent_guid" => $parent["guid"],
-                               "text" => $text,
-                               "author_signature" => ""];
+               $comment = [
+                       "author"      => self::myHandle($owner),
+                       "guid"        => $item["guid"],
+                       "created_at"  => $created,
+                       "parent_guid" => $toplevel_item["guid"],
+                       "text"        => $text,
+                       "author_signature" => ""
+               ];
 
                // Send the thread parent guid only if it is a threaded comment
                if ($item['thr-parent'] != $item['parent-uri']) {
-                       $comment['thread_parent_guid'] = self::getGuidFromUri($item['thr-parent'], $item['uid']);
+                       $comment['thread_parent_guid'] = $thread_parent_item['guid'];
                }
 
-               Cache::set($cachekey, $comment, CACHE_QUARTER_HOUR);
+               Cache::set($cachekey, $comment, Cache::QUARTER_HOUR);
 
                return($comment);
        }
@@ -3735,6 +3823,8 @@ class Diaspora
         * @param bool  $public_batch Is it a public post?
         *
         * @return int The result of the transmission
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        public static function sendFollowup(array $item, array $owner, array $contact, $public_batch = false)
        {
@@ -3744,12 +3834,12 @@ class Diaspora
                } elseif (in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
                        $message = self::constructLike($item, $owner);
                        $type = "like";
-               } else {
+               } elseif (!in_array($item["verb"], [ACTIVITY_FOLLOW])) {
                        $message = self::constructComment($item, $owner);
                        $type = "comment";
                }
 
-               if (!$message) {
+               if (empty($message)) {
                        return false;
                }
 
@@ -3761,18 +3851,16 @@ class Diaspora
        /**
         * @brief Creates a message from a signature record entry
         *
-        * @param array $item      The item that will be exported
-        * @param array $signature The entry of the "sign" record
-        *
-        * @return string The message
+        * @param array $item The item that will be exported
+        * @return array The message
         */
-       private static function messageFromSignature(array $item, array $signature)
+       private static function messageFromSignature(array $item)
        {
                // Split the signed text
-               $signed_parts = explode(";", $signature['signed_text']);
+               $signed_parts = explode(";", $item['signed_text']);
 
                if ($item["deleted"]) {
-                       $message = ["author" => $signature['signer'],
+                       $message = ["author" => $item['signer'],
                                        "target_guid" => $signed_parts[0],
                                        "target_type" => $signed_parts[1]];
                } elseif (in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
@@ -3781,7 +3869,7 @@ class Diaspora
                                        "parent_guid" => $signed_parts[3],
                                        "parent_type" => $signed_parts[2],
                                        "positive" => $signed_parts[0],
-                                       "author_signature" => $signature['signature'],
+                                       "author_signature" => $item['signature'],
                                        "parent_author_signature" => ""];
                } else {
                        // Remove the comment guid
@@ -3793,15 +3881,14 @@ class Diaspora
                        // Remove the handle
                        $handle = array_pop($signed_parts);
 
-                       // Glue the parts together
-                       $text = implode(";", $signed_parts);
-
-                       $message = ["author" => $handle,
-                                       "guid" => $guid,
-                                       "parent_guid" => $parent_guid,
-                                       "text" => implode(";", $signed_parts),
-                                       "author_signature" => $signature['signature'],
-                                       "parent_author_signature" => ""];
+                       $message = [
+                               "author" => $handle,
+                               "guid" => $guid,
+                               "parent_guid" => $parent_guid,
+                               "text" => implode(";", $signed_parts),
+                               "author_signature" => $item['signature'],
+                               "parent_author_signature" => ""
+                       ];
                }
                return $message;
        }
@@ -3815,6 +3902,7 @@ class Diaspora
         * @param bool  $public_batch Is it a public post?
         *
         * @return int The result of the transmission
+        * @throws \Exception
         */
        public static function sendRelay(array $item, array $owner, array $contact, $public_batch = false)
        {
@@ -3826,28 +3914,14 @@ class Diaspora
                        $type = "comment";
                }
 
-               logger("Got relayable data ".$type." for item ".$item["guid"]." (".$item["id"].")", LOGGER_DEBUG);
-
-               // fetch the original signature
-
-               $r = q(
-                       "SELECT `signed_text`, `signature`, `signer` FROM `sign` WHERE `iid` = %d LIMIT 1",
-                       intval($item["id"])
-               );
-
-               if (!$r) {
-                       logger("Couldn't fetch signatur for item ".$item["guid"]." (".$item["id"].")", LOGGER_DEBUG);
-                       return false;
-               }
-
-               $signature = $r[0];
+               Logger::log("Got relayable data ".$type." for item ".$item["guid"]." (".$item["id"].")", Logger::DEBUG);
 
                // Old way - is used by the internal Friendica functions
                /// @todo Change all signatur storing functions to the new format
-               if ($signature['signed_text'] && $signature['signature'] && $signature['signer']) {
-                       $message = self::messageFromSignature($item, $signature);
+               if ($item['signed_text'] && $item['signature'] && $item['signer']) {
+                       $message = self::messageFromSignature($item);
                } else {// New way
-                       $msg = json_decode($signature['signed_text'], true);
+                       $msg = json_decode($item['signed_text'], true);
 
                        $message = [];
                        if (is_array($msg)) {
@@ -3864,13 +3938,13 @@ class Diaspora
                                        $message[$field] = $data;
                                }
                        } else {
-                               logger("Signature text for item ".$item["guid"]." (".$item["id"].") couldn't be extracted: ".$signature['signed_text'], LOGGER_DEBUG);
+                               Logger::log("Signature text for item ".$item["guid"]." (".$item["id"].") couldn't be extracted: ".$item['signed_text'], Logger::DEBUG);
                        }
                }
 
                $message["parent_author_signature"] = self::signature($owner, $message);
 
-               logger("Relayed data ".print_r($message, true), LOGGER_DEBUG);
+               Logger::log("Relayed data ".print_r($message, true), Logger::DEBUG);
 
                return self::buildAndTransmit($owner, $contact, $type, $message, $public_batch, $item["guid"]);
        }
@@ -3885,6 +3959,7 @@ class Diaspora
         * @param bool  $relay        Is the retraction transmitted from a relay?
         *
         * @return int The result of the transmission
+        * @throws \Exception
         */
        public static function sendRetraction(array $item, array $owner, array $contact, $public_batch = false, $relay = false)
        {
@@ -3904,7 +3979,7 @@ class Diaspora
                                "target_guid" => $item['guid'],
                                "target_type" => $target_type];
 
-               logger("Got message ".print_r($message, true), LOGGER_DEBUG);
+               Logger::log("Got message ".print_r($message, true), Logger::DEBUG);
 
                return self::buildAndTransmit($owner, $contact, $msg_type, $message, $public_batch, $item["guid"]);
        }
@@ -3917,30 +3992,18 @@ class Diaspora
         * @param array $contact Target of the communication
         *
         * @return int The result of the transmission
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        public static function sendMail(array $item, array $owner, array $contact)
        {
                $myaddr = self::myHandle($owner);
 
-               $r = q(
-                       "SELECT * FROM `conv` WHERE `id` = %d AND `uid` = %d LIMIT 1",
-                       intval($item["convid"]),
-                       intval($item["uid"])
-               );
-
-               if (!DBA::isResult($r)) {
-                       logger("conversation not found.");
+               $cnv = DBA::selectFirst('conv', [], ['id' => $item["convid"], 'uid' => $item["uid"]]);
+               if (!DBA::isResult($cnv)) {
+                       Logger::log("conversation not found.");
                        return;
                }
-               $cnv = $r[0];
-
-               $conv = [
-                       "author" => $cnv["creator"],
-                       "guid" => $cnv["guid"],
-                       "subject" => $cnv["subject"],
-                       "created_at" => DateTimeFormat::utc($cnv['created'], DateTimeFormat::ATOM),
-                       "participants" => $cnv["recips"]
-               ];
 
                $body = BBCode::toMarkdown($item["body"]);
                $created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM);
@@ -3958,12 +4021,13 @@ class Diaspora
                        $type = "message";
                } else {
                        $message = [
-                                       "author" => $cnv["creator"],
-                                       "guid" => $cnv["guid"],
-                                       "subject" => $cnv["subject"],
-                                       "created_at" => DateTimeFormat::utc($cnv['created'], DateTimeFormat::ATOM),
-                                       "participants" => $cnv["recips"],
-                                       "message" => $msg];
+                               "author" => $cnv["creator"],
+                               "guid" => $cnv["guid"],
+                               "subject" => $cnv["subject"],
+                               "created_at" => DateTimeFormat::utc($cnv['created'], DateTimeFormat::ATOM),
+                               "participants" => $cnv["recips"],
+                               "message" => $msg
+                       ];
 
                        $type = "conversation";
                }
@@ -4033,6 +4097,7 @@ class Diaspora
         * @param int $uid The user id
         *
         * @return array The profile data
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        private static function createProfileData($uid)
        {
@@ -4084,7 +4149,7 @@ class Diaspora
                        if ($profile['pub_keywords']) {
                                $kw = str_replace(',', ' ', $profile['pub_keywords']);
                                $kw = str_replace('  ', ' ', $kw);
-                               $arr = explode(' ', $profile['pub_keywords']);
+                               $arr = explode(' ', $kw);
                                if (count($arr)) {
                                        for ($x = 0; $x < 5; $x ++) {
                                                if (!empty($arr[$x])) {
@@ -4117,6 +4182,7 @@ class Diaspora
         * @param int  $uid    The user id
         * @param bool $recips optional, default false
         * @return void
+        * @throws \Exception
         */
        public static function sendProfile($uid, $recips = false)
        {
@@ -4133,7 +4199,7 @@ class Diaspora
                        $recips = q(
                                "SELECT `id`,`name`,`network`,`pubkey`,`notify` FROM `contact` WHERE `network` = '%s'
                                AND `uid` = %d AND `rel` != %d",
-                               DBA::escape(NETWORK_DIASPORA),
+                               DBA::escape(Protocol::DIASPORA),
                                intval($uid),
                                intval(Contact::SHARING)
                        );
@@ -4146,36 +4212,25 @@ class Diaspora
                $message = self::createProfileData($uid);
 
                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, "", false);
+                       Logger::log("Send updated profile data for user ".$uid." to contact ".$recip["id"], Logger::DEBUG);
+                       self::buildAndTransmit($owner, $recip, "profile", $message, false, '', true);
                }
        }
 
        /**
-        * @brief Stores the signature for likes that are created on our system
+        * @brief Creates the signature for likes that are created on our system
         *
-        * @param array $contact The contact array of the "like"
-        * @param int   $post_id The post id of the "like"
+        * @param integer $uid  The user of that comment
+        * @param array   $item Item array
         *
-        * @return bool Success
+        * @return array Signed content
+        * @throws \Exception
         */
-       public static function storeLikeSignature(array $contact, $post_id)
+       public static function createLikeSignature($uid, array $item)
        {
-               // Is the contact the owner? Then fetch the private key
-               if (!$contact['self'] || ($contact['uid'] == 0)) {
-                       logger("No owner post, so not storing signature", LOGGER_DEBUG);
-                       return false;
-               }
-
-               $r = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($contact['uid']));
-               if (!DBA::isResult($r)) {
-                       return false;
-               }
-
-               $contact["uprvkey"] = $r[0]['prvkey'];
-
-               $item = Item::selectFirst([], ['id' => $post_id]);
-               if (!DBA::isResult($item)) {
+               $owner = User::getOwnerDataById($uid);
+               if (empty($owner)) {
+                       Logger::log("No owner post, so not storing signature", Logger::DEBUG);
                        return false;
                }
 
@@ -4183,56 +4238,50 @@ class Diaspora
                        return false;
                }
 
-               $message = self::constructLike($item, $contact);
+               $message = self::constructLike($item, $owner);
                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', ['iid' => $post_id, 'signed_text' => json_encode($message)]);
+               $message["author_signature"] = self::signature($owner, $message);
 
-               logger('Stored diaspora like signature');
-               return true;
+               return $message;
        }
 
        /**
-        * @brief Stores the signature for comments that are created on our system
+        * @brief Creates the signature for Comments that are created on our system
         *
-        * @param array  $item       The item array of the comment
-        * @param array  $contact    The contact array of the item owner
-        * @param string $uprvkey    The private key of the sender
-        * @param int    $message_id The message id of the comment
+        * @param integer $uid  The user of that comment
+        * @param array   $item Item array
         *
-        * @return bool Success
+        * @return array Signed content
+        * @throws \Exception
         */
-       public static function storeCommentSignature(array $item, array $contact, $uprvkey, $message_id)
+       public static function createCommentSignature($uid, array $item)
        {
-               if ($uprvkey == "") {
-                       logger('No private key, so not storing comment signature', LOGGER_DEBUG);
+               $owner = User::getOwnerDataById($uid);
+               if (empty($owner)) {
+                       Logger::log("No owner post, so not storing signature", Logger::DEBUG);
                        return false;
                }
 
-               $contact["uprvkey"] = $uprvkey;
+               // This is a workaround for the behaviour of the "insert" function, see mod/item.php
+               $item['thr-parent'] = $item['parent-uri'];
 
-               $message = self::constructComment($item, $contact);
+               $parent = Item::selectFirst(['parent-uri'], ['uri' => $item['parent-uri']]);
+               if (!DBA::isResult($parent)) {
+                       return;
+               }
+
+               $item['parent-uri'] = $parent['parent-uri'];
+
+               $message = self::constructComment($item, $owner);
                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', ['iid' => $message_id, 'signed_text' => json_encode($message)]);
+               $message["author_signature"] = self::signature($owner, $message);
 
-               logger('Stored diaspora comment signature');
-               return true;
+               return $message;
        }
 }