]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/DFRN.php
The Diaspora transport layer is now the default for DFRN
[friendica.git] / src / Protocol / DFRN.php
index 808e8aaa2a652dde6665da0b8748fb70d3b2bca8..88025b88ca956be4d1247ac7c4f2b86251c230d5 100644 (file)
@@ -10,6 +10,8 @@ namespace Friendica\Protocol;
 
 use Friendica\App;
 use Friendica\Content\OEmbed;
+use Friendica\Content\Text\BBCode;
+use Friendica\Content\Text\HTML;
 use Friendica\Core\Addon;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
@@ -17,16 +19,19 @@ use Friendica\Core\System;
 use Friendica\Core\Worker;
 use Friendica\Database\DBM;
 use Friendica\Model\Contact;
+use Friendica\Model\Event;
 use Friendica\Model\GContact;
 use Friendica\Model\Group;
+use Friendica\Model\Item;
 use Friendica\Model\Profile;
-use Friendica\Model\Term;
 use Friendica\Model\User;
 use Friendica\Object\Image;
 use Friendica\Protocol\OStatus;
 use Friendica\Util\Crypto;
+use Friendica\Util\DateTimeFormat;
+use Friendica\Util\Network;
 use Friendica\Util\XML;
-
+use Friendica\Protocol\Diaspora;
 use dba;
 use DOMDocument;
 use DOMXPath;
@@ -36,13 +41,8 @@ use HTMLPurifier_Config;
 require_once 'boot.php';
 require_once 'include/dba.php';
 require_once "include/enotify.php";
-require_once "include/threads.php";
 require_once "include/items.php";
-require_once "include/tags.php";
-require_once "include/event.php";
 require_once "include/text.php";
-require_once "include/html2bbcode.php";
-require_once "include/bbcode.php";
 
 /**
  * @brief This class contain functions to create and send DFRN XML files
@@ -225,7 +225,7 @@ class DFRN
                        }
                }
 
-               $check_date = datetime_convert('UTC', 'UTC', $last_update, 'Y-m-d H:i:s');
+               $check_date = DateTimeFormat::utc($last_update);
 
                $r = q(
                        "SELECT `item`.*, `item`.`id` AS `item_id`,
@@ -417,7 +417,7 @@ class DFRN
 
                XML::addElement($doc, $mail, "dfrn:id", $item['uri']);
                XML::addElement($doc, $mail, "dfrn:in-reply-to", $item['parent-uri']);
-               XML::addElement($doc, $mail, "dfrn:sentdate", datetime_convert('UTC', 'UTC', $item['created'] . '+00:00', ATOM_TIME));
+               XML::addElement($doc, $mail, "dfrn:sentdate", DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM));
                XML::addElement($doc, $mail, "dfrn:subject", $item['title']);
                XML::addElement($doc, $mail, "dfrn:content", $item['body']);
 
@@ -583,7 +583,7 @@ class DFRN
 
                /// @todo We need a way to transmit the different page flags like "PAGE_PRVGROUP"
 
-               XML::addElement($doc, $root, "updated", datetime_convert("UTC", "UTC", "now", ATOM_TIME));
+               XML::addElement($doc, $root, "updated", DateTimeFormat::utcNow(DateTimeFormat::ATOM));
 
                $author = self::addAuthor($doc, $owner, $authorelement, $public);
                $root->appendChild($author);
@@ -618,9 +618,9 @@ class DFRN
 
                $author = $doc->createElement($authorelement);
 
-               $namdate = datetime_convert('UTC', 'UTC', $owner['name-date'].'+00:00', ATOM_TIME);
-               $uridate = datetime_convert('UTC', 'UTC', $owner['uri-date'].'+00:00', ATOM_TIME);
-               $picdate = datetime_convert('UTC', 'UTC', $owner['avatar-date'].'+00:00', ATOM_TIME);
+               $namdate = DateTimeFormat::utc($owner['name-date'].'+00:00', DateTimeFormat::ATOM);
+               $uridate = DateTimeFormat::utc($owner['uri-date'].'+00:00', DateTimeFormat::ATOM);
+               $picdate = DateTimeFormat::utc($owner['avatar-date'].'+00:00', DateTimeFormat::ATOM);
 
                $attributes = [];
 
@@ -792,7 +792,7 @@ class DFRN
                if ($activity) {
                        $entry = $doc->createElement($element);
 
-                       $r = parse_xml_string($activity, false);
+                       $r = XML::parseString($activity, false);
                        if (!$r) {
                                return false;
                        }
@@ -815,7 +815,7 @@ class DFRN
                                        $r->link = preg_replace('/\<link(.*?)\"\>/', '<link$1"/>', $r->link);
 
                                        // XML does need a single element as root element so we add a dummy element here
-                                       $data = parse_xml_string("<dummy>" . $r->link . "</dummy>", false);
+                                       $data = XML::parseString("<dummy>" . $r->link . "</dummy>", false);
                                        if (is_object($data)) {
                                                foreach ($data->link as $link) {
                                                        $attributes = [];
@@ -831,7 +831,7 @@ class DFRN
                                }
                        }
                        if ($r->content) {
-                               XML::addElement($doc, $entry, "content", bbcode($r->content), ["type" => "html"]);
+                               XML::addElement($doc, $entry, "content", BBCode::convert($r->content), ["type" => "html"]);
                        }
 
                        return $entry;
@@ -899,7 +899,7 @@ class DFRN
                }
 
                if ($item['deleted']) {
-                       $attributes = ["ref" => $item['uri'], "when" => datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00', ATOM_TIME)];
+                       $attributes = ["ref" => $item['uri'], "when" => DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM)];
                        return XML::createElement($doc, "at:deleted-entry", "", $attributes);
                }
 
@@ -921,14 +921,15 @@ class DFRN
                }
 
                if ($item['allow_cid'] || $item['allow_gid'] || $item['deny_cid'] || $item['deny_gid']) {
-                       $body = fix_private_photos($item['body'], $owner['uid'], $item, $cid);
+                       $body = Item::fixPrivatePhotos($item['body'], $owner['uid'], $item, $cid);
                } else {
                        $body = $item['body'];
                }
 
                // Remove the abstract element. It is only locally important.
-               $body = remove_abstract($body);
+               $body = BBCode::stripAbstract($body);
 
+               $htmlbody = '';
                if ($type == 'html') {
                        $htmlbody = $body;
 
@@ -936,7 +937,7 @@ class DFRN
                                $htmlbody = "[b]".$item['title']."[/b]\n\n".$htmlbody;
                        }
 
-                       $htmlbody = bbcode($htmlbody, false, false, 7);
+                       $htmlbody = BBCode::convert($htmlbody, false, 7);
                }
 
                $author = self::addEntryAuthor($doc, "author", $item["author-link"], $item);
@@ -979,8 +980,8 @@ class DFRN
                XML::addElement($doc, $entry, "id", $item["uri"]);
                XML::addElement($doc, $entry, "title", $item["title"]);
 
-               XML::addElement($doc, $entry, "published", datetime_convert("UTC", "UTC", $item["created"] . "+00:00", ATOM_TIME));
-               XML::addElement($doc, $entry, "updated", datetime_convert("UTC", "UTC", $item["edited"] . "+00:00", ATOM_TIME));
+               XML::addElement($doc, $entry, "published", DateTimeFormat::utc($item["created"] . "+00:00", DateTimeFormat::ATOM));
+               XML::addElement($doc, $entry, "updated", DateTimeFormat::utc($item["edited"] . "+00:00", DateTimeFormat::ATOM));
 
                // "dfrn:env" is used to read the content
                XML::addElement($doc, $entry, "dfrn:env", base64url_encode($body, true));
@@ -1058,7 +1059,7 @@ class DFRN
                        $entry->appendChild($actarg);
                }
 
-               $tags = item_getfeedtags($item);
+               $tags = Item::getFeedTags($item);
 
                if (count($tags)) {
                        foreach ($tags as $t) {
@@ -1145,13 +1146,20 @@ class DFRN
         * @param string $atom     Content that will be transmitted
         * @param bool   $dissolve (to be documented)
         *
-        * @return int Deliver status. -1 means an error.
+        * @return int Deliver status. Negative values mean an error.
         * @todo Add array type-hint for $owner, $contact
         */
        public static function deliver($owner, $contact, $atom, $dissolve = false)
        {
                $a = get_app();
 
+               // At first try the Diaspora transport layer
+               $ret = self::transmit($owner, $contact, $atom);
+               if ($ret >= 200) {
+                       logger('Delivery via Diaspora transport layer was successfull with status ' . $ret);
+                       return $ret;
+               }
+
                $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
 
                if ($contact['duplex'] && $contact['dfrn-id']) {
@@ -1186,7 +1194,7 @@ class DFRN
 
                logger('dfrn_deliver: ' . $url);
 
-               $ret = z_fetch_url($url);
+               $ret = Network::curl($url);
 
                if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
                        return -2; // timed out
@@ -1211,7 +1219,7 @@ class DFRN
                        return 3;
                }
 
-               $res = parse_xml_string($xml);
+               $res = XML::parseString($xml);
 
                if ((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id))) {
                        return (($res->status) ? $res->status : 3);
@@ -1332,7 +1340,7 @@ class DFRN
 
                logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars, true), LOGGER_DATA);
 
