]> git.mxchange.org Git - friendica.git/blobdiff - include/dfrn.php
Merge remote-tracking branch 'upstream/develop' into 1702-null-date
[friendica.git] / include / dfrn.php
index 2dca1522c62da7b10af55c49c47de5c44111f7fd..39372aef14e603d3d3486c9d8d15a83a9d2f14a4 100644 (file)
@@ -3,7 +3,8 @@
  * @file include/dfrn.php
  * @brief The implementation of the dfrn protocol
  *
- * https://github.com/friendica/friendica/wiki/Protocol
+ * @see https://github.com/friendica/friendica/wiki/Protocol and
+ * https://github.com/friendica/friendica/blob/master/spec/dfrn2.pdf
  */
 
 require_once("include/Contact.php");
@@ -98,21 +99,21 @@ class dfrn {
 
                $sql_extra = " AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = '' ";
 
-               $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`
+               $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`, `user`.`account-type`
                        FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
-                       WHERE `contact`.`self` = 1 AND `user`.`nickname` = '%s' LIMIT 1",
+                       WHERE `contact`.`self` AND `user`.`nickname` = '%s' LIMIT 1",
                        dbesc($owner_nick)
                );
 
-               if(! count($r))
+               if (! dbm::is_result($r)) {
                        killme();
+               }
 
                $owner = $r[0];
                $owner_id = $owner['uid'];
                $owner_nick = $owner['nickname'];
 
                $sql_post_table = "";
