]> git.mxchange.org Git - friendica.git/blobdiff - include/items.php
don't make -desc so obnoxious looking
[friendica.git] / include / items.php
old mode 100755 (executable)
new mode 100644 (file)
index 5a297c8..b1dc170
@@ -7,14 +7,11 @@ require_once('include/crypto.php');
 
 function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
 
-       // default permissions - anonymous user
-
-       if(! strlen($owner_nick))
-               killme();
 
+       $sitefeed    = ((strlen($owner_nick)) ? false : true); // not yet implemented, need to rewrite huge chunks of following logic
        $public_feed = (($dfrn_id) ? false : true);
-       $starred = false;
-       $converse = false;
+       $starred     = false;   // not yet implemented, possible security issues
+       $converse    = false;
 
        if($public_feed && $a->argc > 2) {
                for($x = 2; $x < $a->argc; $x++) {
@@ -22,9 +19,16 @@ function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0)
                                $converse = true;
                        if($a->argv[$x] == 'starred')
                                $starred = true;
+                       if($a->argv[$x] === 'category' && $a->argc > ($x + 1) && strlen($a->argv[$x+1]))
+                               $category = $a->argv[$x+1];
                }
+
+
        }
 
+       
+
+       // default permissions - anonymous user
 
        $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid`  = '' AND `deny_gid`  = '' ";
 
@@ -103,6 +107,10 @@ function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0)
        if(! strlen($last_update))
                $last_update = 'now -30 days';
 
+       if(isset($category)) {
+               $sql_extra .= file_tag_file_query('item',$category,'category');
+       }
+
        if($public_feed) {
                if(! $converse)
                        $sql_extra .= " AND `contact`.`self` = 1 ";
@@ -418,7 +426,7 @@ function get_atom_elements($feed,$item) {
        // 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)) {
+       if((strpos($res['body'],'<') !== false) && (strpos($res['body'],'>') !== false)) {
 
                $res['body'] = reltoabs($res['body'],$base_url);
 
@@ -431,14 +439,22 @@ function get_atom_elements($feed,$item) {
 
                // we shouldn't need a whitelist, because the bbcode converter
                // will strip out any unsupported tags.
-               // $config->set('HTML.Allowed', 'p,b,a[href],i'); 
 
                $purifier = new HTMLPurifier($config);
                $res['body'] = $purifier->purify($res['body']);
 
-               $res['body'] = html2bbcode($res['body']);
+               $res['body'] = @html2bbcode($res['body']);
+       }
+       elseif(! $have_real_body) {
+
+               // it's not one of our messages and it has no tags
+               // so it's probably just text. We'll escape it just to be safe.
+
+               $res['body'] = escape_tags($res['body']);
        }
 
+       // this tag is obsolete but we keep it for really old sites
+
        $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
        if($allow && $allow[0]['data'] == 1)
                $res['last-child'] = 1;
@@ -572,20 +588,21 @@ function get_atom_elements($feed,$item) {
 
        if($rawobj) {
                $res['object'] = '<object>' . "\n";
-               if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
-                       $res['object-type'] = $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'];
-                       $res['object'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
+               $child = $rawobj[0]['child'];
+               if($child[NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
+                       $res['object-type'] = $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'];
+                       $res['object'] .= '<type>' . $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
                }       
-               if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
-                       $res['object'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
-               if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
-                       $res['object'] .= '<link>' . encode_rel_links($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
-               if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
-                       $res['object'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
-               if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
-                       $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
+               if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'id') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
+                       $res['object'] .= '<id>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
+               if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'link') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
+                       $res['object'] .= '<link>' . encode_rel_links($child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
+               if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'title') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
+                       $res['object'] .= '<title>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
+               if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'content') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
+                       $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
                        if(! $body)
-                               $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
+                               $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
                        // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
                        $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
                        if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
@@ -610,20 +627,20 @@ function get_atom_elements($feed,$item) {
 
        if($rawobj) {
                $res['target'] = '<target>' . "\n";
-               if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
-                       $res['target'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
+               $child = $rawobj[0]['child'];
+               if($child[NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
+                       $res['target'] .= '<type>' . $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
                }       
-               if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
-                       $res['target'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
-
-               if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
-                       $res['target'] .= '<link>' . encode_rel_links($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
-               if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
-                       $res['target'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
-               if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
-                       $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
+               if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'id') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
+                       $res['target'] .= '<id>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
+               if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'link') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
+                       $res['target'] .= '<link>' . encode_rel_links($child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
+               if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'data') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
+                       $res['target'] .= '<title>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
+               if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'data') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
+                       $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
                        if(! $body)
-                               $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
+                               $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
                        // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
                        $res['target'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
                        if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
@@ -935,7 +952,7 @@ function tag_deliver($uid,$item_id) {
 
        $mention = false;
 
-       $u = q("select uid, nickname, language, username, email, `page-flags`, `notify-flags` from user where uid = %d limit 1",
+       $u = q("select * from user where uid = %d limit 1",
                intval($uid)
        );
        if(! count($u))
@@ -1010,10 +1027,20 @@ function tag_deliver($uid,$item_id) {
        if(! count($c))
                return;
 
-       q("update item set wall = 1, origin = 1, forum_mode = 1, `owner-name` = '%s', `owner-link` = '%s', `owner-avatar` = '%s'  where id = %d limit 1",
+       // also reset all the privacy bits to the forum default permissions
+
+       $private = ($u[0]['allow_cid'] || $u[0]['allow_gid'] || $u[0]['deny_cid'] || $u[0]['deny_gid']) ? 1 : 0;
+
+       q("update item set wall = 1, origin = 1, forum_mode = 1, `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",
                dbesc($c[0]['name']),
                dbesc($c[0]['url']),
                dbesc($c[0]['thumb']),
