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/files.php');
10 require_once('include/text.php');
11 require_once('include/email.php');
12 require_once('include/ostatus_conversation.php');
13 require_once('include/threads.php');
14 require_once('include/socgraph.php');
15 require_once('mod/share.php');
17 function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0, $forpubsub = false) {
20 $sitefeed = ((strlen($owner_nick)) ? false : true); // not yet implemented, need to rewrite huge chunks of following logic
21 $public_feed = (($dfrn_id) ? false : true);
22 $starred = false; // not yet implemented, possible security issues
25 if($public_feed && $a->argc > 2) {
26 for($x = 2; $x < $a->argc; $x++) {
27 if($a->argv[$x] == 'converse')
29 if($a->argv[$x] == 'starred')
31 if($a->argv[$x] === 'category' && $a->argc > ($x + 1) && strlen($a->argv[$x+1]))
32 $category = $a->argv[$x+1];
38 // default permissions - anonymous user
40 $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
42 $r = q("SELECT `contact`.*, `user`.`uid` AS `user_uid`, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`
43 FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
44 WHERE `contact`.`self` = 1 AND `user`.`nickname` = '%s' LIMIT 1",
52 $owner_id = $owner['user_uid'];
53 $owner_nick = $owner['nickname'];
55 $birthday = feed_birthday($owner_id,$owner['timezone']);
65 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
69 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
70 $my_id = '1:' . $dfrn_id;
73 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
74 $my_id = '0:' . $dfrn_id;
81 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
89 require_once('include/security.php');
90 $groups = init_groups_visitor($contact['id']);
93 for($x = 0; $x < count($groups); $x ++)
94 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
95 $gs = implode('|', $groups);
98 $gs = '<<>>' ; // Impossible to match
100 $sql_extra = sprintf("
101 AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' )
102 AND ( `deny_cid` = '' OR NOT `deny_cid` REGEXP '<%d>' )
103 AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
104 AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s')
106 intval($contact['id']),
107 intval($contact['id']),
118 // Include answers to status.net posts in pubsub feeds
120 $sql_post_table = "INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent` ";
121 $visibility = sprintf("OR (`item`.`network` = '%s' AND `thread`.`network`='%s')",
122 dbesc(NETWORK_DFRN), dbesc(NETWORK_OSTATUS));
123 $date_field = "`received`";
124 $sql_order = "`item`.`received` DESC";
126 $date_field = "`changed`";
127 $sql_order = "`item`.`parent` ".$sort.", `item`.`created` ASC";
130 if(! strlen($last_update))
131 $last_update = 'now -30 days';
133 if(isset($category)) {
134 $sql_post_table = sprintf("INNER JOIN (SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d ORDER BY `tid` DESC) AS `term` ON `item`.`id` = `term`.`oid` ",
135 dbesc(protect_sprintf($category)), intval(TERM_OBJ_POST), intval(TERM_CATEGORY), intval($owner_id));
136 //$sql_extra .= file_tag_file_query('item',$category,'category');
141 $sql_extra .= " AND `contact`.`self` = 1 ";
144 $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
146 // AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
147 // dbesc($check_date),
149 $r = q("SELECT STRAIGHT_JOIN `item`.*, `item`.`id` AS `item_id`,
150 `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`,
151 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
152 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
153 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`,
154 `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
155 FROM `item` $sql_post_table
156 INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
157 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
158 LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
159 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`parent` != 0
160 AND ((`item`.`wall` = 1) $visibility) AND `item`.$date_field > '%s'
162 ORDER BY $sql_order LIMIT 0, 300",
168 // Will check further below if this actually returned results.
169 // We will provide an empty feed if that is the case.
173 $feed_template = get_markup_template(($dfrn_id) ? 'atom_feed_dfrn.tpl' : 'atom_feed.tpl');
177 $hubxml = feed_hublinks();
179 $salmon = feed_salmonlinks($owner_nick);
181 $alternatelink = $owner['url'];
184 $alternatelink .= "/category/".$category;
186 $atom .= replace_macros($feed_template, array(
187 '$version' => xmlify(FRIENDICA_VERSION),
188 '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
189 '$feed_title' => xmlify($owner['name']),
190 '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', 'now' , ATOM_TIME)) ,
192 '$salmon' => $salmon,
193 '$alternatelink' => xmlify($alternatelink),
194 '$name' => xmlify($owner['name']),
195 '$profile_page' => xmlify($owner['url']),
196 '$photo' => xmlify($owner['photo']),
197 '$thumb' => xmlify($owner['thumb']),
198 '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
199 '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
200 '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
201 '$birthday' => ((strlen($birthday)) ? '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>' : ''),
202 '$community' => (($owner['page-flags'] == PAGE_COMMUNITY) ? '<dfrn:community>1</dfrn:community>' : '')
205 call_hooks('atom_feed', $atom);
207 if(! count($items)) {
209 call_hooks('atom_feed_end', $atom);
211 $atom .= '</feed>' . "\r\n";
215 foreach($items as $item) {
217 // prevent private email from leaking.
218 if($item['network'] === NETWORK_MAIL)
221 // public feeds get html, our own nodes use bbcode
225 // catch any email that's in a public conversation and make sure it doesn't leak
233 $atom .= atom_entry($item,$type,null,$owner,true);
236 call_hooks('atom_feed_end', $atom);
238 $atom .= '</feed>' . "\r\n";
244 function construct_verb($item) {
246 return $item['verb'];
247 return ACTIVITY_POST;
250 function construct_activity_object($item) {
252 if($item['object']) {
253 $o = '<as:object>' . "\r\n";
254 $r = parse_xml_string($item['object'],false);
260 $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
262 $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
264 $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
266 if(substr($r->link,0,1) === '<') {
267 // patch up some facebook "like" activity objects that got stored incorrectly
268 // for a couple of months prior to 9-Jun-2011 and generated bad XML.
269 // we can probably remove this hack here and in the following function in a few months time.
270 if(strstr($r->link,'&') && (! strstr($r->link,'&')))
271 $r->link = str_replace('&','&', $r->link);
272 $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
276 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
279 $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
280 $o .= '</as:object>' . "\r\n";
287 function construct_activity_target($item) {
289 if($item['target']) {
290 $o = '<as:target>' . "\r\n";
291 $r = parse_xml_string($item['target'],false);
295 $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
297 $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
299 $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
301 if(substr($r->link,0,1) === '<') {
302 if(strstr($r->link,'&') && (! strstr($r->link,'&')))
303 $r->link = str_replace('&','&', $r->link);
304 $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
308 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
311 $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
312 $o .= '</as:target>' . "\r\n";
321 * The purpose of this function is to apply system message length limits to
322 * imported messages without including any embedded photos in the length
324 if(! function_exists('limit_body_size')) {
325 function limit_body_size($body) {
327 // logger('limit_body_size: start', LOGGER_DEBUG);
329 $maxlen = get_max_import_size();
331 // If the length of the body, including the embedded images, is smaller
332 // than the maximum, then don't waste time looking for the images
333 if($maxlen && (strlen($body) > $maxlen)) {
335 logger('limit_body_size: the total body length exceeds the limit', LOGGER_DEBUG);
342 $img_start = strpos($orig_body, '[img');
343 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
344 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
345 while(($img_st_close !== false) && ($img_end !== false)) {
347 $img_st_close++; // make it point to AFTER the closing bracket
348 $img_end += $img_start;
349 $img_end += strlen('[/img]');
351 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
352 // This is an embedded image
354 if( ($textlen + $img_start) > $maxlen ) {
355 if($textlen < $maxlen) {
356 logger('limit_body_size: the limit happens before an embedded image', LOGGER_DEBUG);
357 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
362 $new_body = $new_body . substr($orig_body, 0, $img_start);
363 $textlen += $img_start;
366 $new_body = $new_body . substr($orig_body, $img_start, $img_end - $img_start);
370 if( ($textlen + $img_end) > $maxlen ) {
371 if($textlen < $maxlen) {
372 logger('limit_body_size: the limit happens before the end of a non-embedded image', LOGGER_DEBUG);
373 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
378 $new_body = $new_body . substr($orig_body, 0, $img_end);
379 $textlen += $img_end;
382 $orig_body = substr($orig_body, $img_end);
384 if($orig_body === false) // in case the body ends on a closing image tag
387 $img_start = strpos($orig_body, '[img');
388 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
389 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
392 if( ($textlen + strlen($orig_body)) > $maxlen) {
393 if($textlen < $maxlen) {
394 logger('limit_body_size: the limit happens after the end of the last image', LOGGER_DEBUG);
395 $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
400 logger('limit_body_size: the text size with embedded images extracted did not violate the limit', LOGGER_DEBUG);
401 $new_body = $new_body . $orig_body;
402 $textlen += strlen($orig_body);
411 function title_is_body($title, $body) {
413 $title = strip_tags($title);
414 $title = trim($title);
415 $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
416 $title = str_replace(array("\n", "\r", "\t", " "), array("","","",""), $title);
418 $body = strip_tags($body);
420 $body = html_entity_decode($body, ENT_QUOTES, 'UTF-8');
421 $body = str_replace(array("\n", "\r", "\t", " "), array("","","",""), $body);
423 if (strlen($title) < strlen($body))
424 $body = substr($body, 0, strlen($title));
426 if (($title != $body) and (substr($title, -3) == "...")) {
427 $pos = strrpos($title, "...");
429 $title = substr($title, 0, $pos);
430 $body = substr($body, 0, $pos);
434 return($title == $body);
439 function get_atom_elements($feed, $item, $contact = array()) {
441 require_once('library/HTMLPurifier.auto.php');
442 require_once('include/html2bbcode.php');
444 $best_photo = array();
448 $author = $item->get_author();
450 $res['author-name'] = unxmlify($author->get_name());
451 $res['author-link'] = unxmlify($author->get_link());
454 $res['author-name'] = unxmlify($feed->get_title());
455 $res['author-link'] = unxmlify($feed->get_permalink());
457 $res['uri'] = unxmlify($item->get_id());
458 $res['title'] = unxmlify($item->get_title());
459 $res['body'] = unxmlify($item->get_content());
460 $res['plink'] = unxmlify($item->get_link(0));
462 if (isset($contact["network"]) AND ($contact["network"] == NETWORK_FEED) AND strstr($res['plink'], ".app.net/")) {
463 logger("get_atom_elements: detected app.net posting: ".print_r($res, true), LOGGER_DEBUG);
465 $res['body'] = nl2br($res['body']);
468 // removing the content of the title if its identically to the body
469 // This helps with auto generated titles e.g. from tumblr
470 if (title_is_body($res["title"], $res["body"]))
474 $base_url = implode('/', array_slice(explode('/',$res['plink']),0,3));
478 // look for a photo. We should check media size and find the best one,
479 // but for now let's just find any author photo
480 // Additionally we look for an alternate author link. On OStatus this one is the one we want.
482 // Search for ostatus conversation url
483 $authorlinks = $item->feed->data["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["feed"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["author"][0]["child"]["http://www.w3.org/2005/Atom"]["link"];
484 if (is_array($authorlinks)) {
485 foreach ($authorlinks as $link) {
486 $linkdata = array_shift($link["attribs"]);
488 if ($linkdata["rel"] == "alternate")
489 $res["author-link"] = $linkdata["href"];
493 $rawauthor = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
495 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
496 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
497 foreach($base as $link) {
498 if($link['attribs']['']['rel'] === 'alternate')
499 $res['author-link'] = unxmlify($link['attribs']['']['href']);
501 if(!x($res, 'author-avatar') || !$res['author-avatar']) {
502 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
503 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
508 $rawactor = $item->get_item_tags(NAMESPACE_ACTIVITY, 'actor');
510 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
511 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
512 if($base && count($base)) {
513 foreach($base as $link) {
514 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
515 $res['author-link'] = unxmlify($link['attribs']['']['href']);
516 if(!x($res, 'author-avatar') || !$res['author-avatar']) {
517 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
518 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
524 // No photo/profile-link on the item - look at the feed level
526 if((! (x($res,'author-link'))) || (! (x($res,'author-avatar')))) {
527 $rawauthor = $feed->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
528 if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
529 $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
530 foreach($base as $link) {
531 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
532 $res['author-link'] = unxmlify($link['attribs']['']['href']);
533 if(! $res['author-avatar']) {
534 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
535 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
540 $rawactor = $feed->get_feed_tags(NAMESPACE_ACTIVITY, 'subject');
542 if($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'],ACTIVITY_OBJ_PERSON)) {
543 $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
545 if($base && count($base)) {
546 foreach($base as $link) {
547 if($link['attribs']['']['rel'] === 'alternate' && (! $res['author-link']))
548 $res['author-link'] = unxmlify($link['attribs']['']['href']);
549 if(! (x($res,'author-avatar'))) {
550 if($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo')
551 $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
558 $apps = $item->get_item_tags(NAMESPACE_STATUSNET,'notice_info');
559 if($apps && $apps[0]['attribs']['']['source']) {
560 $res['app'] = strip_tags(unxmlify($apps[0]['attribs']['']['source']));
561 if($res['app'] === 'web')
562 $res['app'] = 'OStatus';
565 // base64 encoded json structure representing Diaspora signature
567 $dsig = $item->get_item_tags(NAMESPACE_DFRN,'diaspora_signature');
569 $res['dsprsig'] = unxmlify($dsig[0]['data']);
572 $dguid = $item->get_item_tags(NAMESPACE_DFRN,'diaspora_guid');
574 $res['guid'] = unxmlify($dguid[0]['data']);
576 $bm = $item->get_item_tags(NAMESPACE_DFRN,'bookmark');
578 $res['bookmark'] = ((unxmlify($bm[0]['data']) === 'true') ? 1 : 0);
582 * If there's a copy of the body content which is guaranteed to have survived mangling in transit, use it.
585 $have_real_body = false;
587 $rawenv = $item->get_item_tags(NAMESPACE_DFRN, 'env');
589 $have_real_body = true;
590 $res['body'] = $rawenv[0]['data'];
591 $res['body'] = str_replace(array(' ',"\t","\r","\n"), array('','','',''),$res['body']);
592 // make sure nobody is trying to sneak some html tags by us
593 $res['body'] = notags(base64url_decode($res['body']));
597 $res['body'] = limit_body_size($res['body']);
599 // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust
600 // the content type. Our own network only emits text normally, though it might have been converted to
601 // html if we used a pubsubhubbub transport. But if we see even one html tag in our text, we will
602 // have to assume it is all html and needs to be purified.
604 // It doesn't matter all that much security wise - because before this content is used anywhere, we are
605 // going to escape any tags we find regardless, but this lets us import a limited subset of html from
606 // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining
609 if((strpos($res['body'],'<') !== false) && (strpos($res['body'],'>') !== false)) {
611 $res['body'] = reltoabs($res['body'],$base_url);
613 $res['body'] = html2bb_video($res['body']);
615 $res['body'] = oembed_html2bbcode($res['body']);
617 $config = HTMLPurifier_Config::createDefault();
618 $config->set('Cache.DefinitionImpl', null);
620 // we shouldn't need a whitelist, because the bbcode converter
621 // will strip out any unsupported tags.
623 $purifier = new HTMLPurifier($config);
624 $res['body'] = $purifier->purify($res['body']);
626 $res['body'] = @html2bbcode($res['body']);
630 elseif(! $have_real_body) {
632 // it's not one of our messages and it has no tags
633 // so it's probably just text. We'll escape it just to be safe.
635 $res['body'] = escape_tags($res['body']);
639 // this tag is obsolete but we keep it for really old sites
641 $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
642 if($allow && $allow[0]['data'] == 1)
643 $res['last-child'] = 1;
645 $res['last-child'] = 0;
647 $private = $item->get_item_tags(NAMESPACE_DFRN,'private');
648 if($private && intval($private[0]['data']) > 0)
649 $res['private'] = intval($private[0]['data']);
653 $extid = $item->get_item_tags(NAMESPACE_DFRN,'extid');
654 if($extid && $extid[0]['data'])
655 $res['extid'] = $extid[0]['data'];
657 $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
659 $res['location'] = unxmlify($rawlocation[0]['data']);
662 $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
664 $res['created'] = unxmlify($rawcreated[0]['data']);
667 $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
669 $res['edited'] = unxmlify($rawedited[0]['data']);
671 if((x($res,'edited')) && (! (x($res,'created'))))
672 $res['created'] = $res['edited'];
674 if(! $res['created'])
675 $res['created'] = $item->get_date('c');
678 $res['edited'] = $item->get_date('c');
681 // Disallow time travelling posts
683 $d1 = strtotime($res['created']);
684 $d2 = strtotime($res['edited']);
685 $d3 = strtotime('now');
688 $res['created'] = datetime_convert();
690 $res['edited'] = datetime_convert();
692 $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
693 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
694 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
695 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
696 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
697 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
698 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
699 elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
700 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
702 if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
703 $base = $rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
705 foreach($base as $link) {
706 if(!x($res, 'owner-avatar') || !$res['owner-avatar']) {
707 if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
708 $res['owner-avatar'] = unxmlify($link['attribs']['']['href']);
713 $rawgeo = $item->get_item_tags(NAMESPACE_GEORSS,'point');
715 $res['coord'] = unxmlify($rawgeo[0]['data']);
717 if ($contact["network"] == NETWORK_FEED) {
718 $res['verb'] = ACTIVITY_POST;
719 $res['object-type'] = ACTIVITY_OBJ_NOTE;
722 $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
724 // select between supported verbs
727 $res['verb'] = unxmlify($rawverb[0]['data']);
730 // translate OStatus unfollow to activity streams if it happened to get selected
732 if((x($res,'verb')) && ($res['verb'] === 'http://ostatus.org/schema/1.0/unfollow'))
733 $res['verb'] = ACTIVITY_UNFOLLOW;
735 $cats = $item->get_categories();
738 foreach($cats as $cat) {
739 $term = $cat->get_term();
741 $term = $cat->get_label();
742 $scheme = $cat->get_scheme();
743 if($scheme && $term && stristr($scheme,'X-DFRN:'))
744 $tag_arr[] = substr($scheme,7,1) . '[url=' . unxmlify(substr($scheme,9)) . ']' . unxmlify($term) . '[/url]';
746 $tag_arr[] = notags(trim($term));
748 $res['tag'] = implode(',', $tag_arr);
751 $attach = $item->get_enclosures();
754 foreach($attach as $att) {
755 $len = intval($att->get_length());
756 $link = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_link()))));
757 $title = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_title()))));
758 $type = str_replace(array(',','"'),array('%2D','%22'),notags(trim(unxmlify($att->get_type()))));
759 if(strpos($type,';'))
760 $type = substr($type,0,strpos($type,';'));
761 if((! $link) || (strpos($link,'http') !== 0))
767 $type = 'application/octet-stream';
769 $att_arr[] = '[attach]href="' . $link . '" length="' . $len . '" type="' . $type . '" title="' . $title . '"[/attach]';
771 $res['attach'] = implode(',', $att_arr);
774 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
777 $res['object'] = '<object>' . "\n";
778 $child = $rawobj[0]['child'];
779 if($child[NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
780 $res['object-type'] = $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'];
781 $res['object'] .= '<type>' . $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
783 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'id') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
784 $res['object'] .= '<id>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
785 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'link') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
786 $res['object'] .= '<link>' . encode_rel_links($child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
787 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'title') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
788 $res['object'] .= '<title>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
789 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'content') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
790 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
792 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
793 // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
794 $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
795 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
797 $body = html2bb_video($body);
799 $config = HTMLPurifier_Config::createDefault();
800 $config->set('Cache.DefinitionImpl', null);
802 $purifier = new HTMLPurifier($config);
803 $body = $purifier->purify($body);
804 $body = html2bbcode($body);
807 $res['object'] .= '<content>' . $body . '</content>' . "\n";
810 $res['object'] .= '</object>' . "\n";
813 $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'target');
816 $res['target'] = '<target>' . "\n";
817 $child = $rawobj[0]['child'];
818 if($child[NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
819 $res['target'] .= '<type>' . $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
821 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'id') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
822 $res['target'] .= '<id>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
823 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'link') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['link'])
824 $res['target'] .= '<link>' . encode_rel_links($child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
825 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'data') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
826 $res['target'] .= '<title>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
827 if(x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'data') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
828 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
830 $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
831 // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
832 $res['target'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
833 if((strpos($body,'<') !== false) || (strpos($body,'>') !== false)) {
835 $body = html2bb_video($body);
837 $config = HTMLPurifier_Config::createDefault();
838 $config->set('Cache.DefinitionImpl', null);
840 $purifier = new HTMLPurifier($config);
841 $body = $purifier->purify($body);
842 $body = html2bbcode($body);
845 $res['target'] .= '<content>' . $body . '</content>' . "\n";
848 $res['target'] .= '</target>' . "\n";
851 // This is some experimental stuff. By now retweets are shown with "RT:"
852 // But: There is data so that the message could be shown similar to native retweets
853 // There is some better way to parse this array - but it didn't worked for me.
854 $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"];
855 if (is_array($child)) {
856 logger('get_atom_elements: Looking for status.net repeated message');
858 $message = $child["http://activitystrea.ms/spec/1.0/"]["object"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["content"][0]["data"];
859 $orig_id = ostatus_convert_href($child["http://activitystrea.ms/spec/1.0/"]["object"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["id"][0]["data"]);
860 $author = $child[SIMPLEPIE_NAMESPACE_ATOM_10]["author"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10];
861 $uri = $author["uri"][0]["data"];
862 $name = $author["name"][0]["data"];
863 $avatar = @array_shift($author["link"][2]["attribs"]);
864 $avatar = $avatar["href"];
866 if (($name != "") and ($uri != "") and ($avatar != "") and ($message != "")) {
867 logger('get_atom_elements: fixing sender of repeated message. '.$orig_id, LOGGER_DEBUG);
869 if (!intval(get_config('system','wall-to-wall_share'))) {
870 $prefix = share_header($name, $uri, $avatar, "", "", $orig_link);
872 $res["body"] = $prefix.html2bbcode($message)."[/share]";
874 $res["owner-name"] = $res["author-name"];
875 $res["owner-link"] = $res["author-link"];
876 $res["owner-avatar"] = $res["author-avatar"];
878 $res["author-name"] = $name;
879 $res["author-link"] = $uri;
880 $res["author-avatar"] = $avatar;
882 $res["body"] = html2bbcode($message);
887 // Search for ostatus conversation url
888 $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"];
890 if (is_array($links)) {
891 foreach ($links as $link) {
892 $conversation = array_shift($link["attribs"]);
894 if ($conversation["rel"] == "ostatus:conversation") {
895 $res["ostatus_conversation"] = ostatus_convert_href($conversation["href"]);
896 logger('get_atom_elements: found conversation url '.$res["ostatus_conversation"]);
897 } elseif ($conversation["rel"] == "alternate") {
898 $res["plink"] = $conversation["href"];
899 logger('get_atom_elements: found plink '.$res["plink"]);
904 if (isset($contact["network"]) AND ($contact["network"] == NETWORK_FEED) AND $contact['fetch_further_information']) {
907 // Handle enclosures and treat them as preview picture
909 foreach ($attach AS $attachment)
910 if ($attachment->type == "image/jpeg")
911 $preview = $attachment->link;
913 $res["body"] = $res["title"].add_page_info($res['plink'], false, $preview, ($contact['fetch_further_information'] == 2), $contact['ffi_keyword_blacklist']);
914 $res["tag"] = add_page_keywords($res['plink'], false, $preview, ($contact['fetch_further_information'] == 2), $contact['ffi_keyword_blacklist']);
916 $res["object-type"] = ACTIVITY_OBJ_BOOKMARK;
917 unset($res["attach"]);
918 } elseif (isset($contact["network"]) AND ($contact["network"] == NETWORK_OSTATUS))
919 $res["body"] = add_page_info_to_body($res["body"]);
920 elseif (isset($contact["network"]) AND ($contact["network"] == NETWORK_FEED) AND strstr($res['plink'], ".app.net/")) {
921 $res["body"] = add_page_info_to_body($res["body"]);
924 $arr = array('feed' => $feed, 'item' => $item, 'result' => $res);
926 call_hooks('parse_atom', $arr);
931 function add_page_info_data($data) {
932 call_hooks('page_info_data', $data);
934 // It maybe is a rich content, but if it does have everything that a link has,
935 // then treat it that way
936 if (($data["type"] == "rich") AND is_string($data["title"]) AND
937 is_string($data["text"]) AND (sizeof($data["images"]) > 0))
938 $data["type"] = "link";
940 if ((($data["type"] != "link") AND ($data["type"] != "video") AND ($data["type"] != "photo")) OR ($data["title"] == $url))
943 if ($no_photos AND ($data["type"] == "photo"))
946 // If the link contains BBCode stuff, make a short link out of this to avoid parsing problems
947 if (strpos($data["url"], '[') OR strpos($data["url"], ']')) {
948 require_once("include/network.php");
949 $data["url"] = short_link($data["url"]);
952 if (($data["type"] != "photo") AND is_string($data["title"]))
953 $text .= "[bookmark=".$data["url"]."]".trim($data["title"])."[/bookmark]";
955 if (($data["type"] != "video") AND ($photo != ""))
956 $text .= '[img]'.$photo.'[/img]';
957 elseif (($data["type"] != "video") AND (sizeof($data["images"]) > 0)) {
958 $imagedata = $data["images"][0];
959 $text .= '[img]'.$imagedata["src"].'[/img]';
962 if (($data["type"] != "photo") AND is_string($data["text"]))
963 $text .= "[quote]".$data["text"]."[/quote]";
966 if (isset($data["keywords"]) AND count($data["keywords"])) {
969 foreach ($data["keywords"] AS $keyword) {
970 $hashtag = str_replace(array(" ", "+", "/", ".", "#", "'"),
971 array("","", "", "", "", ""), $keyword);
972 $hashtags .= "#[url=".$a->get_baseurl()."/search?tag=".rawurlencode($hashtag)."]".$hashtag."[/url] ";
976 return("\n[class=type-".$data["type"]."]".$text."[/class]".$hashtags);
979 function query_page_info($url, $no_photos = false, $photo = "", $keywords = false, $keyword_blacklist = "") {
980 require_once("mod/parse_url.php");
982 $data = Cache::get("parse_url:".$url);
984 $data = parseurl_getsiteinfo($url, true);
985 Cache::set("parse_url:".$url,serialize($data));
987 $data = unserialize($data);
990 $data["images"][0]["src"] = $photo;
992 logger('fetch page info for '.$url.' '.print_r($data, true), LOGGER_DEBUG);
994 if (!$keywords AND isset($data["keywords"]))
995 unset($data["keywords"]);
997 if (($keyword_blacklist != "") AND isset($data["keywords"])) {
998 $list = explode(",", $keyword_blacklist);
999 foreach ($list AS $keyword) {
1000 $keyword = trim($keyword);
1001 $index = array_search($keyword, $data["keywords"]);
1002 if ($index !== false)
1003 unset($data["keywords"][$index]);
1010 function add_page_keywords($url, $no_photos = false, $photo = "", $keywords = false, $keyword_blacklist = "") {
1011 $data = query_page_info($url, $no_photos, $photo, $keywords, $keyword_blacklist);
1014 if (isset($data["keywords"]) AND count($data["keywords"])) {
1016 foreach ($data["keywords"] AS $keyword) {
1017 $hashtag = str_replace(array(" ", "+", "/", ".", "#", "'"),
1018 array("","", "", "", "", ""), $keyword);
1023 $tags .= "#[url=".$a->get_baseurl()."/search?tag=".rawurlencode($hashtag)."]".$hashtag."[/url]";
1030 function add_page_info($url, $no_photos = false, $photo = "", $keywords = false, $keyword_blacklist = "") {
1031 $data = query_page_info($url, $no_photos, $photo, $keywords, $keyword_blacklist);
1033 $text = add_page_info_data($data);
1038 function add_page_info_to_body($body, $texturl = false, $no_photos = false) {
1040 logger('add_page_info_to_body: fetch page info for body '.$body, LOGGER_DEBUG);
1042 $URLSearchString = "^\[\]";
1044 // Adding these spaces is a quick hack due to my problems with regular expressions :)
1045 preg_match("/[^!#@]\[url\]([$URLSearchString]*)\[\/url\]/ism", " ".$body, $matches);
1048 preg_match("/[^!#@]\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", " ".$body, $matches);
1050 // Convert urls without bbcode elements
1051 if (!$matches AND $texturl) {
1052 preg_match("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", " ".$body, $matches);
1054 // Yeah, a hack. I really hate regular expressions :)
1056 $matches[1] = $matches[2];
1060 $footer = add_page_info($matches[1], $no_photos);
1062 // Remove the link from the body if the link is attached at the end of the post
1063 if (isset($footer) AND (trim($footer) != "") AND (strpos($footer, $matches[1]))) {
1064 $removedlink = trim(str_replace($matches[1], "", $body));
1065 if (($removedlink == "") OR strstr($body, $removedlink))
1066 $body = $removedlink;
1068 $url = str_replace(array('/', '.'), array('\/', '\.'), $matches[1]);
1069 $removedlink = preg_replace("/\[url\=".$url."\](.*?)\[\/url\]/ism", '', $body);
1070 if (($removedlink == "") OR strstr($body, $removedlink))
1071 $body = $removedlink;
1074 // Add the page information to the bottom
1075 if (isset($footer) AND (trim($footer) != ""))
1081 function encode_rel_links($links) {
1083 if(! ((is_array($links)) && (count($links))))
1085 foreach($links as $link) {
1087 if($link['attribs']['']['rel'])
1088 $o .= 'rel="' . $link['attribs']['']['rel'] . '" ';
1089 if($link['attribs']['']['type'])
1090 $o .= 'type="' . $link['attribs']['']['type'] . '" ';
1091 if($link['attribs']['']['href'])
1092 $o .= 'href="' . $link['attribs']['']['href'] . '" ';
1093 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['width'])
1094 $o .= 'media:width="' . $link['attribs'][NAMESPACE_MEDIA]['width'] . '" ';
1095 if( (x($link['attribs'],NAMESPACE_MEDIA)) && $link['attribs'][NAMESPACE_MEDIA]['height'])
1096 $o .= 'media:height="' . $link['attribs'][NAMESPACE_MEDIA]['height'] . '" ';
1097 $o .= ' />' . "\n" ;
1104 function item_store($arr,$force_parent = false, $notify = false, $dontcache = false) {
1106 // If it is a posting where users should get notifications, then define it as wall posting
1109 $arr['type'] = 'wall';
1111 $arr['last-child'] = 1;
1112 $arr['network'] = NETWORK_DFRN;
1115 // If a Diaspora signature structure was passed in, pull it out of the
1116 // item array and set it aside for later storage.
1119 if(x($arr,'dsprsig')) {
1120 $dsprsig = json_decode(base64_decode($arr['dsprsig']));
1121 unset($arr['dsprsig']);
1124 // Converting the plink
1125 if ($arr['network'] == NETWORK_OSTATUS) {
1126 if (isset($arr['plink']))
1127 $arr['plink'] = ostatus_convert_href($arr['plink']);
1128 elseif (isset($arr['uri']))
1129 $arr['plink'] = ostatus_convert_href($arr['uri']);
1132 // if an OStatus conversation url was passed in, it is stored and then
1133 // removed from the array.
1134 $ostatus_conversation = null;
1136 if (isset($arr["ostatus_conversation"])) {
1137 $ostatus_conversation = $arr["ostatus_conversation"];
1138 unset($arr["ostatus_conversation"]);
1141 if(x($arr, 'gravity'))
1142 $arr['gravity'] = intval($arr['gravity']);
1143 elseif($arr['parent-uri'] === $arr['uri'])
1144 $arr['gravity'] = 0;
1145 elseif(activity_match($arr['verb'],ACTIVITY_POST))
1146 $arr['gravity'] = 6;
1148 $arr['gravity'] = 6; // extensible catchall
1150 if(! x($arr,'type'))
1151 $arr['type'] = 'remote';
1155 /* check for create date and expire time */
1156 $uid = intval($arr['uid']);
1157 $r = q("SELECT expire FROM user WHERE uid = %d", intval($uid));
1159 $expire_interval = $r[0]['expire'];
1160 if ($expire_interval>0) {
1161 $expire_date = new DateTime( '- '.$expire_interval.' days', new DateTimeZone('UTC'));
1162 $created_date = new DateTime($arr['created'], new DateTimeZone('UTC'));
1163 if ($created_date < $expire_date) {
1164 logger('item-store: item created ('.$arr['created'].') before expiration time ('.$expire_date->format(DateTime::W3C).'). ignored. ' . print_r($arr,true), LOGGER_DEBUG);
1170 // If there is no guid then take the same guid that was taken before for the same uri
1171 if ((trim($arr['guid']) == "") AND (trim($arr['uri']) != "")) {
1172 logger('item_store: checking for an existing guid for uri '.$arr['uri'], LOGGER_DEBUG);
1173 $r = q("SELECT `guid` FROM `item` WHERE `uri` = '%s' AND `guid` != '' LIMIT 1",
1174 dbesc(trim($arr['uri']))
1178 $arr['guid'] = $r[0]["guid"];
1179 logger('item_store: found guid '.$arr['guid'].' for uri '.$arr['uri'], LOGGER_DEBUG);
1183 // If there is no guid then take the same guid that was taken before for the same plink
1184 if ((trim($arr['guid']) == "") AND (trim($arr['plink']) != "")) {
1185 logger('item_store: checking for an existing guid for plink '.$arr['plink'], LOGGER_DEBUG);
1186 $r = q("SELECT `guid` FROM `item` WHERE `plink` = '%s' AND `guid` != '' LIMIT 1",
1187 dbesc(trim($arr['plink']))
1191 $arr['guid'] = $r[0]["guid"];
1192 logger('item_store: found guid '.$arr['guid'].' for plink '.$arr['plink'], LOGGER_DEBUG);
1196 // Shouldn't happen but we want to make absolutely sure it doesn't leak from a plugin.
1197 // Deactivated, since the bbcode parser can handle with it - and it destroys posts with some smileys that contain "<"
1198 //if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false))
1199 // $arr['body'] = strip_tags($arr['body']);
1202 if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
1203 require_once('library/langdet/Text/LanguageDetect.php');
1204 $naked_body = preg_replace('/\[(.+?)\]/','',$arr['body']);
1205 $l = new Text_LanguageDetect;
1206 //$lng = $l->detectConfidence($naked_body);
1207 //$arr['postopts'] = (($lng['language']) ? 'lang=' . $lng['language'] . ';' . $lng['confidence'] : '');
1208 $lng = $l->detect($naked_body, 3);
1210 if (sizeof($lng) > 0) {
1213 foreach ($lng as $language => $score) {
1214 if ($postopts == "")
1215 $postopts = "lang=";
1219 $postopts .= $language.";".$score;
1221 $arr['postopts'] = $postopts;
1225 $arr['wall'] = ((x($arr,'wall')) ? intval($arr['wall']) : 0);
1226 $arr['uri'] = ((x($arr,'uri')) ? notags(trim($arr['uri'])) : random_string());
1227 $arr['extid'] = ((x($arr,'extid')) ? notags(trim($arr['extid'])) : '');
1228 $arr['author-name'] = ((x($arr,'author-name')) ? notags(trim($arr['author-name'])) : '');
1229 $arr['author-link'] = ((x($arr,'author-link')) ? notags(trim($arr['author-link'])) : '');
1230 $arr['author-avatar'] = ((x($arr,'author-avatar')) ? notags(trim($arr['author-avatar'])) : '');
1231 $arr['owner-name'] = ((x($arr,'owner-name')) ? notags(trim($arr['owner-name'])) : '');
1232 $arr['owner-link'] = ((x($arr,'owner-link')) ? notags(trim($arr['owner-link'])) : '');
1233 $arr['owner-avatar'] = ((x($arr,'owner-avatar')) ? notags(trim($arr['owner-avatar'])) : '');
1234 $arr['created'] = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
1235 $arr['edited'] = ((x($arr,'edited') !== false) ? datetime_convert('UTC','UTC',$arr['edited']) : datetime_convert());
1236 $arr['commented'] = ((x($arr,'commented') !== false) ? datetime_convert('UTC','UTC',$arr['commented']) : datetime_convert());
1237 $arr['received'] = ((x($arr,'received') !== false) ? datetime_convert('UTC','UTC',$arr['received']) : datetime_convert());
1238 $arr['changed'] = ((x($arr,'changed') !== false) ? datetime_convert('UTC','UTC',$arr['changed']) : datetime_convert());
1239 $arr['title'] = ((x($arr,'title')) ? notags(trim($arr['title'])) : '');
1240 $arr['location'] = ((x($arr,'location')) ? notags(trim($arr['location'])) : '');
1241 $arr['coord'] = ((x($arr,'coord')) ? notags(trim($arr['coord'])) : '');
1242 $arr['last-child'] = ((x($arr,'last-child')) ? intval($arr['last-child']) : 0 );
1243 $arr['visible'] = ((x($arr,'visible') !== false) ? intval($arr['visible']) : 1 );
1244 $arr['deleted'] = 0;
1245 $arr['parent-uri'] = ((x($arr,'parent-uri')) ? notags(trim($arr['parent-uri'])) : '');
1246 $arr['verb'] = ((x($arr,'verb')) ? notags(trim($arr['verb'])) : '');
1247 $arr['object-type'] = ((x($arr,'object-type')) ? notags(trim($arr['object-type'])) : '');
1248 $arr['object'] = ((x($arr,'object')) ? trim($arr['object']) : '');
1249 $arr['target-type'] = ((x($arr,'target-type')) ? notags(trim($arr['target-type'])) : '');
1250 $arr['target'] = ((x($arr,'target')) ? trim($arr['target']) : '');
1251 $arr['plink'] = ((x($arr,'plink')) ? notags(trim($arr['plink'])) : '');
1252 $arr['allow_cid'] = ((x($arr,'allow_cid')) ? trim($arr['allow_cid']) : '');
1253 $arr['allow_gid'] = ((x($arr,'allow_gid')) ? trim($arr['allow_gid']) : '');
1254 $arr['deny_cid'] = ((x($arr,'deny_cid')) ? trim($arr['deny_cid']) : '');
1255 $arr['deny_gid'] = ((x($arr,'deny_gid')) ? trim($arr['deny_gid']) : '');
1256 $arr['private'] = ((x($arr,'private')) ? intval($arr['private']) : 0 );
1257 $arr['bookmark'] = ((x($arr,'bookmark')) ? intval($arr['bookmark']) : 0 );
1258 $arr['body'] = ((x($arr,'body')) ? trim($arr['body']) : '');
1259 $arr['tag'] = ((x($arr,'tag')) ? notags(trim($arr['tag'])) : '');
1260 $arr['attach'] = ((x($arr,'attach')) ? notags(trim($arr['attach'])) : '');
1261 $arr['app'] = ((x($arr,'app')) ? notags(trim($arr['app'])) : '');
1262 $arr['origin'] = ((x($arr,'origin')) ? intval($arr['origin']) : 0 );
1263 $arr['network'] = ((x($arr,'network')) ? trim($arr['network']) : '');
1264 $arr['guid'] = ((x($arr,'guid')) ? notags(trim($arr['guid'])) : get_guid(32, $arr['network']));
1265 $arr['postopts'] = ((x($arr,'postopts')) ? trim($arr['postopts']) : '');
1266 $arr['resource-id'] = ((x($arr,'resource-id')) ? trim($arr['resource-id']) : '');
1267 $arr['event-id'] = ((x($arr,'event-id')) ? intval($arr['event-id']) : 0 );
1268 $arr['inform'] = ((x($arr,'inform')) ? trim($arr['inform']) : '');
1269 $arr['file'] = ((x($arr,'file')) ? trim($arr['file']) : '');
1271 if ($arr['plink'] == "") {
1273 $arr['plink'] = $a->get_baseurl().'/display/'.urlencode($arr['guid']);
1276 if ($arr['network'] == "") {
1277 $r = q("SELECT `network` FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
1278 intval($arr['contact-id']),
1283 $arr['network'] = $r[0]["network"];
1285 // Fallback to friendica (why is it empty in some cases?)
1286 if ($arr['network'] == "")
1287 $arr['network'] = NETWORK_DFRN;
1289 logger("item_store: Set network to ".$arr["network"]." for ".$arr["uri"], LOGGER_DEBUG);
1292 if ($arr['guid'] != "") {
1293 // Checking if there is already an item with the same guid
1294 logger('checking for an item for user '.$arr['uid'].' on network '.$arr['network'].' with the guid '.$arr['guid'], LOGGER_DEBUG);
1295 $r = q("SELECT `guid` FROM `item` WHERE `guid` = '%s' AND `network` = '%s' AND `uid` = '%d' LIMIT 1",
1296 dbesc($arr['guid']), dbesc($arr['network']), intval($arr['uid']));
1299 logger('found item with guid '.$arr['guid'].' for user '.$arr['uid'].' on network '.$arr['network'], LOGGER_DEBUG);
1304 // Check for hashtags in the body and repair or add hashtag links
1305 item_body_set_hashtags($arr);
1307 $arr['thr-parent'] = $arr['parent-uri'];
1308 if($arr['parent-uri'] === $arr['uri']) {
1310 $parent_deleted = 0;
1311 $allow_cid = $arr['allow_cid'];
1312 $allow_gid = $arr['allow_gid'];
1313 $deny_cid = $arr['deny_cid'];
1314 $deny_gid = $arr['deny_gid'];
1315 $notify_type = 'wall-new';
1319 // find the parent and snarf the item id and ACLs
1320 // and anything else we need to inherit
1322 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d ORDER BY `id` ASC LIMIT 1",
1323 dbesc($arr['parent-uri']),
1329 // is the new message multi-level threaded?
1330 // even though we don't support it now, preserve the info
1331 // and re-attach to the conversation parent.
1333 if($r[0]['uri'] != $r[0]['parent-uri']) {
1334 $arr['parent-uri'] = $r[0]['parent-uri'];
1335 $z = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `parent-uri` = '%s' AND `uid` = %d
1336 ORDER BY `id` ASC LIMIT 1",
1337 dbesc($r[0]['parent-uri']),
1338 dbesc($r[0]['parent-uri']),
1345 $parent_id = $r[0]['id'];
1346 $parent_deleted = $r[0]['deleted'];
1347 $allow_cid = $r[0]['allow_cid'];
1348 $allow_gid = $r[0]['allow_gid'];
1349 $deny_cid = $r[0]['deny_cid'];
1350 $deny_gid = $r[0]['deny_gid'];
1351 $arr['wall'] = $r[0]['wall'];
1352 $notify_type = 'comment-new';
1354 // if the parent is private, force privacy for the entire conversation
1355 // This differs from the above settings as it subtly allows comments from
1356 // email correspondents to be private even if the overall thread is not.
1358 if($r[0]['private'])
1359 $arr['private'] = $r[0]['private'];
1361 // Edge case. We host a public forum that was originally posted to privately.
1362 // The original author commented, but as this is a comment, the permissions
1363 // weren't fixed up so it will still show the comment as private unless we fix it here.
1365 if((intval($r[0]['forum_mode']) == 1) && (! $r[0]['private']))
1366 $arr['private'] = 0;
1369 // If its a post from myself then tag the thread as "mention"
1370 logger("item_store: Checking if parent ".$parent_id." has to be tagged as mention for user ".$arr['uid'], LOGGER_DEBUG);
1371 $u = q("select * from user where uid = %d limit 1", intval($arr['uid']));
1374 $self = normalise_link($a->get_baseurl() . '/profile/' . $u[0]['nickname']);
1375 logger("item_store: 'myself' is ".$self." for parent ".$parent_id." checking against ".$arr['author-link']." and ".$arr['owner-link'], LOGGER_DEBUG);
1376 if ((normalise_link($arr['author-link']) == $self) OR (normalise_link($arr['owner-link']) == $self)) {
1377 q("UPDATE `thread` SET `mention` = 1 WHERE `iid` = %d", intval($parent_id));
1378 logger("item_store: tagged thread ".$parent_id." as mention for user ".$self, LOGGER_DEBUG);
1384 // Allow one to see reply tweets from status.net even when
1385 // we don't have or can't see the original post.
1388 logger('item_store: $force_parent=true, reply converted to top-level post.');
1390 $arr['parent-uri'] = $arr['uri'];
1391 $arr['gravity'] = 0;
1394 logger('item_store: item parent was not found - ignoring item');
1398 $parent_deleted = 0;
1402 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1",
1404 dbesc($arr['network']),
1407 if($r && count($r)) {
1408 logger('duplicated item with the same uri found. ' . print_r($arr,true));
1412 $r = q("SELECT `id` FROM `item` WHERE `plink` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1",
1413 dbesc($arr['plink']),
1414 dbesc($arr['network']),
1417 if($r && count($r)) {
1418 logger('duplicated item with the same plink found. ' . print_r($arr,true));
1422 // Check for an existing post with the same content. There seems to be a problem with OStatus.
1423 $r = q("SELECT `id` FROM `item` WHERE `body` = '%s' AND `network` = '%s' AND `created` = '%s' AND `contact-id` = %d AND `uid` = %d LIMIT 1",
1424 dbesc($arr['body']),
1425 dbesc($arr['network']),
1426 dbesc($arr['created']),
1427 intval($arr['contact-id']),
1430 if($r && count($r)) {
1431 logger('duplicated item with the same body found. ' . print_r($arr,true));
1435 // Is this item available in the global items (with uid=0)?
1436 if ($arr["uid"] == 0) {
1437 $arr["global"] = true;
1439 q("UPDATE `item` SET `global` = 1 WHERE `guid` = '%s'", dbesc($arr["guid"]));
1441 $isglobal = q("SELECT `global` FROM `item` WHERE `uid` = 0 AND `guid` = '%s'", dbesc($arr["guid"]));
1443 $arr["global"] = (count($isglobal) > 0);
1446 // Fill the cache field
1447 put_item_in_cache($arr);
1449 call_hooks('post_remote',$arr);
1451 if(x($arr,'cancel')) {
1452 logger('item_store: post cancelled by plugin.');
1456 // Store the unescaped version
1461 logger('item_store: ' . print_r($arr,true), LOGGER_DATA);
1463 $r = dbq("INSERT INTO `item` (`"
1464 . implode("`, `", array_keys($arr))
1466 . implode("', '", array_values($arr))
1472 // find the item we just created
1473 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d ORDER BY `id` ASC ",
1479 $current_post = $r[0]['id'];
1480 logger('item_store: created item ' . $current_post);
1482 // Set "success_update" and "last-item" to the date of the last time we heard from this contact
1483 // This can be used to filter for inactive contacts.
1484 // Only do this for public postings to avoid privacy problems, since poco data is public.
1485 // Don't set this value if it isn't from the owner (could be an author that we don't know)
1487 $update = (!$arr['private'] AND (($arr["author-link"] === $arr["owner-link"]) OR ($arr["parent-uri"] === $arr["uri"])));
1489 // Is it a forum? Then we don't care about the rules from above
1490 if (!$update AND ($arr["network"] == NETWORK_DFRN) AND ($arr["parent-uri"] === $arr["uri"])) {
1491 $isforum = q("SELECT `forum` FROM `contact` WHERE `id` = %d AND `forum`",
1492 intval($arr['contact-id']));
1498 q("UPDATE `contact` SET `success_update` = '%s', `last-item` = '%s' WHERE `id` = %d",
1499 dbesc($arr['received']),
1500 dbesc($arr['received']),
1501 intval($arr['contact-id'])
1504 logger('item_store: could not locate created item');
1508 logger('item_store: duplicated post occurred. Removing duplicates. uri = '.$arr['uri'].' uid = '.$arr['uid']);
1509 q("DELETE FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `id` != %d ",
1511 intval($arr['uid']),
1512 intval($current_post)
1516 if((! $parent_id) || ($arr['parent-uri'] === $arr['uri']))
1517 $parent_id = $current_post;
1519 if(strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid))
1522 $private = $arr['private'];
1524 // Set parent id - and also make sure to inherit the parent's ACLs.
1526 $r = q("UPDATE `item` SET `parent` = %d, `allow_cid` = '%s', `allow_gid` = '%s',
1527 `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d, `deleted` = %d WHERE `id` = %d",
1534 intval($parent_deleted),
1535 intval($current_post)
1538 // Complete ostatus threads
1539 if ($ostatus_conversation)
1540 complete_conversation($current_post, $ostatus_conversation);
1542 $arr['id'] = $current_post;
1543 $arr['parent'] = $parent_id;
1544 $arr['allow_cid'] = $allow_cid;
1545 $arr['allow_gid'] = $allow_gid;
1546 $arr['deny_cid'] = $deny_cid;
1547 $arr['deny_gid'] = $deny_gid;
1548 $arr['private'] = $private;
1549 $arr['deleted'] = $parent_deleted;
1551 // update the commented timestamp on the parent
1552 // Only update "commented" if it is really a comment
1553 if (($arr['verb'] == ACTIVITY_POST) OR !get_config("system", "like_no_comment"))
1554 q("UPDATE `item` SET `commented` = '%s', `changed` = '%s' WHERE `id` = %d",
1555 dbesc(datetime_convert()),
1556 dbesc(datetime_convert()),
1560 q("UPDATE `item` SET `changed` = '%s' WHERE `id` = %d",
1561 dbesc(datetime_convert()),
1566 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
1567 intval($current_post),
1568 dbesc($dsprsig->signed_text),
1569 dbesc($dsprsig->signature),
1570 dbesc($dsprsig->signer)
1576 * If this is now the last-child, force all _other_ children of this parent to *not* be last-child
1579 if($arr['last-child']) {
1580 $r = q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d AND `id` != %d",
1582 intval($arr['uid']),
1583 intval($current_post)
1587 $deleted = tag_deliver($arr['uid'],$current_post);
1589 // current post can be deleted if is for a community page and no mention are
1591 if (!$deleted AND !$dontcache) {
1593 $r = q('SELECT * FROM `item` WHERE id = %d', intval($current_post));
1594 if (count($r) == 1) {
1595 call_hooks('post_remote_end', $r[0]);
1597 logger('item_store: new item not found in DB, id ' . $current_post);
1600 // Add every contact of the post to the global contact table
1603 create_tags_from_item($current_post);
1604 create_files_from_item($current_post);
1606 // Only check for notifications on start posts
1607 if ($arr['parent-uri'] === $arr['uri']) {
1608 add_thread($current_post);
1609 logger('item_store: Check notification for contact '.$arr['contact-id'].' and post '.$current_post, LOGGER_DEBUG);
1611 // Send a notification for every new post?
1612 $r = q("SELECT `notify_new_posts` FROM `contact` WHERE `id` = %d AND `uid` = %d AND `notify_new_posts` LIMIT 1",
1613 intval($arr['contact-id']),
1616 $send_notification = count($r);
1618 if (!$send_notification) {
1619 $tags = q("SELECT `url` FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` = %d AND `uid` = %d",
1620 intval(TERM_OBJ_POST), intval($current_post), intval(TERM_MENTION), intval($arr['uid']));
1623 foreach ($tags AS $tag) {
1624 $r = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d AND `notify_new_posts`",
1625 normalise_link($tag["url"]), intval($arr['uid']));
1627 $send_notification = true;
1632 if ($send_notification) {
1633 logger('item_store: Send notification for contact '.$arr['contact-id'].' and post '.$current_post, LOGGER_DEBUG);
1634 $u = q("SELECT * FROM user WHERE uid = %d LIMIT 1",
1635 intval($arr['uid']));
1637 $item = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d",
1638 intval($current_post),
1644 require_once('include/enotify.php');
1646 'type' => NOTIFY_SHARE,
1647 'notify_flags' => $u[0]['notify-flags'],
1648 'language' => $u[0]['language'],
1649 'to_name' => $u[0]['username'],
1650 'to_email' => $u[0]['email'],
1651 'uid' => $u[0]['uid'],
1653 'link' => $a->get_baseurl().'/display/'.urlencode($arr['guid']),
1654 'source_name' => $item[0]['author-name'],
1655 'source_link' => $item[0]['author-link'],
1656 'source_photo' => $item[0]['author-avatar'],
1657 'verb' => ACTIVITY_TAG,
1659 'parent' => $arr['parent']
1661 logger('item_store: Notification sent for contact '.$arr['contact-id'].' and post '.$current_post, LOGGER_DEBUG);
1664 update_thread($parent_id);
1665 add_shadow_entry($arr);
1669 proc_run('php', "include/notifier.php", $notify_type, $current_post);
1671 return $current_post;
1674 function item_body_set_hashtags(&$item) {
1676 $tags = get_tags($item["body"]);
1682 // This sorting is important when there are hashtags that are part of other hashtags
1683 // Otherwise there could be problems with hashtags like #test and #test2
1688 $URLSearchString = "^\[\]";
1690 // All hashtags should point to the home server
1691 //$item["body"] = preg_replace("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
1692 // "#[url=".$a->get_baseurl()."/search?tag=$2]$2[/url]", $item["body"]);
1694 //$item["tag"] = preg_replace("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
1695 // "#[url=".$a->get_baseurl()."/search?tag=$2]$2[/url]", $item["tag"]);
1697 // mask hashtags inside of url, bookmarks and attachments to avoid urls in urls
1698 $item["body"] = preg_replace_callback("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
1700 return("[url=".str_replace("#", "#", $match[1])."]".str_replace("#", "#", $match[2])."[/url]");
1703 $item["body"] = preg_replace_callback("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism",
1705 return("[bookmark=".str_replace("#", "#", $match[1])."]".str_replace("#", "#", $match[2])."[/bookmark]");
1708 $item["body"] = preg_replace_callback("/\[attachment (.*)\](.*?)\[\/attachment\]/ism",
1710 return("[attachment ".str_replace("#", "#", $match[1])."]".$match[2]."[/attachment]");
1713 // Repair recursive urls
1714 $item["body"] = preg_replace("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
1715 "#$2", $item["body"]);
1718 foreach($tags as $tag) {
1719 if(strpos($tag,'#') !== 0)
1722 if(strpos($tag,'[url='))
1725 $basetag = str_replace('_',' ',substr($tag,1));
1727 $newtag = '#[url='.$a->get_baseurl().'/search?tag='.rawurlencode($basetag).']'.$basetag.'[/url]';
1729 $item["body"] = str_replace($tag, $newtag, $item["body"]);
1731 if(!stristr($item["tag"],"/search?tag=".$basetag."]".$basetag."[/url]")) {
1732 if(strlen($item["tag"]))
1733 $item["tag"] = ','.$item["tag"];
1734 $item["tag"] = $newtag.$item["tag"];
1738 // Convert back the masked hashtags
1739 $item["body"] = str_replace("#", "#", $item["body"]);
1742 function get_item_guid($id) {
1743 $r = q("SELECT `guid` FROM `item` WHERE `id` = %d LIMIT 1", intval($id));
1745 return($r[0]["guid"]);
1750 function get_item_id($guid, $uid = 0) {
1756 $uid == local_user();
1758 // Does the given user have this item?
1760 $r = q("SELECT `item`.`id`, `user`.`nickname` FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
1761 WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
1762 AND `item`.`guid` = '%s' AND `item`.`uid` = %d", dbesc($guid), intval($uid));
1765 $nick = $r[0]["nickname"];
1769 // Or is it anywhere on the server?
1771 $r = q("SELECT `item`.`id`, `user`.`nickname` FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
1772 WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
1773 AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
1774 AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
1775 AND `item`.`private` = 0 AND `item`.`wall` = 1
1776 AND `item`.`guid` = '%s'", dbesc($guid));
1779 $nick = $r[0]["nickname"];
1782 return(array("nick" => $nick, "id" => $id));
1786 function get_item_contact($item,$contacts) {
1787 if(! count($contacts) || (! is_array($item)))
1789 foreach($contacts as $contact) {
1790 if($contact['id'] == $item['contact-id']) {
1792 break; // NOTREACHED
1799 * look for mention tags and setup a second delivery chain for forum/community posts if appropriate
1801 * @param int $item_id
1802 * @return bool true if item was deleted, else false
1804 function tag_deliver($uid,$item_id) {
1812 $u = q("select * from user where uid = %d limit 1",
1818 $community_page = (($u[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
1819 $prvgroup = (($u[0]['page-flags'] == PAGE_PRVGROUP) ? true : false);
1822 $i = q("select * from item where id = %d and uid = %d limit 1",
1831 $link = normalise_link($a->get_baseurl() . '/profile/' . $u[0]['nickname']);
1833 // Diaspora uses their own hardwired link URL in @-tags
1834 // instead of the one we supply with webfinger
1836 $dlink = normalise_link($a->get_baseurl() . '/u/' . $u[0]['nickname']);
1838 $cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism',$item['body'],$matches,PREG_SET_ORDER);
1840 foreach($matches as $mtch) {
1841 if(link_compare($link,$mtch[1]) || link_compare($dlink,$mtch[1])) {
1843 logger('tag_deliver: mention found: ' . $mtch[2]);
1849 if ( ($community_page || $prvgroup) &&
1850 (!$item['wall']) && (!$item['origin']) && ($item['id'] == $item['parent'])){
1851 // mmh.. no mention.. community page or private group... no wall.. no origin.. top-post (not a comment)
1853 logger("tag_deliver: no-mention top-level post to communuty or private group. delete.");
1854 q("DELETE FROM item WHERE id = %d and uid = %d",
1864 // send a notification
1866 // use a local photo if we have one
1868 $r = q("select * from contact where uid = %d and nurl = '%s' limit 1",
1869 intval($u[0]['uid']),
1870 dbesc(normalise_link($item['author-link']))
1872 $photo = (($r && count($r)) ? $r[0]['thumb'] : $item['author-avatar']);
1875 require_once('include/enotify.php');
1877 'type' => NOTIFY_TAGSELF,
1878 'notify_flags' => $u[0]['notify-flags'],
1879 'language' => $u[0]['language'],
1880 'to_name' => $u[0]['username'],
1881 'to_email' => $u[0]['email'],
1882 'uid' => $u[0]['uid'],
1884 'link' => $a->get_baseurl() . '/display/'.urlencode(get_item_guid($item['id'])),
1885 'source_name' => $item['author-name'],
1886 'source_link' => $item['author-link'],
1887 'source_photo' => $photo,
1888 'verb' => ACTIVITY_TAG,
1890 'parent' => $item['parent']
1894 $arr = array('item' => $item, 'user' => $u[0], 'contact' => $r[0]);
1896 call_hooks('tagged', $arr);
1898 if((! $community_page) && (! $prvgroup))
1902 // tgroup delivery - setup a second delivery chain
1903 // prevent delivery looping - only proceed
1904 // if the message originated elsewhere and is a top-level post
1906 if(($item['wall']) || ($item['origin']) || ($item['id'] != $item['parent']))
1909 // now change this copy of the post to a forum head message and deliver to all the tgroup members
1912 $c = q("select name, url, thumb from contact where self = 1 and uid = %d limit 1",
1913 intval($u[0]['uid'])
1918 // also reset all the privacy bits to the forum default permissions
1920 $private = ($u[0]['allow_cid'] || $u[0]['allow_gid'] || $u[0]['deny_cid'] || $u[0]['deny_gid']) ? 1 : 0;
1922 $forum_mode = (($prvgroup) ? 2 : 1);
1924 q("update item set wall = 1, origin = 1, forum_mode = %d, `owner-name` = '%s', `owner-link` = '%s', `owner-avatar` = '%s',
1925 `private` = %d, `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s' where id = %d",
1926 intval($forum_mode),
1927 dbesc($c[0]['name']),
1928 dbesc($c[0]['url']),
1929 dbesc($c[0]['thumb']),
1931 dbesc($u[0]['allow_cid']),
1932 dbesc($u[0]['allow_gid']),
1933 dbesc($u[0]['deny_cid']),
1934 dbesc($u[0]['deny_gid']),
1937 update_thread($item_id);
1939 proc_run('php','include/notifier.php','tgroup',$item_id);
1945 function tgroup_check($uid,$item) {
1951 // check that the message originated elsewhere and is a top-level post
1953 if(($item['wall']) || ($item['origin']) || ($item['uri'] != $item['parent-uri']))
1957 $u = q("select * from user where uid = %d limit 1",
1963 $community_page = (($u[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
1964 $prvgroup = (($u[0]['page-flags'] == PAGE_PRVGROUP) ? true : false);
1967 $link = normalise_link($a->get_baseurl() . '/profile/' . $u[0]['nickname']);
1969 // Diaspora uses their own hardwired link URL in @-tags
1970 // instead of the one we supply with webfinger
1972 $dlink = normalise_link($a->get_baseurl() . '/u/' . $u[0]['nickname']);
1974 $cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism',$item['body'],$matches,PREG_SET_ORDER);
1976 foreach($matches as $mtch) {
1977 if(link_compare($link,$mtch[1]) || link_compare($dlink,$mtch[1])) {
1979 logger('tgroup_check: mention found: ' . $mtch[2]);
1987 if((! $community_page) && (! $prvgroup))
2001 function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
2005 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
2007 if($contact['duplex'] && $contact['dfrn-id'])
2008 $idtosend = '0:' . $orig_id;
2009 if($contact['duplex'] && $contact['issued-id'])
2010 $idtosend = '1:' . $orig_id;
2012 $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
2014 $rino_enable = get_config('system','rino_encrypt');
2019 $ssl_val = intval(get_config('system','ssl_policy'));
2023 case SSL_POLICY_FULL:
2024 $ssl_policy = 'full';
2026 case SSL_POLICY_SELFSIGN:
2027 $ssl_policy = 'self';
2029 case SSL_POLICY_NONE:
2031 $ssl_policy = 'none';
2035 $url = $contact['notify'] . '&dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino=1' : '');
2037 logger('dfrn_deliver: ' . $url);
2039 $xml = fetch_url($url);
2041 $curl_stat = $a->get_curl_code();
2043 return(-1); // timed out
2045 logger('dfrn_deliver: ' . $xml, LOGGER_DATA);
2050 if(strpos($xml,'<?xml') === false) {
2051 logger('dfrn_deliver: no valid XML returned');
2052 logger('dfrn_deliver: returned XML: ' . $xml, LOGGER_DATA);
2056 $res = parse_xml_string($xml);
2058 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
2059 return (($res->status) ? $res->status : 3);
2061 $postvars = array();
2062 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
2063 $challenge = hex2bin((string) $res->challenge);
2064 $perm = (($res->perm) ? $res->perm : null);
2065 $dfrn_version = (float) (($res->dfrn_version) ? $res->dfrn_version : 2.0);
2066 $rino_allowed = ((intval($res->rino) === 1) ? 1 : 0);
2067 $page = (($owner['page-flags'] == PAGE_COMMUNITY) ? 1 : 0);
2069 if($owner['page-flags'] == PAGE_PRVGROUP)
2072 $final_dfrn_id = '';
2075 if((($perm == 'rw') && (! intval($contact['writable'])))
2076 || (($perm == 'r') && (intval($contact['writable'])))) {
2077 q("update contact set writable = %d where id = %d",
2078 intval(($perm == 'rw') ? 1 : 0),
2079 intval($contact['id'])
2081 $contact['writable'] = (string) 1 - intval($contact['writable']);
2085 if(($contact['duplex'] && strlen($contact['pubkey']))
2086 || ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
2087 || ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey']))) {
2088 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
2089 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
2092 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
2093 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
2096 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
2098 if(strpos($final_dfrn_id,':') == 1)
2099 $final_dfrn_id = substr($final_dfrn_id,2);
2101 if($final_dfrn_id != $orig_id) {
2102 logger('dfrn_deliver: wrong dfrn_id.');
2103 // did not decode properly - cannot trust this site
2107 $postvars['dfrn_id'] = $idtosend;
2108 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
2110 $postvars['dissolve'] = '1';
2113 if((($contact['rel']) && ($contact['rel'] != CONTACT_IS_SHARING) && (! $contact['blocked'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
2114 $postvars['data'] = $atom;
2115 $postvars['perm'] = 'rw';
2118 $postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
2119 $postvars['perm'] = 'r';
2122 $postvars['ssl_policy'] = $ssl_policy;
2125 $postvars['page'] = $page;
2127 if($rino && $rino_allowed && (! $dissolve)) {
2128 $key = substr(random_string(),0,16);
2129 $data = bin2hex(aes_encrypt($postvars['data'],$key));
2130 $postvars['data'] = $data;
2131 logger('rino: sent key = ' . $key, LOGGER_DEBUG);
2134 if($dfrn_version >= 2.1) {
2135 if(($contact['duplex'] && strlen($contact['pubkey']))
2136 || ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
2137 || ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey']))) {
2139 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
2142 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
2146 if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
2147 openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
2150 openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
2154 logger('md5 rawkey ' . md5($postvars['key']));
2156 $postvars['key'] = bin2hex($postvars['key']);
2159 logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);
2161 $xml = post_url($contact['notify'],$postvars);
2163 logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
2165 $curl_stat = $a->get_curl_code();
2166 if((! $curl_stat) || (! strlen($xml)))
2167 return(-1); // timed out
2169 if(($curl_stat == 503) && (stristr($a->get_curl_headers(),'retry-after')))
2172 if(strpos($xml,'<?xml') === false) {
2173 logger('dfrn_deliver: phase 2: no valid XML returned');
2174 logger('dfrn_deliver: phase 2: returned XML: ' . $xml, LOGGER_DATA);
2178 if($contact['term-date'] != '0000-00-00 00:00:00') {
2179 logger("dfrn_deliver: $url back from the dead - removing mark for death");
2180 require_once('include/Contact.php');
2181 unmark_for_death($contact);
2184 $res = parse_xml_string($xml);
2186 return $res->status;
2191 This function returns true if $update has an edited timestamp newer
2192 than $existing, i.e. $update contains new data which should override
2193 what's already there. If there is no timestamp yet, the update is
2194 assumed to be newer. If the update has no timestamp, the existing
2195 item is assumed to be up-to-date. If the timestamps are equal it
2196 assumes the update has been seen before and should be ignored.
2198 function edited_timestamp_is_newer($existing, $update) {
2199 if (!x($existing,'edited') || !$existing['edited']) {
2202 if (!x($update,'edited') || !$update['edited']) {
2205 $existing_edited = datetime_convert('UTC', 'UTC', $existing['edited']);
2206 $update_edited = datetime_convert('UTC', 'UTC', $update['edited']);
2207 return (strcmp($existing_edited, $update_edited) < 0);
2212 * consume_feed - process atom feed and update anything/everything we might need to update
2214 * $xml = the (atom) feed to consume - RSS isn't as fully supported but may work for simple feeds.
2216 * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
2217 * It is this person's stuff that is going to be updated.
2218 * $contact = the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
2219 * from an external network and MAY create an appropriate contact record. Otherwise, we MUST
2220 * have a contact record.
2221 * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or
2222 * might not) try and subscribe to it.
2223 * $datedir sorts in reverse order
2224 * $pass - by default ($pass = 0) we cannot guarantee that a parent item has been
2225 * imported prior to its children being seen in the stream unless we are certain
2226 * of how the feed is arranged/ordered.
2227 * With $pass = 1, we only pull parent items out of the stream.
2228 * With $pass = 2, we only pull children (comments/likes).
2230 * So running this twice, first with pass 1 and then with pass 2 will do the right
2231 * thing regardless of feed ordering. This won't be adequate in a fully-threaded
2232 * model where comments can have sub-threads. That would require some massive sorting
2233 * to get all the feed items into a mostly linear ordering, and might still require
2237 function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0) {
2239 require_once('library/simplepie/simplepie.inc');
2240 require_once('include/contact_selectors.php');
2242 if(! strlen($xml)) {
2243 logger('consume_feed: empty input');
2247 // Test - remove before flight
2248 // if ($contact['network'] === NETWORK_OSTATUS) {
2249 // $tempfile = tempnam(get_temppath(), "ostatus");
2250 // file_put_contents($tempfile, $xml);
2253 $feed = new SimplePie();
2254 $feed->set_raw_data($xml);
2256 $feed->enable_order_by_date(true);
2258 $feed->enable_order_by_date(false);
2262 logger('consume_feed: Error parsing XML: ' . $feed->error());
2264 $permalink = $feed->get_permalink();
2266 // Check at the feed level for updated contact name and/or photo
2270 $photo_timestamp = '';
2273 $contact_updated = '';
2275 $hubs = $feed->get_links('hub');
2276 logger('consume_feed: hubs: ' . print_r($hubs,true), LOGGER_DATA);
2279 $hub = implode(',', $hubs);
2281 $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'owner');
2283 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
2285 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
2286 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
2287 $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
2288 $new_name = $elems['name'][0]['data'];
2290 // Manually checking for changed contact names
2291 if (($new_name != $contact['name']) AND ($new_name != "") AND ($name_updated <= $contact['name-date'])) {
2292 $name_updated = date("c");
2293 $photo_timestamp = date("c");
2296 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
2297 if ($photo_timestamp == "")
2298 $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
2299 $photo_url = $elems['link'][0]['attribs']['']['href'];
2302 if((x($rawtags[0]['child'], NAMESPACE_DFRN)) && (x($rawtags[0]['child'][NAMESPACE_DFRN],'birthday'))) {
2303 $birthday = datetime_convert('UTC','UTC', $rawtags[0]['child'][NAMESPACE_DFRN]['birthday'][0]['data']);
2307 if((is_array($contact)) && ($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
2308 logger('consume_feed: Updating photo for '.$contact['name'].' from '.$photo_url.' uid: '.$contact['uid']);
2310 $contact_updated = $photo_timestamp;
2312 require_once("include/Photo.php");
2313 $photo_failure = false;
2314 $have_photo = false;
2316 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
2317 intval($contact['id']),
2318 intval($contact['uid'])
2321 $resource_id = $r[0]['resource-id'];
2325 $resource_id = photo_new_resource();
2328 $img_str = fetch_url($photo_url,true);
2329 // guess mimetype from headers or filename
2330 $type = guess_image_type($photo_url,true);
2333 $img = new Photo($img_str, $type);
2334 if($img->is_valid()) {
2336 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
2337 dbesc($resource_id),
2338 intval($contact['id']),
2339 intval($contact['uid'])
2343 $img->scaleImageSquare(175);
2345 $hash = $resource_id;
2346 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 4);
2348 $img->scaleImage(80);
2349 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 5);
2351 $img->scaleImage(48);
2352 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 6);
2356 q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
2357 WHERE `uid` = %d AND `id` = %d",
2358 dbesc(datetime_convert()),
2359 dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.'.$img->getExt()),
2360 dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.'.$img->getExt()),
2361 dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.'.$img->getExt()),
2362 intval($contact['uid']),
2363 intval($contact['id'])
2368 if((is_array($contact)) && ($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
2369 if ($name_updated > $contact_updated)
2370 $contact_updated = $name_updated;
2372 $r = q("select * from contact where uid = %d and id = %d limit 1",
2373 intval($contact['uid']),
2374 intval($contact['id'])
2377 $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d",
2378 dbesc(notags(trim($new_name))),
2379 dbesc(datetime_convert()),
2380 intval($contact['uid']),
2381 intval($contact['id'])
2384 // do our best to update the name on content items
2387 q("update item set `author-name` = '%s' where `author-name` = '%s' and `author-link` = '%s' and uid = %d",
2388 dbesc(notags(trim($new_name))),
2389 dbesc($r[0]['name']),
2390 dbesc($r[0]['url']),
2391 intval($contact['uid'])
2396 if ($contact_updated AND $new_name AND $photo_url)
2397 poco_check($contact['url'], $new_name, NETWORK_DFRN, $photo_url, "", "", "", "", "", $contact_updated, 2, $contact['id'], $contact['uid']);
2399 if(strlen($birthday)) {
2400 if(substr($birthday,0,4) != $contact['bdyear']) {
2401 logger('consume_feed: updating birthday: ' . $birthday);
2405 * Add new birthday event for this person
2407 * $bdtext is just a readable placeholder in case the event is shared
2408 * with others. We will replace it during presentation to our $importer
2409 * to contain a sparkle link and perhaps a photo.
2413 $bdtext = sprintf( t('%s\'s birthday'), $contact['name']);
2414 $bdtext2 = sprintf( t('Happy Birthday %s'), ' [url=' . $contact['url'] . ']' . $contact['name'] . '[/url]' ) ;
2417 $r = q("INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`summary`,`desc`,`type`)
2418 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
2419 intval($contact['uid']),
2420 intval($contact['id']),
2421 dbesc(datetime_convert()),
2422 dbesc(datetime_convert()),
2423 dbesc(datetime_convert('UTC','UTC', $birthday)),
2424 dbesc(datetime_convert('UTC','UTC', $birthday . ' + 1 day ')),
2433 q("UPDATE `contact` SET `bdyear` = '%s' WHERE `uid` = %d AND `id` = %d",
2434 dbesc(substr($birthday,0,4)),
2435 intval($contact['uid']),
2436 intval($contact['id'])
2439 // This function is called twice without reloading the contact
2440 // Make sure we only create one event. This is why &$contact
2441 // is a reference var in this function
2443 $contact['bdyear'] = substr($birthday,0,4);
2447 $community_page = 0;
2448 $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'community');
2450 $community_page = intval($rawtags[0]['data']);
2452 if(is_array($contact) && intval($contact['forum']) != $community_page) {
2453 q("update contact set forum = %d where id = %d",
2454 intval($community_page),
2455 intval($contact['id'])
2457 $contact['forum'] = (string) $community_page;
2461 // process any deleted entries
2463 $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
2464 if(is_array($del_entries) && count($del_entries) && $pass != 2) {
2465 foreach($del_entries as $dentry) {
2467 if(isset($dentry['attribs']['']['ref'])) {
2468 $uri = $dentry['attribs']['']['ref'];
2470 if(isset($dentry['attribs']['']['when'])) {
2471 $when = $dentry['attribs']['']['when'];
2472 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
2475 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
2477 if($deleted && is_array($contact)) {
2478 $r = q("SELECT `item`.*, `contact`.`self` FROM `item` INNER JOIN `contact` on `item`.`contact-id` = `contact`.`id`
2479 WHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d AND NOT `item`.`file` LIKE '%%[%%' LIMIT 1",
2481 intval($importer['uid']),
2482 intval($contact['id'])
2487 if(! $item['deleted'])
2488 logger('consume_feed: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
2490 if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
2491 $xo = parse_xml_string($item['object'],false);
2492 $xt = parse_xml_string($item['target'],false);
2493 if($xt->type === ACTIVITY_OBJ_NOTE) {
2494 $i = q("select * from `item` where uri = '%s' and uid = %d limit 1",
2496 intval($importer['importer_uid'])
2500 // For tags, the owner cannot remove the tag on the author's copy of the post.
2502 $owner_remove = (($item['contact-id'] == $i[0]['contact-id']) ? true: false);
2503 $author_remove = (($item['origin'] && $item['self']) ? true : false);
2504 $author_copy = (($item['origin']) ? true : false);
2506 if($owner_remove && $author_copy)
2508 if($author_remove || $owner_remove) {
2509 $tags = explode(',',$i[0]['tag']);
2512 foreach($tags as $tag)
2513 if(trim($tag) !== trim($xo->body))
2514 $newtags[] = trim($tag);
2516 q("update item set tag = '%s' where id = %d",
2517 dbesc(implode(',',$newtags)),
2520 create_tags_from_item($i[0]['id']);
2526 if($item['uri'] == $item['parent-uri']) {
2527 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
2528 `body` = '', `title` = ''
2529 WHERE `parent-uri` = '%s' AND `uid` = %d",
2531 dbesc(datetime_convert()),
2532 dbesc($item['uri']),
2533 intval($importer['uid'])
2535 create_tags_from_itemuri($item['uri'], $importer['uid']);
2536 create_files_from_itemuri($item['uri'], $importer['uid']);
2537 update_thread_uri($item['uri'], $importer['uid']);
2540 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
2541 `body` = '', `title` = ''
2542 WHERE `uri` = '%s' AND `uid` = %d",
2544 dbesc(datetime_convert()),
2546 intval($importer['uid'])
2548 create_tags_from_itemuri($uri, $importer['uid']);
2549 create_files_from_itemuri($uri, $importer['uid']);
2550 if($item['last-child']) {
2551 // ensure that last-child is set in case the comment that had it just got wiped.
2552 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
2553 dbesc(datetime_convert()),
2554 dbesc($item['parent-uri']),
2555 intval($item['uid'])
2557 // who is the last child now?
2558 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `moderated` = 0 AND `uid` = %d
2559 ORDER BY `created` DESC LIMIT 1",
2560 dbesc($item['parent-uri']),
2561 intval($importer['uid'])
2564 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d",
2575 // Now process the feed
2577 if($feed->get_item_quantity()) {
2579 logger('consume_feed: feed item count = ' . $feed->get_item_quantity());
2581 // in inverse date order
2583 $items = array_reverse($feed->get_items());
2585 $items = $feed->get_items();
2588 foreach($items as $item) {
2591 $item_id = $item->get_id();
2592 $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
2593 if(isset($rawthread[0]['attribs']['']['ref'])) {
2595 $parent_uri = $rawthread[0]['attribs']['']['ref'];
2598 if(($is_reply) && is_array($contact)) {
2603 // not allowed to post
2605 if($contact['rel'] == CONTACT_IS_FOLLOWER)
2609 // Have we seen it? If not, import it.
2611 $item_id = $item->get_id();
2612 $datarray = get_atom_elements($feed, $item, $contact);
2614 if((! x($datarray,'author-name')) && ($contact['network'] != NETWORK_DFRN))
2615 $datarray['author-name'] = $contact['name'];
2616 if((! x($datarray,'author-link')) && ($contact['network'] != NETWORK_DFRN))
2617 $datarray['author-link'] = $contact['url'];
2618 if((! x($datarray,'author-avatar')) && ($contact['network'] != NETWORK_DFRN))
2619 $datarray['author-avatar'] = $contact['thumb'];
2621 if((! x($datarray,'author-name')) || (! x($datarray,'author-link'))) {
2622 logger('consume_feed: no author information! ' . print_r($datarray,true));
2626 $force_parent = false;
2627 if($contact['network'] === NETWORK_OSTATUS || stristr($contact['url'],'twitter.com')) {
2628 if($contact['network'] === NETWORK_OSTATUS)
2629 $force_parent = true;
2630 if(strlen($datarray['title']))
2631 unset($datarray['title']);
2632 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
2633 dbesc(datetime_convert()),
2635 intval($importer['uid'])
2637 $datarray['last-child'] = 1;
2638 update_thread_uri($parent_uri, $importer['uid']);
2642 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2644 intval($importer['uid'])
2647 // Update content if 'updated' changes
2650 if (edited_timestamp_is_newer($r[0], $datarray)) {
2652 // do not accept (ignore) an earlier edit than one we currently have.
2653 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
2656 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
2657 dbesc($datarray['title']),
2658 dbesc($datarray['body']),
2659 dbesc($datarray['tag']),
2660 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
2661 dbesc(datetime_convert()),
2663 intval($importer['uid'])
2665 create_tags_from_itemuri($item_id, $importer['uid']);
2666 update_thread_uri($item_id, $importer['uid']);
2669 // update last-child if it changes
2671 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
2672 if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
2673 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
2674 dbesc(datetime_convert()),
2676 intval($importer['uid'])
2678 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
2679 intval($allow[0]['data']),
2680 dbesc(datetime_convert()),
2682 intval($importer['uid'])
2684 update_thread_uri($item_id, $importer['uid']);
2690 if(($contact['network'] === NETWORK_FEED) || (! strlen($contact['notify']))) {
2691 // one way feed - no remote comment ability
2692 $datarray['last-child'] = 0;
2694 $datarray['parent-uri'] = $parent_uri;
2695 $datarray['uid'] = $importer['uid'];
2696 $datarray['contact-id'] = $contact['id'];
2697 if((activity_match($datarray['verb'],ACTIVITY_LIKE)) || (activity_match($datarray['verb'],ACTIVITY_DISLIKE))) {
2698 $datarray['type'] = 'activity';
2699 $datarray['gravity'] = GRAVITY_LIKE;
2700 // only one like or dislike per person
2701 // splitted into two queries for performance issues
2702 $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`parent-uri` = '%s') limit 1",
2703 intval($datarray['uid']),
2704 intval($datarray['contact-id']),
2705 dbesc($datarray['verb']),
2711 $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`thr-parent` = '%s') limit 1",
2712 intval($datarray['uid']),
2713 intval($datarray['contact-id']),
2714 dbesc($datarray['verb']),
2721 if(($datarray['verb'] === ACTIVITY_TAG) && ($datarray['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
2722 $xo = parse_xml_string($datarray['object'],false);
2723 $xt = parse_xml_string($datarray['target'],false);
2725 if($xt->type == ACTIVITY_OBJ_NOTE) {
2726 $r = q("select * from item where `uri` = '%s' AND `uid` = %d limit 1",
2728 intval($importer['importer_uid'])
2733 // extract tag, if not duplicate, add to parent item
2734 if($xo->id && $xo->content) {
2735 $newtag = '#[url=' . $xo->id . ']'. $xo->content . '[/url]';
2736 if(! (stristr($r[0]['tag'],$newtag))) {
2737 q("UPDATE item SET tag = '%s' WHERE id = %d",
2738 dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . $newtag),
2741 create_tags_from_item($r[0]['id']);
2747 $r = item_store($datarray,$force_parent);
2753 // Head post of a conversation. Have we seen it? If not, import it.
2755 $item_id = $item->get_id();
2757 $datarray = get_atom_elements($feed, $item, $contact);
2759 if(is_array($contact)) {
2760 if((! x($datarray,'author-name')) && ($contact['network'] != NETWORK_DFRN))
2761 $datarray['author-name'] = $contact['name'];
2762 if((! x($datarray,'author-link')) && ($contact['network'] != NETWORK_DFRN))
2763 $datarray['author-link'] = $contact['url'];
2764 if((! x($datarray,'author-avatar')) && ($contact['network'] != NETWORK_DFRN))
2765 $datarray['author-avatar'] = $contact['thumb'];
2768 if((! x($datarray,'author-name')) || (! x($datarray,'author-link'))) {
2769 logger('consume_feed: no author information! ' . print_r($datarray,true));
2773 // special handling for events
2775 if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
2776 $ev = bbtoevent($datarray['body']);
2777 if(x($ev,'desc') && x($ev,'start')) {
2778 $ev['uid'] = $importer['uid'];
2779 $ev['uri'] = $item_id;
2780 $ev['edited'] = $datarray['edited'];
2781 $ev['private'] = $datarray['private'];
2783 if(is_array($contact))
2784 $ev['cid'] = $contact['id'];
2785 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2787 intval($importer['uid'])
2790 $ev['id'] = $r[0]['id'];
2791 $xyz = event_store($ev);
2796 if($contact['network'] === NETWORK_OSTATUS || stristr($contact['url'],'twitter.com')) {
2797 if(strlen($datarray['title']))
2798 unset($datarray['title']);
2799 $datarray['last-child'] = 1;
2803 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
2805 intval($importer['uid'])
2808 // Update content if 'updated' changes
2811 if (edited_timestamp_is_newer($r[0], $datarray)) {
2813 // do not accept (ignore) an earlier edit than one we currently have.
2814 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
2817 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
2818 dbesc($datarray['title']),
2819 dbesc($datarray['body']),
2820 dbesc($datarray['tag']),
2821 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
2822 dbesc(datetime_convert()),
2824 intval($importer['uid'])
2826 create_tags_from_itemuri($item_id, $importer['uid']);
2827 update_thread_uri($item_id, $importer['uid']);
2830 // update last-child if it changes
2832 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
2833 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
2834 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
2835 intval($allow[0]['data']),
2836 dbesc(datetime_convert()),
2838 intval($importer['uid'])
2840 update_thread_uri($item_id, $importer['uid']);
2845 if(activity_match($datarray['verb'],ACTIVITY_FOLLOW)) {
2846 logger('consume-feed: New follower');
2847 new_follower($importer,$contact,$datarray,$item);
2850 if(activity_match($datarray['verb'],ACTIVITY_UNFOLLOW)) {
2851 lose_follower($importer,$contact,$datarray,$item);
2855 if(activity_match($datarray['verb'],ACTIVITY_REQ_FRIEND)) {
2856 logger('consume-feed: New friend request');
2857 new_follower($importer,$contact,$datarray,$item,true);
2860 if(activity_match($datarray['verb'],ACTIVITY_UNFRIEND)) {
2861 lose_sharer($importer,$contact,$datarray,$item);
2866 if(! is_array($contact))
2870 if(($contact['network'] === NETWORK_FEED) || (! strlen($contact['notify']))) {
2871 // one way feed - no remote comment ability
2872 $datarray['last-child'] = 0;
2874 if($contact['network'] === NETWORK_FEED)
2875 $datarray['private'] = 2;
2877 $datarray['parent-uri'] = $item_id;
2878 $datarray['uid'] = $importer['uid'];
2879 $datarray['contact-id'] = $contact['id'];
2881 if(! link_compare($datarray['owner-link'],$contact['url'])) {
2882 // The item owner info is not our contact. It's OK and is to be expected if this is a tgroup delivery,
2883 // but otherwise there's a possible data mixup on the sender's system.
2884 // the tgroup delivery code called from item_store will correct it if it's a forum,
2885 // but we're going to unconditionally correct it here so that the post will always be owned by our contact.
2886 logger('consume_feed: Correcting item owner.', LOGGER_DEBUG);
2887 $datarray['owner-name'] = $contact['name'];
2888 $datarray['owner-link'] = $contact['url'];
2889 $datarray['owner-avatar'] = $contact['thumb'];
2892 // We've allowed "followers" to reach this point so we can decide if they are
2893 // posting an @-tag delivery, which followers are allowed to do for certain
2894 // page types. Now that we've parsed the post, let's check if it is legit. Otherwise ignore it.
2896 if(($contact['rel'] == CONTACT_IS_FOLLOWER) && (! tgroup_check($importer['uid'],$datarray)))
2899 // This is my contact on another system, but it's really me.
2900 // Turn this into a wall post.
2901 $notify = item_is_remote_self($contact, $datarray);
2903 $r = item_store($datarray, false, $notify);
2904 logger('Stored - Contact '.$contact['url'].' Notify '.$notify.' return '.$r.' Item '.print_r($datarray, true), LOGGER_DEBUG);
2912 function item_is_remote_self($contact, &$datarray) {
2915 if (!$contact['remote_self'])
2918 // Prevent the forwarding of posts that are forwarded
2919 if ($datarray["extid"] == NETWORK_DFRN)
2922 // Prevent to forward already forwarded posts
2923 if ($datarray["app"] == $a->get_hostname())
2926 // Only forward posts
2927 if ($datarray["verb"] != ACTIVITY_POST)
2930 if (($contact['network'] != NETWORK_FEED) AND $datarray['private'])
2933 $datarray2 = $datarray;
2934 logger('remote-self start - Contact '.$contact['url'].' - '.$contact['remote_self'].' Item '.print_r($datarray, true), LOGGER_DEBUG);
2935 if ($contact['remote_self'] == 2) {
2936 $r = q("SELECT `id`,`url`,`name`,`thumb` FROM `contact` WHERE `uid` = %d AND `self`",
2937 intval($contact['uid']));
2939 $datarray['contact-id'] = $r[0]["id"];
2941 $datarray['owner-name'] = $r[0]["name"];
2942 $datarray['owner-link'] = $r[0]["url"];
2943 $datarray['owner-avatar'] = $r[0]["thumb"];
2945 $datarray['author-name'] = $datarray['owner-name'];
2946 $datarray['author-link'] = $datarray['owner-link'];
2947 $datarray['author-avatar'] = $datarray['owner-avatar'];
2950 if ($contact['network'] != NETWORK_FEED) {
2951 $datarray["guid"] = get_guid(32);
2952 unset($datarray["plink"]);
2953 $datarray["uri"] = item_new_uri($a->get_hostname(),$contact['uid']);
2954 $datarray["parent-uri"] = $datarray["uri"];
2955 $datarray["extid"] = $contact['network'];
2956 $urlpart = parse_url($datarray2['author-link']);
2957 $datarray["app"] = $urlpart["host"];
2959 $datarray['private'] = 0;
2962 //if (!isset($datarray["app"]) OR ($datarray["app"] == ""))
2963 // $datarray["app"] = network_to_name($contact['network']);
2965 if ($contact['network'] != NETWORK_FEED) {
2966 // Store the original post
2967 $r = item_store($datarray2, false, false);
2968 logger('remote-self post original item - Contact '.$contact['url'].' return '.$r.' Item '.print_r($datarray2, true), LOGGER_DEBUG);
2970 $datarray["app"] = "Feed";
2975 function local_delivery($importer,$data) {
2978 logger(__function__, LOGGER_TRACE);
2980 if($importer['readonly']) {
2981 // We aren't receiving stuff from this person. But we will quietly ignore them
2982 // rather than a blatant "go away" message.
2983 logger('local_delivery: ignoring');
2988 // Consume notification feed. This may differ from consuming a public feed in several ways
2989 // - might contain email or friend suggestions
2990 // - might contain remote followup to our message
2991 // - in which case we need to accept it and then notify other conversants
2992 // - we may need to send various email notifications
2994 $feed = new SimplePie();
2995 $feed->set_raw_data($data);
2996 $feed->enable_order_by_date(false);
3001 logger('local_delivery: Error parsing XML: ' . $feed->error());
3004 // Check at the feed level for updated contact name and/or photo
3008 $photo_timestamp = '';
3010 $contact_updated = '';
3013 $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'owner');
3015 // Fallback should not be needed here. If it isn't DFRN it won't have DFRN updated tags
3017 // $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
3020 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
3021 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
3022 $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
3023 $new_name = $elems['name'][0]['data'];
3025 // Manually checking for changed contact names
3026 if (($new_name != $importer['name']) AND ($new_name != "") AND ($name_updated <= $importer['name-date'])) {
3027 $name_updated = date("c");
3028 $photo_timestamp = date("c");
3031 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
3032 if ($photo_timestamp == "")
3033 $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
3034 $photo_url = $elems['link'][0]['attribs']['']['href'];
3038 if(($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $importer['avatar-date'])) {
3040 $contact_updated = $photo_timestamp;
3042 logger('local_delivery: Updating photo for ' . $importer['name']);
3043 require_once("include/Photo.php");
3044 $photo_failure = false;
3045 $have_photo = false;
3047 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
3048 intval($importer['id']),
3049 intval($importer['importer_uid'])
3052 $resource_id = $r[0]['resource-id'];
3056 $resource_id = photo_new_resource();
3059 $img_str = fetch_url($photo_url,true);
3060 // guess mimetype from headers or filename
3061 $type = guess_image_type($photo_url,true);
3064 $img = new Photo($img_str, $type);
3065 if($img->is_valid()) {
3067 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
3068 dbesc($resource_id),
3069 intval($importer['id']),
3070 intval($importer['importer_uid'])
3074 $img->scaleImageSquare(175);
3076 $hash = $resource_id;
3077 $r = $img->store($importer['importer_uid'], $importer['id'], $hash, basename($photo_url), 'Contact Photos', 4);
3079 $img->scaleImage(80);
3080 $r = $img->store($importer['importer_uid'], $importer['id'], $hash, basename($photo_url), 'Contact Photos', 5);
3082 $img->scaleImage(48);
3083 $r = $img->store($importer['importer_uid'], $importer['id'], $hash, basename($photo_url), 'Contact Photos', 6);
3087 q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
3088 WHERE `uid` = %d AND `id` = %d",
3089 dbesc(datetime_convert()),
3090 dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.'.$img->getExt()),
3091 dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.'.$img->getExt()),
3092 dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.'.$img->getExt()),
3093 intval($importer['importer_uid']),
3094 intval($importer['id'])
3099 if(($name_updated) && (strlen($new_name)) && ($name_updated > $importer['name-date'])) {
3100 if ($name_updated > $contact_updated)
3101 $contact_updated = $name_updated;
3103 $r = q("select * from contact where uid = %d and id = %d limit 1",
3104 intval($importer['importer_uid']),
3105 intval($importer['id'])
3108 $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d",
3109 dbesc(notags(trim($new_name))),
3110 dbesc(datetime_convert()),
3111 intval($importer['importer_uid']),
3112 intval($importer['id'])
3115 // do our best to update the name on content items
3118 q("update item set `author-name` = '%s' where `author-name` = '%s' and `author-link` = '%s' and uid = %d",
3119 dbesc(notags(trim($new_name))),
3120 dbesc($r[0]['name']),
3121 dbesc($r[0]['url']),
3122 intval($importer['importer_uid'])
3127 if ($contact_updated AND $new_name AND $photo_url)
3128 poco_check($importer['url'], $new_name, NETWORK_DFRN, $photo_url, "", "", "", "", "", $contact_updated, 2, $importer['id'], $importer['importer_uid']);
3130 // Currently unsupported - needs a lot of work
3131 $reloc = $feed->get_feed_tags( NAMESPACE_DFRN, 'relocate' );
3132 if(isset($reloc[0]['child'][NAMESPACE_DFRN])) {
3133 $base = $reloc[0]['child'][NAMESPACE_DFRN];
3135 $newloc['uid'] = $importer['importer_uid'];
3136 $newloc['cid'] = $importer['id'];
3137 $newloc['name'] = notags(unxmlify($base['name'][0]['data']));
3138 $newloc['photo'] = notags(unxmlify($base['photo'][0]['data']));
3139 $newloc['thumb'] = notags(unxmlify($base['thumb'][0]['data']));
3140 $newloc['micro'] = notags(unxmlify($base['micro'][0]['data']));
3141 $newloc['url'] = notags(unxmlify($base['url'][0]['data']));
3142 $newloc['request'] = notags(unxmlify($base['request'][0]['data']));
3143 $newloc['confirm'] = notags(unxmlify($base['confirm'][0]['data']));
3144 $newloc['notify'] = notags(unxmlify($base['notify'][0]['data']));
3145 $newloc['poll'] = notags(unxmlify($base['poll'][0]['data']));
3146 $newloc['sitepubkey'] = notags(unxmlify($base['sitepubkey'][0]['data']));
3147 /** relocated user must have original key pair */
3148 /*$newloc['pubkey'] = notags(unxmlify($base['pubkey'][0]['data']));
3149 $newloc['prvkey'] = notags(unxmlify($base['prvkey'][0]['data']));*/
3151 logger("items:relocate contact ".print_r($newloc, true).print_r($importer, true), LOGGER_DEBUG);
3154 $r = q("SELECT photo, url FROM contact WHERE id=%d AND uid=%d;",
3155 intval($importer['id']),
3156 intval($importer['importer_uid']));
3161 $x = q("UPDATE contact SET
3172 `site-pubkey` = '%s'
3173 WHERE id=%d AND uid=%d;",
3174 dbesc($newloc['name']),
3175 dbesc($newloc['photo']),
3176 dbesc($newloc['thumb']),
3177 dbesc($newloc['micro']),
3178 dbesc($newloc['url']),
3179 dbesc(normalise_link($newloc['url'])),
3180 dbesc($newloc['request']),
3181 dbesc($newloc['confirm']),
3182 dbesc($newloc['notify']),
3183 dbesc($newloc['poll']),
3184 dbesc($newloc['sitepubkey']),
3185 intval($importer['id']),
3186 intval($importer['importer_uid']));
3192 'owner-link' => array($old['url'], $newloc['url']),
3193 'author-link' => array($old['url'], $newloc['url']),
3194 'owner-avatar' => array($old['photo'], $newloc['photo']),
3195 'author-avatar' => array($old['photo'], $newloc['photo']),
3197 foreach ($fields as $n=>$f){
3198 $x = q("UPDATE `item` SET `%s`='%s' WHERE `%s`='%s' AND uid=%d",
3201 intval($importer['importer_uid']));
3207 // merge with current record, current contents have priority
3208 // update record, set url-updated
3209 // update profile photos
3215 // handle friend suggestion notification
3217 $sugg = $feed->get_feed_tags( NAMESPACE_DFRN, 'suggest' );
3218 if(isset($sugg[0]['child'][NAMESPACE_DFRN])) {
3219 $base = $sugg[0]['child'][NAMESPACE_DFRN];
3221 $fsugg['uid'] = $importer['importer_uid'];
3222 $fsugg['cid'] = $importer['id'];
3223 $fsugg['name'] = notags(unxmlify($base['name'][0]['data']));
3224 $fsugg['photo'] = notags(unxmlify($base['photo'][0]['data']));
3225 $fsugg['url'] = notags(unxmlify($base['url'][0]['data']));
3226 $fsugg['request'] = notags(unxmlify($base['request'][0]['data']));
3227 $fsugg['body'] = escape_tags(unxmlify($base['note'][0]['data']));
3229 // Does our member already have a friend matching this description?
3231 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `nurl` = '%s' AND `uid` = %d LIMIT 1",
3232 dbesc($fsugg['name']),
3233 dbesc(normalise_link($fsugg['url'])),
3234 intval($fsugg['uid'])
3239 // Do we already have an fcontact record for this person?
3242 $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
3243 dbesc($fsugg['url']),
3244 dbesc($fsugg['name']),
3245 dbesc($fsugg['request'])
3250 // OK, we do. Do we already have an introduction for this person ?
3251 $r = q("select id from intro where uid = %d and fid = %d limit 1",
3252 intval($fsugg['uid']),
3259 $r = q("INSERT INTO `fcontact` ( `name`,`url`,`photo`,`request` ) VALUES ( '%s', '%s', '%s', '%s' ) ",
3260 dbesc($fsugg['name']),
3261 dbesc($fsugg['url']),
3262 dbesc($fsugg['photo']),
3263 dbesc($fsugg['request'])
3265 $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
3266 dbesc($fsugg['url']),
3267 dbesc($fsugg['name']),
3268 dbesc($fsugg['request'])
3273 // database record did not get created. Quietly give up.
3278 $hash = random_string();
3280 $r = q("INSERT INTO `intro` ( `uid`, `fid`, `contact-id`, `note`, `hash`, `datetime`, `blocked` )
3281 VALUES( %d, %d, %d, '%s', '%s', '%s', %d )",
3282 intval($fsugg['uid']),
3284 intval($fsugg['cid']),
3285 dbesc($fsugg['body']),
3287 dbesc(datetime_convert()),
3292 'type' => NOTIFY_SUGGEST,
3293 'notify_flags' => $importer['notify-flags'],
3294 'language' => $importer['language'],
3295 'to_name' => $importer['username'],
3296 'to_email' => $importer['email'],
3297 'uid' => $importer['importer_uid'],
3299 'link' => $a->get_baseurl() . '/notifications/intros',
3300 'source_name' => $importer['name'],
3301 'source_link' => $importer['url'],
3302 'source_photo' => $importer['photo'],
3303 'verb' => ACTIVITY_REQ_FRIEND,
3312 $rawmail = $feed->get_feed_tags( NAMESPACE_DFRN, 'mail' );
3313 if(isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
3315 logger('local_delivery: private message received');
3318 $base = $rawmail[0]['child'][NAMESPACE_DFRN];
3321 $msg['uid'] = $importer['importer_uid'];
3322 $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
3323 $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
3324 $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
3325 $msg['contact-id'] = $importer['id'];
3326 $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
3327 $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
3329 $msg['replied'] = 0;
3330 $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
3331 $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
3332 $msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
3336 $r = dbq("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg))
3337 . "`) VALUES ('" . implode("', '", array_values($msg)) . "')" );
3339 // send notifications.
3341 require_once('include/enotify.php');
3343 $notif_params = array(
3344 'type' => NOTIFY_MAIL,
3345 'notify_flags' => $importer['notify-flags'],
3346 'language' => $importer['language'],
3347 'to_name' => $importer['username'],
3348 'to_email' => $importer['email'],
3349 'uid' => $importer['importer_uid'],
3351 'source_name' => $msg['from-name'],
3352 'source_link' => $importer['url'],
3353 'source_photo' => $importer['thumb'],
3354 'verb' => ACTIVITY_POST,
3358 notification($notif_params);
3364 $community_page = 0;
3365 $rawtags = $feed->get_feed_tags( NAMESPACE_DFRN, 'community');
3367 $community_page = intval($rawtags[0]['data']);
3369 if(intval($importer['forum']) != $community_page) {
3370 q("update contact set forum = %d where id = %d",
3371 intval($community_page),
3372 intval($importer['id'])
3374 $importer['forum'] = (string) $community_page;
3377 logger('local_delivery: feed item count = ' . $feed->get_item_quantity());
3379 // process any deleted entries
3381 $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
3382 if(is_array($del_entries) && count($del_entries)) {
3383 foreach($del_entries as $dentry) {
3385 if(isset($dentry['attribs']['']['ref'])) {
3386 $uri = $dentry['attribs']['']['ref'];
3388 if(isset($dentry['attribs']['']['when'])) {
3389 $when = $dentry['attribs']['']['when'];
3390 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
3393 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
3397 // check for relayed deletes to our conversation
3400 $r = q("select * from item where uri = '%s' and uid = %d limit 1",
3402 intval($importer['importer_uid'])
3405 $parent_uri = $r[0]['parent-uri'];
3406 if($r[0]['id'] != $r[0]['parent'])
3413 if($importer['page-flags'] == PAGE_COMMUNITY || $importer['page-flags'] == PAGE_PRVGROUP ) {
3416 logger('local_delivery: possible community delete');
3419 $sql_extra = " and contact.self = 1 and item.wall = 1 ";
3421 // was the top-level post for this reply written by somebody on this site?
3422 // Specifically, the recipient?
3424 $is_a_remote_delete = false;
3426 // POSSIBLE CLEANUP --> Why select so many fields when only forum_mode and wall are used?
3427 $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, `item`.`forum_mode`,`item`.`origin`,`item`.`wall`,
3428 `contact`.`name`, `contact`.`url`, `contact`.`thumb` from `item`
3429 INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
3430 WHERE `item`.`uri` = '%s' AND (`item`.`parent-uri` = '%s' or `item`.`thr-parent` = '%s')
3431 AND `item`.`uid` = %d
3437 intval($importer['importer_uid'])
3440 $is_a_remote_delete = true;
3442 // Does this have the characteristics of a community or private group comment?
3443 // If it's a reply to a wall post on a community/prvgroup page it's a
3444 // valid community comment. Also forum_mode makes it valid for sure.
3445 // If neither, it's not.
3447 if($is_a_remote_delete && $community) {
3448 if((! $r[0]['forum_mode']) && (! $r[0]['wall'])) {
3449 $is_a_remote_delete = false;
3450 logger('local_delivery: not a community delete');
3454 if($is_a_remote_delete) {
3455 logger('local_delivery: received remote delete');
3459 $r = q("SELECT `item`.*, `contact`.`self` FROM `item` INNER JOIN contact on `item`.`contact-id` = `contact`.`id`
3460 WHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d AND NOT `item`.`file` LIKE '%%[%%' LIMIT 1",
3462 intval($importer['importer_uid']),
3463 intval($importer['id'])
3469 if($item['deleted'])
3472 logger('local_delivery: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
3474 if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
3475 $xo = parse_xml_string($item['object'],false);
3476 $xt = parse_xml_string($item['target'],false);
3478 if($xt->type === ACTIVITY_OBJ_NOTE) {
3479 $i = q("select * from `item` where uri = '%s' and uid = %d limit 1",
3481 intval($importer['importer_uid'])
3485 // For tags, the owner cannot remove the tag on the author's copy of the post.
3487 $owner_remove = (($item['contact-id'] == $i[0]['contact-id']) ? true: false);
3488 $author_remove = (($item['origin'] && $item['self']) ? true : false);
3489 $author_copy = (($item['origin']) ? true : false);
3491 if($owner_remove && $author_copy)
3493 if($author_remove || $owner_remove) {
3494 $tags = explode(',',$i[0]['tag']);
3497 foreach($tags as $tag)
3498 if(trim($tag) !== trim($xo->body))
3499 $newtags[] = trim($tag);
3501 q("update item set tag = '%s' where id = %d",
3502 dbesc(implode(',',$newtags)),
3505 create_tags_from_item($i[0]['id']);
3511 if($item['uri'] == $item['parent-uri']) {
3512 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
3513 `body` = '', `title` = ''
3514 WHERE `parent-uri` = '%s' AND `uid` = %d",
3516 dbesc(datetime_convert()),
3517 dbesc($item['uri']),
3518 intval($importer['importer_uid'])
3520 create_tags_from_itemuri($item['uri'], $importer['importer_uid']);
3521 create_files_from_itemuri($item['uri'], $importer['importer_uid']);
3522 update_thread_uri($item['uri'], $importer['importer_uid']);
3525 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
3526 `body` = '', `title` = ''
3527 WHERE `uri` = '%s' AND `uid` = %d",
3529 dbesc(datetime_convert()),
3531 intval($importer['importer_uid'])
3533 create_tags_from_itemuri($uri, $importer['importer_uid']);
3534 create_files_from_itemuri($uri, $importer['importer_uid']);
3535 update_thread_uri($uri, $importer['importer_uid']);
3536 if($item['last-child']) {
3537 // ensure that last-child is set in case the comment that had it just got wiped.
3538 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
3539 dbesc(datetime_convert()),
3540 dbesc($item['parent-uri']),
3541 intval($item['uid'])
3543 // who is the last child now?
3544 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
3545 ORDER BY `created` DESC LIMIT 1",
3546 dbesc($item['parent-uri']),
3547 intval($importer['importer_uid'])
3550 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d",
3555 // if this is a relayed delete, propagate it to other recipients
3557 if($is_a_remote_delete)
3558 proc_run('php',"include/notifier.php","drop",$item['id']);
3566 foreach($feed->get_items() as $item) {
3569 $item_id = $item->get_id();
3570 $rawthread = $item->get_item_tags( NAMESPACE_THREAD, 'in-reply-to');
3571 if(isset($rawthread[0]['attribs']['']['ref'])) {
3573 $parent_uri = $rawthread[0]['attribs']['']['ref'];
3579 if($importer['page-flags'] == PAGE_COMMUNITY || $importer['page-flags'] == PAGE_PRVGROUP ) {
3582 logger('local_delivery: possible community reply');
3585 $sql_extra = " and contact.self = 1 and item.wall = 1 ";
3587 // was the top-level post for this reply written by somebody on this site?
3588 // Specifically, the recipient?
3590 $is_a_remote_comment = false;
3591 $top_uri = $parent_uri;
3593 $r = q("select `item`.`parent-uri` from `item`
3594 WHERE `item`.`uri` = '%s'
3598 if($r && count($r)) {
3599 $top_uri = $r[0]['parent-uri'];
3601 // POSSIBLE CLEANUP --> Why select so many fields when only forum_mode and wall are used?
3602 $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, `item`.`forum_mode`,`item`.`origin`,`item`.`wall`,
3603 `contact`.`name`, `contact`.`url`, `contact`.`thumb` from `item`
3604 INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
3605 WHERE `item`.`uri` = '%s' AND (`item`.`parent-uri` = '%s' or `item`.`thr-parent` = '%s')
3606 AND `item`.`uid` = %d
3612 intval($importer['importer_uid'])
3615 $is_a_remote_comment = true;
3618 // Does this have the characteristics of a community or private group comment?
3619 // If it's a reply to a wall post on a community/prvgroup page it's a
3620 // valid community comment. Also forum_mode makes it valid for sure.
3621 // If neither, it's not.
3623 if($is_a_remote_comment && $community) {
3624 if((! $r[0]['forum_mode']) && (! $r[0]['wall'])) {
3625 $is_a_remote_comment = false;
3626 logger('local_delivery: not a community reply');
3630 if($is_a_remote_comment) {
3631 logger('local_delivery: received remote comment');
3633 // remote reply to our post. Import and then notify everybody else.
3635 $datarray = get_atom_elements($feed, $item);
3637 $r = q("SELECT `id`, `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3639 intval($importer['importer_uid'])
3642 // Update content if 'updated' changes
3646 if (edited_timestamp_is_newer($r[0], $datarray)) {
3648 // do not accept (ignore) an earlier edit than one we currently have.
3649 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
3652 logger('received updated comment' , LOGGER_DEBUG);
3653 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
3654 dbesc($datarray['title']),
3655 dbesc($datarray['body']),
3656 dbesc($datarray['tag']),
3657 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
3658 dbesc(datetime_convert()),
3660 intval($importer['importer_uid'])
3662 create_tags_from_itemuri($item_id, $importer['importer_uid']);
3664 proc_run('php',"include/notifier.php","comment-import",$iid);
3673 $own = q("select name,url,thumb from contact where uid = %d and self = 1 limit 1",
3674 intval($importer['importer_uid'])
3678 $datarray['type'] = 'remote-comment';
3679 $datarray['wall'] = 1;
3680 $datarray['parent-uri'] = $parent_uri;
3681 $datarray['uid'] = $importer['importer_uid'];
3682 $datarray['owner-name'] = $own[0]['name'];
3683 $datarray['owner-link'] = $own[0]['url'];
3684 $datarray['owner-avatar'] = $own[0]['thumb'];
3685 $datarray['contact-id'] = $importer['id'];
3687 if(($datarray['verb'] === ACTIVITY_LIKE) || ($datarray['verb'] === ACTIVITY_DISLIKE)) {
3689 $datarray['type'] = 'activity';
3690 $datarray['gravity'] = GRAVITY_LIKE;
3691 $datarray['last-child'] = 0;
3692 // only one like or dislike per person
3693 // splitted into two queries for performance issues
3694 $r = q("select id from item where uid = %d and `contact-id` = %d and verb = '%s' and (`parent-uri` = '%s') and deleted = 0 limit 1",
3695 intval($datarray['uid']),
3696 intval($datarray['contact-id']),
3697 dbesc($datarray['verb']),
3698 dbesc($datarray['parent-uri'])
3704 $r = q("select id from item where uid = %d and `contact-id` = %d and verb = '%s' and (`thr-parent` = '%s') and deleted = 0 limit 1",
3705 intval($datarray['uid']),
3706 intval($datarray['contact-id']),
3707 dbesc($datarray['verb']),
3708 dbesc($datarray['parent-uri'])
3715 if(($datarray['verb'] === ACTIVITY_TAG) && ($datarray['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
3717 $xo = parse_xml_string($datarray['object'],false);
3718 $xt = parse_xml_string($datarray['target'],false);
3720 if(($xt->type == ACTIVITY_OBJ_NOTE) && ($xt->id)) {
3722 // fetch the parent item
3724 $tagp = q("select * from item where uri = '%s' and uid = %d limit 1",
3726 intval($importer['importer_uid'])
3731 // extract tag, if not duplicate, and this user allows tags, add to parent item
3733 if($xo->id && $xo->content) {
3734 $newtag = '#[url=' . $xo->id . ']'. $xo->content . '[/url]';
3735 if(! (stristr($tagp[0]['tag'],$newtag))) {
3736 $i = q("SELECT `blocktags` FROM `user` where `uid` = %d LIMIT 1",
3737 intval($importer['importer_uid'])
3739 if(count($i) && ! intval($i[0]['blocktags'])) {
3740 q("UPDATE item SET tag = '%s', `edited` = '%s', `changed` = '%s' WHERE id = %d",
3741 dbesc($tagp[0]['tag'] . (strlen($tagp[0]['tag']) ? ',' : '') . $newtag),
3742 intval($tagp[0]['id']),
3743 dbesc(datetime_convert()),
3744 dbesc(datetime_convert())
3746 create_tags_from_item($tagp[0]['id']);
3754 $posted_id = item_store($datarray);
3759 $datarray["id"] = $posted_id;
3761 $r = q("SELECT `parent`, `parent-uri` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
3763 intval($importer['importer_uid'])
3766 $parent = $r[0]['parent'];
3767 $parent_uri = $r[0]['parent-uri'];
3771 $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
3772 dbesc(datetime_convert()),
3773 intval($importer['importer_uid']),
3774 intval($r[0]['parent'])
3777 $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d",
3778 dbesc(datetime_convert()),
3779 intval($importer['importer_uid']),
3784 if($posted_id && $parent) {
3786 proc_run('php',"include/notifier.php","comment-import","$posted_id");
3788 if((! $is_like) && (! $importer['self'])) {
3790 require_once('include/enotify.php');
3793 'type' => NOTIFY_COMMENT,
3794 'notify_flags' => $importer['notify-flags'],
3795 'language' => $importer['language'],
3796 'to_name' => $importer['username'],
3797 'to_email' => $importer['email'],
3798 'uid' => $importer['importer_uid'],
3799 'item' => $datarray,
3800 'link' => $a->get_baseurl().'/display/'.urlencode(get_item_guid($posted_id)),
3801 'source_name' => stripslashes($datarray['author-name']),
3802 'source_link' => $datarray['author-link'],
3803 'source_photo' => ((link_compare($datarray['author-link'],$importer['url']))
3804 ? $importer['thumb'] : $datarray['author-avatar']),
3805 'verb' => ACTIVITY_POST,
3807 'parent' => $parent,
3808 'parent_uri' => $parent_uri,
3820 // regular comment that is part of this total conversation. Have we seen it? If not, import it.
3822 $item_id = $item->get_id();
3823 $datarray = get_atom_elements($feed,$item);
3825 if($importer['rel'] == CONTACT_IS_FOLLOWER)
3828 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
3830 intval($importer['importer_uid'])
3833 // Update content if 'updated' changes
3836 if (edited_timestamp_is_newer($r[0], $datarray)) {
3838 // do not accept (ignore) an earlier edit than one we currently have.
3839 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
3842 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
3843 dbesc($datarray['title']),
3844 dbesc($datarray['body']),
3845 dbesc($datarray['tag']),
3846 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
3847 dbesc(datetime_convert()),
3849 intval($importer['importer_uid'])
3851 create_tags_from_itemuri($item_id, $importer['importer_uid']);
3854 // update last-child if it changes
3856 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
3857 if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) {
3858 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
3859 dbesc(datetime_convert()),
3861 intval($importer['importer_uid'])
3863 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
3864 intval($allow[0]['data']),
3865 dbesc(datetime_convert()),
3867 intval($importer['importer_uid'])
3873 $datarray['parent-uri'] = $parent_uri;
3874 $datarray['uid'] = $importer['importer_uid'];
3875 $datarray['contact-id'] = $importer['id'];
3876 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
3877 $datarray['type'] = 'activity';
3878 $datarray['gravity'] = GRAVITY_LIKE;
3879 // only one like or dislike per person
3880 // splitted into two queries for performance issues
3881 $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`parent-uri` = '%s') limit 1",
3882 intval($datarray['uid']),
3883 intval($datarray['contact-id']),
3884 dbesc($datarray['verb']),
3890 $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`thr-parent` = '%s') limit 1",
3891 intval($datarray['uid']),
3892 intval($datarray['contact-id']),
3893 dbesc($datarray['verb']),
3901 if(($datarray['verb'] === ACTIVITY_TAG) && ($datarray['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
3903 $xo = parse_xml_string($datarray['object'],false);
3904 $xt = parse_xml_string($datarray['target'],false);
3906 if($xt->type == ACTIVITY_OBJ_NOTE) {
3907 $r = q("select * from item where `uri` = '%s' AND `uid` = %d limit 1",
3909 intval($importer['importer_uid'])
3914 // extract tag, if not duplicate, add to parent item
3916 if(! (stristr($r[0]['tag'],trim($xo->content)))) {
3917 q("UPDATE item SET tag = '%s' WHERE id = %d",
3918 dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . '#[url=' . $xo->id . ']'. $xo->content . '[/url]'),
3921 create_tags_from_item($r[0]['id']);
3927 $posted_id = item_store($datarray);
3929 // find out if our user is involved in this conversation and wants to be notified.
3931 if(!x($datarray['type']) || $datarray['type'] != 'activity') {
3933 $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
3935 intval($importer['importer_uid'])
3938 if(count($myconv)) {
3939 $importer_url = $a->get_baseurl() . '/profile/' . $importer['nickname'];
3941 // first make sure this isn't our own post coming back to us from a wall-to-wall event
3942 if(! link_compare($datarray['author-link'],$importer_url)) {
3945 foreach($myconv as $conv) {
3947 // now if we find a match, it means we're in this conversation
3949 if(! link_compare($conv['author-link'],$importer_url))
3952 require_once('include/enotify.php');
3954 $conv_parent = $conv['parent'];
3957 'type' => NOTIFY_COMMENT,
3958 'notify_flags' => $importer['notify-flags'],
3959 'language' => $importer['language'],
3960 'to_name' => $importer['username'],
3961 'to_email' => $importer['email'],
3962 'uid' => $importer['importer_uid'],
3963 'item' => $datarray,
3964 'link' => $a->get_baseurl().'/display/'.urlencode(get_item_guid($posted_id)),
3965 'source_name' => stripslashes($datarray['author-name']),
3966 'source_link' => $datarray['author-link'],
3967 'source_photo' => ((link_compare($datarray['author-link'],$importer['url']))
3968 ? $importer['thumb'] : $datarray['author-avatar']),
3969 'verb' => ACTIVITY_POST,
3971 'parent' => $conv_parent,
3972 'parent_uri' => $parent_uri
3976 // only send one notification
3988 // Head post of a conversation. Have we seen it? If not, import it.
3991 $item_id = $item->get_id();
3992 $datarray = get_atom_elements($feed,$item);
3994 if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
3995 $ev = bbtoevent($datarray['body']);
3996 if(x($ev,'desc') && x($ev,'start')) {
3997 $ev['cid'] = $importer['id'];
3998 $ev['uid'] = $importer['uid'];
3999 $ev['uri'] = $item_id;
4000 $ev['edited'] = $datarray['edited'];
4001 $ev['private'] = $datarray['private'];
4003 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
4005 intval($importer['uid'])
4008 $ev['id'] = $r[0]['id'];
4009 $xyz = event_store($ev);
4014 $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
4016 intval($importer['importer_uid'])
4019 // Update content if 'updated' changes
4022 if (edited_timestamp_is_newer($r[0], $datarray)) {
4024 // do not accept (ignore) an earlier edit than one we currently have.
4025 if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
4028 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
4029 dbesc($datarray['title']),
4030 dbesc($datarray['body']),
4031 dbesc($datarray['tag']),
4032 dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
4033 dbesc(datetime_convert()),
4035 intval($importer['importer_uid'])
4037 create_tags_from_itemuri($item_id, $importer['importer_uid']);
4038 update_thread_uri($item_id, $importer['importer_uid']);
4041 // update last-child if it changes
4043 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
4044 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
4045 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
4046 intval($allow[0]['data']),
4047 dbesc(datetime_convert()),
4049 intval($importer['importer_uid'])
4055 $datarray['parent-uri'] = $item_id;
4056 $datarray['uid'] = $importer['importer_uid'];
4057 $datarray['contact-id'] = $importer['id'];
4060 if(! link_compare($datarray['owner-link'],$importer['url'])) {
4061 // The item owner info is not our contact. It's OK and is to be expected if this is a tgroup delivery,
4062 // but otherwise there's a possible data mixup on the sender's system.
4063 // the tgroup delivery code called from item_store will correct it if it's a forum,
4064 // but we're going to unconditionally correct it here so that the post will always be owned by our contact.
4065 logger('local_delivery: Correcting item owner.', LOGGER_DEBUG);
4066 $datarray['owner-name'] = $importer['senderName'];
4067 $datarray['owner-link'] = $importer['url'];
4068 $datarray['owner-avatar'] = $importer['thumb'];
4071 if(($importer['rel'] == CONTACT_IS_FOLLOWER) && (! tgroup_check($importer['importer_uid'],$datarray)))
4074 // This is my contact on another system, but it's really me.
4075 // Turn this into a wall post.
4076 $notify = item_is_remote_self($importer, $datarray);
4078 $posted_id = item_store($datarray, false, $notify);
4080 if(stristr($datarray['verb'],ACTIVITY_POKE)) {
4081 $verb = urldecode(substr($datarray['verb'],strpos($datarray['verb'],'#')+1));
4084 $xo = parse_xml_string($datarray['object'],false);
4086 if(($xo->type == ACTIVITY_OBJ_PERSON) && ($xo->id)) {
4088 // somebody was poked/prodded. Was it me?
4090 $links = parse_xml_string("<links>".unxmlify($xo->link)."</links>",false);
4092 foreach($links->link as $l) {
4093 $atts = $l->attributes();
4094 switch($atts['rel']) {
4096 $Blink = $atts['href'];
4102 if($Blink && link_compare($Blink,$a->get_baseurl() . '/profile/' . $importer['nickname'])) {
4104 // send a notification
4105 require_once('include/enotify.php');
4108 'type' => NOTIFY_POKE,
4109 'notify_flags' => $importer['notify-flags'],
4110 'language' => $importer['language'],
4111 'to_name' => $importer['username'],
4112 'to_email' => $importer['email'],
4113 'uid' => $importer['importer_uid'],
4114 'item' => $datarray,
4115 'link' => $a->get_baseurl().'/display/'.urlencode(get_item_guid($posted_id)),
4116 'source_name' => stripslashes($datarray['author-name']),
4117 'source_link' => $datarray['author-link'],
4118 'source_photo' => ((link_compare($datarray['author-link'],$importer['url']))
4119 ? $importer['thumb'] : $datarray['author-avatar']),
4120 'verb' => $datarray['verb'],
4121 'otype' => 'person',
4122 'activity' => $verb,
4123 'parent' => $datarray['parent']
4139 function new_follower($importer,$contact,$datarray,$item,$sharing = false) {
4140 $url = notags(trim($datarray['author-link']));
4141 $name = notags(trim($datarray['author-name']));
4142 $photo = notags(trim($datarray['author-avatar']));
4144 $rawtag = $item->get_item_tags(NAMESPACE_ACTIVITY,'actor');
4145 if($rawtag && $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'])
4146 $nick = $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'];
4148 if(is_array($contact)) {
4149 if(($contact['network'] == NETWORK_OSTATUS && $contact['rel'] == CONTACT_IS_SHARING)
4150 || ($sharing && $contact['rel'] == CONTACT_IS_FOLLOWER)) {
4151 $r = q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d",
4152 intval(CONTACT_IS_FRIEND),
4153 intval($contact['id']),
4154 intval($importer['uid'])
4157 // send email notification to owner?
4161 // create contact record
4163 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `name`, `nick`, `photo`, `network`, `rel`,
4164 `blocked`, `readonly`, `pending`, `writable` )
4165 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 1, 1 ) ",
4166 intval($importer['uid']),
4167 dbesc(datetime_convert()),
4169 dbesc(normalise_link($url)),
4173 dbesc(($sharing) ? NETWORK_ZOT : NETWORK_OSTATUS),
4174 intval(($sharing) ? CONTACT_IS_SHARING : CONTACT_IS_FOLLOWER)
4176 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `pending` = 1 LIMIT 1",
4177 intval($importer['uid']),
4181 $contact_record = $r[0];
4183 // create notification
4184 $hash = random_string();
4186 if(is_array($contact_record)) {
4187 $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `hash`, `datetime`)
4188 VALUES ( %d, %d, 0, 0, '%s', '%s' )",
4189 intval($importer['uid']),
4190 intval($contact_record['id']),
4192 dbesc(datetime_convert())
4196 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
4197 intval($importer['uid'])
4202 if(intval($r[0]['def_gid'])) {
4203 require_once('include/group.php');
4204 group_add_member($r[0]['uid'],'',$contact_record['id'],$r[0]['def_gid']);
4207 if(($r[0]['notify-flags'] & NOTIFY_INTRO) &&
4208 in_array($r[0]['page-flags'], array(PAGE_NORMAL, PAGE_SOAPBOX, PAGE_FREELOVE))) {
4211 'type' => NOTIFY_INTRO,
4212 'notify_flags' => $r[0]['notify-flags'],
4213 'language' => $r[0]['language'],
4214 'to_name' => $r[0]['username'],
4215 'to_email' => $r[0]['email'],
4216 'uid' => $r[0]['uid'],
4217 'link' => $a->get_baseurl() . '/notifications/intro',
4218 'source_name' => ((strlen(stripslashes($contact_record['name']))) ? stripslashes($contact_record['name']) : t('[Name Withheld]')),
4219 'source_link' => $contact_record['url'],
4220 'source_photo' => $contact_record['photo'],
4221 'verb' => ($sharing ? ACTIVITY_FRIEND : ACTIVITY_FOLLOW),
4230 function lose_follower($importer,$contact,$datarray,$item) {
4232 if(($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_SHARING)) {
4233 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d",
4234 intval(CONTACT_IS_SHARING),
4235 intval($contact['id'])
4239 contact_remove($contact['id']);
4243 function lose_sharer($importer,$contact,$datarray,$item) {
4245 if(($contact['rel'] == CONTACT_IS_FRIEND) || ($contact['rel'] == CONTACT_IS_FOLLOWER)) {
4246 q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d",
4247 intval(CONTACT_IS_FOLLOWER),
4248 intval($contact['id'])
4252 contact_remove($contact['id']);
4257 function subscribe_to_hub($url,$importer,$contact,$hubmode = 'subscribe') {
4261 if(is_array($importer)) {
4262 $r = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
4263 intval($importer['uid'])
4267 // Diaspora has different message-ids in feeds than they do
4268 // through the direct Diaspora protocol. If we try and use
4269 // the feed, we'll get duplicates. So don't.
4271 if((! count($r)) || $contact['network'] === NETWORK_DIASPORA)
4274 $push_url = get_config('system','url') . '/pubsub/' . $r[0]['nickname'] . '/' . $contact['id'];
4276 // Use a single verify token, even if multiple hubs
4278 $verify_token = ((strlen($contact['hub-verify'])) ? $contact['hub-verify'] : random_string());
4280 $params= 'hub.mode=' . $hubmode . '&hub.callback=' . urlencode($push_url) . '&hub.topic=' . urlencode($contact['poll']) . '&hub.verify=async&hub.verify_token=' . $verify_token;
4282 logger('subscribe_to_hub: ' . $hubmode . ' ' . $contact['name'] . ' to hub ' . $url . ' endpoint: ' . $push_url . ' with verifier ' . $verify_token);
4284 if(! strlen($contact['hub-verify'])) {
4285 $r = q("UPDATE `contact` SET `hub-verify` = '%s' WHERE `id` = %d",
4286 dbesc($verify_token),
4287 intval($contact['id'])
4291 post_url($url,$params);
4293 logger('subscribe_to_hub: returns: ' . $a->get_curl_code(), LOGGER_DEBUG);
4300 function atom_author($tag,$name,$uri,$h,$w,$photo) {
4304 $name = xmlify($name);
4305 $uri = xmlify($uri);
4308 $photo = xmlify($photo);
4312 $o .= "<name>$name</name>\r\n";
4313 $o .= "<uri>$uri</uri>\r\n";
4314 $o .= '<link rel="photo" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
4315 $o .= '<link rel="avatar" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
4317 call_hooks('atom_author', $o);
4319 $o .= "</$tag>\r\n";
4323 function atom_entry($item,$type,$author,$owner,$comment = false,$cid = 0) {
4327 if(! $item['parent'])
4330 if($item['deleted'])
4331 return '<at:deleted-entry ref="' . xmlify($item['uri']) . '" when="' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '" />' . "\r\n";
4334 if($item['allow_cid'] || $item['allow_gid'] || $item['deny_cid'] || $item['deny_gid'])
4335 $body = fix_private_photos($item['body'],$owner['uid'],$item,$cid);
4337 $body = $item['body'];
4340 $o = "\r\n\r\n<entry>\r\n";
4342 if(is_array($author))
4343 $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb']);
4345 $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']));
4346 if(strlen($item['owner-name']))
4347 $o .= atom_author('dfrn:owner',$item['owner-name'],$item['owner-link'],80,80,$item['owner-avatar']);
4349 if(($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
4350 $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
4351 $o .= '<thr:in-reply-to ref="' . xmlify($parent_item) . '" type="text/html" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['parent']) . '" />' . "\r\n";
4356 if ($item['title'] != "")
4357 $htmlbody = "[b]".$item['title']."[/b]\n\n".$htmlbody;
4359 $htmlbody = bbcode(bb_remove_share_information($htmlbody), false, false, 7);
4361 $o .= '<id>' . xmlify($item['uri']) . '</id>' . "\r\n";
4362 $o .= '<title>' . xmlify($item['title']) . '</title>' . "\r\n";
4363 $o .= '<published>' . xmlify(datetime_convert('UTC','UTC',$item['created'] . '+00:00',ATOM_TIME)) . '</published>' . "\r\n";
4364 $o .= '<updated>' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '</updated>' . "\r\n";
4365 $o .= '<dfrn:env>' . base64url_encode($body, true) . '</dfrn:env>' . "\r\n";
4366 $o .= '<content type="' . $type . '" >' . xmlify((($type === 'html') ? $htmlbody : $body)) . '</content>' . "\r\n";
4367 $o .= '<link rel="alternate" type="text/html" href="' . xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']) . '" />' . "\r\n";
4371 $o .= '<dfrn:comment-allow>' . intval($item['last-child']) . '</dfrn:comment-allow>' . "\r\n";
4373 if($item['location']) {
4374 $o .= '<dfrn:location>' . xmlify($item['location']) . '</dfrn:location>' . "\r\n";
4375 $o .= '<poco:address><poco:formatted>' . xmlify($item['location']) . '</poco:formatted></poco:address>' . "\r\n";
4379 $o .= '<georss:point>' . xmlify($item['coord']) . '</georss:point>' . "\r\n";
4381 if(($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid']))
4382 $o .= '<dfrn:private>' . (($item['private']) ? $item['private'] : 1) . '</dfrn:private>' . "\r\n";
4385 $o .= '<dfrn:extid>' . xmlify($item['extid']) . '</dfrn:extid>' . "\r\n";
4386 if($item['bookmark'])
4387 $o .= '<dfrn:bookmark>true</dfrn:bookmark>' . "\r\n";
4390 $o .= '<statusnet:notice_info local_id="' . $item['id'] . '" source="' . xmlify($item['app']) . '" ></statusnet:notice_info>' . "\r\n";
4393 $o .= '<dfrn:diaspora_guid>' . $item['guid'] . '</dfrn:diaspora_guid>' . "\r\n";
4395 if($item['signed_text']) {
4396 $sign = base64_encode(json_encode(array('signed_text' => $item['signed_text'],'signature' => $item['signature'],'signer' => $item['signer'])));
4397 $o .= '<dfrn:diaspora_signature>' . xmlify($sign) . '</dfrn:diaspora_signature>' . "\r\n";
4400 $verb = construct_verb($item);
4401 $o .= '<as:verb>' . xmlify($verb) . '</as:verb>' . "\r\n";
4402 $actobj = construct_activity_object($item);
4405 $actarg = construct_activity_target($item);
4409 $tags = item_getfeedtags($item);
4411 foreach($tags as $t) {
4412 $o .= '<category scheme="X-DFRN:' . xmlify($t[0]) . ':' . xmlify($t[1]) . '" term="' . xmlify($t[2]) . '" />' . "\r\n";
4416 $o .= item_getfeedattach($item);
4418 $mentioned = get_mentions($item);
4422 call_hooks('atom_entry', $o);
4424 $o .= '</entry>' . "\r\n";
4429 function fix_private_photos($s, $uid, $item = null, $cid = 0) {
4431 if(get_config('system','disable_embedded'))
4436 logger('fix_private_photos: check for photos', LOGGER_DEBUG);
4437 $site = substr($a->get_baseurl(),strpos($a->get_baseurl(),'://'));
4442 $img_start = strpos($orig_body, '[img');
4443 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
4444 $img_len = ($img_start !== false ? strpos(substr($orig_body, $img_start + $img_st_close + 1), '[/img]') : false);
4445 while( ($img_st_close !== false) && ($img_len !== false) ) {
4447 $img_st_close++; // make it point to AFTER the closing bracket
4448 $image = substr($orig_body, $img_start + $img_st_close, $img_len);
4450 logger('fix_private_photos: found photo ' . $image, LOGGER_DEBUG);
4453 if(stristr($image , $site . '/photo/')) {
4454 // Only embed locally hosted photos
4456 $i = basename($image);
4457 $i = str_replace(array('.jpg','.png','.gif'),array('','',''),$i);
4458 $x = strpos($i,'-');
4461 $res = substr($i,$x+1);
4462 $i = substr($i,0,$x);
4463 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d AND `uid` = %d",
4470 // Check to see if we should replace this photo link with an embedded image
4471 // 1. No need to do so if the photo is public
4472 // 2. If there's a contact-id provided, see if they're in the access list
4473 // for the photo. If so, embed it.
4474 // 3. Otherwise, if we have an item, see if the item permissions match the photo
4475 // permissions, regardless of order but first check to see if they're an exact
4476 // match to save some processing overhead.
4478 if(has_permissions($r[0])) {
4480 $recips = enumerate_permissions($r[0]);
4481 if(in_array($cid, $recips)) {
4486 if(compare_permissions($item,$r[0]))
4491 $data = $r[0]['data'];
4492 $type = $r[0]['type'];
4494 // If a custom width and height were specified, apply before embedding
4495 if(preg_match("/\[img\=([0-9]*)x([0-9]*)\]/is", substr($orig_body, $img_start, $img_st_close), $match)) {
4496 logger('fix_private_photos: scaling photo', LOGGER_DEBUG);
4498 $width = intval($match[1]);
4499 $height = intval($match[2]);
4501 $ph = new Photo($data, $type);
4502 if($ph->is_valid()) {
4503 $ph->scaleImage(max($width, $height));
4504 $data = $ph->imageString();
4505 $type = $ph->getType();
4509 logger('fix_private_photos: replacing photo', LOGGER_DEBUG);
4510 $image = 'data:' . $type . ';base64,' . base64_encode($data);
4511 logger('fix_private_photos: replaced: ' . $image, LOGGER_DATA);
4517 $new_body = $new_body . substr($orig_body, 0, $img_start + $img_st_close) . $image . '[/img]';
4518 $orig_body = substr($orig_body, $img_start + $img_st_close + $img_len + strlen('[/img]'));
4519 if($orig_body === false)
4522 $img_start = strpos($orig_body, '[img');
4523 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
4524 $img_len = ($img_start !== false ? strpos(substr($orig_body, $img_start + $img_st_close + 1), '[/img]') : false);
4527 $new_body = $new_body . $orig_body;
4533 function has_permissions($obj) {
4534 if(($obj['allow_cid'] != '') || ($obj['allow_gid'] != '') || ($obj['deny_cid'] != '') || ($obj['deny_gid'] != ''))
4539 function compare_permissions($obj1,$obj2) {
4540 // first part is easy. Check that these are exactly the same.
4541 if(($obj1['allow_cid'] == $obj2['allow_cid'])
4542 && ($obj1['allow_gid'] == $obj2['allow_gid'])
4543 && ($obj1['deny_cid'] == $obj2['deny_cid'])
4544 && ($obj1['deny_gid'] == $obj2['deny_gid']))
4547 // This is harder. Parse all the permissions and compare the resulting set.
4549 $recipients1 = enumerate_permissions($obj1);
4550 $recipients2 = enumerate_permissions($obj2);
4553 if($recipients1 == $recipients2)
4558 // returns an array of contact-ids that are allowed to see this object
4560 function enumerate_permissions($obj) {
4561 require_once('include/group.php');
4562 $allow_people = expand_acl($obj['allow_cid']);
4563 $allow_groups = expand_groups(expand_acl($obj['allow_gid']));
4564 $deny_people = expand_acl($obj['deny_cid']);
4565 $deny_groups = expand_groups(expand_acl($obj['deny_gid']));
4566 $recipients = array_unique(array_merge($allow_people,$allow_groups));
4567 $deny = array_unique(array_merge($deny_people,$deny_groups));
4568 $recipients = array_diff($recipients,$deny);
4572 function item_getfeedtags($item) {
4575 $cnt = preg_match_all('|\#\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
4577 for($x = 0; $x < $cnt; $x ++) {
4579 $ret[] = array('#',$matches[1][$x], $matches[2][$x]);
4583 $cnt = preg_match_all('|\@\[url\=(.*?)\](.*?)\[\/url\]|',$item['tag'],$matches);
4585 for($x = 0; $x < $cnt; $x ++) {
4587 $ret[] = array('@',$matches[1][$x], $matches[2][$x]);
4593 function item_getfeedattach($item) {
4595 $arr = explode('[/attach],',$item['attach']);
4597 foreach($arr as $r) {
4599 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|',$r,$matches);
4601 $ret .= '<link rel="enclosure" href="' . xmlify($matches[1]) . '" type="' . xmlify($matches[3]) . '" ';
4602 if(intval($matches[2]))
4603 $ret .= 'length="' . intval($matches[2]) . '" ';
4604 if($matches[4] !== ' ')
4605 $ret .= 'title="' . xmlify(trim($matches[4])) . '" ';
4606 $ret .= ' />' . "\r\n";
4615 function item_expire($uid, $days, $network = "", $force = false) {
4617 if((! $uid) || ($days < 1))
4620 // $expire_network_only = save your own wall posts
4621 // and just expire conversations started by others
4623 $expire_network_only = get_pconfig($uid,'expire','network_only');
4624 $sql_extra = ((intval($expire_network_only)) ? " AND wall = 0 " : "");
4626 if ($network != "") {
4627 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($network));
4628 // There is an index "uid_network_received" but not "uid_network_created"
4629 // This avoids the creation of another index just for one purpose.
4630 // And it doesn't really matter wether to look at "received" or "created"
4631 $range = "AND `received` < UTC_TIMESTAMP() - INTERVAL %d DAY ";
4633 $range = "AND `created` < UTC_TIMESTAMP() - INTERVAL %d DAY ";
4635 $r = q("SELECT * FROM `item`
4636 WHERE `uid` = %d $range
4647 $expire_items = get_pconfig($uid, 'expire','items');
4648 $expire_items = (($expire_items===false)?1:intval($expire_items)); // default if not set: 1
4650 // Forcing expiring of items - but not notes and marked items
4652 $expire_items = true;
4654 $expire_notes = get_pconfig($uid, 'expire','notes');
4655 $expire_notes = (($expire_notes===false)?1:intval($expire_notes)); // default if not set: 1
4657 $expire_starred = get_pconfig($uid, 'expire','starred');
4658 $expire_starred = (($expire_starred===false)?1:intval($expire_starred)); // default if not set: 1
4660 $expire_photos = get_pconfig($uid, 'expire','photos');
4661 $expire_photos = (($expire_photos===false)?0:intval($expire_photos)); // default if not set: 0
4663 logger('expire: # items=' . count($r). "; expire items: $expire_items, expire notes: $expire_notes, expire starred: $expire_starred, expire photos: $expire_photos");
4665 foreach($r as $item) {
4667 // don't expire filed items
4669 if(strpos($item['file'],'[') !== false)
4672 // Only expire posts, not photos and photo comments
4674 if($expire_photos==0 && strlen($item['resource-id']))
4676 if($expire_starred==0 && intval($item['starred']))
4678 if($expire_notes==0 && $item['type']=='note')
4680 if($expire_items==0 && $item['type']!='note')
4683 drop_item($item['id'],false);
4686 proc_run('php',"include/notifier.php","expire","$uid");
4691 function drop_items($items) {
4694 if(! local_user() && ! remote_user())
4698 foreach($items as $item) {
4699 $owner = drop_item($item,false);
4700 if($owner && ! $uid)
4705 // multiple threads may have been deleted, send an expire notification
4708 proc_run('php',"include/notifier.php","expire","$uid");
4712 function drop_item($id,$interactive = true) {
4716 // locate item to be deleted
4718 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
4725 notice( t('Item not found.') . EOL);
4726 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
4731 $owner = $item['uid'];
4735 // check if logged in user is either the author or owner of this item
4737 if(is_array($_SESSION['remote'])) {
4738 foreach($_SESSION['remote'] as $visitor) {
4739 if($visitor['uid'] == $item['uid'] && $visitor['cid'] == $item['contact-id']) {
4740 $cid = $visitor['cid'];
4747 if((local_user() == $item['uid']) || ($cid) || (! $interactive)) {
4749 // Check if we should do HTML-based delete confirmation
4750 if($_REQUEST['confirm']) {
4751 // <form> can't take arguments in its "action" parameter
4752 // so add any arguments as hidden inputs
4753 $query = explode_querystring($a->query_string);
4755 foreach($query['args'] as $arg) {
4756 if(strpos($arg, 'confirm=') === false) {
4757 $arg_parts = explode('=', $arg);
4758 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
4762 return replace_macros(get_markup_template('confirm.tpl'), array(
4764 '$message' => t('Do you really want to delete this item?'),
4765 '$extra_inputs' => $inputs,
4766 '$confirm' => t('Yes'),
4767 '$confirm_url' => $query['base'],
4768 '$confirm_name' => 'confirmed',
4769 '$cancel' => t('Cancel'),
4772 // Now check how the user responded to the confirmation query
4773 if($_REQUEST['canceled']) {
4774 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
4777 logger('delete item: ' . $item['id'], LOGGER_DEBUG);
4780 $r = q("UPDATE `item` SET `deleted` = 1, `title` = '', `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d",
4781 dbesc(datetime_convert()),
4782 dbesc(datetime_convert()),
4785 create_tags_from_item($item['id']);
4786 create_files_from_item($item['id']);
4787 delete_thread($item['id'], $item['parent-uri']);
4789 // clean up categories and tags so they don't end up as orphans
4792 $cnt = preg_match_all('/<(.*?)>/',$item['file'],$matches,PREG_SET_ORDER);
4794 foreach($matches as $mtch) {
4795 file_tag_unsave_file($item['uid'],$item['id'],$mtch[1],true);
4801 $cnt = preg_match_all('/\[(.*?)\]/',$item['file'],$matches,PREG_SET_ORDER);
4803 foreach($matches as $mtch) {
4804 file_tag_unsave_file($item['uid'],$item['id'],$mtch[1],false);
4808 // If item is a link to a photo resource, nuke all the associated photos
4809 // (visitors will not have photo resources)
4810 // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
4811 // generate a resource-id and therefore aren't intimately linked to the item.
4813 if(strlen($item['resource-id'])) {
4814 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
4815 dbesc($item['resource-id']),
4816 intval($item['uid'])
4818 // ignore the result
4821 // If item is a link to an event, nuke the event record.
4823 if(intval($item['event-id'])) {
4824 q("DELETE FROM `event` WHERE `id` = %d AND `uid` = %d",
4825 intval($item['event-id']),
4826 intval($item['uid'])
4828 // ignore the result
4831 // If item has attachments, drop them
4833 foreach(explode(",",$item['attach']) as $attach){
4834 preg_match("|attach/(\d+)|", $attach, $matches);
4835 q("DELETE FROM `attach` WHERE `id` = %d AND `uid` = %d",
4836 intval($matches[1]),
4839 // ignore the result
4843 // clean up item_id and sign meta-data tables
4846 // Old code - caused very long queries and warning entries in the mysql logfiles:
4848 $r = q("DELETE FROM item_id where iid in (select id from item where parent = %d and uid = %d)",
4849 intval($item['id']),
4850 intval($item['uid'])
4853 $r = q("DELETE FROM sign where iid in (select id from item where parent = %d and uid = %d)",
4854 intval($item['id']),
4855 intval($item['uid'])
4859 // The new code splits the queries since the mysql optimizer really has bad problems with subqueries
4861 // Creating list of parents
4862 $r = q("select id from item where parent = %d and uid = %d",
4863 intval($item['id']),
4864 intval($item['uid'])
4869 foreach ($r AS $row) {
4870 if ($parentid != "")
4873 $parentid .= $row["id"];
4877 if ($parentid != "") {
4878 $r = q("DELETE FROM item_id where iid in (%s)", dbesc($parentid));
4880 $r = q("DELETE FROM sign where iid in (%s)", dbesc($parentid));
4883 // If it's the parent of a comment thread, kill all the kids
4885 if($item['uri'] == $item['parent-uri']) {
4886 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' , `title` = ''
4887 WHERE `parent-uri` = '%s' AND `uid` = %d ",
4888 dbesc(datetime_convert()),
4889 dbesc(datetime_convert()),
4890 dbesc($item['parent-uri']),
4891 intval($item['uid'])
4893 create_tags_from_itemuri($item['parent-uri'], $item['uid']);
4894 create_files_from_itemuri($item['parent-uri'], $item['uid']);
4895 delete_thread_uri($item['parent-uri'], $item['uid']);
4896 // ignore the result
4899 // ensure that last-child is set in case the comment that had it just got wiped.
4900 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
4901 dbesc(datetime_convert()),
4902 dbesc($item['parent-uri']),
4903 intval($item['uid'])
4905 // who is the last child now?
4906 $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",
4907 dbesc($item['parent-uri']),
4908 intval($item['uid'])
4911 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d",
4916 // Add a relayable_retraction signature for Diaspora.
4917 store_diaspora_retract_sig($item, $a->user, $a->get_baseurl());
4920 $drop_id = intval($item['id']);
4922 // send the notification upstream/downstream as the case may be
4924 proc_run('php',"include/notifier.php","drop","$drop_id");
4928 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
4934 notice( t('Permission denied.') . EOL);
4935 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
4942 function first_post_date($uid,$wall = false) {
4943 $r = q("select id, created from item
4944 where uid = %d and wall = %d and deleted = 0 and visible = 1 AND moderated = 0
4946 order by created asc limit 1",
4948 intval($wall ? 1 : 0)
4951 // logger('first_post_date: ' . $r[0]['id'] . ' ' . $r[0]['created'], LOGGER_DATA);
4952 return substr(datetime_convert('',date_default_timezone_get(),$r[0]['created']),0,10);
4957 /* modified posted_dates() {below} to arrange the list in years */
4958 function list_post_dates($uid, $wall) {
4959 $dnow = datetime_convert('',date_default_timezone_get(),'now','Y-m-d');
4961 $dthen = first_post_date($uid, $wall);
4965 // Set the start and end date to the beginning of the month
4966 $dnow = substr($dnow,0,8).'01';
4967 $dthen = substr($dthen,0,8).'01';
4971 // Starting with the current month, get the first and last days of every
4972 // month down to and including the month of the first post
4973 while(substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
4974 $dyear = intval(substr($dnow,0,4));
4975 $dstart = substr($dnow,0,8) . '01';
4976 $dend = substr($dnow,0,8) . get_dim(intval($dnow),intval(substr($dnow,5)));
4977 $start_month = datetime_convert('','',$dstart,'Y-m-d');
4978 $end_month = datetime_convert('','',$dend,'Y-m-d');
4979 $str = day_translate(datetime_convert('','',$dnow,'F'));
4981 $ret[$dyear] = array();
4982 $ret[$dyear][] = array($str,$end_month,$start_month);
4983 $dnow = datetime_convert('','',$dnow . ' -1 month', 'Y-m-d');
4988 function posted_dates($uid,$wall) {
4989 $dnow = datetime_convert('',date_default_timezone_get(),'now','Y-m-d');
4991 $dthen = first_post_date($uid,$wall);
4995 // Set the start and end date to the beginning of the month
4996 $dnow = substr($dnow,0,8).'01';
4997 $dthen = substr($dthen,0,8).'01';
5000 // Starting with the current month, get the first and last days of every
5001 // month down to and including the month of the first post
5002 while(substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
5003 $dstart = substr($dnow,0,8) . '01';
5004 $dend = substr($dnow,0,8) . get_dim(intval($dnow),intval(substr($dnow,5)));
5005 $start_month = datetime_convert('','',$dstart,'Y-m-d');
5006 $end_month = datetime_convert('','',$dend,'Y-m-d');
5007 $str = day_translate(datetime_convert('','',$dnow,'F Y'));
5008 $ret[] = array($str,$end_month,$start_month);
5009 $dnow = datetime_convert('','',$dnow . ' -1 month', 'Y-m-d');
5015 function posted_date_widget($url,$uid,$wall) {
5018 if(! feature_enabled($uid,'archives'))
5021 // For former Facebook folks that left because of "timeline"
5023 /* if($wall && intval(get_pconfig($uid,'system','no_wall_archive_widget')))
5026 $visible_years = get_pconfig($uid,'system','archive_visible_years');
5027 if(! $visible_years)
5030 $ret = list_post_dates($uid,$wall);
5035 $cutoff_year = intval(datetime_convert('',date_default_timezone_get(),'now','Y')) - $visible_years;
5036 $cutoff = ((array_key_exists($cutoff_year,$ret))? true : false);
5038 $o = replace_macros(get_markup_template('posted_date_widget.tpl'),array(
5039 '$title' => t('Archives'),
5040 '$size' => $visible_years,
5041 '$cutoff_year' => $cutoff_year,
5042 '$cutoff' => $cutoff,
5045 '$showmore' => t('show more')
5051 function store_diaspora_retract_sig($item, $user, $baseurl) {
5052 // Note that we can't add a target_author_signature
5053 // if the comment was deleted by a remote user. That should be ok, because if a remote user is deleting
5054 // the comment, that means we're the home of the post, and Diaspora will only
5055 // check the parent_author_signature of retractions that it doesn't have to relay further
5057 // I don't think this function gets called for an "unlike," but I'll check anyway
5059 $enabled = intval(get_config('system','diaspora_enabled'));
5061 logger('drop_item: diaspora support disabled, not storing retraction signature', LOGGER_DEBUG);
5065 logger('drop_item: storing diaspora retraction signature');
5067 $signed_text = $item['guid'] . ';' . ( ($item['verb'] === ACTIVITY_LIKE) ? 'Like' : 'Comment');
5069 if(local_user() == $item['uid']) {
5071 $handle = $user['nickname'] . '@' . substr($baseurl, strpos($baseurl,'://') + 3);
5072 $authorsig = base64_encode(rsa_sign($signed_text,$user['prvkey'],'sha256'));
5075 $r = q("SELECT `nick`, `url` FROM `contact` WHERE `id` = '%d' LIMIT 1",
5076 $item['contact-id'] // If this function gets called, drop_item() has already checked remote_user() == $item['contact-id']
5079 // The below handle only works for NETWORK_DFRN. I think that's ok, because this function
5080 // only handles DFRN deletes
5081 $handle_baseurl_start = strpos($r['url'],'://') + 3;
5082 $handle_baseurl_length = strpos($r['url'],'/profile') - $handle_baseurl_start;
5083 $handle = $r['nick'] . '@' . substr($r['url'], $handle_baseurl_start, $handle_baseurl_length);
5089 q("insert into sign (`retract_iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
5090 intval($item['id']),
5091 dbesc($signed_text),