-               $visibility = "";
 
                if(! $public_feed) {
 
@@ -135,12 +136,13 @@ class dfrn {
                                        break; // NOTREACHED
                        }
 
-                       $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
+                       $r = q("SELECT * FROM `contact` WHERE NOT `blocked` AND `contact`.`uid` = %d $sql_extra LIMIT 1",
                                intval($owner_id)
                        );
 
-                       if(! count($r))
+                       if (! dbm::is_result($r)) {
                                killme();
+                       }
 
                        $contact = $r[0];
                        require_once('include/security.php');
@@ -171,9 +173,6 @@ class dfrn {
                else
                        $sort = 'ASC';
 
-               $date_field = "`changed`";
-               $sql_order = "`item`.`parent` ".$sort.", `item`.`created` ASC";
-
                if(! strlen($last_update))
                        $last_update = 'now -30 days';
 
@@ -190,22 +189,19 @@ class dfrn {
 
                $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
 
-               //      AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
-               //      dbesc($check_date),
-
-               $r = q("SELECT STRAIGHT_JOIN `item`.*, `item`.`id` AS `item_id`,
+               $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
                        `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`,
                        `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
                        `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
                        `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
-                       FROM `item` $sql_post_table
-                       INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
-                       AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
+                       FROM `item` USE INDEX (`uid_wall_changed`) $sql_post_table
+                       STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
+                       AND (NOT `contact`.`blocked` OR `contact`.`pending`)
                        LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
-                       WHERE `item`.`uid` = %d AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`parent` != 0
-                       AND ((`item`.`wall` = 1) $visibility) AND `item`.$date_field > '%s'
+                       WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`moderated` AND `item`.`parent` != 0
+                       AND `item`.`wall` AND `item`.`changed` > '%s'
                        $sql_extra
-                       ORDER BY $sql_order LIMIT 0, 300",
+                       ORDER BY `item`.`parent` ".$sort.", `item`.`created` ASC LIMIT 0, 300",
                        intval($owner_id),
                        dbesc($check_date),
                        dbesc($sort)
@@ -368,6 +364,8 @@ class dfrn {
 
                xml::add_element($doc, $relocate, "dfrn:url", $owner['url']);
                xml::add_element($doc, $relocate, "dfrn:name", $owner['name']);
+               xml::add_element($doc, $relocate, "dfrn:addr", $owner['addr']);
+               xml::add_element($doc, $relocate, "dfrn:avatar", $owner['avatar']);
                xml::add_element($doc, $relocate, "dfrn:photo", $photos[4]);
                xml::add_element($doc, $relocate, "dfrn:thumb", $photos[5]);
                xml::add_element($doc, $relocate, "dfrn:micro", $photos[6]);
@@ -393,7 +391,7 @@ class dfrn {
         *
         * @return object XML root object
         */
-       private function add_header($doc, $owner, $authorelement, $alternatelink = "", $public = false) {
+       private static function add_header($doc, $owner, $authorelement, $alternatelink = "", $public = false) {
 
                if ($alternatelink == "")
                        $alternatelink = $owner['url'];
@@ -438,9 +436,13 @@ class dfrn {
                        xml::add_element($doc, $root, "link", "", $attributes);
                }
 
+               // For backward compatibility we keep this element
                if ($owner['page-flags'] == PAGE_COMMUNITY)
                        xml::add_element($doc, $root, "dfrn:community", 1);
 
+               // The former element is replaced by this one
+               xml::add_element($doc, $root, "dfrn:account_type", $owner["account-type"]);
+
                /// @todo We need a way to transmit the different page flags like "PAGE_PRVGROUP"
 
                xml::add_element($doc, $root, "updated", datetime_convert("UTC", "UTC", "now", ATOM_TIME));
@@ -460,7 +462,7 @@ class dfrn {
         *
         * @return object XML author object
         */
-       private function add_author($doc, $owner, $authorelement, $public) {
+       private static function add_author($doc, $owner, $authorelement, $public) {
 
                // Is the profile hidden or shouldn't be published in the net? Then add the "hide" element
                $r = q("SELECT `id` FROM `profile` INNER JOIN `user` ON `user`.`uid` = `profile`.`uid`
@@ -510,14 +512,16 @@ class dfrn {
                        xml::add_element($doc, $author, "dfrn:birthday", $birthday);
 
                // Only show contact details when we are allowed to
-               $r = q("SELECT `profile`.`about`, `profile`.`name`, `profile`.`homepage`, `user`.`nickname`, `user`.`timezone`,
-                               `profile`.`locality`, `profile`.`region`, `profile`.`country-name`, `profile`.`pub_keywords`, `profile`.`dob`
+               $r = q("SELECT `profile`.`about`, `profile`.`name`, `profile`.`homepage`, `user`.`nickname`,
+                               `user`.`timezone`, `profile`.`locality`, `profile`.`region`, `profile`.`country-name`,
+                               `profile`.`pub_keywords`, `profile`.`xmpp`, `profile`.`dob`
                        FROM `profile`
                                INNER JOIN `user` ON `user`.`uid` = `profile`.`uid`
                                WHERE `profile`.`is-default` AND NOT `user`.`hidewall` AND `user`.`uid` = %d",
                        intval($owner['uid']));
                if ($r) {
                        $profile = $r[0];
+
                        xml::add_element($doc, $author, "poco:displayName", $profile["name"]);
                        xml::add_element($doc, $author, "poco:updated", $namdate);
 
@@ -548,12 +552,10 @@ class dfrn {
 
                        }
 
-                       /// @todo When we are having the XMPP address in the profile we should propagate it here
-                       $xmpp = "";
-                       if (trim($xmpp) != "") {
+                       if (trim($profile["xmpp"]) != "") {
                                $ims = $doc->createElement("poco:ims");
                                xml::add_element($doc, $ims, "poco:type", "xmpp");
-                               xml::add_element($doc, $ims, "poco:value", $xmpp);
+                               xml::add_element($doc, $ims, "poco:value", $profile["xmpp"]);
                                xml::add_element($doc, $ims, "poco:primary", "true");
                                $author->appendChild($ims);
                        }
@@ -589,7 +591,7 @@ class dfrn {
         *
         * @return object XML author object
         */
-       private function add_entry_author($doc, $element, $contact_url, $item) {
+       private static function add_entry_author($doc, $element, $contact_url, $item) {
 
                $contact = get_contact_details_by_url($contact_url, $item["uid"]);
 
@@ -629,7 +631,7 @@ class dfrn {
         *
         * @return object XML activity object
         */
-       private function create_activity($doc, $element, $activity) {
+       private static function create_activity($doc, $element, $activity) {
 
                if($activity) {
                        $entry = $doc->createElement($element);
@@ -683,7 +685,7 @@ class dfrn {
         *
         * @return object XML attachment object
         */
-       private function get_attachment($doc, $root, $item) {
+       private static function get_attachment($doc, $root, $item) {
                $arr = explode('[/attach],',$item['attach']);
                if(count($arr)) {
                        foreach($arr as $r) {
@@ -718,7 +720,7 @@ class dfrn {
         *
         * @return object XML entry object
         */
-       private function entry($doc, $type, $item, $owner, $comment = false, $cid = 0) {
+       private static function entry($doc, $type, $item, $owner, $comment = false, $cid = 0) {
 
                $mentioned = array();
 
@@ -911,11 +913,18 @@ class dfrn {
 
                logger('dfrn_deliver: ' . $url);
 
-               $xml = fetch_url($url);
+               $ret = z_fetch_url($url);
+
+               if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
+                       return -2; // timed out
+               }
+
+               $xml = $ret['body'];
 
                $curl_stat = $a->get_curl_code();
-               if(! $curl_stat)
-                       return(-1); // timed out
+               if (!$curl_stat) {
+                       return -3; // timed out
+               }
 
                logger('dfrn_deliver: ' . $xml, LOGGER_DATA);
 
@@ -1015,24 +1024,24 @@ class dfrn {
                                                $key = Crypto::createNewRandomKey();
                                        } catch (CryptoTestFailed $ex) {
                                                logger('Cannot safely create a key');
-                                               return -1;
+                                               return -4;
                                        } catch (CannotPerformOperation $ex) {
                                                logger('Cannot safely create a key');
-                                               return -1;
+                                               return -5;
                                        }
                                        try {
                                                $data = Crypto::encrypt($postvars['data'], $key);
                                        } catch (CryptoTestFailed $ex) {
                                                logger('Cannot safely perform encryption');
-                                               return -1;
+                                               return -6;
                                        } catch (CannotPerformOperation $ex) {
                                                logger('Cannot safely perform encryption');
-                                               return -1;
+                                               return -7;
                                        }
                                        break;
                                default:
                                        logger("rino: invalid requested verision '$rino_remote_version'");
-                                       return -1;
+                                       return -8;
                        }
 
                        $postvars['rino'] = $rino_remote_version;
@@ -1066,16 +1075,18 @@ class dfrn {
 
                logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);
 
-               $xml = post_url($contact['notify'],$postvars);
+               $xml = post_url($contact['notify'], $postvars);
 
                logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
 
                $curl_stat = $a->get_curl_code();
-               if((! $curl_stat) || (! strlen($xml)))
-                       return(-1); // timed out
+               if ((!$curl_stat) || (!strlen($xml))) {
+                       return -9; // timed out
+               }
 
-               if(($curl_stat == 503) && (stristr($a->get_curl_headers(),'retry-after')))
-                       return(-1);
+               if (($curl_stat == 503) && (stristr($a->get_curl_headers(),'retry-after'))) {
+                       return -10;
+               }
 
                if(strpos($xml,'<?xml') === false) {
                        logger('dfrn_deliver: phase 2: no valid XML returned');
@@ -1101,14 +1112,24 @@ class dfrn {
         * @param string $birthday Birthday of the contact
         *
         */
-       private function birthday_event($contact, $birthday) {
+       private static function birthday_event($contact, $birthday) {
+
+               // Check for duplicates
+               $r = q("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("birthday"));
+
+               if (dbm::is_result($r)) {
+                       return;
+               }
 
                logger("updating birthday: ".$birthday." for contact ".$contact["id"]);
 
                $bdtext = sprintf(t("%s\'s birthday"), $contact["name"]);
                $bdtext2 = sprintf(t("Happy Birthday %s"), " [url=".$contact["url"]."]".$contact["name"]."[/url]") ;
 
-
                $r = q("INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`summary`,`desc`,`type`)
                        VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s') ",
                        intval($contact["uid"]),
@@ -1134,14 +1155,14 @@ class dfrn {
         *
         * @return Returns an array with relevant data of the author
         */
-       private function fetchauthor($xpath, $context, $importer, $element, $onlyfetch, $xml = "") {
+       private static function fetchauthor($xpath, $context, $importer, $element, $onlyfetch, $xml = "") {
 
                $author = array();
                $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`,
-                               `name`, `nick`, `about`, `location`, `keywords`, `bdyear`, `bd`, `hidden`
+                               `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));
                if ($r) {
@@ -1159,7 +1180,7 @@ class dfrn {
 
                // Until now we aren't serving different sizes - but maybe later
                $avatarlist = array();
-               // @todo check if "avatar" or "photo" would be the best field in the specification
+               /// @todo check if "avatar" or "photo" would be the best field in the specification
                $avatars = $xpath->query($element."/atom:link[@rel='avatar']", $context);
                foreach($avatars AS $avatar) {
                        $href = "";
@@ -1217,9 +1238,13 @@ class dfrn {
                        if ($value != "")
                                $poco["location"] = $value;
 
+                       /// @todo Only search for elements with "poco:type" = "xmpp"
+                       $value = $xpath->evaluate($element."/poco:ims/poco:value/text()", $context)->item(0)->nodeValue;
+                       if ($value != "")
+                               $poco["xmpp"] = $value;
+
                        /// @todo Add support for the following fields that we don't support by now in the contact table:
                        /// - poco:utcOffset
-                       /// - poco:ims
                        /// - poco:urls
                        /// - poco:locality
                        /// - poco:region
@@ -1306,12 +1331,13 @@ class dfrn {
 
                                q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `about` = '%s', `location` = '%s',
                                        `addr` = '%s', `keywords` = '%s', `bdyear` = '%s', `bd` = '%s', `hidden` = %d,
-                                       `name-date`  = '%s', `uri-date` = '%s'
+                                       `xmpp` = '%s', `name-date`  = '%s', `uri-date` = '%s'
                                        WHERE `id` = %d AND `network` = '%s'",
                                        dbesc($contact["name"]), dbesc($contact["nick"]), dbesc($contact["about"]), dbesc($contact["location"]),
                                        dbesc($contact["addr"]), dbesc($contact["keywords"]), dbesc($contact["bdyear"]),
-                                       dbesc($contact["bd"]), intval($contact["hidden"]), dbesc($contact["name-date"]),
-                                       dbesc($contact["uri-date"]), intval($contact["id"]), dbesc($contact["network"]));
+                                       dbesc($contact["bd"]), intval($contact["hidden"]), dbesc($contact["xmpp"]),
+                                       dbesc($contact["name-date"]), dbesc($contact["uri-date"]),
+                                       intval($contact["id"]), dbesc($contact["network"]));
                        }
 
                        update_contact_avatar($author["avatar"], $importer["uid"], $contact["id"],
@@ -1325,6 +1351,7 @@ class dfrn {
                        $poco["generation"] = 2;
                        $poco["photo"] = $author["avatar"];
                        $poco["hide"] = $hide;
+                       $poco["contact-type"] = $contact["contact-type"];
                        update_gcontact($poco);
                }
 
@@ -1340,7 +1367,7 @@ class dfrn {
         *
         * @return string XML string
         */
-       private function transform_activity($xpath, $activity, $element) {
+       private static function transform_activity($xpath, $activity, $element) {
                if (!is_object($activity))
                        return "";
 
@@ -1373,7 +1400,7 @@ class dfrn {
 
                $objxml = $obj_doc->saveXML($obj_element);
 
-               // @todo This isn't totally clean. We should find a way to transform the namespaces
+               /// @todo This isn't totally clean. We should find a way to transform the namespaces
                $objxml = str_replace("<".$element.' xmlns="http://www.w3.org/2005/Atom">', "<".$element.">", $objxml);
                return($objxml);
        }
@@ -1385,7 +1412,7 @@ class dfrn {
         * @param object $mail mail elements
         * @param array $importer Record of the importer user mixed with contact of the content
         */
-       private function process_mail($xpath, $mail, $importer) {
+       private static function process_mail($xpath, $mail, $importer) {
 
                logger("Processing mails");
 
@@ -1403,9 +1430,9 @@ class dfrn {
                $msg["seen"] = 0;
                $msg["replied"] = 0;
 
-               dbesc_array($msg);
+               dbm::esc_array($msg, true);
 
-               $r = dbq("INSERT INTO `mail` (`".implode("`, `", array_keys($msg))."`) VALUES ('".implode("', '", array_values($msg))."')");
+               $r = dbq("INSERT INTO `mail` (`".implode("`, `", array_keys($msg))."`) VALUES (".implode(", ", array_values($msg)).")");
 
                // send notifications.
 
@@ -1436,7 +1463,8 @@ class dfrn {
         * @param object $suggestion suggestion elements
         * @param array $importer Record of the importer user mixed with contact of the content
         */
-       private function process_suggestion($xpath, $suggestion, $importer) {
+       private static function process_suggestion($xpath, $suggestion, $importer) {
+               $a = get_app();
 
                logger("Processing suggestions");
 
@@ -1456,7 +1484,7 @@ class dfrn {
                        dbesc(normalise_link($suggest["url"])),
                        intval($suggest["uid"])
                );
-               if(count($r))
+               if (dbm::is_result($r))
                        return false;
 
                // Do we already have an fcontact record for this person?
@@ -1467,7 +1495,7 @@ class dfrn {
                        dbesc($suggest["name"]),
                        dbesc($suggest["request"])
                );
-               if(count($r)) {
+               if (dbm::is_result($r)) {
                        $fid = $r[0]["id"];
 
                        // OK, we do. Do we already have an introduction for this person ?
@@ -1475,7 +1503,7 @@ class dfrn {
                                intval($suggest["uid"]),
                                intval($fid)
                        );
-                       if(count($r))
+                       if (dbm::is_result($r))
                                return false;
                }
                if(!$fid)
@@ -1490,7 +1518,7 @@ class dfrn {
                        dbesc($suggest["name"]),
                        dbesc($suggest["request"])
                );
-               if(count($r))
+               if (dbm::is_result($r))
                        $fid = $r[0]["id"];
                else
                        // database record did not get created. Quietly give up.
@@ -1537,7 +1565,7 @@ class dfrn {
         * @param object $relocation relocation elements
         * @param array $importer Record of the importer user mixed with contact of the content
         */
-       private function process_relocation($xpath, $relocation, $importer) {
+       private static function process_relocation($xpath, $relocation, $importer) {
 
                logger("Processing relocations");
 
@@ -1545,7 +1573,9 @@ class dfrn {
                $relocate["uid"] = $importer["importer_uid"];
                $relocate["cid"] = $importer["id"];
                $relocate["url"] = $xpath->query("dfrn:url/text()", $relocation)->item(0)->nodeValue;
+               $relocate["addr"] = $xpath->query("dfrn:addr/text()", $relocation)->item(0)->nodeValue;
                $relocate["name"] = $xpath->query("dfrn:name/text()", $relocation)->item(0)->nodeValue;
+               $relocate["avatar"] = $xpath->query("dfrn:avatar/text()", $relocation)->item(0)->nodeValue;
                $relocate["photo"] = $xpath->query("dfrn:photo/text()", $relocation)->item(0)->nodeValue;
                $relocate["thumb"] = $xpath->query("dfrn:thumb/text()", $relocation)->item(0)->nodeValue;
                $relocate["micro"] = $xpath->query("dfrn:micro/text()", $relocation)->item(0)->nodeValue;
@@ -1555,6 +1585,12 @@ class dfrn {
                $relocate["poll"] = $xpath->query("dfrn:poll/text()", $relocation)->item(0)->nodeValue;
                $relocate["sitepubkey"] = $xpath->query("dfrn:sitepubkey/text()", $relocation)->item(0)->nodeValue;
 
+               if (($relocate["avatar"] == "") AND ($relocate["photo"] != ""))
+                       $relocate["avatar"] = $relocate["photo"];
+
+               if ($relocate["addr"] == "")
+                       $relocate["addr"] = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$3@$2", $relocate["url"]);
+
                // update contact
                $r = q("SELECT `photo`, `url` FROM `contact` WHERE `id` = %d AND `uid` = %d;",
                        intval($importer["id"]),
@@ -1564,51 +1600,83 @@ class dfrn {
 
                $old = $r[0];
 
-               $x = q("UPDATE `contact` SET
+               // Update the gcontact entry
+               $relocate["server_url"] = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $relocate["url"]);
+
+               $x = q("UPDATE `gcontact` SET
                                        `name` = '%s',
                                        `photo` = '%s',
-                                       `thumb` = '%s',
-                                       `micro` = '%s',
                                        `url` = '%s',
                                        `nurl` = '%s',
+                                       `addr` = '%s',
+                                       `connect` = '%s',
+                                       `notify` = '%s',
+                                       `server_url` = '%s'
+                       WHERE `nurl` = '%s';",
+                                       dbesc($relocate["name"]),
+                                       dbesc($relocate["avatar"]),
+                                       dbesc($relocate["url"]),
+                                       dbesc(normalise_link($relocate["url"])),
+                                       dbesc($relocate["addr"]),
+                                       dbesc($relocate["addr"]),
+                                       dbesc($relocate["notify"]),
+                                       dbesc($relocate["server_url"]),
+                                       dbesc(normalise_link($old["url"])));
+
+               // Update the contact table. We try to find every entry.
+               $x = q("UPDATE `contact` SET
+                                       `name` = '%s',
+                                       `avatar` = '%s',
+                                       `url` = '%s',
+                                       `nurl` = '%s',
+                                       `addr` = '%s',
                                        `request` = '%s',
                                        `confirm` = '%s',
                                        `notify` = '%s',
                                        `poll` = '%s',
                                        `site-pubkey` = '%s'
-                       WHERE `id` = %d AND `uid` = %d;",
+                       WHERE (`id` = %d AND `uid` = %d) OR (`nurl` = '%s');",
                                        dbesc($relocate["name"]),
-                                       dbesc($relocate["photo"]),
-                                       dbesc($relocate["thumb"]),
-                                       dbesc($relocate["micro"]),
+                                       dbesc($relocate["avatar"]),
                                        dbesc($relocate["url"]),
                                        dbesc(normalise_link($relocate["url"])),
+                                       dbesc($relocate["addr"]),
                                        dbesc($relocate["request"]),
                                        dbesc($relocate["confirm"]),
                                        dbesc($relocate["notify"]),
                                        dbesc($relocate["poll"]),
                                        dbesc($relocate["sitepubkey"]),
                                        intval($importer["id"]),
-                                       intval($importer["importer_uid"]));
+                                       intval($importer["importer_uid"]),
+                                       dbesc(normalise_link($old["url"])));
+
+               update_contact_avatar($relocate["avatar"], $importer["importer_uid"], $importer["id"], true);
 
                if ($x === false)
                        return false;
 
                // update items
+               /// @todo This is an extreme performance killer
                $fields = array(
                        'owner-link' => array($old["url"], $relocate["url"]),
                        'author-link' => array($old["url"], $relocate["url"]),
-                       'owner-avatar' => array($old["photo"], $relocate["photo"]),
-                       'author-avatar' => array($old["photo"], $relocate["photo"]),
+                       //'owner-avatar' => array($old["photo"], $relocate["photo"]),
+                       //'author-avatar' => array($old["photo"], $relocate["photo"]),
                        );
-               foreach ($fields as $n=>$f){
-                       $x = q("UPDATE `item` SET `%s` = '%s' WHERE `%s` = '%s' AND `uid` = %d",
-                                       $n, dbesc($f[1]),
+               foreach ($fields as $n=>$f) {
+                       $r = q("SELECT `id` FROM `item` WHERE `%s` = '%s' AND `uid` = %d LIMIT 1",
                                        $n, dbesc($f[0]),
                                        intval($importer["importer_uid"]));
-                               if ($x === false)
-                                       return false;
+
+                       if ($r) {
+                               $x = q("UPDATE `item` SET `%s` = '%s' WHERE `%s` = '%s' AND `uid` = %d",
+                                               $n, dbesc($f[1]),
+                                               $n, dbesc($f[0]),
+                                               intval($importer["importer_uid"]));
+                                       if ($x === false)
+                                               return false;
                        }
+               }
 
                /// @TODO
                /// merge with current record, current contents have priority
@@ -1626,7 +1694,7 @@ class dfrn {
         * @param array $importer Record of the importer user mixed with contact of the content
         * @param int $entrytype Is it a toplevel entry, a comment or a relayed comment?
         */
-       private function update_content($current, $item, $importer, $entrytype) {
+       private static function update_content($current, $item, $importer, $entrytype) {
                $changed = false;
 
                if (edited_timestamp_is_newer($current, $item)) {
@@ -1650,7 +1718,7 @@ class dfrn {
                        $changed = true;
 
                        if ($entrytype == DFRN_REPLY_RC)
-                               proc_run("php", "include/notifier.php","comment-import", $current["id"]);
+                               proc_run(PRIORITY_HIGH, "include/notifier.php","comment-import", $current["id"]);
                }
 
                // update last-child if it changes
@@ -1678,7 +1746,7 @@ class dfrn {
         *
         * @return int Is it a toplevel entry, a comment or a relayed comment?
         */
-       private function get_entry_type($importer, $item) {
+       private static function get_entry_type($importer, $item) {
                if ($item["parent-uri"] != $item["uri"]) {
                        $community = false;
 
@@ -1699,7 +1767,7 @@ class dfrn {
                                LIMIT 1",
                                dbesc($item["parent-uri"])
                        );
-                       if($r && count($r)) {
+                       if (dbm::is_result($r)) {
                                $r = q("SELECT `item`.`forum_mode`, `item`.`wall` FROM `item`
                                        INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
                                        WHERE `item`.`uri` = '%s' AND (`item`.`parent-uri` = '%s' OR `item`.`thr-parent` = '%s')
@@ -1711,7 +1779,7 @@ class dfrn {
                                        dbesc($r[0]["parent-uri"]),
                                        intval($importer["importer_uid"])
                                );
-                               if($r && count($r))
+                               if (dbm::is_result($r))
                                        $is_a_remote_action = true;
                        }
 
@@ -1744,7 +1812,7 @@ class dfrn {
         * @param array $importer Record of the importer user mixed with contact of the content
         * @param int $posted_id The record number of item record that was just posted
         */
-       private function do_poke($item, $importer, $posted_id) {
+       private static function do_poke($item, $importer, $posted_id) {
                $verb = urldecode(substr($item["verb"],strpos($item["verb"], "#")+1));
                if(!$verb)
                        return;
@@ -1799,7 +1867,7 @@ class dfrn {
         *
         * @return bool Should the processing of the entries be continued?
         */
-       private function process_verbs($entrytype, $importer, &$item, &$is_like) {
+       private static function process_verbs($entrytype, $importer, &$item, &$is_like) {
 
                logger("Process verb ".$item["verb"]." and object-type ".$item["object-type"]." for entrytype ".$entrytype, LOGGER_DEBUG);
 
@@ -1849,7 +1917,7 @@ class dfrn {
                                        dbesc($item["verb"]),
                                        dbesc($item["parent-uri"])
                                );
-                               if($r && count($r))
+                               if (dbm::is_result($r))
                                        return false;
 
                                $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `author-link` = '%s' AND `verb` = '%s' AND `thr-parent` = '%s' AND NOT `deleted` LIMIT 1",
@@ -1858,7 +1926,7 @@ class dfrn {
                                        dbesc($item["verb"]),
                                        dbesc($item["parent-uri"])
                                );
-                               if($r && count($r))
+                               if (dbm::is_result($r))
                                        return false;
                        } else
                                $is_like = false;
@@ -1874,7 +1942,7 @@ class dfrn {
                                                intval($importer["importer_uid"])
                                        );
 
-                                       if(!count($r))
+                                       if (!dbm::is_result($r))
                                                return false;
 
                                        // extract tag, if not duplicate, add to parent item
@@ -1899,7 +1967,7 @@ class dfrn {
         * @param object $links link elements
         * @param array $item the item record
         */
-       private function parse_links($links, &$item) {
+       private static function parse_links($links, &$item) {
                $rel = "";
                $href = "";
                $type = "";
@@ -1942,7 +2010,7 @@ class dfrn {
         * @param object $entry entry elements
         * @param array $importer Record of the importer user mixed with contact of the content
         */
-       private function process_entry($header, $xpath, $entry, $importer) {
+       private static function process_entry($header, $xpath, $entry, $importer) {
 
                logger("Processing entries");
 
@@ -1951,6 +2019,20 @@ class dfrn {
                // Get the uri
                $item["uri"] = $xpath->query("atom:id/text()", $entry)->item(0)->nodeValue;
 
+               $item["edited"] = $xpath->query("atom:updated/text()", $entry)->item(0)->nodeValue;
+
+               $current = q("SELECT `id`, `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+                       dbesc($item["uri"]),
+                       intval($importer["importer_uid"])
+               );
+
+               // Is there an existing item?
+               if (dbm::is_result($current) AND edited_timestamp_is_newer($current[0], $item) AND
+                       (datetime_convert("UTC","UTC",$item["edited"]) < $current[0]["edited"])) {
+                       logger("Item ".$item["uri"]." already existed.", LOGGER_DEBUG);
+                       return;
+               }
+
                // Fetch the owner
                $owner = self::fetchauthor($xpath, $entry, $importer, "dfrn:owner", true);
 
@@ -1968,7 +2050,6 @@ class dfrn {
                $item["title"] = $xpath->query("atom:title/text()", $entry)->item(0)->nodeValue;
 
                $item["created"] = $xpath->query("atom:published/text()", $entry)->item(0)->nodeValue;
-               $item["edited"] = $xpath->query("atom:updated/text()", $entry)->item(0)->nodeValue;
 
                $item["body"] = $xpath->query("dfrn:env/text()", $entry)->item(0)->nodeValue;
                $item["body"] = str_replace(array(' ',"\t","\r","\n"), array('','','',''),$item["body"]);
@@ -2146,7 +2227,7 @@ class dfrn {
                                                dbesc($item["uri"]),
                                                intval($importer["uid"])
                                        );
-                                       if(count($r))
+                                       if (dbm::is_result($r))
                                                $ev["id"] = $r[0]["id"];
 
                                        $event_id = event_store($ev);
@@ -2156,18 +2237,13 @@ class dfrn {
                        }
                }
 
-               $r = q("SELECT `id`, `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
-                       dbesc($item["uri"]),
-                       intval($importer["importer_uid"])
-               );
-
                if (!self::process_verbs($entrytype, $importer, $item, $is_like)) {
                        logger("Exiting because 'process_verbs' told us so", LOGGER_DEBUG);
                        return;
                }
 
                // Update content if 'updated' changes
-               if(count($r)) {
+               if (dbm::is_result($current)) {
                        if (self::update_content($r[0], $item, $importer, $entrytype))
                                logger("Item ".$item["uri"]." was updated.", LOGGER_DEBUG);
                        else
@@ -2189,7 +2265,7 @@ class dfrn {
                                        intval($posted_id),
                                        intval($importer["importer_uid"])
                                );
-                               if(count($r)) {
+                               if (dbm::is_result($r)) {
                                        $parent = $r[0]["parent"];
                                        $parent_uri = $r[0]["parent-uri"];
                                }
@@ -2210,7 +2286,7 @@ class dfrn {
 
                                if($posted_id AND $parent AND ($entrytype == DFRN_REPLY_RC)) {
                                        logger("Notifying followers about comment ".$posted_id, LOGGER_DEBUG);
-                                       proc_run("php", "include/notifier.php", "comment-import", $posted_id);
+                                       proc_run(PRIORITY_HIGH, "include/notifier.php", "comment-import", $posted_id);
                                }
 
                                return true;
@@ -2252,7 +2328,7 @@ class dfrn {
         * @param object $deletion deletion elements
         * @param array $importer Record of the importer user mixed with contact of the content
         */
-       private function process_deletion($xpath, $deletion, $importer) {
+       private static function process_deletion($xpath, $deletion, $importer) {
 
                logger("Processing deletions");
 
@@ -2277,7 +2353,7 @@ class dfrn {
                                intval($importer["uid"]),
                                intval($importer["id"])
                        );
-               if(!count($r)) {
+               if (!dbm::is_result($r)) {
                        logger("Item with uri ".$uri." from contact ".$importer["id"]." for user ".$importer["uid"]." wasn't found.", LOGGER_DEBUG);
                        return;
                } else {
@@ -2306,7 +2382,7 @@ class dfrn {
                                                dbesc($xt->id),
                                                intval($importer["importer_uid"])
                                        );
-                                       if(count($i)) {
+                                       if (dbm::is_result($i)) {
 
                                                // For tags, the owner cannot remove the tag on the author's copy of the post.
 
@@ -2371,7 +2447,7 @@ class dfrn {
                                                        dbesc($item["parent-uri"]),
                                                        intval($importer["uid"])
                                        );
-                                       if(count($r)) {
+                                       if (dbm::is_result($r)) {
                                                q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d",
                                                        intval($r[0]["id"])
                                                );
@@ -2381,7 +2457,7 @@ class dfrn {
 
                                if($entrytype == DFRN_REPLY_RC) {
                                        logger("Notifying followers about deletion of post ".$item["id"], LOGGER_DEBUG);
-                                       proc_run("php", "include/notifier.php","drop", $item["id"]);
+                                       proc_run(PRIORITY_HIGH, "include/notifier.php","drop", $item["id"]);
                                }
                        }
                }
@@ -2441,7 +2517,19 @@ class dfrn {
 
                logger("Import DFRN message for user ".$importer["uid"]." from contact ".$importer["id"], LOGGER_DEBUG);
 
-               // is it a public forum? Private forums aren't supported by now with this method
+               // The account type is new since 3.5.1
+               if ($xpath->query("/atom:feed/dfrn:account_type")->length > 0) {
+                       $accounttype = intval($xpath->evaluate("/atom:feed/dfrn:account_type/text()", $context)->item(0)->nodeValue);
+
+                       if ($accounttype != $importer["contact-type"])
+                               q("UPDATE `contact` SET `contact-type` = %d WHERE `id` = %d",
+                                       intval($accounttype),
+                                       intval($importer["id"])
+                               );
+               }
+
+               // is it a public forum? Private forums aren't supported with this method
+               // This is deprecated since 3.5.1
                $forum = intval($xpath->evaluate("/atom:feed/dfrn:community/text()", $context)->item(0)->nodeValue);
 
                if ($forum != $importer["forum"])