]> git.mxchange.org Git - friendica.git/blobdiff - include/items.php
how the heck did favicon get into the repo? Gone now...
[friendica.git] / include / items.php
index 6900c5920b0240458dc49785e9035d34521804d8..63e180f7611f8482f67ca139eb44f7d14eb6fc28 100644 (file)
@@ -113,7 +113,7 @@ function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0)
 
        $items = $r;
 
-       $feed_template = load_view_file('view/atom_feed.tpl');
+       $feed_template = get_markup_template('atom_feed.tpl');
 
        $atom = '';
 
@@ -180,7 +180,11 @@ function construct_activity_object($item) {
 
        if($item['object']) {
                $o = '<as:object>' . "\r\n";
-               $r = @simplexml_load_string($item['object']);
+               $r = parse_xml_string($item['object'],false);
+
+
+               if(! $r)
+                       return '';
                if($r->type)
                        $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
                if($r->id)
@@ -188,8 +192,15 @@ function construct_activity_object($item) {
                if($r->title)
                        $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
                if($r->link) {
-                       if(substr($r->link,0,1) === '<') 
+                       if(substr($r->link,0,1) === '<') {
+                               // patch up some facebook "like" activity objects that got stored incorrectly
+                               // for a couple of months prior to 9-Jun-2011 and generated bad XML.
+                               // we can probably remove this hack here and in the following function in a few months time.
+                               if(strstr($r->link,'&') && (! strstr($r->link,'&amp;')))
+                                       $r->link = str_replace('&','&amp;', $r->link);
+                               $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
                                $o .= $r->link;
+                       }                                       
                        else
                                $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
                }
@@ -206,7 +217,9 @@ function construct_activity_target($item) {
 
        if($item['target']) {
                $o = '<as:target>' . "\r\n";
-               $r = @simplexml_load_string($item['target']);
+               $r = parse_xml_string($item['target'],false);
+               if(! $r)
+                       return '';
                if($r->type)
                        $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
                if($r->id)
@@ -214,8 +227,12 @@ function construct_activity_target($item) {
                if($r->title)
                        $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
                if($r->link) {
-                       if(substr($r->link,0,1) === '<') 
+                       if(substr($r->link,0,1) === '<') {
+                               if(strstr($r->link,'&') && (! strstr($r->link,'&amp;')))
+                                       $r->link = str_replace('&','&amp;', $r->link);
+                               $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
                                $o .= $r->link;
+                       }                                       
                        else
                                $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
                }
@@ -241,8 +258,14 @@ function get_atom_elements($feed,$item) {
        $res = array();
 
        $author = $item->get_author();
-       $res['author-name'] = unxmlify($author->get_name());
-       $res['author-link'] = unxmlify($author->get_link());
+       if($author) { 
+               $res['author-name'] = unxmlify($author->get_name());
+               $res['author-link'] = unxmlify($author->get_link());
+       }
+       else {
+               $res['author-name'] = unxmlify($feed->get_title());
+               $res['author-link'] = unxmlify($feed->get_permalink());
+       }
        $res['uri'] = unxmlify($item->get_id());
        $res['title'] = unxmlify($item->get_title());
        $res['body'] = unxmlify($item->get_content());
@@ -313,6 +336,12 @@ function get_atom_elements($feed,$item) {
                }
        }
 
+       $apps = $item->get_item_tags(NAMESPACE_STATUSNET,'notice_info');
+       if($apps && $apps[0]['attribs']['']['source']) {
+               $res['app'] = strip_tags(unxmlify($apps[0]['attribs']['']['source']));
+               if($res['app'] === 'web')
+                       $res['app'] = 'OStatus';
+       }                  
 
        /**
         * If there's a copy of the body content which is guaranteed to have survived mangling in transit, use it.
@@ -343,12 +372,14 @@ 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)) {
 
                $res['body'] = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
                        '[youtube]$1[/youtube]', $res['body']);
 
+               $res['body'] = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
+                       '[youtube]$1[/youtube]', $res['body']);
+
                $res['body'] = oembed_html2bbcode($res['body']);
 
                $config = HTMLPurifier_Config::createDefault();
@@ -376,6 +407,9 @@ function get_atom_elements($feed,$item) {
        else
                $res['private'] = 0;
 
+       $extid = $item->get_item_tags(NAMESPACE_DFRN,'extid');
+       if($extid && $extid[0]['data'])
+               $res['extid'] = $extid[0]['data'];
 
        $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
        if($rawlocation)
@@ -451,7 +485,45 @@ function get_atom_elements($feed,$item) {
        if((x($res,'verb')) && ($res['verb'] === 'http://ostatus.org/schema/1.0/unfollow'))
                $res['verb'] = ACTIVITY_UNFOLLOW;
 
-               
+
+       $cats = $item->get_categories();
+       if($cats) {
+               $tag_arr = array();
+               foreach($cats as $cat) {
+                       $term = $cat->get_term();
+                       if(! $term)
+                               $term = $cat->get_label();
+                       $scheme = $cat->get_scheme();
+                       if($scheme && $term && stristr($scheme,'X-DFRN:'))
+                               $tag_arr[] = substr($scheme,7,1) . '[url=' . unxmlify(substr($scheme,9)) . ']' . unxmlify($term) . '[/url]';
+                       elseif($term)
+                               $tag_arr[] = notags(trim($term));
+               }
+               $res['tag'] =  implode(',', $tag_arr);
+       }
+
+       $attach = $item->get_enclosures();
+       if($attach) {
+               $att_arr = array();
+               foreach($attach as $att) {
+                       $len   = intval($att->get_length());
+                       $link  = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_link()))));
+                       $title = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_title()))));
+                       $type  = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_type()))));
+                       if(strpos($type,';'))
+                               $type = substr($type,0,strpos($type,';'));
+                       if((! $link) || (strpos($link,'http') !== 0))
+                               continue;
+
+                       if(! $title)
+                               $title = ' ';
+                       if(! $type)
+                               $type = 'application/octet-stream';
+
+                       $att_arr[] = '[attach]href="' . $link . '" size="' . $len . '" type="' . $type . '" title="' . $title . '"[/attach]'; 
+               }
+               $res['attach'] = implode(',', $att_arr);
+       }
 
        $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
 
@@ -478,6 +550,10 @@ function get_atom_elements($feed,$item) {
                                $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
                                        '[youtube]$1[/youtube]', $body);
 
+               $res['body'] = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
+                       '[youtube]$1[/youtube]', $res['body']);
+
+
                                $config = HTMLPurifier_Config::createDefault();
                                $config->set('Cache.DefinitionImpl', null);
 
@@ -517,6 +593,9 @@ function get_atom_elements($feed,$item) {
                                $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
                                        '[youtube]$1[/youtube]', $body);
 
+               $res['body'] = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
+                       '[youtube]$1[/youtube]', $res['body']);
+
                                $config = HTMLPurifier_Config::createDefault();
                                $config->set('Cache.DefinitionImpl', null);
 
@@ -581,6 +660,7 @@ function item_store($arr,$force_parent = false) {
 
        $arr['wall']          = ((x($arr,'wall'))          ? intval($arr['wall'])                : 0);
        $arr['uri']           = ((x($arr,'uri'))           ? notags(trim($arr['uri']))           : random_string());
+       $arr['extid']         = ((x($arr,'extid'))         ? notags(trim($arr['extid']))         : '');
        $arr['author-name']   = ((x($arr,'author-name'))   ? notags(trim($arr['author-name']))   : '');
        $arr['author-link']   = ((x($arr,'author-link'))   ? notags(trim($arr['author-link']))   : '');
        $arr['author-avatar'] = ((x($arr,'author-avatar')) ? notags(trim($arr['author-avatar'])) : '');
@@ -589,6 +669,7 @@ function item_store($arr,$force_parent = false) {
        $arr['owner-avatar']  = ((x($arr,'owner-avatar'))  ? notags(trim($arr['owner-avatar']))  : '');
        $arr['created']       = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
        $arr['edited']        = ((x($arr,'edited')  !== false) ? datetime_convert('UTC','UTC',$arr['edited'])  : datetime_convert());
+       $arr['received']      = datetime_convert();
        $arr['changed']       = datetime_convert();
        $arr['title']         = ((x($arr,'title'))         ? notags(trim($arr['title']))         : '');
        $arr['location']      = ((x($arr,'location'))      ? notags(trim($arr['location']))      : '');
@@ -609,6 +690,9 @@ function item_store($arr,$force_parent = false) {
        $arr['deny_gid']      = ((x($arr,'deny_gid'))      ? trim($arr['deny_gid'])              : '');
        $arr['private']       = ((x($arr,'private'))       ? intval($arr['private'])             : 0 );
        $arr['body']          = ((x($arr,'body'))          ? trim($arr['body'])                  : '');
+       $arr['tag']           = ((x($arr,'tag'))           ? notags(trim($arr['tag']))           : '');
+       $arr['attach']        = ((x($arr,'attach'))        ? notags(trim($arr['attach']))        : '');
+       $arr['app']           = ((x($arr,'app'))           ? notags(trim($arr['app']))           : '');
 
        if($arr['parent-uri'] === $arr['uri']) {
                $parent_id = 0;
@@ -656,6 +740,7 @@ function item_store($arr,$force_parent = false) {
                                $parent_id = 0;
                                $arr['thr-parent'] = $arr['parent-uri'];
                                $arr['parent-uri'] = $arr['uri'];
+                               $arr['gravity'] = 0;
                        }
                        else {
                                logger('item_store: item parent was not found - ignoring item');
@@ -682,6 +767,15 @@ function item_store($arr,$force_parent = false) {
                $arr['uri'],           // already dbesc'd
                intval($arr['uid'])
        );
+       if(! count($r)) {
+               // This is not good, but perhaps we encountered a rare race/cache condition, so back off and try again. 
+               sleep(3);
+               $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+                       $arr['uri'],           // already dbesc'd
+                       intval($arr['uid'])
+               );
+       }
+
        if(count($r)) {
                $current_post = $r[0]['id'];
                logger('item_store: created item ' . $current_post);
@@ -762,7 +856,7 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
        if(! $rino_enable)
                $rino = 0;
 
-       $url = $contact['notify'] . '?dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino=1' : '');
+       $url = $contact['notify'] . '&dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino=1' : '');
 
        logger('dfrn_deliver: ' . $url);
 
@@ -783,7 +877,7 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
                return 3;
        }
 
-       $res = simplexml_load_string($xml);
+       $res = parse_xml_string($xml);
 
        if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
                return (($res->status) ? $res->status : 3);
@@ -822,14 +916,14 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
        if($dissolve)
                $postvars['dissolve'] = '1';
 
-       if(($contact['rel']) && ($contact['rel'] != REL_FAN) && (! $contact['blocked']) && (! $contact['readonly'])) {
-               $postvars['data'] = $atom;
-       }
-       elseif($owner['page-flags'] == PAGE_COMMUNITY) {
+
+       if((($contact['rel']) && ($contact['rel'] != REL_FAN) && (! $contact['blocked'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
                $postvars['data'] = $atom;
+               $postvars['perm'] = 'rw';
        }
        else {
                $postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
+               $postvars['perm'] = 'r';
        }
 
        if($rino && $rino_allowed && (! $dissolve)) {
@@ -871,17 +965,15 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
        if((! $curl_stat) || (! strlen($xml)))
                return(-1); // timed out
 
-
        if(strpos($xml,'<?xml') === false) {
                logger('dfrn_deliver: phase 2: no valid XML returned');
                logger('dfrn_deliver: phase 2: returned XML: ' . $xml, LOGGER_DATA);
                return 3;
        }
 
-       $res = simplexml_load_string($xml);
+       $res = parse_xml_string($xml);
 
-       return $res->status;
+       return $res->status; 
 }
 
 
@@ -901,12 +993,10 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
  *
  */
 
-function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) {
+function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $secure_feed = false) {
 
-       require_once('simplepie/simplepie.inc');
+       require_once('library/simplepie/simplepie.inc');
 
-       if(! $contact)
-               logger('consume feed: anonymous');
        $feed = new SimplePie();
        $feed->set_raw_data($xml);
        if($datedir)
@@ -918,6 +1008,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) {
        if($feed->error())
                logger('consume_feed: Error parsing XML: ' . $feed->error());
 
+       $permalink = $feed->get_permalink();
 
        // Check at the feed level for updated contact name and/or photo
 
@@ -1162,6 +1253,13 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) {
                                $item_id  = $item->get_id();
                                $datarray = get_atom_elements($feed,$item);
 
+                               if(! x($datarray,'author-name'))
+                                       $datarray['author-name'] = $contact['name'];
+                               if(! x($datarray,'author-link'))
+                                       $datarray['author-link'] = $contact['url'];
+                               if(! x($datarray,'author-avatar'))
+                                       $datarray['author-avatar'] = $contact['thumb'];
+
                                $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
                                        dbesc($item_id),
                                        intval($importer['uid'])
@@ -1232,8 +1330,38 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) {
                                // Head post of a conversation. Have we seen it? If not, import it.
 
                                $item_id  = $item->get_id();
+
                                $datarray = get_atom_elements($feed,$item);
 
+                               if(is_array($contact)) {
+                                       if(! x($datarray,'author-name'))
+                                               $datarray['author-name'] = $contact['name'];
+                                       if(! x($datarray,'author-link'))
+                                               $datarray['author-link'] = $contact['url'];
+                                       if(! x($datarray,'author-avatar'))
+                                               $datarray['author-avatar'] = $contact['thumb'];
+                               }
+
+                               if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
+                                       $ev = bbtoevent($datarray['body']);
+                                       if(x($ev,'desc') && x($ev,'start')) {
+                                               $ev['uid'] = $importer['uid'];
+                                               $ev['uri'] = $item_id;
+                                               $ev['edited'] = $datarray['edited'];
+
+                                               if(is_array($contact))
+                                                       $ev['cid'] = $contact['id'];
+                                               $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+                                                       dbesc($item_id),
+                                                       intval($importer['uid'])
+                                               );
+                                               if(count($r))
+                                                       $ev['id'] = $r[0]['id'];
+                                               $xyz = event_store($ev);
+                                               continue;
+                                       }
+                               }
+
                                $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
                                        dbesc($item_id),
                                        intval($importer['uid'])
@@ -1277,7 +1405,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) {
                                if(! is_array($contact))
                                        return;
 
-                               if($contact['network'] === 'stat') {
+                               if($contact['network'] === 'stat' || stristr($permalink,'twitter.com')) {
                                        if(strlen($datarray['title']))
                                                unset($datarray['title']);
                                        $datarray['last-child'] = 1;
@@ -1324,8 +1452,8 @@ function new_follower($importer,$contact,$datarray,$item) {
                // create contact record - set to readonly
 
                $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `nick`, `photo`, `network`, `rel`, 
-                       `blocked`, `readonly`, `pending` )
-                       VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 1, 1 ) ",
+                       `blocked`, `readonly`, `pending`, `writable` )
+                       VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 1, 1, 1 ) ",
                        intval($importer['uid']),
                        dbesc(datetime_convert()),
                        dbesc($url),
@@ -1361,7 +1489,7 @@ function new_follower($importer,$contact,$datarray,$item) {
                $a = get_app();
                if(count($r)) {
                        if(($r[0]['notify-flags'] & NOTIFY_INTRO) && ($r[0]['page-flags'] == PAGE_NORMAL)) {
-                               $email_tpl = load_view_file('view/follow_notify_eml.tpl');
+                               $email_tpl = get_intltext_template('follow_notify_eml.tpl');
                                $email = replace_macros($email_tpl, array(
                                        '$requestor' => ((strlen($name)) ? $name : t('[Name Withheld]')),
                                        '$url' => $url,
@@ -1372,7 +1500,9 @@ function new_follower($importer,$contact,$datarray,$item) {
                                $res = mail($r[0]['email'], 
                                        t("You have a new follower at ") . $a->config['sitename'],
                                        $email,
-                                       'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] );
+                                       'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] . "\n"
+                                       . 'Content-type: text/plain; charset=UTF-8' . "\n"
+                                       . 'Content-transfer-encoding: 8bit' );
                        
                        }
                }
@@ -1451,17 +1581,24 @@ function atom_author($tag,$name,$uri,$h,$w,$photo) {
 
 function atom_entry($item,$type,$author,$owner,$comment = false) {
 
+       $a = get_app();
+
        if($item['deleted'])
                return '<at:deleted-entry ref="' . xmlify($item['uri']) . '" when="' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '" />' . "\r\n";
 
-       $a = get_app();
+
+       if($item['allow_cid'] || $item['allow_gid'] || $item['deny_cid'] || $item['deny_gid'])
+               $body = fix_private_photos($item['body'],$owner['uid']);
+       else
+               $body = $item['body'];
+
 
        $o = "\r\n\r\n<entry>\r\n";
 
        if(is_array($author))
                $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb']);
        else
-               $o .= atom_author('author',$item['name'],$item['url'],80,80,$item['thumb']);
+               $o .= atom_author('author',(($item['author-name']) ? $item['author-name'] : $item['name']),(($item['author-link']) ? $item['author-link'] : $item['url']),80,80,(($item['author-avatar']) ? $item['author-avatar'] : $item['thumb']));
        if(strlen($item['owner-name']))
                $o .= atom_author('dfrn:owner',$item['owner-name'],$item['owner-link'],80,80,$item['owner-avatar']);
 
@@ -1472,8 +1609,8 @@ function atom_entry($item,$type,$author,$owner,$comment = false) {
        $o .= '<title>' . xmlify($item['title']) . '</title>' . "\r\n";
        $o .= '<published>' . xmlify(datetime_convert('UTC','UTC',$item['created'] . '+00:00',ATOM_TIME)) . '</published>' . "\r\n";
        $o .= '<updated>' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '</updated>' . "\r\n";
-       $o .= '<dfrn:env>' . base64url_encode($item['body'], true) . '</dfrn:env>' . "\r\n";
-       $o .= '<content type="' . $type . '" >' . xmlify(($type === 'html') ? bbcode($item['body']) : $item['body']) . '</content>' . "\r\n";
+       $o .= '<dfrn:env>' . base64url_encode($body, true) . '</dfrn:env>' . "\r\n";
+       $o .= '<content type="' . $type . '" >' . xmlify(($type === 'html') ? bbcode($body) : $body) . '</content>' . "\r\n";
        $o .= '<link rel="alternate" type="text/html" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']) . '" />' . "\r\n";
        if($comment)
                $o .= '<dfrn:comment-allow>' . intval($item['last-child']) . '</dfrn:comment-allow>' . "\r\n";
@@ -1489,6 +1626,11 @@ function atom_entry($item,$type,$author,$owner,$comment = false) {
        if(($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid']))
                $o .= '<dfrn:private>1</dfrn:private>' . "\r\n";
 
+       if($item['extid'])
+               $o .= '<dfrn:extid>' . $item['extid'] . '</dfrn:extid>' . "\r\n";
+
+       if($item['app'])
+               $o .= '<statusnet:notice_info local_id="' . $item['id'] . '" source="' . $item['app'] . '" ></statusnet:notice_info>';
        $verb = construct_verb($item);
        $o .= '<as:verb>' . xmlify($verb) . '</as:verb>' . "\r\n";
        $actobj = construct_activity_object($item);
@@ -1498,6 +1640,15 @@ function atom_entry($item,$type,$author,$owner,$comment = false) {
        if(strlen($actarg))
                $o .= $actarg;
 
+       $tags = item_getfeedtags($item);
+       if(count($tags)) {
+               foreach($tags as $t) {
+                       $o .= '<category scheme="X-DFRN:' . xmlify($t[0]) . ':' . xmlify($t[1]) . '" term="' . xmlify($t[2]) . '" />' . "\r\n";
+               }
+       }
+
+       $o .= item_getfeedattach($item);
+
        $mentioned = get_mentions($item);
        if($mentioned)
                $o .= $mentioned;
@@ -1508,6 +1659,81 @@ function atom_entry($item,$type,$author,$owner,$comment = false) {
        
        return $o;
 }
+
+function fix_private_photos($s,$uid) {
+       $a = get_app();
+       logger('fix_private_photos');
+
+       if(preg_match("/\[img\](.*?)\[\/img\]/is",$s,$matches)) {
+               $image = $matches[1];
+               logger('fix_private_photos: found photo ' . $image);
+               if(stristr($image ,$a->get_baseurl() . '/photo/')) {
+                       $i = basename($image);
+                       $i = str_replace('.jpg','',$i);
+                       $x = strpos($i,'-');
+                       if($x) {
+                               $res = substr($i,$x+1);
+                               $i = substr($i,0,$x);
+                               $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d AND `uid` = %d",
+                                       dbesc($i),
+                                       intval($res),
+                                       intval($uid)
+                               );
+                               if(count($r)) {
+                                       logger('replacing photo');
+                                       $s = str_replace($image, 'data:image/jpg;base64,' . base64_encode($r[0]['data']), $s);
+                               }
+                       }
+                       logger('fix_private_photos: replaced: ' . $s, LOGGER_DATA);
+               }       
+       }
+       return($s);
+}
+
+
+
+function item_getfeedtags($item) {
+       $ret = array();
+       $matches = false;
+       $cnt = preg_match_all('|\#\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
+       if($cnt) {
+               for($x = 0; $x < count($matches); $x ++) {
+                       if($matches[1][$x])
+                               $ret[] = array('#',$matches[1][$x], $matches[2][$x]);
+               }
+       }
+       $matches = false; 
+       $cnt = preg_match_all('|\@\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
+       if($cnt) {
+               for($x = 0; $x < count($matches); $x ++) {
+                       if($matches[1][$x])
+                               $ret[] = array('#',$matches[1][$x], $matches[2][$x]);
+               }
+       } 
+       return $ret;
+}
+
+function item_getfeedattach($item) {
+       $ret = '';
+       $arr = explode(',',$item['attach']);
+       if(count($arr)) {
+               foreach($arr as $r) {
+                       $matches = false;
+                       $cnt = preg_match('|\[attach\]href=\"(.*?)\" size=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"\[\/attach\]|',$r,$matches);
+                       if($cnt) {
+                               $ret .= '<link rel="enclosure" href="' . xmlify($matches[1]) . '" type="' . xmlify($matches[3]) . '" ';
+                               if(intval($matches[2]))
+                                       $ret .= 'size="' . intval($matches[2]) . '" ';
+                               if($matches[4] !== ' ')
+                                       $ret .= 'title="' . xmlify(trim($matches[4])) . '" ';
+                               $ret .= ' />' . "\r\n";
+                       }
+               }
+       }
+       return $ret;
+}
+
+
        
 function item_expire($uid,$days) {
 
@@ -1554,4 +1780,131 @@ function item_expire($uid,$days) {
 
        proc_run('php',"include/notifier.php","expire","$uid");
 
+}
+
+
+function drop_items($items) {
+       $uid = 0;
+
+       if(count($items)) {
+               foreach($items as $item) {
+                       $owner = drop_item($item,false);
+                       if($owner && ! $uid)
+                               $uid = $owner;
+               }
+       }
+
+       // multiple threads may have been deleted, send an expire notification
+
+       if($uid)
+               proc_run('php',"include/notifier.php","expire","$uid");
+}
+
+
+function drop_item($id,$interactive = true) {
+
+       $a = get_app();
+
+       // locate item to be deleted
+
+       $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
+               intval($id)
+       );
+
+       if(! count($r)) {
+               if(! $interactive)
+                       return 0;
+               notice( t('Item not found.') . EOL);
+               goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
+       }
+
+       $item = $r[0];
+
+       $owner = $item['uid'];
+
+       // check if logged in user is either the author or owner of this item
+
+       if((local_user() == $item['uid']) || (remote_user() == $item['contact-id'])) {
+
+               // delete the item
+
+               $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
+                       dbesc(datetime_convert()),
+                       dbesc(datetime_convert()),
+                       intval($item['id'])
+               );
+
+               // 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. 
+
+               if(strlen($item['resource-id'])) {
+                       q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
+                               dbesc($item['resource-id']),
+                               intval($item['uid'])
+                       );
+                       // ignore the result
+               }
+
+               // 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",
+                               intval($item['event-id']),
+                               intval($item['uid'])
+                       );
+                       // ignore the result
+               }
+
+
+               // If it's the parent of a comment thread, kill all the kids
+
+               if($item['uri'] == $item['parent-uri']) {
+                       $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' 
+                               WHERE `parent-uri` = '%s' AND `uid` = %d ",
+                               dbesc(datetime_convert()),
+                               dbesc(datetime_convert()),
+                               dbesc($item['parent-uri']),
+                               intval($item['uid'])
+                       );
+                       // ignore the result
+               }
+               else {
+                       // 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 ",
+                               dbesc(datetime_convert()),
+                               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 `uid` = %d ORDER BY `edited` DESC LIMIT 1",
+                               dbesc($item['parent-uri']),
+                               intval($item['uid'])
+                       );
+                       if(count($r)) {
+                               q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
+                                       intval($r[0]['id'])
+                               );
+                       }       
+               }
+               $drop_id = intval($item['id']);
+                       
+               // send the notification upstream/downstream as the case may be
+
+               if(! $interactive)
+                       return $owner;
+
+               proc_run('php',"include/notifier.php","drop","$drop_id");
+               goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
+               //NOTREACHED
+       }
+       else {
+               if(! $interactive)
+                       return 0;
+               notice( t('Permission denied.') . EOL);
+               goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
+               //NOTREACHED
+       }
+       
 }
\ No newline at end of file