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');
8 require_once('include/tags.php');
9 require_once('include/text.php');
11 function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
14 $sitefeed = ((strlen($owner_nick)) ? false : true); // not yet implemented, need to rewrite huge chunks of following logic
15 $public_feed = (($dfrn_id) ? false : true);
16 $starred = false; // not yet implemented, possible security issues
19 if($public_feed && $a->argc > 2) {
20 for($x = 2; $x < $a->argc; $x++) {
21 if($a->argv[$x] == 'converse')
23 if($a->argv[$x] == 'starred')
25 if($a->argv[$x] === 'category' && $a->argc > ($x + 1) && strlen($a->argv[$x+1]))
26 $category = $a->argv[$x+1];
32 // default permissions - anonymous user
34 $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
36 $r = q("SELECT `contact`.*, `user`.`uid` AS `user_uid`, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`
37 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
38 WHERE `contact`.`self` = 1 AND `user`.`nickname` = '%s' LIMIT 1",
46 $owner_id = $owner['user_uid'];
47 $owner_nick = $owner['nickname'];
49 $birthday = feed_birthday($owner_id,$owner['timezone']);
56 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
60 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
61 $my_id = '1:' . $dfrn_id;
64 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
65 $my_id = '0:' . $dfrn_id;
72 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
80 require_once('include/security.php');
81 $groups = init_groups_visitor($contact['id']);
84 for($x = 0; $x < count($groups); $x ++)
85 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
86 $gs = implode('|', $groups);
89 $gs = '<<>>' ; // Impossible to match
91 $sql_extra = sprintf("
92 AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' )
93 AND ( `deny_cid` = '' OR NOT `deny_cid` REGEXP '<%d>' )
94 AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
95 AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s')
97 intval($contact['id']),
98 intval($contact['id']),
109 if(! strlen($last_update))
110 $last_update = 'now -30 days';
112 if(isset($category)) {
113 $sql_extra .= file_tag_file_query('item',$category,'category');
118 $sql_extra .= " AND `contact`.`self` = 1 ";
121 $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
123 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
124 `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`,
125 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
126 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
127 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`,
128 `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
129 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
130 LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
131 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`parent` != 0
132 AND `item`.`wall` = 1 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
133 AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
135 ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
142 // Will check further below if this actually returned results.
143 // We will provide an empty feed if that is the case.
147 $feed_template = get_markup_template(($dfrn_id) ? 'atom_feed_dfrn.tpl' : 'atom_feed.tpl');
151 $hubxml = feed_hublinks();
153 $salmon = feed_salmonlinks($owner_nick);
155 $atom .= replace_macros($feed_template, array(
156 '$version' => xmlify(FRIENDICA_VERSION),
157 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
158 '$feed_title' => xmlify($owner['name']),
159 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', 'now' , ATOM_TIME)) ,
161 '$salmon' => $salmon,
162 '$name' => xmlify($owner['name']),
163 '$profile_page' => xmlify($owner['url']),
164 '$photo' => xmlify($owner['photo']),
165 '$thumb' => xmlify($owner['thumb']),
166 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
167 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
168 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
169 '$birthday' => ((strlen($birthday)) ? '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>' : ''),
170 '$community' => (($owner['page-flags'] == PAGE_COMMUNITY) ? '<dfrn:community>1</dfrn:community>' : '')
173 call_hooks('atom_feed', $atom);
175 if(! count($items)) {
177 call_hooks('atom_feed_end', $atom);
179 $atom .= '</feed>' . "\r\n";
183 foreach($items as $item) {
185 // prevent private email from leaking.
186 if($item['network'] === NETWORK_MAIL)
189 // public feeds get html, our own nodes use bbcode
193 // catch any email that's in a public conversation and make sure it doesn't leak
201 $atom .= atom_entry($item,$type,null,$owner,true);
204 call_hooks('atom_feed_end', $atom);
206 $atom .= '</feed>' . "\r\n";
212 function construct_verb($item) {
214 return $item['verb'];
215 return ACTIVITY_POST;
218 function construct_activity_object($item) {
220 if($item['object']) {
221 $o = '<as:object>' . "\r\n";
222 $r = parse_xml_string($item['object'],false);
228 $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
230 $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
232 $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
234 if(substr($r->link,0,1) === '<') {
235 // patch up some facebook "like" activity objects that got stored incorrectly
236 // for a couple of months prior to 9-Jun-2011 and generated bad XML.
237 // we can probably remove this hack here and in the following function in a few months time.
238 if(strstr($r->link,'&') && (! strstr($r->link,'&')))
239 $r->link = str_replace('&','&', $r->link);
240 $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
244 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
247 $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
248 $o .= '</as:object>' . "\r\n";
255 function construct_activity_target($item) {
257 if($item['target']) {
258 $o = '<as:target>' . "\r\n";
259 $r = parse_xml_string($item['target'],false);
263 $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
265 $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
267 $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
269 if(substr($r->link,0,1) === '<') {
270 if(strstr($r->link,'&') && (! strstr($r->link,'&')))
271 $r->link = str_replace('&','&', $r->link);
272 $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
276 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
279 $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
280 $o .= '</as:target>' . "\r\n";
289 * The purpose of this function is to apply system message length limits to
290 * imported messages without including any embedded photos in the length
292 if(! function_exists('limit_body_size')) {
293 function limit_body_size($body) {
295 logger('limit_body_size: start', LOGGER_DEBUG);
297 $maxlen = get_max_import_size();
299 // If the length of the body, including the embedded images, is smaller
300 // than the maximum, then don't waste time looking for the images
301 if($maxlen && (strlen($body) > $maxlen)) {
303 logger('limit_body_size: the total body length exceeds the limit', LOGGER_DEBUG);
310 $img_start = strpos($orig_body, '[img');
311 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
312 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
313 while(($img_st_close !== false) && ($img_end !== false)) {
315 $img_st_close++; // make it point to AFTER the closing bracket
316 $img_end += $img_start;
317 $img_end += strlen('[/img]');
319 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
320 // This is an embedded image
322 if( ($textlen + $img_start) > $maxlen ) {
323 if($textlen < $maxlen) {
324 logger('limit_body_size: the limit happens before an embedded image', LOGGER_DEBUG);
325 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
330 $new_body = $new_body . substr($orig_body, 0, $img_start);
331 $textlen += $img_start;
334 $new_body = $new_body . substr($orig_body, $img_start, $img_end - $img_start);
338 if( ($textlen + $img_end) > $maxlen ) {
339 if($textlen < $maxlen) {
340 logger('limit_body_size: the limit happens before the end of a non-embedded image', LOGGER_DEBUG);
341 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
346 $new_body = $new_body . substr($orig_body, 0, $img_end);
347 $textlen += $img_end;
350 $orig_body = substr($orig_body, $img_end);
352 if($orig_body === false) // in case the body ends on a closing image tag
355 $img_start = strpos($orig_body, '[img');
356 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
357 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
360 if( ($textlen + strlen($orig_body)) > $maxlen) {
361 if($textlen < $maxlen) {
362 logger('limit_body_size: the limit happens after the end of the last image', LOGGER_DEBUG);
363 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
368 logger('limit_body_size: the text size with embedded images extracted did not violate the limit', LOGGER_DEBUG);
369 $new_body = $new_body . $orig_body;
370 $textlen += strlen($orig_body);
379 function title_is_body($title, $body) {
381 $title = strip_tags($title);
382 $title = trim($title);
383 $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
384 $title = str_replace(array("\n", "\r", "\t", " "), array("","","",""), $title);
386 $body = strip_tags($body);
388 $body = html_entity_decode($body, ENT_QUOTES, 'UTF-8');
389 $body = str_replace(array("\n", "\r", "\t", " "), array("","","",""), $body);
391 if (strlen($title) < strlen($body))
392 $body = substr($body, 0, strlen($title));
394 if (($title != $body) and (substr($title, -3) == "...")) {
395 $pos = strrpos($title, "...");
397 $title = substr($title, 0, $pos);
398 $body = substr($body, 0, $pos);
402 return($title == $body);
407 function get_atom_elements($feed,$item) {
409 require_once('library/HTMLPurifier.auto.php');
410 require_once('include/html2bbcode.php');
412 $best_photo = array();
416 $author = $item->get_author();
418 $res['author-name'] = unxmlify($author->get_name());
419 $res['author-link'] = unxmlify($author->get_link());
422 $res['author-name'] = unxmlify($feed->get_title());
423 $res['author-link'] = unxmlify($feed->get_permalink());
425 $res['uri'] = unxmlify($item->get_id());
426 $res['title'] = unxmlify($item->get_title());
427 $res['body'] = unxmlify($item->get_content());
428 $res['plink'] = unxmlify($item->get_link(0));
430 // removing the content of the title if its identically to the body
431 // This helps with auto generated titles e.g. from tumblr
432 if (title_is_body($res["title"], $res["body"]))
436 $base_url = implode('/', array_slice(explode('/',$res['plink']),0,3));
440 // look for a photo. We should check media size and find the best one,
441 // but for now let's just find any author photo
443 $rawauthor = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
445 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
446 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
447 foreach($base as $link) {
448 if(!x($res, 'author-avatar') || !$res['author-avatar']) {
449 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
450 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
455 $rawactor = $item->get_item_tags(NAMESPACE_ACTIVITY, 'actor');
457 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
458 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
459 if($base && count($base)) {
460 foreach($base as $link) {
461 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
462 $res['author-link'] = unxmlify($link['attribs']['']['href']);
463 if(!x($res, 'author-avatar') || !$res['author-avatar']) {
464 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
465 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
471 // No photo/profile-link on the item - look at the feed level
473 if((! (x($res,'author-link'))) || (! (x($res,'author-avatar')))) {
474 $rawauthor = $feed->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
475 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
476 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
477 foreach($base as $link) {
478 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
479 $res['author-link'] = unxmlify($link['attribs']['']['href']);
480 if(! $res['author-avatar']) {
481 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
482 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
487 $rawactor = $feed->get_feed_tags(NAMESPACE_ACTIVITY, 'subject');
489 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
490 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
492 if($base && count($base)) {
493 foreach($base as $link) {
494 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
495 $res['author-link'] = unxmlify($link['attribs']['']['href']);
496 if(! (x($res,'author-avatar'))) {
497 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
498 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
505 $apps = $item->get_item_tags(NAMESPACE_STATUSNET,'notice_info');
506 if($apps && $apps[0]['attribs']['']['source']) {
507 $res['app'] = strip_tags(unxmlify($apps[0]['attribs']['']['source']));
508 if($res['app'] === 'web')
509 $res['app'] = 'OStatus';
512 // base64 encoded json structure representing Diaspora signature
514 $dsig = $item->get_item_tags(NAMESPACE_DFRN,'diaspora_signature');
516 $res['dsprsig'] = unxmlify($dsig[0]['data']);
519 $dguid = $item->get_item_tags(NAMESPACE_DFRN,'diaspora_guid');
521 $res['guid'] = unxmlify($dguid[0]['data']);
523 $bm = $item->get_item_tags(NAMESPACE_DFRN,'bookmark');
525 $res['bookmark'] = ((unxmlify($bm[0]['data']) === 'true') ? 1 : 0);
529 * If there's a copy of the body content which is guaranteed to have survived mangling in transit, use it.
532 $have_real_body = false;
534 $rawenv = $item->get_item_tags(NAMESPACE_DFRN, 'env');
536 $have_real_body = true;
537 $res['body'] = $rawenv[0]['data'];
538 $res['body'] = str_replace(array(' ',"\t","\r","\n"), array('','','',''),$res['body']);
539 // make sure nobody is trying to sneak some html tags by us
540 $res['body'] = notags(base64url_decode($res['body']));
544 $res['body'] = limit_body_size($res['body']);
546 // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust
547 // the content type. Our own network only emits text normally, though it might have been converted to
548 // html if we used a pubsubhubbub transport. But if we see even one html tag in our text, we will
549 // have to assume it is all html and needs to be purified.
551 // It doesn't matter all that much security wise - because before this content is used anywhere, we are
552 // going to escape any tags we find regardless, but this lets us import a limited subset of html from
553 // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining
556 if((strpos($res['body'],'<') !== false) && (strpos($res['body'],'>') !== false)) {
558 $res['body'] = reltoabs($res['body'],$base_url);
560 $res['body'] = html2bb_video($res['body']);
562 $res['body'] = oembed_html2bbcode($res['body']);
564 $config = HTMLPurifier_Config::createDefault();
565 $config->set('Cache.DefinitionImpl', null);
567 // we shouldn't need a whitelist, because the bbcode converter
568 // will strip out any unsupported tags.
570 $purifier = new HTMLPurifier($config);
571 $res['body'] = $purifier->purify($res['body']);
573 $res['body'] = @html2bbcode($res['body']);
577 elseif(! $have_real_body) {
579 // it's not one of our messages and it has no tags
580 // so it's probably just text. We'll escape it just to be safe.
582 $res['body'] = escape_tags($res['body']);
586 // this tag is obsolete but we keep it for really old sites
588 $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
589 if($allow && $allow[0]['data'] == 1)
590 $res['last-child'] = 1;
592 $res['last-child'] = 0;
594 $private = $item->get_item_tags(NAMESPACE_DFRN,'private');
595 if($private && intval($private[0]['data']) > 0)
596 $res['private'] = intval($private[0]['data']);
600 $extid = $item->get_item_tags(NAMESPACE_DFRN,'extid');
601 if($extid && $extid[0]['data'])
602 $res['extid'] = $extid[0]['data'];
604 $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
606 $res['location'] = unxmlify($rawlocation[0]['data']);
609 $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
611 $res['created'] = unxmlify($rawcreated[0]['data']);
614 $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
616 $res['edited'] = unxmlify($rawedited[0]['data']);
618 if((x($res,'edited')) && (! (x($res,'created'))))
619 $res['created'] = $res['edited'];
621 if(! $res['created'])
622 $res['created'] = $item->get_date('c');
625 $res['edited'] = $item->get_date('c');
628 // Disallow time travelling posts
630 $d1 = strtotime($res['created']);
631 $d2 = strtotime($res['edited']);
632 $d3 = strtotime('now');
635 $res['created'] = datetime_convert();
637 $res['edited'] = datetime_convert();
639 $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
640 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
641 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
642 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
643 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
644 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
645 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
646 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
647 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
649 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
650 $base = $rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
652 foreach($base as $link) {
653 if(!x($res, 'owner-avatar') || !$res['owner-avatar']) {
654 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
655 $res['owner-avatar'] = unxmlify($link['attribs']['']['href']);
660 $rawgeo = $item->get_item_tags(NAMESPACE_GEORSS,'point');
662 $res['coord'] = unxmlify($rawgeo[0]['data']);
665 $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
667 // select between supported verbs
670 $res['verb'] = unxmlify($rawverb[0]['data']);
673 // translate OStatus unfollow to activity streams if it happened to get selected
675 if((x($res,'verb')) && ($res['verb'] === 'http://ostatus.org/schema/1.0/unfollow'))
676 $res['verb'] = ACTIVITY_UNFOLLOW;
678 $cats = $item->get_categories();
681 foreach($cats as $cat) {
682 $term = $cat->get_term();
684 $term = $cat->get_label();
685 $scheme = $cat->get_scheme();
686 if($scheme && $term && stristr($scheme,'X-DFRN:'))
687 $tag_arr[] = substr($scheme,7,1) . '[url=' . unxmlify(substr($scheme,9)) . ']' . unxmlify($term) . '[/url]';
689 $tag_arr[] = notags(trim($term));
691 $res['tag'] = implode(',', $tag_arr);
694 $attach = $item->get_enclosures();
697 foreach($attach as $att) {
698 $len = intval($att->get_length());
699 $link = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_link()))));
700 $title = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_title()))));
701 $type = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_type()))));
702 if(strpos($type,';'))
703 $type = substr($type,0,strpos($type,';'));
704 if((! $link) || (strpos($link,'http') !== 0))
710 $type = 'application/octet-stream';
712 $att_arr[] = '[attach]href="' . $link . '" length="' . $len . '" type="' . $type . '" title="' . $title . '"[/attach]';
714 $res['attach'] = implode(',', $att_arr);
717 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
720 $res['object'] = '<object>' . "\n";
721 $child = $rawobj[0]['child'];
722 if($child[NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
723 $res['object-type'] = $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'];
724 $res['object'] .= '<type>' . $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
726 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'id') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
727 $res['object'] .= '<id>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
728 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'link') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
729 $res['object'] .= '<link>' . encode_rel_links($child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
730 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'title') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
731 $res['object'] .= '<title>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
732 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'content') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
733 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
735 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
736 // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
737 $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
738 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
740 $body = html2bb_video($body);
742 $config = HTMLPurifier_Config::createDefault();
743 $config->set('Cache.DefinitionImpl', null);
745 $purifier = new HTMLPurifier($config);
746 $body = $purifier->purify($body);
747 $body = html2bbcode($body);
750 $res['object'] .= '<content>' . $body . '</content>' . "\n";
753 $res['object'] .= '</object>' . "\n";
756 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'target');
759 $res['target'] = '<target>' . "\n";
760 $child = $rawobj[0]['child'];
761 if($child[NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
762 $res['target'] .= '<type>' . $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
764 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'id') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
765 $res['target'] .= '<id>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
766 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'link') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
767 $res['target'] .= '<link>' . encode_rel_links($child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
768 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'data') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
769 $res['target'] .= '<title>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
770 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'data') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
771 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
773 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
774 // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
775 $res['target'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
776 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
778 $body = html2bb_video($body);
780 $config = HTMLPurifier_Config::createDefault();
781 $config->set('Cache.DefinitionImpl', null);
783 $purifier = new HTMLPurifier($config);
784 $body = $purifier->purify($body);
785 $body = html2bbcode($body);
788 $res['target'] .= '<content>' . $body . '</content>' . "\n";
791 $res['target'] .= '</target>' . "\n";
794 // This is some experimental stuff. By now retweets are shown with "RT:"
795 // But: There is data so that the message could be shown similar to native retweets
796 // There is some better way to parse this array - but it didn't worked for me.
797 $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"];
798 if (is_array($child)) {
799 logger('get_atom_elements: Looking for status.net repeated message');
801 $message = $child["http://activitystrea.ms/spec/1.0/"]["object"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["content"][0]["data"];
802 $orig_uri = $child["http://activitystrea.ms/spec/1.0/"]["object"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["id"][0]["data"];
803 $author = $child[SIMPLEPIE_NAMESPACE_ATOM_10]["author"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10];
804 $uri = $author["uri"][0]["data"];
805 $name = $author["name"][0]["data"];
806 $avatar = @array_shift($author["link"][2]["attribs"]);
807 $avatar = $avatar["href"];
809 if (($name != "") and ($uri != "") and ($avatar != "") and ($message != "")) {
810 logger('get_atom_elements: fixing sender of repeated message.');
812 if (intval(get_config('system','new_share'))) {
813 $prefix = "[share author='".str_replace("'", "'",$name).
815 "' avatar='".$avatar.
816 "' link='".$orig_uri."']";
818 $res["body"] = $prefix.html2bbcode($message)."[/share]";
820 $res["owner-name"] = $res["author-name"];
821 $res["owner-link"] = $res["author-link"];
822 $res["owner-avatar"] = $res["author-avatar"];
824 $res["author-name"] = $name;
825 $res["author-link"] = $uri;
826 $res["author-avatar"] = $avatar;
828 $res["body"] = html2bbcode($message);
833 $arr = array('feed' => $feed, 'item' => $item, 'result' => $res);
835 call_hooks('parse_atom', $arr);
837 //if (($res["title"] != "") or (strpos($res["body"], "RT @") > 0)) {
838 //if (strpos($res["body"], "RT @") !== false) {
839 // $debugfile = tempnam("/home/ike/log", "item-res2-");
840 // file_put_contents($debugfile, serialize($arr));
846 function encode_rel_links($links) {
848 if(! ((is_array($links)) && (count($links))))
850 foreach($links as $link) {
852 if($link['attribs']['']['rel'])
853 $o .= 'rel="' . $link['attribs']['']['rel'] . '" ';
854 if($link['attribs']['']['type'])
855 $o .= 'type="' . $link['attribs']['']['type'] . '" ';
856 if($link['attribs']['']['href'])
857 $o .= 'href="' . $link['attribs']['']['href'] . '" ';
858 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['width'])
859 $o .= 'media:width="' . $link['attribs'][NAMESPACE_MEDIA]['width'] . '" ';
860 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['height'])
861 $o .= 'media:height="' . $link['attribs'][NAMESPACE_MEDIA]['height'] . '" ';
869 function item_store($arr,$force_parent = false) {
871 // If a Diaspora signature structure was passed in, pull it out of the
872 // item array and set it aside for later storage.
875 if(x($arr,'dsprsig')) {
876 $dsprsig = json_decode(base64_decode($arr['dsprsig']));
877 unset($arr['dsprsig']);
880 if(x($arr, 'gravity'))
881 $arr['gravity'] = intval($arr['gravity']);
882 elseif($arr['parent-uri'] === $arr['uri'])
884 elseif(activity_match($arr['verb'],ACTIVITY_POST))
887 $arr['gravity'] = 6; // extensible catchall
890 $arr['type'] = 'remote';
892 // Shouldn't happen but we want to make absolutely sure it doesn't leak from a plugin.
894 if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false))
895 $arr['body'] = strip_tags($arr['body']);
898 if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
899 require_once('library/langdet/Text/LanguageDetect.php');
900 $naked_body = preg_replace('/\[(.+?)\]/','',$arr['body']);
901 $l = new Text_LanguageDetect;
902 //$lng = $l->detectConfidence($naked_body);
903 //$arr['postopts'] = (($lng['language']) ? 'lang=' . $lng['language'] . ';' . $lng['confidence'] : '');
904 $lng = $l->detect($naked_body, 3);
906 if (sizeof($lng) > 0) {
909 foreach ($lng as $language => $score) {
915 $postopts .= $language.";".$score;
917 $arr['postopts'] = $postopts;
921 $arr['wall'] = ((x($arr,'wall')) ? intval($arr['wall']) : 0);
922 $arr['uri'] = ((x($arr,'uri')) ? notags(trim($arr['uri'])) : random_string());
923 $arr['extid'] = ((x($arr,'extid')) ? notags(trim($arr['extid'])) : '');
924 $arr['author-name'] = ((x($arr,'author-name')) ? notags(trim($arr['author-name'])) : '');
925 $arr['author-link'] = ((x($arr,'author-link')) ? notags(trim($arr['author-link'])) : '');
926 $arr['author-avatar'] = ((x($arr,'author-avatar')) ? notags(trim($arr['author-avatar'])) : '');
927 $arr['owner-name'] = ((x($arr,'owner-name')) ? notags(trim($arr['owner-name'])) : '');
928 $arr['owner-link'] = ((x($arr,'owner-link')) ? notags(trim($arr['owner-link'])) : '');
929 $arr['owner-avatar'] = ((x($arr,'owner-avatar')) ? notags(trim($arr['owner-avatar'])) : '');
930 $arr['created'] = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
931 $arr['edited'] = ((x($arr,'edited') !== false) ? datetime_convert('UTC','UTC',$arr['edited']) : datetime_convert());
932 $arr['commented'] = datetime_convert();
933 $arr['received'] = datetime_convert();
934 $arr['changed'] = datetime_convert();
935 $arr['title'] = ((x($arr,'title')) ? notags(trim($arr['title'])) : '');
936 $arr['location'] = ((x($arr,'location')) ? notags(trim($arr['location'])) : '');
937 $arr['coord'] = ((x($arr,'coord')) ? notags(trim($arr['coord'])) : '');
938 $arr['last-child'] = ((x($arr,'last-child')) ? intval($arr['last-child']) : 0 );
939 $arr['visible'] = ((x($arr,'visible') !== false) ? intval($arr['visible']) : 1 );
941 $arr['parent-uri'] = ((x($arr,'parent-uri')) ? notags(trim($arr['parent-uri'])) : '');
942 $arr['verb'] = ((x($arr,'verb')) ? notags(trim($arr['verb'])) : '');
943 $arr['object-type'] = ((x($arr,'object-type')) ? notags(trim($arr['object-type'])) : '');
944 $arr['object'] = ((x($arr,'object')) ? trim($arr['object']) : '');
945 $arr['target-type'] = ((x($arr,'target-type')) ? notags(trim($arr['target-type'])) : '');
946 $arr['target'] = ((x($arr,'target')) ? trim($arr['target']) : '');
947 $arr['plink'] = ((x($arr,'plink')) ? notags(trim($arr['plink'])) : '');
948 $arr['allow_cid'] = ((x($arr,'allow_cid')) ? trim($arr['allow_cid']) : '');
949 $arr['allow_gid'] = ((x($arr,'allow_gid')) ? trim($arr['allow_gid']) : '');
950 $arr['deny_cid'] = ((x($arr,'deny_cid')) ? trim($arr['deny_cid']) : '');
951 $arr['deny_gid'] = ((x($arr,'deny_gid')) ? trim($arr['deny_gid']) : '');
952 $arr['private'] = ((x($arr,'private')) ? intval($arr['private']) : 0 );
953 $arr['bookmark'] = ((x($arr,'bookmark')) ? intval($arr['bookmark']) : 0 );
954 $arr['body'] = ((x($arr,'body')) ? trim($arr['body']) : '');
955 $arr['tag'] = ((x($arr,'tag')) ? notags(trim($arr['tag'])) : '');
956 $arr['attach'] = ((x($arr,'attach')) ? notags(trim($arr['attach'])) : '');
957 $arr['app'] = ((x($arr,'app')) ? notags(trim($arr['app'])) : '');
958 $arr['origin'] = ((x($arr,'origin')) ? intval($arr['origin']) : 0 );
959 $arr['guid'] = ((x($arr,'guid')) ? notags(trim($arr['guid'])) : get_guid());
962 $arr['thr-parent'] = $arr['parent-uri'];
963 if($arr['parent-uri'] === $arr['uri']) {
966 $allow_cid = $arr['allow_cid'];
967 $allow_gid = $arr['allow_gid'];
968 $deny_cid = $arr['deny_cid'];
969 $deny_gid = $arr['deny_gid'];
973 // find the parent and snarf the item id and ACL's
974 // and anything else we need to inherit
976 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d ORDER BY `id` ASC LIMIT 1",
977 dbesc($arr['parent-uri']),
983 // is the new message multi-level threaded?
984 // even though we don't support it now, preserve the info
985 // and re-attach to the conversation parent.
987 if($r[0]['uri'] != $r[0]['parent-uri']) {
988 $arr['parent-uri'] = $r[0]['parent-uri'];
989 $z = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `parent-uri` = '%s' AND `uid` = %d
990 ORDER BY `id` ASC LIMIT 1",
991 dbesc($r[0]['parent-uri']),
992 dbesc($r[0]['parent-uri']),
999 $parent_id = $r[0]['id'];
1000 $parent_deleted = $r[0]['deleted'];
1001 $allow_cid = $r[0]['allow_cid'];
1002 $allow_gid = $r[0]['allow_gid'];
1003 $deny_cid = $r[0]['deny_cid'];
1004 $deny_gid = $r[0]['deny_gid'];
1005 $arr['wall'] = $r[0]['wall'];
1007 // if the parent is private, force privacy for the entire conversation
1008 // This differs from the above settings as it subtly allows comments from
1009 // email correspondents to be private even if the overall thread is not.
1011 if($r[0]['private'])
1012 $arr['private'] = $r[0]['private'];
1014 // Edge case. We host a public forum that was originally posted to privately.
1015 // The original author commented, but as this is a comment, the permissions
1016 // weren't fixed up so it will still show the comment as private unless we fix it here.
1018 if((intval($r[0]['forum_mode']) == 1) && (! $r[0]['private']))
1019 $arr['private'] = 0;
1023 // Allow one to see reply tweets from status.net even when
1024 // we don't have or can't see the original post.
1027 logger('item_store: $force_parent=true, reply converted to top-level post.');
1029 $arr['parent-uri'] = $arr['uri'];
1030 $arr['gravity'] = 0;
1033 logger('item_store: item parent was not found - ignoring item');
1037 $parent_deleted = 0;
1041 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1045 if($r && count($r)) {
1046 logger('item-store: duplicate item ignored. ' . print_r($arr,true));
1050 call_hooks('post_remote',$arr);
1052 if(x($arr,'cancel')) {
1053 logger('item_store: post cancelled by plugin.');
1059 logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
1061 $r = dbq("INSERT INTO `item` (`"
1062 . implode("`, `", array_keys($arr))
1064 . implode("', '", array_values($arr))
1067 // find the item we just created
1069 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d ORDER BY `id` ASC ",
1070 $arr['uri'], // already dbesc'd
1075 $current_post = $r[0]['id'];
1076 logger('item_store: created item ' . $current_post);
1077 create_tags_from_item($r[0]['id']);
1079 logger('item_store: could not locate created item');
1083 logger('item_store: duplicated post occurred. Removing duplicates.');
1084 q("DELETE FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `id` != %d ",
1086 intval($arr['uid']),
1087 intval($current_post)
1091 if((! $parent_id) || ($arr['parent-uri'] === $arr['uri']))
1092 $parent_id = $current_post;
1094 if(strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid))
1097 $private = $arr['private'];
1099 // Set parent id - and also make sure to inherit the parent's ACL's.
1101 $r = q("UPDATE `item` SET `parent` = %d, `allow_cid` = '%s', `allow_gid` = '%s',
1102 `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d, `deleted` = %d WHERE `id` = %d LIMIT 1",
1109 intval($parent_deleted),
1110 intval($current_post)
1112 create_tags_from_item($current_post);
1114 $arr['id'] = $current_post;
1115 $arr['parent'] = $parent_id;
1116 $arr['allow_cid'] = $allow_cid;
1117 $arr['allow_gid'] = $allow_gid;
1118 $arr['deny_cid'] = $deny_cid;
1119 $arr['deny_gid'] = $deny_gid;
1120 $arr['private'] = $private;
1121 $arr['deleted'] = $parent_deleted;
1122 call_hooks('post_remote_end',$arr);
1124 // update the commented timestamp on the parent
1126 q("UPDATE `item` set `commented` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
1127 dbesc(datetime_convert()),
1128 dbesc(datetime_convert()),
1133 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
1134 intval($current_post),
1135 dbesc($dsprsig->signed_text),
1136 dbesc($dsprsig->signature),
1137 dbesc($dsprsig->signer)
1143 * If this is now the last-child, force all _other_ children of this parent to *not* be last-child
1146 if($arr['last-child']) {
1147 $r = q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d AND `id` != %d",
1149 intval($arr['uid']),
1150 intval($current_post)
1154 tag_deliver($arr['uid'],$current_post);
1156 // Store the fresh generated item into the cache
1157 $cachefile = get_cachefile($arr["guid"]."-".hash("md5", $arr['body']));
1159 if (($cachefile != '') AND !file_exists($cachefile)) {
1160 $s = prepare_text($arr['body']);
1161 file_put_contents($cachefile, $s);
1162 logger('item_store: put item '.$current_post.' into cachefile '.$cachefile);
1165 return $current_post;
1168 function get_item_contact($item,$contacts) {
1169 if(! count($contacts) || (! is_array($item)))
1171 foreach($contacts as $contact) {
1172 if($contact['id'] == $item['contact-id']) {
1174 break; // NOTREACHED
1181 function tag_deliver($uid,$item_id) {
1183 // look for mention tags and setup a second delivery chain for forum/community posts if appropriate
1189 $u = q("select * from user where uid = %d limit 1",
1195 $community_page = (($u[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
1196 $prvgroup = (($u[0]['page-flags'] == PAGE_PRVGROUP) ? true : false);
1199 $i = q("select * from item where id = %d and uid = %d limit 1",
1208 $link = normalise_link($a->get_baseurl() . '/profile/' . $u[0]['nickname']);
1210 // Diaspora uses their own hardwired link URL in @-tags
1211 // instead of the one we supply with webfinger
1213 $dlink = normalise_link($a->get_baseurl() . '/u/' . $u[0]['nickname']);
1215 $cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism',$item['body'],$matches,PREG_SET_ORDER);
1217 foreach($matches as $mtch) {
1218 if(link_compare($link,$mtch[1]) || link_compare($dlink,$mtch[1])) {
1220 logger('tag_deliver: mention found: ' . $mtch[2]);
1228 // send a notification
1230 // use a local photo if we have one
1232 $r = q("select * from contact where uid = %d and nurl = '%s' limit 1",
1233 intval($u[0]['uid']),
1234 dbesc(normalise_link($item['author-link']))
1236 $photo = (($r && count($r)) ? $r[0]['thumb'] : $item['author-avatar']);
1239 require_once('include/enotify.php');
1241 'type' => NOTIFY_TAGSELF,
1242 'notify_flags' => $u[0]['notify-flags'],
1243 'language' => $u[0]['language'],
1244 'to_name' => $u[0]['username'],
1245 'to_email' => $u[0]['email'],
1246 'uid' => $u[0]['uid'],
1248 'link' => $a->get_baseurl() . '/display/' . $u[0]['nickname'] . '/' . $item['id'],
1249 'source_name' => $item['author-name'],
1250 'source_link' => $item['author-link'],
1251 'source_photo' => $photo,
1252 'verb' => ACTIVITY_TAG,
1257 $arr = array('item' => $item, 'user' => $u[0], 'contact' => $r[0]);
1259 call_hooks('tagged', $arr);
1261 if((! $community_page) && (! $prvgroup))
1265 // tgroup delivery - setup a second delivery chain
1266 // prevent delivery looping - only proceed
1267 // if the message originated elsewhere and is a top-level post
1269 if(($item['wall']) || ($item['origin']) || ($item['id'] != $item['parent']))
1272 // now change this copy of the post to a forum head message and deliver to all the tgroup members
1275 $c = q("select name, url, thumb from contact where self = 1 and uid = %d limit 1",
1276 intval($u[0]['uid'])
1281 // also reset all the privacy bits to the forum default permissions
1283 $private = ($u[0]['allow_cid'] || $u[0]['allow_gid'] || $u[0]['deny_cid'] || $u[0]['deny_gid']) ? 1 : 0;
1285 $forum_mode = (($prvgroup) ? 2 : 1);
1287 q("update item set wall = 1, origin = 1, forum_mode = %d, `owner-name` = '%s', `owner-link` = '%s', `owner-avatar` = '%s',
1288 `private` = %d, `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s' where id = %d limit 1",
1289 intval($forum_mode),
1290 dbesc($c[0]['name']),
1291 dbesc($c[0]['url']),
1292 dbesc($c[0]['thumb']),
1294 dbesc($u[0]['allow_cid']),
1295 dbesc($u[0]['allow_gid']),
1296 dbesc($u[0]['deny_cid']),
1297 dbesc($u[0]['deny_gid']),
1301 proc_run('php','include/notifier.php','tgroup',$item_id);
1307 function tgroup_check($uid,$item) {
1313 // check that the message originated elsewhere and is a top-level post
1315 if(($item['wall']) || ($item['origin']) || ($item['uri'] != $item['parent-uri']))
1319 $u = q("select * from user where uid = %d limit 1",
1325 $community_page = (($u[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
1326 $prvgroup = (($u[0]['page-flags'] == PAGE_PRVGROUP) ? true : false);
1329 $link = normalise_link($a->get_baseurl() . '/profile/' . $u[0]['nickname']);
1331 // Diaspora uses their own hardwired link URL in @-tags
1332 // instead of the one we supply with webfinger
1334 $dlink = normalise_link($a->get_baseurl() . '/u/' . $u[0]['nickname']);
1336 $cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism',$item['body'],$matches,PREG_SET_ORDER);
1338 foreach($matches as $mtch) {
1339 if(link_compare($link,$mtch[1]) || link_compare($dlink,$mtch[1])) {
1341 logger('tgroup_check: mention found: ' . $mtch[2]);
1349 if((! $community_page) && (! $prvgroup))
1363 function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
1367 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
1369 if($contact['duplex'] && $contact['dfrn-id'])
1370 $idtosend = '0:' . $orig_id;
1371 if($contact['duplex'] && $contact['issued-id'])
1372 $idtosend = '1:' . $orig_id;
1374 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
1376 $rino_enable = get_config('system','rino_encrypt');
1381 $ssl_val = intval(get_config('system','ssl_policy'));
1385 case SSL_POLICY_FULL:
1386 $ssl_policy = 'full';
1388 case SSL_POLICY_SELFSIGN:
1389 $ssl_policy = 'self';
1391 case SSL_POLICY_NONE:
1393 $ssl_policy = 'none';
1397 $url = $contact['notify'] . '&dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino=1' : '');
1399 logger('dfrn_deliver: ' . $url);
1401 $xml = fetch_url($url);
1403 $curl_stat = $a->get_curl_code();
1405 return(-1); // timed out
1407 logger('dfrn_deliver: ' . $xml, LOGGER_DATA);
1412 if(strpos($xml,'<?xml') === false) {
1413 logger('dfrn_deliver: no valid XML returned');
1414 logger('dfrn_deliver: returned XML: ' . $xml, LOGGER_DATA);
1418 $res = parse_xml_string($xml);
1420 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
1421 return (($res->status) ? $res->status : 3);
1423 $postvars = array();
1424 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
1425 $challenge = hex2bin((string) $res->challenge);
1426 $perm = (($res->perm) ? $res->perm : null);
1427 $dfrn_version = (float) (($res->dfrn_version) ? $res->dfrn_version : 2.0);
1428 $rino_allowed = ((intval($res->rino) === 1) ? 1 : 0);
1429 $page = (($owner['page-flags'] == PAGE_COMMUNITY) ? 1 : 0);
1431 if($owner['page-flags'] == PAGE_PRVGROUP)
1434 $final_dfrn_id = '';
1437 if((($perm == 'rw') && (! intval($contact['writable'])))
1438 || (($perm == 'r') && (intval($contact['writable'])))) {
1439 q("update contact set writable = %d where id = %d limit 1",
1440 intval(($perm == 'rw') ? 1 : 0),
1441 intval($contact['id'])
1443 $contact['writable'] = (string) 1 - intval($contact['writable']);
1447 if(($contact['duplex'] && strlen($contact['pubkey']))
1448 || ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
1449 || ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey']))) {
1450 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
1451 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
1454 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
1455 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
1458 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
1460 if(strpos($final_dfrn_id,':') == 1)
1461 $final_dfrn_id = substr($final_dfrn_id,2);
1463 if($final_dfrn_id != $orig_id) {
1464 logger('dfrn_deliver: wrong dfrn_id.');
1465 // did not decode properly - cannot trust this site
1469 $postvars['dfrn_id'] = $idtosend;
1470 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
1472 $postvars['dissolve'] = '1';
1475 if((($contact['rel']) && ($contact['rel'] != CONTACT_IS_SHARING) && (! $contact['blocked'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
1476 $postvars['data'] = $atom;
1477 $postvars['perm'] = 'rw';
1480 $postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
1481 $postvars['perm'] = 'r';
1484 $postvars['ssl_policy'] = $ssl_policy;
1487 $postvars['page'] = $page;
1489 if($rino && $rino_allowed && (! $dissolve)) {
1490 $key = substr(random_string(),0,16);
1491 $data = bin2hex(aes_encrypt($postvars['data'],$key));
1492 $postvars['data'] = $data;
1493 logger('rino: sent key = ' . $key, LOGGER_DEBUG);
1496 if($dfrn_version >= 2.1) {
1497 if(($contact['duplex'] && strlen($contact['pubkey']))
1498 || ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
1499 || ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey']))) {
1501 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
1504 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
1508 if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
1509 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
1512 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
1516 logger('md5 rawkey ' . md5($postvars['key']));
1518 $postvars['key'] = bin2hex($postvars['key']);
1521 logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);
1523 $xml = post_url($contact['notify'],$postvars);
1525 logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
1527 $curl_stat = $a->get_curl_code();
1528 if((! $curl_stat) || (! strlen($xml)))
1529 return(-1); // timed out
1531 if(($curl_stat == 503) && (stristr($a->get_curl_headers(),'retry-after')))
1534 if(strpos($xml,'<?xml') === false) {
1535 logger('dfrn_deliver: phase 2: no valid XML returned');
1536 logger('dfrn_deliver: phase 2: returned XML: ' . $xml, LOGGER_DATA);
1540 if($contact['term-date'] != '0000-00-00 00:00:00') {
1541 logger("dfrn_deliver: $url back from the dead - removing mark for death");
1542 require_once('include/Contact.php');
1543 unmark_for_death($contact);
1546 $res = parse_xml_string($xml);
1548 return $res->status;
1554 * consume_feed - process atom feed and update anything/everything we might need to update
1556 * $xml = the (atom) feed to consume - RSS isn't as fully supported but may work for simple feeds.
1558 * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
1559 * It is this person's stuff that is going to be updated.
1560 * $contact = the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
1561 * from an external network and MAY create an appropriate contact record. Otherwise, we MUST
1562 * have a contact record.
1563 * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or
1564 * might not) try and subscribe to it.
1565 * $datedir sorts in reverse order
1566 * $pass - by default ($pass = 0) we cannot guarantee that a parent item has been
1567 * imported prior to its children being seen in the stream unless we are certain
1568 * of how the feed is arranged/ordered.
1569 * With $pass = 1, we only pull parent items out of the stream.
1570 * With $pass = 2, we only pull children (comments/likes).
1572 * So running this twice, first with pass 1 and then with pass 2 will do the right
1573 * thing regardless of feed ordering. This won't be adequate in a fully-threaded
1574 * model where comments can have sub-threads. That would require some massive sorting
1575 * to get all the feed items into a mostly linear ordering, and might still require
1579 function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0) {
1581 require_once('library/simplepie/simplepie.inc');
1583 if(! strlen($xml)) {
1584 logger('consume_feed: empty input');
1588 $feed = new SimplePie();
1589 $feed->set_raw_data($xml);
1591 $feed->enable_order_by_date(true);
1593 $feed->enable_order_by_date(false);
1597 logger('consume_feed: Error parsing XML: ' . $feed->error());
1599 $permalink = $feed->get_permalink();
1601 // Check at the feed level for updated contact name and/or photo
1605 $photo_timestamp = '';
1609 $hubs = $feed->get_links('hub');
1610 logger('consume_feed: hubs: ' . print_r($hubs,true), LOGGER_DATA);
1613 $hub = implode(',', $hubs);
1615 $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'owner');
1617 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
1619 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
1620 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
1621 $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
1622 $new_name = $elems['name'][0]['data'];
1624 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
1625 $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
1626 $photo_url = $elems['link'][0]['attribs']['']['href'];
1629 if((x($rawtags[0]['child'], NAMESPACE_DFRN)) && (x($rawtags[0]['child'][NAMESPACE_DFRN],'birthday'))) {
1630 $birthday = datetime_convert('UTC','UTC', $rawtags[0]['child'][NAMESPACE_DFRN]['birthday'][0]['data']);
1634 if((is_array($contact)) && ($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
1635 logger('consume_feed: Updating photo for ' . $contact['name']);
1636 require_once("include/Photo.php");
1637 $photo_failure = false;
1638 $have_photo = false;
1640 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
1641 intval($contact['id']),
1642 intval($contact['uid'])
1645 $resource_id = $r[0]['resource-id'];
1649 $resource_id = photo_new_resource();
1652 $img_str = fetch_url($photo_url,true);
1653 // guess mimetype from headers or filename
1654 $type = guess_image_type($photo_url,true);
1657 $img = new Photo($img_str, $type);
1658 if($img->is_valid()) {
1660 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
1661 dbesc($resource_id),
1662 intval($contact['id']),
1663 intval($contact['uid'])
1667 $img->scaleImageSquare(175);
1669 $hash = $resource_id;
1670 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 4);
1672 $img->scaleImage(80);
1673 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 5);
1675 $img->scaleImage(48);
1676 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 6);
1680 q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
1681 WHERE `uid` = %d AND `id` = %d LIMIT 1",
1682 dbesc(datetime_convert()),
1683 dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.'.$img->getExt()),
1684 dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.'.$img->getExt()),
1685 dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.'.$img->getExt()),
1686 intval($contact['uid']),
1687 intval($contact['id'])
1692 if((is_array($contact)) && ($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
1693 $r = q("select * from contact where uid = %d and id = %d limit 1",
1694 intval($contact['uid']),
1695 intval($contact['id'])
1698 $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1699 dbesc(notags(trim($new_name))),
1700 dbesc(datetime_convert()),
1701 intval($contact['uid']),
1702 intval($contact['id'])
1705 // do our best to update the name on content items
1708 q("update item set `author-name` = '%s' where `author-name` = '%s' and `author-link` = '%s' and uid = %d",
1709 dbesc(notags(trim($new_name))),
1710 dbesc($r[0]['name']),
1711 dbesc($r[0]['url']),
1712 intval($contact['uid'])
1717 if(strlen($birthday)) {
1718 if(substr($birthday,0,4) != $contact['bdyear']) {
1719 logger('consume_feed: updating birthday: ' . $birthday);
1723 * Add new birthday event for this person
1725 * $bdtext is just a readable placeholder in case the event is shared
1726 * with others. We will replace it during presentation to our $importer
1727 * to contain a sparkle link and perhaps a photo.
1731 $bdtext = sprintf( t('%s\'s birthday'), $contact['name']);
1732 $bdtext2 = sprintf( t('Happy Birthday %s'), ' [url=' . $contact['url'] . ']' . $contact['name'] . '[/url]' ) ;
1735 $r = q("INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`summary`,`desc`,`type`)
1736 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
1737 intval($contact['uid']),
1738 intval($contact['id']),
1739 dbesc(datetime_convert()),
1740 dbesc(datetime_convert()),
1741 dbesc(datetime_convert('UTC','UTC', $birthday)),
1742 dbesc(datetime_convert('UTC','UTC', $birthday . ' + 1 day ')),
1751 q("UPDATE `contact` SET `bdyear` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1752 dbesc(substr($birthday,0,4)),
1753 intval($contact['uid']),
1754 intval($contact['id'])
1757 // This function is called twice without reloading the contact
1758 // Make sure we only create one event. This is why &$contact
1759 // is a reference var in this function
1761 $contact['bdyear'] = substr($birthday,0,4);
1766 $community_page = 0;
1767 $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'community');
1769 $community_page = intval($rawtags[0]['data']);
1771 if(is_array($contact) && intval($contact['forum']) != $community_page) {
1772 q("update contact set forum = %d where id = %d limit 1",
1773 intval($community_page),
1774 intval($contact['id'])
1776 $contact['forum'] = (string) $community_page;
1780 // process any deleted entries
1782 $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
1783 if(is_array($del_entries) && count($del_entries) && $pass != 2) {
1784 foreach($del_entries as $dentry) {
1786 if(isset($dentry['attribs']['']['ref'])) {
1787 $uri = $dentry['attribs']['']['ref'];
1789 if(isset($dentry['attribs']['']['when'])) {
1790 $when = $dentry['attribs']['']['when'];
1791 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
1794 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
1796 if($deleted && is_array($contact)) {
1797 $r = q("SELECT `item`.*, `contact`.`self` FROM `item` left join `contact` on `item`.`contact-id` = `contact`.`id`
1798 WHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d AND NOT `item`.`file` LIKE '%%[%%' LIMIT 1",
1800 intval($importer['uid']),
1801 intval($contact['id'])
1806 if(! $item['deleted'])
1807 logger('consume_feed: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
1809 if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
1810 $xo = parse_xml_string($item['object'],false);
1811 $xt = parse_xml_string($item['target'],false);
1812 if($xt->type === ACTIVITY_OBJ_NOTE) {
1813 $i = q("select * from `item` where uri = '%s' and uid = %d limit 1",
1815 intval($importer['importer_uid'])
1819 // For tags, the owner cannot remove the tag on the author's copy of the post.
1821 $owner_remove = (($item['contact-id'] == $i[0]['contact-id']) ? true: false);
1822 $author_remove = (($item['origin'] && $item['self']) ? true : false);
1823 $author_copy = (($item['origin']) ? true : false);
1825 if($owner_remove && $author_copy)
1827 if($author_remove || $owner_remove) {
1828 $tags = explode(',',$i[0]['tag']);
1831 foreach($tags as $tag)
1832 if(trim($tag) !== trim($xo->body))
1833 $newtags[] = trim($tag);
1835 q("update item set tag = '%s' where id = %d limit 1",
1836 dbesc(implode(',',$newtags)),
1839 create_tags_from_item($i[0]['id']);
1845 if($item['uri'] == $item['parent-uri']) {
1846 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1847 `body` = '', `title` = ''
1848 WHERE `parent-uri` = '%s' AND `uid` = %d",
1850 dbesc(datetime_convert()),
1851 dbesc($item['uri']),
1852 intval($importer['uid'])
1854 create_tags_from_itemuri($item['uri'], $importer['uid']);
1857 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1858 `body` = '', `title` = ''
1859 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1861 dbesc(datetime_convert()),
1863 intval($importer['uid'])
1865 create_tags_from_itemuri($uri, $importer['uid']);
1866 if($item['last-child']) {
1867 // ensure that last-child is set in case the comment that had it just got wiped.
1868 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
1869 dbesc(datetime_convert()),
1870 dbesc($item['parent-uri']),
1871 intval($item['uid'])
1873 // who is the last child now?
1874 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `moderated` = 0 AND `uid` = %d
1875 ORDER BY `created` DESC LIMIT 1",
1876 dbesc($item['parent-uri']),
1877 intval($importer['uid'])
1880 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
1891 // Now process the feed
1893 if($feed->get_item_quantity()) {
1895 logger('consume_feed: feed item count = ' . $feed->get_item_quantity());
1897 // in inverse date order
1899 $items = array_reverse($feed->get_items());
1901 $items = $feed->get_items();
1904 foreach($items as $item) {
1907 $item_id = $item->get_id();
1908 $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
1909 if(isset($rawthread[0]['attribs']['']['ref'])) {
1911 $parent_uri = $rawthread[0]['attribs']['']['ref'];
1914 if(($is_reply) && is_array($contact)) {
1919 // not allowed to post
1921 if($contact['rel'] == CONTACT_IS_FOLLOWER)
1925 // Have we seen it? If not, import it.
1927 $item_id = $item->get_id();
1928 $datarray = get_atom_elements($feed,$item);
1930 if((! x($datarray,'author-name')) && ($contact['network'] != NETWORK_DFRN))
1931 $datarray['author-name'] = $contact['name'];
1932 if((! x($datarray,'author-link')) && ($contact['network'] != NETWORK_DFRN))
1933 $datarray['author-link'] = $contact['url'];
1934 if((! x($datarray,'author-avatar')) && ($contact['network'] != NETWORK_DFRN))
1935 $datarray['author-avatar'] = $contact['thumb'];
1937 if((! x($datarray,'author-name')) || (! x($datarray,'author-link'))) {
1938 logger('consume_feed: no author information! ' . print_r($datarray,true));
1942 $force_parent = false;
1943 if($contact['network'] === NETWORK_OSTATUS || stristr($contact['url'],'twitter.com')) {
1944 if($contact['network'] === NETWORK_OSTATUS)
1945 $force_parent = true;
1946 if(strlen($datarray['title']))
1947 unset($datarray['title']);
1948 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1949 dbesc(datetime_convert()),
1951 intval($importer['uid'])
1953 $datarray['last-child'] = 1;
1957 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1959 intval($importer['uid'])
1962 // Update content if 'updated' changes
1965 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
1967 // do not accept (ignore) an earlier edit than one we currently have.
1968 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
1971 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1972 dbesc($datarray['title']),
1973 dbesc($datarray['body']),
1974 dbesc($datarray['tag']),
1975 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
1977 intval($importer['uid'])
1979 create_tags_from_itemuri($item_id, $importer['uid']);
1982 // update last-child if it changes
1984 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
1985 if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
1986 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1987 dbesc(datetime_convert()),
1989 intval($importer['uid'])
1991 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1992 intval($allow[0]['data']),
1993 dbesc(datetime_convert()),
1995 intval($importer['uid'])
2002 if(($contact['network'] === NETWORK_FEED) || (! strlen($contact['notify']))) {
2003 // one way feed - no remote comment ability
2004 $datarray['last-child'] = 0;
2006 $datarray['parent-uri'] = $parent_uri;
2007 $datarray['uid'] = $importer['uid'];
2008 $datarray['contact-id'] = $contact['id'];
2009 if((activity_match($datarray['verb'],ACTIVITY_LIKE)) || (activity_match($datarray['verb'],ACTIVITY_DISLIKE))) {
2010 $datarray['type'] = 'activity';
2011 $datarray['gravity'] = GRAVITY_LIKE;
2012 // only one like or dislike per person
2013 $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",
2014 intval($datarray['uid']),
2015 intval($datarray['contact-id']),
2016 dbesc($datarray['verb']),
2024 if(($datarray['verb'] === ACTIVITY_TAG) && ($datarray['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
2025 $xo = parse_xml_string($datarray['object'],false);
2026 $xt = parse_xml_string($datarray['target'],false);
2028 if($xt->type == ACTIVITY_OBJ_NOTE) {
2029 $r = q("select * from item where `uri` = '%s' AND `uid` = %d limit 1",
2031 intval($importer['importer_uid'])
2036 // extract tag, if not duplicate, add to parent item
2037 if($xo->id && $xo->content) {
2038 $newtag = '#[url=' . $xo->id . ']'. $xo->content . '[/url]';
2039 if(! (stristr($r[0]['tag'],$newtag))) {
2040 q("UPDATE item SET tag = '%s' WHERE id = %d LIMIT 1",
2041 dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . $newtag),
2044 create_tags_from_item($r[0]['id']);
2050 $r = item_store($datarray,$force_parent);
2056 // Head post of a conversation. Have we seen it? If not, import it.
2058 $item_id = $item->get_id();
2060 $datarray = get_atom_elements($feed,$item);
2062 if(is_array($contact)) {
2063 if((! x($datarray,'author-name')) && ($contact['network'] != NETWORK_DFRN))
2064 $datarray['author-name'] = $contact['name'];
2065 if((! x($datarray,'author-link')) && ($contact['network'] != NETWORK_DFRN))
2066 $datarray['author-link'] = $contact['url'];
2067 if((! x($datarray,'author-avatar')) && ($contact['network'] != NETWORK_DFRN))
2068 $datarray['author-avatar'] = $contact['thumb'];
2071 if((! x($datarray,'author-name')) || (! x($datarray,'author-link'))) {
2072 logger('consume_feed: no author information! ' . print_r($datarray,true));
2076 // special handling for events
2078 if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
2079 $ev = bbtoevent($datarray['body']);
2080 if(x($ev,'desc') && x($ev,'start')) {
2081 $ev['uid'] = $importer['uid'];
2082 $ev['uri'] = $item_id;
2083 $ev['edited'] = $datarray['edited'];
2084 $ev['private'] = $datarray['private'];
2086 if(is_array($contact))
2087 $ev['cid'] = $contact['id'];
2088 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2090 intval($importer['uid'])
2093 $ev['id'] = $r[0]['id'];
2094 $xyz = event_store($ev);
2099 if($contact['network'] === NETWORK_OSTATUS || stristr($contact['url'],'twitter.com')) {
2100 if(strlen($datarray['title']))
2101 unset($datarray['title']);
2102 $datarray['last-child'] = 1;
2106 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2108 intval($importer['uid'])
2111 // Update content if 'updated' changes
2114 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
2116 // do not accept (ignore) an earlier edit than one we currently have.
2117 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
2120 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2121 dbesc($datarray['title']),
2122 dbesc($datarray['body']),
2123 dbesc($datarray['tag']),
2124 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
2126 intval($importer['uid'])
2128 create_tags_from_itemuri($item_id, $importer['uid']);
2131 // update last-child if it changes
2133 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
2134 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
2135 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2136 intval($allow[0]['data']),
2137 dbesc(datetime_convert()),
2139 intval($importer['uid'])
2145 if(activity_match($datarray['verb'],ACTIVITY_FOLLOW)) {
2146 logger('consume-feed: New follower');
2147 new_follower($importer,$contact,$datarray,$item);
2150 if(activity_match($datarray['verb'],ACTIVITY_UNFOLLOW)) {
2151 lose_follower($importer,$contact,$datarray,$item);
2155 if(activity_match($datarray['verb'],ACTIVITY_REQ_FRIEND)) {
2156 logger('consume-feed: New friend request');
2157 new_follower($importer,$contact,$datarray,$item,true);
2160 if(activity_match($datarray['verb'],ACTIVITY_UNFRIEND)) {
2161 lose_sharer($importer,$contact,$datarray,$item);
2166 if(! is_array($contact))
2170 if(($contact['network'] === NETWORK_FEED) || (! strlen($contact['notify']))) {
2171 // one way feed - no remote comment ability
2172 $datarray['last-child'] = 0;
2174 if($contact['network'] === NETWORK_FEED)
2175 $datarray['private'] = 2;
2177 // This is my contact on another system, but it's really me.
2178 // Turn this into a wall post.
2180 if($contact['remote_self']) {
2181 $datarray['wall'] = 1;
2182 if($contact['network'] === NETWORK_FEED) {
2183 $datarray['private'] = 0;
2187 $datarray['parent-uri'] = $item_id;
2188 $datarray['uid'] = $importer['uid'];
2189 $datarray['contact-id'] = $contact['id'];
2191 if(! link_compare($datarray['owner-link'],$contact['url'])) {
2192 // The item owner info is not our contact. It's OK and is to be expected if this is a tgroup delivery,
2193 // but otherwise there's a possible data mixup on the sender's system.
2194 // the tgroup delivery code called from item_store will correct it if it's a forum,
2195 // but we're going to unconditionally correct it here so that the post will always be owned by our contact.
2196 logger('consume_feed: Correcting item owner.', LOGGER_DEBUG);
2197 $datarray['owner-name'] = $contact['name'];
2198 $datarray['owner-link'] = $contact['url'];
2199 $datarray['owner-avatar'] = $contact['thumb'];
2202 // We've allowed "followers" to reach this point so we can decide if they are
2203 // posting an @-tag delivery, which followers are allowed to do for certain
2204 // page types. Now that we've parsed the post, let's check if it is legit. Otherwise ignore it.
2206 if(($contact['rel'] == CONTACT_IS_FOLLOWER) && (! tgroup_check($importer['uid'],$datarray)))
2210 $r = item_store($datarray);
2218 function local_delivery($importer,$data) {
2221 logger(__function__, LOGGER_TRACE);
2223 if($importer['readonly']) {
2224 // We aren't receiving stuff from this person. But we will quietly ignore them
2225 // rather than a blatant "go away" message.
2226 logger('local_delivery: ignoring');
2231 // Consume notification feed. This may differ from consuming a public feed in several ways
2232 // - might contain email or friend suggestions
2233 // - might contain remote followup to our message
2234 // - in which case we need to accept it and then notify other conversants
2235 // - we may need to send various email notifications
2237 $feed = new SimplePie();
2238 $feed->set_raw_data($data);
2239 $feed->enable_order_by_date(false);
2244 logger('local_delivery: Error parsing XML: ' . $feed->error());
2247 // Check at the feed level for updated contact name and/or photo
2251 $photo_timestamp = '';
2255 $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'owner');
2257 // Fallback should not be needed here. If it isn't DFRN it won't have DFRN updated tags
2259 // $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
2262 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
2263 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
2264 $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
2265 $new_name = $elems['name'][0]['data'];
2267 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
2268 $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
2269 $photo_url = $elems['link'][0]['attribs']['']['href'];
2273 if(($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $importer['avatar-date'])) {
2274 logger('local_delivery: Updating photo for ' . $importer['name']);
2275 require_once("include/Photo.php");
2276 $photo_failure = false;
2277 $have_photo = false;
2279 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
2280 intval($importer['id']),
2281 intval($importer['importer_uid'])
2284 $resource_id = $r[0]['resource-id'];
2288 $resource_id = photo_new_resource();
2291 $img_str = fetch_url($photo_url,true);
2292 // guess mimetype from headers or filename
2293 $type = guess_image_type($photo_url,true);
2296 $img = new Photo($img_str, $type);
2297 if($img->is_valid()) {
2299 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
2300 dbesc($resource_id),
2301 intval($importer['id']),
2302 intval($importer['importer_uid'])
2306 $img->scaleImageSquare(175);
2308 $hash = $resource_id;
2309 $r = $img->store($importer['importer_uid'], $importer['id'], $hash, basename($photo_url), 'Contact Photos', 4);
2311 $img->scaleImage(80);
2312 $r = $img->store($importer['importer_uid'], $importer['id'], $hash, basename($photo_url), 'Contact Photos', 5);
2314 $img->scaleImage(48);
2315 $r = $img->store($importer['importer_uid'], $importer['id'], $hash, basename($photo_url), 'Contact Photos', 6);
2319 q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
2320 WHERE `uid` = %d AND `id` = %d LIMIT 1",
2321 dbesc(datetime_convert()),
2322 dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.'.$img->getExt()),
2323 dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.'.$img->getExt()),
2324 dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.'.$img->getExt()),
2325 intval($importer['importer_uid']),
2326 intval($importer['id'])
2331 if(($name_updated) && (strlen($new_name)) && ($name_updated > $importer['name-date'])) {
2332 $r = q("select * from contact where uid = %d and id = %d limit 1",
2333 intval($importer['importer_uid']),
2334 intval($importer['id'])
2337 $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
2338 dbesc(notags(trim($new_name))),
2339 dbesc(datetime_convert()),
2340 intval($importer['importer_uid']),
2341 intval($importer['id'])
2344 // do our best to update the name on content items
2347 q("update item set `author-name` = '%s' where `author-name` = '%s' and `author-link` = '%s' and uid = %d",
2348 dbesc(notags(trim($new_name))),
2349 dbesc($r[0]['name']),
2350 dbesc($r[0]['url']),
2351 intval($importer['importer_uid'])
2358 // Currently unsupported - needs a lot of work
2359 $reloc = $feed->get_feed_tags( NAMESPACE_DFRN, 'relocate' );
2360 if(isset($reloc[0]['child'][NAMESPACE_DFRN])) {
2361 $base = $reloc[0]['child'][NAMESPACE_DFRN];
2363 $newloc['uid'] = $importer['importer_uid'];
2364 $newloc['cid'] = $importer['id'];
2365 $newloc['name'] = notags(unxmlify($base['name'][0]['data']));
2366 $newloc['photo'] = notags(unxmlify($base['photo'][0]['data']));
2367 $newloc['thumb'] = notags(unxmlify($base['thumb'][0]['data']));
2368 $newloc['micro'] = notags(unxmlify($base['micro'][0]['data']));
2369 $newloc['url'] = notags(unxmlify($base['url'][0]['data']));
2370 $newloc['request'] = notags(unxmlify($base['request'][0]['data']));
2371 $newloc['confirm'] = notags(unxmlify($base['confirm'][0]['data']));
2372 $newloc['notify'] = notags(unxmlify($base['notify'][0]['data']));
2373 $newloc['poll'] = notags(unxmlify($base['poll'][0]['data']));
2374 $newloc['sitepubkey'] = notags(unxmlify($base['sitepubkey'][0]['data']));
2375 /** relocated user must have original key pair */
2376 /*$newloc['pubkey'] = notags(unxmlify($base['pubkey'][0]['data']));
2377 $newloc['prvkey'] = notags(unxmlify($base['prvkey'][0]['data']));*/
2379 logger("items:relocate contact ".print_r($newloc, true).print_r($importer, true), LOGGER_DEBUG);
2382 $r = q("SELECT photo, url FROM contact WHERE id=%d AND uid=%d;",
2383 intval($importer['id']),
2384 intval($importer['importer_uid']));
2389 $x = q("UPDATE contact SET
2399 `site-pubkey` = '%s'
2400 WHERE id=%d AND uid=%d;",
2401 dbesc($newloc['name']),
2402 dbesc($newloc['photo']),
2403 dbesc($newloc['thumb']),
2404 dbesc($newloc['micro']),
2405 dbesc($newloc['url']),
2406 dbesc($newloc['request']),
2407 dbesc($newloc['confirm']),
2408 dbesc($newloc['notify']),
2409 dbesc($newloc['poll']),
2410 dbesc($newloc['sitepubkey']),
2411 intval($importer['id']),
2412 intval($importer['importer_uid']));
2418 'owner-link' => array($old['url'], $newloc['url']),
2419 'author-link' => array($old['url'], $newloc['url']),
2420 'owner-avatar' => array($old['photo'], $newloc['photo']),
2421 'author-avatar' => array($old['photo'], $newloc['photo']),
2423 foreach ($fields as $n=>$f){
2424 $x = q("UPDATE item SET `%s`='%s' WHERE `%s`='%s' AND uid=%d",
2427 intval($importer['importer_uid']));
2433 // merge with current record, current contents have priority
2434 // update record, set url-updated
2435 // update profile photos
2441 // handle friend suggestion notification
2443 $sugg = $feed->get_feed_tags( NAMESPACE_DFRN, 'suggest' );
2444 if(isset($sugg[0]['child'][NAMESPACE_DFRN])) {
2445 $base = $sugg[0]['child'][NAMESPACE_DFRN];
2447 $fsugg['uid'] = $importer['importer_uid'];
2448 $fsugg['cid'] = $importer['id'];
2449 $fsugg['name'] = notags(unxmlify($base['name'][0]['data']));
2450 $fsugg['photo'] = notags(unxmlify($base['photo'][0]['data']));
2451 $fsugg['url'] = notags(unxmlify($base['url'][0]['data']));
2452 $fsugg['request'] = notags(unxmlify($base['request'][0]['data']));
2453 $fsugg['body'] = escape_tags(unxmlify($base['note'][0]['data']));
2455 // Does our member already have a friend matching this description?
2457 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `nurl` = '%s' AND `uid` = %d LIMIT 1",
2458 dbesc($fsugg['name']),
2459 dbesc(normalise_link($fsugg['url'])),
2460 intval($fsugg['uid'])
2465 // Do we already have an fcontact record for this person?
2468 $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
2469 dbesc($fsugg['url']),
2470 dbesc($fsugg['name']),
2471 dbesc($fsugg['request'])
2476 // OK, we do. Do we already have an introduction for this person ?
2477 $r = q("select id from intro where uid = %d and fid = %d limit 1",
2478 intval($fsugg['uid']),
2485 $r = q("INSERT INTO `fcontact` ( `name`,`url`,`photo`,`request` ) VALUES ( '%s', '%s', '%s', '%s' ) ",
2486 dbesc($fsugg['name']),
2487 dbesc($fsugg['url']),
2488 dbesc($fsugg['photo']),
2489 dbesc($fsugg['request'])
2491 $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
2492 dbesc($fsugg['url']),
2493 dbesc($fsugg['name']),
2494 dbesc($fsugg['request'])
2499 // database record did not get created. Quietly give up.
2504 $hash = random_string();
2506 $r = q("INSERT INTO `intro` ( `uid`, `fid`, `contact-id`, `note`, `hash`, `datetime`, `blocked` )
2507 VALUES( %d, %d, %d, '%s', '%s', '%s', %d )",
2508 intval($fsugg['uid']),
2510 intval($fsugg['cid']),
2511 dbesc($fsugg['body']),
2513 dbesc(datetime_convert()),
2518 'type' => NOTIFY_SUGGEST,
2519 'notify_flags' => $importer['notify-flags'],
2520 'language' => $importer['language'],
2521 'to_name' => $importer['username'],
2522 'to_email' => $importer['email'],
2523 'uid' => $importer['importer_uid'],
2525 'link' => $a->get_baseurl() . '/notifications/intros',
2526 'source_name' => $importer['name'],
2527 'source_link' => $importer['url'],
2528 'source_photo' => $importer['photo'],
2529 'verb' => ACTIVITY_REQ_FRIEND,
2538 $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
2539 if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
2541 logger('local_delivery: private message received');
2544 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
2547 $msg['uid'] = $importer['importer_uid'];
2548 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
2549 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
2550 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
2551 $msg['contact-id'] = $importer['id'];
2552 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
2553 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
2555 $msg['replied'] = 0;
2556 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
2557 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
2558 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
2562 $r = dbq("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg))
2563 . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
2565 // send notifications.
2567 require_once('include/enotify.php');
2569 $notif_params = array(
2570 'type' => NOTIFY_MAIL,
2571 'notify_flags' => $importer['notify-flags'],
2572 'language' => $importer['language'],
2573 'to_name' => $importer['username'],
2574 'to_email' => $importer['email'],
2575 'uid' => $importer['importer_uid'],
2577 'source_name' => $msg['from-name'],
2578 'source_link' => $importer['url'],
2579 'source_photo' => $importer['thumb'],
2580 'verb' => ACTIVITY_POST,
2584 notification($notif_params);
2590 $community_page = 0;
2591 $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'community');
2593 $community_page = intval($rawtags[0]['data']);
2595 if(intval($importer['forum']) != $community_page) {
2596 q("update contact set forum = %d where id = %d limit 1",
2597 intval($community_page),
2598 intval($importer['id'])
2600 $importer['forum'] = (string) $community_page;
2603 logger('local_delivery: feed item count = ' . $feed->get_item_quantity());
2605 // process any deleted entries
2607 $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
2608 if(is_array($del_entries) && count($del_entries)) {
2609 foreach($del_entries as $dentry) {
2611 if(isset($dentry['attribs']['']['ref'])) {
2612 $uri = $dentry['attribs']['']['ref'];
2614 if(isset($dentry['attribs']['']['when'])) {
2615 $when = $dentry['attribs']['']['when'];
2616 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
2619 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
2623 // check for relayed deletes to our conversation
2626 $r = q("select * from item where uri = '%s' and uid = %d limit 1",
2628 intval($importer['importer_uid'])
2631 $parent_uri = $r[0]['parent-uri'];
2632 if($r[0]['id'] != $r[0]['parent'])
2639 if($importer['page-flags'] == PAGE_COMMUNITY || $importer['page-flags'] == PAGE_PRVGROUP ) {
2642 logger('local_delivery: possible community delete');
2645 $sql_extra = " and contact.self = 1 and item.wall = 1 ";
2647 // was the top-level post for this reply written by somebody on this site?
2648 // Specifically, the recipient?
2650 $is_a_remote_delete = false;
2652 // POSSIBLE CLEANUP --> Why select so many fields when only forum_mode and wall are used?
2653 $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, `item`.`forum_mode`,`item`.`origin`,`item`.`wall`,
2654 `contact`.`name`, `contact`.`url`, `contact`.`thumb` from `item`
2655 LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
2656 WHERE `item`.`uri` = '%s' AND (`item`.`parent-uri` = '%s' or `item`.`thr-parent` = '%s')
2657 AND `item`.`uid` = %d
2663 intval($importer['importer_uid'])
2666 $is_a_remote_delete = true;
2668 // Does this have the characteristics of a community or private group comment?
2669 // If it's a reply to a wall post on a community/prvgroup page it's a
2670 // valid community comment. Also forum_mode makes it valid for sure.
2671 // If neither, it's not.
2673 if($is_a_remote_delete && $community) {
2674 if((! $r[0]['forum_mode']) && (! $r[0]['wall'])) {
2675 $is_a_remote_delete = false;
2676 logger('local_delivery: not a community delete');
2680 if($is_a_remote_delete) {
2681 logger('local_delivery: received remote delete');
2685 $r = q("SELECT `item`.*, `contact`.`self` FROM `item` left join contact on `item`.`contact-id` = `contact`.`id`
2686 WHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d AND NOT `item`.`file` LIKE '%%[%%' LIMIT 1",
2688 intval($importer['importer_uid']),
2689 intval($importer['id'])
2695 if($item['deleted'])
2698 logger('local_delivery: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
2700 if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
2701 $xo = parse_xml_string($item['object'],false);
2702 $xt = parse_xml_string($item['target'],false);
2704 if($xt->type === ACTIVITY_OBJ_NOTE) {
2705 $i = q("select * from `item` where uri = '%s' and uid = %d limit 1",
2707 intval($importer['importer_uid'])
2711 // For tags, the owner cannot remove the tag on the author's copy of the post.
2713 $owner_remove = (($item['contact-id'] == $i[0]['contact-id']) ? true: false);
2714 $author_remove = (($item['origin'] && $item['self']) ? true : false);
2715 $author_copy = (($item['origin']) ? true : false);
2717 if($owner_remove && $author_copy)
2719 if($author_remove || $owner_remove) {
2720 $tags = explode(',',$i[0]['tag']);
2723 foreach($tags as $tag)
2724 if(trim($tag) !== trim($xo->body))
2725 $newtags[] = trim($tag);
2727 q("update item set tag = '%s' where id = %d limit 1",
2728 dbesc(implode(',',$newtags)),
2731 create_tags_from_item($i[0]['id']);
2737 if($item['uri'] == $item['parent-uri']) {
2738 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
2739 `body` = '', `title` = ''
2740 WHERE `parent-uri` = '%s' AND `uid` = %d",
2742 dbesc(datetime_convert()),
2743 dbesc($item['uri']),
2744 intval($importer['importer_uid'])
2746 create_tags_from_itemuri($item['uri'], $importer['importer_uid']);
2749 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
2750 `body` = '', `title` = ''
2751 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2753 dbesc(datetime_convert()),
2755 intval($importer['importer_uid'])
2757 create_tags_from_itemuri($uri, $importer['importer_uid']);
2758 if($item['last-child']) {
2759 // ensure that last-child is set in case the comment that had it just got wiped.
2760 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
2761 dbesc(datetime_convert()),
2762 dbesc($item['parent-uri']),
2763 intval($item['uid'])
2765 // who is the last child now?
2766 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
2767 ORDER BY `created` DESC LIMIT 1",
2768 dbesc($item['parent-uri']),
2769 intval($importer['importer_uid'])
2772 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
2777 // if this is a relayed delete, propagate it to other recipients
2779 if($is_a_remote_delete)
2780 proc_run('php',"include/notifier.php","drop",$item['id']);
2788 foreach($feed->get_items() as $item) {
2791 $item_id = $item->get_id();
2792 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
2793 if(isset($rawthread[0]['attribs']['']['ref'])) {
2795 $parent_uri = $rawthread[0]['attribs']['']['ref'];
2801 if($importer['page-flags'] == PAGE_COMMUNITY || $importer['page-flags'] == PAGE_PRVGROUP ) {
2804 logger('local_delivery: possible community reply');
2807 $sql_extra = " and contact.self = 1 and item.wall = 1 ";
2809 // was the top-level post for this reply written by somebody on this site?
2810 // Specifically, the recipient?
2812 $is_a_remote_comment = false;
2813 $top_uri = $parent_uri;
2815 $r = q("select `item`.`parent-uri` from `item`
2816 WHERE `item`.`uri` = '%s'
2820 if($r && count($r)) {
2821 $top_uri = $r[0]['parent-uri'];
2823 // POSSIBLE CLEANUP --> Why select so many fields when only forum_mode and wall are used?
2824 $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, `item`.`forum_mode`,`item`.`origin`,`item`.`wall`,
2825 `contact`.`name`, `contact`.`url`, `contact`.`thumb` from `item`
2826 LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
2827 WHERE `item`.`uri` = '%s' AND (`item`.`parent-uri` = '%s' or `item`.`thr-parent` = '%s')
2828 AND `item`.`uid` = %d
2834 intval($importer['importer_uid'])
2837 $is_a_remote_comment = true;
2840 // Does this have the characteristics of a community or private group comment?
2841 // If it's a reply to a wall post on a community/prvgroup page it's a
2842 // valid community comment. Also forum_mode makes it valid for sure.
2843 // If neither, it's not.
2845 if($is_a_remote_comment && $community) {
2846 if((! $r[0]['forum_mode']) && (! $r[0]['wall'])) {
2847 $is_a_remote_comment = false;
2848 logger('local_delivery: not a community reply');
2852 if($is_a_remote_comment) {
2853 logger('local_delivery: received remote comment');
2855 // remote reply to our post. Import and then notify everybody else.
2857 $datarray = get_atom_elements($feed,$item);
2859 $r = q("SELECT `id`, `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2861 intval($importer['importer_uid'])
2864 // Update content if 'updated' changes
2868 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
2870 // do not accept (ignore) an earlier edit than one we currently have.
2871 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
2874 logger('received updated comment' , LOGGER_DEBUG);
2875 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2876 dbesc($datarray['title']),
2877 dbesc($datarray['body']),
2878 dbesc($datarray['tag']),
2879 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
2881 intval($importer['importer_uid'])
2883 create_tags_from_itemuri($item_id, $importer['importer_uid']);
2885 proc_run('php',"include/notifier.php","comment-import",$iid);
2894 $own = q("select name,url,thumb from contact where uid = %d and self = 1 limit 1",
2895 intval($importer['importer_uid'])
2899 $datarray['type'] = 'remote-comment';
2900 $datarray['wall'] = 1;
2901 $datarray['parent-uri'] = $parent_uri;
2902 $datarray['uid'] = $importer['importer_uid'];
2903 $datarray['owner-name'] = $own[0]['name'];
2904 $datarray['owner-link'] = $own[0]['url'];
2905 $datarray['owner-avatar'] = $own[0]['thumb'];
2906 $datarray['contact-id'] = $importer['id'];
2908 if(($datarray['verb'] === ACTIVITY_LIKE) || ($datarray['verb'] === ACTIVITY_DISLIKE)) {
2910 $datarray['type'] = 'activity';
2911 $datarray['gravity'] = GRAVITY_LIKE;
2912 $datarray['last-child'] = 0;
2913 // only one like or dislike per person
2914 $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",
2915 intval($datarray['uid']),
2916 intval($datarray['contact-id']),
2917 dbesc($datarray['verb']),
2918 dbesc($datarray['parent-uri']),
2919 dbesc($datarray['parent-uri'])
2926 if(($datarray['verb'] === ACTIVITY_TAG) && ($datarray['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
2928 $xo = parse_xml_string($datarray['object'],false);
2929 $xt = parse_xml_string($datarray['target'],false);
2931 if(($xt->type == ACTIVITY_OBJ_NOTE) && ($xt->id)) {
2933 // fetch the parent item
2935 $tagp = q("select * from item where uri = '%s' and uid = %d limit 1",
2937 intval($importer['importer_uid'])
2942 // extract tag, if not duplicate, and this user allows tags, add to parent item
2944 if($xo->id && $xo->content) {
2945 $newtag = '#[url=' . $xo->id . ']'. $xo->content . '[/url]';
2946 if(! (stristr($tagp[0]['tag'],$newtag))) {
2947 $i = q("SELECT `blocktags` FROM `user` where `uid` = %d LIMIT 1",
2948 intval($importer['importer_uid'])
2950 if(count($i) && ! intval($i[0]['blocktags'])) {
2951 q("UPDATE item SET tag = '%s', `edited` = '%s' WHERE id = %d LIMIT 1",
2952 dbesc($tagp[0]['tag'] . (strlen($tagp[0]['tag']) ? ',' : '') . $newtag),
2953 intval($tagp[0]['id']),
2954 dbesc(datetime_convert())
2956 create_tags_from_item($tagp[0]['id']);
2964 $posted_id = item_store($datarray);
2968 $r = q("SELECT `parent`, `parent-uri` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
2970 intval($importer['importer_uid'])
2973 $parent = $r[0]['parent'];
2974 $parent_uri = $r[0]['parent-uri'];
2978 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
2979 dbesc(datetime_convert()),
2980 intval($importer['importer_uid']),
2981 intval($r[0]['parent'])
2984 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
2985 dbesc(datetime_convert()),
2986 intval($importer['importer_uid']),
2991 if($posted_id && $parent) {
2993 proc_run('php',"include/notifier.php","comment-import","$posted_id");
2995 if((! $is_like) && (! $importer['self'])) {
2997 require_once('include/enotify.php');
3000 'type' => NOTIFY_COMMENT,
3001 'notify_flags' => $importer['notify-flags'],
3002 'language' => $importer['language'],
3003 'to_name' => $importer['username'],
3004 'to_email' => $importer['email'],
3005 'uid' => $importer['importer_uid'],
3006 'item' => $datarray,
3007 'link' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
3008 'source_name' => stripslashes($datarray['author-name']),
3009 'source_link' => $datarray['author-link'],
3010 'source_photo' => ((link_compare($datarray['author-link'],$importer['url']))
3011 ? $importer['thumb'] : $datarray['author-avatar']),
3012 'verb' => ACTIVITY_POST,
3014 'parent' => $parent,
3015 'parent_uri' => $parent_uri,
3027 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
3029 $item_id = $item->get_id();
3030 $datarray = get_atom_elements($feed,$item);
3032 if($importer['rel'] == CONTACT_IS_FOLLOWER)
3035 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3037 intval($importer['importer_uid'])
3040 // Update content if 'updated' changes
3043 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
3045 // do not accept (ignore) an earlier edit than one we currently have.
3046 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
3049 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3050 dbesc($datarray['title']),
3051 dbesc($datarray['body']),
3052 dbesc($datarray['tag']),
3053 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
3055 intval($importer['importer_uid'])
3057 create_tags_from_itemuri($item_id, $importer['importer_uid']);
3060 // update last-child if it changes
3062 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
3063 if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
3064 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
3065 dbesc(datetime_convert()),
3067 intval($importer['importer_uid'])
3069 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3070 intval($allow[0]['data']),
3071 dbesc(datetime_convert()),
3073 intval($importer['importer_uid'])
3079 $datarray['parent-uri'] = $parent_uri;
3080 $datarray['uid'] = $importer['importer_uid'];
3081 $datarray['contact-id'] = $importer['id'];
3082 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
3083 $datarray['type'] = 'activity';
3084 $datarray['gravity'] = GRAVITY_LIKE;
3085 // only one like or dislike per person
3086 $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",
3087 intval($datarray['uid']),
3088 intval($datarray['contact-id']),
3089 dbesc($datarray['verb']),
3098 if(($datarray['verb'] === ACTIVITY_TAG) && ($datarray['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
3100 $xo = parse_xml_string($datarray['object'],false);
3101 $xt = parse_xml_string($datarray['target'],false);
3103 if($xt->type == ACTIVITY_OBJ_NOTE) {
3104 $r = q("select * from item where `uri` = '%s' AND `uid` = %d limit 1",
3106 intval($importer['importer_uid'])
3111 // extract tag, if not duplicate, add to parent item
3113 if(! (stristr($r[0]['tag'],trim($xo->content)))) {
3114 q("UPDATE item SET tag = '%s' WHERE id = %d LIMIT 1",
3115 dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . '#[url=' . $xo->id . ']'. $xo->content . '[/url]'),
3118 create_tags_from_item($r[0]['id']);
3124 $posted_id = item_store($datarray);
3126 // find out if our user is involved in this conversation and wants to be notified.
3128 if(!x($datarray['type']) || $datarray['type'] != 'activity') {
3130 $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
3132 intval($importer['importer_uid'])
3135 if(count($myconv)) {
3136 $importer_url = $a->get_baseurl() . '/profile/' . $importer['nickname'];
3138 // first make sure this isn't our own post coming back to us from a wall-to-wall event
3139 if(! link_compare($datarray['author-link'],$importer_url)) {
3142 foreach($myconv as $conv) {
3144 // now if we find a match, it means we're in this conversation
3146 if(! link_compare($conv['author-link'],$importer_url))
3149 require_once('include/enotify.php');
3151 $conv_parent = $conv['parent'];
3154 'type' => NOTIFY_COMMENT,
3155 'notify_flags' => $importer['notify-flags'],
3156 'language' => $importer['language'],
3157 'to_name' => $importer['username'],
3158 'to_email' => $importer['email'],
3159 'uid' => $importer['importer_uid'],
3160 'item' => $datarray,
3161 'link' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
3162 'source_name' => stripslashes($datarray['author-name']),
3163 'source_link' => $datarray['author-link'],
3164 'source_photo' => ((link_compare($datarray['author-link'],$importer['url']))
3165 ? $importer['thumb'] : $datarray['author-avatar']),
3166 'verb' => ACTIVITY_POST,
3168 'parent' => $conv_parent,
3169 'parent_uri' => $parent_uri
3173 // only send one notification
3185 // Head post of a conversation. Have we seen it? If not, import it.
3188 $item_id = $item->get_id();
3189 $datarray = get_atom_elements($feed,$item);
3191 if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
3192 $ev = bbtoevent($datarray['body']);
3193 if(x($ev,'desc') && x($ev,'start')) {
3194 $ev['cid'] = $importer['id'];
3195 $ev['uid'] = $importer['uid'];
3196 $ev['uri'] = $item_id;
3197 $ev['edited'] = $datarray['edited'];
3198 $ev['private'] = $datarray['private'];
3200 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3202 intval($importer['uid'])
3205 $ev['id'] = $r[0]['id'];
3206 $xyz = event_store($ev);
3211 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3213 intval($importer['importer_uid'])
3216 // Update content if 'updated' changes
3219 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
3221 // do not accept (ignore) an earlier edit than one we currently have.
3222 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
3225 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3226 dbesc($datarray['title']),
3227 dbesc($datarray['body']),
3228 dbesc($datarray['tag']),
3229 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
3231 intval($importer['importer_uid'])
3233 create_tags_from_itemuri($item_id, $importer['importer_uid']);
3236 // update last-child if it changes
3238 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
3239 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
3240 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3241 intval($allow[0]['data']),
3242 dbesc(datetime_convert()),
3244 intval($importer['importer_uid'])
3250 // This is my contact on another system, but it's really me.
3251 // Turn this into a wall post.
3253 if($importer['remote_self'])
3254 $datarray['wall'] = 1;
3256 $datarray['parent-uri'] = $item_id;
3257 $datarray['uid'] = $importer['importer_uid'];
3258 $datarray['contact-id'] = $importer['id'];
3261 if(! link_compare($datarray['owner-link'],$importer['url'])) {
3262 // The item owner info is not our contact. It's OK and is to be expected if this is a tgroup delivery,
3263 // but otherwise there's a possible data mixup on the sender's system.
3264 // the tgroup delivery code called from item_store will correct it if it's a forum,
3265 // but we're going to unconditionally correct it here so that the post will always be owned by our contact.
3266 logger('local_delivery: Correcting item owner.', LOGGER_DEBUG);
3267 $datarray['owner-name'] = $importer['senderName'];
3268 $datarray['owner-link'] = $importer['url'];
3269 $datarray['owner-avatar'] = $importer['thumb'];
3272 if(($importer['rel'] == CONTACT_IS_FOLLOWER) && (! tgroup_check($importer['importer_uid'],$datarray)))
3275 $posted_id = item_store($datarray);
3277 if(stristr($datarray['verb'],ACTIVITY_POKE)) {
3278 $verb = urldecode(substr($datarray['verb'],strpos($datarray['verb'],'#')+1));
3281 $xo = parse_xml_string($datarray['object'],false);
3283 if(($xo->type == ACTIVITY_OBJ_PERSON) && ($xo->id)) {
3285 // somebody was poked/prodded. Was it me?
3287 $links = parse_xml_string("<links>".unxmlify($xo->link)."</links>",false);
3289 foreach($links->link as $l) {
3290 $atts = $l->attributes();
3291 switch($atts['rel']) {
3293 $Blink = $atts['href'];
3299 if($Blink && link_compare($Blink,$a->get_baseurl() . '/profile/' . $importer['nickname'])) {
3301 // send a notification
3302 require_once('include/enotify.php');
3305 'type' => NOTIFY_POKE,
3306 'notify_flags' => $importer['notify-flags'],
3307 'language' => $importer['language'],
3308 'to_name' => $importer['username'],
3309 'to_email' => $importer['email'],
3310 'uid' => $importer['importer_uid'],
3311 'item' => $datarray,
3312 'link' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
3313 'source_name' => stripslashes($datarray['author-name']),
3314 'source_link' => $datarray['author-link'],
3315 'source_photo' => ((link_compare($datarray['author-link'],$importer['url']))
3316 ? $importer['thumb'] : $datarray['author-avatar']),
3317 'verb' => $datarray['verb'],
3318 'otype' => 'person',
3319 'activity' => $verb,
3336 function new_follower($importer,$contact,$datarray,$item,$sharing = false) {
3337 $url = notags(trim($datarray['author-link']));
3338 $name = notags(trim($datarray['author-name']));
3339 $photo = notags(trim($datarray['author-avatar']));
3341 $rawtag = $item->get_item_tags(NAMESPACE_ACTIVITY,'actor');
3342 if($rawtag && $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'])
3343 $nick = $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'];
3345 if(is_array($contact)) {
3346 if(($contact['network'] == NETWORK_OSTATUS && $contact['rel'] == CONTACT_IS_SHARING)
3347 || ($sharing && $contact['rel'] == CONTACT_IS_FOLLOWER)) {
3348 $r = q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
3349 intval(CONTACT_IS_FRIEND),
3350 intval($contact['id']),
3351 intval($importer['uid'])
3354 // send email notification to owner?
3358 // create contact record
3360 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `name`, `nick`, `photo`, `network`, `rel`,
3361 `blocked`, `readonly`, `pending`, `writable` )
3362 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 1, 1 ) ",
3363 intval($importer['uid']),
3364 dbesc(datetime_convert()),
3366 dbesc(normalise_link($url)),
3370 dbesc(($sharing) ? NETWORK_ZOT : NETWORK_OSTATUS),
3371 intval(($sharing) ? CONTACT_IS_SHARING : CONTACT_IS_FOLLOWER)
3373 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `pending` = 1 LIMIT 1",
3374 intval($importer['uid']),
3378 $contact_record = $r[0];
3380 // create notification
3381 $hash = random_string();
3383 if(is_array($contact_record)) {
3384 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `hash`, `datetime`)
3385 VALUES ( %d, %d, 0, 0, '%s', '%s' )",
3386 intval($importer['uid']),
3387 intval($contact_record['id']),
3389 dbesc(datetime_convert())
3392 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
3393 intval($importer['uid'])
3398 if(intval($r[0]['def_gid'])) {
3399 require_once('include/group.php');
3400 group_add_member($r[0]['uid'],'',$contact_record['id'],$r[0]['def_gid']);
3403 if(($r[0]['notify-flags'] & NOTIFY_INTRO) && ($r[0]['page-flags'] == PAGE_NORMAL)) {
3404 $email_tpl = get_intltext_template('follow_notify_eml.tpl');
3405 $email = replace_macros($email_tpl, array(
3406 '$requestor' => ((strlen($name)) ? $name : t('[Name Withheld]')),
3408 '$myname' => $r[0]['username'],
3409 '$siteurl' => $a->get_baseurl(),
3410 '$sitename' => $a->config['sitename']
3412 $res = mail($r[0]['email'],
3413 (($sharing) ? t('A new person is sharing with you at ') : t("You have a new follower at ")) . $a->config['sitename'],
3415 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] . "\n"
3416 . 'Content-type: text/plain; charset=UTF-8' . "\n"
3417 . 'Content-transfer-encoding: 8bit' );
3424 function lose_follower($importer,$contact,$datarray,$item) {
3426 if(($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_SHARING)) {
3427 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
3428 intval(CONTACT_IS_SHARING),
3429 intval($contact['id'])
3433 contact_remove($contact['id']);
3437 function lose_sharer($importer,$contact,$datarray,$item) {
3439 if(($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_FOLLOWER)) {
3440 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
3441 intval(CONTACT_IS_FOLLOWER),
3442 intval($contact['id'])
3446 contact_remove($contact['id']);
3451 function subscribe_to_hub($url,$importer,$contact,$hubmode = 'subscribe') {
3455 if(is_array($importer)) {
3456 $r = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
3457 intval($importer['uid'])
3461 // Diaspora has different message-ids in feeds than they do
3462 // through the direct Diaspora protocol. If we try and use
3463 // the feed, we'll get duplicates. So don't.
3465 if((! count($r)) || $contact['network'] === NETWORK_DIASPORA)
3468 $push_url = get_config('system','url') . '/pubsub/' . $r[0]['nickname'] . '/' . $contact['id'];
3470 // Use a single verify token, even if multiple hubs
3472 $verify_token = ((strlen($contact['hub-verify'])) ? $contact['hub-verify'] : random_string());
3474 $params= 'hub.mode=' . $hubmode . '&hub.callback=' . urlencode($push_url) . '&hub.topic=' . urlencode($contact['poll']) . '&hub.verify=async&hub.verify_token=' . $verify_token;
3476 logger('subscribe_to_hub: ' . $hubmode . ' ' . $contact['name'] . ' to hub ' . $url . ' endpoint: ' . $push_url . ' with verifier ' . $verify_token);
3478 if(! strlen($contact['hub-verify'])) {
3479 $r = q("UPDATE `contact` SET `hub-verify` = '%s' WHERE `id` = %d LIMIT 1",
3480 dbesc($verify_token),
3481 intval($contact['id'])
3485 post_url($url,$params);
3487 logger('subscribe_to_hub: returns: ' . $a->get_curl_code(), LOGGER_DEBUG);
3494 function atom_author($tag,$name,$uri,$h,$w,$photo) {
3498 $name = xmlify($name);
3499 $uri = xmlify($uri);
3502 $photo = xmlify($photo);
3506 $o .= "<name>$name</name>\r\n";
3507 $o .= "<uri>$uri</uri>\r\n";
3508 $o .= '<link rel="photo" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
3509 $o .= '<link rel="avatar" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
3511 call_hooks('atom_author', $o);
3513 $o .= "</$tag>\r\n";
3517 function atom_entry($item,$type,$author,$owner,$comment = false,$cid = 0) {
3521 if(! $item['parent'])
3524 if($item['deleted'])
3525 return '<at:deleted-entry ref="' . xmlify($item['uri']) . '" when="' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '" />' . "\r\n";
3528 if($item['allow_cid'] || $item['allow_gid'] || $item['deny_cid'] || $item['deny_gid'])
3529 $body = fix_private_photos($item['body'],$owner['uid'],$item,$cid);
3531 $body = $item['body'];
3533 $o = "\r\n\r\n<entry>\r\n";
3535 if(is_array($author))
3536 $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb']);
3538 $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']));
3539 if(strlen($item['owner-name']))
3540 $o .= atom_author('dfrn:owner',$item['owner-name'],$item['owner-link'],80,80,$item['owner-avatar']);
3542 if(($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
3543 $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
3544 $o .= '<thr:in-reply-to ref="' . xmlify($parent_item) . '" type="text/html" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['parent']) . '" />' . "\r\n";
3547 $o .= '<id>' . xmlify($item['uri']) . '</id>' . "\r\n";
3548 $o .= '<title>' . xmlify($item['title']) . '</title>' . "\r\n";
3549 $o .= '<published>' . xmlify(datetime_convert('UTC','UTC',$item['created'] . '+00:00',ATOM_TIME)) . '</published>' . "\r\n";
3550 $o .= '<updated>' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '</updated>' . "\r\n";
3551 $o .= '<dfrn:env>' . base64url_encode($body, true) . '</dfrn:env>' . "\r\n";
3552 $o .= '<content type="' . $type . '" >' . xmlify((($type === 'html') ? bbcode($body) : $body)) . '</content>' . "\r\n";
3553 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']) . '" />' . "\r\n";
3555 $o .= '<dfrn:comment-allow>' . intval($item['last-child']) . '</dfrn:comment-allow>' . "\r\n";
3557 if($item['location']) {
3558 $o .= '<dfrn:location>' . xmlify($item['location']) . '</dfrn:location>' . "\r\n";
3559 $o .= '<poco:address><poco:formatted>' . xmlify($item['location']) . '</poco:formatted></poco:address>' . "\r\n";
3563 $o .= '<georss:point>' . xmlify($item['coord']) . '</georss:point>' . "\r\n";
3565 if(($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid']))
3566 $o .= '<dfrn:private>' . (($item['private']) ? $item['private'] : 1) . '</dfrn:private>' . "\r\n";
3569 $o .= '<dfrn:extid>' . xmlify($item['extid']) . '</dfrn:extid>' . "\r\n";
3570 if($item['bookmark'])
3571 $o .= '<dfrn:bookmark>true</dfrn:bookmark>' . "\r\n";
3574 $o .= '<statusnet:notice_info local_id="' . $item['id'] . '" source="' . xmlify($item['app']) . '" ></statusnet:notice_info>' . "\r\n";
3577 $o .= '<dfrn:diaspora_guid>' . $item['guid'] . '</dfrn:diaspora_guid>' . "\r\n";
3579 if($item['signed_text']) {
3580 $sign = base64_encode(json_encode(array('signed_text' => $item['signed_text'],'signature' => $item['signature'],'signer' => $item['signer'])));
3581 $o .= '<dfrn:diaspora_signature>' . xmlify($sign) . '</dfrn:diaspora_signature>' . "\r\n";
3584 $verb = construct_verb($item);
3585 $o .= '<as:verb>' . xmlify($verb) . '</as:verb>' . "\r\n";
3586 $actobj = construct_activity_object($item);
3589 $actarg = construct_activity_target($item);
3593 $tags = item_getfeedtags($item);
3595 foreach($tags as $t) {
3596 $o .= '<category scheme="X-DFRN:' . xmlify($t[0]) . ':' . xmlify($t[1]) . '" term="' . xmlify($t[2]) . '" />' . "\r\n";
3600 $o .= item_getfeedattach($item);
3602 $mentioned = get_mentions($item);
3606 call_hooks('atom_entry', $o);
3608 $o .= '</entry>' . "\r\n";
3613 function fix_private_photos($s, $uid, $item = null, $cid = 0) {
3616 logger('fix_private_photos', LOGGER_DEBUG);
3617 $site = substr($a->get_baseurl(),strpos($a->get_baseurl(),'://'));
3622 $img_start = strpos($orig_body, '[img');
3623 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
3624 $img_len = ($img_start !== false ? strpos(substr($orig_body, $img_start + $img_st_close + 1), '[/img]') : false);
3625 while( ($img_st_close !== false) && ($img_len !== false) ) {
3627 $img_st_close++; // make it point to AFTER the closing bracket
3628 $image = substr($orig_body, $img_start + $img_st_close, $img_len);
3630 logger('fix_private_photos: found photo ' . $image, LOGGER_DEBUG);
3633 if(stristr($image , $site . '/photo/')) {
3634 // Only embed locally hosted photos
3636 $i = basename($image);
3637 $i = str_replace(array('.jpg','.png'),array('',''),$i);
3638 $x = strpos($i,'-');
3641 $res = substr($i,$x+1);
3642 $i = substr($i,0,$x);
3643 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d AND `uid` = %d",
3650 // Check to see if we should replace this photo link with an embedded image
3651 // 1. No need to do so if the photo is public
3652 // 2. If there's a contact-id provided, see if they're in the access list
3653 // for the photo. If so, embed it.
3654 // 3. Otherwise, if we have an item, see if the item permissions match the photo
3655 // permissions, regardless of order but first check to see if they're an exact
3656 // match to save some processing overhead.
3658 if(has_permissions($r[0])) {
3660 $recips = enumerate_permissions($r[0]);
3661 if(in_array($cid, $recips)) {
3666 if(compare_permissions($item,$r[0]))
3671 $data = $r[0]['data'];
3672 $type = $r[0]['type'];
3674 // If a custom width and height were specified, apply before embedding
3675 if(preg_match("/\[img\=([0-9]*)x([0-9]*)\]/is", substr($orig_body, $img_start, $img_st_close), $match)) {
3676 logger('fix_private_photos: scaling photo', LOGGER_DEBUG);
3678 $width = intval($match[1]);
3679 $height = intval($match[2]);
3681 $ph = new Photo($data, $type);
3682 if($ph->is_valid()) {
3683 $ph->scaleImage(max($width, $height));
3684 $data = $ph->imageString();
3685 $type = $ph->getType();
3689 logger('fix_private_photos: replacing photo', LOGGER_DEBUG);
3690 $image = 'data:' . $type . ';base64,' . base64_encode($data);
3691 logger('fix_private_photos: replaced: ' . $image, LOGGER_DATA);
3697 $new_body = $new_body . substr($orig_body, 0, $img_start + $img_st_close) . $image . '[/img]';
3698 $orig_body = substr($orig_body, $img_start + $img_st_close + $img_len + strlen('[/img]'));
3699 if($orig_body === false)
3702 $img_start = strpos($orig_body, '[img');
3703 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
3704 $img_len = ($img_start !== false ? strpos(substr($orig_body, $img_start + $img_st_close + 1), '[/img]') : false);
3707 $new_body = $new_body . $orig_body;
3713 function has_permissions($obj) {
3714 if(($obj['allow_cid'] != '') || ($obj['allow_gid'] != '') || ($obj['deny_cid'] != '') || ($obj['deny_gid'] != ''))
3719 function compare_permissions($obj1,$obj2) {
3720 // first part is easy. Check that these are exactly the same.
3721 if(($obj1['allow_cid'] == $obj2['allow_cid'])
3722 && ($obj1['allow_gid'] == $obj2['allow_gid'])
3723 && ($obj1['deny_cid'] == $obj2['deny_cid'])
3724 && ($obj1['deny_gid'] == $obj2['deny_gid']))
3727 // This is harder. Parse all the permissions and compare the resulting set.
3729 $recipients1 = enumerate_permissions($obj1);
3730 $recipients2 = enumerate_permissions($obj2);
3733 if($recipients1 == $recipients2)
3738 // returns an array of contact-ids that are allowed to see this object
3740 function enumerate_permissions($obj) {
3741 require_once('include/group.php');
3742 $allow_people = expand_acl($obj['allow_cid']);
3743 $allow_groups = expand_groups(expand_acl($obj['allow_gid']));
3744 $deny_people = expand_acl($obj['deny_cid']);
3745 $deny_groups = expand_groups(expand_acl($obj['deny_gid']));
3746 $recipients = array_unique(array_merge($allow_people,$allow_groups));
3747 $deny = array_unique(array_merge($deny_people,$deny_groups));
3748 $recipients = array_diff($recipients,$deny);
3752 function item_getfeedtags($item) {
3755 $cnt = preg_match_all('|\#\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
3757 for($x = 0; $x < $cnt; $x ++) {
3759 $ret[] = array('#',$matches[1][$x], $matches[2][$x]);
3763 $cnt = preg_match_all('|\@\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
3765 for($x = 0; $x < $cnt; $x ++) {
3767 $ret[] = array('@',$matches[1][$x], $matches[2][$x]);
3773 function item_getfeedattach($item) {
3775 $arr = explode(',',$item['attach']);
3777 foreach($arr as $r) {
3779 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"\[\/attach\]|',$r,$matches);
3781 $ret .= '<link rel="enclosure" href="' . xmlify($matches[1]) . '" type="' . xmlify($matches[3]) . '" ';
3782 if(intval($matches[2]))
3783 $ret .= 'length="' . intval($matches[2]) . '" ';
3784 if($matches[4] !== ' ')
3785 $ret .= 'title="' . xmlify(trim($matches[4])) . '" ';
3786 $ret .= ' />' . "\r\n";
3795 function item_expire($uid,$days) {
3797 if((! $uid) || ($days < 1))
3800 // $expire_network_only = save your own wall posts
3801 // and just expire conversations started by others
3803 $expire_network_only = get_pconfig($uid,'expire','network_only');
3804 $sql_extra = ((intval($expire_network_only)) ? " AND wall = 0 " : "");
3806 $r = q("SELECT * FROM `item`
3808 AND `created` < UTC_TIMESTAMP() - INTERVAL %d DAY
3819 $expire_items = get_pconfig($uid, 'expire','items');
3820 $expire_items = (($expire_items===false)?1:intval($expire_items)); // default if not set: 1
3822 $expire_notes = get_pconfig($uid, 'expire','notes');
3823 $expire_notes = (($expire_notes===false)?1:intval($expire_notes)); // default if not set: 1
3825 $expire_starred = get_pconfig($uid, 'expire','starred');
3826 $expire_starred = (($expire_starred===false)?1:intval($expire_starred)); // default if not set: 1
3828 $expire_photos = get_pconfig($uid, 'expire','photos');
3829 $expire_photos = (($expire_photos===false)?0:intval($expire_photos)); // default if not set: 0
3831 logger('expire: # items=' . count($r). "; expire items: $expire_items, expire notes: $expire_notes, expire starred: $expire_starred, expire photos: $expire_photos");
3833 foreach($r as $item) {
3835 // don't expire filed items
3837 if(strpos($item['file'],'[') !== false)
3840 // Only expire posts, not photos and photo comments
3842 if($expire_photos==0 && strlen($item['resource-id']))
3844 if($expire_starred==0 && intval($item['starred']))
3846 if($expire_notes==0 && $item['type']=='note')
3848 if($expire_items==0 && $item['type']!='note')
3851 drop_item($item['id'],false);
3854 proc_run('php',"include/notifier.php","expire","$uid");
3859 function drop_items($items) {
3862 if(! local_user() && ! remote_user())
3866 foreach($items as $item) {
3867 $owner = drop_item($item,false);
3868 if($owner && ! $uid)
3873 // multiple threads may have been deleted, send an expire notification
3876 proc_run('php',"include/notifier.php","expire","$uid");
3880 function drop_item($id,$interactive = true) {
3884 // locate item to be deleted
3886 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
3893 notice( t('Item not found.') . EOL);
3894 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
3899 $owner = $item['uid'];
3903 // check if logged in user is either the author or owner of this item
3905 if(is_array($_SESSION['remote'])) {
3906 foreach($_SESSION['remote'] as $visitor) {
3907 if($visitor['uid'] == $item['uid'] && $visitor['cid'] == $item['contact-id']) {
3908 $cid = $visitor['cid'];
3915 if((local_user() == $item['uid']) || ($cid) || (! $interactive)) {
3917 logger('delete item: ' . $item['id'], LOGGER_DEBUG);
3920 $r = q("UPDATE `item` SET `deleted` = 1, `title` = '', `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
3921 dbesc(datetime_convert()),
3922 dbesc(datetime_convert()),
3925 create_tags_from_item($item['id']);
3927 // clean up categories and tags so they don't end up as orphans
3930 $cnt = preg_match_all('/<(.*?)>/',$item['file'],$matches,PREG_SET_ORDER);
3932 foreach($matches as $mtch) {
3933 file_tag_unsave_file($item['uid'],$item['id'],$mtch[1],true);
3939 $cnt = preg_match_all('/\[(.*?)\]/',$item['file'],$matches,PREG_SET_ORDER);
3941 foreach($matches as $mtch) {
3942 file_tag_unsave_file($item['uid'],$item['id'],$mtch[1],false);
3946 // If item is a link to a photo resource, nuke all the associated photos
3947 // (visitors will not have photo resources)
3948 // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
3949 // generate a resource-id and therefore aren't intimately linked to the item.
3951 if(strlen($item['resource-id'])) {
3952 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
3953 dbesc($item['resource-id']),
3954 intval($item['uid'])
3956 // ignore the result
3959 // If item is a link to an event, nuke the event record.
3961 if(intval($item['event-id'])) {
3962 q("DELETE FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
3963 intval($item['event-id']),
3964 intval($item['uid'])
3966 // ignore the result
3969 // clean up item_id and sign meta-data tables
3971 $r = q("DELETE FROM item_id where iid in (select id from item where parent = %d and uid = %d)",
3972 intval($item['id']),
3973 intval($item['uid'])
3976 $r = q("DELETE FROM sign where iid in (select id from item where parent = %d and uid = %d)",
3977 intval($item['id']),
3978 intval($item['uid'])
3981 // If it's the parent of a comment thread, kill all the kids
3983 if($item['uri'] == $item['parent-uri']) {
3984 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' , `title` = ''
3985 WHERE `parent-uri` = '%s' AND `uid` = %d ",
3986 dbesc(datetime_convert()),
3987 dbesc(datetime_convert()),
3988 dbesc($item['parent-uri']),
3989 intval($item['uid'])
3991 create_tags_from_item($item['parent-uri'], $item['uid']);
3992 // ignore the result
3995 // ensure that last-child is set in case the comment that had it just got wiped.
3996 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
3997 dbesc(datetime_convert()),
3998 dbesc($item['parent-uri']),
3999 intval($item['uid'])
4001 // who is the last child now?
4002 $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",
4003 dbesc($item['parent-uri']),
4004 intval($item['uid'])
4007 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
4012 // Add a relayable_retraction signature for Diaspora.
4013 store_diaspora_retract_sig($item, $a->user, $a->get_baseurl());
4015 $drop_id = intval($item['id']);
4017 // send the notification upstream/downstream as the case may be
4019 proc_run('php',"include/notifier.php","drop","$drop_id");
4023 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
4029 notice( t('Permission denied.') . EOL);
4030 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
4037 function first_post_date($uid,$wall = false) {
4038 $r = q("select id, created from item
4039 where uid = %d and wall = %d and deleted = 0 and visible = 1 AND moderated = 0
4041 order by created asc limit 1",
4043 intval($wall ? 1 : 0)
4046 // logger('first_post_date: ' . $r[0]['id'] . ' ' . $r[0]['created'], LOGGER_DATA);
4047 return substr(datetime_convert('',date_default_timezone_get(),$r[0]['created']),0,10);
4052 function posted_dates($uid,$wall) {
4053 $dnow = datetime_convert('',date_default_timezone_get(),'now','Y-m-d');
4055 $dthen = first_post_date($uid,$wall);
4059 // If it's near the end of a long month, backup to the 28th so that in
4060 // consecutive loops we'll always get a whole month difference.
4062 if(intval(substr($dnow,8)) > 28)
4063 $dnow = substr($dnow,0,8) . '28';
4064 if(intval(substr($dthen,8)) > 28)
4065 $dnow = substr($dthen,0,8) . '28';
4068 // Starting with the current month, get the first and last days of every
4069 // month down to and including the month of the first post
4070 while(substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
4071 $dstart = substr($dnow,0,8) . '01';
4072 $dend = substr($dnow,0,8) . get_dim(intval($dnow),intval(substr($dnow,5)));
4073 $start_month = datetime_convert('','',$dstart,'Y-m-d');
4074 $end_month = datetime_convert('','',$dend,'Y-m-d');
4075 $str = day_translate(datetime_convert('','',$dnow,'F Y'));
4076 $ret[] = array($str,$end_month,$start_month);
4077 $dnow = datetime_convert('','',$dnow . ' -1 month', 'Y-m-d');
4083 function posted_date_widget($url,$uid,$wall) {
4086 if(! feature_enabled($uid,'archives'))
4089 // For former Facebook folks that left because of "timeline"
4091 /* if($wall && intval(get_pconfig($uid,'system','no_wall_archive_widget')))
4094 $ret = posted_dates($uid,$wall);
4098 $o = replace_macros(get_markup_template('posted_date_widget.tpl'),array(
4099 '$title' => t('Archives'),
4100 '$size' => ((count($ret) > 6) ? 6 : count($ret)),
4107 function store_diaspora_retract_sig($item, $user, $baseurl) {
4108 // Note that we can't add a target_author_signature
4109 // if the comment was deleted by a remote user. That should be ok, because if a remote user is deleting
4110 // the comment, that means we're the home of the post, and Diaspora will only
4111 // check the parent_author_signature of retractions that it doesn't have to relay further
4113 // I don't think this function gets called for an "unlike," but I'll check anyway
4115 $enabled = intval(get_config('system','diaspora_enabled'));
4117 logger('drop_item: diaspora support disabled, not storing retraction signature', LOGGER_DEBUG);
4121 logger('drop_item: storing diaspora retraction signature');
4123 $signed_text = $item['guid'] . ';' . ( ($item['verb'] === ACTIVITY_LIKE) ? 'Like' : 'Comment');
4125 if(local_user() == $item['uid']) {
4127 $handle = $user['nickname'] . '@' . substr($baseurl, strpos($baseurl,'://') + 3);
4128 $authorsig = base64_encode(rsa_sign($signed_text,$user['prvkey'],'sha256'));
4131 $r = q("SELECT `nick`, `url` FROM `contact` WHERE `id` = '%d' LIMIT 1",
4132 $item['contact-id'] // If this function gets called, drop_item() has already checked remote_user() == $item['contact-id']
4135 // The below handle only works for NETWORK_DFRN. I think that's ok, because this function
4136 // only handles DFRN deletes
4137 $handle_baseurl_start = strpos($r['url'],'://') + 3;
4138 $handle_baseurl_length = strpos($r['url'],'/profile') - $handle_baseurl_start;
4139 $handle = $r['nick'] . '@' . substr($r['url'], $handle_baseurl_start, $handle_baseurl_length);
4145 q("insert into sign (`retract_iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
4146 intval($item['id']),
4147 dbesc($signed_text),