]> git.mxchange.org Git - friendica.git/blobdiff - include/items.php
Performance stuff: Caching for getsiteinfo function, check before running onepoll.php
[friendica.git] / include / items.php
old mode 100755 (executable)
new mode 100644 (file)
index 54b392b..22ae2f6
@@ -6,8 +6,11 @@ require_once('include/salmon.php');
 require_once('include/crypto.php');
 require_once('include/Photo.php');
 require_once('include/tags.php');
+require_once('include/files.php');
 require_once('include/text.php');
 require_once('include/email.php');
+require_once('include/ostatus_conversation.php');
+require_once('include/threads.php');
 
 function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
 
@@ -35,7 +38,7 @@ function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0)
        $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid`  = '' AND `deny_gid`  = '' ";
 
        $r = q("SELECT `contact`.*, `user`.`uid` AS `user_uid`, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`
-               FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
+               FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
                WHERE `contact`.`self` = 1 AND `user`.`nickname` = '%s' LIMIT 1",
                dbesc($owner_nick)
        );
@@ -49,6 +52,8 @@ function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0)
 
        $birthday = feed_birthday($owner_id,$owner['timezone']);
 
+       $sql_post_table = "";
+
        if(! $public_feed) {
 
                $sql_extra = '';
@@ -82,18 +87,18 @@ function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0)
                $groups = init_groups_visitor($contact['id']);
 
                if(count($groups)) {
-                       for($x = 0; $x < count($groups); $x ++) 
+                       for($x = 0; $x < count($groups); $x ++)
                                $groups[$x] = '<' . intval($groups[$x]) . '>' ;
                        $gs = implode('|', $groups);
                }
                else
-                       $gs = '<<>>' ; // Impossible to match 
+                       $gs = '<<>>' ; // Impossible to match
 