-               $xml = post_url($contact['notify'], $postvars);
+               $xml = Network::post($contact['notify'], $postvars);
 
                logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
 
@@ -1351,12 +1359,72 @@ class DFRN
                        return 3;
                }
 
-               if ($contact['term-date'] > NULL_DATE) {
-                       logger("dfrn_deliver: $url back from the dead - removing mark for death");
+               $res = XML::parseString($xml);
+
+               if (!isset($res->status)) {
+                       return -11;
+               }
+
+               if (!empty($res->message)) {
+                       logger('Transmit returned status '.$res->status.' - '.$res->message, LOGGER_DEBUG);
+               }
+
+               if ($res->status == 200) {
                        Contact::unmarkForArchival($contact);
                }
 
-               $res = parse_xml_string($xml);
+               return intval($res->status);
+       }
+
+       /**
+        * @brief Transmits atom content to the contacts via the Diaspora transport layer
+        *
+        * @param array  $owner    Owner record
+        * @param array  $contact  Contact record of the receiver
+        * @param string $atom     Content that will be transmitted
+        *
+        * @return int Deliver status. Negative values mean an error.
+        */
+       public static function transmit($owner, $contact, $atom)
+       {
+               $a = get_app();
+
+               // Currently disabled, at first we will not use the batch delivery
+               // $public_batch = !$items[0]['private'];
+               $public_batch = false;
+
+               $msg = DFRN::entries($items, $owner);
+
+               $fcontact = Diaspora::personByHandle($contact['addr']);
+               if (empty($fcontact)) {
+                       logger("unable to find contact details");
+                       return;
+               }
+
+               $envelope = Diaspora::buildMessage($msg, $owner, $contact, $owner['uprvkey'], $fcontact['pubkey'], $public_batch);
+
+               $dest_url = ($public_batch ? $fcontact["batch"] : $contact["notify"]);
+
+               $content_type = ($public_batch ? "application/magic-envelope+xml" : "application/json");
+
+               $xml = Network::post($dest_url, $envelope, ["Content-Type: ".$content_type]);
+
+               $curl_stat = $a->get_curl_code();
+               if (!$curl_stat || empty($xml)) {
+                       return -9; // timed out
+               }
+
+               if (($curl_stat == 503) && (stristr($a->get_curl_headers(), 'retry-after'))) {
+                       return -10;
+               }
+
+               if (strpos($xml, '<?xml') === false) {
+                       logger('no valid XML returned');
+                       logger('returned XML: ' . $xml, LOGGER_DATA);
+                       return 3;
+               }
+
+               $res = XML::parseString($xml);
 
                if (!isset($res->status)) {
                        return -11;
@@ -1366,6 +1434,10 @@ class DFRN
                        logger('Delivery returned status '.$res->status.' - '.$res->message, LOGGER_DEBUG);
                }
 
+               if ($res->status == 200) {
+                       Contact::unmarkForArchival($contact);
+               }
+
                return intval($res->status);
        }
 