+               intval($private),
+               dbesc($u[0]['allow_cid']),
+               dbesc($u[0]['allow_gid']),
+               dbesc($u[0]['deny_cid']),
+               dbesc($u[0]['deny_gid']),
                intval($item_id)
        );
 
@@ -1092,12 +1119,23 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
        $postvars     = array();
        $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
        $challenge    = hex2bin((string) $res->challenge);
+       $perm         = (($res->perm) ? $res->perm : null);
        $dfrn_version = (float) (($res->dfrn_version) ? $res->dfrn_version : 2.0);
        $rino_allowed = ((intval($res->rino) === 1) ? 1 : 0);
        $page         = (($owner['page-flags'] == PAGE_COMMUNITY) ? 1 : 0);
 
        $final_dfrn_id = '';
 
+       if($perm) {
+               if((($perm == 'rw') && (! intval($contact['writable']))) 
+               || (($perm == 'r') && (intval($contact['writable'])))) {
+                       q("update contact set writable = %d where id = %d limit 1",
+                               intval(($perm == 'rw') ? 1 : 0),
+                               intval($contact['id'])
+                       );
+                       $contact['writable'] = (string) 1 - intval($contact['writable']);                       
+               }
+       }
 
        if(($contact['duplex'] && strlen($contact['pubkey'])) 
                || ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
@@ -1437,7 +1475,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                        }
                        if($deleted && is_array($contact)) {
                                $r = q("SELECT `item`.*, `contact`.`self` FROM `item` left join `contact` on `item`.`contact-id` = `contact`.`id` 
-                                       WHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d LIMIT 1",
+                                       WHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d AND NOT `item`.`file` LIKE '%%[%%' LIMIT 1",
                                        dbesc($uri),
                                        intval($importer['uid']),
                                        intval($contact['id'])
@@ -1448,7 +1486,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                        if(! $item['deleted'])
                                                logger('consume_feed: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
 
-                                       if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTVITY_OBJ_TAGTERM)) {
+                                       if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
                                                $xo = parse_xml_string($item['object'],false);
                                                $xt = parse_xml_string($item['target'],false);
                                                if($xt->type === ACTIVITY_OBJ_NOTE) {
@@ -1582,9 +1620,10 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
 
                                if(count($r)) {
                                        if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
-                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
                                                        dbesc($datarray['title']),
                                                        dbesc($datarray['body']),
+                                                       dbesc($datarray['tag']),
                                                        dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
                                                        dbesc($item_id),
                                                        intval($importer['uid'])
@@ -1727,9 +1766,10 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
 
                                if(count($r)) {
                                        if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
-                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
                                                        dbesc($datarray['title']),
                                                        dbesc($datarray['body']),
+                                                       dbesc($datarray['tag']),
                                                        dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
                                                        dbesc($item_id),
                                                        intval($importer['uid'])
@@ -1800,7 +1840,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                        // 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. 
-                                       logger('local_delivery: Correcting item owner.', LOGGER_DEBUG);
+                                       logger('consume_feed: Correcting item owner.', LOGGER_DEBUG);
                                        $datarray['owner-name']   = $contact['name'];
                                        $datarray['owner-link']   = $contact['url'];
                                        $datarray['owner-avatar'] = $contact['thumb'];
@@ -1837,6 +1877,8 @@ function local_delivery($importer,$data) {
        $feed->enable_order_by_date(false);
        $feed->init();
 
+/*
+       // Currently unsupported - needs a lot of work
        $reloc = $feed->get_feed_tags( NAMESPACE_DFRN, 'relocate' );
        if(isset($reloc[0]['child'][NAMESPACE_DFRN])) {
                $base = $reloc[0]['child'][NAMESPACE_DFRN];
@@ -1861,6 +1903,7 @@ function local_delivery($importer,$data) {
                // schedule a scan?
 
        }
+*/
 
        // handle friend suggestion notification
 
@@ -1896,6 +1939,14 @@ function local_delivery($importer,$data) {
                );
                if(count($r)) {
                        $fid = $r[0]['id'];
+
+                       // OK, we do. Do we already have an introduction for this person ?
+                       $r = q("select id from intro where uid = %d and fid = %d limit 1",
+                               intval($fsugg['uid']),
+                               intval($fid)
+                       );
+                       if(count($r))
+                               return 0;
                }
                if(! $fid)
                        $r = q("INSERT INTO `fcontact` ( `name`,`url`,`photo`,`request` ) VALUES ( '%s', '%s', '%s', '%s' ) ",
@@ -1916,6 +1967,7 @@ function local_delivery($importer,$data) {
                else
                        return 0;
 
+
                $hash = random_string();
  
                $r = q("INSERT INTO `intro` ( `uid`, `fid`, `contact-id`, `note`, `hash`, `datetime`, `blocked` )
@@ -2036,7 +2088,7 @@ function local_delivery($importer,$data) {
                        if($deleted) {
 
                                $r = q("SELECT `item`.*, `contact`.`self` FROM `item` left join contact on `item`.`contact-id` = `contact`.`id`
-                                       WHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d LIMIT 1",
+                                       WHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d AND NOT `item`.`file` LIKE '%%[%%' LIMIT 1",
                                        dbesc($uri),
                                        intval($importer['importer_uid']),
                                        intval($importer['id'])
@@ -2050,7 +2102,7 @@ function local_delivery($importer,$data) {
 
                                        logger('local_delivery: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
 
-                                       if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTVITY_OBJ_TAGTERM)) {
+                                       if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
                                                $xo = parse_xml_string($item['object'],false);
                                                $xt = parse_xml_string($item['target'],false);
 
@@ -2185,8 +2237,36 @@ function local_delivery($importer,$data) {
                                logger('local_delivery: received remote comment');
                                $is_like = false;
                                // remote reply to our post. Import and then notify everybody else.
+
                                $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),
+                                       intval($importer['importer_uid'])
+                               );
+
+                               // Update content if 'updated' changes
+
+                               if(count($r)) {
+                                       $iid = $r[0]['id'];
+                                       if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
+                                               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",
+                                                       dbesc($datarray['title']),
+                                                       dbesc($datarray['body']),
+                                                       dbesc($datarray['tag']),
+                                                       dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
+                                                       dbesc($item_id),
+                                                       intval($importer['importer_uid'])
+                                               );
+
+                                               proc_run('php',"include/notifier.php","comment-import",$iid);
+
+                                       }
+
+                                       continue;
+                               }
+
 
                                // TODO: make this next part work against both delivery threads of a community post
 
@@ -2196,14 +2276,22 @@ function local_delivery($importer,$data) {
 //                                     return 0;
 //                             }                                       
 
+                               // our user with $importer['importer_uid'] is the owner
+
+                               $own = q("select name,url,thumb from contact where uid = %d and self = 1 limit 1",
+                                       intval($importer['importer_uid'])
+                               );
+
+
                                $datarray['type'] = 'remote-comment';
                                $datarray['wall'] = 1;
                                $datarray['parent-uri'] = $parent_uri;
                                $datarray['uid'] = $importer['importer_uid'];
-                               $datarray['owner-name'] = $r[0]['name'];
-                               $datarray['owner-link'] = $r[0]['url'];
-                               $datarray['owner-avatar'] = $r[0]['thumb'];
+                               $datarray['owner-name'] = $own[0]['name'];
+                               $datarray['owner-link'] = $own[0]['url'];
+                               $datarray['owner-avatar'] = $own[0]['thumb'];
                                $datarray['contact-id'] = $importer['id'];
+
                                if(($datarray['verb'] === ACTIVITY_LIKE) || ($datarray['verb'] === ACTIVITY_DISLIKE)) {
                                        $is_like = true;
                                        $datarray['type'] = 'activity';
@@ -2220,26 +2308,34 @@ function local_delivery($importer,$data) {
                                }
 
                                if(($datarray['verb'] === ACTIVITY_TAG) && ($datarray['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
-
-
+                                       
                                        $xo = parse_xml_string($datarray['object'],false);
                                        $xt = parse_xml_string($datarray['target'],false);
 
-                                       if(($xt->type == ACTIVITY_OBJ_NOTE) && ($xt->id == $r[0]['uri'])) {
+                                       if(($xt->type == ACTIVITY_OBJ_NOTE) && ($xt->id)) {
+
+                                               // fetch the parent item
+
+                                               $tagp = q("select * from item where uri = '%s' and uid = %d limit 1",
+                                                       dbesc($xt->id),
+                                                       intval($importer['importer_uid'])
+                                               );
+                                               if(! count($tagp))
+                                                       continue;       
 
                                                // extract tag, if not duplicate, and this user allows tags, add to parent item                                         
 
                                                if($xo->id && $xo->content) {
                                                        $newtag = '#[url=' . $xo->id . ']'. $xo->content . '[/url]';
-
-                                                       if(! (stristr($r[0]['tag'],$newtag))) {
+                                                       if(! (stristr($tagp[0]['tag'],$newtag))) {
                                                                $i = q("SELECT `blocktags` FROM `user` where `uid` = %d LIMIT 1",
                                                                        intval($importer['importer_uid'])
                                                                );
-                                                               if(count($i) && ! ($i[0]['blocktags'])) {
-                                                                       q("UPDATE item SET tag = '%s' WHERE id = %d LIMIT 1",
-                                                                               dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . $newtag),
-                                                                               intval($r[0]['id'])
+                                                               if(count($i) && ! intval($i[0]['blocktags'])) {
+                                                                       q("UPDATE item SET tag = '%s', `edited` = '%s' WHERE id = %d LIMIT 1",
+                                                                               dbesc($tagp[0]['tag'] . (strlen($tagp[0]['tag']) ? ',' : '') . $newtag),
+                                                                               intval($tagp[0]['id']),
+                                                                               dbesc(datetime_convert())
                                                                        );
                                                                }
                                                        }
@@ -2332,9 +2428,10 @@ function local_delivery($importer,$data) {
 
                                if(count($r)) {
                                        if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
-                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
                                                        dbesc($datarray['title']),
                                                        dbesc($datarray['body']),
+                                                       dbesc($datarray['tag']),
                                                        dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
                                                        dbesc($item_id),
                                                        intval($importer['importer_uid'])
@@ -2406,9 +2503,9 @@ function local_delivery($importer,$data) {
 
                                // find out if our user is involved in this conversation and wants to be notified.
                        
-                               if($datarray['type'] != 'activity') {
+                               if(!x($datarray['type']) || $datarray['type'] != 'activity') {
 
-                                       $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 ",
+                                       $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
                                                dbesc($parent_uri),
                                                intval($importer['importer_uid'])
                                        );
@@ -2497,9 +2594,10 @@ function local_delivery($importer,$data) {
 
                        if(count($r)) {
                                if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {  
-                                       $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+                                       $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
                                                dbesc($datarray['title']),
                                                dbesc($datarray['body']),
+                                               dbesc($datarray['tag']),
                                                dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
                                                dbesc($item_id),
                                                intval($importer['importer_uid'])
@@ -2925,6 +3023,11 @@ function item_expire($uid,$days) {
 
        foreach($r as $item) {
 
+               // don't expire filed items
+
+               if(strpos($item['file'],'[') !== false)
+                       continue;
+
                // Only expire posts, not photos and photo comments
 
                if($expire_photos==0 && strlen($item['resource-id']))
@@ -2936,21 +3039,7 @@ function item_expire($uid,$days) {
                if($expire_items==0 && $item['type']!='note')
                        continue;
 
-               $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
-                       dbesc(datetime_convert()),
-                       dbesc(datetime_convert()),
-                       intval($item['id'])
-               );
-
-               // kill the kids
-
-               $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
-                       dbesc(datetime_convert()),
-                       dbesc(datetime_convert()),
-                       dbesc($item['parent-uri']),
-                       intval($item['uid'])
-               );
-
+               drop_item($item['id'],false);
        }
 
        proc_run('php',"include/notifier.php","expire","$uid");
@@ -2961,7 +3050,7 @@ function item_expire($uid,$days) {
 function drop_items($items) {
        $uid = 0;
 
-       if((! local_user()) && (! $remote_user()))
+       if(! local_user() && ! remote_user())
                return;
 
        if(count($items)) {
@@ -3012,6 +3101,25 @@ function drop_item($id,$interactive = true) {
                        intval($item['id'])
                );
 
+               // clean up categories and tags so they don't end up as orphans
+
+               $matches = false;
+               $cnt = preg_match_all('/<(.*?)>/',$item['file'],$matches,PREG_SET_ORDER);
+               if($cnt) {
+                       foreach($matches as $mtch) {
+                               file_tag_unsave_file($item['uid'],$item['id'],$mtch[1],true);
+                       }
+               }
+
+               $matches = false;
+
+               $cnt = preg_match_all('/\[(.*?)\]/',$item['file'],$matches,PREG_SET_ORDER);
+               if($cnt) {
+                       foreach($matches as $mtch) {
+                               file_tag_unsave_file($item['uid'],$item['id'],$mtch[1],false);
+                       }
+               }
+
                // 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
@@ -3035,6 +3143,17 @@ function drop_item($id,$interactive = true) {
                        // ignore the result
                }
 
+               // clean up item_id and sign meta-data tables
+
+               $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'])
+               );
+
+               $r = q("DELETE FROM sign where iid in (select id from item where parent = %d and uid = %d)",
+                       intval($item['id']),
+                       intval($item['uid'])
+               );
 
                // If it's the parent of a comment thread, kill all the kids
 
@@ -3067,7 +3186,7 @@ function drop_item($id,$interactive = true) {
                        }       
                }
                $drop_id = intval($item['id']);
-                       
+
                // send the notification upstream/downstream as the case may be
 
                if(! $interactive)