3 require_once('include/bbcode.php');
4 require_once('include/oembed.php');
5 require_once('include/salmon.php');
6 require_once('include/crypto.php');
7 require_once('include/Photo.php');
10 function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
13 $sitefeed = ((strlen($owner_nick)) ? false : true); // not yet implemented, need to rewrite huge chunks of following logic
14 $public_feed = (($dfrn_id) ? false : true);
15 $starred = false; // not yet implemented, possible security issues
18 if($public_feed && $a->argc > 2) {
19 for($x = 2; $x < $a->argc; $x++) {
20 if($a->argv[$x] == 'converse')
22 if($a->argv[$x] == 'starred')
24 if($a->argv[$x] === 'category' && $a->argc > ($x + 1) && strlen($a->argv[$x+1]))
25 $category = $a->argv[$x+1];
31 // default permissions - anonymous user
33 $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
35 $r = q("SELECT `contact`.*, `user`.`uid` AS `user_uid`, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`
36 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
37 WHERE `contact`.`self` = 1 AND `user`.`nickname` = '%s' LIMIT 1",
45 $owner_id = $owner['user_uid'];
46 $owner_nick = $owner['nickname'];
48 $birthday = feed_birthday($owner_id,$owner['timezone']);
55 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
59 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
60 $my_id = '1:' . $dfrn_id;
63 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
64 $my_id = '0:' . $dfrn_id;
71 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
79 require_once('include/security.php');
80 $groups = init_groups_visitor($contact['id']);
83 for($x = 0; $x < count($groups); $x ++)
84 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
85 $gs = implode('|', $groups);
88 $gs = '<<>>' ; // Impossible to match
90 $sql_extra = sprintf("
91 AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' )
92 AND ( `deny_cid` = '' OR NOT `deny_cid` REGEXP '<%d>' )
93 AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
94 AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s')
96 intval($contact['id']),
97 intval($contact['id']),
108 if(! strlen($last_update))
109 $last_update = 'now -30 days';
111 if(isset($category)) {
112 $sql_extra .= file_tag_file_query('item',$category,'category');
117 $sql_extra .= " AND `contact`.`self` = 1 ";
120 $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
122 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
123 `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`,
124 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
125 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
126 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`,
127 `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
128 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
129 LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
130 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`parent` != 0
131 AND `item`.`wall` = 1 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
132 AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
134 ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
141 // Will check further below if this actually returned results.
142 // We will provide an empty feed if that is the case.
146 $feed_template = get_markup_template(($dfrn_id) ? 'atom_feed_dfrn.tpl' : 'atom_feed.tpl');
150 $hubxml = feed_hublinks();
152 $salmon = feed_salmonlinks($owner_nick);
154 $atom .= replace_macros($feed_template, array(
155 '$version' => xmlify(FRIENDICA_VERSION),
156 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
157 '$feed_title' => xmlify($owner['name']),
158 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', 'now' , ATOM_TIME)) ,
160 '$salmon' => $salmon,
161 '$name' => xmlify($owner['name']),
162 '$profile_page' => xmlify($owner['url']),
163 '$photo' => xmlify($owner['photo']),
164 '$thumb' => xmlify($owner['thumb']),
165 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
166 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
167 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
168 '$birthday' => ((strlen($birthday)) ? '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>' : ''),
169 '$community' => (($owner['page-flags'] == PAGE_COMMUNITY) ? '<dfrn:community>1</dfrn:community>' : '')
172 call_hooks('atom_feed', $atom);
174 if(! count($items)) {
176 call_hooks('atom_feed_end', $atom);
178 $atom .= '</feed>' . "\r\n";
182 foreach($items as $item) {
184 // prevent private email from leaking.
185 if($item['network'] === NETWORK_MAIL)
188 // public feeds get html, our own nodes use bbcode
192 // catch any email that's in a public conversation and make sure it doesn't leak
200 $atom .= atom_entry($item,$type,null,$owner,true);
203 call_hooks('atom_feed_end', $atom);
205 $atom .= '</feed>' . "\r\n";
211 function construct_verb($item) {
213 return $item['verb'];
214 return ACTIVITY_POST;
217 function construct_activity_object($item) {
219 if($item['object']) {
220 $o = '<as:object>' . "\r\n";
221 $r = parse_xml_string($item['object'],false);
227 $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
229 $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
231 $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
233 if(substr($r->link,0,1) === '<') {
234 // patch up some facebook "like" activity objects that got stored incorrectly
235 // for a couple of months prior to 9-Jun-2011 and generated bad XML.
236 // we can probably remove this hack here and in the following function in a few months time.
237 if(strstr($r->link,'&') && (! strstr($r->link,'&')))
238 $r->link = str_replace('&','&', $r->link);
239 $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
243 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
246 $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
247 $o .= '</as:object>' . "\r\n";
254 function construct_activity_target($item) {
256 if($item['target']) {
257 $o = '<as:target>' . "\r\n";
258 $r = parse_xml_string($item['target'],false);
262 $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
264 $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
266 $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
268 if(substr($r->link,0,1) === '<') {
269 if(strstr($r->link,'&') && (! strstr($r->link,'&')))
270 $r->link = str_replace('&','&', $r->link);
271 $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
275 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
278 $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
279 $o .= '</as:target>' . "\r\n";
288 * The purpose of this function is to apply system message length limits to
289 * imported messages without including any embedded photos in the length
291 if(! function_exists('limit_body_size')) {
292 function limit_body_size($body) {
294 logger('limit_body_size: start', LOGGER_DEBUG);
296 $maxlen = get_max_import_size();
298 // If the length of the body, including the embedded images, is smaller
299 // than the maximum, then don't waste time looking for the images
300 if($maxlen && (strlen($body) > $maxlen)) {
302 logger('limit_body_size: the total body length exceeds the limit', LOGGER_DEBUG);
309 $img_start = strpos($orig_body, '[img');
310 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
311 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
312 while(($img_st_close !== false) && ($img_end !== false)) {
314 $img_st_close++; // make it point to AFTER the closing bracket
315 $img_end += $img_start;
316 $img_end += strlen('[/img]');
318 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
319 // This is an embedded image
321 if( ($textlen + $img_start) > $maxlen ) {
322 if($textlen < $maxlen) {
323 logger('limit_body_size: the limit happens before an embedded image', LOGGER_DEBUG);
324 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
329 $new_body = $new_body . substr($orig_body, 0, $img_start);
330 $textlen += $img_start;
333 $new_body = $new_body . substr($orig_body, $img_start, $img_end - $img_start);
337 if( ($textlen + $img_end) > $maxlen ) {
338 if($textlen < $maxlen) {
339 logger('limit_body_size: the limit happens before the end of a non-embedded image', LOGGER_DEBUG);
340 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
345 $new_body = $new_body . substr($orig_body, 0, $img_end);
346 $textlen += $img_end;
349 $orig_body = substr($orig_body, $img_end);
351 if($orig_body === false) // in case the body ends on a closing image tag
354 $img_start = strpos($orig_body, '[img');
355 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
356 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
359 if( ($textlen + strlen($orig_body)) > $maxlen) {
360 if($textlen < $maxlen) {
361 logger('limit_body_size: the limit happens after the end of the last image', LOGGER_DEBUG);
362 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
367 logger('limit_body_size: the text size with embedded images extracted did not violate the limit', LOGGER_DEBUG);
368 $new_body = $new_body . $orig_body;
369 $textlen += strlen($orig_body);
378 function title_is_body($title, $body) {
380 $title = strip_tags($title);
381 $title = trim($title);
382 $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
383 $title = str_replace(array("\n", "\r", "\t", " "), array("","","",""), $title);
385 $body = strip_tags($body);
387 $body = html_entity_decode($body, ENT_QUOTES, 'UTF-8');
388 $body = str_replace(array("\n", "\r", "\t", " "), array("","","",""), $body);
390 if (strlen($title) < strlen($body))
391 $body = substr($body, 0, strlen($title));
393 if (($title != $body) and (substr($title, -3) == "...")) {
394 $pos = strrpos($title, "...");
396 $title = substr($title, 0, $pos);
397 $body = substr($body, 0, $pos);
401 return($title == $body);
406 function get_atom_elements($feed,$item) {
408 require_once('library/HTMLPurifier.auto.php');
409 require_once('include/html2bbcode.php');
411 $best_photo = array();
415 $author = $item->get_author();
417 $res['author-name'] = unxmlify($author->get_name());
418 $res['author-link'] = unxmlify($author->get_link());
421 $res['author-name'] = unxmlify($feed->get_title());
422 $res['author-link'] = unxmlify($feed->get_permalink());
424 $res['uri'] = unxmlify($item->get_id());
425 $res['title'] = unxmlify($item->get_title());
426 $res['body'] = unxmlify($item->get_content());
427 $res['plink'] = unxmlify($item->get_link(0));
429 // removing the content of the title if its identically to the body
430 // This helps with auto generated titles e.g. from tumblr
431 if (title_is_body($res["title"], $res["body"]))
435 $base_url = implode('/', array_slice(explode('/',$res['plink']),0,3));
439 // look for a photo. We should check media size and find the best one,
440 // but for now let's just find any author photo
442 $rawauthor = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
444 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
445 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
446 foreach($base as $link) {
447 if(!x($res, 'author-avatar') || !$res['author-avatar']) {
448 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
449 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
454 $rawactor = $item->get_item_tags(NAMESPACE_ACTIVITY, 'actor');
456 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
457 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
458 if($base && count($base)) {
459 foreach($base as $link) {
460 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
461 $res['author-link'] = unxmlify($link['attribs']['']['href']);
462 if(!x($res, 'author-avatar') || !$res['author-avatar']) {
463 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
464 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
470 // No photo/profile-link on the item - look at the feed level
472 if((! (x($res,'author-link'))) || (! (x($res,'author-avatar')))) {
473 $rawauthor = $feed->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
474 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
475 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
476 foreach($base as $link) {
477 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
478 $res['author-link'] = unxmlify($link['attribs']['']['href']);
479 if(! $res['author-avatar']) {
480 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
481 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
486 $rawactor = $feed->get_feed_tags(NAMESPACE_ACTIVITY, 'subject');
488 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
489 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
491 if($base && count($base)) {
492 foreach($base as $link) {
493 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
494 $res['author-link'] = unxmlify($link['attribs']['']['href']);
495 if(! (x($res,'author-avatar'))) {
496 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
497 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
504 $apps = $item->get_item_tags(NAMESPACE_STATUSNET,'notice_info');
505 if($apps && $apps[0]['attribs']['']['source']) {
506 $res['app'] = strip_tags(unxmlify($apps[0]['attribs']['']['source']));
507 if($res['app'] === 'web')
508 $res['app'] = 'OStatus';
511 // base64 encoded json structure representing Diaspora signature
513 $dsig = $item->get_item_tags(NAMESPACE_DFRN,'diaspora_signature');
515 $res['dsprsig'] = unxmlify($dsig[0]['data']);
518 $dguid = $item->get_item_tags(NAMESPACE_DFRN,'diaspora_guid');
520 $res['guid'] = unxmlify($dguid[0]['data']);
522 $bm = $item->get_item_tags(NAMESPACE_DFRN,'bookmark');
524 $res['bookmark'] = ((unxmlify($bm[0]['data']) === 'true') ? 1 : 0);
528 * If there's a copy of the body content which is guaranteed to have survived mangling in transit, use it.
531 $have_real_body = false;
533 $rawenv = $item->get_item_tags(NAMESPACE_DFRN, 'env');
535 $have_real_body = true;
536 $res['body'] = $rawenv[0]['data'];
537 $res['body'] = str_replace(array(' ',"\t","\r","\n"), array('','','',''),$res['body']);
538 // make sure nobody is trying to sneak some html tags by us
539 $res['body'] = notags(base64url_decode($res['body']));
543 $res['body'] = limit_body_size($res['body']);
545 // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust
546 // the content type. Our own network only emits text normally, though it might have been converted to
547 // html if we used a pubsubhubbub transport. But if we see even one html tag in our text, we will
548 // have to assume it is all html and needs to be purified.
550 // It doesn't matter all that much security wise - because before this content is used anywhere, we are
551 // going to escape any tags we find regardless, but this lets us import a limited subset of html from
552 // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining
555 if((strpos($res['body'],'<') !== false) && (strpos($res['body'],'>') !== false)) {
557 $res['body'] = reltoabs($res['body'],$base_url);
559 $res['body'] = html2bb_video($res['body']);
561 $res['body'] = oembed_html2bbcode($res['body']);
563 $config = HTMLPurifier_Config::createDefault();
564 $config->set('Cache.DefinitionImpl', null);
566 // we shouldn't need a whitelist, because the bbcode converter
567 // will strip out any unsupported tags.
569 $purifier = new HTMLPurifier($config);
570 $res['body'] = $purifier->purify($res['body']);
572 $res['body'] = @html2bbcode($res['body']);
576 elseif(! $have_real_body) {
578 // it's not one of our messages and it has no tags
579 // so it's probably just text. We'll escape it just to be safe.
581 $res['body'] = escape_tags($res['body']);
585 // this tag is obsolete but we keep it for really old sites
587 $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
588 if($allow && $allow[0]['data'] == 1)
589 $res['last-child'] = 1;
591 $res['last-child'] = 0;
593 $private = $item->get_item_tags(NAMESPACE_DFRN,'private');
594 if($private && intval($private[0]['data']) > 0)
595 $res['private'] = intval($private[0]['data']);
599 $extid = $item->get_item_tags(NAMESPACE_DFRN,'extid');
600 if($extid && $extid[0]['data'])
601 $res['extid'] = $extid[0]['data'];
603 $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
605 $res['location'] = unxmlify($rawlocation[0]['data']);
608 $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
610 $res['created'] = unxmlify($rawcreated[0]['data']);
613 $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
615 $res['edited'] = unxmlify($rawedited[0]['data']);
617 if((x($res,'edited')) && (! (x($res,'created'))))
618 $res['created'] = $res['edited'];
620 if(! $res['created'])
621 $res['created'] = $item->get_date('c');
624 $res['edited'] = $item->get_date('c');
627 // Disallow time travelling posts
629 $d1 = strtotime($res['created']);
630 $d2 = strtotime($res['edited']);
631 $d3 = strtotime('now');
634 $res['created'] = datetime_convert();
636 $res['edited'] = datetime_convert();
638 $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
639 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
640 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
641 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
642 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
643 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
644 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
645 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
646 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
648 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
649 $base = $rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
651 foreach($base as $link) {
652 if(!x($res, 'owner-avatar') || !$res['owner-avatar']) {
653 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
654 $res['owner-avatar'] = unxmlify($link['attribs']['']['href']);
659 $rawgeo = $item->get_item_tags(NAMESPACE_GEORSS,'point');
661 $res['coord'] = unxmlify($rawgeo[0]['data']);
664 $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
666 // select between supported verbs
669 $res['verb'] = unxmlify($rawverb[0]['data']);
672 // translate OStatus unfollow to activity streams if it happened to get selected
674 if((x($res,'verb')) && ($res['verb'] === 'http://ostatus.org/schema/1.0/unfollow'))
675 $res['verb'] = ACTIVITY_UNFOLLOW;
677 $cats = $item->get_categories();
680 foreach($cats as $cat) {
681 $term = $cat->get_term();
683 $term = $cat->get_label();
684 $scheme = $cat->get_scheme();
685 if($scheme && $term && stristr($scheme,'X-DFRN:'))
686 $tag_arr[] = substr($scheme,7,1) . '[url=' . unxmlify(substr($scheme,9)) . ']' . unxmlify($term) . '[/url]';
688 $tag_arr[] = notags(trim($term));
690 $res['tag'] = implode(',', $tag_arr);
693 $attach = $item->get_enclosures();
696 foreach($attach as $att) {
697 $len = intval($att->get_length());
698 $link = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_link()))));
699 $title = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_title()))));
700 $type = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_type()))));
701 if(strpos($type,';'))
702 $type = substr($type,0,strpos($type,';'));
703 if((! $link) || (strpos($link,'http') !== 0))
709 $type = 'application/octet-stream';
711 $att_arr[] = '[attach]href="' . $link . '" length="' . $len . '" type="' . $type . '" title="' . $title . '"[/attach]';
713 $res['attach'] = implode(',', $att_arr);
716 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
719 $res['object'] = '<object>' . "\n";
720 $child = $rawobj[0]['child'];
721 if($child[NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
722 $res['object-type'] = $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'];
723 $res['object'] .= '<type>' . $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
725 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'id') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
726 $res['object'] .= '<id>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
727 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'link') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
728 $res['object'] .= '<link>' . encode_rel_links($child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
729 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'title') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
730 $res['object'] .= '<title>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
731 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'content') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
732 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
734 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
735 // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
736 $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
737 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
739 $body = html2bb_video($body);
741 $config = HTMLPurifier_Config::createDefault();
742 $config->set('Cache.DefinitionImpl', null);
744 $purifier = new HTMLPurifier($config);
745 $body = $purifier->purify($body);
746 $body = html2bbcode($body);
749 $res['object'] .= '<content>' . $body . '</content>' . "\n";
752 $res['object'] .= '</object>' . "\n";
755 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'target');
758 $res['target'] = '<target>' . "\n";
759 $child = $rawobj[0]['child'];
760 if($child[NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
761 $res['target'] .= '<type>' . $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
763 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'id') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
764 $res['target'] .= '<id>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
765 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'link') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
766 $res['target'] .= '<link>' . encode_rel_links($child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
767 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'data') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
768 $res['target'] .= '<title>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
769 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'data') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
770 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
772 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
773 // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
774 $res['target'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
775 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
777 $body = html2bb_video($body);
779 $config = HTMLPurifier_Config::createDefault();
780 $config->set('Cache.DefinitionImpl', null);
782 $purifier = new HTMLPurifier($config);
783 $body = $purifier->purify($body);
784 $body = html2bbcode($body);
787 $res['target'] .= '<content>' . $body . '</content>' . "\n";
790 $res['target'] .= '</target>' . "\n";
793 // This is some experimental stuff. By now retweets are shown with "RT:"
794 // But: There is data so that the message could be shown similar to native retweets
795 // There is some better way to parse this array - but it didn't worked for me.
796 $child = $item->feed->data["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["feed"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["entry"][0]["child"]["http://activitystrea.ms/spec/1.0/"][object][0]["child"];
797 if (is_array($child)) {
798 logger('get_atom_elements: Looking for status.net repeated message');
800 $message = $child["http://activitystrea.ms/spec/1.0/"]["object"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["content"][0]["data"];
801 $orig_uri = $child["http://activitystrea.ms/spec/1.0/"]["object"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["id"][0]["data"];
802 $author = $child[SIMPLEPIE_NAMESPACE_ATOM_10]["author"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10];
803 $uri = $author["uri"][0]["data"];
804 $name = $author["name"][0]["data"];
805 $avatar = @array_shift($author["link"][2]["attribs"]);
806 $avatar = $avatar["href"];
808 if (($name != "") and ($uri != "") and ($avatar != "") and ($message != "")) {
809 logger('get_atom_elements: fixing sender of repeated message.');
811 if (intval(get_config('system','new_share'))) {
812 $prefix = "[share author='".str_replace("'", "'",$name).
814 "' avatar='".$avatar.
815 "' link='".$orig_uri."']";
817 $res["body"] = $prefix.html2bbcode($message)."[/share]";
819 $res["owner-name"] = $res["author-name"];
820 $res["owner-link"] = $res["author-link"];
821 $res["owner-avatar"] = $res["author-avatar"];
823 $res["author-name"] = $name;
824 $res["author-link"] = $uri;
825 $res["author-avatar"] = $avatar;
827 $res["body"] = html2bbcode($message);
832 $arr = array('feed' => $feed, 'item' => $item, 'result' => $res);
834 call_hooks('parse_atom', $arr);
836 //if (($res["title"] != "") or (strpos($res["body"], "RT @") > 0)) {
837 //if (strpos($res["body"], "RT @") !== false) {
838 // $debugfile = tempnam("/home/ike/log", "item-res2-");
839 // file_put_contents($debugfile, serialize($arr));
845 function encode_rel_links($links) {
847 if(! ((is_array($links)) && (count($links))))
849 foreach($links as $link) {
851 if($link['attribs']['']['rel'])
852 $o .= 'rel="' . $link['attribs']['']['rel'] . '" ';
853 if($link['attribs']['']['type'])
854 $o .= 'type="' . $link['attribs']['']['type'] . '" ';
855 if($link['attribs']['']['href'])
856 $o .= 'href="' . $link['attribs']['']['href'] . '" ';
857 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['width'])
858 $o .= 'media:width="' . $link['attribs'][NAMESPACE_MEDIA]['width'] . '" ';
859 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['height'])
860 $o .= 'media:height="' . $link['attribs'][NAMESPACE_MEDIA]['height'] . '" ';
868 function item_store($arr,$force_parent = false) {
870 // If a Diaspora signature structure was passed in, pull it out of the
871 // item array and set it aside for later storage.
874 if(x($arr,'dsprsig')) {
875 $dsprsig = json_decode(base64_decode($arr['dsprsig']));
876 unset($arr['dsprsig']);
879 if(x($arr, 'gravity'))
880 $arr['gravity'] = intval($arr['gravity']);
881 elseif($arr['parent-uri'] === $arr['uri'])
883 elseif(activity_match($arr['verb'],ACTIVITY_POST))
886 $arr['gravity'] = 6; // extensible catchall
889 $arr['type'] = 'remote';
891 // Shouldn't happen but we want to make absolutely sure it doesn't leak from a plugin.
893 if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false))
894 $arr['body'] = strip_tags($arr['body']);
897 if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
898 require_once('library/langdet/Text/LanguageDetect.php');
899 $naked_body = preg_replace('/\[(.+?)\]/','',$arr['body']);
900 $l = new Text_LanguageDetect;
901 //$lng = $l->detectConfidence($naked_body);
902 //$arr['postopts'] = (($lng['language']) ? 'lang=' . $lng['language'] . ';' . $lng['confidence'] : '');
903 $lng = $l->detect($naked_body, 3);
905 if (sizeof($lng) > 0) {
908 foreach ($lng as $language => $score) {
914 $postopts .= $language.";".$score;
916 $arr['postopts'] = $postopts;
920 $arr['wall'] = ((x($arr,'wall')) ? intval($arr['wall']) : 0);
921 $arr['uri'] = ((x($arr,'uri')) ? notags(trim($arr['uri'])) : random_string());
922 $arr['extid'] = ((x($arr,'extid')) ? notags(trim($arr['extid'])) : '');
923 $arr['author-name'] = ((x($arr,'author-name')) ? notags(trim($arr['author-name'])) : '');
924 $arr['author-link'] = ((x($arr,'author-link')) ? notags(trim($arr['author-link'])) : '');
925 $arr['author-avatar'] = ((x($arr,'author-avatar')) ? notags(trim($arr['author-avatar'])) : '');
926 $arr['owner-name'] = ((x($arr,'owner-name')) ? notags(trim($arr['owner-name'])) : '');
927 $arr['owner-link'] = ((x($arr,'owner-link')) ? notags(trim($arr['owner-link'])) : '');
928 $arr['owner-avatar'] = ((x($arr,'owner-avatar')) ? notags(trim($arr['owner-avatar'])) : '');
929 $arr['created'] = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
930 $arr['edited'] = ((x($arr,'edited') !== false) ? datetime_convert('UTC','UTC',$arr['edited']) : datetime_convert());
931 $arr['commented'] = datetime_convert();
932 $arr['received'] = datetime_convert();
933 $arr['changed'] = datetime_convert();
934 $arr['title'] = ((x($arr,'title')) ? notags(trim($arr['title'])) : '');
935 $arr['location'] = ((x($arr,'location')) ? notags(trim($arr['location'])) : '');
936 $arr['coord'] = ((x($arr,'coord')) ? notags(trim($arr['coord'])) : '');
937 $arr['last-child'] = ((x($arr,'last-child')) ? intval($arr['last-child']) : 0 );
938 $arr['visible'] = ((x($arr,'visible') !== false) ? intval($arr['visible']) : 1 );
940 $arr['parent-uri'] = ((x($arr,'parent-uri')) ? notags(trim($arr['parent-uri'])) : '');
941 $arr['verb'] = ((x($arr,'verb')) ? notags(trim($arr['verb'])) : '');
942 $arr['object-type'] = ((x($arr,'object-type')) ? notags(trim($arr['object-type'])) : '');
943 $arr['object'] = ((x($arr,'object')) ? trim($arr['object']) : '');
944 $arr['target-type'] = ((x($arr,'target-type')) ? notags(trim($arr['target-type'])) : '');
945 $arr['target'] = ((x($arr,'target')) ? trim($arr['target']) : '');
946 $arr['plink'] = ((x($arr,'plink')) ? notags(trim($arr['plink'])) : '');
947 $arr['allow_cid'] = ((x($arr,'allow_cid')) ? trim($arr['allow_cid']) : '');
948 $arr['allow_gid'] = ((x($arr,'allow_gid')) ? trim($arr['allow_gid']) : '');
949 $arr['deny_cid'] = ((x($arr,'deny_cid')) ? trim($arr['deny_cid']) : '');
950 $arr['deny_gid'] = ((x($arr,'deny_gid')) ? trim($arr['deny_gid']) : '');
951 $arr['private'] = ((x($arr,'private')) ? intval($arr['private']) : 0 );
952 $arr['bookmark'] = ((x($arr,'bookmark')) ? intval($arr['bookmark']) : 0 );
953 $arr['body'] = ((x($arr,'body')) ? trim($arr['body']) : '');
954 $arr['tag'] = ((x($arr,'tag')) ? notags(trim($arr['tag'])) : '');
955 $arr['attach'] = ((x($arr,'attach')) ? notags(trim($arr['attach'])) : '');
956 $arr['app'] = ((x($arr,'app')) ? notags(trim($arr['app'])) : '');
957 $arr['origin'] = ((x($arr,'origin')) ? intval($arr['origin']) : 0 );
958 $arr['guid'] = ((x($arr,'guid')) ? notags(trim($arr['guid'])) : get_guid());
961 $arr['thr-parent'] = $arr['parent-uri'];
962 if($arr['parent-uri'] === $arr['uri']) {
965 $allow_cid = $arr['allow_cid'];
966 $allow_gid = $arr['allow_gid'];
967 $deny_cid = $arr['deny_cid'];
968 $deny_gid = $arr['deny_gid'];
972 // find the parent and snarf the item id and ACL's
973 // and anything else we need to inherit
975 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d ORDER BY `id` ASC LIMIT 1",
976 dbesc($arr['parent-uri']),
982 // is the new message multi-level threaded?
983 // even though we don't support it now, preserve the info
984 // and re-attach to the conversation parent.
986 if($r[0]['uri'] != $r[0]['parent-uri']) {
987 $arr['parent-uri'] = $r[0]['parent-uri'];
988 $z = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `parent-uri` = '%s' AND `uid` = %d
989 ORDER BY `id` ASC LIMIT 1",
990 dbesc($r[0]['parent-uri']),
991 dbesc($r[0]['parent-uri']),
998 $parent_id = $r[0]['id'];
999 $parent_deleted = $r[0]['deleted'];
1000 $allow_cid = $r[0]['allow_cid'];
1001 $allow_gid = $r[0]['allow_gid'];
1002 $deny_cid = $r[0]['deny_cid'];
1003 $deny_gid = $r[0]['deny_gid'];
1004 $arr['wall'] = $r[0]['wall'];
1006 // if the parent is private, force privacy for the entire conversation
1007 // This differs from the above settings as it subtly allows comments from
1008 // email correspondents to be private even if the overall thread is not.
1010 if($r[0]['private'])
1011 $arr['private'] = $r[0]['private'];
1013 // Edge case. We host a public forum that was originally posted to privately.
1014 // The original author commented, but as this is a comment, the permissions
1015 // weren't fixed up so it will still show the comment as private unless we fix it here.
1017 if((intval($r[0]['forum_mode']) == 1) && (! $r[0]['private']))
1018 $arr['private'] = 0;
1022 // Allow one to see reply tweets from status.net even when
1023 // we don't have or can't see the original post.
1026 logger('item_store: $force_parent=true, reply converted to top-level post.');
1028 $arr['parent-uri'] = $arr['uri'];
1029 $arr['gravity'] = 0;
1032 logger('item_store: item parent was not found - ignoring item');
1036 $parent_deleted = 0;
1040 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1044 if($r && count($r)) {
1045 logger('item-store: duplicate item ignored. ' . print_r($arr,true));
1049 call_hooks('post_remote',$arr);
1051 if(x($arr,'cancel')) {
1052 logger('item_store: post cancelled by plugin.');
1058 logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
1060 $r = dbq("INSERT INTO `item` (`"
1061 . implode("`, `", array_keys($arr))
1063 . implode("', '", array_values($arr))
1066 // find the item we just created
1068 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d ORDER BY `id` ASC ",
1069 $arr['uri'], // already dbesc'd
1074 $current_post = $r[0]['id'];
1075 logger('item_store: created item ' . $current_post);
1078 logger('item_store: could not locate created item');
1082 logger('item_store: duplicated post occurred. Removing duplicates.');
1083 q("DELETE FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `id` != %d ",
1085 intval($arr['uid']),
1086 intval($current_post)
1090 if((! $parent_id) || ($arr['parent-uri'] === $arr['uri']))
1091 $parent_id = $current_post;
1093 if(strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid))
1096 $private = $arr['private'];
1098 // Set parent id - and also make sure to inherit the parent's ACL's.
1100 $r = q("UPDATE `item` SET `parent` = %d, `allow_cid` = '%s', `allow_gid` = '%s',
1101 `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d, `deleted` = %d WHERE `id` = %d LIMIT 1",
1108 intval($parent_deleted),
1109 intval($current_post)
1112 $arr['id'] = $current_post;
1113 $arr['parent'] = $parent_id;
1114 $arr['allow_cid'] = $allow_cid;
1115 $arr['allow_gid'] = $allow_gid;
1116 $arr['deny_cid'] = $deny_cid;
1117 $arr['deny_gid'] = $deny_gid;
1118 $arr['private'] = $private;
1119 $arr['deleted'] = $parent_deleted;
1120 call_hooks('post_remote_end',$arr);
1122 // update the commented timestamp on the parent
1124 q("UPDATE `item` set `commented` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
1125 dbesc(datetime_convert()),
1126 dbesc(datetime_convert()),
1131 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
1132 intval($current_post),
1133 dbesc($dsprsig->signed_text),
1134 dbesc($dsprsig->signature),
1135 dbesc($dsprsig->signer)
1141 * If this is now the last-child, force all _other_ children of this parent to *not* be last-child
1144 if($arr['last-child']) {
1145 $r = q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d AND `id` != %d",
1147 intval($arr['uid']),
1148 intval($current_post)
1152 tag_deliver($arr['uid'],$current_post);
1154 return $current_post;
1157 function get_item_contact($item,$contacts) {
1158 if(! count($contacts) || (! is_array($item)))
1160 foreach($contacts as $contact) {
1161 if($contact['id'] == $item['contact-id']) {
1163 break; // NOTREACHED
1170 function tag_deliver($uid,$item_id) {
1172 // look for mention tags and setup a second delivery chain for forum/community posts if appropriate
1178 $u = q("select * from user where uid = %d limit 1",
1184 $community_page = (($u[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
1185 $prvgroup = (($u[0]['page-flags'] == PAGE_PRVGROUP) ? true : false);
1188 $i = q("select * from item where id = %d and uid = %d limit 1",
1197 $link = normalise_link($a->get_baseurl() . '/profile/' . $u[0]['nickname']);
1199 // Diaspora uses their own hardwired link URL in @-tags
1200 // instead of the one we supply with webfinger
1202 $dlink = normalise_link($a->get_baseurl() . '/u/' . $u[0]['nickname']);
1204 $cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism',$item['body'],$matches,PREG_SET_ORDER);
1206 foreach($matches as $mtch) {
1207 if(link_compare($link,$mtch[1]) || link_compare($dlink,$mtch[1])) {
1209 logger('tag_deliver: mention found: ' . $mtch[2]);
1217 // send a notification
1219 // use a local photo if we have one
1221 $r = q("select * from contact where uid = %d and nurl = '%s' limit 1",
1222 intval($u[0]['uid']),
1223 dbesc(normalise_link($item['author-link']))
1225 $photo = (($r && count($r)) ? $r[0]['thumb'] : $item['author-avatar']);
1228 require_once('include/enotify.php');
1230 'type' => NOTIFY_TAGSELF,
1231 'notify_flags' => $u[0]['notify-flags'],
1232 'language' => $u[0]['language'],
1233 'to_name' => $u[0]['username'],
1234 'to_email' => $u[0]['email'],
1235 'uid' => $u[0]['uid'],
1237 'link' => $a->get_baseurl() . '/display/' . $u[0]['nickname'] . '/' . $item['id'],
1238 'source_name' => $item['author-name'],
1239 'source_link' => $item['author-link'],
1240 'source_photo' => $photo,
1241 'verb' => ACTIVITY_TAG,
1246 $arr = array('item' => $item, 'user' => $u[0], 'contact' => $r[0]);
1248 call_hooks('tagged', $arr);
1250 if((! $community_page) && (! $prvgroup))
1254 // tgroup delivery - setup a second delivery chain
1255 // prevent delivery looping - only proceed
1256 // if the message originated elsewhere and is a top-level post
1258 if(($item['wall']) || ($item['origin']) || ($item['id'] != $item['parent']))
1261 // now change this copy of the post to a forum head message and deliver to all the tgroup members
1264 $c = q("select name, url, thumb from contact where self = 1 and uid = %d limit 1",
1265 intval($u[0]['uid'])
1270 // also reset all the privacy bits to the forum default permissions
1272 $private = ($u[0]['allow_cid'] || $u[0]['allow_gid'] || $u[0]['deny_cid'] || $u[0]['deny_gid']) ? 1 : 0;
1274 $forum_mode = (($prvgroup) ? 2 : 1);
1276 q("update item set wall = 1, origin = 1, forum_mode = %d, `owner-name` = '%s', `owner-link` = '%s', `owner-avatar` = '%s',
1277 `private` = %d, `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s' where id = %d limit 1",
1278 intval($forum_mode),
1279 dbesc($c[0]['name']),
1280 dbesc($c[0]['url']),
1281 dbesc($c[0]['thumb']),
1283 dbesc($u[0]['allow_cid']),
1284 dbesc($u[0]['allow_gid']),
1285 dbesc($u[0]['deny_cid']),
1286 dbesc($u[0]['deny_gid']),
1290 proc_run('php','include/notifier.php','tgroup',$item_id);
1296 function tgroup_check($uid,$item) {
1302 // check that the message originated elsewhere and is a top-level post
1304 if(($item['wall']) || ($item['origin']) || ($item['uri'] != $item['parent-uri']))
1308 $u = q("select * from user where uid = %d limit 1",
1314 $community_page = (($u[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
1315 $prvgroup = (($u[0]['page-flags'] == PAGE_PRVGROUP) ? true : false);
1318 $link = normalise_link($a->get_baseurl() . '/profile/' . $u[0]['nickname']);
1320 // Diaspora uses their own hardwired link URL in @-tags
1321 // instead of the one we supply with webfinger
1323 $dlink = normalise_link($a->get_baseurl() . '/u/' . $u[0]['nickname']);
1325 $cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism',$item['body'],$matches,PREG_SET_ORDER);
1327 foreach($matches as $mtch) {
1328 if(link_compare($link,$mtch[1]) || link_compare($dlink,$mtch[1])) {
1330 logger('tgroup_check: mention found: ' . $mtch[2]);
1338 if((! $community_page) && (! $prvgroup))
1352 function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
1356 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
1358 if($contact['duplex'] && $contact['dfrn-id'])
1359 $idtosend = '0:' . $orig_id;
1360 if($contact['duplex'] && $contact['issued-id'])
1361 $idtosend = '1:' . $orig_id;
1363 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
1365 $rino_enable = get_config('system','rino_encrypt');
1370 $ssl_val = intval(get_config('system','ssl_policy'));
1374 case SSL_POLICY_FULL:
1375 $ssl_policy = 'full';
1377 case SSL_POLICY_SELFSIGN:
1378 $ssl_policy = 'self';
1380 case SSL_POLICY_NONE:
1382 $ssl_policy = 'none';
1386 $url = $contact['notify'] . '&dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino=1' : '');
1388 logger('dfrn_deliver: ' . $url);
1390 $xml = fetch_url($url);
1392 $curl_stat = $a->get_curl_code();
1394 return(-1); // timed out
1396 logger('dfrn_deliver: ' . $xml, LOGGER_DATA);
1401 if(strpos($xml,'<?xml') === false) {
1402 logger('dfrn_deliver: no valid XML returned');
1403 logger('dfrn_deliver: returned XML: ' . $xml, LOGGER_DATA);
1407 $res = parse_xml_string($xml);
1409 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
1410 return (($res->status) ? $res->status : 3);
1412 $postvars = array();
1413 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
1414 $challenge = hex2bin((string) $res->challenge);
1415 $perm = (($res->perm) ? $res->perm : null);
1416 $dfrn_version = (float) (($res->dfrn_version) ? $res->dfrn_version : 2.0);
1417 $rino_allowed = ((intval($res->rino) === 1) ? 1 : 0);
1418 $page = (($owner['page-flags'] == PAGE_COMMUNITY) ? 1 : 0);
1420 if($owner['page-flags'] == PAGE_PRVGROUP)
1423 $final_dfrn_id = '';
1426 if((($perm == 'rw') && (! intval($contact['writable'])))
1427 || (($perm == 'r') && (intval($contact['writable'])))) {
1428 q("update contact set writable = %d where id = %d limit 1",
1429 intval(($perm == 'rw') ? 1 : 0),
1430 intval($contact['id'])
1432 $contact['writable'] = (string) 1 - intval($contact['writable']);
1436 if(($contact['duplex'] && strlen($contact['pubkey']))
1437 || ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
1438 || ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey']))) {
1439 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
1440 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
1443 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
1444 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
1447 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
1449 if(strpos($final_dfrn_id,':') == 1)
1450 $final_dfrn_id = substr($final_dfrn_id,2);
1452 if($final_dfrn_id != $orig_id) {
1453 logger('dfrn_deliver: wrong dfrn_id.');
1454 // did not decode properly - cannot trust this site
1458 $postvars['dfrn_id'] = $idtosend;
1459 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
1461 $postvars['dissolve'] = '1';
1464 if((($contact['rel']) && ($contact['rel'] != CONTACT_IS_SHARING) && (! $contact['blocked'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
1465 $postvars['data'] = $atom;
1466 $postvars['perm'] = 'rw';
1469 $postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
1470 $postvars['perm'] = 'r';
1473 $postvars['ssl_policy'] = $ssl_policy;
1476 $postvars['page'] = $page;
1478 if($rino && $rino_allowed && (! $dissolve)) {
1479 $key = substr(random_string(),0,16);
1480 $data = bin2hex(aes_encrypt($postvars['data'],$key));
1481 $postvars['data'] = $data;
1482 logger('rino: sent key = ' . $key, LOGGER_DEBUG);
1485 if($dfrn_version >= 2.1) {
1486 if(($contact['duplex'] && strlen($contact['pubkey']))
1487 || ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
1488 || ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey']))) {
1490 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
1493 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
1497 if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
1498 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
1501 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
1505 logger('md5 rawkey ' . md5($postvars['key']));
1507 $postvars['key'] = bin2hex($postvars['key']);
1510 logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);
1512 $xml = post_url($contact['notify'],$postvars);
1514 logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
1516 $curl_stat = $a->get_curl_code();
1517 if((! $curl_stat) || (! strlen($xml)))
1518 return(-1); // timed out
1520 if(($curl_stat == 503) && (stristr($a->get_curl_headers(),'retry-after')))
1523 if(strpos($xml,'<?xml') === false) {
1524 logger('dfrn_deliver: phase 2: no valid XML returned');
1525 logger('dfrn_deliver: phase 2: returned XML: ' . $xml, LOGGER_DATA);
1529 if($contact['term-date'] != '0000-00-00 00:00:00') {
1530 logger("dfrn_deliver: $url back from the dead - removing mark for death");
1531 require_once('include/Contact.php');
1532 unmark_for_death($contact);
1535 $res = parse_xml_string($xml);
1537 return $res->status;
1543 * consume_feed - process atom feed and update anything/everything we might need to update
1545 * $xml = the (atom) feed to consume - RSS isn't as fully supported but may work for simple feeds.
1547 * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
1548 * It is this person's stuff that is going to be updated.
1549 * $contact = the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
1550 * from an external network and MAY create an appropriate contact record. Otherwise, we MUST
1551 * have a contact record.
1552 * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or
1553 * might not) try and subscribe to it.
1554 * $datedir sorts in reverse order
1555 * $pass - by default ($pass = 0) we cannot guarantee that a parent item has been
1556 * imported prior to its children being seen in the stream unless we are certain
1557 * of how the feed is arranged/ordered.
1558 * With $pass = 1, we only pull parent items out of the stream.
1559 * With $pass = 2, we only pull children (comments/likes).
1561 * So running this twice, first with pass 1 and then with pass 2 will do the right
1562 * thing regardless of feed ordering. This won't be adequate in a fully-threaded
1563 * model where comments can have sub-threads. That would require some massive sorting
1564 * to get all the feed items into a mostly linear ordering, and might still require
1568 function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0) {
1570 require_once('library/simplepie/simplepie.inc');
1572 if(! strlen($xml)) {
1573 logger('consume_feed: empty input');
1577 $feed = new SimplePie();
1578 $feed->set_raw_data($xml);
1580 $feed->enable_order_by_date(true);
1582 $feed->enable_order_by_date(false);
1586 logger('consume_feed: Error parsing XML: ' . $feed->error());
1588 $permalink = $feed->get_permalink();
1590 // Check at the feed level for updated contact name and/or photo
1594 $photo_timestamp = '';
1598 $hubs = $feed->get_links('hub');
1599 logger('consume_feed: hubs: ' . print_r($hubs,true), LOGGER_DATA);
1602 $hub = implode(',', $hubs);
1604 $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'owner');
1606 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
1608 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
1609 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
1610 $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
1611 $new_name = $elems['name'][0]['data'];
1613 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
1614 $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
1615 $photo_url = $elems['link'][0]['attribs']['']['href'];
1618 if((x($rawtags[0]['child'], NAMESPACE_DFRN)) && (x($rawtags[0]['child'][NAMESPACE_DFRN],'birthday'))) {
1619 $birthday = datetime_convert('UTC','UTC', $rawtags[0]['child'][NAMESPACE_DFRN]['birthday'][0]['data']);
1623 if((is_array($contact)) && ($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
1624 logger('consume_feed: Updating photo for ' . $contact['name']);
1625 require_once("include/Photo.php");
1626 $photo_failure = false;
1627 $have_photo = false;
1629 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
1630 intval($contact['id']),
1631 intval($contact['uid'])
1634 $resource_id = $r[0]['resource-id'];
1638 $resource_id = photo_new_resource();
1641 $img_str = fetch_url($photo_url,true);
1642 // guess mimetype from headers or filename
1643 $type = guess_image_type($photo_url,true);
1646 $img = new Photo($img_str, $type);
1647 if($img->is_valid()) {
1649 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
1650 dbesc($resource_id),
1651 intval($contact['id']),
1652 intval($contact['uid'])
1656 $img->scaleImageSquare(175);
1658 $hash = $resource_id;
1659 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 4);
1661 $img->scaleImage(80);
1662 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 5);
1664 $img->scaleImage(48);
1665 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 6);
1669 q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
1670 WHERE `uid` = %d AND `id` = %d LIMIT 1",
1671 dbesc(datetime_convert()),
1672 dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.'.$img->getExt()),
1673 dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.'.$img->getExt()),
1674 dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.'.$img->getExt()),
1675 intval($contact['uid']),
1676 intval($contact['id'])
1681 if((is_array($contact)) && ($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
1682 $r = q("select * from contact where uid = %d and id = %d limit 1",
1683 intval($contact['uid']),
1684 intval($contact['id'])
1687 $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1688 dbesc(notags(trim($new_name))),
1689 dbesc(datetime_convert()),
1690 intval($contact['uid']),
1691 intval($contact['id'])
1694 // do our best to update the name on content items
1697 q("update item set `author-name` = '%s' where `author-name` = '%s' and `author-link` = '%s' and uid = %d",
1698 dbesc(notags(trim($new_name))),
1699 dbesc($r[0]['name']),
1700 dbesc($r[0]['url']),
1701 intval($contact['uid'])
1706 if(strlen($birthday)) {
1707 if(substr($birthday,0,4) != $contact['bdyear']) {
1708 logger('consume_feed: updating birthday: ' . $birthday);
1712 * Add new birthday event for this person
1714 * $bdtext is just a readable placeholder in case the event is shared
1715 * with others. We will replace it during presentation to our $importer
1716 * to contain a sparkle link and perhaps a photo.
1720 $bdtext = sprintf( t('%s\'s birthday'), $contact['name']);
1721 $bdtext2 = sprintf( t('Happy Birthday %s'), ' [url=' . $contact['url'] . ']' . $contact['name'] . '[/url]' ) ;
1724 $r = q("INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`summary`,`desc`,`type`)
1725 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
1726 intval($contact['uid']),
1727 intval($contact['id']),
1728 dbesc(datetime_convert()),
1729 dbesc(datetime_convert()),
1730 dbesc(datetime_convert('UTC','UTC', $birthday)),
1731 dbesc(datetime_convert('UTC','UTC', $birthday . ' + 1 day ')),
1740 q("UPDATE `contact` SET `bdyear` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1741 dbesc(substr($birthday,0,4)),
1742 intval($contact['uid']),
1743 intval($contact['id'])
1746 // This function is called twice without reloading the contact
1747 // Make sure we only create one event. This is why &$contact
1748 // is a reference var in this function
1750 $contact['bdyear'] = substr($birthday,0,4);
1755 $community_page = 0;
1756 $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'community');
1758 $community_page = intval($rawtags[0]['data']);
1760 if(is_array($contact) && intval($contact['forum']) != $community_page) {
1761 q("update contact set forum = %d where id = %d limit 1",
1762 intval($community_page),
1763 intval($contact['id'])
1765 $contact['forum'] = (string) $community_page;
1769 // process any deleted entries
1771 $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
1772 if(is_array($del_entries) && count($del_entries) && $pass != 2) {
1773 foreach($del_entries as $dentry) {
1775 if(isset($dentry['attribs']['']['ref'])) {
1776 $uri = $dentry['attribs']['']['ref'];
1778 if(isset($dentry['attribs']['']['when'])) {
1779 $when = $dentry['attribs']['']['when'];
1780 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
1783 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
1785 if($deleted && is_array($contact)) {
1786 $r = q("SELECT `item`.*, `contact`.`self` FROM `item` left join `contact` on `item`.`contact-id` = `contact`.`id`
1787 WHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d AND NOT `item`.`file` LIKE '%%[%%' LIMIT 1",
1789 intval($importer['uid']),
1790 intval($contact['id'])
1795 if(! $item['deleted'])
1796 logger('consume_feed: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
1798 if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
1799 $xo = parse_xml_string($item['object'],false);
1800 $xt = parse_xml_string($item['target'],false);
1801 if($xt->type === ACTIVITY_OBJ_NOTE) {
1802 $i = q("select * from `item` where uri = '%s' and uid = %d limit 1",
1804 intval($importer['importer_uid'])
1808 // For tags, the owner cannot remove the tag on the author's copy of the post.
1810 $owner_remove = (($item['contact-id'] == $i[0]['contact-id']) ? true: false);
1811 $author_remove = (($item['origin'] && $item['self']) ? true : false);
1812 $author_copy = (($item['origin']) ? true : false);
1814 if($owner_remove && $author_copy)
1816 if($author_remove || $owner_remove) {
1817 $tags = explode(',',$i[0]['tag']);
1820 foreach($tags as $tag)
1821 if(trim($tag) !== trim($xo->body))
1822 $newtags[] = trim($tag);
1824 q("update item set tag = '%s' where id = %d limit 1",
1825 dbesc(implode(',',$newtags)),
1833 if($item['uri'] == $item['parent-uri']) {
1834 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1835 `body` = '', `title` = ''
1836 WHERE `parent-uri` = '%s' AND `uid` = %d",
1838 dbesc(datetime_convert()),
1839 dbesc($item['uri']),
1840 intval($importer['uid'])
1844 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1845 `body` = '', `title` = ''
1846 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1848 dbesc(datetime_convert()),
1850 intval($importer['uid'])
1852 if($item['last-child']) {
1853 // ensure that last-child is set in case the comment that had it just got wiped.
1854 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
1855 dbesc(datetime_convert()),
1856 dbesc($item['parent-uri']),
1857 intval($item['uid'])
1859 // who is the last child now?
1860 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `moderated` = 0 AND `uid` = %d
1861 ORDER BY `created` DESC LIMIT 1",
1862 dbesc($item['parent-uri']),
1863 intval($importer['uid'])
1866 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
1877 // Now process the feed
1879 if($feed->get_item_quantity()) {
1881 logger('consume_feed: feed item count = ' . $feed->get_item_quantity());
1883 // in inverse date order
1885 $items = array_reverse($feed->get_items());
1887 $items = $feed->get_items();
1890 foreach($items as $item) {
1893 $item_id = $item->get_id();
1894 $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
1895 if(isset($rawthread[0]['attribs']['']['ref'])) {
1897 $parent_uri = $rawthread[0]['attribs']['']['ref'];
1900 if(($is_reply) && is_array($contact)) {
1905 // not allowed to post
1907 if($contact['rel'] == CONTACT_IS_FOLLOWER)
1911 // Have we seen it? If not, import it.
1913 $item_id = $item->get_id();
1914 $datarray = get_atom_elements($feed,$item);
1916 if((! x($datarray,'author-name')) && ($contact['network'] != NETWORK_DFRN))
1917 $datarray['author-name'] = $contact['name'];
1918 if((! x($datarray,'author-link')) && ($contact['network'] != NETWORK_DFRN))
1919 $datarray['author-link'] = $contact['url'];
1920 if((! x($datarray,'author-avatar')) && ($contact['network'] != NETWORK_DFRN))
1921 $datarray['author-avatar'] = $contact['thumb'];
1923 if((! x($datarray,'author-name')) || (! x($datarray,'author-link'))) {
1924 logger('consume_feed: no author information! ' . print_r($datarray,true));
1928 $force_parent = false;
1929 if($contact['network'] === NETWORK_OSTATUS || stristr($contact['url'],'twitter.com')) {
1930 if($contact['network'] === NETWORK_OSTATUS)
1931 $force_parent = true;
1932 if(strlen($datarray['title']))
1933 unset($datarray['title']);
1934 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1935 dbesc(datetime_convert()),
1937 intval($importer['uid'])
1939 $datarray['last-child'] = 1;
1943 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1945 intval($importer['uid'])
1948 // Update content if 'updated' changes
1951 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
1953 // do not accept (ignore) an earlier edit than one we currently have.
1954 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
1957 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1958 dbesc($datarray['title']),
1959 dbesc($datarray['body']),
1960 dbesc($datarray['tag']),
1961 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
1963 intval($importer['uid'])
1967 // update last-child if it changes
1969 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
1970 if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
1971 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1972 dbesc(datetime_convert()),
1974 intval($importer['uid'])
1976 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1977 intval($allow[0]['data']),
1978 dbesc(datetime_convert()),
1980 intval($importer['uid'])
1987 if(($contact['network'] === NETWORK_FEED) || (! strlen($contact['notify']))) {
1988 // one way feed - no remote comment ability
1989 $datarray['last-child'] = 0;
1991 $datarray['parent-uri'] = $parent_uri;
1992 $datarray['uid'] = $importer['uid'];
1993 $datarray['contact-id'] = $contact['id'];
1994 if((activity_match($datarray['verb'],ACTIVITY_LIKE)) || (activity_match($datarray['verb'],ACTIVITY_DISLIKE))) {
1995 $datarray['type'] = 'activity';
1996 $datarray['gravity'] = GRAVITY_LIKE;
1997 // only one like or dislike per person
1998 $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`parent-uri` = '%s' OR `thr-parent` = '%s') limit 1",
1999 intval($datarray['uid']),
2000 intval($datarray['contact-id']),
2001 dbesc($datarray['verb']),
2009 if(($datarray['verb'] === ACTIVITY_TAG) && ($datarray['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
2010 $xo = parse_xml_string($datarray['object'],false);
2011 $xt = parse_xml_string($datarray['target'],false);
2013 if($xt->type == ACTIVITY_OBJ_NOTE) {
2014 $r = q("select * from item where `uri` = '%s' AND `uid` = %d limit 1",
2016 intval($importer['importer_uid'])
2021 // extract tag, if not duplicate, add to parent item
2022 if($xo->id && $xo->content) {
2023 $newtag = '#[url=' . $xo->id . ']'. $xo->content . '[/url]';
2024 if(! (stristr($r[0]['tag'],$newtag))) {
2025 q("UPDATE item SET tag = '%s' WHERE id = %d LIMIT 1",
2026 dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . $newtag),
2034 $r = item_store($datarray,$force_parent);
2040 // Head post of a conversation. Have we seen it? If not, import it.
2042 $item_id = $item->get_id();
2044 $datarray = get_atom_elements($feed,$item);
2046 if(is_array($contact)) {
2047 if((! x($datarray,'author-name')) && ($contact['network'] != NETWORK_DFRN))
2048 $datarray['author-name'] = $contact['name'];
2049 if((! x($datarray,'author-link')) && ($contact['network'] != NETWORK_DFRN))
2050 $datarray['author-link'] = $contact['url'];
2051 if((! x($datarray,'author-avatar')) && ($contact['network'] != NETWORK_DFRN))
2052 $datarray['author-avatar'] = $contact['thumb'];
2055 if((! x($datarray,'author-name')) || (! x($datarray,'author-link'))) {
2056 logger('consume_feed: no author information! ' . print_r($datarray,true));
2060 // special handling for events
2062 if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
2063 $ev = bbtoevent($datarray['body']);
2064 if(x($ev,'desc') && x($ev,'start')) {
2065 $ev['uid'] = $importer['uid'];
2066 $ev['uri'] = $item_id;
2067 $ev['edited'] = $datarray['edited'];
2068 $ev['private'] = $datarray['private'];
2070 if(is_array($contact))
2071 $ev['cid'] = $contact['id'];
2072 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2074 intval($importer['uid'])
2077 $ev['id'] = $r[0]['id'];
2078 $xyz = event_store($ev);
2083 if($contact['network'] === NETWORK_OSTATUS || stristr($contact['url'],'twitter.com')) {
2084 if(strlen($datarray['title']))
2085 unset($datarray['title']);
2086 $datarray['last-child'] = 1;
2090 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2092 intval($importer['uid'])
2095 // Update content if 'updated' changes
2098 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
2100 // do not accept (ignore) an earlier edit than one we currently have.
2101 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
2104 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2105 dbesc($datarray['title']),
2106 dbesc($datarray['body']),
2107 dbesc($datarray['tag']),
2108 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
2110 intval($importer['uid'])
2114 // update last-child if it changes
2116 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
2117 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
2118 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2119 intval($allow[0]['data']),
2120 dbesc(datetime_convert()),
2122 intval($importer['uid'])
2128 if(activity_match($datarray['verb'],ACTIVITY_FOLLOW)) {
2129 logger('consume-feed: New follower');
2130 new_follower($importer,$contact,$datarray,$item);
2133 if(activity_match($datarray['verb'],ACTIVITY_UNFOLLOW)) {
2134 lose_follower($importer,$contact,$datarray,$item);
2138 if(activity_match($datarray['verb'],ACTIVITY_REQ_FRIEND)) {
2139 logger('consume-feed: New friend request');
2140 new_follower($importer,$contact,$datarray,$item,true);
2143 if(activity_match($datarray['verb'],ACTIVITY_UNFRIEND)) {
2144 lose_sharer($importer,$contact,$datarray,$item);
2149 if(! is_array($contact))
2153 if(($contact['network'] === NETWORK_FEED) || (! strlen($contact['notify']))) {
2154 // one way feed - no remote comment ability
2155 $datarray['last-child'] = 0;
2157 if($contact['network'] === NETWORK_FEED)
2158 $datarray['private'] = 2;
2160 // This is my contact on another system, but it's really me.
2161 // Turn this into a wall post.
2163 if($contact['remote_self']) {
2164 $datarray['wall'] = 1;
2165 if($contact['network'] === NETWORK_FEED) {
2166 $datarray['private'] = 0;
2170 $datarray['parent-uri'] = $item_id;
2171 $datarray['uid'] = $importer['uid'];
2172 $datarray['contact-id'] = $contact['id'];
2174 if(! link_compare($datarray['owner-link'],$contact['url'])) {
2175 // The item owner info is not our contact. It's OK and is to be expected if this is a tgroup delivery,
2176 // but otherwise there's a possible data mixup on the sender's system.
2177 // the tgroup delivery code called from item_store will correct it if it's a forum,
2178 // but we're going to unconditionally correct it here so that the post will always be owned by our contact.
2179 logger('consume_feed: Correcting item owner.', LOGGER_DEBUG);
2180 $datarray['owner-name'] = $contact['name'];
2181 $datarray['owner-link'] = $contact['url'];
2182 $datarray['owner-avatar'] = $contact['thumb'];
2185 // We've allowed "followers" to reach this point so we can decide if they are
2186 // posting an @-tag delivery, which followers are allowed to do for certain
2187 // page types. Now that we've parsed the post, let's check if it is legit. Otherwise ignore it.
2189 if(($contact['rel'] == CONTACT_IS_FOLLOWER) && (! tgroup_check($importer['uid'],$datarray)))
2193 $r = item_store($datarray);
2201 function local_delivery($importer,$data) {
2204 logger(__function__, LOGGER_TRACE);
2206 if($importer['readonly']) {
2207 // We aren't receiving stuff from this person. But we will quietly ignore them
2208 // rather than a blatant "go away" message.
2209 logger('local_delivery: ignoring');
2214 // Consume notification feed. This may differ from consuming a public feed in several ways
2215 // - might contain email or friend suggestions
2216 // - might contain remote followup to our message
2217 // - in which case we need to accept it and then notify other conversants
2218 // - we may need to send various email notifications
2220 $feed = new SimplePie();
2221 $feed->set_raw_data($data);
2222 $feed->enable_order_by_date(false);
2227 logger('local_delivery: Error parsing XML: ' . $feed->error());
2230 // Check at the feed level for updated contact name and/or photo
2234 $photo_timestamp = '';
2238 $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'owner');
2240 // Fallback should not be needed here. If it isn't DFRN it won't have DFRN updated tags
2242 // $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
2245 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
2246 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
2247 $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
2248 $new_name = $elems['name'][0]['data'];
2250 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
2251 $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
2252 $photo_url = $elems['link'][0]['attribs']['']['href'];
2256 if(($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $importer['avatar-date'])) {
2257 logger('local_delivery: Updating photo for ' . $importer['name']);
2258 require_once("include/Photo.php");
2259 $photo_failure = false;
2260 $have_photo = false;
2262 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
2263 intval($importer['id']),
2264 intval($importer['importer_uid'])
2267 $resource_id = $r[0]['resource-id'];
2271 $resource_id = photo_new_resource();
2274 $img_str = fetch_url($photo_url,true);
2275 // guess mimetype from headers or filename
2276 $type = guess_image_type($photo_url,true);
2279 $img = new Photo($img_str, $type);
2280 if($img->is_valid()) {
2282 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
2283 dbesc($resource_id),
2284 intval($importer['id']),
2285 intval($importer['importer_uid'])
2289 $img->scaleImageSquare(175);
2291 $hash = $resource_id;
2292 $r = $img->store($importer['importer_uid'], $importer['id'], $hash, basename($photo_url), 'Contact Photos', 4);
2294 $img->scaleImage(80);
2295 $r = $img->store($importer['importer_uid'], $importer['id'], $hash, basename($photo_url), 'Contact Photos', 5);
2297 $img->scaleImage(48);
2298 $r = $img->store($importer['importer_uid'], $importer['id'], $hash, basename($photo_url), 'Contact Photos', 6);
2302 q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
2303 WHERE `uid` = %d AND `id` = %d LIMIT 1",
2304 dbesc(datetime_convert()),
2305 dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.'.$img->getExt()),
2306 dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.'.$img->getExt()),
2307 dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.'.$img->getExt()),
2308 intval($importer['importer_uid']),
2309 intval($importer['id'])
2314 if(($name_updated) && (strlen($new_name)) && ($name_updated > $importer['name-date'])) {
2315 $r = q("select * from contact where uid = %d and id = %d limit 1",
2316 intval($importer['importer_uid']),
2317 intval($importer['id'])
2320 $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
2321 dbesc(notags(trim($new_name))),
2322 dbesc(datetime_convert()),
2323 intval($importer['importer_uid']),
2324 intval($importer['id'])
2327 // do our best to update the name on content items
2330 q("update item set `author-name` = '%s' where `author-name` = '%s' and `author-link` = '%s' and uid = %d",
2331 dbesc(notags(trim($new_name))),
2332 dbesc($r[0]['name']),
2333 dbesc($r[0]['url']),
2334 intval($importer['importer_uid'])
2341 // Currently unsupported - needs a lot of work
2342 $reloc = $feed->get_feed_tags( NAMESPACE_DFRN, 'relocate' );
2343 if(isset($reloc[0]['child'][NAMESPACE_DFRN])) {
2344 $base = $reloc[0]['child'][NAMESPACE_DFRN];
2346 $newloc['uid'] = $importer['importer_uid'];
2347 $newloc['cid'] = $importer['id'];
2348 $newloc['name'] = notags(unxmlify($base['name'][0]['data']));
2349 $newloc['photo'] = notags(unxmlify($base['photo'][0]['data']));
2350 $newloc['thumb'] = notags(unxmlify($base['thumb'][0]['data']));
2351 $newloc['micro'] = notags(unxmlify($base['micro'][0]['data']));
2352 $newloc['url'] = notags(unxmlify($base['url'][0]['data']));
2353 $newloc['request'] = notags(unxmlify($base['request'][0]['data']));
2354 $newloc['confirm'] = notags(unxmlify($base['confirm'][0]['data']));
2355 $newloc['notify'] = notags(unxmlify($base['notify'][0]['data']));
2356 $newloc['poll'] = notags(unxmlify($base['poll'][0]['data']));
2357 $newloc['sitepubkey'] = notags(unxmlify($base['sitepubkey'][0]['data']));
2358 /** relocated user must have original key pair */
2359 /*$newloc['pubkey'] = notags(unxmlify($base['pubkey'][0]['data']));
2360 $newloc['prvkey'] = notags(unxmlify($base['prvkey'][0]['data']));*/
2362 logger("items:relocate contact ".print_r($newloc, true).print_r($importer, true), LOGGER_DEBUG);
2365 $r = q("SELECT photo, url FROM contact WHERE id=%d AND uid=%d;",
2366 intval($importer['id']),
2367 intval($importer['importer_uid']));
2372 $x = q("UPDATE contact SET
2382 `site-pubkey` = '%s'
2383 WHERE id=%d AND uid=%d;",
2384 dbesc($newloc['name']),
2385 dbesc($newloc['photo']),
2386 dbesc($newloc['thumb']),
2387 dbesc($newloc['micro']),
2388 dbesc($newloc['url']),
2389 dbesc($newloc['request']),
2390 dbesc($newloc['confirm']),
2391 dbesc($newloc['notify']),
2392 dbesc($newloc['poll']),
2393 dbesc($newloc['sitepubkey']),
2394 intval($importer['id']),
2395 intval($importer['importer_uid']));
2401 'owner-link' => array($old['url'], $newloc['url']),
2402 'author-link' => array($old['url'], $newloc['url']),
2403 'owner-avatar' => array($old['photo'], $newloc['photo']),
2404 'author-avatar' => array($old['photo'], $newloc['photo']),
2406 foreach ($fields as $n=>$f){
2407 $x = q("UPDATE item SET `%s`='%s' WHERE `%s`='%s' AND uid=%d",
2410 intval($importer['importer_uid']));
2416 // merge with current record, current contents have priority
2417 // update record, set url-updated
2418 // update profile photos
2424 // handle friend suggestion notification
2426 $sugg = $feed->get_feed_tags( NAMESPACE_DFRN, 'suggest' );
2427 if(isset($sugg[0]['child'][NAMESPACE_DFRN])) {
2428 $base = $sugg[0]['child'][NAMESPACE_DFRN];
2430 $fsugg['uid'] = $importer['importer_uid'];
2431 $fsugg['cid'] = $importer['id'];
2432 $fsugg['name'] = notags(unxmlify($base['name'][0]['data']));
2433 $fsugg['photo'] = notags(unxmlify($base['photo'][0]['data']));
2434 $fsugg['url'] = notags(unxmlify($base['url'][0]['data']));
2435 $fsugg['request'] = notags(unxmlify($base['request'][0]['data']));
2436 $fsugg['body'] = escape_tags(unxmlify($base['note'][0]['data']));
2438 // Does our member already have a friend matching this description?
2440 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `nurl` = '%s' AND `uid` = %d LIMIT 1",
2441 dbesc($fsugg['name']),
2442 dbesc(normalise_link($fsugg['url'])),
2443 intval($fsugg['uid'])
2448 // Do we already have an fcontact record for this person?
2451 $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
2452 dbesc($fsugg['url']),
2453 dbesc($fsugg['name']),
2454 dbesc($fsugg['request'])
2459 // OK, we do. Do we already have an introduction for this person ?
2460 $r = q("select id from intro where uid = %d and fid = %d limit 1",
2461 intval($fsugg['uid']),
2468 $r = q("INSERT INTO `fcontact` ( `name`,`url`,`photo`,`request` ) VALUES ( '%s', '%s', '%s', '%s' ) ",
2469 dbesc($fsugg['name']),
2470 dbesc($fsugg['url']),
2471 dbesc($fsugg['photo']),
2472 dbesc($fsugg['request'])
2474 $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
2475 dbesc($fsugg['url']),
2476 dbesc($fsugg['name']),
2477 dbesc($fsugg['request'])
2482 // database record did not get created. Quietly give up.
2487 $hash = random_string();
2489 $r = q("INSERT INTO `intro` ( `uid`, `fid`, `contact-id`, `note`, `hash`, `datetime`, `blocked` )
2490 VALUES( %d, %d, %d, '%s', '%s', '%s', %d )",
2491 intval($fsugg['uid']),
2493 intval($fsugg['cid']),
2494 dbesc($fsugg['body']),
2496 dbesc(datetime_convert()),
2501 'type' => NOTIFY_SUGGEST,
2502 'notify_flags' => $importer['notify-flags'],
2503 'language' => $importer['language'],
2504 'to_name' => $importer['username'],
2505 'to_email' => $importer['email'],
2506 'uid' => $importer['importer_uid'],
2508 'link' => $a->get_baseurl() . '/notifications/intros',
2509 'source_name' => $importer['name'],
2510 'source_link' => $importer['url'],
2511 'source_photo' => $importer['photo'],
2512 'verb' => ACTIVITY_REQ_FRIEND,
2521 $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
2522 if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
2524 logger('local_delivery: private message received');
2527 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
2530 $msg['uid'] = $importer['importer_uid'];
2531 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
2532 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
2533 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
2534 $msg['contact-id'] = $importer['id'];
2535 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
2536 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
2538 $msg['replied'] = 0;
2539 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
2540 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
2541 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
2545 $r = dbq("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg))
2546 . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
2548 // send notifications.
2550 require_once('include/enotify.php');
2552 $notif_params = array(
2553 'type' => NOTIFY_MAIL,
2554 'notify_flags' => $importer['notify-flags'],
2555 'language' => $importer['language'],
2556 'to_name' => $importer['username'],
2557 'to_email' => $importer['email'],
2558 'uid' => $importer['importer_uid'],
2560 'source_name' => $msg['from-name'],
2561 'source_link' => $importer['url'],
2562 'source_photo' => $importer['thumb'],
2563 'verb' => ACTIVITY_POST,
2567 notification($notif_params);
2573 $community_page = 0;
2574 $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'community');
2576 $community_page = intval($rawtags[0]['data']);
2578 if(intval($importer['forum']) != $community_page) {
2579 q("update contact set forum = %d where id = %d limit 1",
2580 intval($community_page),
2581 intval($importer['id'])
2583 $importer['forum'] = (string) $community_page;
2586 logger('local_delivery: feed item count = ' . $feed->get_item_quantity());
2588 // process any deleted entries
2590 $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
2591 if(is_array($del_entries) && count($del_entries)) {
2592 foreach($del_entries as $dentry) {
2594 if(isset($dentry['attribs']['']['ref'])) {
2595 $uri = $dentry['attribs']['']['ref'];
2597 if(isset($dentry['attribs']['']['when'])) {
2598 $when = $dentry['attribs']['']['when'];
2599 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
2602 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
2606 // check for relayed deletes to our conversation
2609 $r = q("select * from item where uri = '%s' and uid = %d limit 1",
2611 intval($importer['importer_uid'])
2614 $parent_uri = $r[0]['parent-uri'];
2615 if($r[0]['id'] != $r[0]['parent'])
2622 if($importer['page-flags'] == PAGE_COMMUNITY || $importer['page-flags'] == PAGE_PRVGROUP ) {
2625 logger('local_delivery: possible community delete');
2628 $sql_extra = " and contact.self = 1 and item.wall = 1 ";
2630 // was the top-level post for this reply written by somebody on this site?
2631 // Specifically, the recipient?
2633 $is_a_remote_delete = false;
2635 // POSSIBLE CLEANUP --> Why select so many fields when only forum_mode and wall are used?
2636 $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, `item`.`forum_mode`,`item`.`origin`,`item`.`wall`,
2637 `contact`.`name`, `contact`.`url`, `contact`.`thumb` from `item`
2638 LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
2639 WHERE `item`.`uri` = '%s' AND (`item`.`parent-uri` = '%s' or `item`.`thr-parent` = '%s')
2640 AND `item`.`uid` = %d
2646 intval($importer['importer_uid'])
2649 $is_a_remote_delete = true;
2651 // Does this have the characteristics of a community or private group comment?
2652 // If it's a reply to a wall post on a community/prvgroup page it's a
2653 // valid community comment. Also forum_mode makes it valid for sure.
2654 // If neither, it's not.
2656 if($is_a_remote_delete && $community) {
2657 if((! $r[0]['forum_mode']) && (! $r[0]['wall'])) {
2658 $is_a_remote_delete = false;
2659 logger('local_delivery: not a community delete');
2663 if($is_a_remote_delete) {
2664 logger('local_delivery: received remote delete');
2668 $r = q("SELECT `item`.*, `contact`.`self` FROM `item` left join contact on `item`.`contact-id` = `contact`.`id`
2669 WHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d AND NOT `item`.`file` LIKE '%%[%%' LIMIT 1",
2671 intval($importer['importer_uid']),
2672 intval($importer['id'])
2678 if($item['deleted'])
2681 logger('local_delivery: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
2683 if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
2684 $xo = parse_xml_string($item['object'],false);
2685 $xt = parse_xml_string($item['target'],false);
2687 if($xt->type === ACTIVITY_OBJ_NOTE) {
2688 $i = q("select * from `item` where uri = '%s' and uid = %d limit 1",
2690 intval($importer['importer_uid'])
2694 // For tags, the owner cannot remove the tag on the author's copy of the post.
2696 $owner_remove = (($item['contact-id'] == $i[0]['contact-id']) ? true: false);
2697 $author_remove = (($item['origin'] && $item['self']) ? true : false);
2698 $author_copy = (($item['origin']) ? true : false);
2700 if($owner_remove && $author_copy)
2702 if($author_remove || $owner_remove) {
2703 $tags = explode(',',$i[0]['tag']);
2706 foreach($tags as $tag)
2707 if(trim($tag) !== trim($xo->body))
2708 $newtags[] = trim($tag);
2710 q("update item set tag = '%s' where id = %d limit 1",
2711 dbesc(implode(',',$newtags)),
2719 if($item['uri'] == $item['parent-uri']) {
2720 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
2721 `body` = '', `title` = ''
2722 WHERE `parent-uri` = '%s' AND `uid` = %d",
2724 dbesc(datetime_convert()),
2725 dbesc($item['uri']),
2726 intval($importer['importer_uid'])
2730 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
2731 `body` = '', `title` = ''
2732 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2734 dbesc(datetime_convert()),
2736 intval($importer['importer_uid'])
2738 if($item['last-child']) {
2739 // ensure that last-child is set in case the comment that had it just got wiped.
2740 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
2741 dbesc(datetime_convert()),
2742 dbesc($item['parent-uri']),
2743 intval($item['uid'])
2745 // who is the last child now?
2746 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
2747 ORDER BY `created` DESC LIMIT 1",
2748 dbesc($item['parent-uri']),
2749 intval($importer['importer_uid'])
2752 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
2757 // if this is a relayed delete, propagate it to other recipients
2759 if($is_a_remote_delete)
2760 proc_run('php',"include/notifier.php","drop",$item['id']);
2768 foreach($feed->get_items() as $item) {
2771 $item_id = $item->get_id();
2772 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
2773 if(isset($rawthread[0]['attribs']['']['ref'])) {
2775 $parent_uri = $rawthread[0]['attribs']['']['ref'];
2781 if($importer['page-flags'] == PAGE_COMMUNITY || $importer['page-flags'] == PAGE_PRVGROUP ) {
2784 logger('local_delivery: possible community reply');
2787 $sql_extra = " and contact.self = 1 and item.wall = 1 ";
2789 // was the top-level post for this reply written by somebody on this site?
2790 // Specifically, the recipient?
2792 $is_a_remote_comment = false;
2793 $top_uri = $parent_uri;
2795 $r = q("select `item`.`parent-uri` from `item`
2796 WHERE `item`.`uri` = '%s'
2800 if($r && count($r)) {
2801 $top_uri = $r[0]['parent-uri'];
2803 // POSSIBLE CLEANUP --> Why select so many fields when only forum_mode and wall are used?
2804 $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, `item`.`forum_mode`,`item`.`origin`,`item`.`wall`,
2805 `contact`.`name`, `contact`.`url`, `contact`.`thumb` from `item`
2806 LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
2807 WHERE `item`.`uri` = '%s' AND (`item`.`parent-uri` = '%s' or `item`.`thr-parent` = '%s')
2808 AND `item`.`uid` = %d
2814 intval($importer['importer_uid'])
2817 $is_a_remote_comment = true;
2820 // Does this have the characteristics of a community or private group comment?
2821 // If it's a reply to a wall post on a community/prvgroup page it's a
2822 // valid community comment. Also forum_mode makes it valid for sure.
2823 // If neither, it's not.
2825 if($is_a_remote_comment && $community) {
2826 if((! $r[0]['forum_mode']) && (! $r[0]['wall'])) {
2827 $is_a_remote_comment = false;
2828 logger('local_delivery: not a community reply');
2832 if($is_a_remote_comment) {
2833 logger('local_delivery: received remote comment');
2835 // remote reply to our post. Import and then notify everybody else.
2837 $datarray = get_atom_elements($feed,$item);
2839 $r = q("SELECT `id`, `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2841 intval($importer['importer_uid'])
2844 // Update content if 'updated' changes
2848 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
2850 // do not accept (ignore) an earlier edit than one we currently have.
2851 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
2854 logger('received updated comment' , LOGGER_DEBUG);
2855 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2856 dbesc($datarray['title']),
2857 dbesc($datarray['body']),
2858 dbesc($datarray['tag']),
2859 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
2861 intval($importer['importer_uid'])
2864 proc_run('php',"include/notifier.php","comment-import",$iid);
2873 $own = q("select name,url,thumb from contact where uid = %d and self = 1 limit 1",
2874 intval($importer['importer_uid'])
2878 $datarray['type'] = 'remote-comment';
2879 $datarray['wall'] = 1;
2880 $datarray['parent-uri'] = $parent_uri;
2881 $datarray['uid'] = $importer['importer_uid'];
2882 $datarray['owner-name'] = $own[0]['name'];
2883 $datarray['owner-link'] = $own[0]['url'];
2884 $datarray['owner-avatar'] = $own[0]['thumb'];
2885 $datarray['contact-id'] = $importer['id'];
2887 if(($datarray['verb'] === ACTIVITY_LIKE) || ($datarray['verb'] === ACTIVITY_DISLIKE)) {
2889 $datarray['type'] = 'activity';
2890 $datarray['gravity'] = GRAVITY_LIKE;
2891 $datarray['last-child'] = 0;
2892 // only one like or dislike per person
2893 $r = q("select id from item where uid = %d and `contact-id` = %d and verb = '%s' and (`thr-parent` = '%s' or `parent-uri` = '%s') and deleted = 0 limit 1",
2894 intval($datarray['uid']),
2895 intval($datarray['contact-id']),
2896 dbesc($datarray['verb']),
2897 dbesc($datarray['parent-uri']),
2898 dbesc($datarray['parent-uri'])
2905 if(($datarray['verb'] === ACTIVITY_TAG) && ($datarray['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
2907 $xo = parse_xml_string($datarray['object'],false);
2908 $xt = parse_xml_string($datarray['target'],false);
2910 if(($xt->type == ACTIVITY_OBJ_NOTE) && ($xt->id)) {
2912 // fetch the parent item
2914 $tagp = q("select * from item where uri = '%s' and uid = %d limit 1",
2916 intval($importer['importer_uid'])
2921 // extract tag, if not duplicate, and this user allows tags, add to parent item
2923 if($xo->id && $xo->content) {
2924 $newtag = '#[url=' . $xo->id . ']'. $xo->content . '[/url]';
2925 if(! (stristr($tagp[0]['tag'],$newtag))) {
2926 $i = q("SELECT `blocktags` FROM `user` where `uid` = %d LIMIT 1",
2927 intval($importer['importer_uid'])
2929 if(count($i) && ! intval($i[0]['blocktags'])) {
2930 q("UPDATE item SET tag = '%s', `edited` = '%s' WHERE id = %d LIMIT 1",
2931 dbesc($tagp[0]['tag'] . (strlen($tagp[0]['tag']) ? ',' : '') . $newtag),
2932 intval($tagp[0]['id']),
2933 dbesc(datetime_convert())
2942 $posted_id = item_store($datarray);
2946 $r = q("SELECT `parent`, `parent-uri` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
2948 intval($importer['importer_uid'])
2951 $parent = $r[0]['parent'];
2952 $parent_uri = $r[0]['parent-uri'];
2956 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
2957 dbesc(datetime_convert()),
2958 intval($importer['importer_uid']),
2959 intval($r[0]['parent'])
2962 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
2963 dbesc(datetime_convert()),
2964 intval($importer['importer_uid']),
2969 if($posted_id && $parent) {
2971 proc_run('php',"include/notifier.php","comment-import","$posted_id");
2973 if((! $is_like) && (! $importer['self'])) {
2975 require_once('include/enotify.php');
2978 'type' => NOTIFY_COMMENT,
2979 'notify_flags' => $importer['notify-flags'],
2980 'language' => $importer['language'],
2981 'to_name' => $importer['username'],
2982 'to_email' => $importer['email'],
2983 'uid' => $importer['importer_uid'],
2984 'item' => $datarray,
2985 'link' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
2986 'source_name' => stripslashes($datarray['author-name']),
2987 'source_link' => $datarray['author-link'],
2988 'source_photo' => ((link_compare($datarray['author-link'],$importer['url']))
2989 ? $importer['thumb'] : $datarray['author-avatar']),
2990 'verb' => ACTIVITY_POST,
2992 'parent' => $parent,
2993 'parent_uri' => $parent_uri,
3005 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
3007 $item_id = $item->get_id();
3008 $datarray = get_atom_elements($feed,$item);
3010 if($importer['rel'] == CONTACT_IS_FOLLOWER)
3013 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3015 intval($importer['importer_uid'])
3018 // Update content if 'updated' changes
3021 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
3023 // do not accept (ignore) an earlier edit than one we currently have.
3024 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
3027 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3028 dbesc($datarray['title']),
3029 dbesc($datarray['body']),
3030 dbesc($datarray['tag']),
3031 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
3033 intval($importer['importer_uid'])
3037 // update last-child if it changes
3039 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
3040 if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
3041 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
3042 dbesc(datetime_convert()),
3044 intval($importer['importer_uid'])
3046 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3047 intval($allow[0]['data']),
3048 dbesc(datetime_convert()),
3050 intval($importer['importer_uid'])
3056 $datarray['parent-uri'] = $parent_uri;
3057 $datarray['uid'] = $importer['importer_uid'];
3058 $datarray['contact-id'] = $importer['id'];
3059 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
3060 $datarray['type'] = 'activity';
3061 $datarray['gravity'] = GRAVITY_LIKE;
3062 // only one like or dislike per person
3063 $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`parent-uri` = '%s' OR `thr-parent` = '%s') limit 1",
3064 intval($datarray['uid']),
3065 intval($datarray['contact-id']),
3066 dbesc($datarray['verb']),
3075 if(($datarray['verb'] === ACTIVITY_TAG) && ($datarray['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
3077 $xo = parse_xml_string($datarray['object'],false);
3078 $xt = parse_xml_string($datarray['target'],false);
3080 if($xt->type == ACTIVITY_OBJ_NOTE) {
3081 $r = q("select * from item where `uri` = '%s' AND `uid` = %d limit 1",
3083 intval($importer['importer_uid'])
3088 // extract tag, if not duplicate, add to parent item
3090 if(! (stristr($r[0]['tag'],trim($xo->content)))) {
3091 q("UPDATE item SET tag = '%s' WHERE id = %d LIMIT 1",
3092 dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . '#[url=' . $xo->id . ']'. $xo->content . '[/url]'),
3100 $posted_id = item_store($datarray);
3102 // find out if our user is involved in this conversation and wants to be notified.
3104 if(!x($datarray['type']) || $datarray['type'] != 'activity') {
3106 $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
3108 intval($importer['importer_uid'])
3111 if(count($myconv)) {
3112 $importer_url = $a->get_baseurl() . '/profile/' . $importer['nickname'];
3114 // first make sure this isn't our own post coming back to us from a wall-to-wall event
3115 if(! link_compare($datarray['author-link'],$importer_url)) {
3118 foreach($myconv as $conv) {
3120 // now if we find a match, it means we're in this conversation
3122 if(! link_compare($conv['author-link'],$importer_url))
3125 require_once('include/enotify.php');
3127 $conv_parent = $conv['parent'];
3130 'type' => NOTIFY_COMMENT,
3131 'notify_flags' => $importer['notify-flags'],
3132 'language' => $importer['language'],
3133 'to_name' => $importer['username'],
3134 'to_email' => $importer['email'],
3135 'uid' => $importer['importer_uid'],
3136 'item' => $datarray,
3137 'link' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
3138 'source_name' => stripslashes($datarray['author-name']),
3139 'source_link' => $datarray['author-link'],
3140 'source_photo' => ((link_compare($datarray['author-link'],$importer['url']))
3141 ? $importer['thumb'] : $datarray['author-avatar']),
3142 'verb' => ACTIVITY_POST,
3144 'parent' => $conv_parent,
3145 'parent_uri' => $parent_uri
3149 // only send one notification
3161 // Head post of a conversation. Have we seen it? If not, import it.
3164 $item_id = $item->get_id();
3165 $datarray = get_atom_elements($feed,$item);
3167 if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
3168 $ev = bbtoevent($datarray['body']);
3169 if(x($ev,'desc') && x($ev,'start')) {
3170 $ev['cid'] = $importer['id'];
3171 $ev['uid'] = $importer['uid'];
3172 $ev['uri'] = $item_id;
3173 $ev['edited'] = $datarray['edited'];
3174 $ev['private'] = $datarray['private'];
3176 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3178 intval($importer['uid'])
3181 $ev['id'] = $r[0]['id'];
3182 $xyz = event_store($ev);
3187 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3189 intval($importer['importer_uid'])
3192 // Update content if 'updated' changes
3195 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
3197 // do not accept (ignore) an earlier edit than one we currently have.
3198 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
3201 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3202 dbesc($datarray['title']),
3203 dbesc($datarray['body']),
3204 dbesc($datarray['tag']),
3205 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
3207 intval($importer['importer_uid'])
3211 // update last-child if it changes
3213 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
3214 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
3215 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3216 intval($allow[0]['data']),
3217 dbesc(datetime_convert()),
3219 intval($importer['importer_uid'])
3225 // This is my contact on another system, but it's really me.
3226 // Turn this into a wall post.
3228 if($importer['remote_self'])
3229 $datarray['wall'] = 1;
3231 $datarray['parent-uri'] = $item_id;
3232 $datarray['uid'] = $importer['importer_uid'];
3233 $datarray['contact-id'] = $importer['id'];
3236 if(! link_compare($datarray['owner-link'],$importer['url'])) {
3237 // The item owner info is not our contact. It's OK and is to be expected if this is a tgroup delivery,
3238 // but otherwise there's a possible data mixup on the sender's system.
3239 // the tgroup delivery code called from item_store will correct it if it's a forum,
3240 // but we're going to unconditionally correct it here so that the post will always be owned by our contact.
3241 logger('local_delivery: Correcting item owner.', LOGGER_DEBUG);
3242 $datarray['owner-name'] = $importer['senderName'];
3243 $datarray['owner-link'] = $importer['url'];
3244 $datarray['owner-avatar'] = $importer['thumb'];
3247 if(($importer['rel'] == CONTACT_IS_FOLLOWER) && (! tgroup_check($importer['importer_uid'],$datarray)))
3250 $posted_id = item_store($datarray);
3252 if(stristr($datarray['verb'],ACTIVITY_POKE)) {
3253 $verb = urldecode(substr($datarray['verb'],strpos($datarray['verb'],'#')+1));
3256 $xo = parse_xml_string($datarray['object'],false);
3258 if(($xo->type == ACTIVITY_OBJ_PERSON) && ($xo->id)) {
3260 // somebody was poked/prodded. Was it me?
3262 $links = parse_xml_string("<links>".unxmlify($xo->link)."</links>",false);
3264 foreach($links->link as $l) {
3265 $atts = $l->attributes();
3266 switch($atts['rel']) {
3268 $Blink = $atts['href'];
3274 if($Blink && link_compare($Blink,$a->get_baseurl() . '/profile/' . $importer['nickname'])) {
3276 // send a notification
3277 require_once('include/enotify.php');
3280 'type' => NOTIFY_POKE,
3281 'notify_flags' => $importer['notify-flags'],
3282 'language' => $importer['language'],
3283 'to_name' => $importer['username'],
3284 'to_email' => $importer['email'],
3285 'uid' => $importer['importer_uid'],
3286 'item' => $datarray,
3287 'link' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
3288 'source_name' => stripslashes($datarray['author-name']),
3289 'source_link' => $datarray['author-link'],
3290 'source_photo' => ((link_compare($datarray['author-link'],$importer['url']))
3291 ? $importer['thumb'] : $datarray['author-avatar']),
3292 'verb' => $datarray['verb'],
3293 'otype' => 'person',
3294 'activity' => $verb,
3311 function new_follower($importer,$contact,$datarray,$item,$sharing = false) {
3312 $url = notags(trim($datarray['author-link']));
3313 $name = notags(trim($datarray['author-name']));
3314 $photo = notags(trim($datarray['author-avatar']));
3316 $rawtag = $item->get_item_tags(NAMESPACE_ACTIVITY,'actor');
3317 if($rawtag && $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'])
3318 $nick = $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'];
3320 if(is_array($contact)) {
3321 if(($contact['network'] == NETWORK_OSTATUS && $contact['rel'] == CONTACT_IS_SHARING)
3322 || ($sharing && $contact['rel'] == CONTACT_IS_FOLLOWER)) {
3323 $r = q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
3324 intval(CONTACT_IS_FRIEND),
3325 intval($contact['id']),
3326 intval($importer['uid'])
3329 // send email notification to owner?
3333 // create contact record
3335 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `name`, `nick`, `photo`, `network`, `rel`,
3336 `blocked`, `readonly`, `pending`, `writable` )
3337 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 1, 1 ) ",
3338 intval($importer['uid']),
3339 dbesc(datetime_convert()),
3341 dbesc(normalise_link($url)),
3345 dbesc(($sharing) ? NETWORK_ZOT : NETWORK_OSTATUS),
3346 intval(($sharing) ? CONTACT_IS_SHARING : CONTACT_IS_FOLLOWER)
3348 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `pending` = 1 LIMIT 1",
3349 intval($importer['uid']),
3353 $contact_record = $r[0];
3355 // create notification
3356 $hash = random_string();
3358 if(is_array($contact_record)) {
3359 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `hash`, `datetime`)
3360 VALUES ( %d, %d, 0, 0, '%s', '%s' )",
3361 intval($importer['uid']),
3362 intval($contact_record['id']),
3364 dbesc(datetime_convert())
3367 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
3368 intval($importer['uid'])
3373 if(intval($r[0]['def_gid'])) {
3374 require_once('include/group.php');
3375 group_add_member($r[0]['uid'],'',$contact_record['id'],$r[0]['def_gid']);
3378 if(($r[0]['notify-flags'] & NOTIFY_INTRO) && ($r[0]['page-flags'] == PAGE_NORMAL)) {
3379 $email_tpl = get_intltext_template('follow_notify_eml.tpl');
3380 $email = replace_macros($email_tpl, array(
3381 '$requestor' => ((strlen($name)) ? $name : t('[Name Withheld]')),
3383 '$myname' => $r[0]['username'],
3384 '$siteurl' => $a->get_baseurl(),
3385 '$sitename' => $a->config['sitename']
3387 $res = mail($r[0]['email'],
3388 (($sharing) ? t('A new person is sharing with you at ') : t("You have a new follower at ")) . $a->config['sitename'],
3390 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] . "\n"
3391 . 'Content-type: text/plain; charset=UTF-8' . "\n"
3392 . 'Content-transfer-encoding: 8bit' );
3399 function lose_follower($importer,$contact,$datarray,$item) {
3401 if(($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_SHARING)) {
3402 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
3403 intval(CONTACT_IS_SHARING),
3404 intval($contact['id'])
3408 contact_remove($contact['id']);
3412 function lose_sharer($importer,$contact,$datarray,$item) {
3414 if(($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_FOLLOWER)) {
3415 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
3416 intval(CONTACT_IS_FOLLOWER),
3417 intval($contact['id'])
3421 contact_remove($contact['id']);
3426 function subscribe_to_hub($url,$importer,$contact,$hubmode = 'subscribe') {
3430 if(is_array($importer)) {
3431 $r = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
3432 intval($importer['uid'])
3436 // Diaspora has different message-ids in feeds than they do
3437 // through the direct Diaspora protocol. If we try and use
3438 // the feed, we'll get duplicates. So don't.
3440 if((! count($r)) || $contact['network'] === NETWORK_DIASPORA)
3443 $push_url = get_config('system','url') . '/pubsub/' . $r[0]['nickname'] . '/' . $contact['id'];
3445 // Use a single verify token, even if multiple hubs
3447 $verify_token = ((strlen($contact['hub-verify'])) ? $contact['hub-verify'] : random_string());
3449 $params= 'hub.mode=' . $hubmode . '&hub.callback=' . urlencode($push_url) . '&hub.topic=' . urlencode($contact['poll']) . '&hub.verify=async&hub.verify_token=' . $verify_token;
3451 logger('subscribe_to_hub: ' . $hubmode . ' ' . $contact['name'] . ' to hub ' . $url . ' endpoint: ' . $push_url . ' with verifier ' . $verify_token);
3453 if(! strlen($contact['hub-verify'])) {
3454 $r = q("UPDATE `contact` SET `hub-verify` = '%s' WHERE `id` = %d LIMIT 1",
3455 dbesc($verify_token),
3456 intval($contact['id'])
3460 post_url($url,$params);
3462 logger('subscribe_to_hub: returns: ' . $a->get_curl_code(), LOGGER_DEBUG);
3469 function atom_author($tag,$name,$uri,$h,$w,$photo) {
3473 $name = xmlify($name);
3474 $uri = xmlify($uri);
3477 $photo = xmlify($photo);
3481 $o .= "<name>$name</name>\r\n";
3482 $o .= "<uri>$uri</uri>\r\n";
3483 $o .= '<link rel="photo" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
3484 $o .= '<link rel="avatar" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
3486 call_hooks('atom_author', $o);
3488 $o .= "</$tag>\r\n";
3492 function atom_entry($item,$type,$author,$owner,$comment = false,$cid = 0) {
3496 if(! $item['parent'])
3499 if($item['deleted'])
3500 return '<at:deleted-entry ref="' . xmlify($item['uri']) . '" when="' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '" />' . "\r\n";
3503 if($item['allow_cid'] || $item['allow_gid'] || $item['deny_cid'] || $item['deny_gid'])
3504 $body = fix_private_photos($item['body'],$owner['uid'],$item,$cid);
3506 $body = $item['body'];
3508 $o = "\r\n\r\n<entry>\r\n";
3510 if(is_array($author))
3511 $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb']);
3513 $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']));
3514 if(strlen($item['owner-name']))
3515 $o .= atom_author('dfrn:owner',$item['owner-name'],$item['owner-link'],80,80,$item['owner-avatar']);
3517 if(($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
3518 $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
3519 $o .= '<thr:in-reply-to ref="' . xmlify($parent_item) . '" type="text/html" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['parent']) . '" />' . "\r\n";
3522 $o .= '<id>' . xmlify($item['uri']) . '</id>' . "\r\n";
3523 $o .= '<title>' . xmlify($item['title']) . '</title>' . "\r\n";
3524 $o .= '<published>' . xmlify(datetime_convert('UTC','UTC',$item['created'] . '+00:00',ATOM_TIME)) . '</published>' . "\r\n";
3525 $o .= '<updated>' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '</updated>' . "\r\n";
3526 $o .= '<dfrn:env>' . base64url_encode($body, true) . '</dfrn:env>' . "\r\n";
3527 $o .= '<content type="' . $type . '" >' . xmlify((($type === 'html') ? bbcode($body) : $body)) . '</content>' . "\r\n";
3528 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']) . '" />' . "\r\n";
3530 $o .= '<dfrn:comment-allow>' . intval($item['last-child']) . '</dfrn:comment-allow>' . "\r\n";
3532 if($item['location']) {
3533 $o .= '<dfrn:location>' . xmlify($item['location']) . '</dfrn:location>' . "\r\n";
3534 $o .= '<poco:address><poco:formatted>' . xmlify($item['location']) . '</poco:formatted></poco:address>' . "\r\n";
3538 $o .= '<georss:point>' . xmlify($item['coord']) . '</georss:point>' . "\r\n";
3540 if(($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid']))
3541 $o .= '<dfrn:private>' . (($item['private']) ? $item['private'] : 1) . '</dfrn:private>' . "\r\n";
3544 $o .= '<dfrn:extid>' . xmlify($item['extid']) . '</dfrn:extid>' . "\r\n";
3545 if($item['bookmark'])
3546 $o .= '<dfrn:bookmark>true</dfrn:bookmark>' . "\r\n";
3549 $o .= '<statusnet:notice_info local_id="' . $item['id'] . '" source="' . xmlify($item['app']) . '" ></statusnet:notice_info>' . "\r\n";
3552 $o .= '<dfrn:diaspora_guid>' . $item['guid'] . '</dfrn:diaspora_guid>' . "\r\n";
3554 if($item['signed_text']) {
3555 $sign = base64_encode(json_encode(array('signed_text' => $item['signed_text'],'signature' => $item['signature'],'signer' => $item['signer'])));
3556 $o .= '<dfrn:diaspora_signature>' . xmlify($sign) . '</dfrn:diaspora_signature>' . "\r\n";
3559 $verb = construct_verb($item);
3560 $o .= '<as:verb>' . xmlify($verb) . '</as:verb>' . "\r\n";
3561 $actobj = construct_activity_object($item);
3564 $actarg = construct_activity_target($item);
3568 $tags = item_getfeedtags($item);
3570 foreach($tags as $t) {
3571 $o .= '<category scheme="X-DFRN:' . xmlify($t[0]) . ':' . xmlify($t[1]) . '" term="' . xmlify($t[2]) . '" />' . "\r\n";
3575 $o .= item_getfeedattach($item);
3577 $mentioned = get_mentions($item);
3581 call_hooks('atom_entry', $o);
3583 $o .= '</entry>' . "\r\n";
3588 function fix_private_photos($s, $uid, $item = null, $cid = 0) {
3591 logger('fix_private_photos', LOGGER_DEBUG);
3592 $site = substr($a->get_baseurl(),strpos($a->get_baseurl(),'://'));
3597 $img_start = strpos($orig_body, '[img');
3598 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
3599 $img_len = ($img_start !== false ? strpos(substr($orig_body, $img_start + $img_st_close + 1), '[/img]') : false);
3600 while( ($img_st_close !== false) && ($img_len !== false) ) {
3602 $img_st_close++; // make it point to AFTER the closing bracket
3603 $image = substr($orig_body, $img_start + $img_st_close, $img_len);
3605 logger('fix_private_photos: found photo ' . $image, LOGGER_DEBUG);
3608 if(stristr($image , $site . '/photo/')) {
3609 // Only embed locally hosted photos
3611 $i = basename($image);
3612 $i = str_replace(array('.jpg','.png'),array('',''),$i);
3613 $x = strpos($i,'-');
3616 $res = substr($i,$x+1);
3617 $i = substr($i,0,$x);
3618 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d AND `uid` = %d",
3625 // Check to see if we should replace this photo link with an embedded image
3626 // 1. No need to do so if the photo is public
3627 // 2. If there's a contact-id provided, see if they're in the access list
3628 // for the photo. If so, embed it.
3629 // 3. Otherwise, if we have an item, see if the item permissions match the photo
3630 // permissions, regardless of order but first check to see if they're an exact
3631 // match to save some processing overhead.
3633 if(has_permissions($r[0])) {
3635 $recips = enumerate_permissions($r[0]);
3636 if(in_array($cid, $recips)) {
3641 if(compare_permissions($item,$r[0]))
3646 $data = $r[0]['data'];
3647 $type = $r[0]['type'];
3649 // If a custom width and height were specified, apply before embedding
3650 if(preg_match("/\[img\=([0-9]*)x([0-9]*)\]/is", substr($orig_body, $img_start, $img_st_close), $match)) {
3651 logger('fix_private_photos: scaling photo', LOGGER_DEBUG);
3653 $width = intval($match[1]);
3654 $height = intval($match[2]);
3656 $ph = new Photo($data, $type);
3657 if($ph->is_valid()) {
3658 $ph->scaleImage(max($width, $height));
3659 $data = $ph->imageString();
3660 $type = $ph->getType();
3664 logger('fix_private_photos: replacing photo', LOGGER_DEBUG);
3665 $image = 'data:' . $type . ';base64,' . base64_encode($data);
3666 logger('fix_private_photos: replaced: ' . $image, LOGGER_DATA);
3672 $new_body = $new_body . substr($orig_body, 0, $img_start + $img_st_close) . $image . '[/img]';
3673 $orig_body = substr($orig_body, $img_start + $img_st_close + $img_len + strlen('[/img]'));
3674 if($orig_body === false)
3677 $img_start = strpos($orig_body, '[img');
3678 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
3679 $img_len = ($img_start !== false ? strpos(substr($orig_body, $img_start + $img_st_close + 1), '[/img]') : false);
3682 $new_body = $new_body . $orig_body;
3688 function has_permissions($obj) {
3689 if(($obj['allow_cid'] != '') || ($obj['allow_gid'] != '') || ($obj['deny_cid'] != '') || ($obj['deny_gid'] != ''))
3694 function compare_permissions($obj1,$obj2) {
3695 // first part is easy. Check that these are exactly the same.
3696 if(($obj1['allow_cid'] == $obj2['allow_cid'])
3697 && ($obj1['allow_gid'] == $obj2['allow_gid'])
3698 && ($obj1['deny_cid'] == $obj2['deny_cid'])
3699 && ($obj1['deny_gid'] == $obj2['deny_gid']))
3702 // This is harder. Parse all the permissions and compare the resulting set.
3704 $recipients1 = enumerate_permissions($obj1);
3705 $recipients2 = enumerate_permissions($obj2);
3708 if($recipients1 == $recipients2)
3713 // returns an array of contact-ids that are allowed to see this object
3715 function enumerate_permissions($obj) {
3716 require_once('include/group.php');
3717 $allow_people = expand_acl($obj['allow_cid']);
3718 $allow_groups = expand_groups(expand_acl($obj['allow_gid']));
3719 $deny_people = expand_acl($obj['deny_cid']);
3720 $deny_groups = expand_groups(expand_acl($obj['deny_gid']));
3721 $recipients = array_unique(array_merge($allow_people,$allow_groups));
3722 $deny = array_unique(array_merge($deny_people,$deny_groups));
3723 $recipients = array_diff($recipients,$deny);
3727 function item_getfeedtags($item) {
3730 $cnt = preg_match_all('|\#\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
3732 for($x = 0; $x < $cnt; $x ++) {
3734 $ret[] = array('#',$matches[1][$x], $matches[2][$x]);
3738 $cnt = preg_match_all('|\@\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
3740 for($x = 0; $x < $cnt; $x ++) {
3742 $ret[] = array('@',$matches[1][$x], $matches[2][$x]);
3748 function item_getfeedattach($item) {
3750 $arr = explode(',',$item['attach']);
3752 foreach($arr as $r) {
3754 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"\[\/attach\]|',$r,$matches);
3756 $ret .= '<link rel="enclosure" href="' . xmlify($matches[1]) . '" type="' . xmlify($matches[3]) . '" ';
3757 if(intval($matches[2]))
3758 $ret .= 'length="' . intval($matches[2]) . '" ';
3759 if($matches[4] !== ' ')
3760 $ret .= 'title="' . xmlify(trim($matches[4])) . '" ';
3761 $ret .= ' />' . "\r\n";
3770 function item_expire($uid,$days) {
3772 if((! $uid) || ($days < 1))
3775 // $expire_network_only = save your own wall posts
3776 // and just expire conversations started by others
3778 $expire_network_only = get_pconfig($uid,'expire','network_only');
3779 $sql_extra = ((intval($expire_network_only)) ? " AND wall = 0 " : "");
3781 $r = q("SELECT * FROM `item`
3783 AND `created` < UTC_TIMESTAMP() - INTERVAL %d DAY
3794 $expire_items = get_pconfig($uid, 'expire','items');
3795 $expire_items = (($expire_items===false)?1:intval($expire_items)); // default if not set: 1
3797 $expire_notes = get_pconfig($uid, 'expire','notes');
3798 $expire_notes = (($expire_notes===false)?1:intval($expire_notes)); // default if not set: 1
3800 $expire_starred = get_pconfig($uid, 'expire','starred');
3801 $expire_starred = (($expire_starred===false)?1:intval($expire_starred)); // default if not set: 1
3803 $expire_photos = get_pconfig($uid, 'expire','photos');
3804 $expire_photos = (($expire_photos===false)?0:intval($expire_photos)); // default if not set: 0
3806 logger('expire: # items=' . count($r). "; expire items: $expire_items, expire notes: $expire_notes, expire starred: $expire_starred, expire photos: $expire_photos");
3808 foreach($r as $item) {
3810 // don't expire filed items
3812 if(strpos($item['file'],'[') !== false)
3815 // Only expire posts, not photos and photo comments
3817 if($expire_photos==0 && strlen($item['resource-id']))
3819 if($expire_starred==0 && intval($item['starred']))
3821 if($expire_notes==0 && $item['type']=='note')
3823 if($expire_items==0 && $item['type']!='note')
3826 drop_item($item['id'],false);
3829 proc_run('php',"include/notifier.php","expire","$uid");
3834 function drop_items($items) {
3837 if(! local_user() && ! remote_user())
3841 foreach($items as $item) {
3842 $owner = drop_item($item,false);
3843 if($owner && ! $uid)
3848 // multiple threads may have been deleted, send an expire notification
3851 proc_run('php',"include/notifier.php","expire","$uid");
3855 function drop_item($id,$interactive = true) {
3859 // locate item to be deleted
3861 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
3868 notice( t('Item not found.') . EOL);
3869 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
3874 $owner = $item['uid'];
3878 // check if logged in user is either the author or owner of this item
3880 if(is_array($_SESSION['remote'])) {
3881 foreach($_SESSION['remote'] as $visitor) {
3882 if($visitor['uid'] == $item['uid'] && $visitor['cid'] == $item['contact-id']) {
3883 $cid = $visitor['cid'];
3890 if((local_user() == $item['uid']) || ($cid) || (! $interactive)) {
3892 logger('delete item: ' . $item['id'], LOGGER_DEBUG);
3895 $r = q("UPDATE `item` SET `deleted` = 1, `title` = '', `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
3896 dbesc(datetime_convert()),
3897 dbesc(datetime_convert()),
3901 // clean up categories and tags so they don't end up as orphans
3904 $cnt = preg_match_all('/<(.*?)>/',$item['file'],$matches,PREG_SET_ORDER);
3906 foreach($matches as $mtch) {
3907 file_tag_unsave_file($item['uid'],$item['id'],$mtch[1],true);
3913 $cnt = preg_match_all('/\[(.*?)\]/',$item['file'],$matches,PREG_SET_ORDER);
3915 foreach($matches as $mtch) {
3916 file_tag_unsave_file($item['uid'],$item['id'],$mtch[1],false);
3920 // If item is a link to a photo resource, nuke all the associated photos
3921 // (visitors will not have photo resources)
3922 // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
3923 // generate a resource-id and therefore aren't intimately linked to the item.
3925 if(strlen($item['resource-id'])) {
3926 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
3927 dbesc($item['resource-id']),
3928 intval($item['uid'])
3930 // ignore the result
3933 // If item is a link to an event, nuke the event record.
3935 if(intval($item['event-id'])) {
3936 q("DELETE FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
3937 intval($item['event-id']),
3938 intval($item['uid'])
3940 // ignore the result
3943 // clean up item_id and sign meta-data tables
3945 $r = q("DELETE FROM item_id where iid in (select id from item where parent = %d and uid = %d)",
3946 intval($item['id']),
3947 intval($item['uid'])
3950 $r = q("DELETE FROM sign where iid in (select id from item where parent = %d and uid = %d)",
3951 intval($item['id']),
3952 intval($item['uid'])
3955 // If it's the parent of a comment thread, kill all the kids
3957 if($item['uri'] == $item['parent-uri']) {
3958 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' , `title` = ''
3959 WHERE `parent-uri` = '%s' AND `uid` = %d ",
3960 dbesc(datetime_convert()),
3961 dbesc(datetime_convert()),
3962 dbesc($item['parent-uri']),
3963 intval($item['uid'])
3965 // ignore the result
3968 // ensure that last-child is set in case the comment that had it just got wiped.
3969 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
3970 dbesc(datetime_convert()),
3971 dbesc($item['parent-uri']),
3972 intval($item['uid'])
3974 // who is the last child now?
3975 $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",
3976 dbesc($item['parent-uri']),
3977 intval($item['uid'])
3980 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
3985 // Add a relayable_retraction signature for Diaspora.
3986 store_diaspora_retract_sig($item, $a->user, $a->get_baseurl());
3988 $drop_id = intval($item['id']);
3990 // send the notification upstream/downstream as the case may be
3992 proc_run('php',"include/notifier.php","drop","$drop_id");
3996 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
4002 notice( t('Permission denied.') . EOL);
4003 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
4010 function first_post_date($uid,$wall = false) {
4011 $r = q("select id, created from item
4012 where uid = %d and wall = %d and deleted = 0 and visible = 1 AND moderated = 0
4014 order by created asc limit 1",
4016 intval($wall ? 1 : 0)
4019 // logger('first_post_date: ' . $r[0]['id'] . ' ' . $r[0]['created'], LOGGER_DATA);
4020 return substr(datetime_convert('',date_default_timezone_get(),$r[0]['created']),0,10);
4025 function posted_dates($uid,$wall) {
4026 $dnow = datetime_convert('',date_default_timezone_get(),'now','Y-m-d');
4028 $dthen = first_post_date($uid,$wall);
4032 // If it's near the end of a long month, backup to the 28th so that in
4033 // consecutive loops we'll always get a whole month difference.
4035 if(intval(substr($dnow,8)) > 28)
4036 $dnow = substr($dnow,0,8) . '28';
4037 if(intval(substr($dthen,8)) > 28)
4038 $dnow = substr($dthen,0,8) . '28';
4041 // Starting with the current month, get the first and last days of every
4042 // month down to and including the month of the first post
4043 while(substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
4044 $dstart = substr($dnow,0,8) . '01';
4045 $dend = substr($dnow,0,8) . get_dim(intval($dnow),intval(substr($dnow,5)));
4046 $start_month = datetime_convert('','',$dstart,'Y-m-d');
4047 $end_month = datetime_convert('','',$dend,'Y-m-d');
4048 $str = day_translate(datetime_convert('','',$dnow,'F Y'));
4049 $ret[] = array($str,$end_month,$start_month);
4050 $dnow = datetime_convert('','',$dnow . ' -1 month', 'Y-m-d');
4056 function posted_date_widget($url,$uid,$wall) {
4059 if(! feature_enabled($uid,'archives'))
4062 // For former Facebook folks that left because of "timeline"
4064 /* if($wall && intval(get_pconfig($uid,'system','no_wall_archive_widget')))
4067 $ret = posted_dates($uid,$wall);
4071 $o = replace_macros(get_markup_template('posted_date_widget.tpl'),array(
4072 '$title' => t('Archives'),
4073 '$size' => ((count($ret) > 6) ? 6 : count($ret)),
4080 function store_diaspora_retract_sig($item, $user, $baseurl) {
4081 // Note that we can't add a target_author_signature
4082 // if the comment was deleted by a remote user. That should be ok, because if a remote user is deleting
4083 // the comment, that means we're the home of the post, and Diaspora will only
4084 // check the parent_author_signature of retractions that it doesn't have to relay further
4086 // I don't think this function gets called for an "unlike," but I'll check anyway
4088 $enabled = intval(get_config('system','diaspora_enabled'));
4090 logger('drop_item: diaspora support disabled, not storing retraction signature', LOGGER_DEBUG);
4094 logger('drop_item: storing diaspora retraction signature');
4096 $signed_text = $item['guid'] . ';' . ( ($item['verb'] === ACTIVITY_LIKE) ? 'Like' : 'Comment');
4098 if(local_user() == $item['uid']) {
4100 $handle = $user['nickname'] . '@' . substr($baseurl, strpos($baseurl,'://') + 3);
4101 $authorsig = base64_encode(rsa_sign($signed_text,$user['prvkey'],'sha256'));
4104 $r = q("SELECT `nick`, `url` FROM `contact` WHERE `id` = '%d' LIMIT 1",
4105 $item['contact-id'] // If this function gets called, drop_item() has already checked remote_user() == $item['contact-id']
4108 // The below handle only works for NETWORK_DFRN. I think that's ok, because this function
4109 // only handles DFRN deletes
4110 $handle_baseurl_start = strpos($r['url'],'://') + 3;
4111 $handle_baseurl_length = strpos($r['url'],'/profile') - $handle_baseurl_start;
4112 $handle = $r['nick'] . '@' . substr($r['url'], $handle_baseurl_start, $handle_baseurl_length);
4118 q("insert into sign (`retract_iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
4119 intval($item['id']),
4120 dbesc($signed_text),