@@ -1384,7 +1456,7 @@ class DFRN
                        "SELECT `id` FROM `event` WHERE `uid` = %d AND `cid` = %d AND `start` = '%s' AND `type` = '%s' LIMIT 1",
                        intval($contact["uid"]),
                        intval($contact["id"]),
-                       dbesc(datetime_convert("UTC", "UTC", $birthday)),
+                       dbesc(DateTimeFormat::utc($birthday)),
                        dbesc("birthday")
                );
 
@@ -1402,10 +1474,10 @@ class DFRN
                        VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s') ",
                        intval($contact["uid"]),
                        intval($contact["id"]),
-                       dbesc(datetime_convert()),
-                       dbesc(datetime_convert()),
-                       dbesc(datetime_convert("UTC", "UTC", $birthday)),
-                       dbesc(datetime_convert("UTC", "UTC", $birthday . " + 1 day ")),
+                       dbesc(DateTimeFormat::utcNow()),
+                       dbesc(DateTimeFormat::utcNow()),
+                       dbesc(DateTimeFormat::utc($birthday)),
+                       dbesc(DateTimeFormat::utc($birthday . " + 1 day ")),
                        dbesc($bdtext),
                        dbesc($bdtext2),
                        dbesc("birthday")
@@ -1431,22 +1503,20 @@ class DFRN
                $author["name"] = $xpath->evaluate($element."/atom:name/text()", $context)->item(0)->nodeValue;
                $author["link"] = $xpath->evaluate($element."/atom:uri/text()", $context)->item(0)->nodeValue;
 