-               $sql_extra = sprintf(" 
-                       AND ( `allow_cid` = '' OR     `allow_cid` REGEXP '<%d>' ) 
-                       AND ( `deny_cid`  = '' OR NOT `deny_cid`  REGEXP '<%d>' ) 
+               $sql_extra = sprintf("
+                       AND ( `allow_cid` = '' OR     `allow_cid` REGEXP '<%d>' )
+                       AND ( `deny_cid`  = '' OR NOT `deny_cid`  REGEXP '<%d>' )
                        AND ( `allow_gid` = '' OR     `allow_gid` REGEXP '%s' )
-                       AND ( `deny_gid`  = '' OR NOT `deny_gid`  REGEXP '%s') 
+                       AND ( `deny_gid`  = '' OR NOT `deny_gid`  REGEXP '%s')
                ",
                        intval($contact['id']),
                        intval($contact['id']),
@@ -111,7 +116,9 @@ function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0)
                $last_update = 'now -30 days';
 
        if(isset($category)) {
-               $sql_extra .= file_tag_file_query('item',$category,'category');
+               $sql_post_table = sprintf("INNER JOIN (SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d ORDER BY `tid` DESC) AS `term` ON `item`.`id` = `term`.`oid` ",
+                               dbesc(protect_sprintf($category)), intval(TERM_OBJ_POST), intval(TERM_CATEGORY), intval($owner_id));
+               //$sql_extra .= file_tag_file_query('item',$category,'category');
        }
 
        if($public_feed) {
@@ -121,22 +128,25 @@ function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0)
 
        $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
 
-       $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
-               `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`, 
+       //      AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
+       //      dbesc($check_date),
+
+       $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`, 
+               `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
                `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`,
                `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
-               FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
+               FROM `item` $sql_post_table
+               INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
+               AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
                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 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
-               AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
+               WHERE `item`.`uid` = %d AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`parent` != 0
+               AND `item`.`wall` = 1 AND `item`.`changed` > '%s'
                $sql_extra
                ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
                intval($owner_id),
                dbesc($check_date),
-               dbesc($check_date),
                dbesc($sort)
        );
 
@@ -153,6 +163,11 @@ function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0)
 
        $salmon = feed_salmonlinks($owner_nick);
 
+       $alternatelink = $owner['url'];
+
+       if(isset($category))
+               $alternatelink .= "/category/".$category;
+
        $atom .= replace_macros($feed_template, array(
                '$version'      => xmlify(FRIENDICA_VERSION),
                '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
@@ -160,13 +175,14 @@ function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0)
                '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', 'now' , ATOM_TIME)) ,
                '$hub'          => $hubxml,
                '$salmon'       => $salmon,
+               '$alternatelink' => xmlify($alternatelink),
                '$name'         => xmlify($owner['name']),
                '$profile_page' => xmlify($owner['url']),
                '$photo'        => xmlify($owner['photo']),
                '$thumb'        => xmlify($owner['thumb']),
                '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
                '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
-               '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) , 
+               '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) ,
                '$birthday'     => ((strlen($birthday)) ? '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>' : ''),
                '$community'    => (($owner['page-flags'] == PAGE_COMMUNITY) ? '<dfrn:community>1</dfrn:community>' : '')
        ));
@@ -251,7 +267,7 @@ function construct_activity_object($item) {
        }
 
        return '';
-} 
+}
 
 function construct_activity_target($item) {
 
@@ -293,7 +309,7 @@ function construct_activity_target($item) {
 if(! function_exists('limit_body_size')) {
 function limit_body_size($body) {
 
-       logger('limit_body_size: start', LOGGER_DEBUG);
+//     logger('limit_body_size: start', LOGGER_DEBUG);
 
        $maxlen = get_max_import_size();
 
@@ -405,7 +421,7 @@ function title_is_body($title, $body) {
 
 
 
-function get_atom_elements($feed,$item) {
+function get_atom_elements($feed, $item, $contact = array()) {
 
        require_once('library/HTMLPurifier.auto.php');
        require_once('include/html2bbcode.php');
@@ -415,7 +431,7 @@ function get_atom_elements($feed,$item) {
        $res = array();
 
        $author = $item->get_author();
-       if($author) { 
+       if($author) {
                $res['author-name'] = unxmlify($author->get_name());
                $res['author-link'] = unxmlify($author->get_link());
        }
@@ -428,6 +444,12 @@ function get_atom_elements($feed,$item) {
        $res['body'] = unxmlify($item->get_content());
        $res['plink'] = unxmlify($item->get_link(0));
 
+       if (isset($contact["network"]) AND ($contact["network"] == NETWORK_FEED) AND strstr($res['plink'], ".app.net/")) {
+               logger("get_atom_elements: detected app.net posting: ".print_r($res, true), LOGGER_DEBUG);
+               $res['title'] = "";
+               $res['body'] = nl2br($res['body']);
+       }
+
        // removing the content of the title if its identically to the body
        // This helps with auto generated titles e.g. from tumblr
        if (title_is_body($res["title"], $res["body"]))
@@ -541,17 +563,17 @@ function get_atom_elements($feed,$item) {
                $res['body'] = notags(base64url_decode($res['body']));
        }
 
-       
+
        $res['body'] = limit_body_size($res['body']);
 
-       // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust 
-       // the content type. Our own network only emits text normally, though it might have been converted to 
+       // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust
+       // the content type. Our own network only emits text normally, though it might have been converted to
        // html if we used a pubsubhubbub transport. But if we see even one html tag in our text, we will
        // have to assume it is all html and needs to be purified.
 
-       // It doesn't matter all that much security wise - because before this content is used anywhere, we are 
-       // going to escape any tags we find regardless, but this lets us import a limited subset of html from 
-       // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining 
+       // It doesn't matter all that much security wise - because before this content is used anywhere, we are
+       // going to escape any tags we find regardless, but this lets us import a limited subset of html from
+       // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining
        // html.
 
        if((strpos($res['body'],'<') !== false) && (strpos($res['body'],'>') !== false)) {
@@ -617,7 +639,7 @@ function get_atom_elements($feed,$item) {
                $res['edited'] = unxmlify($rawedited[0]['data']);
 
        if((x($res,'edited')) && (! (x($res,'created'))))
-               $res['created'] = $res['edited']; 
+               $res['created'] = $res['edited'];
 
        if(! $res['created'])
                $res['created'] = $item->get_date('c');
@@ -662,6 +684,10 @@ function get_atom_elements($feed,$item) {
        if($rawgeo)
                $res['coord'] = unxmlify($rawgeo[0]['data']);
 
+       if ($contact["network"] == NETWORK_FEED) {
+               $res['verb'] = ACTIVITY_POST;
+               $res['object-type'] = ACTIVITY_OBJ_NOTE;
+       }
 
        $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
 
@@ -710,7 +736,7 @@ function get_atom_elements($feed,$item) {
                        if(! $type)
                                $type = 'application/octet-stream';
 
-                       $att_arr[] = '[attach]href="' . $link . '" length="' . $len . '" type="' . $type . '" title="' . $title . '"[/attach]'; 
+                       $att_arr[] = '[attach]href="' . $link . '" length="' . $len . '" type="' . $type . '" title="' . $title . '"[/attach]';
                }
                $res['attach'] = implode(',', $att_arr);
        }
@@ -810,7 +836,7 @@ function get_atom_elements($feed,$item) {
                if (($name != "") and ($uri != "") and ($avatar != "") and ($message != "")) {
                        logger('get_atom_elements: fixing sender of repeated message.');
 
-                       if (intval(get_config('system','new_share'))) {
+                       if (!intval(get_config('system','wall-to-wall_share'))) {
                                $prefix = "[share author='".str_replace("'", "&#039;",$name).
                                                "' profile='".$uri.
                                                "' avatar='".$avatar.
@@ -831,19 +857,170 @@ function get_atom_elements($feed,$item) {
                }
        }
 
+       // Search for ostatus conversation url
+       $links = $item->feed->data["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["feed"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["entry"][0]["child"]["http://www.w3.org/2005/Atom"]["link"];
+
+       if (is_array($links)) {
+               foreach ($links as $link) {
+                       $conversation = array_shift($link["attribs"]);
+
+                       if ($conversation["rel"] == "ostatus:conversation") {
+                               $res["ostatus_conversation"] = $conversation["href"];
+                               logger('get_atom_elements: found conversation url '.$res["ostatus_conversation"]);
+                       }
+               };
+       }
+
+       if (isset($contact["network"]) AND ($contact["network"] == NETWORK_FEED) AND $contact['fetch_further_information']) {
+               $preview = "";
+
+               // Handle enclosures and treat them as preview picture
+               if (isset($attach))
+                       foreach ($attach AS $attachment)
+                               if ($attachment->type == "image/jpeg")
+                                       $preview = $attachment->link;
+
+               $res["body"] = $res["title"].add_page_info($res['plink'], false, $preview, ($contact['fetch_further_information'] == 2), $contact['ffi_keyword_blacklist']);
+               $res["title"] = "";
+               $res["object-type"] = ACTIVITY_OBJ_BOOKMARK;
+               unset($res["attach"]);
+       } elseif (isset($contact["network"]) AND ($contact["network"] == NETWORK_OSTATUS))
+               $res["body"] = add_page_info_to_body($res["body"]);
+       elseif (isset($contact["network"]) AND ($contact["network"] == NETWORK_FEED) AND strstr($res['plink'], ".app.net/")) {
+               $res["body"] = add_page_info_to_body($res["body"]);
+       }
+
        $arr = array('feed' => $feed, 'item' => $item, 'result' => $res);
 
        call_hooks('parse_atom', $arr);
 
-       //if (($res["title"] != "") or (strpos($res["body"], "RT @") > 0)) {
-       //if (strpos($res["body"], "RT @") !== false) {
-       //      $debugfile = tempnam("/home/ike/log", "item-res2-");
-       //      file_put_contents($debugfile, serialize($arr));
-       //}
-
        return $res;
 }
 
+function add_page_info_data($data) {
+       call_hooks('page_info_data', $data);
+
+       // It maybe is a rich content, but if it does have everything that a link has,
+       // then treat it that way
+       if (($data["type"] == "rich") AND is_string($data["title"]) AND
+               is_string($data["text"]) AND (sizeof($data["images"]) > 0))
+               $data["type"] = "link";
+
+       if ((($data["type"] != "link") AND ($data["type"] != "video") AND ($data["type"] != "photo")) OR ($data["title"] == $url))
+               return("");
+
+       if ($no_photos AND ($data["type"] == "photo"))
+               return("");
+
+       // If the link contains BBCode stuff, make a short link out of this to avoid parsing problems
+       if (strpos($data["url"], '[') OR strpos($data["url"], ']')) {
+               require_once("include/network.php");
+               $data["url"] = short_link($data["url"]);
+       }
+
+       if (($data["type"] != "photo") AND is_string($data["title"]))
+               $text .= "[bookmark=".$data["url"]."]".trim($data["title"])."[/bookmark]";
+
+       if (($data["type"] != "video") AND ($photo != ""))
+               $text .= '[img]'.$photo.'[/img]';
+       elseif (($data["type"] != "video") AND (sizeof($data["images"]) > 0)) {
+               $imagedata = $data["images"][0];
+               $text .= '[img]'.$imagedata["src"].'[/img]';
+       }
+
+       if (($data["type"] != "photo") AND is_string($data["text"]))
+               $text .= "[quote]".$data["text"]."[/quote]";
+
+       $hashtags = "";
+       if (isset($data["keywords"]) AND count($data["keywords"])) {
+               $a = get_app();
+               $hashtags = "\n";
+               foreach ($data["keywords"] AS $keyword) {
+                       $hashtag = str_replace(array(" ", "+", "/", ".", "#", "'"),
+                                               array("","", "", "", "", ""), $keyword);
+                       $hashtags .= "#[url=".$a->get_baseurl()."/search?tag=".rawurlencode($hashtag)."]".$hashtag."[/url] ";
+               }
+       }
+
+       return("\n[class=type-".$data["type"]."]".$text."[/class]".$hashtags);
+}
+
+function add_page_info($url, $no_photos = false, $photo = "", $keywords = false, $keyword_blacklist = "") {
+       require_once("mod/parse_url.php");
+
+       $data = Cache::get("parse_url:".$url);
+       if (is_null($data)){
+               $data = parseurl_getsiteinfo($url, true);
+               Cache::set("parse_url:".$url,serialize($data));
+       } else
+               $data = unserialize($data);
+
+       if ($photo != "")
+               $data["images"][0]["src"] = $photo;
+
+       logger('add_page_info: fetch page info for '.$url.' '.print_r($data, true), LOGGER_DEBUG);
+
+       if (!$keywords AND isset($data["keywords"]))
+               unset($data["keywords"]);
+
+       if (($keyword_blacklist != "") AND isset($data["keywords"])) {
+               $list = explode(",", $keyword_blacklist);
+               foreach ($list AS $keyword) {
+                       $keyword = trim($keyword);
+                       $index = array_search($keyword, $data["keywords"]);
+                       if ($index !== false)
+                               unset($data["keywords"][$index]);
+               }
+       }
+
+       $text = add_page_info_data($data);
+
+       return($text);
+}
+
+function add_page_info_to_body($body, $texturl = false, $no_photos = false) {
+
+       logger('add_page_info_to_body: fetch page info for body '.$body, LOGGER_DEBUG);
+
+       $URLSearchString = "^\[\]";
+
+       // Adding these spaces is a quick hack due to my problems with regular expressions :)
+       preg_match("/[^!#@]\[url\]([$URLSearchString]*)\[\/url\]/ism", " ".$body, $matches);
+
+       if (!$matches)
+               preg_match("/[^!#@]\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", " ".$body, $matches);
+
+       // Convert urls without bbcode elements
+       if (!$matches AND $texturl) {
+               preg_match("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", " ".$body, $matches);
+
+               // Yeah, a hack. I really hate regular expressions :)
+               if ($matches)
+                       $matches[1] = $matches[2];
+       }
+
+       if ($matches)
+               $footer = add_page_info($matches[1], $no_photos);
+
+       // Remove the link from the body if the link is attached at the end of the post
+       if (isset($footer) AND (trim($footer) != "") AND (strpos($footer, $matches[1]))) {
+               $removedlink = trim(str_replace($matches[1], "", $body));
+               if (($removedlink == "") OR strstr($body, $removedlink))
+                       $body = $removedlink;
+
+               $url = str_replace(array('/', '.'), array('\/', '\.'), $matches[1]);
+               $removedlink = preg_replace("/\[url\=".$url."\](.*?)\[\/url\]/ism", '', $body);
+               if (($removedlink == "") OR strstr($body, $removedlink))
+                       $body = $removedlink;
+       }
+
+       // Add the page information to the bottom
+       if (isset($footer) AND (trim($footer) != ""))
+               $body .= $footer;
+
+       return $body;
+}
+
 function encode_rel_links($links) {
        $o = '';
        if(! ((is_array($links)) && (count($links))))
@@ -867,9 +1044,18 @@ function encode_rel_links($links) {
 
 
 
-function item_store($arr,$force_parent = false) {
+function item_store($arr,$force_parent = false, $notify = false) {
 
-       // If a Diaspora signature structure was passed in, pull it out of the 
+       // If it is a posting where users should get notifications, then define it as wall posting
+       if ($notify) {
+               $arr['wall'] = 1;
+               $arr['type'] = 'wall';
+               $arr['origin'] = 1;
+               $arr['last-child'] = 1;
+               $arr['network'] = NETWORK_DFRN;
+       }
+
+       // If a Diaspora signature structure was passed in, pull it out of the
        // item array and set it aside for later storage.
 
        $dsprsig = null;
@@ -878,6 +1064,15 @@ function item_store($arr,$force_parent = false) {
                unset($arr['dsprsig']);
        }
 
+       // if an OStatus conversation url was passed in, it is stored and then
+       // removed from the array.
+       $ostatus_conversation = null;
+
+       if (isset($arr["ostatus_conversation"])) {
+               $ostatus_conversation = $arr["ostatus_conversation"];
+               unset($arr["ostatus_conversation"]);
+       }
+
        if(x($arr, 'gravity'))
                $arr['gravity'] = intval($arr['gravity']);
        elseif($arr['parent-uri'] === $arr['uri'])
@@ -890,10 +1085,40 @@ function item_store($arr,$force_parent = false) {
        if(! x($arr,'type'))
                $arr['type']      = 'remote';
 
-       // Shouldn't happen but we want to make absolutely sure it doesn't leak from a plugin.
 
-       if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false)) 
-               $arr['body'] = strip_tags($arr['body']);
+
+       /* check for create  date and expire time */
+       $uid = intval($arr['uid']);
+       $r = q("SELECT expire FROM user WHERE uid = %d", $uid);
+       if(count($r)) {
+               $expire_interval = $r[0]['expire'];
+               if ($expire_interval>0) {
+                       $expire_date =  new DateTime( '- '.$expire_interval.' days', new DateTimeZone('UTC'));
+                       $created_date = new DateTime($arr['created'], new DateTimeZone('UTC'));
+                       if ($created_date < $expire_date) {
+                               logger('item-store: item created ('.$arr['created'].') before expiration time ('.$expire_date->format(DateTime::W3C).'). ignored. ' . print_r($arr,true), LOGGER_DEBUG);
+                               return 0;
+                       }
+               }
+       }
+
+       // If there is no guid then take the same guid that was taken before for the same uri
+       if ((trim($arr['guid']) == "") AND (trim($arr['uri']) != "")) {
+               logger('item_store: checking for an existing guid for uri '.$arr['uri'], LOGGER_DEBUG);
+               $r = q("SELECT `guid` FROM `item` WHERE `uri` = '%s' AND `guid` != '' LIMIT 1",
+                       dbesc(trim($arr['uri']))
+               );
+
+               if(count($r)) {
+                       $arr['guid'] = $r[0]["guid"];
+                       logger('item_store: found guid '.$arr['guid'].' for uri '.$arr['uri'], LOGGER_DEBUG);
+               }
+       }
+
+       // Shouldn't happen but we want to make absolutely sure it doesn't leak from a plugin.
+       // Deactivated, since the bbcode parser can handle with it - and it destroys posts with some smileys that contain "<"
+       //if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false))
+       //      $arr['body'] = strip_tags($arr['body']);
 
 
        if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
@@ -957,8 +1182,34 @@ function item_store($arr,$force_parent = false) {
        $arr['attach']        = ((x($arr,'attach'))        ? notags(trim($arr['attach']))        : '');
        $arr['app']           = ((x($arr,'app'))           ? notags(trim($arr['app']))           : '');
        $arr['origin']        = ((x($arr,'origin'))        ? intval($arr['origin'])              : 0 );
-       $arr['guid']          = ((x($arr,'guid'))          ? notags(trim($arr['guid']))          : get_guid());
+       $arr['guid']          = ((x($arr,'guid'))          ? notags(trim($arr['guid']))          : get_guid(30));
+       $arr['network']       = ((x($arr,'network'))       ? trim($arr['network'])               : '');
+       $arr['postopts']      = ((x($arr,'postopts'))      ? trim($arr['postopts'])              : '');
+       $arr['resource-id']   = ((x($arr,'resource-id'))   ? trim($arr['resource-id'])           : '');
+       $arr['event-id']      = ((x($arr,'event-id'))      ? intval($arr['event-id'])            : 0 );
+       $arr['inform']        = ((x($arr,'inform'))        ? trim($arr['inform'])                : '');
+       $arr['file']          = ((x($arr,'file'))          ? trim($arr['file'])                  : '');
+
+       if ($arr['plink'] == "") {
+               $a = get_app();
+               $arr['plink'] = $a->get_baseurl().'/display/'.urlencode($arr['guid']);
+       }
+
+       if ($arr['network'] == "") {
+               $r = q("SELECT `network` FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
+                       intval($arr['contact-id']),
+                       intval($arr['uid'])
+               );
+
+               if(count($r))
+                       $arr['network'] = $r[0]["network"];
 
+               // Fallback to friendica (why is it empty in some cases?)
+               if ($arr['network'] == "")
+                       $arr['network'] = NETWORK_DFRN;
+
+               logger("item_store: Set network to ".$arr["network"]." for ".$arr["uri"], LOGGER_DEBUG);
+       }
 
        $arr['thr-parent'] = $arr['parent-uri'];
        if($arr['parent-uri'] === $arr['uri']) {
@@ -968,10 +1219,11 @@ function item_store($arr,$force_parent = false) {
                $allow_gid = $arr['allow_gid'];
                $deny_cid  = $arr['deny_cid'];
                $deny_gid  = $arr['deny_gid'];
+               $notify_type = 'wall-new';
        }
-       else { 
+       else {
 
-               // find the parent and snarf the item id and ACL's
+               // find the parent and snarf the item id and ACLs
                // and anything else we need to inherit
 
                $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d ORDER BY `id` ASC LIMIT 1",
@@ -987,7 +1239,7 @@ function item_store($arr,$force_parent = false) {
 
                        if($r[0]['uri'] != $r[0]['parent-uri']) {
                                $arr['parent-uri'] = $r[0]['parent-uri'];
-                               $z = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `parent-uri` = '%s' AND `uid` = %d 
+                               $z = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `parent-uri` = '%s' AND `uid` = %d
                                        ORDER BY `id` ASC LIMIT 1",
                                        dbesc($r[0]['parent-uri']),
                                        dbesc($r[0]['parent-uri']),
@@ -1004,20 +1256,35 @@ function item_store($arr,$force_parent = false) {
                        $deny_cid       = $r[0]['deny_cid'];
                        $deny_gid       = $r[0]['deny_gid'];
                        $arr['wall']    = $r[0]['wall'];
+                       $notify_type    = 'comment-new';
 
                        // if the parent is private, force privacy for the entire conversation
-                       // This differs from the above settings as it subtly allows comments from 
-                       // email correspondents to be private even if the overall thread is not. 
+                       // This differs from the above settings as it subtly allows comments from
+                       // email correspondents to be private even if the overall thread is not.
 
                        if($r[0]['private'])
                                $arr['private'] = $r[0]['private'];
 
                        // Edge case. We host a public forum that was originally posted to privately.
                        // The original author commented, but as this is a comment, the permissions
-                       // weren't fixed up so it will still show the comment as private unless we fix it here. 
+                       // weren't fixed up so it will still show the comment as private unless we fix it here.
 
                        if((intval($r[0]['forum_mode']) == 1) && (! $r[0]['private']))
                                $arr['private'] = 0;
+
+
+                       // If its a post from myself then tag the thread as "mention"
+                       logger("item_store: Checking if parent ".$parent_id." has to be tagged as mention for user ".$arr['uid'], LOGGER_DEBUG);
+                       $u = q("select * from user where uid = %d limit 1", intval($arr['uid']));
+                       if(count($u)) {
+                               $a = get_app();
+                               $self = normalise_link($a->get_baseurl() . '/profile/' . $u[0]['nickname']);
+                               logger("item_store: 'myself' is ".$self." for parent ".$parent_id." checking against ".$arr['author-link']." and ".$arr['owner-link'], LOGGER_DEBUG);
+                               if ((normalise_link($arr['author-link']) == $self) OR (normalise_link($arr['owner-link']) == $self)) {
+                                       q("UPDATE `thread` SET `mention` = 1 WHERE `iid` = %d", intval($parent_id));
+                                       logger("item_store: tagged thread ".$parent_id." as mention for user ".$self, LOGGER_DEBUG);
+                               }
+                       }
                }
                else {
 
@@ -1059,10 +1326,10 @@ function item_store($arr,$force_parent = false) {
 
        logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
 
-       $r = dbq("INSERT INTO `item` (`" 
-                       . implode("`, `", array_keys($arr)) 
-                       . "`) VALUES ('" 
-                       . implode("', '", array_values($arr)) 
+       $r = dbq("INSERT INTO `item` (`"
+                       . implode("`, `", array_keys($arr))
+                       . "`) VALUES ('"
+                       . implode("', '", array_values($arr))
                        . "')" );
 
        // find the item we just created
@@ -1075,7 +1342,50 @@ function item_store($arr,$force_parent = false) {
        if(count($r)) {
                $current_post = $r[0]['id'];
                logger('item_store: created item ' . $current_post);
-               create_tags_from_item($r[0]['id']);
+
+               // Only check for notifications on start posts
+               if ($arr['parent-uri'] === $arr['uri']) {
+                       add_thread($r[0]['id']);
+                       logger('item_store: Check notification for contact '.$arr['contact-id'].' and post '.$current_post, LOGGER_DEBUG);
+
+                       // Send a notification for every new post?
+                       $r = q("SELECT `notify_new_posts` FROM `contact` WHERE `id` = %d AND `uid` = %d AND `notify_new_posts` LIMIT 1",
+                               intval($arr['contact-id']),
+                               intval($arr['uid'])
+                       );
+
+                       if(count($r)) {
+                               logger('item_store: Send notification for contact '.$arr['contact-id'].' and post '.$current_post, LOGGER_DEBUG);
+                               $u = q("SELECT * FROM user WHERE uid = %d LIMIT 1",
+                                       intval($arr['uid']));
+
+                               $item = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d",
+                                       intval($current_post),
+                                       intval($arr['uid'])
+                               );
+
+                               $a = get_app();
+
+                               require_once('include/enotify.php');
+                               notification(array(
+                                       'type'         => NOTIFY_SHARE,
+                                       'notify_flags' => $u[0]['notify-flags'],
+                                       'language'     => $u[0]['language'],
+                                       'to_name'      => $u[0]['username'],
+                                       'to_email'     => $u[0]['email'],
+                                       'uid'          => $u[0]['uid'],
+                                       'item'         => $item[0],
+                                       'link'         => $a->get_baseurl().'/display/'.urlencode($arr['guid']),
+                                       'source_name'  => $item[0]['author-name'],
+                                       'source_link'  => $item[0]['author-link'],
+                                       'source_photo' => $item[0]['author-avatar'],
+                                       'verb'         => ACTIVITY_TAG,
+                                       'otype'        => 'item'
+                               ));
+                               logger('item_store: Notification sent for contact '.$arr['contact-id'].' and post '.$current_post, LOGGER_DEBUG);
+                       }
+               }
+
        } else {
                logger('item_store: could not locate created item');
                return 0;
@@ -1092,15 +1402,15 @@ function item_store($arr,$force_parent = false) {
        if((! $parent_id) || ($arr['parent-uri'] === $arr['uri']))
                $parent_id = $current_post;
 
-       if(strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid))
+       if(strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid))
                $private = 1;
        else
-               $private = $arr['private']; 
+               $private = $arr['private'];
 
-       // Set parent id - and also make sure to inherit the parent's ACL's.
+       // Set parent id - and also make sure to inherit the parent's ACLs.
 
        $r = q("UPDATE `item` SET `parent` = %d, `allow_cid` = '%s', `allow_gid` = '%s',
-               `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d, `deleted` = %d WHERE `id` = %d LIMIT 1",
+               `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d, `deleted` = %d WHERE `id` = %d",
                intval($parent_id),
                dbesc($allow_cid),
                dbesc($allow_gid),
@@ -1110,25 +1420,28 @@ function item_store($arr,$force_parent = false) {
                intval($parent_deleted),
                intval($current_post)
        );
-       create_tags_from_item($current_post);
 
-        $arr['id'] = $current_post;
-        $arr['parent'] = $parent_id;
-        $arr['allow_cid'] = $allow_cid;
-        $arr['allow_gid'] = $allow_gid;
-        $arr['deny_cid'] = $deny_cid;
-        $arr['deny_gid'] = $deny_gid;
-        $arr['private'] = $private;
-        $arr['deleted'] = $parent_deleted;
-       call_hooks('post_remote_end',$arr);
+       // Complete ostatus threads
+       if ($ostatus_conversation)
+               complete_conversation($current_post, $ostatus_conversation);
+
+       $arr['id'] = $current_post;
+       $arr['parent'] = $parent_id;
+       $arr['allow_cid'] = $allow_cid;
+       $arr['allow_gid'] = $allow_gid;
+       $arr['deny_cid'] = $deny_cid;
+       $arr['deny_gid'] = $deny_gid;
+       $arr['private'] = $private;
+       $arr['deleted'] = $parent_deleted;
 
        // update the commented timestamp on the parent
 
-       q("UPDATE `item` set `commented` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
+       q("UPDATE `item` set `commented` = '%s', `changed` = '%s' WHERE `id` = %d",
                dbesc(datetime_convert()),
                dbesc(datetime_convert()),
                intval($parent_id)
        );
+       update_thread($parent_id);
 
        if($dsprsig) {
                q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
@@ -1152,20 +1465,85 @@ function item_store($arr,$force_parent = false) {
                );
        }
 
-       tag_deliver($arr['uid'],$current_post);
+       $deleted = tag_deliver($arr['uid'],$current_post);
 
-       // Store the fresh generated item into the cache
-       $cachefile = get_cachefile($arr["guid"]."-".hash("md5", $arr['body']));
+       // current post can be deleted if is for a communuty page and no mention are
+       // in it.
+       if (!$deleted) {
+
+               // Store the fresh generated item into the cache
+               $cachefile = get_cachefile(urlencode($arr["guid"])."-".hash("md5", $arr['body']));
+
+               if (($cachefile != '') AND !file_exists($cachefile)) {
+                       $s = prepare_text($arr['body']);
+                       $a = get_app();
+                       $stamp1 = microtime(true);
+                       file_put_contents($cachefile, $s);
+                       $a->save_timestamp($stamp1, "file");
+                       logger('item_store: put item '.$current_post.' into cachefile '.$cachefile);
+               }
 
-       if (($cachefile != '') AND !file_exists($cachefile)) {
-               $s = prepare_text($arr['body']);
-               file_put_contents($cachefile, $s);
-               logger('item_store: put item '.$current_post.' into cachefile '.$cachefile);
+               $r = q('SELECT * FROM `item` WHERE id = %d', intval($current_post));
+               if (count($r) == 1) {
+                       call_hooks('post_remote_end', $r[0]);
+               } else {
+                       logger('item_store: new item not found in DB, id ' . $current_post);
+               }
        }
 
+       create_tags_from_item($current_post);
+       create_files_from_item($current_post);
+
+       if ($notify)
+               proc_run('php', "include/notifier.php", $notify_type, $current_post);
+
        return $current_post;
 }
 
+function get_item_guid($id) {
+       $r = q("SELECT `guid` FROM `item` WHERE `id` = %d LIMIT 1", intval($id));
+       if (count($r))
+               return($r[0]["guid"]);
+       else
+               return("");
+}
+
+function get_item_id($guid, $uid = 0) {
+
+       $nick = "";
+       $id = 0;
+
+       if ($uid == 0)
+               $uid == local_user();
+
+       // Does the given user have this item?
+       if ($uid) {
+               $r = q("SELECT `item`.`id`, `user`.`nickname` FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
+                       WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
+                               AND `item`.`guid` = '%s' AND `item`.`uid` = %d", dbesc($guid), intval($uid));
+               if (count($r)) {
+                       $id = $r[0]["id"];
+                       $nick = $r[0]["nickname"];
+               }
+       }
+
+       // Or is it anywhere on the server?
+       if ($nick == "") {
+               $r = q("SELECT `item`.`id`, `user`.`nickname` FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
+                       WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
+                               AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
+                               AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
+                               AND `item`.`private` = 0 AND `item`.`wall` = 1
+                               AND `item`.`guid` = '%s'", dbesc($guid));
+               if (count($r)) {
+                       $id = $r[0]["id"];
+                       $nick = $r[0]["nickname"];
+               }
+       }
+       return(array("nick" => $nick, "id" => $id));
+}
+
+// return - test
 function get_item_contact($item,$contacts) {
        if(! count($contacts) || (! is_array($item)))
                return false;
@@ -1178,10 +1556,15 @@ function get_item_contact($item,$contacts) {
        return false;
 }
 
-
+/**
+ * look for mention tags and setup a second delivery chain for forum/community posts if appropriate
+ * @param int $uid
+ * @param int $item_id
+ * @return bool true if item was deleted, else false
+ */
 function tag_deliver($uid,$item_id) {
 
-       // look for mention tags and setup a second delivery chain for forum/community posts if appropriate
+       //
 
        $a = get_app();
 
@@ -1223,8 +1606,21 @@ function tag_deliver($uid,$item_id) {
                }
        }
 
-       if(! $mention)
+       if(! $mention){
+               if ( ($community_page || $prvgroup) &&
+                         (!$item['wall']) && (!$item['origin']) && ($item['id'] == $item['parent'])){
+                       // mmh.. no mention.. community page or private group... no wall.. no origin.. top-post (not a comment)
+                       // delete it!
+                       logger("tag_deliver: no-mention top-level post to communuty or private group. delete.");
+                       q("DELETE FROM item WHERE id = %d and uid = %d",
+                               intval($item_id),
+                               intval($uid)
+                       );
+                       return true;
+               }
                return;
+       }
+
 
        // send a notification
 
@@ -1246,12 +1642,13 @@ function tag_deliver($uid,$item_id) {
                'to_email'     => $u[0]['email'],
                'uid'          => $u[0]['uid'],
                'item'         => $item,
-               'link'         => $a->get_baseurl() . '/display/' . $u[0]['nickname'] . '/' . $item['id'],
+               'link'         => $a->get_baseurl() . '/display/'.urlencode(get_item_guid($item['id'])),
                'source_name'  => $item['author-name'],
                'source_link'  => $item['author-link'],
                'source_photo' => $photo,
                'verb'         => ACTIVITY_TAG,
-               'otype'        => 'item'
+               'otype'        => 'item',
+               'parent'       => $item['parent']
        ));
 
 
@@ -1285,8 +1682,8 @@ function tag_deliver($uid,$item_id) {
 
        $forum_mode = (($prvgroup) ? 2 : 1);
 
-       q("update item set wall = 1, origin = 1, forum_mode = %d, `owner-name` = '%s', `owner-link` = '%s', `owner-avatar` = '%s', 
-               `private` = %d, `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'  where id = %d limit 1",
+       q("update item set wall = 1, origin = 1, forum_mode = %d, `owner-name` = '%s', `owner-link` = '%s', `owner-avatar` = '%s',
+               `private` = %d, `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'  where id = %d",
                intval($forum_mode),
                dbesc($c[0]['name']),
                dbesc($c[0]['url']),
@@ -1298,6 +1695,7 @@ function tag_deliver($uid,$item_id) {
                dbesc($u[0]['deny_gid']),
                intval($item_id)
        );
+       update_thread($item_id);
 
        proc_run('php','include/notifier.php','tgroup',$item_id);
 
@@ -1435,9 +1833,9 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
        $final_dfrn_id = '';
 
        if($perm) {
-               if((($perm == 'rw') && (! intval($contact['writable']))) 
+               if((($perm == 'rw') && (! intval($contact['writable'])))
                || (($perm == 'r') && (intval($contact['writable'])))) {
-                       q("update contact set writable = %d where id = %d limit 1",
+                       q("update contact set writable = %d where id = %d",
                                intval(($perm == 'rw') ? 1 : 0),
                                intval($contact['id'])
                        );
@@ -1445,7 +1843,7 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
                }
        }
 
-       if(($contact['duplex'] && strlen($contact['pubkey'])) 
+       if(($contact['duplex'] && strlen($contact['pubkey']))
                || ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
                || ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey']))) {
                openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
@@ -1463,7 +1861,7 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
 
        if($final_dfrn_id != $orig_id) {
                logger('dfrn_deliver: wrong dfrn_id.');
-               // did not decode properly - cannot trust this site 
+               // did not decode properly - cannot trust this site
                return 3;
        }
 
@@ -1486,16 +1884,16 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
 
        if($page)
                $postvars['page'] = $page;
-       
+
        if($rino && $rino_allowed && (! $dissolve)) {
                $key = substr(random_string(),0,16);
                $data = bin2hex(aes_encrypt($postvars['data'],$key));
                $postvars['data'] = $data;
-               logger('rino: sent key = ' . $key, LOGGER_DEBUG);       
+               logger('rino: sent key = ' . $key, LOGGER_DEBUG);
 
 
-               if($dfrn_version >= 2.1) {      
-                       if(($contact['duplex'] && strlen($contact['pubkey'])) 
+               if($dfrn_version >= 2.1) {
+                       if(($contact['duplex'] && strlen($contact['pubkey']))
                                || ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
                                || ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey']))) {
 
@@ -1546,10 +1944,30 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
 
        $res = parse_xml_string($xml);
 
-       return $res->status; 
+       return $res->status;
 }
 
 
+/*
+  This function returns true if $update has an edited timestamp newer
+  than $existing, i.e. $update contains new data which should override
+  what's already there.  If there is no timestamp yet, the update is
+  assumed to be newer.  If the update has no timestamp, the existing
+  item is assumed to be up-to-date.  If the timestamps are equal it
+  assumes the update has been seen before and should be ignored.
+  */
+function edited_timestamp_is_newer($existing, $update) {
+    if (!x($existing,'edited') || !$existing['edited']) {
+       return true;
+    }
+    if (!x($update,'edited') || !$update['edited']) {
+       return false;
+    }
+    $existing_edited = datetime_convert('UTC', 'UTC', $existing['edited']);
+    $update_edited = datetime_convert('UTC', 'UTC', $update['edited']);
+    return (strcmp($existing_edited, $update_edited) < 0);
+}
+
 /**
  *
  * consume_feed - process atom feed and update anything/everything we might need to update
@@ -1559,12 +1977,12 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
  * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
  *             It is this person's stuff that is going to be updated.
  * $contact =  the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
- *             from an external network and MAY create an appropriate contact record. Otherwise, we MUST 
+ *             from an external network and MAY create an appropriate contact record. Otherwise, we MUST
  *             have a contact record.
- * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or 
+ * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or
  *        might not) try and subscribe to it.
  * $datedir sorts in reverse order
- * $pass - by default ($pass = 0) we cannot guarantee that a parent item has been 
+ * $pass - by default ($pass = 0) we cannot guarantee that a parent item has been
  *      imported prior to its children being seen in the stream unless we are certain
  *      of how the feed is arranged/ordered.
  * With $pass = 1, we only pull parent items out of the stream.
@@ -1574,12 +1992,13 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
  * thing regardless of feed ordering. This won't be adequate in a fully-threaded
  * model where comments can have sub-threads. That would require some massive sorting
  * to get all the feed items into a mostly linear ordering, and might still require
- * recursion.  
+ * recursion.
  */
 
 function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0) {
 
        require_once('library/simplepie/simplepie.inc');
+       require_once('include/contact_selectors.php');
 
        if(! strlen($xml)) {
                logger('consume_feed: empty input');
@@ -1633,7 +2052,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
        }
 
        if((is_array($contact)) && ($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
-               logger('consume_feed: Updating photo for ' . $contact['name']);
+               logger('consume_feed: Updating photo for '.$contact['name'].' from '.$photo_url.' uid: '.$contact['uid']);
                require_once("include/Photo.php");
                $photo_failure = false;
                $have_photo = false;
@@ -1649,12 +2068,12 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                else {
                        $resource_id = photo_new_resource();
                }
-                       
+
                $img_str = fetch_url($photo_url,true);
                // guess mimetype from headers or filename
                $type = guess_image_type($photo_url,true);
-               
-               
+
+
                $img = new Photo($img_str, $type);
                if($img->is_valid()) {
                        if($have_photo) {
@@ -1679,7 +2098,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                        $a = get_app();
 
                        q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
-                               WHERE `uid` = %d AND `id` = %d LIMIT 1",
+                               WHERE `uid` = %d AND `id` = %d",
                                dbesc(datetime_convert()),
                                dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.'.$img->getExt()),
                                dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.'.$img->getExt()),
@@ -1696,7 +2115,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                        intval($contact['id'])
                );
 
-               $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
+               $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d",
                        dbesc(notags(trim($new_name))),
                        dbesc(datetime_convert()),
                        intval($contact['uid']),
@@ -1725,7 +2144,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                         *
                         * $bdtext is just a readable placeholder in case the event is shared
                         * with others. We will replace it during presentation to our $importer
-                        * to contain a sparkle link and perhaps a photo. 
+                        * to contain a sparkle link and perhaps a photo.
                         *
                         */
 
@@ -1736,7 +2155,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                        $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']),
-                               intval($contact['id']),
+                               intval($contact['id']),
                                dbesc(datetime_convert()),
                                dbesc(datetime_convert()),
                                dbesc(datetime_convert('UTC','UTC', $birthday)),
@@ -1749,14 +2168,14 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
 
                        // update bdyear
 
-                       q("UPDATE `contact` SET `bdyear` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
+                       q("UPDATE `contact` SET `bdyear` = '%s' WHERE `uid` = %d AND `id` = %d",
                                dbesc(substr($birthday,0,4)),
                                intval($contact['uid']),
                                intval($contact['id'])
                        );
 
                        // This function is called twice without reloading the contact
-                       // Make sure we only create one event. This is why &$contact 
+                       // Make sure we only create one event. This is why &$contact
                        // is a reference var in this function
 
                        $contact['bdyear'] = substr($birthday,0,4);
@@ -1770,7 +2189,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                $community_page = intval($rawtags[0]['data']);
        }
        if(is_array($contact) && intval($contact['forum']) != $community_page) {
-               q("update contact set forum = %d where id = %d limit 1",
+               q("update contact set forum = %d where id = %d",
                        intval($community_page),
                        intval($contact['id'])
                );
@@ -1795,7 +2214,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                        $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
                        }
                        if($deleted && is_array($contact)) {
-                               $r = q("SELECT `item`.*, `contact`.`self` FROM `item` left join `contact` on `item`.`contact-id` = `contact`.`id` 
+                               $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']),
@@ -1833,7 +2252,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                                                                        if(trim($tag) !== trim($xo->body))
                                                                                                $newtags[] = trim($tag);
                                                                        }
-                                                                       q("update item set tag = '%s' where id = %d limit 1",
+                                                                       q("update item set tag = '%s' where id = %d",
                                                                                dbesc(implode(',',$newtags)),
                                                                                intval($i[0]['id'])
                                                                        );
@@ -1853,17 +2272,20 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                                        intval($importer['uid'])
                                                );
                                                create_tags_from_itemuri($item['uri'], $importer['uid']);
+                                               create_files_from_itemuri($item['uri'], $importer['uid']);
+                                               update_thread_uri($item['uri'], $importer['uid']);
                                        }
                                        else {
                                                $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
-                                                       `body` = '', `title` = '' 
-                                                       WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+                                                       `body` = '', `title` = ''
+                                                       WHERE `uri` = '%s' AND `uid` = %d",
                                                        dbesc($when),
                                                        dbesc(datetime_convert()),
                                                        dbesc($uri),
                                                        intval($importer['uid'])
                                                );
                                                create_tags_from_itemuri($uri, $importer['uid']);
+                                               create_files_from_itemuri($uri, $importer['uid']);
                                                if($item['last-child']) {
                                                        // ensure that last-child is set in case the comment that had it just got wiped.
                                                        q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
@@ -1871,20 +2293,20 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                                                dbesc($item['parent-uri']),
                                                                intval($item['uid'])
                                                        );
-                                                       // who is the last child now? 
-                                                       $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `moderated` = 0 AND `uid` = %d 
+                                                       // who is the last child now?
+                                                       $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `moderated` = 0 AND `uid` = %d
                                                                ORDER BY `created` DESC LIMIT 1",
                                                                        dbesc($item['parent-uri']),
                                                                        intval($importer['uid'])
                                                        );
                                                        if(count($r)) {
-                                                               q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
+                                                               q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d",
                                                                        intval($r[0]['id'])
                                                                );
                                                        }
-                                               }       
+                                               }
                                        }
-                               }       
+                               }
                        }
                }
        }
@@ -1895,7 +2317,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
 
                logger('consume_feed: feed item count = ' . $feed->get_item_quantity());
 
-        // in inverse date order
+       // in inverse date order
                if ($datedir)
                        $items = array_reverse($feed->get_items());
                else
@@ -1926,7 +2348,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                // Have we seen it? If not, import it.
 
                                $item_id  = $item->get_id();
-                               $datarray = get_atom_elements($feed,$item);
+                               $datarray = get_atom_elements($feed, $item, $contact);
 
                                if((! x($datarray,'author-name')) && ($contact['network'] != NETWORK_DFRN))
                                        $datarray['author-name'] = $contact['name'];
@@ -1952,6 +2374,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                                intval($importer['uid'])
                                        );
                                        $datarray['last-child'] = 1;
+                                       update_thread_uri($parent_uri, $importer['uid']);
                                }
 
 
@@ -1963,21 +2386,23 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                // Update content if 'updated' changes
 
                                if(count($r)) {
-                                       if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
+                                       if (edited_timestamp_is_newer($r[0], $datarray)) {
 
                                                // do not accept (ignore) an earlier edit than one we currently have.
                                                if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
                                                        continue;
 
-                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
                                                        dbesc($datarray['title']),
                                                        dbesc($datarray['body']),
                                                        dbesc($datarray['tag']),
                                                        dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
+                                                       dbesc(datetime_convert()),
                                                        dbesc($item_id),
                                                        intval($importer['uid'])
                                                );
                                                create_tags_from_itemuri($item_id, $importer['uid']);
+                                               update_thread_uri($item_id, $importer['uid']);
                                        }
 
                                        // update last-child if it changes
@@ -1989,12 +2414,13 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                                        dbesc($parent_uri),
                                                        intval($importer['uid'])
                                                );
-                                               $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s'  WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+                                               $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s'  WHERE `uri` = '%s' AND `uid` = %d",
                                                        intval($allow[0]['data']),
                                                        dbesc(datetime_convert()),
                                                        dbesc($item_id),
                                                        intval($importer['uid'])
                                                );
+                                               update_thread_uri($item_id, $importer['uid']);
                                        }
                                        continue;
                                }
@@ -2011,11 +2437,20 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                        $datarray['type'] = 'activity';
                                        $datarray['gravity'] = GRAVITY_LIKE;
                                        // only one like or dislike per person
-                                       $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`parent-uri` = '%s' OR `thr-parent` = '%s') limit 1",
+                                       // splitted into two queries for performance issues
+                                       $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`parent-uri` = '%s') limit 1",
+                                               intval($datarray['uid']),
+                                               intval($datarray['contact-id']),
+                                               dbesc($datarray['verb']),
+                                               dbesc($parent_uri)
+                                       );
+                                       if($r && count($r))
+                                               continue;
+
+                                       $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`thr-parent` = '%s') limit 1",
                                                intval($datarray['uid']),
                                                intval($datarray['contact-id']),
                                                dbesc($datarray['verb']),
