3 require_once('include/bbcode.php');
4 require_once('include/oembed.php');
5 require_once('include/salmon.php');
6 require_once('include/crypto.php');
8 function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
10 // default permissions - anonymous user
12 if(! strlen($owner_nick))
15 $public_feed = (($dfrn_id) ? false : true);
19 if($public_feed && $a->argc > 2) {
20 for($x = 2; $x < $a->argc; $x++) {
21 if($a->argv[$x] == 'converse')
23 if($a->argv[$x] == 'starred')
29 $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
31 $r = q("SELECT `contact`.*, `user`.`uid` AS `user_uid`, `user`.`nickname`, `user`.`timezone`
32 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
33 WHERE `contact`.`self` = 1 AND `user`.`nickname` = '%s' LIMIT 1",
41 $owner_id = $owner['user_uid'];
42 $owner_nick = $owner['nickname'];
44 $birthday = feed_birthday($owner_id,$owner['timezone']);
51 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
55 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
56 $my_id = '1:' . $dfrn_id;
59 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
60 $my_id = '0:' . $dfrn_id;
67 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
75 $groups = init_groups_visitor($contact['id']);
78 for($x = 0; $x < count($groups); $x ++)
79 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
80 $gs = implode('|', $groups);
83 $gs = '<<>>' ; // Impossible to match
85 $sql_extra = sprintf("
86 AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' )
87 AND ( `deny_cid` = '' OR NOT `deny_cid` REGEXP '<%d>' )
88 AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
89 AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s')
91 intval($contact['id']),
92 intval($contact['id']),
103 if(! strlen($last_update))
104 $last_update = 'now -30 days';
108 $sql_extra .= " AND `contact`.`self` = 1 ";
111 $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
113 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
114 `contact`.`name`, `contact`.`photo`, `contact`.`url`,
115 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
116 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
117 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`,
118 `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
119 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
120 LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
121 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`parent` != 0
122 AND `item`.`wall` = 1 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
123 AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
125 ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
132 // Will check further below if this actually returned results.
133 // We will provide an empty feed if that is the case.
137 $feed_template = get_markup_template(($dfrn_id) ? 'atom_feed_dfrn.tpl' : 'atom_feed.tpl');
141 $hubxml = feed_hublinks();
143 $salmon = feed_salmonlinks($owner_nick);
145 $atom .= replace_macros($feed_template, array(
146 '$version' => xmlify(FRIENDIKA_VERSION),
147 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
148 '$feed_title' => xmlify($owner['name']),
149 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', 'now' , ATOM_TIME)) ,
151 '$salmon' => $salmon,
152 '$name' => xmlify($owner['name']),
153 '$profile_page' => xmlify($owner['url']),
154 '$photo' => xmlify($owner['photo']),
155 '$thumb' => xmlify($owner['thumb']),
156 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
157 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
158 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
159 '$birthday' => ((strlen($birthday)) ? '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>' : '')
162 call_hooks('atom_feed', $atom);
164 if(! count($items)) {
166 call_hooks('atom_feed_end', $atom);
168 $atom .= '</feed>' . "\r\n";
172 foreach($items as $item) {
174 // public feeds get html, our own nodes use bbcode
178 // catch any email that's in a public conversation and make sure it doesn't leak
186 $atom .= atom_entry($item,$type,null,$owner,true);
189 call_hooks('atom_feed_end', $atom);
191 $atom .= '</feed>' . "\r\n";
197 function construct_verb($item) {
199 return $item['verb'];
200 return ACTIVITY_POST;
203 function construct_activity_object($item) {
205 if($item['object']) {
206 $o = '<as:object>' . "\r\n";
207 $r = parse_xml_string($item['object'],false);
213 $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
215 $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
217 $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
219 if(substr($r->link,0,1) === '<') {
220 // patch up some facebook "like" activity objects that got stored incorrectly
221 // for a couple of months prior to 9-Jun-2011 and generated bad XML.
222 // we can probably remove this hack here and in the following function in a few months time.
223 if(strstr($r->link,'&') && (! strstr($r->link,'&')))
224 $r->link = str_replace('&','&', $r->link);
225 $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
229 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
232 $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
233 $o .= '</as:object>' . "\r\n";
240 function construct_activity_target($item) {
242 if($item['target']) {
243 $o = '<as:target>' . "\r\n";
244 $r = parse_xml_string($item['target'],false);
248 $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
250 $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
252 $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
254 if(substr($r->link,0,1) === '<') {
255 if(strstr($r->link,'&') && (! strstr($r->link,'&')))
256 $r->link = str_replace('&','&', $r->link);
257 $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
261 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
264 $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
265 $o .= '</as:target>' . "\r\n";
275 function get_atom_elements($feed,$item) {
277 require_once('library/HTMLPurifier.auto.php');
278 require_once('include/html2bbcode.php');
280 $best_photo = array();
284 $author = $item->get_author();
286 $res['author-name'] = unxmlify($author->get_name());
287 $res['author-link'] = unxmlify($author->get_link());
290 $res['author-name'] = unxmlify($feed->get_title());
291 $res['author-link'] = unxmlify($feed->get_permalink());
293 $res['uri'] = unxmlify($item->get_id());
294 $res['title'] = unxmlify($item->get_title());
295 $res['body'] = unxmlify($item->get_content());
296 $res['plink'] = unxmlify($item->get_link(0));
298 // look for a photo. We should check media size and find the best one,
299 // but for now let's just find any author photo
301 $rawauthor = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
303 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
304 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
305 foreach($base as $link) {
306 if(! $res['author-avatar']) {
307 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
308 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
313 $rawactor = $item->get_item_tags(NAMESPACE_ACTIVITY, 'actor');
315 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
316 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
317 if($base && count($base)) {
318 foreach($base as $link) {
319 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
320 $res['author-link'] = unxmlify($link['attribs']['']['href']);
321 if(! $res['author-avatar']) {
322 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
323 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
329 // No photo/profile-link on the item - look at the feed level
331 if((! (x($res,'author-link'))) || (! (x($res,'author-avatar')))) {
332 $rawauthor = $feed->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
333 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
334 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
335 foreach($base as $link) {
336 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
337 $res['author-link'] = unxmlify($link['attribs']['']['href']);
338 if(! $res['author-avatar']) {
339 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
340 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
345 $rawactor = $feed->get_feed_tags(NAMESPACE_ACTIVITY, 'subject');
347 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
348 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
350 if($base && count($base)) {
351 foreach($base as $link) {
352 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
353 $res['author-link'] = unxmlify($link['attribs']['']['href']);
354 if(! (x($res,'author-avatar'))) {
355 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
356 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
363 $apps = $item->get_item_tags(NAMESPACE_STATUSNET,'notice_info');
364 if($apps && $apps[0]['attribs']['']['source']) {
365 $res['app'] = strip_tags(unxmlify($apps[0]['attribs']['']['source']));
366 if($res['app'] === 'web')
367 $res['app'] = 'OStatus';
370 // base64 encoded json structure representing Diaspora signature
372 $dsig = $item->get_item_tags(NAMESPACE_DFRN,'diaspora_signature');
374 $res['dsprsig'] = unxmlify($dsig[0]['data']);
377 $dguid = $item->get_item_tags(NAMESPACE_DFRN,'diaspora_guid');
379 $res['guid'] = unxmlify($dguid[0]['data']);
381 $bm = $item->get_item_tags(NAMESPACE_DFRN,'bookmark');
383 $res['bookmark'] = ((unxmlify($bm[0]['data']) === 'true') ? 1 : 0);
387 * If there's a copy of the body content which is guaranteed to have survived mangling in transit, use it.
390 $have_real_body = false;
392 $rawenv = $item->get_item_tags(NAMESPACE_DFRN, 'env');
394 $have_real_body = true;
395 $res['body'] = $rawenv[0]['data'];
396 $res['body'] = str_replace(array(' ',"\t","\r","\n"), array('','','',''),$res['body']);
397 // make sure nobody is trying to sneak some html tags by us
398 $res['body'] = notags(base64url_decode($res['body']));
401 $maxlen = get_max_import_size();
402 if($maxlen && (strlen($res['body']) > $maxlen))
403 $res['body'] = substr($res['body'],0, $maxlen);
405 // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust
406 // the content type. Our own network only emits text normally, though it might have been converted to
407 // html if we used a pubsubhubbub transport. But if we see even one html tag in our text, we will
408 // have to assume it is all html and needs to be purified.
410 // It doesn't matter all that much security wise - because before this content is used anywhere, we are
411 // going to escape any tags we find regardless, but this lets us import a limited subset of html from
412 // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining
415 if((strpos($res['body'],'<') !== false) || (strpos($res['body'],'>') !== false)) {
417 $res['body'] = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
418 '[youtube]$1[/youtube]', $res['body']);
420 $res['body'] = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
421 '[youtube]$1[/youtube]', $res['body']);
423 $res['body'] = oembed_html2bbcode($res['body']);
425 $config = HTMLPurifier_Config::createDefault();
426 $config->set('Cache.DefinitionImpl', null);
428 // we shouldn't need a whitelist, because the bbcode converter
429 // will strip out any unsupported tags.
430 // $config->set('HTML.Allowed', 'p,b,a[href],i');
432 $purifier = new HTMLPurifier($config);
433 $res['body'] = $purifier->purify($res['body']);
435 $res['body'] = html2bbcode($res['body']);
438 $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
439 if($allow && $allow[0]['data'] == 1)
440 $res['last-child'] = 1;
442 $res['last-child'] = 0;
444 $private = $item->get_item_tags(NAMESPACE_DFRN,'private');
445 if($private && $private[0]['data'] == 1)
450 $extid = $item->get_item_tags(NAMESPACE_DFRN,'extid');
451 if($extid && $extid[0]['data'])
452 $res['extid'] = $extid[0]['data'];
454 $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
456 $res['location'] = unxmlify($rawlocation[0]['data']);
459 $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
461 $res['created'] = unxmlify($rawcreated[0]['data']);
464 $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
466 $res['edited'] = unxmlify($rawedited[0]['data']);
468 if((x($res,'edited')) && (! (x($res,'created'))))
469 $res['created'] = $res['edited'];
471 if(! $res['created'])
472 $res['created'] = $item->get_date('c');
475 $res['edited'] = $item->get_date('c');
478 // Disallow time travelling posts
480 $d1 = strtotime($res['created']);
481 $d2 = strtotime($res['edited']);
482 $d3 = strtotime('now');
485 $res['created'] = datetime_convert();
487 $res['edited'] = datetime_convert();
489 $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
490 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
491 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
492 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
493 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
494 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
495 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
496 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
497 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
499 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
500 $base = $rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
502 foreach($base as $link) {
503 if(! $res['owner-avatar']) {
504 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
505 $res['owner-avatar'] = unxmlify($link['attribs']['']['href']);
510 $rawgeo = $item->get_item_tags(NAMESPACE_GEORSS,'point');
512 $res['coord'] = unxmlify($rawgeo[0]['data']);
515 $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
517 // select between supported verbs
520 $res['verb'] = unxmlify($rawverb[0]['data']);
523 // translate OStatus unfollow to activity streams if it happened to get selected
525 if((x($res,'verb')) && ($res['verb'] === 'http://ostatus.org/schema/1.0/unfollow'))
526 $res['verb'] = ACTIVITY_UNFOLLOW;
528 $cats = $item->get_categories();
531 foreach($cats as $cat) {
532 $term = $cat->get_term();
534 $term = $cat->get_label();
535 $scheme = $cat->get_scheme();
536 if($scheme && $term && stristr($scheme,'X-DFRN:'))
537 $tag_arr[] = substr($scheme,7,1) . '[url=' . unxmlify(substr($scheme,9)) . ']' . unxmlify($term) . '[/url]';
539 $tag_arr[] = notags(trim($term));
541 $res['tag'] = implode(',', $tag_arr);
544 $attach = $item->get_enclosures();
547 foreach($attach as $att) {
548 $len = intval($att->get_length());
549 $link = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_link()))));
550 $title = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_title()))));
551 $type = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_type()))));
552 if(strpos($type,';'))
553 $type = substr($type,0,strpos($type,';'));
554 if((! $link) || (strpos($link,'http') !== 0))
560 $type = 'application/octet-stream';
562 $att_arr[] = '[attach]href="' . $link . '" length="' . $len . '" type="' . $type . '" title="' . $title . '"[/attach]';
564 $res['attach'] = implode(',', $att_arr);
567 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
570 $res['object'] = '<object>' . "\n";
571 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
572 $res['object-type'] = $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'];
573 $res['object'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
575 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
576 $res['object'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
577 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
578 $res['object'] .= '<link>' . encode_rel_links($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
579 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
580 $res['object'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
581 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
582 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
584 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
585 // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
586 $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
587 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
589 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
590 '[youtube]$1[/youtube]', $body);
592 $res['body'] = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
593 '[youtube]$1[/youtube]', $res['body']);
596 $config = HTMLPurifier_Config::createDefault();
597 $config->set('Cache.DefinitionImpl', null);
599 $purifier = new HTMLPurifier($config);
600 $body = $purifier->purify($body);
601 $body = html2bbcode($body);
604 $res['object'] .= '<content>' . $body . '</content>' . "\n";
607 $res['object'] .= '</object>' . "\n";
610 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'target');
613 $res['target'] = '<target>' . "\n";
614 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
615 $res['target'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
617 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
618 $res['target'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
620 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
621 $res['target'] .= '<link>' . encode_rel_links($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
622 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
623 $res['target'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
624 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
625 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
627 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
628 // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
629 $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
630 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
632 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
633 '[youtube]$1[/youtube]', $body);
635 $res['body'] = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
636 '[youtube]$1[/youtube]', $res['body']);
638 $config = HTMLPurifier_Config::createDefault();
639 $config->set('Cache.DefinitionImpl', null);
641 $purifier = new HTMLPurifier($config);
642 $body = $purifier->purify($body);
643 $body = html2bbcode($body);
646 $res['target'] .= '<content>' . $body . '</content>' . "\n";
649 $res['target'] .= '</target>' . "\n";
652 $arr = array('feed' => $feed, 'item' => $item, 'result' => $res);
654 call_hooks('parse_atom', $arr);
659 function encode_rel_links($links) {
661 if(! ((is_array($links)) && (count($links))))
663 foreach($links as $link) {
665 if($link['attribs']['']['rel'])
666 $o .= 'rel="' . $link['attribs']['']['rel'] . '" ';
667 if($link['attribs']['']['type'])
668 $o .= 'type="' . $link['attribs']['']['type'] . '" ';
669 if($link['attribs']['']['href'])
670 $o .= 'href="' . $link['attribs']['']['href'] . '" ';
671 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['width'])
672 $o .= 'media:width="' . $link['attribs'][NAMESPACE_MEDIA]['width'] . '" ';
673 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['height'])
674 $o .= 'media:height="' . $link['attribs'][NAMESPACE_MEDIA]['height'] . '" ';
680 function item_store($arr,$force_parent = false) {
682 // If a Diaspora signature structure was passed in, pull it out of the
683 // item array and set it aside for later storage.
686 if(x($arr,'dsprsig')) {
687 $dsprsig = json_decode(base64_decode($arr['dsprsig']));
688 unset($arr['dsprsig']);
692 $arr['gravity'] = intval($arr['gravity']);
693 elseif($arr['parent-uri'] == $arr['uri'])
695 elseif(activity_match($arr['verb'],ACTIVITY_POST))
698 $arr['gravity'] = 6; // extensible catchall
701 $arr['type'] = 'remote';
703 // Shouldn't happen but we want to make absolutely sure it doesn't leak from a plugin.
705 if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false))
706 $arr['body'] = strip_tags($arr['body']);
709 $arr['wall'] = ((x($arr,'wall')) ? intval($arr['wall']) : 0);
710 $arr['uri'] = ((x($arr,'uri')) ? notags(trim($arr['uri'])) : random_string());
711 $arr['extid'] = ((x($arr,'extid')) ? notags(trim($arr['extid'])) : '');
712 $arr['author-name'] = ((x($arr,'author-name')) ? notags(trim($arr['author-name'])) : '');
713 $arr['author-link'] = ((x($arr,'author-link')) ? notags(trim($arr['author-link'])) : '');
714 $arr['author-avatar'] = ((x($arr,'author-avatar')) ? notags(trim($arr['author-avatar'])) : '');
715 $arr['owner-name'] = ((x($arr,'owner-name')) ? notags(trim($arr['owner-name'])) : '');
716 $arr['owner-link'] = ((x($arr,'owner-link')) ? notags(trim($arr['owner-link'])) : '');
717 $arr['owner-avatar'] = ((x($arr,'owner-avatar')) ? notags(trim($arr['owner-avatar'])) : '');
718 $arr['created'] = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
719 $arr['edited'] = ((x($arr,'edited') !== false) ? datetime_convert('UTC','UTC',$arr['edited']) : datetime_convert());
720 $arr['commented'] = datetime_convert();
721 $arr['received'] = datetime_convert();
722 $arr['changed'] = datetime_convert();
723 $arr['title'] = ((x($arr,'title')) ? notags(trim($arr['title'])) : '');
724 $arr['location'] = ((x($arr,'location')) ? notags(trim($arr['location'])) : '');
725 $arr['coord'] = ((x($arr,'coord')) ? notags(trim($arr['coord'])) : '');
726 $arr['last-child'] = ((x($arr,'last-child')) ? intval($arr['last-child']) : 0 );
727 $arr['visible'] = ((x($arr,'visible') !== false) ? intval($arr['visible']) : 1 );
729 $arr['parent-uri'] = ((x($arr,'parent-uri')) ? notags(trim($arr['parent-uri'])) : '');
730 $arr['verb'] = ((x($arr,'verb')) ? notags(trim($arr['verb'])) : '');
731 $arr['object-type'] = ((x($arr,'object-type')) ? notags(trim($arr['object-type'])) : '');
732 $arr['object'] = ((x($arr,'object')) ? trim($arr['object']) : '');
733 $arr['target-type'] = ((x($arr,'target-type')) ? notags(trim($arr['target-type'])) : '');
734 $arr['target'] = ((x($arr,'target')) ? trim($arr['target']) : '');
735 $arr['plink'] = ((x($arr,'plink')) ? notags(trim($arr['plink'])) : '');
736 $arr['allow_cid'] = ((x($arr,'allow_cid')) ? trim($arr['allow_cid']) : '');
737 $arr['allow_gid'] = ((x($arr,'allow_gid')) ? trim($arr['allow_gid']) : '');
738 $arr['deny_cid'] = ((x($arr,'deny_cid')) ? trim($arr['deny_cid']) : '');
739 $arr['deny_gid'] = ((x($arr,'deny_gid')) ? trim($arr['deny_gid']) : '');
740 $arr['private'] = ((x($arr,'private')) ? intval($arr['private']) : 0 );
741 $arr['bookmark'] = ((x($arr,'bookmark')) ? intval($arr['bookmark']) : 0 );
742 $arr['body'] = ((x($arr,'body')) ? trim($arr['body']) : '');
743 $arr['tag'] = ((x($arr,'tag')) ? notags(trim($arr['tag'])) : '');
744 $arr['attach'] = ((x($arr,'attach')) ? notags(trim($arr['attach'])) : '');
745 $arr['app'] = ((x($arr,'app')) ? notags(trim($arr['app'])) : '');
746 $arr['guid'] = ((x($arr,'guid')) ? notags(trim($arr['guid'])) : get_guid());
748 if($arr['parent-uri'] === $arr['uri']) {
750 $allow_cid = $arr['allow_cid'];
751 $allow_gid = $arr['allow_gid'];
752 $deny_cid = $arr['deny_cid'];
753 $deny_gid = $arr['deny_gid'];
757 // find the parent and snarf the item id and ACL's
758 // and anything else we need to inherit
760 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
761 dbesc($arr['parent-uri']),
767 // is the new message multi-level threaded?
768 // even though we don't support it now, preserve the info
769 // and re-attach to the conversation parent.
771 if($r[0]['uri'] != $r[0]['parent-uri']) {
772 $arr['thr-parent'] = $arr['parent-uri'];
773 $arr['parent-uri'] = $r[0]['parent-uri'];
774 $z = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `parent-uri` = '%s' AND `uid` = %d LIMIT 1",
775 dbesc($r[0]['parent-uri']),
776 dbesc($r[0]['parent-uri']),
783 $parent_id = $r[0]['id'];
784 $parent_deleted = $r[0]['deleted'];
785 $allow_cid = $r[0]['allow_cid'];
786 $allow_gid = $r[0]['allow_gid'];
787 $deny_cid = $r[0]['deny_cid'];
788 $deny_gid = $r[0]['deny_gid'];
789 $arr['wall'] = $r[0]['wall'];
793 // Allow one to see reply tweets from status.net even when
794 // we don't have or can't see the original post.
797 logger('item_store: $force_parent=true, reply converted to top-level post.');
799 $arr['thr-parent'] = $arr['parent-uri'];
800 $arr['parent-uri'] = $arr['uri'];
804 logger('item_store: item parent was not found - ignoring item');
811 call_hooks('post_remote',$arr);
815 logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
817 $r = dbq("INSERT INTO `item` (`"
818 . implode("`, `", array_keys($arr))
820 . implode("', '", array_values($arr))
823 // find the item we just created
825 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
826 $arr['uri'], // already dbesc'd
830 // This is not good, but perhaps we encountered a rare race/cache condition, so back off and try again.
832 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
833 $arr['uri'], // already dbesc'd
839 $current_post = $r[0]['id'];
840 logger('item_store: created item ' . $current_post);
843 logger('item_store: could not locate created item');
847 if((! $parent_id) || ($arr['parent-uri'] === $arr['uri']))
848 $parent_id = $current_post;
850 if(strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid))
853 $private = $arr['private'];
855 // Set parent id - and also make sure to inherit the parent's ACL's.
857 $r = q("UPDATE `item` SET `parent` = %d, `allow_cid` = '%s', `allow_gid` = '%s',
858 `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d, `deleted` = %d WHERE `id` = %d LIMIT 1",
865 intval($parent_deleted),
866 intval($current_post)
869 // update the commented timestamp on the parent
871 q("UPDATE `item` set `commented` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
872 dbesc(datetime_convert()),
873 dbesc(datetime_convert()),
878 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
879 intval($current_post),
880 dbesc($dsprsig->signed_text),
881 dbesc($dsprsig->signature),
882 dbesc($dsprsig->signer)
888 * If this is now the last-child, force all _other_ children of this parent to *not* be last-child
891 if($arr['last-child']) {
892 $r = q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d AND `id` != %d",
895 intval($current_post)
899 return $current_post;
902 function get_item_contact($item,$contacts) {
903 if(! count($contacts) || (! is_array($item)))
905 foreach($contacts as $contact) {
906 if($contact['id'] == $item['contact-id']) {
915 function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
919 if((! strlen($contact['issued-id'])) && (! $contact['duplex']) && (! ($owner['page-flags'] == PAGE_COMMUNITY)))
922 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
924 if($contact['duplex'] && $contact['dfrn-id'])
925 $idtosend = '0:' . $orig_id;
926 if($contact['duplex'] && $contact['issued-id'])
927 $idtosend = '1:' . $orig_id;
929 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
931 $rino_enable = get_config('system','rino_encrypt');
936 $url = $contact['notify'] . '&dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino=1' : '');
938 logger('dfrn_deliver: ' . $url);
940 $xml = fetch_url($url);
942 $curl_stat = $a->get_curl_code();
944 return(-1); // timed out
946 logger('dfrn_deliver: ' . $xml, LOGGER_DATA);
951 if(strpos($xml,'<?xml') === false) {
952 logger('dfrn_deliver: no valid XML returned');
953 logger('dfrn_deliver: returned XML: ' . $xml, LOGGER_DATA);
957 $res = parse_xml_string($xml);
959 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
960 return (($res->status) ? $res->status : 3);
963 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
964 $challenge = hex2bin((string) $res->challenge);
965 $dfrn_version = (float) (($res->dfrn_version) ? $res->dfrn_version : 2.0);
966 $rino_allowed = ((intval($res->rino) === 1) ? 1 : 0);
971 if(($contact['duplex'] && strlen($contact['pubkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
972 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
973 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
976 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
977 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
980 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
982 if(strpos($final_dfrn_id,':') == 1)
983 $final_dfrn_id = substr($final_dfrn_id,2);
985 if($final_dfrn_id != $orig_id) {
986 logger('dfrn_deliver: wrong dfrn_id.');
987 // did not decode properly - cannot trust this site
991 $postvars['dfrn_id'] = $idtosend;
992 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
994 $postvars['dissolve'] = '1';
997 if((($contact['rel']) && ($contact['rel'] != CONTACT_IS_SHARING) && (! $contact['blocked'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
998 $postvars['data'] = $atom;
999 $postvars['perm'] = 'rw';
1002 $postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
1003 $postvars['perm'] = 'r';
1006 if($rino && $rino_allowed && (! $dissolve)) {
1007 $key = substr(random_string(),0,16);
1008 $data = bin2hex(aes_encrypt($postvars['data'],$key));
1009 $postvars['data'] = $data;
1010 logger('rino: sent key = ' . $key, LOGGER_DEBUG);
1013 if($dfrn_version >= 2.1) {
1014 if(($contact['duplex'] && strlen($contact['pubkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
1015 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
1018 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
1022 if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
1023 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
1026 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
1030 logger('md5 rawkey ' . md5($postvars['key']));
1032 $postvars['key'] = bin2hex($postvars['key']);
1035 logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);
1037 $xml = post_url($contact['notify'],$postvars);
1039 logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
1041 $curl_stat = $a->get_curl_code();
1042 if((! $curl_stat) || (! strlen($xml)))
1043 return(-1); // timed out
1045 if(strpos($xml,'<?xml') === false) {
1046 logger('dfrn_deliver: phase 2: no valid XML returned');
1047 logger('dfrn_deliver: phase 2: returned XML: ' . $xml, LOGGER_DATA);
1051 $res = parse_xml_string($xml);
1053 return $res->status;
1059 * consume_feed - process atom feed and update anything/everything we might need to update
1061 * $xml = the (atom) feed to consume - RSS isn't as fully supported but may work for simple feeds.
1063 * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
1064 * It is this person's stuff that is going to be updated.
1065 * $contact = the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
1066 * from an external network and MAY create an appropriate contact record. Otherwise, we MUST
1067 * have a contact record.
1068 * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or
1069 * might not) try and subscribe to it.
1073 function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $secure_feed = false) {
1075 require_once('library/simplepie/simplepie.inc');
1077 if(! strlen($xml)) {
1078 logger('consume_feed: empty input');
1082 $feed = new SimplePie();
1083 $feed->set_raw_data($xml);
1085 $feed->enable_order_by_date(true);
1087 $feed->enable_order_by_date(false);
1091 logger('consume_feed: Error parsing XML: ' . $feed->error());
1093 $permalink = $feed->get_permalink();
1095 // Check at the feed level for updated contact name and/or photo
1099 $photo_timestamp = '';
1103 $hubs = $feed->get_links('hub');
1106 $hub = implode(',', $hubs);
1108 $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'owner');
1110 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
1112 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
1113 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
1114 $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
1115 $new_name = $elems['name'][0]['data'];
1117 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
1118 $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
1119 $photo_url = $elems['link'][0]['attribs']['']['href'];
1122 if((x($rawtags[0]['child'], NAMESPACE_DFRN)) && (x($rawtags[0]['child'][NAMESPACE_DFRN],'birthday'))) {
1123 $birthday = datetime_convert('UTC','UTC', $rawtags[0]['child'][NAMESPACE_DFRN]['birthday'][0]['data']);
1127 if((is_array($contact)) && ($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
1128 logger('consume_feed: Updating photo for ' . $contact['name']);
1129 require_once("Photo.php");
1130 $photo_failure = false;
1131 $have_photo = false;
1133 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
1134 intval($contact['id']),
1135 intval($contact['uid'])
1138 $resource_id = $r[0]['resource-id'];
1142 $resource_id = photo_new_resource();
1145 $img_str = fetch_url($photo_url,true);
1146 $img = new Photo($img_str);
1147 if($img->is_valid()) {
1149 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
1150 dbesc($resource_id),
1151 intval($contact['id']),
1152 intval($contact['uid'])
1156 $img->scaleImageSquare(175);
1158 $hash = $resource_id;
1159 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 4);
1161 $img->scaleImage(80);
1162 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 5);
1164 $img->scaleImage(48);
1165 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 6);
1169 q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
1170 WHERE `uid` = %d AND `id` = %d LIMIT 1",
1171 dbesc(datetime_convert()),
1172 dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.jpg'),
1173 dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.jpg'),
1174 dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.jpg'),
1175 intval($contact['uid']),
1176 intval($contact['id'])
1181 if((is_array($contact)) && ($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
1182 q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1183 dbesc(notags(trim($new_name))),
1184 dbesc(datetime_convert()),
1185 intval($contact['uid']),
1186 intval($contact['id'])
1190 if(strlen($birthday)) {
1191 if(substr($birthday,0,4) != $contact['bdyear']) {
1192 logger('consume_feed: updating birthday: ' . $birthday);
1196 * Add new birthday event for this person
1198 * $bdtext is just a readable placeholder in case the event is shared
1199 * with others. We will replace it during presentation to our $importer
1200 * to contain a sparkle link and perhaps a photo.
1204 $bdtext = t('Birthday:') . ' [url=' . $contact['url'] . ']' . $contact['name'] . '[/url]' ;
1207 $r = q("INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`desc`,`type`)
1208 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s' ) ",
1209 intval($contact['uid']),
1210 intval($contact['id']),
1211 dbesc(datetime_convert()),
1212 dbesc(datetime_convert()),
1213 dbesc(datetime_convert('UTC','UTC', $birthday)),
1214 dbesc(datetime_convert('UTC','UTC', $birthday . ' + 1 day ')),
1222 q("UPDATE `contact` SET `bdyear` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1223 dbesc(substr($birthday,0,4)),
1224 intval($contact['uid']),
1225 intval($contact['id'])
1228 // This function is called twice without reloading the contact
1229 // Make sure we only create one event. This is why &$contact
1230 // is a reference var in this function
1232 $contact['bdyear'] = substr($birthday,0,4);
1238 // process any deleted entries
1240 $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
1241 if(is_array($del_entries) && count($del_entries)) {
1242 foreach($del_entries as $dentry) {
1244 if(isset($dentry['attribs']['']['ref'])) {
1245 $uri = $dentry['attribs']['']['ref'];
1247 if(isset($dentry['attribs']['']['when'])) {
1248 $when = $dentry['attribs']['']['when'];
1249 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
1252 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
1254 if($deleted && is_array($contact)) {
1255 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `contact-id` = %d LIMIT 1",
1257 intval($importer['uid']),
1258 intval($contact['id'])
1263 if(! $item['deleted'])
1264 logger('consume_feed: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
1266 if($item['uri'] == $item['parent-uri']) {
1267 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1268 `body` = '', `title` = ''
1269 WHERE `parent-uri` = '%s' AND `uid` = %d",
1271 dbesc(datetime_convert()),
1272 dbesc($item['uri']),
1273 intval($importer['uid'])
1277 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1278 `body` = '', `title` = ''
1279 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1281 dbesc(datetime_convert()),
1283 intval($importer['uid'])
1285 if($item['last-child']) {
1286 // ensure that last-child is set in case the comment that had it just got wiped.
1287 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
1288 dbesc(datetime_convert()),
1289 dbesc($item['parent-uri']),
1290 intval($item['uid'])
1292 // who is the last child now?
1293 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
1294 ORDER BY `created` DESC LIMIT 1",
1295 dbesc($item['parent-uri']),
1296 intval($importer['uid'])
1299 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
1310 // Now process the feed
1312 if($feed->get_item_quantity()) {
1314 logger('consume_feed: feed item count = ' . $feed->get_item_quantity());
1316 // in inverse date order
1318 $items = array_reverse($feed->get_items());
1320 $items = $feed->get_items();
1323 foreach($items as $item) {
1326 $item_id = $item->get_id();
1327 $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
1328 if(isset($rawthread[0]['attribs']['']['ref'])) {
1330 $parent_uri = $rawthread[0]['attribs']['']['ref'];
1333 if(($is_reply) && is_array($contact)) {
1335 // Have we seen it? If not, import it.
1337 $item_id = $item->get_id();
1338 $datarray = get_atom_elements($feed,$item);
1340 if(! x($datarray,'author-name'))
1341 $datarray['author-name'] = $contact['name'];
1342 if(! x($datarray,'author-link'))
1343 $datarray['author-link'] = $contact['url'];
1344 if(! x($datarray,'author-avatar'))
1345 $datarray['author-avatar'] = $contact['thumb'];
1348 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1350 intval($importer['uid'])
1353 // Update content if 'updated' changes
1356 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
1357 $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1358 dbesc($datarray['body']),
1359 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
1361 intval($importer['uid'])
1365 // update last-child if it changes
1367 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
1368 if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
1369 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1370 dbesc(datetime_convert()),
1372 intval($importer['uid'])
1374 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1375 intval($allow[0]['data']),
1376 dbesc(datetime_convert()),
1378 intval($importer['uid'])
1384 $force_parent = false;
1385 if($contact['network'] === 'stat') {
1386 $force_parent = true;
1387 if(strlen($datarray['title']))
1388 unset($datarray['title']);
1389 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1390 dbesc(datetime_convert()),
1392 intval($importer['uid'])
1394 $datarray['last-child'] = 1;
1397 if(($contact['network'] === 'feed') || (! strlen($contact['notify']))) {
1398 // one way feed - no remote comment ability
1399 $datarray['last-child'] = 0;
1401 $datarray['parent-uri'] = $parent_uri;
1402 $datarray['uid'] = $importer['uid'];
1403 $datarray['contact-id'] = $contact['id'];
1404 if((activity_match($datarray['verb'],ACTIVITY_LIKE)) || (activity_match($datarray['verb'],ACTIVITY_DISLIKE))) {
1405 $datarray['type'] = 'activity';
1406 $datarray['gravity'] = GRAVITY_LIKE;
1409 $r = item_store($datarray,$force_parent);
1415 // Head post of a conversation. Have we seen it? If not, import it.
1417 $item_id = $item->get_id();
1419 $datarray = get_atom_elements($feed,$item);
1421 if(is_array($contact)) {
1422 if(! x($datarray,'author-name'))
1423 $datarray['author-name'] = $contact['name'];
1424 if(! x($datarray,'author-link'))
1425 $datarray['author-link'] = $contact['url'];
1426 if(! x($datarray,'author-avatar'))
1427 $datarray['author-avatar'] = $contact['thumb'];
1430 if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
1431 $ev = bbtoevent($datarray['body']);
1432 if(x($ev,'desc') && x($ev,'start')) {
1433 $ev['uid'] = $importer['uid'];
1434 $ev['uri'] = $item_id;
1435 $ev['edited'] = $datarray['edited'];
1436 $ev['private'] = $datarray['private'];
1438 if(is_array($contact))
1439 $ev['cid'] = $contact['id'];
1440 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1442 intval($importer['uid'])
1445 $ev['id'] = $r[0]['id'];
1446 $xyz = event_store($ev);
1451 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1453 intval($importer['uid'])
1456 // Update content if 'updated' changes
1459 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
1460 $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1461 dbesc($datarray['body']),
1462 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
1464 intval($importer['uid'])
1468 // update last-child if it changes
1470 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
1471 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
1472 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1473 intval($allow[0]['data']),
1474 dbesc(datetime_convert()),
1476 intval($importer['uid'])
1482 if(activity_match($datarray['verb'],ACTIVITY_FOLLOW)) {
1483 logger('consume-feed: New follower');
1484 new_follower($importer,$contact,$datarray,$item);
1487 if(activity_match($datarray['verb'],ACTIVITY_UNFOLLOW)) {
1488 lose_follower($importer,$contact,$datarray,$item);
1491 if(! is_array($contact))
1494 if($contact['network'] === 'stat' || stristr($permalink,'twitter.com')) {
1495 if(strlen($datarray['title']))
1496 unset($datarray['title']);
1497 $datarray['last-child'] = 1;
1500 if(($contact['network'] === 'feed') || (! strlen($contact['notify']))) {
1501 // one way feed - no remote comment ability
1502 $datarray['last-child'] = 0;
1505 // This is my contact on another system, but it's really me.
1506 // Turn this into a wall post.
1508 if($contact['remote_self'])
1509 $datarray['wall'] = 1;
1511 $datarray['parent-uri'] = $item_id;
1512 $datarray['uid'] = $importer['uid'];
1513 $datarray['contact-id'] = $contact['id'];
1514 $r = item_store($datarray);
1522 function new_follower($importer,$contact,$datarray,$item) {
1523 $url = notags(trim($datarray['author-link']));
1524 $name = notags(trim($datarray['author-name']));
1525 $photo = notags(trim($datarray['author-avatar']));
1527 $rawtag = $item->get_item_tags(NAMESPACE_ACTIVITY,'actor');
1528 if($rawtag && $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'])
1529 $nick = $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'];
1531 if(is_array($contact)) {
1532 if($contact['network'] == 'stat' && $contact['rel'] == CONTACT_IS_SHARING) {
1533 $r = q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
1534 intval(CONTACT_IS_FRIEND),
1535 intval($contact['id']),
1536 intval($importer['uid'])
1540 // send email notification to owner?
1544 // create contact record
1546 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `nick`, `photo`, `network`, `rel`,
1547 `blocked`, `readonly`, `pending`, `writable` )
1548 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 1, 1 ) ",
1549 intval($importer['uid']),
1550 dbesc(datetime_convert()),
1556 intval(CONTACT_IS_FOLLOWER)
1558 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `pending` = 1 AND `rel` = %d LIMIT 1",
1559 intval($importer['uid']),
1561 intval(CONTACT_IS_FOLLOWER)
1564 $contact_record = $r[0];
1566 // create notification
1567 $hash = random_string();
1569 if(is_array($contact_record)) {
1570 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `hash`, `datetime`)
1571 VALUES ( %d, %d, 0, 0, '%s', '%s' )",
1572 intval($importer['uid']),
1573 intval($contact_record['id']),
1575 dbesc(datetime_convert())
1578 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
1579 intval($importer['uid'])
1583 if(($r[0]['notify-flags'] & NOTIFY_INTRO) && ($r[0]['page-flags'] == PAGE_NORMAL)) {
1584 $email_tpl = get_intltext_template('follow_notify_eml.tpl');
1585 $email = replace_macros($email_tpl, array(
1586 '$requestor' => ((strlen($name)) ? $name : t('[Name Withheld]')),
1588 '$myname' => $r[0]['username'],
1589 '$siteurl' => $a->get_baseurl(),
1590 '$sitename' => $a->config['sitename']
1592 $res = mail($r[0]['email'],
1593 t("You have a new follower at ") . $a->config['sitename'],
1595 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] . "\n"
1596 . 'Content-type: text/plain; charset=UTF-8' . "\n"
1597 . 'Content-transfer-encoding: 8bit' );
1604 function lose_follower($importer,$contact,$datarray,$item) {
1606 if(($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_SHARING)) {
1607 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
1608 intval(CONTACT_IS_SHARING),
1609 intval($contact['id'])
1613 contact_remove($contact['id']);
1618 function subscribe_to_hub($url,$importer,$contact,$submode = 'subscribe') {
1620 if(is_array($importer)) {
1621 $r = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
1622 intval($importer['uid'])
1626 // Diaspora has different message-ids in feeds than they do
1627 // through the direct Diaspora protocol. If we try and use
1628 // the feed, we'll get duplicates. So don't.
1630 if((! count($r)) || $contact['network'] === NETWORK_DIASPORA)
1633 $push_url = get_config('system','url') . '/pubsub/' . $r[0]['nickname'] . '/' . $contact['id'];
1635 // Use a single verify token, even if multiple hubs
1637 $verify_token = ((strlen($contact['hub-verify'])) ? $contact['hub-verify'] : random_string());
1639 $params= 'hub.mode=' . $hubmode . '&hub.callback=' . urlencode($push_url) . '&hub.topic=' . urlencode($contact['poll']) . '&hub.verify=async&hub.verify_token=' . $verify_token;
1641 logger('subscribe_to_hub: subscribing ' . $contact['name'] . ' to hub ' . $url . ' with verifier ' . $verify_token);
1643 if(! strlen($contact['hub-verify'])) {
1644 $r = q("UPDATE `contact` SET `hub-verify` = '%s' WHERE `id` = %d LIMIT 1",
1645 dbesc($verify_token),
1646 intval($contact['id'])
1650 post_url($url,$params);
1656 function atom_author($tag,$name,$uri,$h,$w,$photo) {
1660 $name = xmlify($name);
1661 $uri = xmlify($uri);
1664 $photo = xmlify($photo);
1668 $o .= "<name>$name</name>\r\n";
1669 $o .= "<uri>$uri</uri>\r\n";
1670 $o .= '<link rel="photo" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
1671 $o .= '<link rel="avatar" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
1673 call_hooks('atom_author', $o);
1675 $o .= "</$tag>\r\n";
1679 function atom_entry($item,$type,$author,$owner,$comment = false) {
1683 if($item['deleted'])
1684 return '<at:deleted-entry ref="' . xmlify($item['uri']) . '" when="' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '" />' . "\r\n";
1687 if($item['allow_cid'] || $item['allow_gid'] || $item['deny_cid'] || $item['deny_gid'])
1688 $body = fix_private_photos($item['body'],$owner['uid']);
1690 $body = $item['body'];
1693 $o = "\r\n\r\n<entry>\r\n";
1695 if(is_array($author))
1696 $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb']);
1698 $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']));
1699 if(strlen($item['owner-name']))
1700 $o .= atom_author('dfrn:owner',$item['owner-name'],$item['owner-link'],80,80,$item['owner-avatar']);
1702 if($item['parent'] != $item['id'])
1703 $o .= '<thr:in-reply-to ref="' . xmlify($item['parent-uri']) . '" type="text/html" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']) . '" />' . "\r\n";
1705 $o .= '<id>' . xmlify($item['uri']) . '</id>' . "\r\n";
1706 $o .= '<title>' . xmlify($item['title']) . '</title>' . "\r\n";
1707 $o .= '<published>' . xmlify(datetime_convert('UTC','UTC',$item['created'] . '+00:00',ATOM_TIME)) . '</published>' . "\r\n";
1708 $o .= '<updated>' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '</updated>' . "\r\n";
1709 $o .= '<dfrn:env>' . base64url_encode($body, true) . '</dfrn:env>' . "\r\n";
1710 $o .= '<content type="' . $type . '" >' . xmlify((($type === 'html') ? bbcode($body) : $body)) . '</content>' . "\r\n";
1711 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']) . '" />' . "\r\n";
1713 $o .= '<dfrn:comment-allow>' . intval($item['last-child']) . '</dfrn:comment-allow>' . "\r\n";
1715 if($item['location']) {
1716 $o .= '<dfrn:location>' . xmlify($item['location']) . '</dfrn:location>' . "\r\n";
1717 $o .= '<poco:address><poco:formatted>' . xmlify($item['location']) . '</poco:formatted></poco:address>' . "\r\n";
1721 $o .= '<georss:point>' . xmlify($item['coord']) . '</georss:point>' . "\r\n";
1723 if(($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid']))
1724 $o .= '<dfrn:private>1</dfrn:private>' . "\r\n";
1727 $o .= '<dfrn:extid>' . xmlify($item['extid']) . '</dfrn:extid>' . "\r\n";
1728 if($item['bookmark'])
1729 $o .= '<dfrn:bookmark>true</dfrn:bookmark>' . "\r\n";
1732 $o .= '<statusnet:notice_info local_id="' . $item['id'] . '" source="' . xmlify($item['app']) . '" ></statusnet:notice_info>' . "\r\n";
1735 $o .= '<dfrn:diaspora_guid>' . $item['guid'] . '</dfrn:diaspora_guid>' . "\r\n";
1737 if($item['signed_text']) {
1738 $sign = base64_encode(json_encode(array('signed_text' => $item['signed_text'],'signature' => $item['signature'],'signer' => $item['signer'])));
1739 $o .= '<dfrn:diaspora_signature>' . xmlify($sign) . '</dfrn:diaspora_signature>' . "\r\n";
1742 $verb = construct_verb($item);
1743 $o .= '<as:verb>' . xmlify($verb) . '</as:verb>' . "\r\n";
1744 $actobj = construct_activity_object($item);
1747 $actarg = construct_activity_target($item);
1751 $tags = item_getfeedtags($item);
1753 foreach($tags as $t) {
1754 $o .= '<category scheme="X-DFRN:' . xmlify($t[0]) . ':' . xmlify($t[1]) . '" term="' . xmlify($t[2]) . '" />' . "\r\n";
1758 $o .= item_getfeedattach($item);
1760 $mentioned = get_mentions($item);
1764 call_hooks('atom_entry', $o);
1766 $o .= '</entry>' . "\r\n";
1771 function fix_private_photos($s,$uid) {
1773 logger('fix_private_photos');
1775 if(preg_match("/\[img\](.*?)\[\/img\]/is",$s,$matches)) {
1776 $image = $matches[1];
1777 logger('fix_private_photos: found photo ' . $image);
1778 if(stristr($image ,$a->get_baseurl() . '/photo/')) {
1779 $i = basename($image);
1780 $i = str_replace('.jpg','',$i);
1781 $x = strpos($i,'-');
1783 $res = substr($i,$x+1);
1784 $i = substr($i,0,$x);
1785 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d AND `uid` = %d",
1791 logger('replacing photo');
1792 $s = str_replace($image, 'data:image/jpg;base64,' . base64_encode($r[0]['data']), $s);
1795 logger('fix_private_photos: replaced: ' . $s, LOGGER_DATA);
1803 function item_getfeedtags($item) {
1806 $cnt = preg_match_all('|\#\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
1808 for($x = 0; $x < count($matches); $x ++) {
1810 $ret[] = array('#',$matches[1][$x], $matches[2][$x]);
1814 $cnt = preg_match_all('|\@\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
1816 for($x = 0; $x < count($matches); $x ++) {
1818 $ret[] = array('#',$matches[1][$x], $matches[2][$x]);
1824 function item_getfeedattach($item) {
1826 $arr = explode(',',$item['attach']);
1828 foreach($arr as $r) {
1830 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"\[\/attach\]|',$r,$matches);
1832 $ret .= '<link rel="enclosure" href="' . xmlify($matches[1]) . '" type="' . xmlify($matches[3]) . '" ';
1833 if(intval($matches[2]))
1834 $ret .= 'length="' . intval($matches[2]) . '" ';
1835 if($matches[4] !== ' ')
1836 $ret .= 'title="' . xmlify(trim($matches[4])) . '" ';
1837 $ret .= ' />' . "\r\n";
1846 function item_expire($uid,$days) {
1848 if((! $uid) || (! $days))
1851 $r = q("SELECT * FROM `item`
1853 AND `created` < UTC_TIMESTAMP() - INTERVAL %d DAY
1863 logger('expire: # items=' . count($r) );
1865 foreach($r as $item) {
1867 // Only expire posts, not photos and photo comments
1869 if(strlen($item['resource-id']))
1872 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
1873 dbesc(datetime_convert()),
1874 dbesc(datetime_convert()),
1880 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
1881 dbesc(datetime_convert()),
1882 dbesc(datetime_convert()),
1883 dbesc($item['parent-uri']),
1884 intval($item['uid'])
1889 proc_run('php',"include/notifier.php","expire","$uid");
1894 function drop_items($items) {
1898 foreach($items as $item) {
1899 $owner = drop_item($item,false);
1900 if($owner && ! $uid)
1905 // multiple threads may have been deleted, send an expire notification
1908 proc_run('php',"include/notifier.php","expire","$uid");
1912 function drop_item($id,$interactive = true) {
1916 // locate item to be deleted
1918 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
1925 notice( t('Item not found.') . EOL);
1926 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
1931 $owner = $item['uid'];
1933 // check if logged in user is either the author or owner of this item
1935 if((local_user() == $item['uid']) || (remote_user() == $item['contact-id'])) {
1939 $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
1940 dbesc(datetime_convert()),
1941 dbesc(datetime_convert()),
1945 // If item is a link to a photo resource, nuke all the associated photos
1946 // (visitors will not have photo resources)
1947 // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
1948 // generate a resource-id and therefore aren't intimately linked to the item.
1950 if(strlen($item['resource-id'])) {
1951 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
1952 dbesc($item['resource-id']),
1953 intval($item['uid'])
1955 // ignore the result
1958 // If item is a link to an event, nuke the event record.
1960 if(intval($item['event-id'])) {
1961 q("DELETE FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
1962 intval($item['event-id']),
1963 intval($item['uid'])
1965 // ignore the result
1969 // If it's the parent of a comment thread, kill all the kids
1971 if($item['uri'] == $item['parent-uri']) {
1972 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = ''
1973 WHERE `parent-uri` = '%s' AND `uid` = %d ",
1974 dbesc(datetime_convert()),
1975 dbesc(datetime_convert()),
1976 dbesc($item['parent-uri']),
1977 intval($item['uid'])
1979 // ignore the result
1982 // ensure that last-child is set in case the comment that had it just got wiped.
1983 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
1984 dbesc(datetime_convert()),
1985 dbesc($item['parent-uri']),
1986 intval($item['uid'])
1988 // who is the last child now?
1989 $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",
1990 dbesc($item['parent-uri']),
1991 intval($item['uid'])
1994 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
1999 $drop_id = intval($item['id']);
2001 // send the notification upstream/downstream as the case may be
2006 proc_run('php',"include/notifier.php","drop","$drop_id");
2007 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
2013 notice( t('Permission denied.') . EOL);
2014 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);