-               $r = q(
-                       "SELECT `id`, `uid`, `url`, `network`, `avatar-date`, `name-date`, `uri-date`, `addr`,
+               $contact_old = dba::fetch_first("SELECT `id`, `uid`, `url`, `network`, `avatar-date`, `avatar`, `name-date`, `uri-date`, `addr`,
                                `name`, `nick`, `about`, `location`, `keywords`, `xmpp`, `bdyear`, `bd`, `hidden`, `contact-type`
-                               FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` != '%s'",
-                       intval($importer["uid"]),
-                       dbesc(normalise_link($author["link"])),
-                       dbesc(NETWORK_STATUSNET)
+                               FROM `contact` WHERE `uid` = ? AND `nurl` = ? AND `network` != ?",
+                       $importer["importer_uid"],
+                       normalise_link($author["link"]),
+                       NETWORK_STATUSNET
                );
 
-               if (DBM::is_result($r)) {
-                       $contact = $r[0];
-                       $author["contact-id"] = $r[0]["id"];
-                       $author["network"] = $r[0]["network"];
+               if (DBM::is_result($contact_old)) {
+                       $author["contact-id"] = $contact_old["id"];
+                       $author["network"] = $contact_old["network"];
                } else {
                        if (!$onlyfetch) {
-                               logger("Contact ".$author["link"]." wasn't found for user ".$importer["uid"]." XML: ".$xml, LOGGER_DEBUG);
+                               logger("Contact ".$author["link"]." wasn't found for user ".$importer["importer_uid"]." XML: ".$xml, LOGGER_DEBUG);
                        }
 
                        $author["contact-id"] = $importer["id"];
@@ -1470,7 +1540,7 @@ class DFRN
                                        $width = $attributes->textContent;
                                }
                                if ($attributes->name == "updated") {
-                                       $contact["avatar-date"] = $attributes->textContent;
+                                       $author["avatar-date"] = $attributes->textContent;
                                }
                        }
                        if (($width > 0) && ($href != "")) {
@@ -1482,10 +1552,10 @@ class DFRN
                        $author["avatar"] = current($avatarlist);
                }
 
-               if (DBM::is_result($r) && !$onlyfetch) {
-                       logger("Check if contact details for contact " . $r[0]["id"] . " (" . $r[0]["nick"] . ") have to be updated.", LOGGER_DEBUG);
+               if (DBM::is_result($contact_old) && !$onlyfetch) {
+                       logger("Check if contact details for contact " . $contact_old["id"] . " (" . $contact_old["nick"] . ") have to be updated.", LOGGER_DEBUG);
 
-                       $poco = ["url" => $contact["url"]];
+                       $poco = ["url" => $contact_old["url"]];
 
                        // When was the last change to name or uri?
                        $name_element = $xpath->query($element . "/atom:name", $context)->item(0);
@@ -1544,12 +1614,12 @@ class DFRN
                        // If the "hide" element is present then the profile isn't searchable.
                        $hide = intval($xpath->evaluate($element . "/dfrn:hide/text()", $context)->item(0)->nodeValue == "true");
 
-                       logger("Hidden status for contact " . $contact["url"] . ": " . $hide, LOGGER_DEBUG);
+                       logger("Hidden status for contact " . $contact_old["url"] . ": " . $hide, LOGGER_DEBUG);
 
                        // If the contact isn't searchable then set the contact to "hidden".
                        // Problem: This can be manually overridden by the user.
                        if ($hide) {
-                               $contact["hidden"] = true;
+                               $contact_old["hidden"] = true;
                        }
 
                        // Save the keywords into the contact table
@@ -1564,8 +1634,6 @@ class DFRN
                        }
 
                        // "dfrn:birthday" contains the birthday converted to UTC
-                       $old_bdyear = $contact["bdyear"];
-
                        $birthday = $xpath->evaluate($element . "/dfrn:birthday/text()", $context)->item(0)->nodeValue;
 
                        if (strtotime($birthday) > time()) {
@@ -1589,15 +1657,15 @@ class DFRN
                                $poco["bd"] = $value;
                        }
 
-                       $contact = array_merge($contact, $poco);
+                       $contact = array_merge($contact_old, $poco);
 
-                       if ($old_bdyear != $contact["bdyear"]) {
+                       if ($contact_old["bdyear"] != $contact["bdyear"]) {
                                self::birthdayEvent($contact, $birthday);
                        }
 
                        // Get all field names
                        $fields = [];
-                       foreach ($r[0] as $field => $data) {
+                       foreach ($contact_old as $field => $data) {
                                $fields[$field] = $data;
                        }
 
@@ -1605,21 +1673,23 @@ class DFRN
                        unset($fields["uid"]);
                        unset($fields["url"]);
                        unset($fields["avatar-date"]);
+                       unset($fields["avatar"]);
                        unset($fields["name-date"]);
                        unset($fields["uri-date"]);
 
+                       $update = false;
                        // Update check for this field has to be done differently
                        $datefields = ["name-date", "uri-date"];
                        foreach ($datefields as $field) {
-                               if (strtotime($contact[$field]) > strtotime($r[0][$field])) {
-                                       logger("Difference for contact " . $contact["id"] . " in field '" . $field . "'. New value: '" . $contact[$field] . "', old value '" . $r[0][$field] . "'", LOGGER_DEBUG);
+                               if (strtotime($contact[$field]) > strtotime($contact_old[$field])) {
+                                       logger("Difference for contact " . $contact["id"] . " in field '" . $field . "'. New value: '" . $contact[$field] . "', old value '" . $contact_old[$field] . "'", LOGGER_DEBUG);
                                        $update = true;
                                }
                        }
 
                        foreach ($fields as $field => $data) {
-                               if ($contact[$field] != $r[0][$field]) {
-                                       logger("Difference for contact " . $contact["id"] . " in field '" . $field . "'. New value: '" . $contact[$field] . "', old value '" . $r[0][$field] . "'", LOGGER_DEBUG);
+                               if ($contact[$field] != $contact_old[$field]) {
+                                       logger("Difference for contact " . $contact["id"] . " in field '" . $field . "'. New value: '" . $contact[$field] . "', old value '" . $contact_old[$field] . "'", LOGGER_DEBUG);
                                        $update = true;
                                }
                        }
@@ -1641,10 +1711,10 @@ class DFRN
                        }
 
                        Contact::updateAvatar(
-                               $author["avatar"],
-                               $importer["uid"],
-                               $contact["id"],
-                               (strtotime($contact["avatar-date"]) > strtotime($r[0]["avatar-date"]))
+                               $author['avatar'],
+                               $importer['importer_uid'],
+                               $contact['id'],
+                               (strtotime($contact['avatar-date']) > strtotime($contact_old['avatar-date']) || ($author['avatar'] != $contact_old['avatar']))
                        );
 
                        /*
@@ -1660,10 +1730,10 @@ class DFRN
                        $poco["contact-type"] = $contact["contact-type"];
                        $gcid = GContact::update($poco);
 
-                       GContact::link($gcid, $importer["uid"], $contact["id"]);
+                       GContact::link($gcid, $importer["importer_uid"], $contact["id"]);
                }
 
-               return($author);
+               return $author;
        }
 
        /**
@@ -1743,7 +1813,7 @@ class DFRN
                $msg["contact-id"] = $importer["id"];
                $msg["uri"] = $xpath->query("dfrn:id/text()", $mail)->item(0)->nodeValue;
                $msg["parent-uri"] = $xpath->query("dfrn:in-reply-to/text()", $mail)->item(0)->nodeValue;
-               $msg["created"] = $xpath->query("dfrn:sentdate/text()", $mail)->item(0)->nodeValue;
+               $msg["created"] = DateTimeFormat::utc($xpath->query("dfrn:sentdate/text()", $mail)->item(0)->nodeValue);
                $msg["title"] = $xpath->query("dfrn:subject/text()", $mail)->item(0)->nodeValue;
                $msg["body"] = $xpath->query("dfrn:content/text()", $mail)->item(0)->nodeValue;
                $msg["seen"] = 0;
@@ -1885,7 +1955,7 @@ class DFRN
                        intval($suggest["cid"]),
                        dbesc($suggest["body"]),
                        dbesc($hash),
-                       dbesc(datetime_convert()),
+                       dbesc(DateTimeFormat::utcNow()),
                        intval(0)
                );
 
@@ -2077,19 +2147,16 @@ class DFRN
 
                if (self::isEditedTimestampNewer($current, $item)) {
                        // do not accept (ignore) an earlier edit than one we currently have.
-                       if (datetime_convert("UTC", "UTC", $item["edited"]) < $current["edited"]) {
+                       if (DateTimeFormat::utc($item["edited"]) < $current["edited"]) {
                                return false;
                        }
 
-                       $fields = ['title' => $item["title"], 'body' => $item["body"],
-                                       'tag' => $item["tag"], 'changed' => datetime_convert(),
-                                       'edited' => datetime_convert("UTC", "UTC", $item["edited"])];
+                       $fields = ['title' => defaults($item, 'title', ''), 'body' => defaults($item, 'body', ''),
+                                       'tag' => defaults($item, 'tag', ''), 'changed' => DateTimeFormat::utcNow(),
+                                       'edited' => DateTimeFormat::utc($item["edited"])];
 
                        $condition = ["`uri` = ? AND `uid` IN (0, ?)", $item["uri"], $importer["importer_uid"]];
-                       dba::update('item', $fields, $condition);
-
-                       create_tags_from_itemuri($item["uri"], $importer["importer_uid"]);
-                       update_thread_uri($item["uri"], $importer["importer_uid"]);
+                       Item::update($fields, $condition);
 
                        $changed = true;
 
@@ -2187,7 +2254,7 @@ class DFRN
                if (!$verb) {
                        return;
                }
-               $xo = parse_xml_string($item["object"], false);
+               $xo = XML::parseString($item["object"], false);
 
                if (($xo->type == ACTIVITY_OBJ_PERSON) && ($xo->id)) {
                        // somebody was poked/prodded. Was it me?
@@ -2213,7 +2280,7 @@ class DFRN
                                        "to_email"     => $importer["email"],
                                        "uid"          => $importer["importer_uid"],
                                        "item"         => $item,
-                                       "link"         => System::baseUrl()."/display/".urlencode(get_item_guid($posted_id)),
+                                       "link"         => System::baseUrl()."/display/".urlencode(Item::getGuidById($posted_id)),
                                        "source_name"  => stripslashes($item["author-name"]),
                                        "source_link"  => $item["author-link"],
                                        "source_photo" => ((link_compare($item["author-link"], $importer["url"]))
@@ -2253,22 +2320,22 @@ class DFRN
                        // This function once was responsible for DFRN and OStatus.
                        if (activity_match($item["verb"], ACTIVITY_FOLLOW)) {
                                logger("New follower");
-                               new_follower($importer, $contact, $item, $nickname);
+                               Contact::addRelationship($importer, $contact, $item, $nickname);
                                return false;
                        }
                        if (activity_match($item["verb"], ACTIVITY_UNFOLLOW)) {
                                logger("Lost follower");
-                               lose_follower($importer, $contact, $item);
+                               Contact::removeFollower($importer, $contact, $item);
                                return false;
                        }
                        if (activity_match($item["verb"], ACTIVITY_REQ_FRIEND)) {
                                logger("New friend request");
-                               new_follower($importer, $contact, $item, $nickname, true);
+                               Contact::addRelationship($importer, $contact, $item, $nickname, true);
                                return false;
                        }
                        if (activity_match($item["verb"], ACTIVITY_UNFRIEND)) {
                                logger("Lost sharer");
-                               lose_sharer($importer, $contact, $item);
+                               Contact::removeSharer($importer, $contact, $item);
                                return false;
                        }
                } else {
@@ -2309,8 +2376,8 @@ class DFRN
                        }
 
                        if (($item["verb"] == ACTIVITY_TAG) && ($item["object-type"] == ACTIVITY_OBJ_TAGTERM)) {
-                               $xo = parse_xml_string($item["object"], false);
-                               $xt = parse_xml_string($item["target"], false);
+                               $xo = XML::parseString($item["object"], false);
+                               $xt = XML::parseString($item["target"], false);
 
                                if ($xt->type == ACTIVITY_OBJ_NOTE) {
                                        $r = q(
@@ -2326,13 +2393,9 @@ class DFRN
 
                                        // extract tag, if not duplicate, add to parent item
                                        if ($xo->content) {
-                                               if (!(stristr($r[0]["tag"], trim($xo->content)))) {
-                                                       q(
-                                                               "UPDATE `item` SET `tag` = '%s' WHERE `id` = %d",
-                                                               dbesc($r[0]["tag"] . (strlen($r[0]["tag"]) ? ',' : '') . '#[url=' . $xo->id . ']'. $xo->content . '[/url]'),
-                                                               intval($r[0]["id"])
-                                                       );
-                                                       create_tags_from_item($r[0]["id"]);
+                                               if (!stristr($r[0]["tag"], trim($xo->content))) {
+                                                       $tag = $r[0]["tag"] . (strlen($r[0]["tag"]) ? ',' : '') . '#[url=' . $xo->id . ']'. $xo->content . '[/url]';
+                                                       Item::update(['tag' => $tag], ['id' => $r[0]["id"]]);
                                                }
                                        }
                                }
@@ -2410,17 +2473,13 @@ class DFRN
 
                $item["edited"] = $xpath->query("atom:updated/text()", $entry)->item(0)->nodeValue;
 
-               $current = q(
-                       "SELECT `id`, `uid`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
-                       dbesc($item["uri"]),
-                       intval($importer["importer_uid"])
+               $current = dba::selectFirst('item',
+                       ['id', 'uid', 'edited', 'body'],
+                       ['uri' => $item["uri"], 'uid' => $importer["importer_uid"]]
                );
-
                // Is there an existing item?
-               if (DBM::is_result($current) && self::isEditedTimestampNewer($current[0], $item)
-                       && (datetime_convert("UTC", "UTC", $item["edited"]) < $current[0]["edited"])
-               ) {
-                       logger("Item ".$item["uri"]." already existed.", LOGGER_DEBUG);
+               if (DBM::is_result($current) && !self::isEditedTimestampNewer($current, $item)) {
+                       logger("Item ".$item["uri"]." (".$item['edited'].") already existed.", LOGGER_DEBUG);
                        return;
                }
 
@@ -2447,7 +2506,7 @@ class DFRN
                // make sure nobody is trying to sneak some html tags by us
                $item["body"] = notags(base64url_decode($item["body"]));
 
-               $item["body"] = limit_body_size($item["body"]);
+               $item["body"] = BBCode::limitBodySize($item["body"]);
 
                /// @todo Do we really need this check for HTML elements? (It was copied from the old function)
                if ((strpos($item['body'], '<') !== false) && (strpos($item['body'], '>') !== false)) {
@@ -2467,7 +2526,7 @@ class DFRN
                        $purifier = new HTMLPurifier($config);
                        $item['body'] = $purifier->purify($item['body']);
 
-                       $item['body'] = @html2bbcode($item['body']);
+                       $item['body'] = @HTML::toBBCode($item['body']);
                }
 
                /// @todo We should check for a repeated post and if we know the repeated author.
@@ -2501,7 +2560,7 @@ class DFRN
 
                $item["guid"] = $xpath->query("dfrn:diaspora_guid/text()", $entry)->item(0)->nodeValue;
 
-               // We store the data from "dfrn:diaspora_signature" in a different table, this is done in "item_store"
+               // We store the data from "dfrn:diaspora_signature" in a different table, this is done in "Item::insert"
                $dsprsig = unxmlify($xpath->query("dfrn:diaspora_signature/text()", $entry)->item(0)->nodeValue);
                if ($dsprsig != "") {
                        $item["dsprsig"] = $dsprsig;
@@ -2517,7 +2576,7 @@ class DFRN
                $item["object"] = self::transformActivity($xpath, $object, "object");
 
                if (trim($item["object"]) != "") {
-                       $r = parse_xml_string($item["object"], false);
+                       $r = XML::parseString($item["object"], false);
                        if (isset($r->type)) {
                                $item["object-type"] = $r->type;
                        }
@@ -2627,11 +2686,11 @@ class DFRN
                        // Is it an event?
                        if ($item["object-type"] == ACTIVITY_OBJ_EVENT) {
                                logger("Item ".$item["uri"]." seems to contain an event.", LOGGER_DEBUG);
-                               $ev = bbtoevent($item["body"]);
+                               $ev = Event::fromBBCode($item["body"]);
                                if ((x($ev, "desc") || x($ev, "summary")) && x($ev, "start")) {
                                        logger("Event in item ".$item["uri"]." was found.", LOGGER_DEBUG);
                                        $ev["cid"]     = $importer["id"];
-                                       $ev["uid"]     = $importer["uid"];
+                                       $ev["uid"]     = $importer["importer_uid"];
                                        $ev["uri"]     = $item["uri"];
                                        $ev["edited"]  = $item["edited"];
                                        $ev["private"] = $item["private"];
@@ -2640,13 +2699,13 @@ class DFRN
                                        $r = q(
                                                "SELECT `id` FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
                                                dbesc($item["uri"]),
-                                               intval($importer["uid"])
+                                               intval($importer["importer_uid"])
                                        );
                                        if (DBM::is_result($r)) {
                                                $ev["id"] = $r[0]["id"];
                                        }
 
-                                       $event_id = event_store($ev);
+                                       $event_id = Event::store($ev);
                                        logger("Event ".$event_id." was stored", LOGGER_DEBUG);
                                        return;
                                }
@@ -2660,7 +2719,7 @@ class DFRN
 
                // Update content if 'updated' changes
                if (DBM::is_result($current)) {
-                       if (self::updateContent($r[0], $item, $importer, $entrytype)) {
+                       if (self::updateContent($current, $item, $importer, $entrytype)) {
                                logger("Item ".$item["uri"]." was updated.", LOGGER_DEBUG);
                        } else {
                                logger("Item ".$item["uri"]." already existed.", LOGGER_DEBUG);
@@ -2669,7 +2728,7 @@ class DFRN
                }
 
                if (in_array($entrytype, [DFRN_REPLY, DFRN_REPLY_RC])) {
-                       $posted_id = item_store($item);
+                       $posted_id = Item::insert($item);
                        $parent = 0;
 
                        if ($posted_id) {
@@ -2695,11 +2754,19 @@ class DFRN
                                return true;
                        }
                } else { // $entrytype == DFRN_TOP_LEVEL
+                       if ($importer["readonly"]) {
+                               logger('ignoring read-only contact '.$importer["id"]);
+                               return;
+                       }
+                       if ($importer["uid"] == 0) {
+                               logger("Contact ".$importer["id"]." isn't known to user ".$importer["importer_uid"].". The post will be ignored.", LOGGER_DEBUG);
+                               return;
+                       }
                        if (!link_compare($item["owner-link"], $importer["url"])) {
                                /*
                                 * The item owner info is not our contact. It's OK and is to be expected if this is a tgroup delivery,
                                 * but otherwise there's a possible data mixup on the sender's system.
-                                * the tgroup delivery code called from item_store will correct it if it's a forum,
+                                * the tgroup delivery code called from Item::insert will correct it if it's a forum,
                                 * but we're going to unconditionally correct it here so that the post will always be owned by our contact.
                                 */
                                logger('Correcting item owner.', LOGGER_DEBUG);
@@ -2715,9 +2782,9 @@ class DFRN
 
                        // This is my contact on another system, but it's really me.
                        // Turn this into a wall post.
-                       $notify = item_is_remote_self($importer, $item);
+                       $notify = Item::isRemoteSelf($importer, $item);
 
-                       $posted_id = item_store($item, false, $notify);
+                       $posted_id = Item::insert($item, false, $notify);
 
                        logger("Item was stored with id ".$posted_id, LOGGER_DEBUG);
 
@@ -2739,126 +2806,54 @@ class DFRN
        private static function processDeletion($xpath, $deletion, $importer)
        {
                logger("Processing deletions");
-
+               $uri = null;
                foreach ($deletion->attributes as $attributes) {
                        if ($attributes->name == "ref") {
                                $uri = $attributes->textContent;
                        }
-                       if ($attributes->name == "when") {
-                               $when = $attributes->textContent;
-                       }
-               }
-               if ($when) {
-                       $when = datetime_convert("UTC", "UTC", $when, "Y-m-d H:i:s");
-               } else {
-                       $when = datetime_convert("UTC", "UTC", "now", "Y-m-d H:i:s");
                }
 
                if (!$uri || !$importer["id"]) {
                        return false;
                }
 
-               /// @todo Only select the used fields
-               $r = q(
-                       "SELECT `item`.*, `contact`.`self` FROM `item` INNER JOIN `contact` on `item`.`contact-id` = `contact`.`id`
-                       WHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d AND NOT `item`.`file` LIKE '%%[%%' LIMIT 1",
-                       dbesc($uri),
-                       intval($importer["uid"]),
-                       intval($importer["id"])
-               );
-               if (!DBM::is_result($r)) {
-                       logger("Item with uri " . $uri . " from contact " . $importer["id"] . " for user " . $importer["uid"] . " wasn't found.", LOGGER_DEBUG);
+               $condition = ["`uri` = ? AND `uid` = ? AND NOT `file` LIKE '%[%'", $uri, $importer["importer_uid"]];
+               $item = dba::selectFirst('item', ['id', 'parent', 'contact-id'], $condition);
+               if (!DBM::is_result($item)) {
+                       logger("Item with uri " . $uri . " for user " . $importer["importer_uid"] . " wasn't found.", LOGGER_DEBUG);
                        return;
-               } else {
-                       $item = $r[0];
+               }
 
-                       $entrytype = self::getEntryType($importer, $item);
+               // When it is a starting post it has to belong to the person that wants to delete it
+               if (($item['id'] == $item['parent']) && ($item['contact-id'] != $importer["id"])) {
+                       logger("Item with uri " . $uri . " don't belong to contact " . $importer["id"] . " - ignoring deletion.", LOGGER_DEBUG);
+                       return;
+               }
 
-                       if (!$item["deleted"]) {
-                               logger('deleting item '.$item["id"].' uri='.$uri, LOGGER_DEBUG);
-                       } else {
+               // Comments can be deleted by the thread owner or comment owner
+               if (($item['id'] != $item['parent']) && ($item['contact-id'] != $importer["id"])) {
+                       $condition = ['id' => $item['parent'], 'contact-id' => $importer["id"]];
+                       if (!dba::exists('item', $condition)) {
+                               logger("Item with uri " . $uri . " wasn't found or mustn't be deleted by contact " . $importer["id"] . " - ignoring deletion.", LOGGER_DEBUG);
                                return;
                        }
+               }
 
-                       if ($item["object-type"] == ACTIVITY_OBJ_EVENT) {
-                               logger("Deleting event ".$item["event-id"], LOGGER_DEBUG);
-                               event_delete($item["event-id"]);
-                       }
-
-                       if (($item["verb"] == ACTIVITY_TAG) && ($item["object-type"] == ACTIVITY_OBJ_TAGTERM)) {
-                               $xo = parse_xml_string($item["object"], false);
-                               $xt = parse_xml_string($item["target"], false);
-
-                               if ($xt->type == ACTIVITY_OBJ_NOTE) {
-                                       $i = q(
-                                               "SELECT `id`, `contact-id`, `tag` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
-                                               dbesc($xt->id),
-                                               intval($importer["importer_uid"])
-                                       );
-                                       if (DBM::is_result($i)) {
-                                               // For tags, the owner cannot remove the tag on the author's copy of the post.
-
-                                               $owner_remove = (($item["contact-id"] == $i[0]["contact-id"]) ? true: false);
-                                               $author_remove = (($item["origin"] && $item["self"]) ? true : false);
-                                               $author_copy = (($item["origin"]) ? true : false);
-
-                                               if ($owner_remove && $author_copy) {
-                                                       return;
-                                               }
-                                               if ($author_remove || $owner_remove) {
-                                                       $tags = explode(',', $i[0]["tag"]);
-                                                       $newtags = [];
-                                                       if (count($tags)) {
-                                                               foreach ($tags as $tag) {
-                                                                       if (trim($tag) !== trim($xo->body)) {
-                                                                               $newtags[] = trim($tag);
-                                                                       }
-                                                               }
-                                                       }
-                                                       q(
-                                                               "UPDATE `item` SET `tag` = '%s' WHERE `id` = %d",
-                                                               dbesc(implode(',', $newtags)),
-                                                               intval($i[0]["id"])
-                                                       );
-                                                       create_tags_from_item($i[0]["id"]);
-                                               }
-                                       }
-                               }
-                       }
+               $entrytype = self::getEntryType($importer, $item);
 
-                       if ($entrytype == DFRN_TOP_LEVEL) {
-                               $r = q(
-                                       "UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
-                                               `body` = '', `title` = ''
-                                       WHERE `parent-uri` = '%s' AND `uid` IN (0, %d)",
-                                       dbesc($when),
-                                       dbesc(datetime_convert()),
-                                       dbesc($uri),
-                                       intval($importer["uid"])
-                               );
-                               create_tags_from_itemuri($uri, $importer["uid"]);
-                               Term::createFromItemURI($uri, $importer["uid"]);
-                               update_thread_uri($uri, $importer["uid"]);
-                       } else {
-                               $r = q(
-                                       "UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
-                                               `body` = '', `title` = ''
-                                       WHERE `uri` = '%s' AND `uid` IN (0, %d)",
-                                       dbesc($when),
-                                       dbesc(datetime_convert()),
-                                       dbesc($uri),
-                                       intval($importer["uid"])
-                               );
-                               create_tags_from_itemuri($uri, $importer["uid"]);
-                               Term::createFromItemURI($uri, $importer["uid"]);
-                               update_thread_uri($uri, $importer["importer_uid"]);
+               if (!$item["deleted"]) {
+                       logger('deleting item '.$item["id"].' uri='.$uri, LOGGER_DEBUG);
+               } else {
+                       return;
+               }
 
-                               // if this is a relayed delete, propagate it to other recipients
+               Item::deleteById($item["id"]);
 
-                               if ($entrytype == DFRN_REPLY_RC) {
-                                       logger("Notifying followers about deletion of post " . $item["id"], LOGGER_DEBUG);
-                                       Worker::add(PRIORITY_HIGH, "Notifier", "drop", $item["id"]);
-                               }
+               if ($entrytype != DFRN_TOP_LEVEL) {
+                       // if this is a relayed delete, propagate it to other recipients
+                       if ($entrytype == DFRN_REPLY_RC) {
+                               logger("Notifying followers about deletion of post " . $item["id"], LOGGER_DEBUG);
+                               Worker::add(PRIORITY_HIGH, "Notifier", "drop", $item["id"]);
                        }
                }
        }
@@ -2894,7 +2889,7 @@ class DFRN
                $xpath->registerNamespace("statusnet", NAMESPACE_STATUSNET);
 
                $header = [];
-               $header["uid"] = $importer["uid"];
+               $header["uid"] = $importer["importer_uid"];
                $header["network"] = NETWORK_DFRN;
                $header["type"] = "remote";
                $header["wall"] = 0;
@@ -2913,7 +2908,7 @@ class DFRN
                        self::fetchauthor($xpath, $doc->firstChild, $importer, "dfrn:owner", false, $xml);
                }
 
-               logger("Import DFRN message for user " . $importer["uid"] . " from contact " . $importer["id"], LOGGER_DEBUG);
+               logger("Import DFRN message for user " . $importer["importer_uid"] . " from contact " . $importer["id"], LOGGER_DEBUG);
 
                // The account type is new since 3.5.1
                if ($xpath->query("/atom:feed/dfrn:account_type")->length > 0) {
@@ -2939,21 +2934,16 @@ class DFRN
                        self::processRelocation($xpath, $relocation, $importer);
                }
 
-               if ($importer["readonly"]) {
-                       // We aren't receiving stuff from this person. But we will quietly ignore them
-                       // rather than a blatant "go away" message.
-                       logger('ignoring contact '.$importer["id"]);
-                       return 403;
-               }
-
-               $mails = $xpath->query("/atom:feed/dfrn:mail");
-               foreach ($mails as $mail) {
-                       self::processMail($xpath, $mail, $importer);
-               }
+               if (($importer["uid"] != 0) && !$importer["readonly"]) {
+                       $mails = $xpath->query("/atom:feed/dfrn:mail");
+                       foreach ($mails as $mail) {
+                               self::processMail($xpath, $mail, $importer);
+                       }
 
-               $suggestions = $xpath->query("/atom:feed/dfrn:suggest");
-               foreach ($suggestions as $suggestion) {
-                       self::processSuggestion($xpath, $suggestion, $importer);
+                       $suggestions = $xpath->query("/atom:feed/dfrn:suggest");
+                       foreach ($suggestions as $suggestion) {
+                               self::processSuggestion($xpath, $suggestion, $importer);
+                       }
                }
 
                $deletions = $xpath->query("/atom:feed/at:deleted-entry");
@@ -2981,7 +2971,7 @@ class DFRN
                                self::processEntry($header, $xpath, $entry, $importer, $xml);
                        }
                }
-               logger("Import done for user " . $importer["uid"] . " from contact " . $importer["id"], LOGGER_DEBUG);
+               logger("Import done for user " . $importer["importer_uid"] . " from contact " . $importer["id"], LOGGER_DEBUG);
                return 200;
        }
 
@@ -3153,8 +3143,8 @@ class DFRN
                        return false;
                }
 
-               $existing_edited = datetime_convert('UTC', 'UTC', $existing['edited']);
-               $update_edited = datetime_convert('UTC', 'UTC', $update['edited']);
+               $existing_edited = DateTimeFormat::utc($existing['edited']);
+               $update_edited = DateTimeFormat::utc($update['edited']);
 
                return (strcmp($existing_edited, $update_edited) < 0);
        }