-                                               dbesc($parent_uri),
                                                dbesc($parent_uri)
                                        );
                                        if($r && count($r))
@@ -2038,7 +2473,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                                if($xo->id && $xo->content) {
                                                        $newtag = '#[url=' . $xo->id . ']'. $xo->content . '[/url]';
                                                        if(! (stristr($r[0]['tag'],$newtag))) {
-                                                               q("UPDATE item SET tag = '%s' WHERE id = %d LIMIT 1",
+                                                               q("UPDATE item SET tag = '%s' WHERE id = %d",
                                                                        dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . $newtag),
                                                                        intval($r[0]['id'])
                                                                );
@@ -2058,7 +2493,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
 
                                $item_id  = $item->get_id();
 
-                               $datarray = get_atom_elements($feed,$item);
+                               $datarray = get_atom_elements($feed, $item, $contact);
 
                                if(is_array($contact)) {
                                        if((! x($datarray,'author-name')) && ($contact['network'] != NETWORK_DFRN))
@@ -2112,33 +2547,36 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                // Update content if 'updated' changes
 
                                if(count($r)) {
-                                       if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
+                                       if (edited_timestamp_is_newer($r[0], $datarray)) {
 
                                                // do not accept (ignore) an earlier edit than one we currently have.
                                                if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
                                                        continue;
 
-                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
                                                        dbesc($datarray['title']),
                                                        dbesc($datarray['body']),
                                                        dbesc($datarray['tag']),
                                                        dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
+                                                       dbesc(datetime_convert()),
                                                        dbesc($item_id),
                                                        intval($importer['uid'])
                                                );
                                                create_tags_from_itemuri($item_id, $importer['uid']);
+                                               update_thread_uri($item_id, $importer['uid']);
                                        }
 
                                        // update last-child if it changes
 
                                        $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
                                        if($allow && $allow[0]['data'] != $r[0]['last-child']) {
-                                               $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+                                               $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
                                                        intval($allow[0]['data']),
                                                        dbesc(datetime_convert()),
                                                        dbesc($item_id),
                                                        intval($importer['uid'])
                                                );
+                                               update_thread_uri($item_id, $importer['uid']);
                                        }
                                        continue;
                                }
@@ -2175,40 +2613,34 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                if($contact['network'] === NETWORK_FEED)
                                        $datarray['private'] = 2;
 
-                               // This is my contact on another system, but it's really me.
-                               // Turn this into a wall post.
-
-                               if($contact['remote_self']) {
-                                       $datarray['wall'] = 1;
-                                       if($contact['network'] === NETWORK_FEED) {
-                                               $datarray['private'] = 0;
-                                       }
-                               }
-
                                $datarray['parent-uri'] = $item_id;
                                $datarray['uid'] = $importer['uid'];
                                $datarray['contact-id'] = $contact['id'];
 
                                if(! link_compare($datarray['owner-link'],$contact['url'])) {
-                                       // The item owner info is not our contact. It's OK and is to be expected if this is a tgroup delivery, 
+                                       // 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,
-                                       // but we're going to unconditionally correct it here so that the post will always be owned by our contact. 
+                                       // but we're going to unconditionally correct it here so that the post will always be owned by our contact.
                                        logger('consume_feed: Correcting item owner.', LOGGER_DEBUG);
                                        $datarray['owner-name']   = $contact['name'];
                                        $datarray['owner-link']   = $contact['url'];
                                        $datarray['owner-avatar'] = $contact['thumb'];
                                }
 
-                               // We've allowed "followers" to reach this point so we can decide if they are 
+                               // We've allowed "followers" to reach this point so we can decide if they are
                                // posting an @-tag delivery, which followers are allowed to do for certain
-                               // page types. Now that we've parsed the post, let's check if it is legit. Otherwise ignore it. 
+                               // page types. Now that we've parsed the post, let's check if it is legit. Otherwise ignore it.
 
                                if(($contact['rel'] == CONTACT_IS_FOLLOWER) && (! tgroup_check($importer['uid'],$datarray)))
                                        continue;
 
+                               // This is my contact on another system, but it's really me.
+                               // Turn this into a wall post.
+                               $notify = item_is_remote_self($contact, $datarray);
 
-                               $r = item_store($datarray);
+                               $r = item_store($datarray, false, $notify);
+                               logger('Stored - Contact '.$contact['url'].' Notify '.$notify.' return '.$r.' Item '.print_r($datarray, true), LOGGER_DEBUG);
                                continue;
 
                        }
