3 require_once('bbcode.php');
4 require_once('oembed.php');
5 require_once('include/salmon.php');
7 function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
9 // default permissions - anonymous user
11 if(! strlen($owner_nick))
14 $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
16 $r = q("SELECT `contact`.*, `user`.`uid` AS `user_uid`, `user`.`nickname`, `user`.`timezone`
17 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
18 WHERE `contact`.`self` = 1 AND `user`.`nickname` = '%s' LIMIT 1",
26 $owner_id = $owner['user_uid'];
27 $owner_nick = $owner['nickname'];
29 $birthday = feed_birthday($owner_id,$owner['timezone']);
31 if(strlen($dfrn_id)) {
36 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
40 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
41 $my_id = '1:' . $dfrn_id;
44 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
45 $my_id = '0:' . $dfrn_id;
52 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
60 $groups = init_groups_visitor($contact['id']);
63 for($x = 0; $x < count($groups); $x ++)
64 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
65 $gs = implode('|', $groups);
68 $gs = '<<>>' ; // Impossible to match
70 $sql_extra = sprintf("
71 AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' )
72 AND ( `deny_cid` = '' OR NOT `deny_cid` REGEXP '<%d>' )
73 AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
74 AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s')
76 intval($contact['id']),
77 intval($contact['id']),
83 if($dfrn_id === '' || $dfrn_id === '*')
88 if(! strlen($last_update))
89 $last_update = 'now -30 days';
91 $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
93 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
94 `contact`.`name`, `contact`.`photo`, `contact`.`url`,
95 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
96 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
97 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`
98 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
99 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`parent` != 0
100 AND `item`.`wall` = 1 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
101 AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
103 ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
110 // Will check further below if this actually returned results.
111 // We will provide an empty feed if that is the case.
115 $feed_template = get_markup_template(($dfrn_id) ? 'atom_feed_dfrn.tpl' : 'atom_feed.tpl');
119 $hubxml = feed_hublinks();
121 $salmon = feed_salmonlinks($owner_nick);
123 $atom .= replace_macros($feed_template, array(
124 '$version' => xmlify(FRIENDIKA_VERSION),
125 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
126 '$feed_title' => xmlify($owner['name']),
127 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', 'now' , ATOM_TIME)) ,
129 '$salmon' => $salmon,
130 '$name' => xmlify($owner['name']),
131 '$profile_page' => xmlify($owner['url']),
132 '$photo' => xmlify($owner['photo']),
133 '$thumb' => xmlify($owner['thumb']),
134 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
135 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
136 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
137 '$birthday' => ((strlen($birthday)) ? '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>' : '')
140 call_hooks('atom_feed', $atom);
142 if(! count($items)) {
144 call_hooks('atom_feed_end', $atom);
146 $atom .= '</feed>' . "\r\n";
150 foreach($items as $item) {
152 // public feeds get html, our own nodes use bbcode
154 if($dfrn_id === '') {
156 // catch any email that's in a public conversation and make sure it doesn't leak
164 $atom .= atom_entry($item,$type,null,$owner,true);
167 call_hooks('atom_feed_end', $atom);
169 $atom .= '</feed>' . "\r\n";
175 function construct_verb($item) {
177 return $item['verb'];
178 return ACTIVITY_POST;
181 function construct_activity_object($item) {
183 if($item['object']) {
184 $o = '<as:object>' . "\r\n";
185 $r = parse_xml_string($item['object'],false);
191 $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
193 $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
195 $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
197 if(substr($r->link,0,1) === '<') {
198 // patch up some facebook "like" activity objects that got stored incorrectly
199 // for a couple of months prior to 9-Jun-2011 and generated bad XML.
200 // we can probably remove this hack here and in the following function in a few months time.
201 if(strstr($r->link,'&') && (! strstr($r->link,'&')))
202 $r->link = str_replace('&','&', $r->link);
203 $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
207 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
210 $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
211 $o .= '</as:object>' . "\r\n";
218 function construct_activity_target($item) {
220 if($item['target']) {
221 $o = '<as:target>' . "\r\n";
222 $r = parse_xml_string($item['target'],false);
226 $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
228 $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
230 $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
232 if(substr($r->link,0,1) === '<') {
233 if(strstr($r->link,'&') && (! strstr($r->link,'&')))
234 $r->link = str_replace('&','&', $r->link);
235 $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
239 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
242 $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
243 $o .= '</as:target>' . "\r\n";
253 function get_atom_elements($feed,$item) {
255 require_once('library/HTMLPurifier.auto.php');
256 require_once('include/html2bbcode.php');
258 $best_photo = array();
262 $author = $item->get_author();
264 $res['author-name'] = unxmlify($author->get_name());
265 $res['author-link'] = unxmlify($author->get_link());
268 $res['author-name'] = unxmlify($feed->get_title());
269 $res['author-link'] = unxmlify($feed->get_permalink());
271 $res['uri'] = unxmlify($item->get_id());
272 $res['title'] = unxmlify($item->get_title());
273 $res['body'] = unxmlify($item->get_content());
274 $res['plink'] = unxmlify($item->get_link(0));
276 // look for a photo. We should check media size and find the best one,
277 // but for now let's just find any author photo
279 $rawauthor = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
281 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
282 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
283 foreach($base as $link) {
284 if(! $res['author-avatar']) {
285 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
286 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
291 $rawactor = $item->get_item_tags(NAMESPACE_ACTIVITY, 'actor');
293 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
294 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
295 if($base && count($base)) {
296 foreach($base as $link) {
297 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
298 $res['author-link'] = unxmlify($link['attribs']['']['href']);
299 if(! $res['author-avatar']) {
300 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
301 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
307 // No photo/profile-link on the item - look at the feed level
309 if((! (x($res,'author-link'))) || (! (x($res,'author-avatar')))) {
310 $rawauthor = $feed->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
311 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
312 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
313 foreach($base as $link) {
314 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
315 $res['author-link'] = unxmlify($link['attribs']['']['href']);
316 if(! $res['author-avatar']) {
317 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
318 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
323 $rawactor = $feed->get_feed_tags(NAMESPACE_ACTIVITY, 'subject');
325 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
326 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
328 if($base && count($base)) {
329 foreach($base as $link) {
330 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
331 $res['author-link'] = unxmlify($link['attribs']['']['href']);
332 if(! (x($res,'author-avatar'))) {
333 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
334 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
341 $apps = $item->get_item_tags(NAMESPACE_STATUSNET,'notice_info');
342 if($apps && $apps[0]['attribs']['']['source']) {
343 $res['app'] = strip_tags(unxmlify($apps[0]['attribs']['']['source']));
344 if($res['app'] === 'web')
345 $res['app'] = 'OStatus';
349 * If there's a copy of the body content which is guaranteed to have survived mangling in transit, use it.
352 $have_real_body = false;
354 $rawenv = $item->get_item_tags(NAMESPACE_DFRN, 'env');
356 $have_real_body = true;
357 $res['body'] = $rawenv[0]['data'];
358 $res['body'] = str_replace(array(' ',"\t","\r","\n"), array('','','',''),$res['body']);
359 // make sure nobody is trying to sneak some html tags by us
360 $res['body'] = notags(base64url_decode($res['body']));
363 $maxlen = get_max_import_size();
364 if($maxlen && (strlen($res['body']) > $maxlen))
365 $res['body'] = substr($res['body'],0, $maxlen);
367 // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust
368 // the content type. Our own network only emits text normally, though it might have been converted to
369 // html if we used a pubsubhubbub transport. But if we see even one html tag in our text, we will
370 // have to assume it is all html and needs to be purified.
372 // It doesn't matter all that much security wise - because before this content is used anywhere, we are
373 // going to escape any tags we find regardless, but this lets us import a limited subset of html from
374 // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining
377 if((strpos($res['body'],'<') !== false) || (strpos($res['body'],'>') !== false)) {
379 $res['body'] = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
380 '[youtube]$1[/youtube]', $res['body']);
382 $res['body'] = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
383 '[youtube]$1[/youtube]', $res['body']);
385 $res['body'] = oembed_html2bbcode($res['body']);
387 $config = HTMLPurifier_Config::createDefault();
388 $config->set('Cache.DefinitionImpl', null);
390 // we shouldn't need a whitelist, because the bbcode converter
391 // will strip out any unsupported tags.
392 // $config->set('HTML.Allowed', 'p,b,a[href],i');
394 $purifier = new HTMLPurifier($config);
395 $res['body'] = $purifier->purify($res['body']);
397 $res['body'] = html2bbcode($res['body']);
400 $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
401 if($allow && $allow[0]['data'] == 1)
402 $res['last-child'] = 1;
404 $res['last-child'] = 0;
406 $private = $item->get_item_tags(NAMESPACE_DFRN,'private');
407 if($private && $private[0]['data'] == 1)
412 $extid = $item->get_item_tags(NAMESPACE_DFRN,'extid');
413 if($extid && $extid[0]['data'])
414 $res['extid'] = $extid[0]['data'];
416 $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
418 $res['location'] = unxmlify($rawlocation[0]['data']);
421 $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
423 $res['created'] = unxmlify($rawcreated[0]['data']);
426 $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
428 $res['edited'] = unxmlify($rawedited[0]['data']);
430 if((x($res,'edited')) && (! (x($res,'created'))))
431 $res['created'] = $res['edited'];
433 if(! $res['created'])
434 $res['created'] = $item->get_date('c');
437 $res['edited'] = $item->get_date('c');
440 // Disallow time travelling posts
442 $d1 = strtotime($res['created']);
443 $d2 = strtotime($res['edited']);
444 $d3 = strtotime('now');
447 $res['created'] = datetime_convert();
449 $res['edited'] = datetime_convert();
451 $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
452 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
453 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
454 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
455 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
456 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
457 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
458 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
459 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
461 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
462 $base = $rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
464 foreach($base as $link) {
465 if(! $res['owner-avatar']) {
466 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
467 $res['owner-avatar'] = unxmlify($link['attribs']['']['href']);
472 $rawgeo = $item->get_item_tags(NAMESPACE_GEORSS,'point');
474 $res['coord'] = unxmlify($rawgeo[0]['data']);
477 $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
479 // select between supported verbs
482 $res['verb'] = unxmlify($rawverb[0]['data']);
485 // translate OStatus unfollow to activity streams if it happened to get selected
487 if((x($res,'verb')) && ($res['verb'] === 'http://ostatus.org/schema/1.0/unfollow'))
488 $res['verb'] = ACTIVITY_UNFOLLOW;
490 $cats = $item->get_categories();
493 foreach($cats as $cat) {
494 $term = $cat->get_term();
496 $term = $cat->get_label();
497 $scheme = $cat->get_scheme();
498 if($scheme && $term && stristr($scheme,'X-DFRN:'))
499 $tag_arr[] = substr($scheme,7,1) . '[url=' . unxmlify(substr($scheme,9)) . ']' . unxmlify($term) . '[/url]';
501 $tag_arr[] = notags(trim($term));
503 $res['tag'] = implode(',', $tag_arr);
506 $attach = $item->get_enclosures();
509 foreach($attach as $att) {
510 $len = intval($att->get_length());
511 $link = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_link()))));
512 $title = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_title()))));
513 $type = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_type()))));
514 if(strpos($type,';'))
515 $type = substr($type,0,strpos($type,';'));
516 if((! $link) || (strpos($link,'http') !== 0))
522 $type = 'application/octet-stream';
524 $att_arr[] = '[attach]href="' . $link . '" length="' . $len . '" type="' . $type . '" title="' . $title . '"[/attach]';
526 $res['attach'] = implode(',', $att_arr);
529 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
532 $res['object'] = '<object>' . "\n";
533 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
534 $res['object-type'] = $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'];
535 $res['object'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
537 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
538 $res['object'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
539 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
540 $res['object'] .= '<link>' . encode_rel_links($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
541 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
542 $res['object'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
543 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
544 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
546 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
547 // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
548 $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
549 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
551 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
552 '[youtube]$1[/youtube]', $body);
554 $res['body'] = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
555 '[youtube]$1[/youtube]', $res['body']);
558 $config = HTMLPurifier_Config::createDefault();
559 $config->set('Cache.DefinitionImpl', null);
561 $purifier = new HTMLPurifier($config);
562 $body = $purifier->purify($body);
563 $body = html2bbcode($body);
566 $res['object'] .= '<content>' . $body . '</content>' . "\n";
569 $res['object'] .= '</object>' . "\n";
572 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'target');
575 $res['target'] = '<target>' . "\n";
576 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
577 $res['target'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
579 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
580 $res['target'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
582 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
583 $res['target'] .= '<link>' . encode_rel_links($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
584 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
585 $res['target'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
586 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
587 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
589 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
590 // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
591 $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
592 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
594 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
595 '[youtube]$1[/youtube]', $body);
597 $res['body'] = preg_replace('#<iframe[^>].+?' . 'http://www.youtube.com/embed/([A-Za-z0-9\-_=]+).+?</iframe>#s',
598 '[youtube]$1[/youtube]', $res['body']);
600 $config = HTMLPurifier_Config::createDefault();
601 $config->set('Cache.DefinitionImpl', null);
603 $purifier = new HTMLPurifier($config);
604 $body = $purifier->purify($body);
605 $body = html2bbcode($body);
608 $res['target'] .= '<content>' . $body . '</content>' . "\n";
611 $res['target'] .= '</target>' . "\n";
614 $arr = array('feed' => $feed, 'item' => $item, 'result' => $res);
616 call_hooks('parse_atom', $arr);
621 function encode_rel_links($links) {
623 if(! ((is_array($links)) && (count($links))))
625 foreach($links as $link) {
627 if($link['attribs']['']['rel'])
628 $o .= 'rel="' . $link['attribs']['']['rel'] . '" ';
629 if($link['attribs']['']['type'])
630 $o .= 'type="' . $link['attribs']['']['type'] . '" ';
631 if($link['attribs']['']['href'])
632 $o .= 'href="' . $link['attribs']['']['href'] . '" ';
633 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['width'])
634 $o .= 'media:width="' . $link['attribs'][NAMESPACE_MEDIA]['width'] . '" ';
635 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['height'])
636 $o .= 'media:height="' . $link['attribs'][NAMESPACE_MEDIA]['height'] . '" ';
642 function item_store($arr,$force_parent = false) {
645 $arr['gravity'] = intval($arr['gravity']);
646 elseif($arr['parent-uri'] == $arr['uri'])
648 elseif(activity_match($arr['verb'],ACTIVITY_POST))
651 $arr['gravity'] = 6; // extensible catchall
654 $arr['type'] = 'remote';
656 // Shouldn't happen but we want to make absolutely sure it doesn't leak from a plugin.
658 if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false))
659 $arr['body'] = strip_tags($arr['body']);
662 $arr['wall'] = ((x($arr,'wall')) ? intval($arr['wall']) : 0);
663 $arr['uri'] = ((x($arr,'uri')) ? notags(trim($arr['uri'])) : random_string());
664 $arr['extid'] = ((x($arr,'extid')) ? notags(trim($arr['extid'])) : '');
665 $arr['author-name'] = ((x($arr,'author-name')) ? notags(trim($arr['author-name'])) : '');
666 $arr['author-link'] = ((x($arr,'author-link')) ? notags(trim($arr['author-link'])) : '');
667 $arr['author-avatar'] = ((x($arr,'author-avatar')) ? notags(trim($arr['author-avatar'])) : '');
668 $arr['owner-name'] = ((x($arr,'owner-name')) ? notags(trim($arr['owner-name'])) : '');
669 $arr['owner-link'] = ((x($arr,'owner-link')) ? notags(trim($arr['owner-link'])) : '');
670 $arr['owner-avatar'] = ((x($arr,'owner-avatar')) ? notags(trim($arr['owner-avatar'])) : '');
671 $arr['created'] = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
672 $arr['edited'] = ((x($arr,'edited') !== false) ? datetime_convert('UTC','UTC',$arr['edited']) : datetime_convert());
673 $arr['received'] = datetime_convert();
674 $arr['changed'] = datetime_convert();
675 $arr['title'] = ((x($arr,'title')) ? notags(trim($arr['title'])) : '');
676 $arr['location'] = ((x($arr,'location')) ? notags(trim($arr['location'])) : '');
677 $arr['coord'] = ((x($arr,'coord')) ? notags(trim($arr['coord'])) : '');
678 $arr['last-child'] = ((x($arr,'last-child')) ? intval($arr['last-child']) : 0 );
679 $arr['visible'] = ((x($arr,'visible') !== false) ? intval($arr['visible']) : 1 );
681 $arr['parent-uri'] = ((x($arr,'parent-uri')) ? notags(trim($arr['parent-uri'])) : '');
682 $arr['verb'] = ((x($arr,'verb')) ? notags(trim($arr['verb'])) : '');
683 $arr['object-type'] = ((x($arr,'object-type')) ? notags(trim($arr['object-type'])) : '');
684 $arr['object'] = ((x($arr,'object')) ? trim($arr['object']) : '');
685 $arr['target-type'] = ((x($arr,'target-type')) ? notags(trim($arr['target-type'])) : '');
686 $arr['target'] = ((x($arr,'target')) ? trim($arr['target']) : '');
687 $arr['plink'] = ((x($arr,'plink')) ? notags(trim($arr['plink'])) : '');
688 $arr['allow_cid'] = ((x($arr,'allow_cid')) ? trim($arr['allow_cid']) : '');
689 $arr['allow_gid'] = ((x($arr,'allow_gid')) ? trim($arr['allow_gid']) : '');
690 $arr['deny_cid'] = ((x($arr,'deny_cid')) ? trim($arr['deny_cid']) : '');
691 $arr['deny_gid'] = ((x($arr,'deny_gid')) ? trim($arr['deny_gid']) : '');
692 $arr['private'] = ((x($arr,'private')) ? intval($arr['private']) : 0 );
693 $arr['body'] = ((x($arr,'body')) ? trim($arr['body']) : '');
694 $arr['tag'] = ((x($arr,'tag')) ? notags(trim($arr['tag'])) : '');
695 $arr['attach'] = ((x($arr,'attach')) ? notags(trim($arr['attach'])) : '');
696 $arr['app'] = ((x($arr,'app')) ? notags(trim($arr['app'])) : '');
698 if($arr['parent-uri'] === $arr['uri']) {
700 $allow_cid = $arr['allow_cid'];
701 $allow_gid = $arr['allow_gid'];
702 $deny_cid = $arr['deny_cid'];
703 $deny_gid = $arr['deny_gid'];
707 // find the parent and snarf the item id and ACL's
708 // and anything else we need to inherit
710 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
711 dbesc($arr['parent-uri']),
717 // is the new message multi-level threaded?
718 // even though we don't support it now, preserve the info
719 // and re-attach to the conversation parent.
721 if($r[0]['uri'] != $r[0]['parent-uri']) {
722 $arr['thr-parent'] = $arr['parent-uri'];
723 $arr['parent-uri'] = $r[0]['parent-uri'];
724 $z = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `parent-uri` = '%s' AND `uid` = %d LIMIT 1",
725 dbesc($r[0]['parent-uri']),
726 dbesc($r[0]['parent-uri']),
733 $parent_id = $r[0]['id'];
734 $parent_deleted = $r[0]['deleted'];
735 $allow_cid = $r[0]['allow_cid'];
736 $allow_gid = $r[0]['allow_gid'];
737 $deny_cid = $r[0]['deny_cid'];
738 $deny_gid = $r[0]['deny_gid'];
739 $arr['wall'] = $r[0]['wall'];
743 // Allow one to see reply tweets from status.net even when
744 // we don't have or can't see the original post.
747 logger('item_store: $force_parent=true, reply converted to top-level post.');
749 $arr['thr-parent'] = $arr['parent-uri'];
750 $arr['parent-uri'] = $arr['uri'];
754 logger('item_store: item parent was not found - ignoring item');
760 $arr['guid'] = get_guid();
762 call_hooks('post_remote',$arr);
766 logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
768 $r = dbq("INSERT INTO `item` (`"
769 . implode("`, `", array_keys($arr))
771 . implode("', '", array_values($arr))
774 // find the item we just created
776 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
777 $arr['uri'], // already dbesc'd
781 // This is not good, but perhaps we encountered a rare race/cache condition, so back off and try again.
783 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
784 $arr['uri'], // already dbesc'd
790 $current_post = $r[0]['id'];
791 logger('item_store: created item ' . $current_post);
794 logger('item_store: could not locate created item');
798 if((! $parent_id) || ($arr['parent-uri'] === $arr['uri']))
799 $parent_id = $current_post;
801 if(strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid))
804 $private = $arr['private'];
806 // Set parent id - and also make sure to inherit the parent's ACL's.
808 $r = q("UPDATE `item` SET `parent` = %d, `allow_cid` = '%s', `allow_gid` = '%s',
809 `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d, `deleted` = %d WHERE `id` = %d LIMIT 1",
816 intval($parent_deleted),
817 intval($current_post)
821 * If this is now the last-child, force all _other_ children of this parent to *not* be last-child
824 if($arr['last-child']) {
825 $r = q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d AND `id` != %d",
828 intval($current_post)
832 return $current_post;
835 function get_item_contact($item,$contacts) {
836 if(! count($contacts) || (! is_array($item)))
838 foreach($contacts as $contact) {
839 if($contact['id'] == $item['contact-id']) {
848 function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
852 if((! strlen($contact['issued-id'])) && (! $contact['duplex']) && (! ($owner['page-flags'] == PAGE_COMMUNITY)))
855 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
857 if($contact['duplex'] && $contact['dfrn-id'])
858 $idtosend = '0:' . $orig_id;
859 if($contact['duplex'] && $contact['issued-id'])
860 $idtosend = '1:' . $orig_id;
862 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
864 $rino_enable = get_config('system','rino_encrypt');
869 $url = $contact['notify'] . '&dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino=1' : '');
871 logger('dfrn_deliver: ' . $url);
873 $xml = fetch_url($url);
875 $curl_stat = $a->get_curl_code();
877 return(-1); // timed out
879 logger('dfrn_deliver: ' . $xml);
884 if(strpos($xml,'<?xml') === false) {
885 logger('dfrn_deliver: no valid XML returned');
886 logger('dfrn_deliver: returned XML: ' . $xml, LOGGER_DATA);
890 $res = parse_xml_string($xml);
892 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
893 return (($res->status) ? $res->status : 3);
896 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
897 $challenge = hex2bin((string) $res->challenge);
898 $dfrn_version = (float) (($res->dfrn_version) ? $res->dfrn_version : 2.0);
899 $rino_allowed = ((intval($res->rino) === 1) ? 1 : 0);
904 if(($contact['duplex'] && strlen($contact['pubkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
905 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
906 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
909 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
910 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
913 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
915 if(strpos($final_dfrn_id,':') == 1)
916 $final_dfrn_id = substr($final_dfrn_id,2);
918 if($final_dfrn_id != $orig_id) {
919 logger('dfrn_deliver: wrong dfrn_id.');
920 // did not decode properly - cannot trust this site
924 $postvars['dfrn_id'] = $idtosend;
925 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
927 $postvars['dissolve'] = '1';
930 if((($contact['rel']) && ($contact['rel'] != CONTACT_IS_SHARING) && (! $contact['blocked'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
931 $postvars['data'] = $atom;
932 $postvars['perm'] = 'rw';
935 $postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
936 $postvars['perm'] = 'r';
939 if($rino && $rino_allowed && (! $dissolve)) {
940 $key = substr(random_string(),0,16);
941 $data = bin2hex(aes_encrypt($postvars['data'],$key));
942 $postvars['data'] = $data;
943 logger('rino: sent key = ' . $key);
946 if($dfrn_version >= 2.1) {
947 if(($contact['duplex'] && strlen($contact['pubkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
948 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
951 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
955 if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
956 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
959 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
963 logger('md5 rawkey ' . md5($postvars['key']));
965 $postvars['key'] = bin2hex($postvars['key']);
968 logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);
970 $xml = post_url($contact['notify'],$postvars);
972 logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
974 $curl_stat = $a->get_curl_code();
975 if((! $curl_stat) || (! strlen($xml)))
976 return(-1); // timed out
978 if(strpos($xml,'<?xml') === false) {
979 logger('dfrn_deliver: phase 2: no valid XML returned');
980 logger('dfrn_deliver: phase 2: returned XML: ' . $xml, LOGGER_DATA);
984 $res = parse_xml_string($xml);
992 * consume_feed - process atom feed and update anything/everything we might need to update
994 * $xml = the (atom) feed to consume - RSS isn't as fully supported but may work for simple feeds.
996 * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
997 * It is this person's stuff that is going to be updated.
998 * $contact = the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
999 * from an external network and MAY create an appropriate contact record. Otherwise, we MUST
1000 * have a contact record.
1001 * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or
1002 * might not) try and subscribe to it.
1006 function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $secure_feed = false) {
1008 require_once('library/simplepie/simplepie.inc');
1010 if(! strlen($xml)) {
1011 logger('consume_feed: empty input');
1015 $feed = new SimplePie();
1016 $feed->set_raw_data($xml);
1018 $feed->enable_order_by_date(true);
1020 $feed->enable_order_by_date(false);
1024 logger('consume_feed: Error parsing XML: ' . $feed->error());
1026 $permalink = $feed->get_permalink();
1028 // Check at the feed level for updated contact name and/or photo
1032 $photo_timestamp = '';
1036 $hubs = $feed->get_links('hub');
1039 $hub = implode(',', $hubs);
1041 $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'owner');
1043 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
1045 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
1046 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
1047 $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
1048 $new_name = $elems['name'][0]['data'];
1050 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
1051 $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
1052 $photo_url = $elems['link'][0]['attribs']['']['href'];
1055 if((x($rawtags[0]['child'], NAMESPACE_DFRN)) && (x($rawtags[0]['child'][NAMESPACE_DFRN],'birthday'))) {
1056 $birthday = datetime_convert('UTC','UTC', $rawtags[0]['child'][NAMESPACE_DFRN]['birthday'][0]['data']);
1060 if((is_array($contact)) && ($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
1061 logger('consume_feed: Updating photo for ' . $contact['name']);
1062 require_once("Photo.php");
1063 $photo_failure = false;
1064 $have_photo = false;
1066 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
1067 intval($contact['id']),
1068 intval($contact['uid'])
1071 $resource_id = $r[0]['resource-id'];
1075 $resource_id = photo_new_resource();
1078 $img_str = fetch_url($photo_url,true);
1079 $img = new Photo($img_str);
1080 if($img->is_valid()) {
1082 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
1083 dbesc($resource_id),
1084 intval($contact['id']),
1085 intval($contact['uid'])
1089 $img->scaleImageSquare(175);
1091 $hash = $resource_id;
1092 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 4);
1094 $img->scaleImage(80);
1095 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 5);
1097 $img->scaleImage(48);
1098 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 6);
1102 q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
1103 WHERE `uid` = %d AND `id` = %d LIMIT 1",
1104 dbesc(datetime_convert()),
1105 dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.jpg'),
1106 dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.jpg'),
1107 dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.jpg'),
1108 intval($contact['uid']),
1109 intval($contact['id'])
1114 if((is_array($contact)) && ($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
1115 q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1116 dbesc(notags(trim($new_name))),
1117 dbesc(datetime_convert()),
1118 intval($contact['uid']),
1119 intval($contact['id'])
1123 if(strlen($birthday)) {
1124 if(substr($birthday,0,4) != $contact['bdyear']) {
1125 logger('consume_feed: updating birthday: ' . $birthday);
1129 * Add new birthday event for this person
1131 * $bdtext is just a readable placeholder in case the event is shared
1132 * with others. We will replace it during presentation to our $importer
1133 * to contain a sparkle link and perhaps a photo.
1137 $bdtext = t('Birthday:') . ' [url=' . $contact['url'] . ']' . $contact['name'] . '[/url]' ;
1140 $r = q("INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`desc`,`type`)
1141 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s' ) ",
1142 intval($contact['uid']),
1143 intval($contact['id']),
1144 dbesc(datetime_convert()),
1145 dbesc(datetime_convert()),
1146 dbesc(datetime_convert('UTC','UTC', $birthday)),
1147 dbesc(datetime_convert('UTC','UTC', $birthday . ' + 1 day ')),
1155 q("UPDATE `contact` SET `bdyear` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1156 dbesc(substr($birthday,0,4)),
1157 intval($contact['uid']),
1158 intval($contact['id'])
1161 // This function is called twice without reloading the contact
1162 // Make sure we only create one event. This is why &$contact
1163 // is a reference var in this function
1165 $contact['bdyear'] = substr($birthday,0,4);
1171 // process any deleted entries
1173 $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
1174 if(is_array($del_entries) && count($del_entries)) {
1175 foreach($del_entries as $dentry) {
1177 if(isset($dentry['attribs']['']['ref'])) {
1178 $uri = $dentry['attribs']['']['ref'];
1180 if(isset($dentry['attribs']['']['when'])) {
1181 $when = $dentry['attribs']['']['when'];
1182 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
1185 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
1187 if($deleted && is_array($contact)) {
1188 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `contact-id` = %d LIMIT 1",
1190 intval($importer['uid']),
1191 intval($contact['id'])
1196 if(! $item['deleted'])
1197 logger('consume_feed: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
1199 if($item['uri'] == $item['parent-uri']) {
1200 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1201 `body` = '', `title` = ''
1202 WHERE `parent-uri` = '%s' AND `uid` = %d",
1204 dbesc(datetime_convert()),
1205 dbesc($item['uri']),
1206 intval($importer['uid'])
1210 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1211 `body` = '', `title` = ''
1212 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1214 dbesc(datetime_convert()),
1216 intval($importer['uid'])
1218 if($item['last-child']) {
1219 // ensure that last-child is set in case the comment that had it just got wiped.
1220 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
1221 dbesc(datetime_convert()),
1222 dbesc($item['parent-uri']),
1223 intval($item['uid'])
1225 // who is the last child now?
1226 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
1227 ORDER BY `created` DESC LIMIT 1",
1228 dbesc($item['parent-uri']),
1229 intval($importer['uid'])
1232 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
1243 // Now process the feed
1245 if($feed->get_item_quantity()) {
1247 logger('consume_feed: feed item count = ' . $feed->get_item_quantity());
1249 // in inverse date order
1251 $items = array_reverse($feed->get_items());
1253 $items = $feed->get_items();
1256 foreach($items as $item) {
1259 $item_id = $item->get_id();
1260 $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
1261 if(isset($rawthread[0]['attribs']['']['ref'])) {
1263 $parent_uri = $rawthread[0]['attribs']['']['ref'];
1266 if(($is_reply) && is_array($contact)) {
1268 // Have we seen it? If not, import it.
1270 $item_id = $item->get_id();
1271 $datarray = get_atom_elements($feed,$item);
1273 if(! x($datarray,'author-name'))
1274 $datarray['author-name'] = $contact['name'];
1275 if(! x($datarray,'author-link'))
1276 $datarray['author-link'] = $contact['url'];
1277 if(! x($datarray,'author-avatar'))
1278 $datarray['author-avatar'] = $contact['thumb'];
1281 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1283 intval($importer['uid'])
1286 // Update content if 'updated' changes
1289 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
1290 $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1291 dbesc($datarray['body']),
1292 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
1294 intval($importer['uid'])
1298 // update last-child if it changes
1300 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
1301 if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
1302 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1303 dbesc(datetime_convert()),
1305 intval($importer['uid'])
1307 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1308 intval($allow[0]['data']),
1309 dbesc(datetime_convert()),
1311 intval($importer['uid'])
1317 $force_parent = false;
1318 if($contact['network'] === 'stat') {
1319 $force_parent = true;
1320 if(strlen($datarray['title']))
1321 unset($datarray['title']);
1322 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1323 dbesc(datetime_convert()),
1325 intval($importer['uid'])
1327 $datarray['last-child'] = 1;
1330 if(($contact['network'] === 'feed') || (! strlen($contact['notify']))) {
1331 // one way feed - no remote comment ability
1332 $datarray['last-child'] = 0;
1334 $datarray['parent-uri'] = $parent_uri;
1335 $datarray['uid'] = $importer['uid'];
1336 $datarray['contact-id'] = $contact['id'];
1337 if((activity_match($datarray['verb'],ACTIVITY_LIKE)) || (activity_match($datarray['verb'],ACTIVITY_DISLIKE))) {
1338 $datarray['type'] = 'activity';
1339 $datarray['gravity'] = GRAVITY_LIKE;
1342 $r = item_store($datarray,$force_parent);
1348 // Head post of a conversation. Have we seen it? If not, import it.
1350 $item_id = $item->get_id();
1352 $datarray = get_atom_elements($feed,$item);
1354 if(is_array($contact)) {
1355 if(! x($datarray,'author-name'))
1356 $datarray['author-name'] = $contact['name'];
1357 if(! x($datarray,'author-link'))
1358 $datarray['author-link'] = $contact['url'];
1359 if(! x($datarray,'author-avatar'))
1360 $datarray['author-avatar'] = $contact['thumb'];
1363 if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
1364 $ev = bbtoevent($datarray['body']);
1365 if(x($ev,'desc') && x($ev,'start')) {
1366 $ev['uid'] = $importer['uid'];
1367 $ev['uri'] = $item_id;
1368 $ev['edited'] = $datarray['edited'];
1369 $ev['private'] = $datarray['private'];
1371 if(is_array($contact))
1372 $ev['cid'] = $contact['id'];
1373 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1375 intval($importer['uid'])
1378 $ev['id'] = $r[0]['id'];
1379 $xyz = event_store($ev);
1384 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1386 intval($importer['uid'])
1389 // Update content if 'updated' changes
1392 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
1393 $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1394 dbesc($datarray['body']),
1395 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
1397 intval($importer['uid'])
1401 // update last-child if it changes
1403 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
1404 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
1405 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1406 intval($allow[0]['data']),
1407 dbesc(datetime_convert()),
1409 intval($importer['uid'])
1415 if(activity_match($datarray['verb'],ACTIVITY_FOLLOW)) {
1416 logger('consume-feed: New follower');
1417 new_follower($importer,$contact,$datarray,$item);
1420 if(activity_match($datarray['verb'],ACTIVITY_UNFOLLOW)) {
1421 lose_follower($importer,$contact,$datarray,$item);
1424 if(! is_array($contact))
1427 if($contact['network'] === 'stat' || stristr($permalink,'twitter.com')) {
1428 if(strlen($datarray['title']))
1429 unset($datarray['title']);
1430 $datarray['last-child'] = 1;
1433 if(($contact['network'] === 'feed') || (! strlen($contact['notify']))) {
1434 // one way feed - no remote comment ability
1435 $datarray['last-child'] = 0;
1438 // This is my contact on another system, but it's really me.
1439 // Turn this into a wall post.
1441 if($contact['remote_self'])
1442 $datarray['wall'] = 1;
1444 $datarray['parent-uri'] = $item_id;
1445 $datarray['uid'] = $importer['uid'];
1446 $datarray['contact-id'] = $contact['id'];
1447 $r = item_store($datarray);
1455 function new_follower($importer,$contact,$datarray,$item) {
1456 $url = notags(trim($datarray['author-link']));
1457 $name = notags(trim($datarray['author-name']));
1458 $photo = notags(trim($datarray['author-avatar']));
1460 $rawtag = $item->get_item_tags(NAMESPACE_ACTIVITY,'actor');
1461 if($rawtag && $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'])
1462 $nick = $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'];
1464 if(is_array($contact)) {
1465 if($contact['network'] == 'stat' && $contact['rel'] == CONTACT_IS_SHARING) {
1466 $r = q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
1467 intval(CONTACT_IS_FRIEND),
1468 intval($contact['id']),
1469 intval($importer['uid'])
1473 // send email notification to owner?
1477 // create contact record
1479 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `name`, `nick`, `photo`, `network`, `rel`,
1480 `blocked`, `readonly`, `pending`, `writable` )
1481 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 1, 1 ) ",
1482 intval($importer['uid']),
1483 dbesc(datetime_convert()),
1489 intval(CONTACT_IS_FOLLOWER)
1491 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `pending` = 1 AND `rel` = %d LIMIT 1",
1492 intval($importer['uid']),
1494 intval(CONTACT_IS_FOLLOWER)
1497 $contact_record = $r[0];
1499 // create notification
1500 $hash = random_string();
1502 if(is_array($contact_record)) {
1503 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `hash`, `datetime`)
1504 VALUES ( %d, %d, 0, 0, '%s', '%s' )",
1505 intval($importer['uid']),
1506 intval($contact_record['id']),
1508 dbesc(datetime_convert())
1511 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
1512 intval($importer['uid'])
1516 if(($r[0]['notify-flags'] & NOTIFY_INTRO) && ($r[0]['page-flags'] == PAGE_NORMAL)) {
1517 $email_tpl = get_intltext_template('follow_notify_eml.tpl');
1518 $email = replace_macros($email_tpl, array(
1519 '$requestor' => ((strlen($name)) ? $name : t('[Name Withheld]')),
1521 '$myname' => $r[0]['username'],
1522 '$siteurl' => $a->get_baseurl(),
1523 '$sitename' => $a->config['sitename']
1525 $res = mail($r[0]['email'],
1526 t("You have a new follower at ") . $a->config['sitename'],
1528 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] . "\n"
1529 . 'Content-type: text/plain; charset=UTF-8' . "\n"
1530 . 'Content-transfer-encoding: 8bit' );
1537 function lose_follower($importer,$contact,$datarray,$item) {
1539 if(($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_SHARING)) {
1540 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
1541 intval(CONTACT_IS_SHARING),
1542 intval($contact['id'])
1546 contact_remove($contact['id']);
1551 function subscribe_to_hub($url,$importer,$contact) {
1553 if(is_array($importer)) {
1554 $r = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
1555 intval($importer['uid'])
1561 $push_url = get_config('system','url') . '/pubsub/' . $r[0]['nickname'] . '/' . $contact['id'];
1563 // Use a single verify token, even if multiple hubs
1565 $verify_token = ((strlen($contact['hub-verify'])) ? $contact['hub-verify'] : random_string());
1567 $params= 'hub.mode=subscribe&hub.callback=' . urlencode($push_url) . '&hub.topic=' . urlencode($contact['poll']) . '&hub.verify=async&hub.verify_token=' . $verify_token;
1569 logger('subscribe_to_hub: subscribing ' . $contact['name'] . ' to hub ' . $url . ' with verifier ' . $verify_token);
1571 if(! strlen($contact['hub-verify'])) {
1572 $r = q("UPDATE `contact` SET `hub-verify` = '%s' WHERE `id` = %d LIMIT 1",
1573 dbesc($verify_token),
1574 intval($contact['id'])
1578 post_url($url,$params);
1584 function atom_author($tag,$name,$uri,$h,$w,$photo) {
1588 $name = xmlify($name);
1589 $uri = xmlify($uri);
1592 $photo = xmlify($photo);
1596 $o .= "<name>$name</name>\r\n";
1597 $o .= "<uri>$uri</uri>\r\n";
1598 $o .= '<link rel="photo" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
1599 $o .= '<link rel="avatar" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
1601 call_hooks('atom_author', $o);
1603 $o .= "</$tag>\r\n";
1607 function atom_entry($item,$type,$author,$owner,$comment = false) {
1611 if($item['deleted'])
1612 return '<at:deleted-entry ref="' . xmlify($item['uri']) . '" when="' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '" />' . "\r\n";
1615 if($item['allow_cid'] || $item['allow_gid'] || $item['deny_cid'] || $item['deny_gid'])
1616 $body = fix_private_photos($item['body'],$owner['uid']);
1618 $body = $item['body'];
1621 $o = "\r\n\r\n<entry>\r\n";
1623 if(is_array($author))
1624 $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb']);
1626 $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']));
1627 if(strlen($item['owner-name']))
1628 $o .= atom_author('dfrn:owner',$item['owner-name'],$item['owner-link'],80,80,$item['owner-avatar']);
1630 if($item['parent'] != $item['id'])
1631 $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";
1633 $o .= '<id>' . xmlify($item['uri']) . '</id>' . "\r\n";
1634 $o .= '<title>' . xmlify($item['title']) . '</title>' . "\r\n";
1635 $o .= '<published>' . xmlify(datetime_convert('UTC','UTC',$item['created'] . '+00:00',ATOM_TIME)) . '</published>' . "\r\n";
1636 $o .= '<updated>' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '</updated>' . "\r\n";
1637 $o .= '<dfrn:env>' . base64url_encode($body, true) . '</dfrn:env>' . "\r\n";
1638 $o .= '<content type="' . $type . '" >' . xmlify((($type === 'html') ? bbcode($body) : $body)) . '</content>' . "\r\n";
1639 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']) . '" />' . "\r\n";
1641 $o .= '<dfrn:comment-allow>' . intval($item['last-child']) . '</dfrn:comment-allow>' . "\r\n";
1643 if($item['location']) {
1644 $o .= '<dfrn:location>' . xmlify($item['location']) . '</dfrn:location>' . "\r\n";
1645 $o .= '<poco:address><poco:formatted>' . xmlify($item['location']) . '</poco:formatted></poco:address>' . "\r\n";
1649 $o .= '<georss:point>' . xmlify($item['coord']) . '</georss:point>' . "\r\n";
1651 if(($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid']))
1652 $o .= '<dfrn:private>1</dfrn:private>' . "\r\n";
1655 $o .= '<dfrn:extid>' . $item['extid'] . '</dfrn:extid>' . "\r\n";
1658 $o .= '<statusnet:notice_info local_id="' . $item['id'] . '" source="' . $item['app'] . '" ></statusnet:notice_info>';
1659 $verb = construct_verb($item);
1660 $o .= '<as:verb>' . xmlify($verb) . '</as:verb>' . "\r\n";
1661 $actobj = construct_activity_object($item);
1664 $actarg = construct_activity_target($item);
1668 $tags = item_getfeedtags($item);
1670 foreach($tags as $t) {
1671 $o .= '<category scheme="X-DFRN:' . xmlify($t[0]) . ':' . xmlify($t[1]) . '" term="' . xmlify($t[2]) . '" />' . "\r\n";
1675 $o .= item_getfeedattach($item);
1677 $mentioned = get_mentions($item);
1681 call_hooks('atom_entry', $o);
1683 $o .= '</entry>' . "\r\n";
1688 function fix_private_photos($s,$uid) {
1690 logger('fix_private_photos');
1692 if(preg_match("/\[img\](.*?)\[\/img\]/is",$s,$matches)) {
1693 $image = $matches[1];
1694 logger('fix_private_photos: found photo ' . $image);
1695 if(stristr($image ,$a->get_baseurl() . '/photo/')) {
1696 $i = basename($image);
1697 $i = str_replace('.jpg','',$i);
1698 $x = strpos($i,'-');
1700 $res = substr($i,$x+1);
1701 $i = substr($i,0,$x);
1702 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d AND `uid` = %d",
1708 logger('replacing photo');
1709 $s = str_replace($image, 'data:image/jpg;base64,' . base64_encode($r[0]['data']), $s);
1712 logger('fix_private_photos: replaced: ' . $s, LOGGER_DATA);
1720 function item_getfeedtags($item) {
1723 $cnt = preg_match_all('|\#\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
1725 for($x = 0; $x < count($matches); $x ++) {
1727 $ret[] = array('#',$matches[1][$x], $matches[2][$x]);
1731 $cnt = preg_match_all('|\@\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
1733 for($x = 0; $x < count($matches); $x ++) {
1735 $ret[] = array('#',$matches[1][$x], $matches[2][$x]);
1741 function item_getfeedattach($item) {
1743 $arr = explode(',',$item['attach']);
1745 foreach($arr as $r) {
1747 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"\[\/attach\]|',$r,$matches);
1749 $ret .= '<link rel="enclosure" href="' . xmlify($matches[1]) . '" type="' . xmlify($matches[3]) . '" ';
1750 if(intval($matches[2]))
1751 $ret .= 'length="' . intval($matches[2]) . '" ';
1752 if($matches[4] !== ' ')
1753 $ret .= 'title="' . xmlify(trim($matches[4])) . '" ';
1754 $ret .= ' />' . "\r\n";
1763 function item_expire($uid,$days) {
1765 if((! $uid) || (! $days))
1768 $r = q("SELECT * FROM `item`
1770 AND `created` < UTC_TIMESTAMP() - INTERVAL %d DAY
1780 logger('expire: # items=' . count($r) );
1782 foreach($r as $item) {
1784 // Only expire posts, not photos and photo comments
1786 if(strlen($item['resource-id']))
1789 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
1790 dbesc(datetime_convert()),
1791 dbesc(datetime_convert()),
1797 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
1798 dbesc(datetime_convert()),
1799 dbesc(datetime_convert()),
1800 dbesc($item['parent-uri']),
1801 intval($item['uid'])
1806 proc_run('php',"include/notifier.php","expire","$uid");
1811 function drop_items($items) {
1815 foreach($items as $item) {
1816 $owner = drop_item($item,false);
1817 if($owner && ! $uid)
1822 // multiple threads may have been deleted, send an expire notification
1825 proc_run('php',"include/notifier.php","expire","$uid");
1829 function drop_item($id,$interactive = true) {
1833 // locate item to be deleted
1835 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
1842 notice( t('Item not found.') . EOL);
1843 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
1848 $owner = $item['uid'];
1850 // check if logged in user is either the author or owner of this item
1852 if((local_user() == $item['uid']) || (remote_user() == $item['contact-id'])) {
1856 $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
1857 dbesc(datetime_convert()),
1858 dbesc(datetime_convert()),
1862 // If item is a link to a photo resource, nuke all the associated photos
1863 // (visitors will not have photo resources)
1864 // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
1865 // generate a resource-id and therefore aren't intimately linked to the item.
1867 if(strlen($item['resource-id'])) {
1868 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
1869 dbesc($item['resource-id']),
1870 intval($item['uid'])
1872 // ignore the result
1875 // If item is a link to an event, nuke the event record.
1877 if(intval($item['event-id'])) {
1878 q("DELETE FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
1879 intval($item['event-id']),
1880 intval($item['uid'])
1882 // ignore the result
1886 // If it's the parent of a comment thread, kill all the kids
1888 if($item['uri'] == $item['parent-uri']) {
1889 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = ''
1890 WHERE `parent-uri` = '%s' AND `uid` = %d ",
1891 dbesc(datetime_convert()),
1892 dbesc(datetime_convert()),
1893 dbesc($item['parent-uri']),
1894 intval($item['uid'])
1896 // ignore the result
1899 // ensure that last-child is set in case the comment that had it just got wiped.
1900 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
1901 dbesc(datetime_convert()),
1902 dbesc($item['parent-uri']),
1903 intval($item['uid'])
1905 // who is the last child now?
1906 $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",
1907 dbesc($item['parent-uri']),
1908 intval($item['uid'])
1911 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
1916 $drop_id = intval($item['id']);
1918 // send the notification upstream/downstream as the case may be
1923 proc_run('php',"include/notifier.php","drop","$drop_id");
1924 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
1930 notice( t('Permission denied.') . EOL);
1931 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);