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');
10 require_once('include/email.php');
11 require_once('include/ostatus_conversation.php');
13 function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
16 $sitefeed = ((strlen($owner_nick)) ? false : true); // not yet implemented, need to rewrite huge chunks of following logic
17 $public_feed = (($dfrn_id) ? false : true);
18 $starred = false; // not yet implemented, possible security issues
21 if($public_feed && $a->argc > 2) {
22 for($x = 2; $x < $a->argc; $x++) {
23 if($a->argv[$x] == 'converse')
25 if($a->argv[$x] == 'starred')
27 if($a->argv[$x] === 'category' && $a->argc > ($x + 1) && strlen($a->argv[$x+1]))
28 $category = $a->argv[$x+1];
34 // default permissions - anonymous user
36 $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
38 $r = q("SELECT `contact`.*, `user`.`uid` AS `user_uid`, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`
39 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
40 WHERE `contact`.`self` = 1 AND `user`.`nickname` = '%s' LIMIT 1",
48 $owner_id = $owner['user_uid'];
49 $owner_nick = $owner['nickname'];
51 $birthday = feed_birthday($owner_id,$owner['timezone']);
58 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
62 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
63 $my_id = '1:' . $dfrn_id;
66 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
67 $my_id = '0:' . $dfrn_id;
74 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
82 require_once('include/security.php');
83 $groups = init_groups_visitor($contact['id']);
86 for($x = 0; $x < count($groups); $x ++)
87 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
88 $gs = implode('|', $groups);
91 $gs = '<<>>' ; // Impossible to match
93 $sql_extra = sprintf("
94 AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' )
95 AND ( `deny_cid` = '' OR NOT `deny_cid` REGEXP '<%d>' )
96 AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
97 AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s')
99 intval($contact['id']),
100 intval($contact['id']),
111 if(! strlen($last_update))
112 $last_update = 'now -30 days';
114 if(isset($category)) {
115 $sql_extra .= file_tag_file_query('item',$category,'category');
120 $sql_extra .= " AND `contact`.`self` = 1 ";
123 $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
125 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
126 `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`,
127 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
128 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
129 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`,
130 `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
131 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
132 LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
133 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`parent` != 0
134 AND `item`.`wall` = 1 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
135 AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
137 ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
144 // Will check further below if this actually returned results.
145 // We will provide an empty feed if that is the case.
149 $feed_template = get_markup_template(($dfrn_id) ? 'atom_feed_dfrn.tpl' : 'atom_feed.tpl');
153 $hubxml = feed_hublinks();
155 $salmon = feed_salmonlinks($owner_nick);
157 $atom .= replace_macros($feed_template, array(
158 '$version' => xmlify(FRIENDICA_VERSION),
159 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
160 '$feed_title' => xmlify($owner['name']),
161 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', 'now' , ATOM_TIME)) ,
163 '$salmon' => $salmon,
164 '$name' => xmlify($owner['name']),
165 '$profile_page' => xmlify($owner['url']),
166 '$photo' => xmlify($owner['photo']),
167 '$thumb' => xmlify($owner['thumb']),
168 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
169 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
170 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
171 '$birthday' => ((strlen($birthday)) ? '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>' : ''),
172 '$community' => (($owner['page-flags'] == PAGE_COMMUNITY) ? '<dfrn:community>1</dfrn:community>' : '')
175 call_hooks('atom_feed', $atom);
177 if(! count($items)) {
179 call_hooks('atom_feed_end', $atom);
181 $atom .= '</feed>' . "\r\n";
185 foreach($items as $item) {
187 // prevent private email from leaking.
188 if($item['network'] === NETWORK_MAIL)
191 // public feeds get html, our own nodes use bbcode
195 // catch any email that's in a public conversation and make sure it doesn't leak
203 $atom .= atom_entry($item,$type,null,$owner,true);
206 call_hooks('atom_feed_end', $atom);
208 $atom .= '</feed>' . "\r\n";
214 function construct_verb($item) {
216 return $item['verb'];
217 return ACTIVITY_POST;
220 function construct_activity_object($item) {
222 if($item['object']) {
223 $o = '<as:object>' . "\r\n";
224 $r = parse_xml_string($item['object'],false);
230 $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
232 $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
234 $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
236 if(substr($r->link,0,1) === '<') {
237 // patch up some facebook "like" activity objects that got stored incorrectly
238 // for a couple of months prior to 9-Jun-2011 and generated bad XML.
239 // we can probably remove this hack here and in the following function in a few months time.
240 if(strstr($r->link,'&') && (! strstr($r->link,'&')))
241 $r->link = str_replace('&','&', $r->link);
242 $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
246 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
249 $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
250 $o .= '</as:object>' . "\r\n";
257 function construct_activity_target($item) {
259 if($item['target']) {
260 $o = '<as:target>' . "\r\n";
261 $r = parse_xml_string($item['target'],false);
265 $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
267 $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
269 $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
271 if(substr($r->link,0,1) === '<') {
272 if(strstr($r->link,'&') && (! strstr($r->link,'&')))
273 $r->link = str_replace('&','&', $r->link);
274 $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
278 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
281 $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
282 $o .= '</as:target>' . "\r\n";
291 * The purpose of this function is to apply system message length limits to
292 * imported messages without including any embedded photos in the length
294 if(! function_exists('limit_body_size')) {
295 function limit_body_size($body) {
297 logger('limit_body_size: start', LOGGER_DEBUG);
299 $maxlen = get_max_import_size();
301 // If the length of the body, including the embedded images, is smaller
302 // than the maximum, then don't waste time looking for the images
303 if($maxlen && (strlen($body) > $maxlen)) {
305 logger('limit_body_size: the total body length exceeds the limit', LOGGER_DEBUG);
312 $img_start = strpos($orig_body, '[img');
313 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
314 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
315 while(($img_st_close !== false) && ($img_end !== false)) {
317 $img_st_close++; // make it point to AFTER the closing bracket
318 $img_end += $img_start;
319 $img_end += strlen('[/img]');
321 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
322 // This is an embedded image
324 if( ($textlen + $img_start) > $maxlen ) {
325 if($textlen < $maxlen) {
326 logger('limit_body_size: the limit happens before an embedded image', LOGGER_DEBUG);
327 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
332 $new_body = $new_body . substr($orig_body, 0, $img_start);
333 $textlen += $img_start;
336 $new_body = $new_body . substr($orig_body, $img_start, $img_end - $img_start);
340 if( ($textlen + $img_end) > $maxlen ) {
341 if($textlen < $maxlen) {
342 logger('limit_body_size: the limit happens before the end of a non-embedded image', LOGGER_DEBUG);
343 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
348 $new_body = $new_body . substr($orig_body, 0, $img_end);
349 $textlen += $img_end;
352 $orig_body = substr($orig_body, $img_end);
354 if($orig_body === false) // in case the body ends on a closing image tag
357 $img_start = strpos($orig_body, '[img');
358 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
359 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
362 if( ($textlen + strlen($orig_body)) > $maxlen) {
363 if($textlen < $maxlen) {
364 logger('limit_body_size: the limit happens after the end of the last image', LOGGER_DEBUG);
365 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
370 logger('limit_body_size: the text size with embedded images extracted did not violate the limit', LOGGER_DEBUG);
371 $new_body = $new_body . $orig_body;
372 $textlen += strlen($orig_body);
381 function title_is_body($title, $body) {
383 $title = strip_tags($title);
384 $title = trim($title);
385 $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
386 $title = str_replace(array("\n", "\r", "\t", " "), array("","","",""), $title);
388 $body = strip_tags($body);
390 $body = html_entity_decode($body, ENT_QUOTES, 'UTF-8');
391 $body = str_replace(array("\n", "\r", "\t", " "), array("","","",""), $body);
393 if (strlen($title) < strlen($body))
394 $body = substr($body, 0, strlen($title));
396 if (($title != $body) and (substr($title, -3) == "...")) {
397 $pos = strrpos($title, "...");
399 $title = substr($title, 0, $pos);
400 $body = substr($body, 0, $pos);
404 return($title == $body);
409 function get_atom_elements($feed,$item) {
411 require_once('library/HTMLPurifier.auto.php');
412 require_once('include/html2bbcode.php');
414 $best_photo = array();
418 $author = $item->get_author();
420 $res['author-name'] = unxmlify($author->get_name());
421 $res['author-link'] = unxmlify($author->get_link());
424 $res['author-name'] = unxmlify($feed->get_title());
425 $res['author-link'] = unxmlify($feed->get_permalink());
427 $res['uri'] = unxmlify($item->get_id());
428 $res['title'] = unxmlify($item->get_title());
429 $res['body'] = unxmlify($item->get_content());
430 $res['plink'] = unxmlify($item->get_link(0));
432 // removing the content of the title if its identically to the body
433 // This helps with auto generated titles e.g. from tumblr
434 if (title_is_body($res["title"], $res["body"]))
438 $base_url = implode('/', array_slice(explode('/',$res['plink']),0,3));
442 // look for a photo. We should check media size and find the best one,
443 // but for now let's just find any author photo
445 $rawauthor = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
447 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
448 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
449 foreach($base as $link) {
450 if(!x($res, 'author-avatar') || !$res['author-avatar']) {
451 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
452 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
457 $rawactor = $item->get_item_tags(NAMESPACE_ACTIVITY, 'actor');
459 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
460 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
461 if($base && count($base)) {
462 foreach($base as $link) {
463 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
464 $res['author-link'] = unxmlify($link['attribs']['']['href']);
465 if(!x($res, 'author-avatar') || !$res['author-avatar']) {
466 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
467 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
473 // No photo/profile-link on the item - look at the feed level
475 if((! (x($res,'author-link'))) || (! (x($res,'author-avatar')))) {
476 $rawauthor = $feed->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
477 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
478 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
479 foreach($base as $link) {
480 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
481 $res['author-link'] = unxmlify($link['attribs']['']['href']);
482 if(! $res['author-avatar']) {
483 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
484 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
489 $rawactor = $feed->get_feed_tags(NAMESPACE_ACTIVITY, 'subject');
491 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
492 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
494 if($base && count($base)) {
495 foreach($base as $link) {
496 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
497 $res['author-link'] = unxmlify($link['attribs']['']['href']);
498 if(! (x($res,'author-avatar'))) {
499 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
500 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
507 $apps = $item->get_item_tags(NAMESPACE_STATUSNET,'notice_info');
508 if($apps && $apps[0]['attribs']['']['source']) {
509 $res['app'] = strip_tags(unxmlify($apps[0]['attribs']['']['source']));
510 if($res['app'] === 'web')
511 $res['app'] = 'OStatus';
514 // base64 encoded json structure representing Diaspora signature
516 $dsig = $item->get_item_tags(NAMESPACE_DFRN,'diaspora_signature');
518 $res['dsprsig'] = unxmlify($dsig[0]['data']);
521 $dguid = $item->get_item_tags(NAMESPACE_DFRN,'diaspora_guid');
523 $res['guid'] = unxmlify($dguid[0]['data']);
525 $bm = $item->get_item_tags(NAMESPACE_DFRN,'bookmark');
527 $res['bookmark'] = ((unxmlify($bm[0]['data']) === 'true') ? 1 : 0);
531 * If there's a copy of the body content which is guaranteed to have survived mangling in transit, use it.
534 $have_real_body = false;
536 $rawenv = $item->get_item_tags(NAMESPACE_DFRN, 'env');
538 $have_real_body = true;
539 $res['body'] = $rawenv[0]['data'];
540 $res['body'] = str_replace(array(' ',"\t","\r","\n"), array('','','',''),$res['body']);
541 // make sure nobody is trying to sneak some html tags by us
542 $res['body'] = notags(base64url_decode($res['body']));
546 $res['body'] = limit_body_size($res['body']);
548 // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust
549 // the content type. Our own network only emits text normally, though it might have been converted to
550 // html if we used a pubsubhubbub transport. But if we see even one html tag in our text, we will
551 // have to assume it is all html and needs to be purified.
553 // It doesn't matter all that much security wise - because before this content is used anywhere, we are
554 // going to escape any tags we find regardless, but this lets us import a limited subset of html from
555 // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining
558 if((strpos($res['body'],'<') !== false) && (strpos($res['body'],'>') !== false)) {
560 $res['body'] = reltoabs($res['body'],$base_url);
562 $res['body'] = html2bb_video($res['body']);
564 $res['body'] = oembed_html2bbcode($res['body']);
566 $config = HTMLPurifier_Config::createDefault();
567 $config->set('Cache.DefinitionImpl', null);
569 // we shouldn't need a whitelist, because the bbcode converter
570 // will strip out any unsupported tags.
572 $purifier = new HTMLPurifier($config);
573 $res['body'] = $purifier->purify($res['body']);
575 $res['body'] = @html2bbcode($res['body']);
579 elseif(! $have_real_body) {
581 // it's not one of our messages and it has no tags
582 // so it's probably just text. We'll escape it just to be safe.
584 $res['body'] = escape_tags($res['body']);
588 // this tag is obsolete but we keep it for really old sites
590 $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
591 if($allow && $allow[0]['data'] == 1)
592 $res['last-child'] = 1;
594 $res['last-child'] = 0;
596 $private = $item->get_item_tags(NAMESPACE_DFRN,'private');
597 if($private && intval($private[0]['data']) > 0)
598 $res['private'] = intval($private[0]['data']);
602 $extid = $item->get_item_tags(NAMESPACE_DFRN,'extid');
603 if($extid && $extid[0]['data'])
604 $res['extid'] = $extid[0]['data'];
606 $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
608 $res['location'] = unxmlify($rawlocation[0]['data']);
611 $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
613 $res['created'] = unxmlify($rawcreated[0]['data']);
616 $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
618 $res['edited'] = unxmlify($rawedited[0]['data']);
620 if((x($res,'edited')) && (! (x($res,'created'))))
621 $res['created'] = $res['edited'];
623 if(! $res['created'])
624 $res['created'] = $item->get_date('c');
627 $res['edited'] = $item->get_date('c');
630 // Disallow time travelling posts
632 $d1 = strtotime($res['created']);
633 $d2 = strtotime($res['edited']);
634 $d3 = strtotime('now');
637 $res['created'] = datetime_convert();
639 $res['edited'] = datetime_convert();
641 $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
642 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
643 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
644 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
645 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
646 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
647 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
648 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
649 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
651 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
652 $base = $rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
654 foreach($base as $link) {
655 if(!x($res, 'owner-avatar') || !$res['owner-avatar']) {
656 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
657 $res['owner-avatar'] = unxmlify($link['attribs']['']['href']);
662 $rawgeo = $item->get_item_tags(NAMESPACE_GEORSS,'point');
664 $res['coord'] = unxmlify($rawgeo[0]['data']);
667 $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
669 // select between supported verbs
672 $res['verb'] = unxmlify($rawverb[0]['data']);
675 // translate OStatus unfollow to activity streams if it happened to get selected
677 if((x($res,'verb')) && ($res['verb'] === 'http://ostatus.org/schema/1.0/unfollow'))
678 $res['verb'] = ACTIVITY_UNFOLLOW;
680 $cats = $item->get_categories();
683 foreach($cats as $cat) {
684 $term = $cat->get_term();
686 $term = $cat->get_label();
687 $scheme = $cat->get_scheme();
688 if($scheme && $term && stristr($scheme,'X-DFRN:'))
689 $tag_arr[] = substr($scheme,7,1) . '[url=' . unxmlify(substr($scheme,9)) . ']' . unxmlify($term) . '[/url]';
691 $tag_arr[] = notags(trim($term));
693 $res['tag'] = implode(',', $tag_arr);
696 $attach = $item->get_enclosures();
699 foreach($attach as $att) {
700 $len = intval($att->get_length());
701 $link = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_link()))));
702 $title = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_title()))));
703 $type = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_type()))));
704 if(strpos($type,';'))
705 $type = substr($type,0,strpos($type,';'));
706 if((! $link) || (strpos($link,'http') !== 0))
712 $type = 'application/octet-stream';
714 $att_arr[] = '[attach]href="' . $link . '" length="' . $len . '" type="' . $type . '" title="' . $title . '"[/attach]';
716 $res['attach'] = implode(',', $att_arr);
719 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
722 $res['object'] = '<object>' . "\n";
723 $child = $rawobj[0]['child'];
724 if($child[NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
725 $res['object-type'] = $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'];
726 $res['object'] .= '<type>' . $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
728 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'id') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
729 $res['object'] .= '<id>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
730 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'link') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
731 $res['object'] .= '<link>' . encode_rel_links($child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
732 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'title') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
733 $res['object'] .= '<title>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
734 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'content') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
735 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
737 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
738 // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
739 $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
740 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
742 $body = html2bb_video($body);
744 $config = HTMLPurifier_Config::createDefault();
745 $config->set('Cache.DefinitionImpl', null);
747 $purifier = new HTMLPurifier($config);
748 $body = $purifier->purify($body);
749 $body = html2bbcode($body);
752 $res['object'] .= '<content>' . $body . '</content>' . "\n";
755 $res['object'] .= '</object>' . "\n";
758 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'target');
761 $res['target'] = '<target>' . "\n";
762 $child = $rawobj[0]['child'];
763 if($child[NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
764 $res['target'] .= '<type>' . $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
766 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'id') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
767 $res['target'] .= '<id>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
768 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'link') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
769 $res['target'] .= '<link>' . encode_rel_links($child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
770 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'data') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
771 $res['target'] .= '<title>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
772 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'data') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
773 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
775 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
776 // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
777 $res['target'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
778 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
780 $body = html2bb_video($body);
782 $config = HTMLPurifier_Config::createDefault();
783 $config->set('Cache.DefinitionImpl', null);
785 $purifier = new HTMLPurifier($config);
786 $body = $purifier->purify($body);
787 $body = html2bbcode($body);
790 $res['target'] .= '<content>' . $body . '</content>' . "\n";
793 $res['target'] .= '</target>' . "\n";
796 // This is some experimental stuff. By now retweets are shown with "RT:"
797 // But: There is data so that the message could be shown similar to native retweets
798 // There is some better way to parse this array - but it didn't worked for me.
799 $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"];
800 if (is_array($child)) {
801 logger('get_atom_elements: Looking for status.net repeated message');
803 $message = $child["http://activitystrea.ms/spec/1.0/"]["object"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["content"][0]["data"];
804 $orig_uri = $child["http://activitystrea.ms/spec/1.0/"]["object"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["id"][0]["data"];
805 $author = $child[SIMPLEPIE_NAMESPACE_ATOM_10]["author"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10];
806 $uri = $author["uri"][0]["data"];
807 $name = $author["name"][0]["data"];
808 $avatar = @array_shift($author["link"][2]["attribs"]);
809 $avatar = $avatar["href"];
811 if (($name != "") and ($uri != "") and ($avatar != "") and ($message != "")) {
812 logger('get_atom_elements: fixing sender of repeated message.');
814 if (intval(get_config('system','new_share'))) {
815 $prefix = "[share author='".str_replace("'", "'",$name).
817 "' avatar='".$avatar.
818 "' link='".$orig_uri."']";
820 $res["body"] = $prefix.html2bbcode($message)."[/share]";
822 $res["owner-name"] = $res["author-name"];
823 $res["owner-link"] = $res["author-link"];
824 $res["owner-avatar"] = $res["author-avatar"];
826 $res["author-name"] = $name;
827 $res["author-link"] = $uri;
828 $res["author-avatar"] = $avatar;
830 $res["body"] = html2bbcode($message);
835 // Search for ostatus conversation url
836 $links = $item->feed->data["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["feed"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["entry"][0]["child"]["http://www.w3.org/2005/Atom"]["link"];
838 if (is_array($links)) {
839 foreach ($links as $link) {
840 $conversation = array_shift($link["attribs"]);
842 if ($conversation["rel"] == "ostatus:conversation") {
843 $res["ostatus_conversation"] = $conversation["href"];
844 logger('get_atom_elements: found conversation url '.$res["ostatus_conversation"]);
849 $arr = array('feed' => $feed, 'item' => $item, 'result' => $res);
851 call_hooks('parse_atom', $arr);
853 //if (($res["title"] != "") or (strpos($res["body"], "RT @") > 0)) {
854 //if (strpos($res["body"], "RT @") !== false) {
855 /*if (strpos($res["body"], "@") !== false) {
856 $debugfile = tempnam("/var/www/virtual/pirati.ca/phptmp/", "item-res2-");
857 file_put_contents($debugfile, serialize($arr));
863 function encode_rel_links($links) {
865 if(! ((is_array($links)) && (count($links))))
867 foreach($links as $link) {
869 if($link['attribs']['']['rel'])
870 $o .= 'rel="' . $link['attribs']['']['rel'] . '" ';
871 if($link['attribs']['']['type'])
872 $o .= 'type="' . $link['attribs']['']['type'] . '" ';
873 if($link['attribs']['']['href'])
874 $o .= 'href="' . $link['attribs']['']['href'] . '" ';
875 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['width'])
876 $o .= 'media:width="' . $link['attribs'][NAMESPACE_MEDIA]['width'] . '" ';
877 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['height'])
878 $o .= 'media:height="' . $link['attribs'][NAMESPACE_MEDIA]['height'] . '" ';
886 function item_store($arr,$force_parent = false) {
888 // If a Diaspora signature structure was passed in, pull it out of the
889 // item array and set it aside for later storage.
892 if(x($arr,'dsprsig')) {
893 $dsprsig = json_decode(base64_decode($arr['dsprsig']));
894 unset($arr['dsprsig']);
897 // if an OStatus conversation url was passed in, it is stored and then
898 // removed from the array.
899 $ostatus_conversation = null;
901 if (isset($arr["ostatus_conversation"])) {
902 $ostatus_conversation = $arr["ostatus_conversation"];
903 unset($arr["ostatus_conversation"]);
906 if(x($arr, 'gravity'))
907 $arr['gravity'] = intval($arr['gravity']);
908 elseif($arr['parent-uri'] === $arr['uri'])
910 elseif(activity_match($arr['verb'],ACTIVITY_POST))
913 $arr['gravity'] = 6; // extensible catchall
916 $arr['type'] = 'remote';
918 // Shouldn't happen but we want to make absolutely sure it doesn't leak from a plugin.
920 if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false))
921 $arr['body'] = strip_tags($arr['body']);
924 if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
925 require_once('library/langdet/Text/LanguageDetect.php');
926 $naked_body = preg_replace('/\[(.+?)\]/','',$arr['body']);
927 $l = new Text_LanguageDetect;
928 //$lng = $l->detectConfidence($naked_body);
929 //$arr['postopts'] = (($lng['language']) ? 'lang=' . $lng['language'] . ';' . $lng['confidence'] : '');
930 $lng = $l->detect($naked_body, 3);
932 if (sizeof($lng) > 0) {
935 foreach ($lng as $language => $score) {
941 $postopts .= $language.";".$score;
943 $arr['postopts'] = $postopts;
947 $arr['wall'] = ((x($arr,'wall')) ? intval($arr['wall']) : 0);
948 $arr['uri'] = ((x($arr,'uri')) ? notags(trim($arr['uri'])) : random_string());
949 $arr['extid'] = ((x($arr,'extid')) ? notags(trim($arr['extid'])) : '');
950 $arr['author-name'] = ((x($arr,'author-name')) ? notags(trim($arr['author-name'])) : '');
951 $arr['author-link'] = ((x($arr,'author-link')) ? notags(trim($arr['author-link'])) : '');
952 $arr['author-avatar'] = ((x($arr,'author-avatar')) ? notags(trim($arr['author-avatar'])) : '');
953 $arr['owner-name'] = ((x($arr,'owner-name')) ? notags(trim($arr['owner-name'])) : '');
954 $arr['owner-link'] = ((x($arr,'owner-link')) ? notags(trim($arr['owner-link'])) : '');
955 $arr['owner-avatar'] = ((x($arr,'owner-avatar')) ? notags(trim($arr['owner-avatar'])) : '');
956 $arr['created'] = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
957 $arr['edited'] = ((x($arr,'edited') !== false) ? datetime_convert('UTC','UTC',$arr['edited']) : datetime_convert());
958 $arr['commented'] = datetime_convert();
959 $arr['received'] = datetime_convert();
960 $arr['changed'] = datetime_convert();
961 $arr['title'] = ((x($arr,'title')) ? notags(trim($arr['title'])) : '');
962 $arr['location'] = ((x($arr,'location')) ? notags(trim($arr['location'])) : '');
963 $arr['coord'] = ((x($arr,'coord')) ? notags(trim($arr['coord'])) : '');
964 $arr['last-child'] = ((x($arr,'last-child')) ? intval($arr['last-child']) : 0 );
965 $arr['visible'] = ((x($arr,'visible') !== false) ? intval($arr['visible']) : 1 );
967 $arr['parent-uri'] = ((x($arr,'parent-uri')) ? notags(trim($arr['parent-uri'])) : '');
968 $arr['verb'] = ((x($arr,'verb')) ? notags(trim($arr['verb'])) : '');
969 $arr['object-type'] = ((x($arr,'object-type')) ? notags(trim($arr['object-type'])) : '');
970 $arr['object'] = ((x($arr,'object')) ? trim($arr['object']) : '');
971 $arr['target-type'] = ((x($arr,'target-type')) ? notags(trim($arr['target-type'])) : '');
972 $arr['target'] = ((x($arr,'target')) ? trim($arr['target']) : '');
973 $arr['plink'] = ((x($arr,'plink')) ? notags(trim($arr['plink'])) : '');
974 $arr['allow_cid'] = ((x($arr,'allow_cid')) ? trim($arr['allow_cid']) : '');
975 $arr['allow_gid'] = ((x($arr,'allow_gid')) ? trim($arr['allow_gid']) : '');
976 $arr['deny_cid'] = ((x($arr,'deny_cid')) ? trim($arr['deny_cid']) : '');
977 $arr['deny_gid'] = ((x($arr,'deny_gid')) ? trim($arr['deny_gid']) : '');
978 $arr['private'] = ((x($arr,'private')) ? intval($arr['private']) : 0 );
979 $arr['bookmark'] = ((x($arr,'bookmark')) ? intval($arr['bookmark']) : 0 );
980 $arr['body'] = ((x($arr,'body')) ? trim($arr['body']) : '');
981 $arr['tag'] = ((x($arr,'tag')) ? notags(trim($arr['tag'])) : '');
982 $arr['attach'] = ((x($arr,'attach')) ? notags(trim($arr['attach'])) : '');
983 $arr['app'] = ((x($arr,'app')) ? notags(trim($arr['app'])) : '');
984 $arr['origin'] = ((x($arr,'origin')) ? intval($arr['origin']) : 0 );
985 $arr['guid'] = ((x($arr,'guid')) ? notags(trim($arr['guid'])) : get_guid());
988 $arr['thr-parent'] = $arr['parent-uri'];
989 if($arr['parent-uri'] === $arr['uri']) {
992 $allow_cid = $arr['allow_cid'];
993 $allow_gid = $arr['allow_gid'];
994 $deny_cid = $arr['deny_cid'];
995 $deny_gid = $arr['deny_gid'];
999 // find the parent and snarf the item id and ACLs
1000 // and anything else we need to inherit
1002 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d ORDER BY `id` ASC LIMIT 1",
1003 dbesc($arr['parent-uri']),
1009 // is the new message multi-level threaded?
1010 // even though we don't support it now, preserve the info
1011 // and re-attach to the conversation parent.
1013 if($r[0]['uri'] != $r[0]['parent-uri']) {
1014 $arr['parent-uri'] = $r[0]['parent-uri'];
1015 $z = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `parent-uri` = '%s' AND `uid` = %d
1016 ORDER BY `id` ASC LIMIT 1",
1017 dbesc($r[0]['parent-uri']),
1018 dbesc($r[0]['parent-uri']),
1025 $parent_id = $r[0]['id'];
1026 $parent_deleted = $r[0]['deleted'];
1027 $allow_cid = $r[0]['allow_cid'];
1028 $allow_gid = $r[0]['allow_gid'];
1029 $deny_cid = $r[0]['deny_cid'];
1030 $deny_gid = $r[0]['deny_gid'];
1031 $arr['wall'] = $r[0]['wall'];
1033 // if the parent is private, force privacy for the entire conversation
1034 // This differs from the above settings as it subtly allows comments from
1035 // email correspondents to be private even if the overall thread is not.
1037 if($r[0]['private'])
1038 $arr['private'] = $r[0]['private'];
1040 // Edge case. We host a public forum that was originally posted to privately.
1041 // The original author commented, but as this is a comment, the permissions
1042 // weren't fixed up so it will still show the comment as private unless we fix it here.
1044 if((intval($r[0]['forum_mode']) == 1) && (! $r[0]['private']))
1045 $arr['private'] = 0;
1049 // Allow one to see reply tweets from status.net even when
1050 // we don't have or can't see the original post.
1053 logger('item_store: $force_parent=true, reply converted to top-level post.');
1055 $arr['parent-uri'] = $arr['uri'];
1056 $arr['gravity'] = 0;
1059 logger('item_store: item parent was not found - ignoring item');
1063 $parent_deleted = 0;
1067 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1071 if($r && count($r)) {
1072 logger('item-store: duplicate item ignored. ' . print_r($arr,true));
1076 call_hooks('post_remote',$arr);
1078 if(x($arr,'cancel')) {
1079 logger('item_store: post cancelled by plugin.');
1085 logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
1087 $r = dbq("INSERT INTO `item` (`"
1088 . implode("`, `", array_keys($arr))
1090 . implode("', '", array_values($arr))
1093 // find the item we just created
1095 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d ORDER BY `id` ASC ",
1096 $arr['uri'], // already dbesc'd
1101 $current_post = $r[0]['id'];
1102 logger('item_store: created item ' . $current_post);
1103 create_tags_from_item($r[0]['id']);
1105 logger('item_store: could not locate created item');
1109 logger('item_store: duplicated post occurred. Removing duplicates.');
1110 q("DELETE FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `id` != %d ",
1112 intval($arr['uid']),
1113 intval($current_post)
1117 if((! $parent_id) || ($arr['parent-uri'] === $arr['uri']))
1118 $parent_id = $current_post;
1120 if(strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid))
1123 $private = $arr['private'];
1125 // Set parent id - and also make sure to inherit the parent's ACL's.
1127 $r = q("UPDATE `item` SET `parent` = %d, `allow_cid` = '%s', `allow_gid` = '%s',
1128 `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d, `deleted` = %d WHERE `id` = %d LIMIT 1",
1135 intval($parent_deleted),
1136 intval($current_post)
1138 create_tags_from_item($current_post);
1140 // Complete ostatus threads
1141 if ($ostatus_conversation)
1142 complete_conversation($current_post, $ostatus_conversation);
1144 $arr['id'] = $current_post;
1145 $arr['parent'] = $parent_id;
1146 $arr['allow_cid'] = $allow_cid;
1147 $arr['allow_gid'] = $allow_gid;
1148 $arr['deny_cid'] = $deny_cid;
1149 $arr['deny_gid'] = $deny_gid;
1150 $arr['private'] = $private;
1151 $arr['deleted'] = $parent_deleted;
1152 call_hooks('post_remote_end',$arr);
1154 // update the commented timestamp on the parent
1156 q("UPDATE `item` set `commented` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
1157 dbesc(datetime_convert()),
1158 dbesc(datetime_convert()),
1163 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
1164 intval($current_post),
1165 dbesc($dsprsig->signed_text),
1166 dbesc($dsprsig->signature),
1167 dbesc($dsprsig->signer)
1173 * If this is now the last-child, force all _other_ children of this parent to *not* be last-child
1176 if($arr['last-child']) {
1177 $r = q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d AND `id` != %d",
1179 intval($arr['uid']),
1180 intval($current_post)
1184 tag_deliver($arr['uid'],$current_post);
1186 // Store the fresh generated item into the cache
1187 $cachefile = get_cachefile($arr["guid"]."-".hash("md5", $arr['body']));
1189 if (($cachefile != '') AND !file_exists($cachefile)) {
1190 $s = prepare_text($arr['body']);
1192 $stamp1 = microtime(true);
1193 file_put_contents($cachefile, $s);
1194 $a->save_timestamp($stamp1, "file");
1195 logger('item_store: put item '.$current_post.' into cachefile '.$cachefile);
1198 return $current_post;
1201 function get_item_contact($item,$contacts) {
1202 if(! count($contacts) || (! is_array($item)))
1204 foreach($contacts as $contact) {
1205 if($contact['id'] == $item['contact-id']) {
1207 break; // NOTREACHED
1214 function tag_deliver($uid,$item_id) {
1216 // look for mention tags and setup a second delivery chain for forum/community posts if appropriate
1222 $u = q("select * from user where uid = %d limit 1",
1228 $community_page = (($u[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
1229 $prvgroup = (($u[0]['page-flags'] == PAGE_PRVGROUP) ? true : false);
1232 $i = q("select * from item where id = %d and uid = %d limit 1",
1241 $link = normalise_link($a->get_baseurl() . '/profile/' . $u[0]['nickname']);
1243 // Diaspora uses their own hardwired link URL in @-tags
1244 // instead of the one we supply with webfinger
1246 $dlink = normalise_link($a->get_baseurl() . '/u/' . $u[0]['nickname']);
1248 $cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism',$item['body'],$matches,PREG_SET_ORDER);
1250 foreach($matches as $mtch) {
1251 if(link_compare($link,$mtch[1]) || link_compare($dlink,$mtch[1])) {
1253 logger('tag_deliver: mention found: ' . $mtch[2]);
1261 // send a notification
1263 // use a local photo if we have one
1265 $r = q("select * from contact where uid = %d and nurl = '%s' limit 1",
1266 intval($u[0]['uid']),
1267 dbesc(normalise_link($item['author-link']))
1269 $photo = (($r && count($r)) ? $r[0]['thumb'] : $item['author-avatar']);
1272 require_once('include/enotify.php');
1274 'type' => NOTIFY_TAGSELF,
1275 'notify_flags' => $u[0]['notify-flags'],
1276 'language' => $u[0]['language'],
1277 'to_name' => $u[0]['username'],
1278 'to_email' => $u[0]['email'],
1279 'uid' => $u[0]['uid'],
1281 'link' => $a->get_baseurl() . '/display/' . $u[0]['nickname'] . '/' . $item['id'],
1282 'source_name' => $item['author-name'],
1283 'source_link' => $item['author-link'],
1284 'source_photo' => $photo,
1285 'verb' => ACTIVITY_TAG,
1290 $arr = array('item' => $item, 'user' => $u[0], 'contact' => $r[0]);
1292 call_hooks('tagged', $arr);
1294 if((! $community_page) && (! $prvgroup))
1298 // tgroup delivery - setup a second delivery chain
1299 // prevent delivery looping - only proceed
1300 // if the message originated elsewhere and is a top-level post
1302 if(($item['wall']) || ($item['origin']) || ($item['id'] != $item['parent']))
1305 // now change this copy of the post to a forum head message and deliver to all the tgroup members
1308 $c = q("select name, url, thumb from contact where self = 1 and uid = %d limit 1",
1309 intval($u[0]['uid'])
1314 // also reset all the privacy bits to the forum default permissions
1316 $private = ($u[0]['allow_cid'] || $u[0]['allow_gid'] || $u[0]['deny_cid'] || $u[0]['deny_gid']) ? 1 : 0;
1318 $forum_mode = (($prvgroup) ? 2 : 1);
1320 q("update item set wall = 1, origin = 1, forum_mode = %d, `owner-name` = '%s', `owner-link` = '%s', `owner-avatar` = '%s',
1321 `private` = %d, `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s' where id = %d limit 1",
1322 intval($forum_mode),
1323 dbesc($c[0]['name']),
1324 dbesc($c[0]['url']),
1325 dbesc($c[0]['thumb']),
1327 dbesc($u[0]['allow_cid']),
1328 dbesc($u[0]['allow_gid']),
1329 dbesc($u[0]['deny_cid']),
1330 dbesc($u[0]['deny_gid']),
1334 proc_run('php','include/notifier.php','tgroup',$item_id);
1340 function tgroup_check($uid,$item) {
1346 // check that the message originated elsewhere and is a top-level post
1348 if(($item['wall']) || ($item['origin']) || ($item['uri'] != $item['parent-uri']))
1352 $u = q("select * from user where uid = %d limit 1",
1358 $community_page = (($u[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
1359 $prvgroup = (($u[0]['page-flags'] == PAGE_PRVGROUP) ? true : false);
1362 $link = normalise_link($a->get_baseurl() . '/profile/' . $u[0]['nickname']);
1364 // Diaspora uses their own hardwired link URL in @-tags
1365 // instead of the one we supply with webfinger
1367 $dlink = normalise_link($a->get_baseurl() . '/u/' . $u[0]['nickname']);
1369 $cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism',$item['body'],$matches,PREG_SET_ORDER);
1371 foreach($matches as $mtch) {
1372 if(link_compare($link,$mtch[1]) || link_compare($dlink,$mtch[1])) {
1374 logger('tgroup_check: mention found: ' . $mtch[2]);
1382 if((! $community_page) && (! $prvgroup))
1396 function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
1400 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
1402 if($contact['duplex'] && $contact['dfrn-id'])
1403 $idtosend = '0:' . $orig_id;
1404 if($contact['duplex'] && $contact['issued-id'])
1405 $idtosend = '1:' . $orig_id;
1407 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
1409 $rino_enable = get_config('system','rino_encrypt');
1414 $ssl_val = intval(get_config('system','ssl_policy'));
1418 case SSL_POLICY_FULL:
1419 $ssl_policy = 'full';
1421 case SSL_POLICY_SELFSIGN:
1422 $ssl_policy = 'self';
1424 case SSL_POLICY_NONE:
1426 $ssl_policy = 'none';
1430 $url = $contact['notify'] . '&dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino=1' : '');
1432 logger('dfrn_deliver: ' . $url);
1434 $xml = fetch_url($url);
1436 $curl_stat = $a->get_curl_code();
1438 return(-1); // timed out
1440 logger('dfrn_deliver: ' . $xml, LOGGER_DATA);
1445 if(strpos($xml,'<?xml') === false) {
1446 logger('dfrn_deliver: no valid XML returned');
1447 logger('dfrn_deliver: returned XML: ' . $xml, LOGGER_DATA);
1451 $res = parse_xml_string($xml);
1453 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
1454 return (($res->status) ? $res->status : 3);
1456 $postvars = array();
1457 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
1458 $challenge = hex2bin((string) $res->challenge);
1459 $perm = (($res->perm) ? $res->perm : null);
1460 $dfrn_version = (float) (($res->dfrn_version) ? $res->dfrn_version : 2.0);
1461 $rino_allowed = ((intval($res->rino) === 1) ? 1 : 0);
1462 $page = (($owner['page-flags'] == PAGE_COMMUNITY) ? 1 : 0);
1464 if($owner['page-flags'] == PAGE_PRVGROUP)
1467 $final_dfrn_id = '';
1470 if((($perm == 'rw') && (! intval($contact['writable'])))
1471 || (($perm == 'r') && (intval($contact['writable'])))) {
1472 q("update contact set writable = %d where id = %d limit 1",
1473 intval(($perm == 'rw') ? 1 : 0),
1474 intval($contact['id'])
1476 $contact['writable'] = (string) 1 - intval($contact['writable']);
1480 if(($contact['duplex'] && strlen($contact['pubkey']))
1481 || ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
1482 || ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey']))) {
1483 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
1484 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
1487 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
1488 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
1491 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
1493 if(strpos($final_dfrn_id,':') == 1)
1494 $final_dfrn_id = substr($final_dfrn_id,2);
1496 if($final_dfrn_id != $orig_id) {
1497 logger('dfrn_deliver: wrong dfrn_id.');
1498 // did not decode properly - cannot trust this site
1502 $postvars['dfrn_id'] = $idtosend;
1503 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
1505 $postvars['dissolve'] = '1';
1508 if((($contact['rel']) && ($contact['rel'] != CONTACT_IS_SHARING) && (! $contact['blocked'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
1509 $postvars['data'] = $atom;
1510 $postvars['perm'] = 'rw';
1513 $postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
1514 $postvars['perm'] = 'r';
1517 $postvars['ssl_policy'] = $ssl_policy;
1520 $postvars['page'] = $page;
1522 if($rino && $rino_allowed && (! $dissolve)) {
1523 $key = substr(random_string(),0,16);
1524 $data = bin2hex(aes_encrypt($postvars['data'],$key));
1525 $postvars['data'] = $data;
1526 logger('rino: sent key = ' . $key, LOGGER_DEBUG);
1529 if($dfrn_version >= 2.1) {
1530 if(($contact['duplex'] && strlen($contact['pubkey']))
1531 || ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
1532 || ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey']))) {
1534 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
1537 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
1541 if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
1542 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
1545 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
1549 logger('md5 rawkey ' . md5($postvars['key']));
1551 $postvars['key'] = bin2hex($postvars['key']);
1554 logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);
1556 $xml = post_url($contact['notify'],$postvars);
1558 logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
1560 $curl_stat = $a->get_curl_code();
1561 if((! $curl_stat) || (! strlen($xml)))
1562 return(-1); // timed out
1564 if(($curl_stat == 503) && (stristr($a->get_curl_headers(),'retry-after')))
1567 if(strpos($xml,'<?xml') === false) {
1568 logger('dfrn_deliver: phase 2: no valid XML returned');
1569 logger('dfrn_deliver: phase 2: returned XML: ' . $xml, LOGGER_DATA);
1573 if($contact['term-date'] != '0000-00-00 00:00:00') {
1574 logger("dfrn_deliver: $url back from the dead - removing mark for death");
1575 require_once('include/Contact.php');
1576 unmark_for_death($contact);
1579 $res = parse_xml_string($xml);
1581 return $res->status;
1587 * consume_feed - process atom feed and update anything/everything we might need to update
1589 * $xml = the (atom) feed to consume - RSS isn't as fully supported but may work for simple feeds.
1591 * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
1592 * It is this person's stuff that is going to be updated.
1593 * $contact = the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
1594 * from an external network and MAY create an appropriate contact record. Otherwise, we MUST
1595 * have a contact record.
1596 * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or
1597 * might not) try and subscribe to it.
1598 * $datedir sorts in reverse order
1599 * $pass - by default ($pass = 0) we cannot guarantee that a parent item has been
1600 * imported prior to its children being seen in the stream unless we are certain
1601 * of how the feed is arranged/ordered.
1602 * With $pass = 1, we only pull parent items out of the stream.
1603 * With $pass = 2, we only pull children (comments/likes).
1605 * So running this twice, first with pass 1 and then with pass 2 will do the right
1606 * thing regardless of feed ordering. This won't be adequate in a fully-threaded
1607 * model where comments can have sub-threads. That would require some massive sorting
1608 * to get all the feed items into a mostly linear ordering, and might still require
1612 function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0) {
1614 require_once('library/simplepie/simplepie.inc');
1616 if(! strlen($xml)) {
1617 logger('consume_feed: empty input');
1621 $feed = new SimplePie();
1622 $feed->set_raw_data($xml);
1624 $feed->enable_order_by_date(true);
1626 $feed->enable_order_by_date(false);
1630 logger('consume_feed: Error parsing XML: ' . $feed->error());
1632 $permalink = $feed->get_permalink();
1634 // Check at the feed level for updated contact name and/or photo
1638 $photo_timestamp = '';
1642 $hubs = $feed->get_links('hub');
1643 logger('consume_feed: hubs: ' . print_r($hubs,true), LOGGER_DATA);
1646 $hub = implode(',', $hubs);
1648 $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'owner');
1650 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
1652 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
1653 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
1654 $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
1655 $new_name = $elems['name'][0]['data'];
1657 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
1658 $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
1659 $photo_url = $elems['link'][0]['attribs']['']['href'];
1662 if((x($rawtags[0]['child'], NAMESPACE_DFRN)) && (x($rawtags[0]['child'][NAMESPACE_DFRN],'birthday'))) {
1663 $birthday = datetime_convert('UTC','UTC', $rawtags[0]['child'][NAMESPACE_DFRN]['birthday'][0]['data']);
1667 if((is_array($contact)) && ($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
1668 logger('consume_feed: Updating photo for ' . $contact['name']);
1669 require_once("include/Photo.php");
1670 $photo_failure = false;
1671 $have_photo = false;
1673 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
1674 intval($contact['id']),
1675 intval($contact['uid'])
1678 $resource_id = $r[0]['resource-id'];
1682 $resource_id = photo_new_resource();
1685 $img_str = fetch_url($photo_url,true);
1686 // guess mimetype from headers or filename
1687 $type = guess_image_type($photo_url,true);
1690 $img = new Photo($img_str, $type);
1691 if($img->is_valid()) {
1693 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
1694 dbesc($resource_id),
1695 intval($contact['id']),
1696 intval($contact['uid'])
1700 $img->scaleImageSquare(175);
1702 $hash = $resource_id;
1703 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 4);
1705 $img->scaleImage(80);
1706 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 5);
1708 $img->scaleImage(48);
1709 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 6);
1713 q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
1714 WHERE `uid` = %d AND `id` = %d LIMIT 1",
1715 dbesc(datetime_convert()),
1716 dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.'.$img->getExt()),
1717 dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.'.$img->getExt()),
1718 dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.'.$img->getExt()),
1719 intval($contact['uid']),
1720 intval($contact['id'])
1725 if((is_array($contact)) && ($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
1726 $r = q("select * from contact where uid = %d and id = %d limit 1",
1727 intval($contact['uid']),
1728 intval($contact['id'])
1731 $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1732 dbesc(notags(trim($new_name))),
1733 dbesc(datetime_convert()),
1734 intval($contact['uid']),
1735 intval($contact['id'])
1738 // do our best to update the name on content items
1741 q("update item set `author-name` = '%s' where `author-name` = '%s' and `author-link` = '%s' and uid = %d",
1742 dbesc(notags(trim($new_name))),
1743 dbesc($r[0]['name']),
1744 dbesc($r[0]['url']),
1745 intval($contact['uid'])
1750 if(strlen($birthday)) {
1751 if(substr($birthday,0,4) != $contact['bdyear']) {
1752 logger('consume_feed: updating birthday: ' . $birthday);
1756 * Add new birthday event for this person
1758 * $bdtext is just a readable placeholder in case the event is shared
1759 * with others. We will replace it during presentation to our $importer
1760 * to contain a sparkle link and perhaps a photo.
1764 $bdtext = sprintf( t('%s\'s birthday'), $contact['name']);
1765 $bdtext2 = sprintf( t('Happy Birthday %s'), ' [url=' . $contact['url'] . ']' . $contact['name'] . '[/url]' ) ;
1768 $r = q("INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`summary`,`desc`,`type`)
1769 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
1770 intval($contact['uid']),
1771 intval($contact['id']),
1772 dbesc(datetime_convert()),
1773 dbesc(datetime_convert()),
1774 dbesc(datetime_convert('UTC','UTC', $birthday)),
1775 dbesc(datetime_convert('UTC','UTC', $birthday . ' + 1 day ')),
1784 q("UPDATE `contact` SET `bdyear` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
1785 dbesc(substr($birthday,0,4)),
1786 intval($contact['uid']),
1787 intval($contact['id'])
1790 // This function is called twice without reloading the contact
1791 // Make sure we only create one event. This is why &$contact
1792 // is a reference var in this function
1794 $contact['bdyear'] = substr($birthday,0,4);
1799 $community_page = 0;
1800 $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'community');
1802 $community_page = intval($rawtags[0]['data']);
1804 if(is_array($contact) && intval($contact['forum']) != $community_page) {
1805 q("update contact set forum = %d where id = %d limit 1",
1806 intval($community_page),
1807 intval($contact['id'])
1809 $contact['forum'] = (string) $community_page;
1813 // process any deleted entries
1815 $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
1816 if(is_array($del_entries) && count($del_entries) && $pass != 2) {
1817 foreach($del_entries as $dentry) {
1819 if(isset($dentry['attribs']['']['ref'])) {
1820 $uri = $dentry['attribs']['']['ref'];
1822 if(isset($dentry['attribs']['']['when'])) {
1823 $when = $dentry['attribs']['']['when'];
1824 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
1827 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
1829 if($deleted && is_array($contact)) {
1830 $r = q("SELECT `item`.*, `contact`.`self` FROM `item` left join `contact` on `item`.`contact-id` = `contact`.`id`
1831 WHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d AND NOT `item`.`file` LIKE '%%[%%' LIMIT 1",
1833 intval($importer['uid']),
1834 intval($contact['id'])
1839 if(! $item['deleted'])
1840 logger('consume_feed: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
1842 if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
1843 $xo = parse_xml_string($item['object'],false);
1844 $xt = parse_xml_string($item['target'],false);
1845 if($xt->type === ACTIVITY_OBJ_NOTE) {
1846 $i = q("select * from `item` where uri = '%s' and uid = %d limit 1",
1848 intval($importer['importer_uid'])
1852 // For tags, the owner cannot remove the tag on the author's copy of the post.
1854 $owner_remove = (($item['contact-id'] == $i[0]['contact-id']) ? true: false);
1855 $author_remove = (($item['origin'] && $item['self']) ? true : false);
1856 $author_copy = (($item['origin']) ? true : false);
1858 if($owner_remove && $author_copy)
1860 if($author_remove || $owner_remove) {
1861 $tags = explode(',',$i[0]['tag']);
1864 foreach($tags as $tag)
1865 if(trim($tag) !== trim($xo->body))
1866 $newtags[] = trim($tag);
1868 q("update item set tag = '%s' where id = %d limit 1",
1869 dbesc(implode(',',$newtags)),
1872 create_tags_from_item($i[0]['id']);
1878 if($item['uri'] == $item['parent-uri']) {
1879 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1880 `body` = '', `title` = ''
1881 WHERE `parent-uri` = '%s' AND `uid` = %d",
1883 dbesc(datetime_convert()),
1884 dbesc($item['uri']),
1885 intval($importer['uid'])
1887 create_tags_from_itemuri($item['uri'], $importer['uid']);
1890 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
1891 `body` = '', `title` = ''
1892 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1894 dbesc(datetime_convert()),
1896 intval($importer['uid'])
1898 create_tags_from_itemuri($uri, $importer['uid']);
1899 if($item['last-child']) {
1900 // ensure that last-child is set in case the comment that had it just got wiped.
1901 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
1902 dbesc(datetime_convert()),
1903 dbesc($item['parent-uri']),
1904 intval($item['uid'])
1906 // who is the last child now?
1907 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `moderated` = 0 AND `uid` = %d
1908 ORDER BY `created` DESC LIMIT 1",
1909 dbesc($item['parent-uri']),
1910 intval($importer['uid'])
1913 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
1924 // Now process the feed
1926 if($feed->get_item_quantity()) {
1928 logger('consume_feed: feed item count = ' . $feed->get_item_quantity());
1930 // in inverse date order
1932 $items = array_reverse($feed->get_items());
1934 $items = $feed->get_items();
1937 foreach($items as $item) {
1940 $item_id = $item->get_id();
1941 $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
1942 if(isset($rawthread[0]['attribs']['']['ref'])) {
1944 $parent_uri = $rawthread[0]['attribs']['']['ref'];
1947 if(($is_reply) && is_array($contact)) {
1952 // not allowed to post
1954 if($contact['rel'] == CONTACT_IS_FOLLOWER)
1958 // Have we seen it? If not, import it.
1960 $item_id = $item->get_id();
1961 $datarray = get_atom_elements($feed,$item);
1963 if((! x($datarray,'author-name')) && ($contact['network'] != NETWORK_DFRN))
1964 $datarray['author-name'] = $contact['name'];
1965 if((! x($datarray,'author-link')) && ($contact['network'] != NETWORK_DFRN))
1966 $datarray['author-link'] = $contact['url'];
1967 if((! x($datarray,'author-avatar')) && ($contact['network'] != NETWORK_DFRN))
1968 $datarray['author-avatar'] = $contact['thumb'];
1970 if((! x($datarray,'author-name')) || (! x($datarray,'author-link'))) {
1971 logger('consume_feed: no author information! ' . print_r($datarray,true));
1975 $force_parent = false;
1976 if($contact['network'] === NETWORK_OSTATUS || stristr($contact['url'],'twitter.com')) {
1977 if($contact['network'] === NETWORK_OSTATUS)
1978 $force_parent = true;
1979 if(strlen($datarray['title']))
1980 unset($datarray['title']);
1981 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
1982 dbesc(datetime_convert()),
1984 intval($importer['uid'])
1986 $datarray['last-child'] = 1;
1990 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1992 intval($importer['uid'])
1995 // Update content if 'updated' changes
1998 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
2000 // do not accept (ignore) an earlier edit than one we currently have.
2001 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
2004 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2005 dbesc($datarray['title']),
2006 dbesc($datarray['body']),
2007 dbesc($datarray['tag']),
2008 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
2010 intval($importer['uid'])
2012 create_tags_from_itemuri($item_id, $importer['uid']);
2015 // update last-child if it changes
2017 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
2018 if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
2019 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
2020 dbesc(datetime_convert()),
2022 intval($importer['uid'])
2024 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2025 intval($allow[0]['data']),
2026 dbesc(datetime_convert()),
2028 intval($importer['uid'])
2035 if(($contact['network'] === NETWORK_FEED) || (! strlen($contact['notify']))) {
2036 // one way feed - no remote comment ability
2037 $datarray['last-child'] = 0;
2039 $datarray['parent-uri'] = $parent_uri;
2040 $datarray['uid'] = $importer['uid'];
2041 $datarray['contact-id'] = $contact['id'];
2042 if((activity_match($datarray['verb'],ACTIVITY_LIKE)) || (activity_match($datarray['verb'],ACTIVITY_DISLIKE))) {
2043 $datarray['type'] = 'activity';
2044 $datarray['gravity'] = GRAVITY_LIKE;
2045 // only one like or dislike per person
2046 $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",
2047 intval($datarray['uid']),
2048 intval($datarray['contact-id']),
2049 dbesc($datarray['verb']),
2057 if(($datarray['verb'] === ACTIVITY_TAG) && ($datarray['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
2058 $xo = parse_xml_string($datarray['object'],false);
2059 $xt = parse_xml_string($datarray['target'],false);
2061 if($xt->type == ACTIVITY_OBJ_NOTE) {
2062 $r = q("select * from item where `uri` = '%s' AND `uid` = %d limit 1",
2064 intval($importer['importer_uid'])
2069 // extract tag, if not duplicate, add to parent item
2070 if($xo->id && $xo->content) {
2071 $newtag = '#[url=' . $xo->id . ']'. $xo->content . '[/url]';
2072 if(! (stristr($r[0]['tag'],$newtag))) {
2073 q("UPDATE item SET tag = '%s' WHERE id = %d LIMIT 1",
2074 dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . $newtag),
2077 create_tags_from_item($r[0]['id']);
2083 $r = item_store($datarray,$force_parent);
2089 // Head post of a conversation. Have we seen it? If not, import it.
2091 $item_id = $item->get_id();
2093 $datarray = get_atom_elements($feed,$item);
2095 if(is_array($contact)) {
2096 if((! x($datarray,'author-name')) && ($contact['network'] != NETWORK_DFRN))
2097 $datarray['author-name'] = $contact['name'];
2098 if((! x($datarray,'author-link')) && ($contact['network'] != NETWORK_DFRN))
2099 $datarray['author-link'] = $contact['url'];
2100 if((! x($datarray,'author-avatar')) && ($contact['network'] != NETWORK_DFRN))
2101 $datarray['author-avatar'] = $contact['thumb'];
2104 if((! x($datarray,'author-name')) || (! x($datarray,'author-link'))) {
2105 logger('consume_feed: no author information! ' . print_r($datarray,true));
2109 // special handling for events
2111 if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
2112 $ev = bbtoevent($datarray['body']);
2113 if(x($ev,'desc') && x($ev,'start')) {
2114 $ev['uid'] = $importer['uid'];
2115 $ev['uri'] = $item_id;
2116 $ev['edited'] = $datarray['edited'];
2117 $ev['private'] = $datarray['private'];
2119 if(is_array($contact))
2120 $ev['cid'] = $contact['id'];
2121 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2123 intval($importer['uid'])
2126 $ev['id'] = $r[0]['id'];
2127 $xyz = event_store($ev);
2132 if($contact['network'] === NETWORK_OSTATUS || stristr($contact['url'],'twitter.com')) {
2133 if(strlen($datarray['title']))
2134 unset($datarray['title']);
2135 $datarray['last-child'] = 1;
2139 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2141 intval($importer['uid'])
2144 // Update content if 'updated' changes
2147 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
2149 // do not accept (ignore) an earlier edit than one we currently have.
2150 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
2153 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2154 dbesc($datarray['title']),
2155 dbesc($datarray['body']),
2156 dbesc($datarray['tag']),
2157 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
2159 intval($importer['uid'])
2161 create_tags_from_itemuri($item_id, $importer['uid']);
2164 // update last-child if it changes
2166 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
2167 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
2168 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2169 intval($allow[0]['data']),
2170 dbesc(datetime_convert()),
2172 intval($importer['uid'])
2178 if(activity_match($datarray['verb'],ACTIVITY_FOLLOW)) {
2179 logger('consume-feed: New follower');
2180 new_follower($importer,$contact,$datarray,$item);
2183 if(activity_match($datarray['verb'],ACTIVITY_UNFOLLOW)) {
2184 lose_follower($importer,$contact,$datarray,$item);
2188 if(activity_match($datarray['verb'],ACTIVITY_REQ_FRIEND)) {
2189 logger('consume-feed: New friend request');
2190 new_follower($importer,$contact,$datarray,$item,true);
2193 if(activity_match($datarray['verb'],ACTIVITY_UNFRIEND)) {
2194 lose_sharer($importer,$contact,$datarray,$item);
2199 if(! is_array($contact))
2203 if(($contact['network'] === NETWORK_FEED) || (! strlen($contact['notify']))) {
2204 // one way feed - no remote comment ability
2205 $datarray['last-child'] = 0;
2207 if($contact['network'] === NETWORK_FEED)
2208 $datarray['private'] = 2;
2210 // This is my contact on another system, but it's really me.
2211 // Turn this into a wall post.
2213 if($contact['remote_self']) {
2214 $datarray['wall'] = 1;
2215 if($contact['network'] === NETWORK_FEED) {
2216 $datarray['private'] = 0;
2220 $datarray['parent-uri'] = $item_id;
2221 $datarray['uid'] = $importer['uid'];
2222 $datarray['contact-id'] = $contact['id'];
2224 if(! link_compare($datarray['owner-link'],$contact['url'])) {
2225 // The item owner info is not our contact. It's OK and is to be expected if this is a tgroup delivery,
2226 // but otherwise there's a possible data mixup on the sender's system.
2227 // the tgroup delivery code called from item_store will correct it if it's a forum,
2228 // but we're going to unconditionally correct it here so that the post will always be owned by our contact.
2229 logger('consume_feed: Correcting item owner.', LOGGER_DEBUG);
2230 $datarray['owner-name'] = $contact['name'];
2231 $datarray['owner-link'] = $contact['url'];
2232 $datarray['owner-avatar'] = $contact['thumb'];
2235 // We've allowed "followers" to reach this point so we can decide if they are
2236 // posting an @-tag delivery, which followers are allowed to do for certain
2237 // page types. Now that we've parsed the post, let's check if it is legit. Otherwise ignore it.
2239 if(($contact['rel'] == CONTACT_IS_FOLLOWER) && (! tgroup_check($importer['uid'],$datarray)))
2243 $r = item_store($datarray);
2251 function local_delivery($importer,$data) {
2254 logger(__function__, LOGGER_TRACE);
2256 if($importer['readonly']) {
2257 // We aren't receiving stuff from this person. But we will quietly ignore them
2258 // rather than a blatant "go away" message.
2259 logger('local_delivery: ignoring');
2264 // Consume notification feed. This may differ from consuming a public feed in several ways
2265 // - might contain email or friend suggestions
2266 // - might contain remote followup to our message
2267 // - in which case we need to accept it and then notify other conversants
2268 // - we may need to send various email notifications
2270 $feed = new SimplePie();
2271 $feed->set_raw_data($data);
2272 $feed->enable_order_by_date(false);
2277 logger('local_delivery: Error parsing XML: ' . $feed->error());
2280 // Check at the feed level for updated contact name and/or photo
2284 $photo_timestamp = '';
2288 $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'owner');
2290 // Fallback should not be needed here. If it isn't DFRN it won't have DFRN updated tags
2292 // $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
2295 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
2296 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
2297 $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
2298 $new_name = $elems['name'][0]['data'];
2300 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
2301 $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
2302 $photo_url = $elems['link'][0]['attribs']['']['href'];
2306 if(($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $importer['avatar-date'])) {
2307 logger('local_delivery: Updating photo for ' . $importer['name']);
2308 require_once("include/Photo.php");
2309 $photo_failure = false;
2310 $have_photo = false;
2312 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
2313 intval($importer['id']),
2314 intval($importer['importer_uid'])
2317 $resource_id = $r[0]['resource-id'];
2321 $resource_id = photo_new_resource();
2324 $img_str = fetch_url($photo_url,true);
2325 // guess mimetype from headers or filename
2326 $type = guess_image_type($photo_url,true);
2329 $img = new Photo($img_str, $type);
2330 if($img->is_valid()) {
2332 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
2333 dbesc($resource_id),
2334 intval($importer['id']),
2335 intval($importer['importer_uid'])
2339 $img->scaleImageSquare(175);
2341 $hash = $resource_id;
2342 $r = $img->store($importer['importer_uid'], $importer['id'], $hash, basename($photo_url), 'Contact Photos', 4);
2344 $img->scaleImage(80);
2345 $r = $img->store($importer['importer_uid'], $importer['id'], $hash, basename($photo_url), 'Contact Photos', 5);
2347 $img->scaleImage(48);
2348 $r = $img->store($importer['importer_uid'], $importer['id'], $hash, basename($photo_url), 'Contact Photos', 6);
2352 q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
2353 WHERE `uid` = %d AND `id` = %d LIMIT 1",
2354 dbesc(datetime_convert()),
2355 dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.'.$img->getExt()),
2356 dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.'.$img->getExt()),
2357 dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.'.$img->getExt()),
2358 intval($importer['importer_uid']),
2359 intval($importer['id'])
2364 if(($name_updated) && (strlen($new_name)) && ($name_updated > $importer['name-date'])) {
2365 $r = q("select * from contact where uid = %d and id = %d limit 1",
2366 intval($importer['importer_uid']),
2367 intval($importer['id'])
2370 $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
2371 dbesc(notags(trim($new_name))),
2372 dbesc(datetime_convert()),
2373 intval($importer['importer_uid']),
2374 intval($importer['id'])
2377 // do our best to update the name on content items
2380 q("update item set `author-name` = '%s' where `author-name` = '%s' and `author-link` = '%s' and uid = %d",
2381 dbesc(notags(trim($new_name))),
2382 dbesc($r[0]['name']),
2383 dbesc($r[0]['url']),
2384 intval($importer['importer_uid'])
2391 // Currently unsupported - needs a lot of work
2392 $reloc = $feed->get_feed_tags( NAMESPACE_DFRN, 'relocate' );
2393 if(isset($reloc[0]['child'][NAMESPACE_DFRN])) {
2394 $base = $reloc[0]['child'][NAMESPACE_DFRN];
2396 $newloc['uid'] = $importer['importer_uid'];
2397 $newloc['cid'] = $importer['id'];
2398 $newloc['name'] = notags(unxmlify($base['name'][0]['data']));
2399 $newloc['photo'] = notags(unxmlify($base['photo'][0]['data']));
2400 $newloc['thumb'] = notags(unxmlify($base['thumb'][0]['data']));
2401 $newloc['micro'] = notags(unxmlify($base['micro'][0]['data']));
2402 $newloc['url'] = notags(unxmlify($base['url'][0]['data']));
2403 $newloc['request'] = notags(unxmlify($base['request'][0]['data']));
2404 $newloc['confirm'] = notags(unxmlify($base['confirm'][0]['data']));
2405 $newloc['notify'] = notags(unxmlify($base['notify'][0]['data']));
2406 $newloc['poll'] = notags(unxmlify($base['poll'][0]['data']));
2407 $newloc['sitepubkey'] = notags(unxmlify($base['sitepubkey'][0]['data']));
2408 /** relocated user must have original key pair */
2409 /*$newloc['pubkey'] = notags(unxmlify($base['pubkey'][0]['data']));
2410 $newloc['prvkey'] = notags(unxmlify($base['prvkey'][0]['data']));*/
2412 logger("items:relocate contact ".print_r($newloc, true).print_r($importer, true), LOGGER_DEBUG);
2415 $r = q("SELECT photo, url FROM contact WHERE id=%d AND uid=%d;",
2416 intval($importer['id']),
2417 intval($importer['importer_uid']));
2422 $x = q("UPDATE contact SET
2432 `site-pubkey` = '%s'
2433 WHERE id=%d AND uid=%d;",
2434 dbesc($newloc['name']),
2435 dbesc($newloc['photo']),
2436 dbesc($newloc['thumb']),
2437 dbesc($newloc['micro']),
2438 dbesc($newloc['url']),
2439 dbesc($newloc['request']),
2440 dbesc($newloc['confirm']),
2441 dbesc($newloc['notify']),
2442 dbesc($newloc['poll']),
2443 dbesc($newloc['sitepubkey']),
2444 intval($importer['id']),
2445 intval($importer['importer_uid']));
2451 'owner-link' => array($old['url'], $newloc['url']),
2452 'author-link' => array($old['url'], $newloc['url']),
2453 'owner-avatar' => array($old['photo'], $newloc['photo']),
2454 'author-avatar' => array($old['photo'], $newloc['photo']),
2456 foreach ($fields as $n=>$f){
2457 $x = q("UPDATE item SET `%s`='%s' WHERE `%s`='%s' AND uid=%d",
2460 intval($importer['importer_uid']));
2466 // merge with current record, current contents have priority
2467 // update record, set url-updated
2468 // update profile photos
2474 // handle friend suggestion notification
2476 $sugg = $feed->get_feed_tags( NAMESPACE_DFRN, 'suggest' );
2477 if(isset($sugg[0]['child'][NAMESPACE_DFRN])) {
2478 $base = $sugg[0]['child'][NAMESPACE_DFRN];
2480 $fsugg['uid'] = $importer['importer_uid'];
2481 $fsugg['cid'] = $importer['id'];
2482 $fsugg['name'] = notags(unxmlify($base['name'][0]['data']));
2483 $fsugg['photo'] = notags(unxmlify($base['photo'][0]['data']));
2484 $fsugg['url'] = notags(unxmlify($base['url'][0]['data']));
2485 $fsugg['request'] = notags(unxmlify($base['request'][0]['data']));
2486 $fsugg['body'] = escape_tags(unxmlify($base['note'][0]['data']));
2488 // Does our member already have a friend matching this description?
2490 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `nurl` = '%s' AND `uid` = %d LIMIT 1",
2491 dbesc($fsugg['name']),
2492 dbesc(normalise_link($fsugg['url'])),
2493 intval($fsugg['uid'])
2498 // Do we already have an fcontact record for this person?
2501 $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
2502 dbesc($fsugg['url']),
2503 dbesc($fsugg['name']),
2504 dbesc($fsugg['request'])
2509 // OK, we do. Do we already have an introduction for this person ?
2510 $r = q("select id from intro where uid = %d and fid = %d limit 1",
2511 intval($fsugg['uid']),
2518 $r = q("INSERT INTO `fcontact` ( `name`,`url`,`photo`,`request` ) VALUES ( '%s', '%s', '%s', '%s' ) ",
2519 dbesc($fsugg['name']),
2520 dbesc($fsugg['url']),
2521 dbesc($fsugg['photo']),
2522 dbesc($fsugg['request'])
2524 $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
2525 dbesc($fsugg['url']),
2526 dbesc($fsugg['name']),
2527 dbesc($fsugg['request'])
2532 // database record did not get created. Quietly give up.
2537 $hash = random_string();
2539 $r = q("INSERT INTO `intro` ( `uid`, `fid`, `contact-id`, `note`, `hash`, `datetime`, `blocked` )
2540 VALUES( %d, %d, %d, '%s', '%s', '%s', %d )",
2541 intval($fsugg['uid']),
2543 intval($fsugg['cid']),
2544 dbesc($fsugg['body']),
2546 dbesc(datetime_convert()),
2551 'type' => NOTIFY_SUGGEST,
2552 'notify_flags' => $importer['notify-flags'],
2553 'language' => $importer['language'],
2554 'to_name' => $importer['username'],
2555 'to_email' => $importer['email'],
2556 'uid' => $importer['importer_uid'],
2558 'link' => $a->get_baseurl() . '/notifications/intros',
2559 'source_name' => $importer['name'],
2560 'source_link' => $importer['url'],
2561 'source_photo' => $importer['photo'],
2562 'verb' => ACTIVITY_REQ_FRIEND,
2571 $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
2572 if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
2574 logger('local_delivery: private message received');
2577 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
2580 $msg['uid'] = $importer['importer_uid'];
2581 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
2582 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
2583 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
2584 $msg['contact-id'] = $importer['id'];
2585 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
2586 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
2588 $msg['replied'] = 0;
2589 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
2590 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
2591 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
2595 $r = dbq("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg))
2596 . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
2598 // send notifications.
2600 require_once('include/enotify.php');
2602 $notif_params = array(
2603 'type' => NOTIFY_MAIL,
2604 'notify_flags' => $importer['notify-flags'],
2605 'language' => $importer['language'],
2606 'to_name' => $importer['username'],
2607 'to_email' => $importer['email'],
2608 'uid' => $importer['importer_uid'],
2610 'source_name' => $msg['from-name'],
2611 'source_link' => $importer['url'],
2612 'source_photo' => $importer['thumb'],
2613 'verb' => ACTIVITY_POST,
2617 notification($notif_params);
2623 $community_page = 0;
2624 $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'community');
2626 $community_page = intval($rawtags[0]['data']);
2628 if(intval($importer['forum']) != $community_page) {
2629 q("update contact set forum = %d where id = %d limit 1",
2630 intval($community_page),
2631 intval($importer['id'])
2633 $importer['forum'] = (string) $community_page;
2636 logger('local_delivery: feed item count = ' . $feed->get_item_quantity());
2638 // process any deleted entries
2640 $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
2641 if(is_array($del_entries) && count($del_entries)) {
2642 foreach($del_entries as $dentry) {
2644 if(isset($dentry['attribs']['']['ref'])) {
2645 $uri = $dentry['attribs']['']['ref'];
2647 if(isset($dentry['attribs']['']['when'])) {
2648 $when = $dentry['attribs']['']['when'];
2649 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
2652 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
2656 // check for relayed deletes to our conversation
2659 $r = q("select * from item where uri = '%s' and uid = %d limit 1",
2661 intval($importer['importer_uid'])
2664 $parent_uri = $r[0]['parent-uri'];
2665 if($r[0]['id'] != $r[0]['parent'])
2672 if($importer['page-flags'] == PAGE_COMMUNITY || $importer['page-flags'] == PAGE_PRVGROUP ) {
2675 logger('local_delivery: possible community delete');
2678 $sql_extra = " and contact.self = 1 and item.wall = 1 ";
2680 // was the top-level post for this reply written by somebody on this site?
2681 // Specifically, the recipient?
2683 $is_a_remote_delete = false;
2685 // POSSIBLE CLEANUP --> Why select so many fields when only forum_mode and wall are used?
2686 $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, `item`.`forum_mode`,`item`.`origin`,`item`.`wall`,
2687 `contact`.`name`, `contact`.`url`, `contact`.`thumb` from `item`
2688 LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
2689 WHERE `item`.`uri` = '%s' AND (`item`.`parent-uri` = '%s' or `item`.`thr-parent` = '%s')
2690 AND `item`.`uid` = %d
2696 intval($importer['importer_uid'])
2699 $is_a_remote_delete = true;
2701 // Does this have the characteristics of a community or private group comment?
2702 // If it's a reply to a wall post on a community/prvgroup page it's a
2703 // valid community comment. Also forum_mode makes it valid for sure.
2704 // If neither, it's not.
2706 if($is_a_remote_delete && $community) {
2707 if((! $r[0]['forum_mode']) && (! $r[0]['wall'])) {
2708 $is_a_remote_delete = false;
2709 logger('local_delivery: not a community delete');
2713 if($is_a_remote_delete) {
2714 logger('local_delivery: received remote delete');
2718 $r = q("SELECT `item`.*, `contact`.`self` FROM `item` left join contact on `item`.`contact-id` = `contact`.`id`
2719 WHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d AND NOT `item`.`file` LIKE '%%[%%' LIMIT 1",
2721 intval($importer['importer_uid']),
2722 intval($importer['id'])
2728 if($item['deleted'])
2731 logger('local_delivery: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
2733 if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
2734 $xo = parse_xml_string($item['object'],false);
2735 $xt = parse_xml_string($item['target'],false);
2737 if($xt->type === ACTIVITY_OBJ_NOTE) {
2738 $i = q("select * from `item` where uri = '%s' and uid = %d limit 1",
2740 intval($importer['importer_uid'])
2744 // For tags, the owner cannot remove the tag on the author's copy of the post.
2746 $owner_remove = (($item['contact-id'] == $i[0]['contact-id']) ? true: false);
2747 $author_remove = (($item['origin'] && $item['self']) ? true : false);
2748 $author_copy = (($item['origin']) ? true : false);
2750 if($owner_remove && $author_copy)
2752 if($author_remove || $owner_remove) {
2753 $tags = explode(',',$i[0]['tag']);
2756 foreach($tags as $tag)
2757 if(trim($tag) !== trim($xo->body))
2758 $newtags[] = trim($tag);
2760 q("update item set tag = '%s' where id = %d limit 1",
2761 dbesc(implode(',',$newtags)),
2764 create_tags_from_item($i[0]['id']);
2770 if($item['uri'] == $item['parent-uri']) {
2771 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
2772 `body` = '', `title` = ''
2773 WHERE `parent-uri` = '%s' AND `uid` = %d",
2775 dbesc(datetime_convert()),
2776 dbesc($item['uri']),
2777 intval($importer['importer_uid'])
2779 create_tags_from_itemuri($item['uri'], $importer['importer_uid']);
2782 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
2783 `body` = '', `title` = ''
2784 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2786 dbesc(datetime_convert()),
2788 intval($importer['importer_uid'])
2790 create_tags_from_itemuri($uri, $importer['importer_uid']);
2791 if($item['last-child']) {
2792 // ensure that last-child is set in case the comment that had it just got wiped.
2793 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
2794 dbesc(datetime_convert()),
2795 dbesc($item['parent-uri']),
2796 intval($item['uid'])
2798 // who is the last child now?
2799 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
2800 ORDER BY `created` DESC LIMIT 1",
2801 dbesc($item['parent-uri']),
2802 intval($importer['importer_uid'])
2805 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
2810 // if this is a relayed delete, propagate it to other recipients
2812 if($is_a_remote_delete)
2813 proc_run('php',"include/notifier.php","drop",$item['id']);
2821 foreach($feed->get_items() as $item) {
2824 $item_id = $item->get_id();
2825 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
2826 if(isset($rawthread[0]['attribs']['']['ref'])) {
2828 $parent_uri = $rawthread[0]['attribs']['']['ref'];
2834 if($importer['page-flags'] == PAGE_COMMUNITY || $importer['page-flags'] == PAGE_PRVGROUP ) {
2837 logger('local_delivery: possible community reply');
2840 $sql_extra = " and contact.self = 1 and item.wall = 1 ";
2842 // was the top-level post for this reply written by somebody on this site?
2843 // Specifically, the recipient?
2845 $is_a_remote_comment = false;
2846 $top_uri = $parent_uri;
2848 $r = q("select `item`.`parent-uri` from `item`
2849 WHERE `item`.`uri` = '%s'
2853 if($r && count($r)) {
2854 $top_uri = $r[0]['parent-uri'];
2856 // POSSIBLE CLEANUP --> Why select so many fields when only forum_mode and wall are used?
2857 $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, `item`.`forum_mode`,`item`.`origin`,`item`.`wall`,
2858 `contact`.`name`, `contact`.`url`, `contact`.`thumb` from `item`
2859 LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
2860 WHERE `item`.`uri` = '%s' AND (`item`.`parent-uri` = '%s' or `item`.`thr-parent` = '%s')
2861 AND `item`.`uid` = %d
2867 intval($importer['importer_uid'])
2870 $is_a_remote_comment = true;
2873 // Does this have the characteristics of a community or private group comment?
2874 // If it's a reply to a wall post on a community/prvgroup page it's a
2875 // valid community comment. Also forum_mode makes it valid for sure.
2876 // If neither, it's not.
2878 if($is_a_remote_comment && $community) {
2879 if((! $r[0]['forum_mode']) && (! $r[0]['wall'])) {
2880 $is_a_remote_comment = false;
2881 logger('local_delivery: not a community reply');
2885 if($is_a_remote_comment) {
2886 logger('local_delivery: received remote comment');
2888 // remote reply to our post. Import and then notify everybody else.
2890 $datarray = get_atom_elements($feed,$item);
2892 $r = q("SELECT `id`, `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2894 intval($importer['importer_uid'])
2897 // Update content if 'updated' changes
2901 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
2903 // do not accept (ignore) an earlier edit than one we currently have.
2904 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
2907 logger('received updated comment' , LOGGER_DEBUG);
2908 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2909 dbesc($datarray['title']),
2910 dbesc($datarray['body']),
2911 dbesc($datarray['tag']),
2912 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
2914 intval($importer['importer_uid'])
2916 create_tags_from_itemuri($item_id, $importer['importer_uid']);
2918 proc_run('php',"include/notifier.php","comment-import",$iid);
2927 $own = q("select name,url,thumb from contact where uid = %d and self = 1 limit 1",
2928 intval($importer['importer_uid'])
2932 $datarray['type'] = 'remote-comment';
2933 $datarray['wall'] = 1;
2934 $datarray['parent-uri'] = $parent_uri;
2935 $datarray['uid'] = $importer['importer_uid'];
2936 $datarray['owner-name'] = $own[0]['name'];
2937 $datarray['owner-link'] = $own[0]['url'];
2938 $datarray['owner-avatar'] = $own[0]['thumb'];
2939 $datarray['contact-id'] = $importer['id'];
2941 if(($datarray['verb'] === ACTIVITY_LIKE) || ($datarray['verb'] === ACTIVITY_DISLIKE)) {
2943 $datarray['type'] = 'activity';
2944 $datarray['gravity'] = GRAVITY_LIKE;
2945 $datarray['last-child'] = 0;
2946 // only one like or dislike per person
2947 $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",
2948 intval($datarray['uid']),
2949 intval($datarray['contact-id']),
2950 dbesc($datarray['verb']),
2951 dbesc($datarray['parent-uri']),
2952 dbesc($datarray['parent-uri'])
2959 if(($datarray['verb'] === ACTIVITY_TAG) && ($datarray['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
2961 $xo = parse_xml_string($datarray['object'],false);
2962 $xt = parse_xml_string($datarray['target'],false);
2964 if(($xt->type == ACTIVITY_OBJ_NOTE) && ($xt->id)) {
2966 // fetch the parent item
2968 $tagp = q("select * from item where uri = '%s' and uid = %d limit 1",
2970 intval($importer['importer_uid'])
2975 // extract tag, if not duplicate, and this user allows tags, add to parent item
2977 if($xo->id && $xo->content) {
2978 $newtag = '#[url=' . $xo->id . ']'. $xo->content . '[/url]';
2979 if(! (stristr($tagp[0]['tag'],$newtag))) {
2980 $i = q("SELECT `blocktags` FROM `user` where `uid` = %d LIMIT 1",
2981 intval($importer['importer_uid'])
2983 if(count($i) && ! intval($i[0]['blocktags'])) {
2984 q("UPDATE item SET tag = '%s', `edited` = '%s' WHERE id = %d LIMIT 1",
2985 dbesc($tagp[0]['tag'] . (strlen($tagp[0]['tag']) ? ',' : '') . $newtag),
2986 intval($tagp[0]['id']),
2987 dbesc(datetime_convert())
2989 create_tags_from_item($tagp[0]['id']);
2997 $posted_id = item_store($datarray);
3001 $r = q("SELECT `parent`, `parent-uri` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
3003 intval($importer['importer_uid'])
3006 $parent = $r[0]['parent'];
3007 $parent_uri = $r[0]['parent-uri'];
3011 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
3012 dbesc(datetime_convert()),
3013 intval($importer['importer_uid']),
3014 intval($r[0]['parent'])
3017 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
3018 dbesc(datetime_convert()),
3019 intval($importer['importer_uid']),
3024 if($posted_id && $parent) {
3026 proc_run('php',"include/notifier.php","comment-import","$posted_id");
3028 if((! $is_like) && (! $importer['self'])) {
3030 require_once('include/enotify.php');
3033 'type' => NOTIFY_COMMENT,
3034 'notify_flags' => $importer['notify-flags'],
3035 'language' => $importer['language'],
3036 'to_name' => $importer['username'],
3037 'to_email' => $importer['email'],
3038 'uid' => $importer['importer_uid'],
3039 'item' => $datarray,
3040 'link' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
3041 'source_name' => stripslashes($datarray['author-name']),
3042 'source_link' => $datarray['author-link'],
3043 'source_photo' => ((link_compare($datarray['author-link'],$importer['url']))
3044 ? $importer['thumb'] : $datarray['author-avatar']),
3045 'verb' => ACTIVITY_POST,
3047 'parent' => $parent,
3048 'parent_uri' => $parent_uri,
3060 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
3062 $item_id = $item->get_id();
3063 $datarray = get_atom_elements($feed,$item);
3065 if($importer['rel'] == CONTACT_IS_FOLLOWER)
3068 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3070 intval($importer['importer_uid'])
3073 // Update content if 'updated' changes
3076 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
3078 // do not accept (ignore) an earlier edit than one we currently have.
3079 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
3082 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3083 dbesc($datarray['title']),
3084 dbesc($datarray['body']),
3085 dbesc($datarray['tag']),
3086 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
3088 intval($importer['importer_uid'])
3090 create_tags_from_itemuri($item_id, $importer['importer_uid']);
3093 // update last-child if it changes
3095 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
3096 if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
3097 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
3098 dbesc(datetime_convert()),
3100 intval($importer['importer_uid'])
3102 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3103 intval($allow[0]['data']),
3104 dbesc(datetime_convert()),
3106 intval($importer['importer_uid'])
3112 $datarray['parent-uri'] = $parent_uri;
3113 $datarray['uid'] = $importer['importer_uid'];
3114 $datarray['contact-id'] = $importer['id'];
3115 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
3116 $datarray['type'] = 'activity';
3117 $datarray['gravity'] = GRAVITY_LIKE;
3118 // only one like or dislike per person
3119 $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",
3120 intval($datarray['uid']),
3121 intval($datarray['contact-id']),
3122 dbesc($datarray['verb']),
3131 if(($datarray['verb'] === ACTIVITY_TAG) && ($datarray['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
3133 $xo = parse_xml_string($datarray['object'],false);
3134 $xt = parse_xml_string($datarray['target'],false);
3136 if($xt->type == ACTIVITY_OBJ_NOTE) {
3137 $r = q("select * from item where `uri` = '%s' AND `uid` = %d limit 1",
3139 intval($importer['importer_uid'])
3144 // extract tag, if not duplicate, add to parent item
3146 if(! (stristr($r[0]['tag'],trim($xo->content)))) {
3147 q("UPDATE item SET tag = '%s' WHERE id = %d LIMIT 1",
3148 dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . '#[url=' . $xo->id . ']'. $xo->content . '[/url]'),
3151 create_tags_from_item($r[0]['id']);
3157 $posted_id = item_store($datarray);
3159 // find out if our user is involved in this conversation and wants to be notified.
3161 if(!x($datarray['type']) || $datarray['type'] != 'activity') {
3163 $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
3165 intval($importer['importer_uid'])
3168 if(count($myconv)) {
3169 $importer_url = $a->get_baseurl() . '/profile/' . $importer['nickname'];
3171 // first make sure this isn't our own post coming back to us from a wall-to-wall event
3172 if(! link_compare($datarray['author-link'],$importer_url)) {
3175 foreach($myconv as $conv) {
3177 // now if we find a match, it means we're in this conversation
3179 if(! link_compare($conv['author-link'],$importer_url))
3182 require_once('include/enotify.php');
3184 $conv_parent = $conv['parent'];
3187 'type' => NOTIFY_COMMENT,
3188 'notify_flags' => $importer['notify-flags'],
3189 'language' => $importer['language'],
3190 'to_name' => $importer['username'],
3191 'to_email' => $importer['email'],
3192 'uid' => $importer['importer_uid'],
3193 'item' => $datarray,
3194 'link' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
3195 'source_name' => stripslashes($datarray['author-name']),
3196 'source_link' => $datarray['author-link'],
3197 'source_photo' => ((link_compare($datarray['author-link'],$importer['url']))
3198 ? $importer['thumb'] : $datarray['author-avatar']),
3199 'verb' => ACTIVITY_POST,
3201 'parent' => $conv_parent,
3202 'parent_uri' => $parent_uri
3206 // only send one notification
3218 // Head post of a conversation. Have we seen it? If not, import it.
3221 $item_id = $item->get_id();
3222 $datarray = get_atom_elements($feed,$item);
3224 if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
3225 $ev = bbtoevent($datarray['body']);
3226 if(x($ev,'desc') && x($ev,'start')) {
3227 $ev['cid'] = $importer['id'];
3228 $ev['uid'] = $importer['uid'];
3229 $ev['uri'] = $item_id;
3230 $ev['edited'] = $datarray['edited'];
3231 $ev['private'] = $datarray['private'];
3233 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3235 intval($importer['uid'])
3238 $ev['id'] = $r[0]['id'];
3239 $xyz = event_store($ev);
3244 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3246 intval($importer['importer_uid'])
3249 // Update content if 'updated' changes
3252 if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) {
3254 // do not accept (ignore) an earlier edit than one we currently have.
3255 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
3258 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3259 dbesc($datarray['title']),
3260 dbesc($datarray['body']),
3261 dbesc($datarray['tag']),
3262 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
3264 intval($importer['importer_uid'])
3266 create_tags_from_itemuri($item_id, $importer['importer_uid']);
3269 // update last-child if it changes
3271 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
3272 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
3273 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3274 intval($allow[0]['data']),
3275 dbesc(datetime_convert()),
3277 intval($importer['importer_uid'])
3283 // This is my contact on another system, but it's really me.
3284 // Turn this into a wall post.
3286 if($importer['remote_self'])
3287 $datarray['wall'] = 1;
3289 $datarray['parent-uri'] = $item_id;
3290 $datarray['uid'] = $importer['importer_uid'];
3291 $datarray['contact-id'] = $importer['id'];
3294 if(! link_compare($datarray['owner-link'],$importer['url'])) {
3295 // The item owner info is not our contact. It's OK and is to be expected if this is a tgroup delivery,
3296 // but otherwise there's a possible data mixup on the sender's system.
3297 // the tgroup delivery code called from item_store will correct it if it's a forum,
3298 // but we're going to unconditionally correct it here so that the post will always be owned by our contact.
3299 logger('local_delivery: Correcting item owner.', LOGGER_DEBUG);
3300 $datarray['owner-name'] = $importer['senderName'];
3301 $datarray['owner-link'] = $importer['url'];
3302 $datarray['owner-avatar'] = $importer['thumb'];
3305 if(($importer['rel'] == CONTACT_IS_FOLLOWER) && (! tgroup_check($importer['importer_uid'],$datarray)))
3308 $posted_id = item_store($datarray);
3310 if(stristr($datarray['verb'],ACTIVITY_POKE)) {
3311 $verb = urldecode(substr($datarray['verb'],strpos($datarray['verb'],'#')+1));
3314 $xo = parse_xml_string($datarray['object'],false);
3316 if(($xo->type == ACTIVITY_OBJ_PERSON) && ($xo->id)) {
3318 // somebody was poked/prodded. Was it me?
3320 $links = parse_xml_string("<links>".unxmlify($xo->link)."</links>",false);
3322 foreach($links->link as $l) {
3323 $atts = $l->attributes();
3324 switch($atts['rel']) {
3326 $Blink = $atts['href'];
3332 if($Blink && link_compare($Blink,$a->get_baseurl() . '/profile/' . $importer['nickname'])) {
3334 // send a notification
3335 require_once('include/enotify.php');
3338 'type' => NOTIFY_POKE,
3339 'notify_flags' => $importer['notify-flags'],
3340 'language' => $importer['language'],
3341 'to_name' => $importer['username'],
3342 'to_email' => $importer['email'],
3343 'uid' => $importer['importer_uid'],
3344 'item' => $datarray,
3345 'link' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id,
3346 'source_name' => stripslashes($datarray['author-name']),
3347 'source_link' => $datarray['author-link'],
3348 'source_photo' => ((link_compare($datarray['author-link'],$importer['url']))
3349 ? $importer['thumb'] : $datarray['author-avatar']),
3350 'verb' => $datarray['verb'],
3351 'otype' => 'person',
3352 'activity' => $verb,
3369 function new_follower($importer,$contact,$datarray,$item,$sharing = false) {
3370 $url = notags(trim($datarray['author-link']));
3371 $name = notags(trim($datarray['author-name']));
3372 $photo = notags(trim($datarray['author-avatar']));
3374 $rawtag = $item->get_item_tags(NAMESPACE_ACTIVITY,'actor');
3375 if($rawtag && $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'])
3376 $nick = $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'];
3378 if(is_array($contact)) {
3379 if(($contact['network'] == NETWORK_OSTATUS && $contact['rel'] == CONTACT_IS_SHARING)
3380 || ($sharing && $contact['rel'] == CONTACT_IS_FOLLOWER)) {
3381 $r = q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
3382 intval(CONTACT_IS_FRIEND),
3383 intval($contact['id']),
3384 intval($importer['uid'])
3387 // send email notification to owner?
3391 // create contact record
3393 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `name`, `nick`, `photo`, `network`, `rel`,
3394 `blocked`, `readonly`, `pending`, `writable` )
3395 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 1, 1 ) ",
3396 intval($importer['uid']),
3397 dbesc(datetime_convert()),
3399 dbesc(normalise_link($url)),
3403 dbesc(($sharing) ? NETWORK_ZOT : NETWORK_OSTATUS),
3404 intval(($sharing) ? CONTACT_IS_SHARING : CONTACT_IS_FOLLOWER)
3406 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `pending` = 1 LIMIT 1",
3407 intval($importer['uid']),
3411 $contact_record = $r[0];
3413 // create notification
3414 $hash = random_string();
3416 if(is_array($contact_record)) {
3417 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `hash`, `datetime`)
3418 VALUES ( %d, %d, 0, 0, '%s', '%s' )",
3419 intval($importer['uid']),
3420 intval($contact_record['id']),
3422 dbesc(datetime_convert())
3425 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
3426 intval($importer['uid'])
3431 if(intval($r[0]['def_gid'])) {
3432 require_once('include/group.php');
3433 group_add_member($r[0]['uid'],'',$contact_record['id'],$r[0]['def_gid']);
3436 if(($r[0]['notify-flags'] & NOTIFY_INTRO) && ($r[0]['page-flags'] == PAGE_NORMAL)) {
3437 $email_tpl = get_intltext_template('follow_notify_eml.tpl');
3438 $email = replace_macros($email_tpl, array(
3439 '$requestor' => ((strlen($name)) ? $name : t('[Name Withheld]')),
3441 '$myname' => $r[0]['username'],
3442 '$siteurl' => $a->get_baseurl(),
3443 '$sitename' => $a->config['sitename']
3445 $res = mail($r[0]['email'],
3446 email_header_encode((($sharing) ? t('A new person is sharing with you at ') : t("You have a new follower at ")) . $a->config['sitename'],'UTF-8'),
3448 'From: ' . 'Administrator' . '@' . $_SERVER['SERVER_NAME'] . "\n"
3449 . 'Content-type: text/plain; charset=UTF-8' . "\n"
3450 . 'Content-transfer-encoding: 8bit' );
3457 function lose_follower($importer,$contact,$datarray,$item) {
3459 if(($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_SHARING)) {
3460 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
3461 intval(CONTACT_IS_SHARING),
3462 intval($contact['id'])
3466 contact_remove($contact['id']);
3470 function lose_sharer($importer,$contact,$datarray,$item) {
3472 if(($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_FOLLOWER)) {
3473 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d LIMIT 1",
3474 intval(CONTACT_IS_FOLLOWER),
3475 intval($contact['id'])
3479 contact_remove($contact['id']);
3484 function subscribe_to_hub($url,$importer,$contact,$hubmode = 'subscribe') {
3488 if(is_array($importer)) {
3489 $r = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
3490 intval($importer['uid'])
3494 // Diaspora has different message-ids in feeds than they do
3495 // through the direct Diaspora protocol. If we try and use
3496 // the feed, we'll get duplicates. So don't.
3498 if((! count($r)) || $contact['network'] === NETWORK_DIASPORA)
3501 $push_url = get_config('system','url') . '/pubsub/' . $r[0]['nickname'] . '/' . $contact['id'];
3503 // Use a single verify token, even if multiple hubs
3505 $verify_token = ((strlen($contact['hub-verify'])) ? $contact['hub-verify'] : random_string());
3507 $params= 'hub.mode=' . $hubmode . '&hub.callback=' . urlencode($push_url) . '&hub.topic=' . urlencode($contact['poll']) . '&hub.verify=async&hub.verify_token=' . $verify_token;
3509 logger('subscribe_to_hub: ' . $hubmode . ' ' . $contact['name'] . ' to hub ' . $url . ' endpoint: ' . $push_url . ' with verifier ' . $verify_token);
3511 if(! strlen($contact['hub-verify'])) {
3512 $r = q("UPDATE `contact` SET `hub-verify` = '%s' WHERE `id` = %d LIMIT 1",
3513 dbesc($verify_token),
3514 intval($contact['id'])
3518 post_url($url,$params);
3520 logger('subscribe_to_hub: returns: ' . $a->get_curl_code(), LOGGER_DEBUG);
3527 function atom_author($tag,$name,$uri,$h,$w,$photo) {
3531 $name = xmlify($name);
3532 $uri = xmlify($uri);
3535 $photo = xmlify($photo);
3539 $o .= "<name>$name</name>\r\n";
3540 $o .= "<uri>$uri</uri>\r\n";
3541 $o .= '<link rel="photo" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
3542 $o .= '<link rel="avatar" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
3544 call_hooks('atom_author', $o);
3546 $o .= "</$tag>\r\n";
3550 function atom_entry($item,$type,$author,$owner,$comment = false,$cid = 0) {
3554 if(! $item['parent'])
3557 if($item['deleted'])
3558 return '<at:deleted-entry ref="' . xmlify($item['uri']) . '" when="' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '" />' . "\r\n";
3561 if($item['allow_cid'] || $item['allow_gid'] || $item['deny_cid'] || $item['deny_gid'])
3562 $body = fix_private_photos($item['body'],$owner['uid'],$item,$cid);
3564 $body = $item['body'];
3566 $o = "\r\n\r\n<entry>\r\n";
3568 if(is_array($author))
3569 $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb']);
3571 $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']));
3572 if(strlen($item['owner-name']))
3573 $o .= atom_author('dfrn:owner',$item['owner-name'],$item['owner-link'],80,80,$item['owner-avatar']);
3575 if(($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
3576 $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
3577 $o .= '<thr:in-reply-to ref="' . xmlify($parent_item) . '" type="text/html" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['parent']) . '" />' . "\r\n";
3580 $o .= '<id>' . xmlify($item['uri']) . '</id>' . "\r\n";
3581 $o .= '<title>' . xmlify($item['title']) . '</title>' . "\r\n";
3582 $o .= '<published>' . xmlify(datetime_convert('UTC','UTC',$item['created'] . '+00:00',ATOM_TIME)) . '</published>' . "\r\n";
3583 $o .= '<updated>' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '</updated>' . "\r\n";
3584 $o .= '<dfrn:env>' . base64url_encode($body, true) . '</dfrn:env>' . "\r\n";
3585 $o .= '<content type="' . $type . '" >' . xmlify((($type === 'html') ? bbcode($body) : $body)) . '</content>' . "\r\n";
3586 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']) . '" />' . "\r\n";
3588 $o .= '<dfrn:comment-allow>' . intval($item['last-child']) . '</dfrn:comment-allow>' . "\r\n";
3590 if($item['location']) {
3591 $o .= '<dfrn:location>' . xmlify($item['location']) . '</dfrn:location>' . "\r\n";
3592 $o .= '<poco:address><poco:formatted>' . xmlify($item['location']) . '</poco:formatted></poco:address>' . "\r\n";
3596 $o .= '<georss:point>' . xmlify($item['coord']) . '</georss:point>' . "\r\n";
3598 if(($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid']))
3599 $o .= '<dfrn:private>' . (($item['private']) ? $item['private'] : 1) . '</dfrn:private>' . "\r\n";
3602 $o .= '<dfrn:extid>' . xmlify($item['extid']) . '</dfrn:extid>' . "\r\n";
3603 if($item['bookmark'])
3604 $o .= '<dfrn:bookmark>true</dfrn:bookmark>' . "\r\n";
3607 $o .= '<statusnet:notice_info local_id="' . $item['id'] . '" source="' . xmlify($item['app']) . '" ></statusnet:notice_info>' . "\r\n";
3610 $o .= '<dfrn:diaspora_guid>' . $item['guid'] . '</dfrn:diaspora_guid>' . "\r\n";
3612 if($item['signed_text']) {
3613 $sign = base64_encode(json_encode(array('signed_text' => $item['signed_text'],'signature' => $item['signature'],'signer' => $item['signer'])));
3614 $o .= '<dfrn:diaspora_signature>' . xmlify($sign) . '</dfrn:diaspora_signature>' . "\r\n";
3617 $verb = construct_verb($item);
3618 $o .= '<as:verb>' . xmlify($verb) . '</as:verb>' . "\r\n";
3619 $actobj = construct_activity_object($item);
3622 $actarg = construct_activity_target($item);
3626 $tags = item_getfeedtags($item);
3628 foreach($tags as $t) {
3629 $o .= '<category scheme="X-DFRN:' . xmlify($t[0]) . ':' . xmlify($t[1]) . '" term="' . xmlify($t[2]) . '" />' . "\r\n";
3633 $o .= item_getfeedattach($item);
3635 $mentioned = get_mentions($item);
3639 call_hooks('atom_entry', $o);
3641 $o .= '</entry>' . "\r\n";
3646 function fix_private_photos($s, $uid, $item = null, $cid = 0) {
3649 logger('fix_private_photos', LOGGER_DEBUG);
3650 $site = substr($a->get_baseurl(),strpos($a->get_baseurl(),'://'));
3655 $img_start = strpos($orig_body, '[img');
3656 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
3657 $img_len = ($img_start !== false ? strpos(substr($orig_body, $img_start + $img_st_close + 1), '[/img]') : false);
3658 while( ($img_st_close !== false) && ($img_len !== false) ) {
3660 $img_st_close++; // make it point to AFTER the closing bracket
3661 $image = substr($orig_body, $img_start + $img_st_close, $img_len);
3663 logger('fix_private_photos: found photo ' . $image, LOGGER_DEBUG);
3666 if(stristr($image , $site . '/photo/')) {
3667 // Only embed locally hosted photos
3669 $i = basename($image);
3670 $i = str_replace(array('.jpg','.png','.gif'),array('','',''),$i);
3671 $x = strpos($i,'-');
3674 $res = substr($i,$x+1);
3675 $i = substr($i,0,$x);
3676 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d AND `uid` = %d",
3683 // Check to see if we should replace this photo link with an embedded image
3684 // 1. No need to do so if the photo is public
3685 // 2. If there's a contact-id provided, see if they're in the access list
3686 // for the photo. If so, embed it.
3687 // 3. Otherwise, if we have an item, see if the item permissions match the photo
3688 // permissions, regardless of order but first check to see if they're an exact
3689 // match to save some processing overhead.
3691 if(has_permissions($r[0])) {
3693 $recips = enumerate_permissions($r[0]);
3694 if(in_array($cid, $recips)) {
3699 if(compare_permissions($item,$r[0]))
3704 $data = $r[0]['data'];
3705 $type = $r[0]['type'];
3707 // If a custom width and height were specified, apply before embedding
3708 if(preg_match("/\[img\=([0-9]*)x([0-9]*)\]/is", substr($orig_body, $img_start, $img_st_close), $match)) {
3709 logger('fix_private_photos: scaling photo', LOGGER_DEBUG);
3711 $width = intval($match[1]);
3712 $height = intval($match[2]);
3714 $ph = new Photo($data, $type);
3715 if($ph->is_valid()) {
3716 $ph->scaleImage(max($width, $height));
3717 $data = $ph->imageString();
3718 $type = $ph->getType();
3722 logger('fix_private_photos: replacing photo', LOGGER_DEBUG);
3723 $image = 'data:' . $type . ';base64,' . base64_encode($data);
3724 logger('fix_private_photos: replaced: ' . $image, LOGGER_DATA);
3730 $new_body = $new_body . substr($orig_body, 0, $img_start + $img_st_close) . $image . '[/img]';
3731 $orig_body = substr($orig_body, $img_start + $img_st_close + $img_len + strlen('[/img]'));
3732 if($orig_body === false)
3735 $img_start = strpos($orig_body, '[img');
3736 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
3737 $img_len = ($img_start !== false ? strpos(substr($orig_body, $img_start + $img_st_close + 1), '[/img]') : false);
3740 $new_body = $new_body . $orig_body;
3746 function has_permissions($obj) {
3747 if(($obj['allow_cid'] != '') || ($obj['allow_gid'] != '') || ($obj['deny_cid'] != '') || ($obj['deny_gid'] != ''))
3752 function compare_permissions($obj1,$obj2) {
3753 // first part is easy. Check that these are exactly the same.
3754 if(($obj1['allow_cid'] == $obj2['allow_cid'])
3755 && ($obj1['allow_gid'] == $obj2['allow_gid'])
3756 && ($obj1['deny_cid'] == $obj2['deny_cid'])
3757 && ($obj1['deny_gid'] == $obj2['deny_gid']))
3760 // This is harder. Parse all the permissions and compare the resulting set.
3762 $recipients1 = enumerate_permissions($obj1);
3763 $recipients2 = enumerate_permissions($obj2);
3766 if($recipients1 == $recipients2)
3771 // returns an array of contact-ids that are allowed to see this object
3773 function enumerate_permissions($obj) {
3774 require_once('include/group.php');
3775 $allow_people = expand_acl($obj['allow_cid']);
3776 $allow_groups = expand_groups(expand_acl($obj['allow_gid']));
3777 $deny_people = expand_acl($obj['deny_cid']);
3778 $deny_groups = expand_groups(expand_acl($obj['deny_gid']));
3779 $recipients = array_unique(array_merge($allow_people,$allow_groups));
3780 $deny = array_unique(array_merge($deny_people,$deny_groups));
3781 $recipients = array_diff($recipients,$deny);
3785 function item_getfeedtags($item) {
3788 $cnt = preg_match_all('|\#\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
3790 for($x = 0; $x < $cnt; $x ++) {
3792 $ret[] = array('#',$matches[1][$x], $matches[2][$x]);
3796 $cnt = preg_match_all('|\@\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
3798 for($x = 0; $x < $cnt; $x ++) {
3800 $ret[] = array('@',$matches[1][$x], $matches[2][$x]);
3806 function item_getfeedattach($item) {
3808 $arr = explode('[/attach],',$item['attach']);
3810 foreach($arr as $r) {
3812 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|',$r,$matches);
3814 $ret .= '<link rel="enclosure" href="' . xmlify($matches[1]) . '" type="' . xmlify($matches[3]) . '" ';
3815 if(intval($matches[2]))
3816 $ret .= 'length="' . intval($matches[2]) . '" ';
3817 if($matches[4] !== ' ')
3818 $ret .= 'title="' . xmlify(trim($matches[4])) . '" ';
3819 $ret .= ' />' . "\r\n";
3828 function item_expire($uid,$days) {
3830 if((! $uid) || ($days < 1))
3833 // $expire_network_only = save your own wall posts
3834 // and just expire conversations started by others
3836 $expire_network_only = get_pconfig($uid,'expire','network_only');
3837 $sql_extra = ((intval($expire_network_only)) ? " AND wall = 0 " : "");
3839 $r = q("SELECT * FROM `item`
3841 AND `created` < UTC_TIMESTAMP() - INTERVAL %d DAY
3852 $expire_items = get_pconfig($uid, 'expire','items');
3853 $expire_items = (($expire_items===false)?1:intval($expire_items)); // default if not set: 1
3855 $expire_notes = get_pconfig($uid, 'expire','notes');
3856 $expire_notes = (($expire_notes===false)?1:intval($expire_notes)); // default if not set: 1
3858 $expire_starred = get_pconfig($uid, 'expire','starred');
3859 $expire_starred = (($expire_starred===false)?1:intval($expire_starred)); // default if not set: 1
3861 $expire_photos = get_pconfig($uid, 'expire','photos');
3862 $expire_photos = (($expire_photos===false)?0:intval($expire_photos)); // default if not set: 0
3864 logger('expire: # items=' . count($r). "; expire items: $expire_items, expire notes: $expire_notes, expire starred: $expire_starred, expire photos: $expire_photos");
3866 foreach($r as $item) {
3868 // don't expire filed items
3870 if(strpos($item['file'],'[') !== false)
3873 // Only expire posts, not photos and photo comments
3875 if($expire_photos==0 && strlen($item['resource-id']))
3877 if($expire_starred==0 && intval($item['starred']))
3879 if($expire_notes==0 && $item['type']=='note')
3881 if($expire_items==0 && $item['type']!='note')
3884 drop_item($item['id'],false);
3887 proc_run('php',"include/notifier.php","expire","$uid");
3892 function drop_items($items) {
3895 if(! local_user() && ! remote_user())
3899 foreach($items as $item) {
3900 $owner = drop_item($item,false);
3901 if($owner && ! $uid)
3906 // multiple threads may have been deleted, send an expire notification
3909 proc_run('php',"include/notifier.php","expire","$uid");
3913 function drop_item($id,$interactive = true) {
3917 // locate item to be deleted
3919 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
3926 notice( t('Item not found.') . EOL);
3927 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
3932 $owner = $item['uid'];
3936 // check if logged in user is either the author or owner of this item
3938 if(is_array($_SESSION['remote'])) {
3939 foreach($_SESSION['remote'] as $visitor) {
3940 if($visitor['uid'] == $item['uid'] && $visitor['cid'] == $item['contact-id']) {
3941 $cid = $visitor['cid'];
3948 if((local_user() == $item['uid']) || ($cid) || (! $interactive)) {
3950 // Check if we should do HTML-based delete confirmation
3951 if($_REQUEST['confirm']) {
3952 // <form> can't take arguments in its "action" parameter
3953 // so add any arguments as hidden inputs
3954 $query = explode_querystring($a->query_string);
3956 foreach($query['args'] as $arg) {
3957 if(strpos($arg, 'confirm=') === false) {
3958 $arg_parts = explode('=', $arg);
3959 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
3963 return replace_macros(get_markup_template('confirm.tpl'), array(
3965 '$message' => t('Do you really want to delete this item?'),
3966 '$extra_inputs' => $inputs,
3967 '$confirm' => t('Yes'),
3968 '$confirm_url' => $query['base'],
3969 '$confirm_name' => 'confirmed',
3970 '$cancel' => t('Cancel'),
3973 // Now check how the user responded to the confirmation query
3974 if($_REQUEST['canceled']) {
3975 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
3978 logger('delete item: ' . $item['id'], LOGGER_DEBUG);
3981 $r = q("UPDATE `item` SET `deleted` = 1, `title` = '', `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
3982 dbesc(datetime_convert()),
3983 dbesc(datetime_convert()),
3986 create_tags_from_item($item['id']);
3988 // clean up categories and tags so they don't end up as orphans
3991 $cnt = preg_match_all('/<(.*?)>/',$item['file'],$matches,PREG_SET_ORDER);
3993 foreach($matches as $mtch) {
3994 file_tag_unsave_file($item['uid'],$item['id'],$mtch[1],true);
4000 $cnt = preg_match_all('/\[(.*?)\]/',$item['file'],$matches,PREG_SET_ORDER);
4002 foreach($matches as $mtch) {
4003 file_tag_unsave_file($item['uid'],$item['id'],$mtch[1],false);
4007 // If item is a link to a photo resource, nuke all the associated photos
4008 // (visitors will not have photo resources)
4009 // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
4010 // generate a resource-id and therefore aren't intimately linked to the item.
4012 if(strlen($item['resource-id'])) {
4013 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
4014 dbesc($item['resource-id']),
4015 intval($item['uid'])
4017 // ignore the result
4020 // If item is a link to an event, nuke the event record.
4022 if(intval($item['event-id'])) {
4023 q("DELETE FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
4024 intval($item['event-id']),
4025 intval($item['uid'])
4027 // ignore the result
4030 // clean up item_id and sign meta-data tables
4032 $r = q("DELETE FROM item_id where iid in (select id from item where parent = %d and uid = %d)",
4033 intval($item['id']),
4034 intval($item['uid'])
4037 $r = q("DELETE FROM sign where iid in (select id from item where parent = %d and uid = %d)",
4038 intval($item['id']),
4039 intval($item['uid'])
4042 // If it's the parent of a comment thread, kill all the kids
4044 if($item['uri'] == $item['parent-uri']) {
4045 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' , `title` = ''
4046 WHERE `parent-uri` = '%s' AND `uid` = %d ",
4047 dbesc(datetime_convert()),
4048 dbesc(datetime_convert()),
4049 dbesc($item['parent-uri']),
4050 intval($item['uid'])
4052 create_tags_from_item($item['parent-uri'], $item['uid']);
4053 // ignore the result
4056 // ensure that last-child is set in case the comment that had it just got wiped.
4057 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
4058 dbesc(datetime_convert()),
4059 dbesc($item['parent-uri']),
4060 intval($item['uid'])
4062 // who is the last child now?
4063 $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",
4064 dbesc($item['parent-uri']),
4065 intval($item['uid'])
4068 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
4073 // Add a relayable_retraction signature for Diaspora.
4074 store_diaspora_retract_sig($item, $a->user, $a->get_baseurl());
4076 $drop_id = intval($item['id']);
4078 // send the notification upstream/downstream as the case may be
4080 proc_run('php',"include/notifier.php","drop","$drop_id");
4084 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
4090 notice( t('Permission denied.') . EOL);
4091 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
4098 function first_post_date($uid,$wall = false) {
4099 $r = q("select id, created from item
4100 where uid = %d and wall = %d and deleted = 0 and visible = 1 AND moderated = 0
4102 order by created asc limit 1",
4104 intval($wall ? 1 : 0)
4107 // logger('first_post_date: ' . $r[0]['id'] . ' ' . $r[0]['created'], LOGGER_DATA);
4108 return substr(datetime_convert('',date_default_timezone_get(),$r[0]['created']),0,10);
4113 function posted_dates($uid,$wall) {
4114 $dnow = datetime_convert('',date_default_timezone_get(),'now','Y-m-d');
4116 $dthen = first_post_date($uid,$wall);
4120 // If it's near the end of a long month, backup to the 28th so that in
4121 // consecutive loops we'll always get a whole month difference.
4123 if(intval(substr($dnow,8)) > 28)
4124 $dnow = substr($dnow,0,8) . '28';
4125 if(intval(substr($dthen,8)) > 28)
4126 $dnow = substr($dthen,0,8) . '28';
4129 // Starting with the current month, get the first and last days of every
4130 // month down to and including the month of the first post
4131 while(substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
4132 $dstart = substr($dnow,0,8) . '01';
4133 $dend = substr($dnow,0,8) . get_dim(intval($dnow),intval(substr($dnow,5)));
4134 $start_month = datetime_convert('','',$dstart,'Y-m-d');
4135 $end_month = datetime_convert('','',$dend,'Y-m-d');
4136 $str = day_translate(datetime_convert('','',$dnow,'F Y'));
4137 $ret[] = array($str,$end_month,$start_month);
4138 $dnow = datetime_convert('','',$dnow . ' -1 month', 'Y-m-d');
4144 function posted_date_widget($url,$uid,$wall) {
4147 if(! feature_enabled($uid,'archives'))
4150 // For former Facebook folks that left because of "timeline"
4152 /* if($wall && intval(get_pconfig($uid,'system','no_wall_archive_widget')))
4155 $ret = posted_dates($uid,$wall);
4159 $o = replace_macros(get_markup_template('posted_date_widget.tpl'),array(
4160 '$title' => t('Archives'),
4161 '$size' => ((count($ret) > 6) ? 6 : count($ret)),
4168 function store_diaspora_retract_sig($item, $user, $baseurl) {
4169 // Note that we can't add a target_author_signature
4170 // if the comment was deleted by a remote user. That should be ok, because if a remote user is deleting
4171 // the comment, that means we're the home of the post, and Diaspora will only
4172 // check the parent_author_signature of retractions that it doesn't have to relay further
4174 // I don't think this function gets called for an "unlike," but I'll check anyway
4176 $enabled = intval(get_config('system','diaspora_enabled'));
4178 logger('drop_item: diaspora support disabled, not storing retraction signature', LOGGER_DEBUG);
4182 logger('drop_item: storing diaspora retraction signature');
4184 $signed_text = $item['guid'] . ';' . ( ($item['verb'] === ACTIVITY_LIKE) ? 'Like' : 'Comment');
4186 if(local_user() == $item['uid']) {
4188 $handle = $user['nickname'] . '@' . substr($baseurl, strpos($baseurl,'://') + 3);
4189 $authorsig = base64_encode(rsa_sign($signed_text,$user['prvkey'],'sha256'));
4192 $r = q("SELECT `nick`, `url` FROM `contact` WHERE `id` = '%d' LIMIT 1",
4193 $item['contact-id'] // If this function gets called, drop_item() has already checked remote_user() == $item['contact-id']
4196 // The below handle only works for NETWORK_DFRN. I think that's ok, because this function
4197 // only handles DFRN deletes
4198 $handle_baseurl_start = strpos($r['url'],'://') + 3;
4199 $handle_baseurl_length = strpos($r['url'],'/profile') - $handle_baseurl_start;
4200 $handle = $r['nick'] . '@' . substr($r['url'], $handle_baseurl_start, $handle_baseurl_length);
4206 q("insert into sign (`retract_iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
4207 intval($item['id']),
4208 dbesc($signed_text),