@@ -2216,10 +2648,69 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
        }
 }
 
+function item_is_remote_self($contact, &$datarray) {
+       $a = get_app();
+
+       if (!$contact['remote_self'])
+               return false;
+
+       // Prevent the forwarding of posts that are forwarded
+       if ($datarray["extid"] == NETWORK_DFRN)
+               return false;
+
+       // Prevent to forward already forwarded posts
+       if ($datarray["app"] == $a->get_hostname())
+               return false;
+
+       if (($contact['network'] != NETWORK_FEED) AND $datarray['private'])
+               return false;
+
+       $datarray2 = $datarray;
+       logger('remote-self start - Contact '.$contact['url'].' - '.$contact['remote_self'].' Item '.print_r($datarray, true), LOGGER_DEBUG);
+       if ($contact['remote_self'] == 2) {
+               $r = q("SELECT `id`,`url`,`name`,`photo`,`network` FROM `contact` WHERE `uid` = %d AND `self`",
+                       intval($contact['uid']));
+               if (count($r)) {
+                       $datarray['contact-id'] = $r[0]["id"];
+
+                       $datarray['owner-name'] = $r[0]["name"];
+                       $datarray['owner-link'] = $r[0]["url"];
+                       $datarray['owner-avatar'] = $r[0]["avatar"];
+
+                       $datarray['author-name']   = $datarray['owner-name'];
+                       $datarray['author-link']   = $datarray['owner-link'];
+                       $datarray['author-avatar'] = $datarray['owner-avatar'];
+               }
+
+               if ($contact['network'] != NETWORK_FEED) {
+                       $datarray["guid"] = get_guid(32);
+                       unset($datarray["plink"]);
+                       $datarray["uri"] = item_new_uri($a->get_hostname(),$contact['uid']);
+                       $datarray["parent-uri"] = $datarray["uri"];
+                       $datarray["extid"] = $contact['network'];
+                       $urlpart = parse_url($datarray2['author-link']);
+                       $datarray["app"] = $urlpart["host"];
+               } else
+                       $datarray['private'] = 0;
+       }
+
+       //if (!isset($datarray["app"]) OR ($datarray["app"] == ""))
+       //      $datarray["app"] = network_to_name($contact['network']);
+
+       if ($contact['network'] != NETWORK_FEED) {
+               // Store the original post
+               $r = item_store($datarray2, false, false);
+               logger('remote-self post original item - Contact '.$contact['url'].' return '.$r.' Item '.print_r($datarray2, true), LOGGER_DEBUG);
+       } else
+               $datarray["app"] = "Feed";
+
+       return true;
+}
+
 function local_delivery($importer,$data) {
        $a = get_app();
 
-    logger(__function__, LOGGER_TRACE);
+       logger(__function__, LOGGER_TRACE);
 
        if($importer['readonly']) {
                // We aren't receiving stuff from this person. But we will quietly ignore them
@@ -2288,12 +2779,12 @@ function local_delivery($importer,$data) {
                else {
                        $resource_id = photo_new_resource();
                }
-                       
+
                $img_str = fetch_url($photo_url,true);
                // guess mimetype from headers or filename
                $type = guess_image_type($photo_url,true);
-               
-               
+
+
                $img = new Photo($img_str, $type);
                if($img->is_valid()) {
                        if($have_photo) {
@@ -2318,7 +2809,7 @@ function local_delivery($importer,$data) {
                        $a = get_app();
 
                        q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
-                               WHERE `uid` = %d AND `id` = %d LIMIT 1",
+                               WHERE `uid` = %d AND `id` = %d",
                                dbesc(datetime_convert()),
                                dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.'.$img->getExt()),
                                dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.'.$img->getExt()),
@@ -2335,7 +2826,7 @@ function local_delivery($importer,$data) {
                        intval($importer['id'])
                );
 
-               $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
+               $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d",
                        dbesc(notags(trim($new_name))),
                        dbesc(datetime_convert()),
                        intval($importer['importer_uid']),
@@ -2377,65 +2868,65 @@ function local_delivery($importer,$data) {
                /*$newloc['pubkey'] = notags(unxmlify($base['pubkey'][0]['data']));
                $newloc['prvkey'] = notags(unxmlify($base['prvkey'][0]['data']));*/
 
-        logger("items:relocate contact ".print_r($newloc, true).print_r($importer, true), LOGGER_DEBUG);
+               logger("items:relocate contact ".print_r($newloc, true).print_r($importer, true), LOGGER_DEBUG);
 
-        // update contact
-        $r = q("SELECT photo, url FROM contact WHERE id=%d AND uid=%d;",
-                    intval($importer['id']),
-                                       intval($importer['importer_uid']));
+               // update contact
+               $r = q("SELECT photo, url FROM contact WHERE id=%d AND uid=%d;",
+                       intval($importer['id']),
+                       intval($importer['importer_uid']));
                if ($r === false)
                        return 1;
-        $old = $r[0];
-
-        $x = q("UPDATE contact SET
-                        name = '%s',
-                        photo = '%s',
-                        thumb = '%s',
-                        micro = '%s',
-                        url = '%s',
-                        request = '%s',
-                        confirm = '%s',
-                        notify = '%s',
-                        poll = '%s',
-                        `site-pubkey` = '%s'
-                WHERE id=%d AND uid=%d;",
-                    dbesc($newloc['name']),
-                    dbesc($newloc['photo']),
-                    dbesc($newloc['thumb']),
-                    dbesc($newloc['micro']),
-                    dbesc($newloc['url']),
-                    dbesc($newloc['request']),
-                    dbesc($newloc['confirm']),
-                    dbesc($newloc['notify']),
-                    dbesc($newloc['poll']),
-                    dbesc($newloc['sitepubkey']),
-                    intval($importer['id']),
+               $old = $r[0];
+
+               $x = q("UPDATE contact SET
+                                       name = '%s',
+                                       photo = '%s',
+                                       thumb = '%s',
+                                       micro = '%s',
+                                       url = '%s',
+                                       request = '%s',
+                                       confirm = '%s',
+                                       notify = '%s',
+                                       poll = '%s',
+                                       `site-pubkey` = '%s'
+                       WHERE id=%d AND uid=%d;",
+                                       dbesc($newloc['name']),
+                                       dbesc($newloc['photo']),
+                                       dbesc($newloc['thumb']),
+                                       dbesc($newloc['micro']),
+                                       dbesc($newloc['url']),
+                                       dbesc($newloc['request']),
+                                       dbesc($newloc['confirm']),
+                                       dbesc($newloc['notify']),
+                                       dbesc($newloc['poll']),
+                                       dbesc($newloc['sitepubkey']),
+                                       intval($importer['id']),
                                        intval($importer['importer_uid']));
 
-        if ($x === false)
+               if ($x === false)
                        return 1;
-        // update items
-        $fields = array(
-            'owner-link' => array($old['url'], $newloc['url']),
-            'author-link' => array($old['url'], $newloc['url']),
-            'owner-avatar' => array($old['photo'], $newloc['photo']),
-            'author-avatar' => array($old['photo'], $newloc['photo']),
-        );
-        foreach ($fields as $n=>$f){
-            $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 1;
-               }
+               // update items
+               $fields = array(
+                       'owner-link' => array($old['url'], $newloc['url']),
+                       'author-link' => array($old['url'], $newloc['url']),
+                       'owner-avatar' => array($old['photo'], $newloc['photo']),
+                       'author-avatar' => array($old['photo'], $newloc['photo']),
+                       );
+               foreach ($fields as $n=>$f){
+                       $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 1;
+                       }
 
                // TODO
                // merge with current record, current contents have priority
                // update record, set url-updated
                // update profile photos
                // schedule a scan?
-        return 0;
+               return 0;
        }
 
 
@@ -2560,7 +3051,7 @@ function local_delivery($importer,$data) {
 
                dbesc_array($msg);
 
-               $r = dbq("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg)) 
+               $r = dbq("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg))
                        . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
 
                // send notifications.
@@ -2594,7 +3085,7 @@ function local_delivery($importer,$data) {
                $community_page = intval($rawtags[0]['data']);
        }
        if(intval($importer['forum']) != $community_page) {
-               q("update contact set forum = %d where id = %d limit 1",
+               q("update contact set forum = %d where id = %d",
                        intval($community_page),
                        intval($importer['id'])
                );
@@ -2623,7 +3114,7 @@ function local_delivery($importer,$data) {
 
                                // check for relayed deletes to our conversation
 
-                               $is_reply = false;              
+                               $is_reply = false;
                                $r = q("select * from item where uri = '%s' and uid = %d limit 1",
                                        dbesc($uri),
                                        intval($importer['importer_uid'])
@@ -2632,7 +3123,7 @@ function local_delivery($importer,$data) {
                                        $parent_uri = $r[0]['parent-uri'];
                                        if($r[0]['id'] != $r[0]['parent'])
                                                $is_reply = true;
-                               }                               
+                               }
 
                                if($is_reply) {
                                        $community = false;
@@ -2644,18 +3135,18 @@ function local_delivery($importer,$data) {
                                        }
                                        else
                                                $sql_extra = " and contact.self = 1 and item.wall = 1 ";
-                                       // was the top-level post for this reply written by somebody on this site? 
-                                       // Specifically, the recipient? 
+
+                                       // was the top-level post for this reply written by somebody on this site?
+                                       // Specifically, the recipient?
 
                                        $is_a_remote_delete = false;
 
                                        // POSSIBLE CLEANUP --> Why select so many fields when only forum_mode and wall are used?
-                                       $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, `item`.`forum_mode`,`item`.`origin`,`item`.`wall`, 
-                                               `contact`.`name`, `contact`.`url`, `contact`.`thumb` from `item` 
-                                               LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id` 
+                                       $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, `item`.`forum_mode`,`item`.`origin`,`item`.`wall`,
+                                               `contact`.`name`, `contact`.`url`, `contact`.`thumb` 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')
-                                               AND `item`.`uid` = %d 
+                                               AND `item`.`uid` = %d
                                                $sql_extra
                                                LIMIT 1",
                                                dbesc($parent_uri),
@@ -2667,8 +3158,8 @@ function local_delivery($importer,$data) {
                                                $is_a_remote_delete = true;
 
                                        // Does this have the characteristics of a community or private group comment?
-                                       // If it's a reply to a wall post on a community/prvgroup page it's a 
-                                       // valid community comment. Also forum_mode makes it valid for sure. 
+                                       // If it's a reply to a wall post on a community/prvgroup page it's a
+                                       // valid community comment. Also forum_mode makes it valid for sure.
                                        // If neither, it's not.
 
                                        if($is_a_remote_delete && $community) {
@@ -2683,7 +3174,7 @@ function local_delivery($importer,$data) {
                                        }
                                }
 
-                               $r = q("SELECT `item`.*, `contact`.`self` FROM `item` left join contact on `item`.`contact-id` = `contact`.`id`
+                               $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['importer_uid']),
@@ -2725,7 +3216,7 @@ function local_delivery($importer,$data) {
                                                                                        if(trim($tag) !== trim($xo->body))
                                                                                                $newtags[] = trim($tag);
                                                                        }
-                                                                       q("update item set tag = '%s' where id = %d limit 1",
+                                                                       q("update item set tag = '%s' where id = %d",
                                                                                dbesc(implode(',',$newtags)),
                                                                                intval($i[0]['id'])
                                                                        );
@@ -2745,17 +3236,21 @@ function local_delivery($importer,$data) {
                                                        intval($importer['importer_uid'])
                                                );
                                                create_tags_from_itemuri($item['uri'], $importer['importer_uid']);
+                                               create_files_from_itemuri($item['uri'], $importer['importer_uid']);
+                                               update_thread_uri($item['uri'], $importer['importer_uid']);
                                        }
                                        else {
                                                $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
                                                        `body` = '', `title` = ''
-                                                       WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+                                                       WHERE `uri` = '%s' AND `uid` = %d",
                                                        dbesc($when),
                                                        dbesc(datetime_convert()),
                                                        dbesc($uri),
                                                        intval($importer['importer_uid'])
                                                );
                                                create_tags_from_itemuri($uri, $importer['importer_uid']);
+                                               create_files_from_itemuri($uri, $importer['importer_uid']);
+                                               update_thread_uri($uri, $importer['importer_uid']);
                                                if($item['last-child']) {
                                                        // ensure that last-child is set in case the comment that had it just got wiped.
                                                        q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
@@ -2770,7 +3265,7 @@ function local_delivery($importer,$data) {
                                                                        intval($importer['importer_uid'])
                                                        );
                                                        if(count($r)) {
-                                                               q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
+                                                               q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d",
                                                                        intval($r[0]['id'])
                                                                );
                                                        }
@@ -2788,7 +3283,7 @@ function local_delivery($importer,$data) {
 
        foreach($feed->get_items() as $item) {
 
-               $is_reply = false;              
+               $is_reply = false;
                $item_id = $item->get_id();
                $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
                if(isset($rawthread[0]['attribs']['']['ref'])) {
@@ -2806,13 +3301,13 @@ function local_delivery($importer,$data) {
                        }
                        else
                                $sql_extra = " and contact.self = 1 and item.wall = 1 ";
-                       // was the top-level post for this reply written by somebody on this site? 
-                       // Specifically, the recipient? 
+
+                       // was the top-level post for this reply written by somebody on this site?
+                       // Specifically, the recipient?
 
                        $is_a_remote_comment = false;
                        $top_uri = $parent_uri;
-                       
+
                        $r = q("select `item`.`parent-uri` from `item`
                                WHERE `item`.`uri` = '%s'
                                LIMIT 1",
@@ -2822,11 +3317,11 @@ function local_delivery($importer,$data) {
                                $top_uri = $r[0]['parent-uri'];
 
                                // POSSIBLE CLEANUP --> Why select so many fields when only forum_mode and wall are used?
-                               $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, `item`.`forum_mode`,`item`.`origin`,`item`.`wall`, 
-                                       `contact`.`name`, `contact`.`url`, `contact`.`thumb` from `item` 
-                                       LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id` 
+                               $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, `item`.`forum_mode`,`item`.`origin`,`item`.`wall`,
+                                       `contact`.`name`, `contact`.`url`, `contact`.`thumb` 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')
-                                       AND `item`.`uid` = %d 
+                                       AND `item`.`uid` = %d
                                        $sql_extra
                                        LIMIT 1",
                                        dbesc($top_uri),
@@ -2839,8 +3334,8 @@ function local_delivery($importer,$data) {
                        }
 
                        // Does this have the characteristics of a community or private group comment?
-                       // If it's a reply to a wall post on a community/prvgroup page it's a 
-                       // valid community comment. Also forum_mode makes it valid for sure. 
+                       // If it's a reply to a wall post on a community/prvgroup page it's a
+                       // valid community comment. Also forum_mode makes it valid for sure.
                        // If neither, it's not.
 
                        if($is_a_remote_comment && $community) {
@@ -2855,7 +3350,7 @@ function local_delivery($importer,$data) {
                                $is_like = false;
                                // remote reply to our post. Import and then notify everybody else.
 
-                               $datarray = get_atom_elements($feed,$item);
+                               $datarray = get_atom_elements($feed, $item);
 
                                $r = q("SELECT `id`, `uid`, `last-child`, `edited`, `body`  FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
                                        dbesc($item_id),
@@ -2866,18 +3361,19 @@ function local_delivery($importer,$data) {
 
                                if(count($r)) {
                                        $iid = $r[0]['id'];
-                                       if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
+                                       if (edited_timestamp_is_newer($r[0], $datarray)) {
 
                                                // do not accept (ignore) an earlier edit than one we currently have.
                                                if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
                                                        continue;
 
                                                logger('received updated comment' , LOGGER_DEBUG);
-                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
                                                        dbesc($datarray['title']),
                                                        dbesc($datarray['body']),
                                                        dbesc($datarray['tag']),
                                                        dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
+                                                       dbesc(datetime_convert()),
                                                        dbesc($item_id),
                                                        intval($importer['importer_uid'])
                                                );
@@ -2912,11 +3408,21 @@ function local_delivery($importer,$data) {
                                        $datarray['gravity'] = GRAVITY_LIKE;
                                        $datarray['last-child'] = 0;
                                        // only one like or dislike per person
-                                       $r = q("select id from item where uid = %d and `contact-id` = %d and verb = '%s' and (`thr-parent` = '%s' or `parent-uri` = '%s') and deleted = 0 limit 1",
+                                       // splitted into two queries for performance issues
+                                       $r = q("select id from item where uid = %d and `contact-id` = %d and verb = '%s' and (`parent-uri` = '%s') and deleted = 0 limit 1",
+                                               intval($datarray['uid']),
+                                               intval($datarray['contact-id']),
+                                               dbesc($datarray['verb']),
+                                               dbesc($datarray['parent-uri'])
+
+                                       );
+                                       if($r && count($r))
+                                               continue;
+
+                                       $r = q("select id from item where uid = %d and `contact-id` = %d and verb = '%s' and (`thr-parent` = '%s') and deleted = 0 limit 1",
                                                intval($datarray['uid']),
                                                intval($datarray['contact-id']),
                                                dbesc($datarray['verb']),
-                                               dbesc($datarray['parent-uri']),
                                                dbesc($datarray['parent-uri'])
 
                                        );
@@ -2949,9 +3455,10 @@ function local_delivery($importer,$data) {
                                                                        intval($importer['importer_uid'])
                                                                );
                                                                if(count($i) && ! intval($i[0]['blocktags'])) {
-                                                                       q("UPDATE item SET tag = '%s', `edited` = '%s' WHERE id = %d LIMIT 1",
+                                                                       q("UPDATE item SET tag = '%s', `edited` = '%s', `changed` = '%s' WHERE id = %d",
                                                                                dbesc($tagp[0]['tag'] . (strlen($tagp[0]['tag']) ? ',' : '') . $newtag),
                                                                                intval($tagp[0]['id']),
+                                                                               dbesc(datetime_convert()),
                                                                                dbesc(datetime_convert())
                                                                        );
                                                                        create_tags_from_item($tagp[0]['id']);
@@ -2982,7 +3489,7 @@ function local_delivery($importer,$data) {
                                                        intval($r[0]['parent'])
                                                );
 
-                                               $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
+                                               $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d",
                                                        dbesc(datetime_convert()),
                                                        intval($importer['importer_uid']),
                                                        intval($posted_id)
@@ -3005,10 +3512,10 @@ function local_delivery($importer,$data) {
                                                                'to_email'     => $importer['email'],
                                                                'uid'          => $importer['importer_uid'],
                                                                'item'         => $datarray,
-                                                               'link'             => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
+                                                               'link'             => $a->get_baseurl().'/display/'.urlencode(get_item_guid($posted_id)),
                                                                'source_name'  => stripslashes($datarray['author-name']),
                                                                'source_link'  => $datarray['author-link'],
-                                                               'source_photo' => ((link_compare($datarray['author-link'],$importer['url'])) 
+                                                               'source_photo' => ((link_compare($datarray['author-link'],$importer['url']))
                                                                        ? $importer['thumb'] : $datarray['author-avatar']),
                                                                'verb'         => ACTIVITY_POST,
                                                                'otype'        => 'item',
@@ -3041,17 +3548,18 @@ function local_delivery($importer,$data) {
                                // Update content if 'updated' changes
 
                                if(count($r)) {
-                                       if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
+                                       if (edited_timestamp_is_newer($r[0], $datarray)) {
 
                                                // do not accept (ignore) an earlier edit than one we currently have.
                                                if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
                                                        continue;
 
-                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
                                                        dbesc($datarray['title']),
                                                        dbesc($datarray['body']),
                                                        dbesc($datarray['tag']),
                                                        dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
+                                                       dbesc(datetime_convert()),
                                                        dbesc($item_id),
                                                        intval($importer['importer_uid'])
                                                );
@@ -3067,7 +3575,7 @@ function local_delivery($importer,$data) {
                                                        dbesc($parent_uri),
                                                        intval($importer['importer_uid'])
                                                );
-                                               $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s'  WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+                                               $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s'  WHERE `uri` = '%s' AND `uid` = %d",
                                                        intval($allow[0]['data']),
                                                        dbesc(datetime_convert()),
                                                        dbesc($item_id),
@@ -3084,11 +3592,20 @@ function local_delivery($importer,$data) {
                                        $datarray['type'] = 'activity';
                                        $datarray['gravity'] = GRAVITY_LIKE;
                                        // only one like or dislike per person
-                                       $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`parent-uri` = '%s' OR `thr-parent` = '%s') limit 1",
+                                       // splitted into two queries for performance issues
+                                       $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`parent-uri` = '%s') limit 1",
+                                               intval($datarray['uid']),
+                                               intval($datarray['contact-id']),
+                                               dbesc($datarray['verb']),
+                                               dbesc($parent_uri)
+                                       );
+                                       if($r && count($r))
+                                               continue;
+
+                                       $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`thr-parent` = '%s') limit 1",
                                                intval($datarray['uid']),
                                                intval($datarray['contact-id']),
                                                dbesc($datarray['verb']),
-                                               dbesc($parent_uri),
                                                dbesc($parent_uri)
                                        );
                                        if($r && count($r))
@@ -3112,7 +3629,7 @@ function local_delivery($importer,$data) {
                                                // 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 LIMIT 1",
+                                                               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'])
                                                                );
@@ -3148,7 +3665,7 @@ function local_delivery($importer,$data) {
                                                                        continue;
 
                                                                require_once('include/enotify.php');
-                                                               
+
                                                                $conv_parent = $conv['parent'];
 
                                                                notification(array(
@@ -3159,10 +3676,10 @@ function local_delivery($importer,$data) {
                                                                        'to_email'     => $importer['email'],
                                                                        'uid'          => $importer['importer_uid'],
                                                                        'item'         => $datarray,
-                                                                       'link'             => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
+                                                                       'link'             => $a->get_baseurl().'/display/'.urlencode(get_item_guid($posted_id)),
                                                                        'source_name'  => stripslashes($datarray['author-name']),
                                                                        'source_link'  => $datarray['author-link'],
-                                                                       'source_photo' => ((link_compare($datarray['author-link'],$importer['url'])) 
+                                                                       'source_photo' => ((link_compare($datarray['author-link'],$importer['url']))
                                                                                ? $importer['thumb'] : $datarray['author-avatar']),
                                                                        'verb'         => ACTIVITY_POST,
                                                                        'otype'        => 'item',
@@ -3217,28 +3734,30 @@ function local_delivery($importer,$data) {
                        // Update content if 'updated' changes
 
                        if(count($r)) {
-                               if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
+                               if (edited_timestamp_is_newer($r[0], $datarray)) {
 
                                        // do not accept (ignore) an earlier edit than one we currently have.
                                        if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
                                                continue;
 
-                                       $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+                                       $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
                                                dbesc($datarray['title']),
                                                dbesc($datarray['body']),
                                                dbesc($datarray['tag']),
                                                dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
+                                               dbesc(datetime_convert()),
                                                dbesc($item_id),
                                                intval($importer['importer_uid'])
                                        );
                                        create_tags_from_itemuri($item_id, $importer['importer_uid']);
+                                       update_thread_uri($item_id, $importer['importer_uid']);
                                }
 
                                // update last-child if it changes
 
                                $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
                                if($allow && $allow[0]['data'] != $r[0]['last-child']) {
-                                       $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+                                       $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
                                                intval($allow[0]['data']),
                                                dbesc(datetime_convert()),
                                                dbesc($item_id),
@@ -3248,22 +3767,16 @@ function local_delivery($importer,$data) {
                                continue;
                        }
 
-                       // This is my contact on another system, but it's really me.
-                       // Turn this into a wall post.
-
-                       if($importer['remote_self'])
-                               $datarray['wall'] = 1;
-
                        $datarray['parent-uri'] = $item_id;
                        $datarray['uid'] = $importer['importer_uid'];
                        $datarray['contact-id'] = $importer['id'];
 
 
                        if(! link_compare($datarray['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, 
+                               // 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,
-                               // but we're going to unconditionally correct it here so that the post will always be owned by our contact. 
+                               // but we're going to unconditionally correct it here so that the post will always be owned by our contact.
                                logger('local_delivery: Correcting item owner.', LOGGER_DEBUG);
                                $datarray['owner-name']   = $importer['senderName'];
                                $datarray['owner-link']   = $importer['url'];
@@ -3273,7 +3786,11 @@ function local_delivery($importer,$data) {
                        if(($importer['rel'] == CONTACT_IS_FOLLOWER) && (! tgroup_check($importer['importer_uid'],$datarray)))
                                continue;
 
-                       $posted_id = item_store($datarray);
+                       // This is my contact on another system, but it's really me.
+                       // Turn this into a wall post.
+                       $notify = item_is_remote_self($importer, $datarray);
+
+                       $posted_id = item_store($datarray, false, $notify);
 
                        if(stristr($datarray['verb'],ACTIVITY_POKE)) {
                                $verb = urldecode(substr($datarray['verb'],strpos($datarray['verb'],'#')+1));
@@ -3287,21 +3804,21 @@ function local_delivery($importer,$data) {
 
                                        $links = parse_xml_string("<links>".unxmlify($xo->link)."</links>",false);
 
-                               foreach($links->link as $l) {
-                               $atts = $l->attributes();
-                               switch($atts['rel']) {
-                                       case "alternate": 
+                               foreach($links->link as $l) {
+                               $atts = $l->attributes();
+                               switch($atts['rel']) {
+                                       case "alternate":
                                                                $Blink = $atts['href'];
                                                                break;
                                                        default:
                                                                break;
-                                   }
-                               }
+                                   }
+                               }
                                        if($Blink && link_compare($Blink,$a->get_baseurl() . '/profile/' . $importer['nickname'])) {
 
                                                // send a notification
                                                require_once('include/enotify.php');
-                                                               
+
                                                notification(array(
                                                        'type'         => NOTIFY_POKE,
                                                        'notify_flags' => $importer['notify-flags'],
@@ -3310,10 +3827,10 @@ function local_delivery($importer,$data) {
                                                        'to_email'     => $importer['email'],
                                                        'uid'          => $importer['importer_uid'],
                                                        'item'         => $datarray,
-                                                       'link'             => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
+                                                       'link'             => $a->get_baseurl().'/display/'.urlencode(get_item_guid($posted_id)),
                                                        'source_name'  => stripslashes($datarray['author-name']),
                                                        'source_link'  => $datarray['author-link'],
-                                                       'source_photo' => ((link_compare($datarray['author-link'],$importer['url'])) 
+                                                       'source_photo' => ((link_compare($datarray['author-link'],$importer['url']))
                                                                ? $importer['thumb'] : $datarray['author-avatar']),
                                                        'verb'         => $datarray['verb'],
                                                        'otype'        => 'person',
@@ -3322,7 +3839,7 @@ function local_delivery($importer,$data) {
                                                ));
                                        }
                                }
-                       }                       
+                       }
 
                        continue;
                }
@@ -3346,7 +3863,7 @@ function new_follower($importer,$contact,$datarray,$item,$sharing = false) {
        if(is_array($contact)) {
                if(($contact['network'] == NETWORK_OSTATUS && $contact['rel'] == CONTACT_IS_SHARING)
                        || ($sharing && $contact['rel'] == CONTACT_IS_FOLLOWER)) {
-                       $r = q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
+                       $r = q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d",
                                intval(CONTACT_IS_FRIEND),
                                intval($contact['id']),
                                intval($importer['uid'])
@@ -3358,7 +3875,7 @@ function new_follower($importer,$contact,$datarray,$item,$sharing = false) {
 
                // create contact record
 
-               $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `name`, `nick`, `photo`, `network`, `rel`, 
+               $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `name`, `nick`, `photo`, `network`, `rel`,
                        `blocked`, `readonly`, `pending`, `writable` )
                        VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 1, 1 ) ",
                        intval($importer['uid']),
@@ -3390,6 +3907,7 @@ function new_follower($importer,$contact,$datarray,$item,$sharing = false) {
                                dbesc(datetime_convert())
                        );
                }
+
                $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
                        intval($importer['uid'])
                );
@@ -3401,21 +3919,26 @@ function new_follower($importer,$contact,$datarray,$item,$sharing = false) {
                                group_add_member($r[0]['uid'],'',$contact_record['id'],$r[0]['def_gid']);
                        }
 
-                       if(($r[0]['notify-flags'] & NOTIFY_INTRO) && ($r[0]['page-flags'] == PAGE_NORMAL)) {
-                               $email_tpl = get_intltext_template('follow_notify_eml.tpl');
-                               $email = replace_macros($email_tpl, array(
-                                       '$requestor' => ((strlen($name)) ? $name : t('[Name Withheld]')),
-                                       '$url' => $url,
-                                       '$myname' => $r[0]['username'],
-                                       '$siteurl' => $a->get_baseurl(),
-                                       '$sitename' => $a->config['sitename']
+                       if(($r[0]['notify-flags'] & NOTIFY_INTRO) &&
+                               (($r[0]['page-flags'] == PAGE_NORMAL) OR ($r[0]['page-flags'] == PAGE_SOAPBOX))) {
+
+
+
+                               notification(array(
+                                       'type'         => NOTIFY_INTRO,
+                                       'notify_flags' => $r[0]['notify-flags'],
+                                       'language'     => $r[0]['language'],
+                                       'to_name'      => $r[0]['username'],
+                                       'to_email'     => $r[0]['email'],
+                                       'uid'          => $r[0]['uid'],
+                                       'link'             => $a->get_baseurl() . '/notifications/intro',
+                                       'source_name'  => ((strlen(stripslashes($contact_record['name']))) ? stripslashes($contact_record['name']) : t('[Name Withheld]')),
+                                       'source_link'  => $contact_record['url'],
+                                       'source_photo' => $contact_record['photo'],
+                                       'verb'         => ($sharing ? ACTIVITY_FRIEND : ACTIVITY_FOLLOW),
+                                       'otype'        => 'intro'
                                ));
-                               $res = mail($r[0]['email'], 
-                                       email_header_encode((($sharing) ? t('A new person is sharing with you at ') : t("You have a new follower at ")) . $a->config['sitename'],'UTF-8'),
-                                       $email,
-                                       'From: ' . 'Administrator' . '@' . $_SERVER['SERVER_NAME'] . "\n"
-                                       . 'Content-type: text/plain; charset=UTF-8' . "\n"
-                                       . 'Content-transfer-encoding: 8bit' );
+
 
                        }
                }
@@ -3425,7 +3948,7 @@ function new_follower($importer,$contact,$datarray,$item,$sharing = false) {
 function lose_follower($importer,$contact,$datarray,$item) {
 
        if(($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_SHARING)) {
-               q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
+               q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d",
                        intval(CONTACT_IS_SHARING),
                        intval($contact['id'])
                );
@@ -3438,7 +3961,7 @@ function lose_follower($importer,$contact,$datarray,$item) {
 function lose_sharer($importer,$contact,$datarray,$item) {
 
        if(($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_FOLLOWER)) {
-               q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
+               q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d",
                        intval(CONTACT_IS_FOLLOWER),
                        intval($contact['id'])
                );
@@ -3459,7 +3982,7 @@ function subscribe_to_hub($url,$importer,$contact,$hubmode = 'subscribe') {
                );
        }
 
-       // Diaspora has different message-ids in feeds than they do 
+       // Diaspora has different message-ids in feeds than they do
        // through the direct Diaspora protocol. If we try and use
        // the feed, we'll get duplicates. So don't.
 
@@ -3477,7 +4000,7 @@ function subscribe_to_hub($url,$importer,$contact,$hubmode = 'subscribe') {
        logger('subscribe_to_hub: ' . $hubmode . ' ' . $contact['name'] . ' to hub ' . $url . ' endpoint: '  . $push_url . ' with verifier ' . $verify_token);
 
        if(! strlen($contact['hub-verify'])) {
-               $r = q("UPDATE `contact` SET `hub-verify` = '%s' WHERE `id` = %d LIMIT 1",
+               $r = q("UPDATE `contact` SET `hub-verify` = '%s' WHERE `id` = %d",
                        dbesc($verify_token),
                        intval($contact['id'])
                );
@@ -3603,18 +4126,22 @@ function atom_entry($item,$type,$author,$owner,$comment = false,$cid = 0) {
        $mentioned = get_mentions($item);
        if($mentioned)
                $o .= $mentioned;
-       
+
        call_hooks('atom_entry', $o);
 
        $o .= '</entry>' . "\r\n";
-       
+
        return $o;
 }
 
 function fix_private_photos($s, $uid, $item = null, $cid = 0) {
+
+       if(get_config('system','disable_embedded'))
+               return $s;
+
        $a = get_app();
 
-       logger('fix_private_photos', LOGGER_DEBUG);
+       logger('fix_private_photos: check for photos', LOGGER_DEBUG);
        $site = substr($a->get_baseurl(),strpos($a->get_baseurl(),'://'));
 
        $orig_body = $s;
@@ -3635,7 +4162,7 @@ function fix_private_photos($s, $uid, $item = null, $cid = 0) {
                        // Only embed locally hosted photos
                        $replace = false;
                        $i = basename($image);
-                       $i = str_replace(array('.jpg','.png'),array('',''),$i);
+                       $i = str_replace(array('.jpg','.png','.gif'),array('','',''),$i);
                        $x = strpos($i,'-');
 
                        if($x) {
@@ -3646,12 +4173,12 @@ function fix_private_photos($s, $uid, $item = null, $cid = 0) {
                                        intval($res),
                                        intval($uid)
                                );
-                               if(count($r)) {
+                               if($r) {
 
                                        // Check to see if we should replace this photo link with an embedded image
                                        // 1. No need to do so if the photo is public
                                        // 2. If there's a contact-id provided, see if they're in the access list
-                                       //    for the photo. If so, embed it. 
+                                       //    for the photo. If so, embed it.
                                        // 3. Otherwise, if we have an item, see if the item permissions match the photo
                                        //    permissions, regardless of order but first check to see if they're an exact
                                        //    match to save some processing overhead.
@@ -3660,7 +4187,7 @@ function fix_private_photos($s, $uid, $item = null, $cid = 0) {
                                                if($cid) {
                                                        $recips = enumerate_permissions($r[0]);
                                                        if(in_array($cid, $recips)) {
-                                                               $replace = true;        
+                                                               $replace = true;
                                                        }
                                                }
                                                elseif($item) {
@@ -3693,7 +4220,7 @@ function fix_private_photos($s, $uid, $item = null, $cid = 0) {
                                        }
                                }
                        }
-               }       
+               }
 
                $new_body = $new_body . substr($orig_body, 0, $img_start + $img_st_close) . $image . '[/img]';
                $orig_body = substr($orig_body, $img_start + $img_st_close + $img_len + strlen('[/img]'));
@@ -3718,7 +4245,7 @@ function has_permissions($obj) {
 }
 
 function compare_permissions($obj1,$obj2) {
-       // first part is easy. Check that these are exactly the same. 
+       // first part is easy. Check that these are exactly the same.
        if(($obj1['allow_cid'] == $obj2['allow_cid'])
                && ($obj1['allow_gid'] == $obj2['allow_gid'])
                && ($obj1['deny_cid'] == $obj2['deny_cid'])
@@ -3760,14 +4287,14 @@ function item_getfeedtags($item) {
                                $ret[] = array('#',$matches[1][$x], $matches[2][$x]);
                }
        }
-       $matches = false; 
+       $matches = false;
        $cnt = preg_match_all('|\@\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
        if($cnt) {
                for($x = 0; $x < $cnt; $x ++) {
                        if($matches[1][$x])
                                $ret[] = array('@',$matches[1][$x], $matches[2][$x]);
                }
-       } 
+       }
        return $ret;
 }
 
@@ -3792,8 +4319,8 @@ function item_getfeedattach($item) {
 }
 
 
-       
-function item_expire($uid,$days) {
+
+function item_expire($uid, $days, $network = "", $force = false) {
 
        if((! $uid) || ($days < 1))
                return;
@@ -3804,10 +4331,18 @@ function item_expire($uid,$days) {
        $expire_network_only = get_pconfig($uid,'expire','network_only');
        $sql_extra = ((intval($expire_network_only)) ? " AND wall = 0 " : "");
 
-       $r = q("SELECT * FROM `item` 
-               WHERE `uid` = %d 
-               AND `created` < UTC_TIMESTAMP() - INTERVAL %d DAY 
-               AND `id` = `parent` 
+       if ($network != "") {
+               $sql_extra .= sprintf(" AND network = '%s' ", dbesc($network));
+               // There is an index "uid_network_received" but not "uid_network_created"
+               // This avoids the creation of another index just for one purpose.
+               // And it doesn't really matter wether to look at "received" or "created"
+               $range = "AND `received` < UTC_TIMESTAMP() - INTERVAL %d DAY ";
+       } else
+               $range = "AND `created` < UTC_TIMESTAMP() - INTERVAL %d DAY ";
+
+       $r = q("SELECT * FROM `item`
+               WHERE `uid` = %d $range
+               AND `id` = `parent`
                $sql_extra
                AND `deleted` = 0",
                intval($uid),
@@ -3820,6 +4355,10 @@ function item_expire($uid,$days) {
        $expire_items = get_pconfig($uid, 'expire','items');
        $expire_items = (($expire_items===false)?1:intval($expire_items)); // default if not set: 1
 
+       // Forcing expiring of items - but not notes and marked items
+       if ($force)
+               $expire_items = true;
+
        $expire_notes = get_pconfig($uid, 'expire','notes');
        $expire_notes = (($expire_notes===false)?1:intval($expire_notes)); // default if not set: 1
 
@@ -3853,7 +4392,7 @@ function item_expire($uid,$days) {
        }
 
        proc_run('php',"include/notifier.php","expire","$uid");
-       
+
 }
 
 
@@ -3915,15 +4454,45 @@ function drop_item($id,$interactive = true) {
 
        if((local_user() == $item['uid']) || ($cid) || (! $interactive)) {
 
+               // Check if we should do HTML-based delete confirmation
+               if($_REQUEST['confirm']) {
+                       // <form> can't take arguments in its "action" parameter
+                       // so add any arguments as hidden inputs
+                       $query = explode_querystring($a->query_string);
+                       $inputs = array();
+                       foreach($query['args'] as $arg) {
+                               if(strpos($arg, 'confirm=') === false) {
+                                       $arg_parts = explode('=', $arg);
+                                       $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
+                               }
+                       }
+
+                       return replace_macros(get_markup_template('confirm.tpl'), array(
+                               '$method' => 'get',
+                               '$message' => t('Do you really want to delete this item?'),
+                               '$extra_inputs' => $inputs,
+                               '$confirm' => t('Yes'),
+                               '$confirm_url' => $query['base'],
+                               '$confirm_name' => 'confirmed',
+                               '$cancel' => t('Cancel'),
+                       ));
+               }
+               // Now check how the user responded to the confirmation query
+               if($_REQUEST['canceled']) {
+                       goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
+               }
+
                logger('delete item: ' . $item['id'], LOGGER_DEBUG);
                // delete the item
 
-               $r = q("UPDATE `item` SET `deleted` = 1, `title` = '', `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
+               $r = q("UPDATE `item` SET `deleted` = 1, `title` = '', `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d",
                        dbesc(datetime_convert()),
                        dbesc(datetime_convert()),
                        intval($item['id'])
                );
                create_tags_from_item($item['id']);
+               create_files_from_item($item['id']);
+               delete_thread($item['id']);
 
                // clean up categories and tags so they don't end up as orphans
 
@@ -3944,10 +4513,10 @@ function drop_item($id,$interactive = true) {
                        }
                }
 
-               // If item is a link to a photo resource, nuke all the associated photos 
+               // If item is a link to a photo resource, nuke all the associated photos
                // (visitors will not have photo resources)
                // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
-               // generate a resource-id and therefore aren't intimately linked to the item. 
+               // generate a resource-id and therefore aren't intimately linked to the item.
 
                if(strlen($item['resource-id'])) {
                        q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
@@ -3960,7 +4529,7 @@ function drop_item($id,$interactive = true) {
                // If item is a link to an event, nuke the event record.
 
                if(intval($item['event-id'])) {
-                       q("DELETE FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
+                       q("DELETE FROM `event` WHERE `id` = %d AND `uid` = %d",
                                intval($item['event-id']),
                                intval($item['uid'])
                        );
@@ -3969,6 +4538,9 @@ function drop_item($id,$interactive = true) {
 
                // clean up item_id and sign meta-data tables
 
+               /*
+               // Old code - caused very long queries and warning entries in the mysql logfiles:
+
                $r = q("DELETE FROM item_id where iid in (select id from item where parent = %d and uid = %d)",
                        intval($item['id']),
                        intval($item['uid'])
@@ -3978,6 +4550,31 @@ function drop_item($id,$interactive = true) {
                        intval($item['id']),
                        intval($item['uid'])
                );
+               */
+
+               // The new code splits the queries since the mysql optimizer really has bad problems with subqueries
+
+               // Creating list of parents
+               $r = q("select id from item where parent = %d and uid = %d",
+                       intval($item['id']),
+                       intval($item['uid'])
+               );
+
+               $parentid = "";
+
+               foreach ($r AS $row) {
+                       if ($parentid != "")
+                               $parentid .= ", ";
+
+                       $parentid .= $row["id"];
+               }
+
+               // Now delete them
+               if ($parentid != "") {
+                       $r = q("DELETE FROM item_id where iid in (%s)", dbesc($parentid));
+
+                       $r = q("DELETE FROM sign where iid in (%s)", dbesc($parentid));
+               }
 
                // If it's the parent of a comment thread, kill all the kids
 
@@ -3990,6 +4587,8 @@ function drop_item($id,$interactive = true) {
                                intval($item['uid'])
                        );
                        create_tags_from_item($item['parent-uri'], $item['uid']);
+                       create_files_from_item($item['parent-uri'], $item['uid']);
+                       delete_thread_uri($item['parent-uri'], $item['uid']);
                        // ignore the result
                }
                else {
@@ -4005,7 +4604,7 @@ function drop_item($id,$interactive = true) {
                                intval($item['uid'])
                        );
                        if(count($r)) {
-                               q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
+                               q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d",
                                        intval($r[0]['id'])
                                );
                        }
@@ -4036,8 +4635,8 @@ function drop_item($id,$interactive = true) {
 
 
 function first_post_date($uid,$wall = false) {
-       $r = q("select id, created from item 
-               where uid = %d and wall = %d and deleted = 0 and visible = 1 AND moderated = 0 
+       $r = q("select id, created from item
+               where uid = %d and wall = %d and deleted = 0 and visible = 1 AND moderated = 0
                and id = parent
                order by created asc limit 1",
                intval($uid),
@@ -4057,13 +4656,9 @@ function posted_dates($uid,$wall) {
        if(! $dthen)
                return array();
 
-       // If it's near the end of a long month, backup to the 28th so that in 
-       // consecutive loops we'll always get a whole month difference.
-
-       if(intval(substr($dnow,8)) > 28)
-               $dnow = substr($dnow,0,8) . '28';
-       if(intval(substr($dthen,8)) > 28)
-               $dnow = substr($dthen,0,8) . '28';
+       // Set the start and end date to the beginning of the month
+       $dnow = substr($dnow,0,8).'01';
+       $dthen = substr($dthen,0,8).'01';
 
        $ret = array();
        // Starting with the current month, get the first and last days of every
@@ -4074,7 +4669,7 @@ function posted_dates($uid,$wall) {
                $start_month = datetime_convert('','',$dstart,'Y-m-d');
                $end_month = datetime_convert('','',$dend,'Y-m-d');
                $str = day_translate(datetime_convert('','',$dnow,'F Y'));
-               $ret[] = array($str,$end_month,$start_month);
+               $ret[] = array($str,$end_month,$start_month);
                $dnow = datetime_convert('','',$dnow . ' -1 month', 'Y-m-d');
        }
        return $ret;